120 lines · cpp
1//===-- BreakpointID.cpp --------------------------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#include <cstdio>10#include <optional>11 12#include "lldb/Breakpoint/Breakpoint.h"13#include "lldb/Breakpoint/BreakpointID.h"14#include "lldb/Utility/Status.h"15#include "lldb/Utility/Stream.h"16 17using namespace lldb;18using namespace lldb_private;19 20BreakpointID::BreakpointID(break_id_t bp_id, break_id_t loc_id)21 : m_break_id(bp_id), m_location_id(loc_id) {}22 23BreakpointID::~BreakpointID() = default;24 25static llvm::StringRef g_range_specifiers[] = {"-", "to", "To", "TO"};26 27// Tells whether or not STR is valid to use between two strings representing28// breakpoint IDs, to indicate a range of breakpoint IDs. This is broken out29// into a separate function so that we can easily change or add to the format30// for specifying ID ranges at a later date.31 32bool BreakpointID::IsRangeIdentifier(llvm::StringRef str) {33 return llvm::is_contained(g_range_specifiers, str);34}35 36bool BreakpointID::IsValidIDExpression(llvm::StringRef str) {37 return BreakpointID::ParseCanonicalReference(str).has_value();38}39 40llvm::ArrayRef<llvm::StringRef> BreakpointID::GetRangeSpecifiers() {41 return llvm::ArrayRef(g_range_specifiers);42}43 44void BreakpointID::GetDescription(Stream *s, lldb::DescriptionLevel level) {45 if (level == eDescriptionLevelVerbose)46 s->Printf("%p BreakpointID:", static_cast<void *>(this));47 48 if (m_break_id == LLDB_INVALID_BREAK_ID)49 s->PutCString("<invalid>");50 else if (m_location_id == LLDB_INVALID_BREAK_ID)51 s->Printf("%i", m_break_id);52 else53 s->Printf("%i.%i", m_break_id, m_location_id);54}55 56void BreakpointID::GetCanonicalReference(Stream *s, break_id_t bp_id,57 break_id_t loc_id) {58 if (bp_id == LLDB_INVALID_BREAK_ID)59 s->PutCString("<invalid>");60 else if (loc_id == LLDB_INVALID_BREAK_ID)61 s->Printf("%i", bp_id);62 else63 s->Printf("%i.%i", bp_id, loc_id);64}65 66std::optional<BreakpointID>67BreakpointID::ParseCanonicalReference(llvm::StringRef input) {68 break_id_t bp_id;69 break_id_t loc_id = LLDB_INVALID_BREAK_ID;70 71 if (input.empty())72 return std::nullopt;73 74 // If it doesn't start with an integer, it's not valid.75 if (input.consumeInteger(0, bp_id))76 return std::nullopt;77 78 // period is optional, but if it exists, it must be followed by a number.79 if (input.consume_front(".")) {80 if (input.consumeInteger(0, loc_id))81 return std::nullopt;82 }83 84 // And at the end, the entire string must have been consumed.85 if (!input.empty())86 return std::nullopt;87 88 return BreakpointID(bp_id, loc_id);89}90 91bool BreakpointID::StringIsBreakpointName(llvm::StringRef str, Status &error) {92 error.Clear();93 if (str.empty())94 {95 error = Status::FromErrorString("Empty breakpoint names are not allowed");96 return false;97 }98 99 // First character must be a letter or _100 if (!isalpha(str[0]) && str[0] != '_')101 {102 error =103 Status::FromErrorStringWithFormatv("Breakpoint names must start with a "104 "character or underscore: {0}",105 str);106 return false;107 }108 109 // Cannot contain ., -, or space.110 if (str.find_first_of(".- ") != llvm::StringRef::npos) {111 error =112 Status::FromErrorStringWithFormatv("Breakpoint names cannot contain "113 "'.' or '-' or spaces: \"{0}\"",114 str);115 return false;116 }117 118 return true;119}120