]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Interpreter/OptionValueFormatEntity.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Interpreter / OptionValueFormatEntity.cpp
1 //===-- OptionValueFormatEntity.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 #include "lldb/Interpreter/OptionValueFormatEntity.h"
11
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Module.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/StringList.h"
20 using namespace lldb;
21 using namespace lldb_private;
22
23 OptionValueFormatEntity::OptionValueFormatEntity(const char *default_format)
24     : OptionValue(), m_current_format(), m_default_format(), m_current_entry(),
25       m_default_entry() {
26   if (default_format && default_format[0]) {
27     llvm::StringRef default_format_str(default_format);
28     Status error = FormatEntity::Parse(default_format_str, m_default_entry);
29     if (error.Success()) {
30       m_default_format = default_format;
31       m_current_format = default_format;
32       m_current_entry = m_default_entry;
33     }
34   }
35 }
36
37 bool OptionValueFormatEntity::Clear() {
38   m_current_entry = m_default_entry;
39   m_current_format = m_default_format;
40   m_value_was_set = false;
41   return true;
42 }
43
44 void OptionValueFormatEntity::DumpValue(const ExecutionContext *exe_ctx,
45                                         Stream &strm, uint32_t dump_mask) {
46   if (dump_mask & eDumpOptionType)
47     strm.Printf("(%s)", GetTypeAsCString());
48   if (dump_mask & eDumpOptionValue) {
49     if (dump_mask & eDumpOptionType)
50       strm.PutCString(" = \"");
51     strm << m_current_format.c_str() << '"';
52   }
53 }
54
55 Status OptionValueFormatEntity::SetValueFromString(llvm::StringRef value_str,
56                                                    VarSetOperationType op) {
57   Status error;
58   switch (op) {
59   case eVarSetOperationClear:
60     Clear();
61     NotifyValueChanged();
62     break;
63
64   case eVarSetOperationReplace:
65   case eVarSetOperationAssign: {
66     // Check if the string starts with a quote character after removing leading
67     // and trailing spaces. If it does start with a quote character, make sure
68     // it ends with the same quote character and remove the quotes before we
69     // parse the format string. If the string doesn't start with a quote, leave
70     // the string alone and parse as is.
71     llvm::StringRef trimmed_value_str = value_str.trim();
72     if (!trimmed_value_str.empty()) {
73       const char first_char = trimmed_value_str[0];
74       if (first_char == '"' || first_char == '\'') {
75         const size_t trimmed_len = trimmed_value_str.size();
76         if (trimmed_len == 1 || value_str[trimmed_len - 1] != first_char) {
77           error.SetErrorStringWithFormat("mismatched quotes");
78           return error;
79         }
80         value_str = trimmed_value_str.substr(1, trimmed_len - 2);
81       }
82     }
83     FormatEntity::Entry entry;
84     error = FormatEntity::Parse(value_str, entry);
85     if (error.Success()) {
86       m_current_entry = std::move(entry);
87       m_current_format = value_str;
88       m_value_was_set = true;
89       NotifyValueChanged();
90     }
91   } break;
92
93   case eVarSetOperationInsertBefore:
94   case eVarSetOperationInsertAfter:
95   case eVarSetOperationRemove:
96   case eVarSetOperationAppend:
97   case eVarSetOperationInvalid:
98     error = OptionValue::SetValueFromString(value_str, op);
99     break;
100   }
101   return error;
102 }
103
104 lldb::OptionValueSP OptionValueFormatEntity::DeepCopy() const {
105   return OptionValueSP(new OptionValueFormatEntity(*this));
106 }
107
108 size_t OptionValueFormatEntity::AutoComplete(CommandInterpreter &interpreter,
109                                              CompletionRequest &request) {
110   return FormatEntity::AutoComplete(request);
111 }