1 //===-- UserSettingsController.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 
9 #include "lldb/Core/UserSettingsController.h"
10 
11 #include "lldb/Interpreter/OptionValueProperties.h"
12 #include "lldb/Utility/Status.h"
13 #include "lldb/Utility/Stream.h"
14 
15 #include <memory>
16 
17 namespace lldb_private {
18 class CommandInterpreter;
19 }
20 namespace lldb_private {
21 class ConstString;
22 }
23 namespace lldb_private {
24 class ExecutionContext;
25 }
26 namespace lldb_private {
27 class Property;
28 }
29 
30 using namespace lldb;
31 using namespace lldb_private;
32 
33 lldb::OptionValueSP
34 Properties::GetPropertyValue(const ExecutionContext *exe_ctx,
35                              llvm::StringRef path, Status &error) const {
36   OptionValuePropertiesSP properties_sp(GetValueProperties());
37   if (properties_sp)
38     return properties_sp->GetSubValue(exe_ctx, path, error);
39   return lldb::OptionValueSP();
40 }
41 
42 Status Properties::SetPropertyValue(const ExecutionContext *exe_ctx,
43                                     VarSetOperationType op,
44                                     llvm::StringRef path,
45                                     llvm::StringRef value) {
46   OptionValuePropertiesSP properties_sp(GetValueProperties());
47   if (properties_sp)
48     return properties_sp->SetSubValue(exe_ctx, op, path, value);
49   Status error;
50   error.SetErrorString("no properties");
51   return error;
52 }
53 
54 void Properties::DumpAllPropertyValues(const ExecutionContext *exe_ctx,
55                                        Stream &strm, uint32_t dump_mask,
56                                        bool is_json) {
57   OptionValuePropertiesSP properties_sp(GetValueProperties());
58   if (!properties_sp)
59     return;
60 
61   if (is_json) {
62     llvm::json::Value json = properties_sp->ToJSON(exe_ctx);
63     strm.Printf("%s", llvm::formatv("{0:2}", json).str().c_str());
64   } else
65     properties_sp->DumpValue(exe_ctx, strm, dump_mask);
66 }
67 
68 void Properties::DumpAllDescriptions(CommandInterpreter &interpreter,
69                                      Stream &strm) const {
70   strm.PutCString("Top level variables:\n\n");
71 
72   OptionValuePropertiesSP properties_sp(GetValueProperties());
73   if (properties_sp)
74     return properties_sp->DumpAllDescriptions(interpreter, strm);
75 }
76 
77 Status Properties::DumpPropertyValue(const ExecutionContext *exe_ctx,
78                                      Stream &strm,
79                                      llvm::StringRef property_path,
80                                      uint32_t dump_mask, bool is_json) {
81   OptionValuePropertiesSP properties_sp(GetValueProperties());
82   if (properties_sp) {
83     return properties_sp->DumpPropertyValue(exe_ctx, strm, property_path,
84                                             dump_mask, is_json);
85   }
86   Status error;
87   error.SetErrorString("empty property list");
88   return error;
89 }
90 
91 size_t
92 Properties::Apropos(llvm::StringRef keyword,
93                     std::vector<const Property *> &matching_properties) const {
94   OptionValuePropertiesSP properties_sp(GetValueProperties());
95   if (properties_sp) {
96     properties_sp->Apropos(keyword, matching_properties);
97   }
98   return matching_properties.size();
99 }
100 
101 llvm::StringRef Properties::GetExperimentalSettingsName() {
102   static constexpr llvm::StringLiteral g_experimental("experimental");
103   return g_experimental;
104 }
105 
106 bool Properties::IsSettingExperimental(llvm::StringRef setting) {
107   if (setting.empty())
108     return false;
109 
110   llvm::StringRef experimental = GetExperimentalSettingsName();
111   size_t dot_pos = setting.find_first_of('.');
112   return setting.take_front(dot_pos) == experimental;
113 }
114