1 //===-- BreakpointID.cpp --------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "lldb/Breakpoint/Breakpoint.h"
12 #include "lldb/Breakpoint/BreakpointID.h"
13 #include "lldb/Utility/Status.h"
14 #include "lldb/Utility/Stream.h"
17 using namespace lldb_private;
19 BreakpointID::BreakpointID(break_id_t bp_id, break_id_t loc_id)
20 : m_break_id(bp_id), m_location_id(loc_id) {}
22 BreakpointID::~BreakpointID() = default;
24 static llvm::StringRef g_range_specifiers[] = {"-", "to", "To", "TO"};
26 // Tells whether or not STR is valid to use between two strings representing
27 // breakpoint IDs, to indicate a range of breakpoint IDs. This is broken out
28 // into a separate function so that we can easily change or add to the format
29 // for specifying ID ranges at a later date.
31 bool BreakpointID::IsRangeIdentifier(llvm::StringRef str) {
32 return llvm::is_contained(g_range_specifiers, str);
35 bool BreakpointID::IsValidIDExpression(llvm::StringRef str) {
36 return BreakpointID::ParseCanonicalReference(str).hasValue();
39 llvm::ArrayRef<llvm::StringRef> BreakpointID::GetRangeSpecifiers() {
40 return llvm::makeArrayRef(g_range_specifiers);
43 void BreakpointID::GetDescription(Stream *s, lldb::DescriptionLevel level) {
44 if (level == eDescriptionLevelVerbose)
45 s->Printf("%p BreakpointID:", static_cast<void *>(this));
47 if (m_break_id == LLDB_INVALID_BREAK_ID)
48 s->PutCString("<invalid>");
49 else if (m_location_id == LLDB_INVALID_BREAK_ID)
50 s->Printf("%i", m_break_id);
52 s->Printf("%i.%i", m_break_id, m_location_id);
55 void BreakpointID::GetCanonicalReference(Stream *s, break_id_t bp_id,
57 if (bp_id == LLDB_INVALID_BREAK_ID)
58 s->PutCString("<invalid>");
59 else if (loc_id == LLDB_INVALID_BREAK_ID)
60 s->Printf("%i", bp_id);
62 s->Printf("%i.%i", bp_id, loc_id);
65 llvm::Optional<BreakpointID>
66 BreakpointID::ParseCanonicalReference(llvm::StringRef input) {
68 break_id_t loc_id = LLDB_INVALID_BREAK_ID;
73 // If it doesn't start with an integer, it's not valid.
74 if (input.consumeInteger(0, bp_id))
77 // period is optional, but if it exists, it must be followed by a number.
78 if (input.consume_front(".")) {
79 if (input.consumeInteger(0, loc_id))
83 // And at the end, the entire string must have been consumed.
87 return BreakpointID(bp_id, loc_id);
90 bool BreakpointID::StringIsBreakpointName(llvm::StringRef str, Status &error) {
94 error.SetErrorString("Empty breakpoint names are not allowed");
98 // First character must be a letter or _
99 if (!isalpha(str[0]) && str[0] != '_')
101 error.SetErrorStringWithFormat("Breakpoint names must start with a "
102 "character or underscore: %s",
107 // Cannot contain ., -, or space.
108 if (str.find_first_of(".- ") != llvm::StringRef::npos) {
109 error.SetErrorStringWithFormat("Breakpoint names cannot contain "
110 "'.' or '-' or spaces: \"%s\"",