LLDB  mainline
OptionValueFormatEntity.cpp
Go to the documentation of this file.
1 //===-- OptionValueFormatEntity.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-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  if (default_format && default_format[0]) {
20  llvm::StringRef default_format_str(default_format);
21  Status error = FormatEntity::Parse(default_format_str, m_default_entry);
22  if (error.Success()) {
23  m_default_format = default_format;
24  m_current_format = default_format;
25  m_current_entry = m_default_entry;
26  }
27  }
28 }
29 
30 void OptionValueFormatEntity::Clear() {
31  m_current_entry = m_default_entry;
32  m_current_format = m_default_format;
33  m_value_was_set = false;
34 }
35 
36 static void EscapeBackticks(llvm::StringRef str, std::string &dst) {
37  dst.clear();
38  dst.reserve(str.size());
39 
40  for (size_t i = 0, e = str.size(); i != e; ++i) {
41  char c = str[i];
42  if (c == '`') {
43  if (i == 0 || str[i - 1] != '\\')
44  dst += '\\';
45  }
46  dst += c;
47  }
48 }
49 
51  Stream &strm, uint32_t dump_mask) {
52  if (dump_mask & eDumpOptionType)
53  strm.Printf("(%s)", GetTypeAsCString());
54  if (dump_mask & eDumpOptionValue) {
55  if (dump_mask & eDumpOptionType)
56  strm.PutCString(" = ");
57  std::string escaped;
58  EscapeBackticks(m_current_format, escaped);
59  strm << '"' << escaped << '"';
60  }
61 }
62 
63 Status OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str,
65  Status error;
66  switch (op) {
68  Clear();
69  NotifyValueChanged();
70  break;
71 
74  // Check if the string starts with a quote character after removing leading
75  // and trailing spaces. If it does start with a quote character, make sure
76  // it ends with the same quote character and remove the quotes before we
77  // parse the format string. If the string doesn't start with a quote, leave
78  // the string alone and parse as is.
79  llvm::StringRef trimmed_value_str = value_str.trim();
80  if (!trimmed_value_str.empty()) {
81  const char first_char = trimmed_value_str[0];
82  if (first_char == '"' || first_char == '\'') {
83  const size_t trimmed_len = trimmed_value_str.size();
84  if (trimmed_len == 1 || value_str[trimmed_len - 1] != first_char) {
85  error.SetErrorString("mismatched quotes");
86  return error;
87  }
88  value_str = trimmed_value_str.substr(1, trimmed_len - 2);
89  }
90  }
91  FormatEntity::Entry entry;
92  error = FormatEntity::Parse(value_str, entry);
93  if (error.Success()) {
94  m_current_entry = std::move(entry);
95  m_current_format = std::string(value_str);
96  m_value_was_set = true;
97  NotifyValueChanged();
98  }
99  } break;
100 
106  error = OptionValue::SetValueFromString(value_str, op);
107  break;
108  }
109  return error;
110 }
111 
112 void OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter,
113  CompletionRequest &request) {
114  FormatEntity::AutoComplete(request);
115 }
lldb_private::eVarSetOperationInsertBefore
@ eVarSetOperationInsertBefore
Definition: lldb-private-enumerations.h:85
lldb_private::eVarSetOperationReplace
@ eVarSetOperationReplace
Definition: lldb-private-enumerations.h:84
lldb_private::ExecutionContext
Definition: ExecutionContext.h:291
Module.h
lldb_private::Stream
Definition: Stream.h:28
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::VarSetOperationType
VarSetOperationType
Settable state variable types.
Definition: lldb-private-enumerations.h:83
lldb_private::eVarSetOperationAppend
@ eVarSetOperationAppend
Definition: lldb-private-enumerations.h:88
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:221
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
StringList.h
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::FormatEntity::Entry
Definition: FormatEntity.h:40
OptionValueFormatEntity.h
lldb_private::eVarSetOperationRemove
@ eVarSetOperationRemove
Definition: lldb-private-enumerations.h:87
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
CommandInterpreter.h
lldb_private::eVarSetOperationInvalid
@ eVarSetOperationInvalid
Definition: lldb-private-enumerations.h:91
Stream.h
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
EscapeBackticks
static void EscapeBackticks(llvm::StringRef str, std::string &dst)
Definition: OptionValueFormatEntity.cpp:36
lldb
Definition: SBAddress.h:15
lldb_private::eVarSetOperationClear
@ eVarSetOperationClear
Definition: lldb-private-enumerations.h:89
DumpValue
static bool DumpValue(Stream &s, const SymbolContext *sc, const ExecutionContext *exe_ctx, const FormatEntity::Entry &entry, ValueObject *valobj)
Definition: FormatEntity.cpp:673
lldb_private::CompletionRequest
Definition: CompletionRequest.h:100
lldb_private::eVarSetOperationInsertAfter
@ eVarSetOperationInsertAfter
Definition: lldb-private-enumerations.h:86
lldb_private::eVarSetOperationAssign
@ eVarSetOperationAssign
Definition: lldb-private-enumerations.h:90