]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Breakpoint/BreakpointID.cpp
Vendor import of lldb trunk r338150:
[FreeBSD/FreeBSD.git] / source / Breakpoint / BreakpointID.cpp
1 //===-- BreakpointID.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // C Includes
11 #include <stdio.h>
12
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project 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"
20
21 using namespace lldb;
22 using namespace lldb_private;
23
24 BreakpointID::BreakpointID(break_id_t bp_id, break_id_t loc_id)
25     : m_break_id(bp_id), m_location_id(loc_id) {}
26
27 BreakpointID::~BreakpointID() = default;
28
29 static llvm::StringRef g_range_specifiers[] = {"-", "to", "To", "TO"};
30
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.
35
36 bool BreakpointID::IsRangeIdentifier(llvm::StringRef str) {
37   for (auto spec : g_range_specifiers) {
38     if (spec == str)
39       return true;
40   }
41
42   return false;
43 }
44
45 bool BreakpointID::IsValidIDExpression(llvm::StringRef str) {
46   return BreakpointID::ParseCanonicalReference(str).hasValue();
47 }
48
49 llvm::ArrayRef<llvm::StringRef> BreakpointID::GetRangeSpecifiers() {
50   return llvm::makeArrayRef(g_range_specifiers);
51 }
52
53 void BreakpointID::GetDescription(Stream *s, lldb::DescriptionLevel level) {
54   if (level == eDescriptionLevelVerbose)
55     s->Printf("%p BreakpointID:", static_cast<void *>(this));
56
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);
61   else
62     s->Printf("%i.%i", m_break_id, m_location_id);
63 }
64
65 void BreakpointID::GetCanonicalReference(Stream *s, break_id_t bp_id,
66                                          break_id_t loc_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);
71   else
72     s->Printf("%i.%i", bp_id, loc_id);
73 }
74
75 llvm::Optional<BreakpointID>
76 BreakpointID::ParseCanonicalReference(llvm::StringRef input) {
77   break_id_t bp_id;
78   break_id_t loc_id = LLDB_INVALID_BREAK_ID;
79
80   if (input.empty())
81     return llvm::None;
82
83   // If it doesn't start with an integer, it's not valid.
84   if (input.consumeInteger(0, bp_id))
85     return llvm::None;
86
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))
90       return llvm::None;
91   }
92
93   // And at the end, the entire string must have been consumed.
94   if (!input.empty())
95     return llvm::None;
96
97   return BreakpointID(bp_id, loc_id);
98 }
99
100 bool BreakpointID::StringIsBreakpointName(llvm::StringRef str, Status &error) {
101   error.Clear();
102   if (str.empty())
103   {
104     error.SetErrorStringWithFormat("Empty breakpoint names are not allowed");
105     return false;
106   }
107
108   // First character must be a letter or _
109   if (!isalpha(str[0]) && str[0] != '_')
110   {
111     error.SetErrorStringWithFormat("Breakpoint names must start with a "
112                                    "character or underscore: %s",
113                                    str.str().c_str());
114     return false;
115   }
116
117   // Cannot contain ., -, or space.
118   if (str.find_first_of(".- ") != llvm::StringRef::npos) {
119     error.SetErrorStringWithFormat("Breakpoint names cannot contain "
120                                    "'.' or '-': \"%s\"",
121                                    str.str().c_str());
122     return false;
123   }
124
125   return true;
126 }