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 //===----------------------------------------------------------------------===//
14 // Other libraries and framework includes
16 #include "lldb/Breakpoint/Breakpoint.h"
17 #include "lldb/Breakpoint/BreakpointID.h"
18 #include "lldb/Utility/Status.h"
19 #include "lldb/Utility/Stream.h"
22 using namespace lldb_private;
24 BreakpointID::BreakpointID(break_id_t bp_id, break_id_t loc_id)
25 : m_break_id(bp_id), m_location_id(loc_id) {}
27 BreakpointID::~BreakpointID() = default;
29 static llvm::StringRef g_range_specifiers[] = {"-", "to", "To", "TO"};
31 // Tells whether or not STR is valid to use between two strings representing
32 // breakpoint IDs, to indicate a range of breakpoint IDs. This is broken out
33 // into a separate function so that we can easily change or add to the format
34 // for specifying ID ranges at a later date.
36 bool BreakpointID::IsRangeIdentifier(llvm::StringRef str) {
37 for (auto spec : g_range_specifiers) {
45 bool BreakpointID::IsValidIDExpression(llvm::StringRef str) {
46 return BreakpointID::ParseCanonicalReference(str).hasValue();
49 llvm::ArrayRef<llvm::StringRef> BreakpointID::GetRangeSpecifiers() {
50 return llvm::makeArrayRef(g_range_specifiers);
53 void BreakpointID::GetDescription(Stream *s, lldb::DescriptionLevel level) {
54 if (level == eDescriptionLevelVerbose)
55 s->Printf("%p BreakpointID:", static_cast<void *>(this));
57 if (m_break_id == LLDB_INVALID_BREAK_ID)
58 s->PutCString("<invalid>");
59 else if (m_location_id == LLDB_INVALID_BREAK_ID)
60 s->Printf("%i", m_break_id);
62 s->Printf("%i.%i", m_break_id, m_location_id);
65 void BreakpointID::GetCanonicalReference(Stream *s, break_id_t bp_id,
67 if (bp_id == LLDB_INVALID_BREAK_ID)
68 s->PutCString("<invalid>");
69 else if (loc_id == LLDB_INVALID_BREAK_ID)
70 s->Printf("%i", bp_id);
72 s->Printf("%i.%i", bp_id, loc_id);
75 llvm::Optional<BreakpointID>
76 BreakpointID::ParseCanonicalReference(llvm::StringRef input) {
78 break_id_t loc_id = LLDB_INVALID_BREAK_ID;
83 // If it doesn't start with an integer, it's not valid.
84 if (input.consumeInteger(0, bp_id))
87 // period is optional, but if it exists, it must be followed by a number.
88 if (input.consume_front(".")) {
89 if (input.consumeInteger(0, loc_id))
93 // And at the end, the entire string must have been consumed.
97 return BreakpointID(bp_id, loc_id);
100 bool BreakpointID::StringIsBreakpointName(llvm::StringRef str, Status &error) {
104 error.SetErrorStringWithFormat("Empty breakpoint names are not allowed");
108 // First character must be a letter or _
109 if (!isalpha(str[0]) && str[0] != '_')
111 error.SetErrorStringWithFormat("Breakpoint names must start with a "
112 "character or underscore: %s",
117 // Cannot contain ., -, or space.
118 if (str.find_first_of(".- ") != llvm::StringRef::npos) {
119 error.SetErrorStringWithFormat("Breakpoint names cannot contain "
120 "'.' or '-': \"%s\"",