xref: /src/contrib/llvm-project/lldb/source/Core/UserSettingsController.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1cfca06d7SDimitry Andric //===-- UserSettingsController.cpp ----------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
914f1b3e8SDimitry Andric #include "lldb/Core/UserSettingsController.h"
1074a628f7SDimitry Andric 
115e95aa85SEd Maste #include "lldb/Interpreter/OptionValueProperties.h"
12b76161e4SDimitry Andric #include "lldb/Utility/Status.h"
1374a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
1474a628f7SDimitry Andric 
1594994d37SDimitry Andric #include <memory>
1674a628f7SDimitry Andric 
1774a628f7SDimitry Andric namespace lldb_private {
1874a628f7SDimitry Andric class CommandInterpreter;
1974a628f7SDimitry Andric }
2074a628f7SDimitry Andric namespace lldb_private {
2174a628f7SDimitry Andric class ConstString;
2274a628f7SDimitry Andric }
2374a628f7SDimitry Andric namespace lldb_private {
2474a628f7SDimitry Andric class ExecutionContext;
2574a628f7SDimitry Andric }
2674a628f7SDimitry Andric namespace lldb_private {
2774a628f7SDimitry Andric class Property;
2874a628f7SDimitry Andric }
29f034231aSEd Maste 
30f034231aSEd Maste using namespace lldb;
31f034231aSEd Maste using namespace lldb_private;
32f034231aSEd Maste 
33b1c73532SDimitry Andric Properties::Properties() = default;
34b1c73532SDimitry Andric 
Properties(const lldb::OptionValuePropertiesSP & collection_sp)35b1c73532SDimitry Andric Properties::Properties(const lldb::OptionValuePropertiesSP &collection_sp)
36b1c73532SDimitry Andric     : m_collection_sp(collection_sp) {}
37b1c73532SDimitry Andric 
38b1c73532SDimitry Andric Properties::~Properties() = default;
39b1c73532SDimitry Andric 
40f034231aSEd Maste lldb::OptionValueSP
GetPropertyValue(const ExecutionContext * exe_ctx,llvm::StringRef path,Status & error) const41b76161e4SDimitry Andric Properties::GetPropertyValue(const ExecutionContext *exe_ctx,
427fa27ce4SDimitry Andric                              llvm::StringRef path, Status &error) const {
43f034231aSEd Maste   OptionValuePropertiesSP properties_sp(GetValueProperties());
44f034231aSEd Maste   if (properties_sp)
457fa27ce4SDimitry Andric     return properties_sp->GetSubValue(exe_ctx, path, error);
46f034231aSEd Maste   return lldb::OptionValueSP();
47f034231aSEd Maste }
48f034231aSEd Maste 
SetPropertyValue(const ExecutionContext * exe_ctx,VarSetOperationType op,llvm::StringRef path,llvm::StringRef value)49b76161e4SDimitry Andric Status Properties::SetPropertyValue(const ExecutionContext *exe_ctx,
50b76161e4SDimitry Andric                                     VarSetOperationType op,
51b76161e4SDimitry Andric                                     llvm::StringRef path,
5214f1b3e8SDimitry Andric                                     llvm::StringRef value) {
53f034231aSEd Maste   OptionValuePropertiesSP properties_sp(GetValueProperties());
54f034231aSEd Maste   if (properties_sp)
55f034231aSEd Maste     return properties_sp->SetSubValue(exe_ctx, op, path, value);
56b76161e4SDimitry Andric   Status error;
57f034231aSEd Maste   error.SetErrorString("no properties");
58f034231aSEd Maste   return error;
59f034231aSEd Maste }
60f034231aSEd Maste 
DumpAllPropertyValues(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask,bool is_json)6114f1b3e8SDimitry Andric void Properties::DumpAllPropertyValues(const ExecutionContext *exe_ctx,
62e3b55780SDimitry Andric                                        Stream &strm, uint32_t dump_mask,
63e3b55780SDimitry Andric                                        bool is_json) {
64f034231aSEd Maste   OptionValuePropertiesSP properties_sp(GetValueProperties());
65e3b55780SDimitry Andric   if (!properties_sp)
66e3b55780SDimitry Andric     return;
67e3b55780SDimitry Andric 
68e3b55780SDimitry Andric   if (is_json) {
69e3b55780SDimitry Andric     llvm::json::Value json = properties_sp->ToJSON(exe_ctx);
70e3b55780SDimitry Andric     strm.Printf("%s", llvm::formatv("{0:2}", json).str().c_str());
71e3b55780SDimitry Andric   } else
72e3b55780SDimitry Andric     properties_sp->DumpValue(exe_ctx, strm, dump_mask);
73f034231aSEd Maste }
74f034231aSEd Maste 
DumpAllDescriptions(CommandInterpreter & interpreter,Stream & strm) const7514f1b3e8SDimitry Andric void Properties::DumpAllDescriptions(CommandInterpreter &interpreter,
7614f1b3e8SDimitry Andric                                      Stream &strm) const {
77f034231aSEd Maste   strm.PutCString("Top level variables:\n\n");
78f034231aSEd Maste 
79f034231aSEd Maste   OptionValuePropertiesSP properties_sp(GetValueProperties());
80f034231aSEd Maste   if (properties_sp)
81f034231aSEd Maste     return properties_sp->DumpAllDescriptions(interpreter, strm);
82f034231aSEd Maste }
83f034231aSEd Maste 
DumpPropertyValue(const ExecutionContext * exe_ctx,Stream & strm,llvm::StringRef property_path,uint32_t dump_mask,bool is_json)84b76161e4SDimitry Andric Status Properties::DumpPropertyValue(const ExecutionContext *exe_ctx,
85b76161e4SDimitry Andric                                      Stream &strm,
86b76161e4SDimitry Andric                                      llvm::StringRef property_path,
87e3b55780SDimitry Andric                                      uint32_t dump_mask, bool is_json) {
88f034231aSEd Maste   OptionValuePropertiesSP properties_sp(GetValueProperties());
8914f1b3e8SDimitry Andric   if (properties_sp) {
9014f1b3e8SDimitry Andric     return properties_sp->DumpPropertyValue(exe_ctx, strm, property_path,
91e3b55780SDimitry Andric                                             dump_mask, is_json);
92f034231aSEd Maste   }
93b76161e4SDimitry Andric   Status error;
94f034231aSEd Maste   error.SetErrorString("empty property list");
95f034231aSEd Maste   return error;
96f034231aSEd Maste }
97f034231aSEd Maste 
98f034231aSEd Maste size_t
Apropos(llvm::StringRef keyword,std::vector<const Property * > & matching_properties) const9914f1b3e8SDimitry Andric Properties::Apropos(llvm::StringRef keyword,
10014f1b3e8SDimitry Andric                     std::vector<const Property *> &matching_properties) const {
101f034231aSEd Maste   OptionValuePropertiesSP properties_sp(GetValueProperties());
10214f1b3e8SDimitry Andric   if (properties_sp) {
103f034231aSEd Maste     properties_sp->Apropos(keyword, matching_properties);
104f034231aSEd Maste   }
105f034231aSEd Maste   return matching_properties.size();
106f034231aSEd Maste }
107f034231aSEd Maste 
GetExperimentalSettingsName()1087fa27ce4SDimitry Andric llvm::StringRef Properties::GetExperimentalSettingsName() {
1097fa27ce4SDimitry Andric   static constexpr llvm::StringLiteral g_experimental("experimental");
1107fa27ce4SDimitry Andric   return g_experimental;
111f034231aSEd Maste }
112f034231aSEd Maste 
IsSettingExperimental(llvm::StringRef setting)11314f1b3e8SDimitry Andric bool Properties::IsSettingExperimental(llvm::StringRef setting) {
11414f1b3e8SDimitry Andric   if (setting.empty())
115f3fbd1c0SDimitry Andric     return false;
116f3fbd1c0SDimitry Andric 
11714f1b3e8SDimitry Andric   llvm::StringRef experimental = GetExperimentalSettingsName();
11814f1b3e8SDimitry Andric   size_t dot_pos = setting.find_first_of('.');
11914f1b3e8SDimitry Andric   return setting.take_front(dot_pos) == experimental;
120f3fbd1c0SDimitry Andric }
121