LLDB  mainline
OptionValueFormatEntity.cpp
Go to the documentation of this file.
1 //===-- OptionValueFormatEntity.cpp -----------------------------*- C++ -*-===//
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include "lldb/Core/Module.h"
13 #include "lldb/Utility/Stream.h"
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 OptionValueFormatEntity::OptionValueFormatEntity(const char *default_format)
19  : OptionValue(), m_current_format(), m_default_format(), m_current_entry(),
20  m_default_entry() {
21  if (default_format && default_format[0]) {
22  llvm::StringRef default_format_str(default_format);
23  Status error = FormatEntity::Parse(default_format_str, m_default_entry);
24  if (error.Success()) {
25  m_default_format = default_format;
26  m_current_format = default_format;
28  }
29  }
30 }
31 
35  m_value_was_set = false;
36  return true;
37 }
38 
39 static void EscapeBackticks(llvm::StringRef str, std::string &dst) {
40  dst.clear();
41  dst.reserve(str.size());
42 
43  for (size_t i = 0, e = str.size(); i != e; ++i) {
44  char c = str[i];
45  if (c == '`') {
46  if (i == 0 || str[i - 1] != '\\')
47  dst += '\\';
48  }
49  dst += c;
50  }
51 }
52 
54  Stream &strm, uint32_t dump_mask) {
55  if (dump_mask & eDumpOptionType)
56  strm.Printf("(%s)", GetTypeAsCString());
57  if (dump_mask & eDumpOptionValue) {
58  if (dump_mask & eDumpOptionType)
59  strm.PutCString(" = ");
60  std::string escaped;
62  strm << '"' << escaped << '"';
63  }
64 }
65 
68  Status error;
69  switch (op) {
71  Clear();
73  break;
74 
77  // Check if the string starts with a quote character after removing leading
78  // and trailing spaces. If it does start with a quote character, make sure
79  // it ends with the same quote character and remove the quotes before we
80  // parse the format string. If the string doesn't start with a quote, leave
81  // the string alone and parse as is.
82  llvm::StringRef trimmed_value_str = value_str.trim();
83  if (!trimmed_value_str.empty()) {
84  const char first_char = trimmed_value_str[0];
85  if (first_char == '"' || first_char == '\'') {
86  const size_t trimmed_len = trimmed_value_str.size();
87  if (trimmed_len == 1 || value_str[trimmed_len - 1] != first_char) {
88  error.SetErrorStringWithFormat("mismatched quotes");
89  return error;
90  }
91  value_str = trimmed_value_str.substr(1, trimmed_len - 2);
92  }
93  }
94  FormatEntity::Entry entry;
95  error = FormatEntity::Parse(value_str, entry);
96  if (error.Success()) {
97  m_current_entry = std::move(entry);
98  m_current_format = value_str;
99  m_value_was_set = true;
101  }
102  } break;
103 
109  error = OptionValue::SetValueFromString(value_str, op);
110  break;
111  }
112  return error;
113 }
114 
115 lldb::OptionValueSP OptionValueFormatEntity::DeepCopy() const {
116  return OptionValueSP(new OptionValueFormatEntity(*this));
117 }
118 
120  CompletionRequest &request) {
121  return FormatEntity::AutoComplete(request);
122 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
OptionValueFormatEntity(const char *default_format)
static Status Parse(const llvm::StringRef &format, Entry &entry)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
virtual Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign)
Status SetValueFromString(llvm::StringRef value, VarSetOperationType op=eVarSetOperationAssign) override
"lldb/Utility/ArgCompletionRequest.h"
VarSetOperationType
Settable state variable types.
static size_t AutoComplete(lldb_private::CompletionRequest &request)
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
bool Success() const
Test for success condition.
Definition: Status.cpp:287
void DumpValue(const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) override
size_t AutoComplete(CommandInterpreter &interpreter, CompletionRequest &request) override
virtual const char * GetTypeAsCString() const
Definition: OptionValue.h:79
Definition: SBAddress.h:15
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
static void EscapeBackticks(llvm::StringRef str, std::string &dst)
lldb::OptionValueSP DeepCopy() const override
An error handling class.
Definition: Status.h:44