1 //===-- BreakpointID.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "lldb/Breakpoint/Breakpoint.h"
13 #include "lldb/Breakpoint/BreakpointID.h"
14 #include "lldb/Utility/Status.h"
15 #include "lldb/Utility/Stream.h"
18 using namespace lldb_private;
20 BreakpointID::BreakpointID(break_id_t bp_id, break_id_t loc_id)
21 : m_break_id(bp_id), m_location_id(loc_id) {}
23 BreakpointID::~BreakpointID() = default;
25 static llvm::StringRef g_range_specifiers[] = {"-", "to", "To", "TO"};
27 // Tells whether or not STR is valid to use between two strings representing
28 // breakpoint IDs, to indicate a range of breakpoint IDs. This is broken out
29 // into a separate function so that we can easily change or add to the format
30 // for specifying ID ranges at a later date.
32 bool BreakpointID::IsRangeIdentifier(llvm::StringRef str) {
33 for (auto spec : g_range_specifiers) {
41 bool BreakpointID::IsValidIDExpression(llvm::StringRef str) {
42 return BreakpointID::ParseCanonicalReference(str).hasValue();
45 llvm::ArrayRef<llvm::StringRef> BreakpointID::GetRangeSpecifiers() {
46 return llvm::makeArrayRef(g_range_specifiers);
49 void BreakpointID::GetDescription(Stream *s, lldb::DescriptionLevel level) {
50 if (level == eDescriptionLevelVerbose)
51 s->Printf("%p BreakpointID:", static_cast<void *>(this));
53 if (m_break_id == LLDB_INVALID_BREAK_ID)
54 s->PutCString("<invalid>");
55 else if (m_location_id == LLDB_INVALID_BREAK_ID)
56 s->Printf("%i", m_break_id);
58 s->Printf("%i.%i", m_break_id, m_location_id);
61 void BreakpointID::GetCanonicalReference(Stream *s, break_id_t bp_id,
63 if (bp_id == LLDB_INVALID_BREAK_ID)
64 s->PutCString("<invalid>");
65 else if (loc_id == LLDB_INVALID_BREAK_ID)
66 s->Printf("%i", bp_id);
68 s->Printf("%i.%i", bp_id, loc_id);
71 llvm::Optional<BreakpointID>
72 BreakpointID::ParseCanonicalReference(llvm::StringRef input) {
74 break_id_t loc_id = LLDB_INVALID_BREAK_ID;
79 // If it doesn't start with an integer, it's not valid.
80 if (input.consumeInteger(0, bp_id))
83 // period is optional, but if it exists, it must be followed by a number.
84 if (input.consume_front(".")) {
85 if (input.consumeInteger(0, loc_id))
89 // And at the end, the entire string must have been consumed.
93 return BreakpointID(bp_id, loc_id);
96 bool BreakpointID::StringIsBreakpointName(llvm::StringRef str, Status &error) {
100 error.SetErrorStringWithFormat("Empty breakpoint names are not allowed");
104 // First character must be a letter or _
105 if (!isalpha(str[0]) && str[0] != '_')
107 error.SetErrorStringWithFormat("Breakpoint names must start with a "
108 "character or underscore: %s",
113 // Cannot contain ., -, or space.
114 if (str.find_first_of(".- ") != llvm::StringRef::npos) {
115 error.SetErrorStringWithFormat("Breakpoint names cannot contain "
116 "'.' or '-': \"%s\"",