180814287SRaphael Isemann //===-- CommandCompletions.cpp --------------------------------------------===//
2c9af18a5SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c9af18a5SGreg Clayton //
7c9af18a5SGreg Clayton //===----------------------------------------------------------------------===//
8c9af18a5SGreg Clayton 
9c8ecc2a9SEugene Zelenko #include "llvm/ADT/SmallString.h"
102cc5a18dSZachary Turner #include "llvm/ADT/StringSet.h"
11c8ecc2a9SEugene Zelenko 
124f3559dbSGongyu Deng #include "lldb/Breakpoint/Watchpoint.h"
139b62fd2fSGreg Clayton #include "lldb/Core/FileSpecList.h"
141f746071SGreg Clayton #include "lldb/Core/Module.h"
15190fadcdSZachary Turner #include "lldb/Core/PluginManager.h"
16190fadcdSZachary Turner #include "lldb/Host/FileSystem.h"
179b62fd2fSGreg Clayton #include "lldb/Interpreter/CommandCompletions.h"
189b62fd2fSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
19633a29cfSZachary Turner #include "lldb/Interpreter/OptionValueProperties.h"
201f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
21f21feadcSGreg Clayton #include "lldb/Symbol/Variable.h"
223ce57e01SGongyu Deng #include "lldb/Target/Language.h"
23a952fe23SGongyu Deng #include "lldb/Target/Process.h"
24a14f4a75SGongyu Deng #include "lldb/Target/RegisterContext.h"
2566fa73faSGongyu Deng #include "lldb/Target/Thread.h"
265713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
272f3df613SZachary Turner #include "lldb/Utility/StreamString.h"
282cc5a18dSZachary Turner #include "lldb/Utility/TildeExpressionResolver.h"
29c9af18a5SGreg Clayton 
307d86ee5aSZachary Turner #include "llvm/Support/FileSystem.h"
312cc5a18dSZachary Turner #include "llvm/Support/Path.h"
32190fadcdSZachary Turner 
33c9af18a5SGreg Clayton using namespace lldb_private;
34c9af18a5SGreg Clayton 
35440460f1SRaphael Isemann // This is the command completion callback that is used to complete the
36440460f1SRaphael Isemann // argument of the option it is bound to (in the OptionDefinition table
37440460f1SRaphael Isemann // below).
38440460f1SRaphael Isemann typedef void (*CompletionCallback)(CommandInterpreter &interpreter,
39440460f1SRaphael Isemann                                    CompletionRequest &request,
40440460f1SRaphael Isemann                                    // A search filter to limit the search...
41440460f1SRaphael Isemann                                    lldb_private::SearchFilter *searcher);
42440460f1SRaphael Isemann 
43440460f1SRaphael Isemann struct CommonCompletionElement {
44440460f1SRaphael Isemann   uint32_t type;
45440460f1SRaphael Isemann   CompletionCallback callback;
46440460f1SRaphael Isemann };
47440460f1SRaphael Isemann 
48440460f1SRaphael Isemann bool CommandCompletions::InvokeCommonCompletionCallbacks(
49440460f1SRaphael Isemann     CommandInterpreter &interpreter, uint32_t completion_mask,
50440460f1SRaphael Isemann     CompletionRequest &request, SearchFilter *searcher) {
51440460f1SRaphael Isemann   bool handled = false;
52440460f1SRaphael Isemann 
53440460f1SRaphael Isemann   const CommonCompletionElement common_completions[] = {
54c9af18a5SGreg Clayton       {eSourceFileCompletion, CommandCompletions::SourceFiles},
55558ce124SJim Ingham       {eDiskFileCompletion, CommandCompletions::DiskFiles},
56558ce124SJim Ingham       {eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
57c9af18a5SGreg Clayton       {eSymbolCompletion, CommandCompletions::Symbols},
58c9af18a5SGreg Clayton       {eModuleCompletion, CommandCompletions::Modules},
59419f1be7SGongyu Deng       {eModuleUUIDCompletion, CommandCompletions::ModuleUUIDs},
603df9a8dfSCaroline Tice       {eSettingsNameCompletion, CommandCompletions::SettingsNames},
61ab65b34fSGreg Clayton       {ePlatformPluginCompletion, CommandCompletions::PlatformPluginNames},
62ab65b34fSGreg Clayton       {eArchitectureCompletion, CommandCompletions::ArchitectureNames},
63f21feadcSGreg Clayton       {eVariablePathCompletion, CommandCompletions::VariablePath},
64a14f4a75SGongyu Deng       {eRegisterCompletion, CommandCompletions::Registers},
650e50b9a4SGongyu Deng       {eBreakpointCompletion, CommandCompletions::Breakpoints},
66763bc230SGongyu Deng       {eProcessPluginCompletion, CommandCompletions::ProcessPluginNames},
678a5e2969SGongyu Deng       {eDisassemblyFlavorCompletion, CommandCompletions::DisassemblyFlavors},
683ce57e01SGongyu Deng       {eTypeLanguageCompletion, CommandCompletions::TypeLanguages},
6966fa73faSGongyu Deng       {eFrameIndexCompletion, CommandCompletions::FrameIndexes},
70b2b7dbb4SGongyu Deng       {eStopHookIDCompletion, CommandCompletions::StopHookIDs},
71a952fe23SGongyu Deng       {eThreadIndexCompletion, CommandCompletions::ThreadIndexes},
724f3559dbSGongyu Deng       {eWatchPointIDCompletion, CommandCompletions::WatchPointIDs},
7322e63cbaSGongyu Deng       {eBreakpointNameCompletion, CommandCompletions::BreakpointNames},
74*19311f5cSGongyu Deng       {eProcessIDCompletion, CommandCompletions::ProcessIDs},
75*19311f5cSGongyu Deng       {eProcessNameCompletion, CommandCompletions::ProcessNames},
76c8ecc2a9SEugene Zelenko       {eNoCompletion, nullptr} // This one has to be last in the list.
77c9af18a5SGreg Clayton   };
78c9af18a5SGreg Clayton 
79b9c1b51eSKate Stone   for (int i = 0;; i++) {
80440460f1SRaphael Isemann     if (common_completions[i].type == eNoCompletion)
81c9af18a5SGreg Clayton       break;
82440460f1SRaphael Isemann     else if ((common_completions[i].type & completion_mask) ==
83440460f1SRaphael Isemann                  common_completions[i].type &&
84440460f1SRaphael Isemann              common_completions[i].callback != nullptr) {
85c9af18a5SGreg Clayton       handled = true;
86440460f1SRaphael Isemann       common_completions[i].callback(interpreter, request, searcher);
87c9af18a5SGreg Clayton     }
88c9af18a5SGreg Clayton   }
89c9af18a5SGreg Clayton   return handled;
90c9af18a5SGreg Clayton }
91c9af18a5SGreg Clayton 
92572fc897SRaphael Isemann namespace {
93572fc897SRaphael Isemann // The Completer class is a convenient base class for building searchers that
94572fc897SRaphael Isemann // go along with the SearchFilter passed to the standard Completer functions.
95572fc897SRaphael Isemann class Completer : public Searcher {
96572fc897SRaphael Isemann public:
97572fc897SRaphael Isemann   Completer(CommandInterpreter &interpreter, CompletionRequest &request)
98572fc897SRaphael Isemann       : m_interpreter(interpreter), m_request(request) {}
99572fc897SRaphael Isemann 
100572fc897SRaphael Isemann   ~Completer() override = default;
101572fc897SRaphael Isemann 
102572fc897SRaphael Isemann   CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context,
103572fc897SRaphael Isemann                                 Address *addr) override = 0;
104572fc897SRaphael Isemann 
105572fc897SRaphael Isemann   lldb::SearchDepth GetDepth() override = 0;
106572fc897SRaphael Isemann 
107572fc897SRaphael Isemann   virtual void DoCompletion(SearchFilter *filter) = 0;
108572fc897SRaphael Isemann 
109572fc897SRaphael Isemann protected:
110572fc897SRaphael Isemann   CommandInterpreter &m_interpreter;
111572fc897SRaphael Isemann   CompletionRequest &m_request;
112572fc897SRaphael Isemann 
113572fc897SRaphael Isemann private:
114eaebcbc6SKonrad Kleine   Completer(const Completer &) = delete;
115eaebcbc6SKonrad Kleine   const Completer &operator=(const Completer &) = delete;
116572fc897SRaphael Isemann };
117572fc897SRaphael Isemann } // namespace
118572fc897SRaphael Isemann 
119572fc897SRaphael Isemann // SourceFileCompleter implements the source file completer
120572fc897SRaphael Isemann namespace {
121572fc897SRaphael Isemann class SourceFileCompleter : public Completer {
122572fc897SRaphael Isemann public:
123572fc897SRaphael Isemann   SourceFileCompleter(CommandInterpreter &interpreter,
124572fc897SRaphael Isemann                       CompletionRequest &request)
125572fc897SRaphael Isemann       : Completer(interpreter, request), m_matching_files() {
126572fc897SRaphael Isemann     FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
127572fc897SRaphael Isemann     m_file_name = partial_spec.GetFilename().GetCString();
128572fc897SRaphael Isemann     m_dir_name = partial_spec.GetDirectory().GetCString();
129572fc897SRaphael Isemann   }
130572fc897SRaphael Isemann 
131572fc897SRaphael Isemann   lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthCompUnit; }
132572fc897SRaphael Isemann 
133572fc897SRaphael Isemann   Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
134572fc897SRaphael Isemann                                           SymbolContext &context,
135572fc897SRaphael Isemann                                           Address *addr) override {
136572fc897SRaphael Isemann     if (context.comp_unit != nullptr) {
137572fc897SRaphael Isemann       const char *cur_file_name =
138572fc897SRaphael Isemann           context.comp_unit->GetPrimaryFile().GetFilename().GetCString();
139572fc897SRaphael Isemann       const char *cur_dir_name =
140572fc897SRaphael Isemann           context.comp_unit->GetPrimaryFile().GetDirectory().GetCString();
141572fc897SRaphael Isemann 
142572fc897SRaphael Isemann       bool match = false;
143572fc897SRaphael Isemann       if (m_file_name && cur_file_name &&
144572fc897SRaphael Isemann           strstr(cur_file_name, m_file_name) == cur_file_name)
145572fc897SRaphael Isemann         match = true;
146572fc897SRaphael Isemann 
147572fc897SRaphael Isemann       if (match && m_dir_name && cur_dir_name &&
148572fc897SRaphael Isemann           strstr(cur_dir_name, m_dir_name) != cur_dir_name)
149572fc897SRaphael Isemann         match = false;
150572fc897SRaphael Isemann 
151572fc897SRaphael Isemann       if (match) {
152572fc897SRaphael Isemann         m_matching_files.AppendIfUnique(context.comp_unit->GetPrimaryFile());
153572fc897SRaphael Isemann       }
154572fc897SRaphael Isemann     }
155572fc897SRaphael Isemann     return Searcher::eCallbackReturnContinue;
156572fc897SRaphael Isemann   }
157572fc897SRaphael Isemann 
158572fc897SRaphael Isemann   void DoCompletion(SearchFilter *filter) override {
159572fc897SRaphael Isemann     filter->Search(*this);
160572fc897SRaphael Isemann     // Now convert the filelist to completions:
161572fc897SRaphael Isemann     for (size_t i = 0; i < m_matching_files.GetSize(); i++) {
162572fc897SRaphael Isemann       m_request.AddCompletion(
163572fc897SRaphael Isemann           m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
164572fc897SRaphael Isemann     }
165572fc897SRaphael Isemann   }
166572fc897SRaphael Isemann 
167572fc897SRaphael Isemann private:
168572fc897SRaphael Isemann   FileSpecList m_matching_files;
169572fc897SRaphael Isemann   const char *m_file_name;
170572fc897SRaphael Isemann   const char *m_dir_name;
171572fc897SRaphael Isemann 
172eaebcbc6SKonrad Kleine   SourceFileCompleter(const SourceFileCompleter &) = delete;
173eaebcbc6SKonrad Kleine   const SourceFileCompleter &operator=(const SourceFileCompleter &) = delete;
174572fc897SRaphael Isemann };
175572fc897SRaphael Isemann } // namespace
176572fc897SRaphael Isemann 
177572fc897SRaphael Isemann static bool regex_chars(const char comp) {
178572fc897SRaphael Isemann   return llvm::StringRef("[](){}+.*|^$\\?").contains(comp);
179572fc897SRaphael Isemann }
180572fc897SRaphael Isemann 
181572fc897SRaphael Isemann namespace {
182572fc897SRaphael Isemann class SymbolCompleter : public Completer {
183572fc897SRaphael Isemann 
184572fc897SRaphael Isemann public:
185572fc897SRaphael Isemann   SymbolCompleter(CommandInterpreter &interpreter, CompletionRequest &request)
186572fc897SRaphael Isemann       : Completer(interpreter, request) {
187572fc897SRaphael Isemann     std::string regex_str;
188572fc897SRaphael Isemann     if (!m_request.GetCursorArgumentPrefix().empty()) {
189572fc897SRaphael Isemann       regex_str.append("^");
190572fc897SRaphael Isemann       regex_str.append(std::string(m_request.GetCursorArgumentPrefix()));
191572fc897SRaphael Isemann     } else {
192572fc897SRaphael Isemann       // Match anything since the completion string is empty
193572fc897SRaphael Isemann       regex_str.append(".");
194572fc897SRaphael Isemann     }
195572fc897SRaphael Isemann     std::string::iterator pos =
196572fc897SRaphael Isemann         find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
197572fc897SRaphael Isemann     while (pos < regex_str.end()) {
198572fc897SRaphael Isemann       pos = regex_str.insert(pos, '\\');
199572fc897SRaphael Isemann       pos = find_if(pos + 2, regex_str.end(), regex_chars);
200572fc897SRaphael Isemann     }
201572fc897SRaphael Isemann     m_regex = RegularExpression(regex_str);
202572fc897SRaphael Isemann   }
203572fc897SRaphael Isemann 
204572fc897SRaphael Isemann   lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; }
205572fc897SRaphael Isemann 
206572fc897SRaphael Isemann   Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
207572fc897SRaphael Isemann                                           SymbolContext &context,
208572fc897SRaphael Isemann                                           Address *addr) override {
209572fc897SRaphael Isemann     if (context.module_sp) {
210572fc897SRaphael Isemann       SymbolContextList sc_list;
211572fc897SRaphael Isemann       const bool include_symbols = true;
212572fc897SRaphael Isemann       const bool include_inlines = true;
213572fc897SRaphael Isemann       context.module_sp->FindFunctions(m_regex, include_symbols,
214572fc897SRaphael Isemann                                        include_inlines, sc_list);
215572fc897SRaphael Isemann 
216572fc897SRaphael Isemann       SymbolContext sc;
217572fc897SRaphael Isemann       // Now add the functions & symbols to the list - only add if unique:
218572fc897SRaphael Isemann       for (uint32_t i = 0; i < sc_list.GetSize(); i++) {
219572fc897SRaphael Isemann         if (sc_list.GetContextAtIndex(i, sc)) {
220572fc897SRaphael Isemann           ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
221572fc897SRaphael Isemann           // Ensure that the function name matches the regex. This is more than
222572fc897SRaphael Isemann           // a sanity check. It is possible that the demangled function name
223572fc897SRaphael Isemann           // does not start with the prefix, for example when it's in an
224572fc897SRaphael Isemann           // anonymous namespace.
225572fc897SRaphael Isemann           if (!func_name.IsEmpty() && m_regex.Execute(func_name.GetStringRef()))
226572fc897SRaphael Isemann             m_match_set.insert(func_name);
227572fc897SRaphael Isemann         }
228572fc897SRaphael Isemann       }
229572fc897SRaphael Isemann     }
230572fc897SRaphael Isemann     return Searcher::eCallbackReturnContinue;
231572fc897SRaphael Isemann   }
232572fc897SRaphael Isemann 
233572fc897SRaphael Isemann   void DoCompletion(SearchFilter *filter) override {
234572fc897SRaphael Isemann     filter->Search(*this);
235572fc897SRaphael Isemann     collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
236572fc897SRaphael Isemann     for (pos = m_match_set.begin(); pos != end; pos++)
237572fc897SRaphael Isemann       m_request.AddCompletion((*pos).GetCString());
238572fc897SRaphael Isemann   }
239572fc897SRaphael Isemann 
240572fc897SRaphael Isemann private:
241572fc897SRaphael Isemann   RegularExpression m_regex;
242572fc897SRaphael Isemann   typedef std::set<ConstString> collection;
243572fc897SRaphael Isemann   collection m_match_set;
244572fc897SRaphael Isemann 
245eaebcbc6SKonrad Kleine   SymbolCompleter(const SymbolCompleter &) = delete;
246eaebcbc6SKonrad Kleine   const SymbolCompleter &operator=(const SymbolCompleter &) = delete;
247572fc897SRaphael Isemann };
248572fc897SRaphael Isemann } // namespace
249572fc897SRaphael Isemann 
250572fc897SRaphael Isemann namespace {
251572fc897SRaphael Isemann class ModuleCompleter : public Completer {
252572fc897SRaphael Isemann public:
253572fc897SRaphael Isemann   ModuleCompleter(CommandInterpreter &interpreter, CompletionRequest &request)
254572fc897SRaphael Isemann       : Completer(interpreter, request) {
255572fc897SRaphael Isemann     FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
256572fc897SRaphael Isemann     m_file_name = partial_spec.GetFilename().GetCString();
257572fc897SRaphael Isemann     m_dir_name = partial_spec.GetDirectory().GetCString();
258572fc897SRaphael Isemann   }
259572fc897SRaphael Isemann 
260572fc897SRaphael Isemann   lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; }
261572fc897SRaphael Isemann 
262572fc897SRaphael Isemann   Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
263572fc897SRaphael Isemann                                           SymbolContext &context,
264572fc897SRaphael Isemann                                           Address *addr) override {
265572fc897SRaphael Isemann     if (context.module_sp) {
266572fc897SRaphael Isemann       const char *cur_file_name =
267572fc897SRaphael Isemann           context.module_sp->GetFileSpec().GetFilename().GetCString();
268572fc897SRaphael Isemann       const char *cur_dir_name =
269572fc897SRaphael Isemann           context.module_sp->GetFileSpec().GetDirectory().GetCString();
270572fc897SRaphael Isemann 
271572fc897SRaphael Isemann       bool match = false;
272572fc897SRaphael Isemann       if (m_file_name && cur_file_name &&
273572fc897SRaphael Isemann           strstr(cur_file_name, m_file_name) == cur_file_name)
274572fc897SRaphael Isemann         match = true;
275572fc897SRaphael Isemann 
276572fc897SRaphael Isemann       if (match && m_dir_name && cur_dir_name &&
277572fc897SRaphael Isemann           strstr(cur_dir_name, m_dir_name) != cur_dir_name)
278572fc897SRaphael Isemann         match = false;
279572fc897SRaphael Isemann 
280572fc897SRaphael Isemann       if (match) {
281572fc897SRaphael Isemann         m_request.AddCompletion(cur_file_name);
282572fc897SRaphael Isemann       }
283572fc897SRaphael Isemann     }
284572fc897SRaphael Isemann     return Searcher::eCallbackReturnContinue;
285572fc897SRaphael Isemann   }
286572fc897SRaphael Isemann 
287572fc897SRaphael Isemann   void DoCompletion(SearchFilter *filter) override { filter->Search(*this); }
288572fc897SRaphael Isemann 
289572fc897SRaphael Isemann private:
290572fc897SRaphael Isemann   const char *m_file_name;
291572fc897SRaphael Isemann   const char *m_dir_name;
292572fc897SRaphael Isemann 
293eaebcbc6SKonrad Kleine   ModuleCompleter(const ModuleCompleter &) = delete;
294eaebcbc6SKonrad Kleine   const ModuleCompleter &operator=(const ModuleCompleter &) = delete;
295572fc897SRaphael Isemann };
296572fc897SRaphael Isemann } // namespace
297572fc897SRaphael Isemann 
298ae34ed2cSRaphael Isemann void CommandCompletions::SourceFiles(CommandInterpreter &interpreter,
299a2e76c0bSRaphael Isemann                                      CompletionRequest &request,
300a2e76c0bSRaphael Isemann                                      SearchFilter *searcher) {
30162174682SRaphael Isemann   SourceFileCompleter completer(interpreter, request);
302c9af18a5SGreg Clayton 
303b9c1b51eSKate Stone   if (searcher == nullptr) {
3042976d00aSJim Ingham     lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
30533df7cd3SJim Ingham     SearchFilterForUnconstrainedSearches null_searcher(target_sp);
306c9af18a5SGreg Clayton     completer.DoCompletion(&null_searcher);
307b9c1b51eSKate Stone   } else {
308c9af18a5SGreg Clayton     completer.DoCompletion(searcher);
309c9af18a5SGreg Clayton   }
310c9af18a5SGreg Clayton }
311c9af18a5SGreg Clayton 
312ae34ed2cSRaphael Isemann static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
3135edee822SRaphael Isemann                                    bool only_directories,
3145edee822SRaphael Isemann                                    CompletionRequest &request,
3152cc5a18dSZachary Turner                                    TildeExpressionResolver &Resolver) {
3162cc5a18dSZachary Turner   llvm::SmallString<256> CompletionBuffer;
3172cc5a18dSZachary Turner   llvm::SmallString<256> Storage;
3182cc5a18dSZachary Turner   partial_name.toVector(CompletionBuffer);
319b2f1fb29SVirgile Bello 
3202cc5a18dSZachary Turner   if (CompletionBuffer.size() >= PATH_MAX)
321ae34ed2cSRaphael Isemann     return;
322b2f1fb29SVirgile Bello 
3232cc5a18dSZachary Turner   namespace path = llvm::sys::path;
324b2f1fb29SVirgile Bello 
3252cc5a18dSZachary Turner   llvm::StringRef SearchDir;
3262cc5a18dSZachary Turner   llvm::StringRef PartialItem;
3272cc5a18dSZachary Turner 
3282cc5a18dSZachary Turner   if (CompletionBuffer.startswith("~")) {
3292cc5a18dSZachary Turner     llvm::StringRef Buffer(CompletionBuffer);
3305c5091fcSZachary Turner     size_t FirstSep =
3315c5091fcSZachary Turner         Buffer.find_if([](char c) { return path::is_separator(c); });
3322cc5a18dSZachary Turner 
3332cc5a18dSZachary Turner     llvm::StringRef Username = Buffer.take_front(FirstSep);
3342cc5a18dSZachary Turner     llvm::StringRef Remainder;
3352cc5a18dSZachary Turner     if (FirstSep != llvm::StringRef::npos)
3362cc5a18dSZachary Turner       Remainder = Buffer.drop_front(FirstSep + 1);
3372cc5a18dSZachary Turner 
338b3f44ad9SStella Stamenova     llvm::SmallString<256> Resolved;
3392cc5a18dSZachary Turner     if (!Resolver.ResolveExact(Username, Resolved)) {
3402cc5a18dSZachary Turner       // We couldn't resolve it as a full username.  If there were no slashes
3412cc5a18dSZachary Turner       // then this might be a partial username.   We try to resolve it as such
3422cc5a18dSZachary Turner       // but after that, we're done regardless of any matches.
3432cc5a18dSZachary Turner       if (FirstSep == llvm::StringRef::npos) {
3442cc5a18dSZachary Turner         llvm::StringSet<> MatchSet;
345a2e76c0bSRaphael Isemann         Resolver.ResolvePartial(Username, MatchSet);
3462cc5a18dSZachary Turner         for (const auto &S : MatchSet) {
3472cc5a18dSZachary Turner           Resolved = S.getKey();
3482cc5a18dSZachary Turner           path::append(Resolved, path::get_separator());
3495edee822SRaphael Isemann           request.AddCompletion(Resolved, "", CompletionMode::Partial);
350b2f1fb29SVirgile Bello         }
351b2f1fb29SVirgile Bello       }
352ae34ed2cSRaphael Isemann       return;
3532cc5a18dSZachary Turner     }
3540734e6a5SZachary Turner 
35505097246SAdrian Prantl     // If there was no trailing slash, then we're done as soon as we resolve
35605097246SAdrian Prantl     // the expression to the correct directory.  Otherwise we need to continue
3572cc5a18dSZachary Turner     // looking for matches within that directory.
3582cc5a18dSZachary Turner     if (FirstSep == llvm::StringRef::npos) {
3592cc5a18dSZachary Turner       // Make sure it ends with a separator.
3602cc5a18dSZachary Turner       path::append(CompletionBuffer, path::get_separator());
3615edee822SRaphael Isemann       request.AddCompletion(CompletionBuffer, "", CompletionMode::Partial);
362ae34ed2cSRaphael Isemann       return;
3632cc5a18dSZachary Turner     }
3642cc5a18dSZachary Turner 
3652cc5a18dSZachary Turner     // We want to keep the form the user typed, so we special case this to
3662cc5a18dSZachary Turner     // search in the fully resolved directory, but CompletionBuffer keeps the
3672cc5a18dSZachary Turner     // unmodified form that the user typed.
3682cc5a18dSZachary Turner     Storage = Resolved;
36988ec2e4eSPavel Labath     llvm::StringRef RemainderDir = path::parent_path(Remainder);
3704621e0b0SRaphael Isemann     if (!RemainderDir.empty()) {
3714621e0b0SRaphael Isemann       // Append the remaining path to the resolved directory.
3724621e0b0SRaphael Isemann       Storage.append(path::get_separator());
3734621e0b0SRaphael Isemann       Storage.append(RemainderDir);
3744621e0b0SRaphael Isemann     }
37517843885SRaphael Isemann     SearchDir = Storage;
3760734e6a5SZachary Turner   } else {
3772cc5a18dSZachary Turner     SearchDir = path::parent_path(CompletionBuffer);
3782cc5a18dSZachary Turner   }
3792cc5a18dSZachary Turner 
3802cc5a18dSZachary Turner   size_t FullPrefixLen = CompletionBuffer.size();
3812cc5a18dSZachary Turner 
3822cc5a18dSZachary Turner   PartialItem = path::filename(CompletionBuffer);
38378a10a7aSFrederic Riss 
38478a10a7aSFrederic Riss   // path::filename() will return "." when the passed path ends with a
38578a10a7aSFrederic Riss   // directory separator. We have to filter those out, but only when the
38678a10a7aSFrederic Riss   // "." doesn't come from the completion request itself.
38778a10a7aSFrederic Riss   if (PartialItem == "." && path::is_separator(CompletionBuffer.back()))
3882cc5a18dSZachary Turner     PartialItem = llvm::StringRef();
3892cc5a18dSZachary Turner 
390426d1379SZachary Turner   if (SearchDir.empty()) {
391426d1379SZachary Turner     llvm::sys::fs::current_path(Storage);
392426d1379SZachary Turner     SearchDir = Storage;
393426d1379SZachary Turner   }
3942cc5a18dSZachary Turner   assert(!PartialItem.contains(path::get_separator()));
3952cc5a18dSZachary Turner 
3962cc5a18dSZachary Turner   // SearchDir now contains the directory to search in, and Prefix contains the
3972cc5a18dSZachary Turner   // text we want to match against items in that directory.
3982cc5a18dSZachary Turner 
399edaf2bccSJonas Devlieghere   FileSystem &fs = FileSystem::Instance();
4002cc5a18dSZachary Turner   std::error_code EC;
401edaf2bccSJonas Devlieghere   llvm::vfs::directory_iterator Iter = fs.DirBegin(SearchDir, EC);
402edaf2bccSJonas Devlieghere   llvm::vfs::directory_iterator End;
4032cc5a18dSZachary Turner   for (; Iter != End && !EC; Iter.increment(EC)) {
4042cc5a18dSZachary Turner     auto &Entry = *Iter;
405edaf2bccSJonas Devlieghere     llvm::ErrorOr<llvm::vfs::Status> Status = fs.GetStatus(Entry.path());
406edaf2bccSJonas Devlieghere 
407edaf2bccSJonas Devlieghere     if (!Status)
408edaf2bccSJonas Devlieghere       continue;
4092cc5a18dSZachary Turner 
4102cc5a18dSZachary Turner     auto Name = path::filename(Entry.path());
4112cc5a18dSZachary Turner 
4122cc5a18dSZachary Turner     // Omit ".", ".."
4132cc5a18dSZachary Turner     if (Name == "." || Name == ".." || !Name.startswith(PartialItem))
4142cc5a18dSZachary Turner       continue;
4152cc5a18dSZachary Turner 
416edaf2bccSJonas Devlieghere     bool is_dir = Status->isDirectory();
4172cc5a18dSZachary Turner 
4182cc5a18dSZachary Turner     // If it's a symlink, then we treat it as a directory as long as the target
4192cc5a18dSZachary Turner     // is a directory.
420edaf2bccSJonas Devlieghere     if (Status->isSymlink()) {
421edaf2bccSJonas Devlieghere       FileSpec symlink_filespec(Entry.path());
422edaf2bccSJonas Devlieghere       FileSpec resolved_filespec;
423edaf2bccSJonas Devlieghere       auto error = fs.ResolveSymbolicLink(symlink_filespec, resolved_filespec);
424edaf2bccSJonas Devlieghere       if (error.Success())
425edaf2bccSJonas Devlieghere         is_dir = fs.IsDirectory(symlink_filespec);
4262cc5a18dSZachary Turner     }
427edaf2bccSJonas Devlieghere 
4282cc5a18dSZachary Turner     if (only_directories && !is_dir)
4292cc5a18dSZachary Turner       continue;
4302cc5a18dSZachary Turner 
4312cc5a18dSZachary Turner     // Shrink it back down so that it just has the original prefix the user
4322cc5a18dSZachary Turner     // typed and remove the part of the name which is common to the located
4332cc5a18dSZachary Turner     // item and what the user typed.
4342cc5a18dSZachary Turner     CompletionBuffer.resize(FullPrefixLen);
4352cc5a18dSZachary Turner     Name = Name.drop_front(PartialItem.size());
4362cc5a18dSZachary Turner     CompletionBuffer.append(Name);
4372cc5a18dSZachary Turner 
4382cc5a18dSZachary Turner     if (is_dir) {
4392cc5a18dSZachary Turner       path::append(CompletionBuffer, path::get_separator());
4400734e6a5SZachary Turner     }
4410734e6a5SZachary Turner 
4425edee822SRaphael Isemann     CompletionMode mode =
4435edee822SRaphael Isemann         is_dir ? CompletionMode::Partial : CompletionMode::Normal;
4445edee822SRaphael Isemann     request.AddCompletion(CompletionBuffer, "", mode);
445d5bd3a1eSZachary Turner   }
446558ce124SJim Ingham }
447558ce124SJim Ingham 
4485edee822SRaphael Isemann static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
4495edee822SRaphael Isemann                                    bool only_directories, StringList &matches,
4505edee822SRaphael Isemann                                    TildeExpressionResolver &Resolver) {
4515edee822SRaphael Isemann   CompletionResult result;
4525edee822SRaphael Isemann   std::string partial_name_str = partial_name.str();
4535edee822SRaphael Isemann   CompletionRequest request(partial_name_str, partial_name_str.size(), result);
4545edee822SRaphael Isemann   DiskFilesOrDirectories(partial_name, only_directories, request, Resolver);
4555edee822SRaphael Isemann   result.GetMatches(matches);
4565edee822SRaphael Isemann }
4575edee822SRaphael Isemann 
458ae34ed2cSRaphael Isemann static void DiskFilesOrDirectories(CompletionRequest &request,
4591a6d7ab5SRaphael Isemann                                    bool only_directories) {
46072787ac6SJonas Devlieghere   StandardTildeExpressionResolver resolver;
4611a6d7ab5SRaphael Isemann   DiskFilesOrDirectories(request.GetCursorArgumentPrefix(), only_directories,
4625edee822SRaphael Isemann                          request, resolver);
4631a6d7ab5SRaphael Isemann }
4641a6d7ab5SRaphael Isemann 
465ae34ed2cSRaphael Isemann void CommandCompletions::DiskFiles(CommandInterpreter &interpreter,
466a2e76c0bSRaphael Isemann                                    CompletionRequest &request,
467a2e76c0bSRaphael Isemann                                    SearchFilter *searcher) {
468ae34ed2cSRaphael Isemann   DiskFilesOrDirectories(request, /*only_dirs*/ false);
4692cc5a18dSZachary Turner }
4702cc5a18dSZachary Turner 
471ae34ed2cSRaphael Isemann void CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name,
4722cc5a18dSZachary Turner                                    StringList &matches,
4732cc5a18dSZachary Turner                                    TildeExpressionResolver &Resolver) {
474ae34ed2cSRaphael Isemann   DiskFilesOrDirectories(partial_file_name, false, matches, Resolver);
475558ce124SJim Ingham }
476558ce124SJim Ingham 
477ae34ed2cSRaphael Isemann void CommandCompletions::DiskDirectories(CommandInterpreter &interpreter,
478a2e76c0bSRaphael Isemann                                          CompletionRequest &request,
479a2e76c0bSRaphael Isemann                                          SearchFilter *searcher) {
480ae34ed2cSRaphael Isemann   DiskFilesOrDirectories(request, /*only_dirs*/ true);
4812cc5a18dSZachary Turner }
4822cc5a18dSZachary Turner 
483ae34ed2cSRaphael Isemann void CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name,
4842cc5a18dSZachary Turner                                          StringList &matches,
4852cc5a18dSZachary Turner                                          TildeExpressionResolver &Resolver) {
486ae34ed2cSRaphael Isemann   DiskFilesOrDirectories(partial_file_name, true, matches, Resolver);
487558ce124SJim Ingham }
488558ce124SJim Ingham 
489ae34ed2cSRaphael Isemann void CommandCompletions::Modules(CommandInterpreter &interpreter,
490a2e76c0bSRaphael Isemann                                  CompletionRequest &request,
491a2e76c0bSRaphael Isemann                                  SearchFilter *searcher) {
492a2e76c0bSRaphael Isemann   ModuleCompleter completer(interpreter, request);
493c9af18a5SGreg Clayton 
494b9c1b51eSKate Stone   if (searcher == nullptr) {
4952976d00aSJim Ingham     lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
49633df7cd3SJim Ingham     SearchFilterForUnconstrainedSearches null_searcher(target_sp);
497c9af18a5SGreg Clayton     completer.DoCompletion(&null_searcher);
498b9c1b51eSKate Stone   } else {
499c9af18a5SGreg Clayton     completer.DoCompletion(searcher);
500c9af18a5SGreg Clayton   }
501c9af18a5SGreg Clayton }
502c9af18a5SGreg Clayton 
503419f1be7SGongyu Deng void CommandCompletions::ModuleUUIDs(CommandInterpreter &interpreter,
504419f1be7SGongyu Deng                                      CompletionRequest &request,
505419f1be7SGongyu Deng                                      SearchFilter *searcher) {
506419f1be7SGongyu Deng   const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
507419f1be7SGongyu Deng   if (!exe_ctx.HasTargetScope())
508419f1be7SGongyu Deng     return;
509419f1be7SGongyu Deng 
510419f1be7SGongyu Deng   exe_ctx.GetTargetPtr()->GetImages().ForEach(
511419f1be7SGongyu Deng       [&request](const lldb::ModuleSP &module) {
512419f1be7SGongyu Deng         StreamString strm;
513419f1be7SGongyu Deng         module->GetDescription(strm.AsRawOstream(),
514419f1be7SGongyu Deng                                lldb::eDescriptionLevelInitial);
515419f1be7SGongyu Deng         request.TryCompleteCurrentArg(module->GetUUID().GetAsString(),
516419f1be7SGongyu Deng                                       strm.GetString());
517419f1be7SGongyu Deng         return true;
518419f1be7SGongyu Deng       });
519419f1be7SGongyu Deng }
520419f1be7SGongyu Deng 
521ae34ed2cSRaphael Isemann void CommandCompletions::Symbols(CommandInterpreter &interpreter,
522a2e76c0bSRaphael Isemann                                  CompletionRequest &request,
523a2e76c0bSRaphael Isemann                                  SearchFilter *searcher) {
524a2e76c0bSRaphael Isemann   SymbolCompleter completer(interpreter, request);
525c9af18a5SGreg Clayton 
526b9c1b51eSKate Stone   if (searcher == nullptr) {
5272976d00aSJim Ingham     lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
52833df7cd3SJim Ingham     SearchFilterForUnconstrainedSearches null_searcher(target_sp);
529c9af18a5SGreg Clayton     completer.DoCompletion(&null_searcher);
530b9c1b51eSKate Stone   } else {
531c9af18a5SGreg Clayton     completer.DoCompletion(searcher);
532c9af18a5SGreg Clayton   }
533c9af18a5SGreg Clayton }
534c9af18a5SGreg Clayton 
535ae34ed2cSRaphael Isemann void CommandCompletions::SettingsNames(CommandInterpreter &interpreter,
536a2e76c0bSRaphael Isemann                                        CompletionRequest &request,
537a2e76c0bSRaphael Isemann                                        SearchFilter *searcher) {
53867cc0636SGreg Clayton   // Cache the full setting name list
53967cc0636SGreg Clayton   static StringList g_property_names;
540b9c1b51eSKate Stone   if (g_property_names.GetSize() == 0) {
54167cc0636SGreg Clayton     // Generate the full setting name list on demand
542b9c1b51eSKate Stone     lldb::OptionValuePropertiesSP properties_sp(
543b9c1b51eSKate Stone         interpreter.GetDebugger().GetValueProperties());
544b9c1b51eSKate Stone     if (properties_sp) {
54567cc0636SGreg Clayton       StreamString strm;
546c8ecc2a9SEugene Zelenko       properties_sp->DumpValue(nullptr, strm, OptionValue::eDumpOptionName);
547adcd0268SBenjamin Kramer       const std::string &str = std::string(strm.GetString());
54867cc0636SGreg Clayton       g_property_names.SplitIntoLines(str.c_str(), str.size());
54967cc0636SGreg Clayton     }
55067cc0636SGreg Clayton   }
5513df9a8dfSCaroline Tice 
55293ca36d7SRaphael Isemann   for (const std::string &s : g_property_names)
55393ca36d7SRaphael Isemann     request.TryCompleteCurrentArg(s);
554b8639f5cSRaphael Isemann }
555b8639f5cSRaphael Isemann 
556ae34ed2cSRaphael Isemann void CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter,
557a2e76c0bSRaphael Isemann                                              CompletionRequest &request,
558a2e76c0bSRaphael Isemann                                              SearchFilter *searcher) {
559ae34ed2cSRaphael Isemann   PluginManager::AutoCompletePlatformName(request.GetCursorArgumentPrefix(),
560ae34ed2cSRaphael Isemann                                           request);
561ab65b34fSGreg Clayton }
562ab65b34fSGreg Clayton 
563ae34ed2cSRaphael Isemann void CommandCompletions::ArchitectureNames(CommandInterpreter &interpreter,
564a2e76c0bSRaphael Isemann                                            CompletionRequest &request,
565a2e76c0bSRaphael Isemann                                            SearchFilter *searcher) {
566ae34ed2cSRaphael Isemann   ArchSpec::AutoComplete(request);
567ab65b34fSGreg Clayton }
568ab65b34fSGreg Clayton 
569ae34ed2cSRaphael Isemann void CommandCompletions::VariablePath(CommandInterpreter &interpreter,
570a2e76c0bSRaphael Isemann                                       CompletionRequest &request,
571a2e76c0bSRaphael Isemann                                       SearchFilter *searcher) {
572ae34ed2cSRaphael Isemann   Variable::AutoComplete(interpreter.GetExecutionContext(), request);
573f21feadcSGreg Clayton }
574a14f4a75SGongyu Deng 
575a14f4a75SGongyu Deng void CommandCompletions::Registers(CommandInterpreter &interpreter,
576a14f4a75SGongyu Deng                                    CompletionRequest &request,
577a14f4a75SGongyu Deng                                    SearchFilter *searcher) {
578a14f4a75SGongyu Deng   std::string reg_prefix = "";
579a14f4a75SGongyu Deng   if (request.GetCursorArgumentPrefix().startswith("$"))
580a14f4a75SGongyu Deng     reg_prefix = "$";
581a14f4a75SGongyu Deng 
582a14f4a75SGongyu Deng   RegisterContext *reg_ctx =
583a14f4a75SGongyu Deng       interpreter.GetExecutionContext().GetRegisterContext();
584a14f4a75SGongyu Deng   const size_t reg_num = reg_ctx->GetRegisterCount();
585a14f4a75SGongyu Deng   for (size_t reg_idx = 0; reg_idx < reg_num; ++reg_idx) {
586a14f4a75SGongyu Deng     const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
587a14f4a75SGongyu Deng     request.TryCompleteCurrentArg(reg_prefix + reg_info->name,
588a14f4a75SGongyu Deng                                   reg_info->alt_name);
589a14f4a75SGongyu Deng   }
590a14f4a75SGongyu Deng }
5910e50b9a4SGongyu Deng 
5920e50b9a4SGongyu Deng void CommandCompletions::Breakpoints(CommandInterpreter &interpreter,
5930e50b9a4SGongyu Deng                                      CompletionRequest &request,
5940e50b9a4SGongyu Deng                                      SearchFilter *searcher) {
5950e50b9a4SGongyu Deng   lldb::TargetSP target = interpreter.GetDebugger().GetSelectedTarget();
5960e50b9a4SGongyu Deng   if (!target)
5970e50b9a4SGongyu Deng     return;
5980e50b9a4SGongyu Deng 
5990e50b9a4SGongyu Deng   const BreakpointList &breakpoints = target->GetBreakpointList();
6000e50b9a4SGongyu Deng 
6010e50b9a4SGongyu Deng   std::unique_lock<std::recursive_mutex> lock;
6020e50b9a4SGongyu Deng   target->GetBreakpointList().GetListMutex(lock);
6030e50b9a4SGongyu Deng 
6040e50b9a4SGongyu Deng   size_t num_breakpoints = breakpoints.GetSize();
6050e50b9a4SGongyu Deng   if (num_breakpoints == 0)
6060e50b9a4SGongyu Deng     return;
6070e50b9a4SGongyu Deng 
6080e50b9a4SGongyu Deng   for (size_t i = 0; i < num_breakpoints; ++i) {
6090e50b9a4SGongyu Deng     lldb::BreakpointSP bp = breakpoints.GetBreakpointAtIndex(i);
6100e50b9a4SGongyu Deng 
6110e50b9a4SGongyu Deng     StreamString s;
6120e50b9a4SGongyu Deng     bp->GetDescription(&s, lldb::eDescriptionLevelBrief);
6130e50b9a4SGongyu Deng     llvm::StringRef bp_info = s.GetString();
6140e50b9a4SGongyu Deng 
6150e50b9a4SGongyu Deng     const size_t colon_pos = bp_info.find_first_of(':');
6160e50b9a4SGongyu Deng     if (colon_pos != llvm::StringRef::npos)
6170e50b9a4SGongyu Deng       bp_info = bp_info.drop_front(colon_pos + 2);
6180e50b9a4SGongyu Deng 
6190e50b9a4SGongyu Deng     request.TryCompleteCurrentArg(std::to_string(bp->GetID()), bp_info);
6200e50b9a4SGongyu Deng   }
6210e50b9a4SGongyu Deng }
622763bc230SGongyu Deng 
62322e63cbaSGongyu Deng void CommandCompletions::BreakpointNames(CommandInterpreter &interpreter,
62422e63cbaSGongyu Deng                                          CompletionRequest &request,
62522e63cbaSGongyu Deng                                          SearchFilter *searcher) {
62622e63cbaSGongyu Deng   lldb::TargetSP target = interpreter.GetDebugger().GetSelectedTarget();
62722e63cbaSGongyu Deng   if (!target)
62822e63cbaSGongyu Deng     return;
62922e63cbaSGongyu Deng 
63022e63cbaSGongyu Deng   std::vector<std::string> name_list;
63122e63cbaSGongyu Deng   target->GetBreakpointNames(name_list);
63222e63cbaSGongyu Deng 
63322e63cbaSGongyu Deng   for (const std::string &name : name_list)
63422e63cbaSGongyu Deng     request.TryCompleteCurrentArg(name);
63522e63cbaSGongyu Deng }
63622e63cbaSGongyu Deng 
637763bc230SGongyu Deng void CommandCompletions::ProcessPluginNames(CommandInterpreter &interpreter,
638763bc230SGongyu Deng                                             CompletionRequest &request,
639763bc230SGongyu Deng                                             SearchFilter *searcher) {
640763bc230SGongyu Deng   PluginManager::AutoCompleteProcessName(request.GetCursorArgumentPrefix(),
641763bc230SGongyu Deng                                          request);
642763bc230SGongyu Deng }
6438a5e2969SGongyu Deng void CommandCompletions::DisassemblyFlavors(CommandInterpreter &interpreter,
6448a5e2969SGongyu Deng                                             CompletionRequest &request,
6458a5e2969SGongyu Deng                                             SearchFilter *searcher) {
6468a5e2969SGongyu Deng   // Currently the only valid options for disassemble -F are default, and for
6478a5e2969SGongyu Deng   // Intel architectures, att and intel.
6488a5e2969SGongyu Deng   static const char *flavors[] = {"default", "att", "intel"};
6498a5e2969SGongyu Deng   for (const char *flavor : flavors) {
6508a5e2969SGongyu Deng     request.TryCompleteCurrentArg(flavor);
6518a5e2969SGongyu Deng   }
6528a5e2969SGongyu Deng }
6533ce57e01SGongyu Deng 
654*19311f5cSGongyu Deng void CommandCompletions::ProcessIDs(CommandInterpreter &interpreter,
655*19311f5cSGongyu Deng                                     CompletionRequest &request,
656*19311f5cSGongyu Deng                                     SearchFilter *searcher) {
657*19311f5cSGongyu Deng   lldb::PlatformSP platform_sp(interpreter.GetPlatform(true));
658*19311f5cSGongyu Deng   if (!platform_sp)
659*19311f5cSGongyu Deng     return;
660*19311f5cSGongyu Deng   ProcessInstanceInfoList process_infos;
661*19311f5cSGongyu Deng   ProcessInstanceInfoMatch match_info;
662*19311f5cSGongyu Deng   platform_sp->FindProcesses(match_info, process_infos);
663*19311f5cSGongyu Deng   for (const ProcessInstanceInfo &info : process_infos)
664*19311f5cSGongyu Deng     request.TryCompleteCurrentArg(std::to_string(info.GetProcessID()),
665*19311f5cSGongyu Deng                                   info.GetNameAsStringRef());
666*19311f5cSGongyu Deng }
667*19311f5cSGongyu Deng 
668*19311f5cSGongyu Deng void CommandCompletions::ProcessNames(CommandInterpreter &interpreter,
669*19311f5cSGongyu Deng                                       CompletionRequest &request,
670*19311f5cSGongyu Deng                                       SearchFilter *searcher) {
671*19311f5cSGongyu Deng   lldb::PlatformSP platform_sp(interpreter.GetPlatform(true));
672*19311f5cSGongyu Deng   if (!platform_sp)
673*19311f5cSGongyu Deng     return;
674*19311f5cSGongyu Deng   ProcessInstanceInfoList process_infos;
675*19311f5cSGongyu Deng   ProcessInstanceInfoMatch match_info;
676*19311f5cSGongyu Deng   platform_sp->FindProcesses(match_info, process_infos);
677*19311f5cSGongyu Deng   for (const ProcessInstanceInfo &info : process_infos)
678*19311f5cSGongyu Deng     request.TryCompleteCurrentArg(info.GetNameAsStringRef());
679*19311f5cSGongyu Deng }
680*19311f5cSGongyu Deng 
6813ce57e01SGongyu Deng void CommandCompletions::TypeLanguages(CommandInterpreter &interpreter,
6823ce57e01SGongyu Deng                                        CompletionRequest &request,
6833ce57e01SGongyu Deng                                        SearchFilter *searcher) {
6843ce57e01SGongyu Deng   for (int bit :
6853ce57e01SGongyu Deng        Language::GetLanguagesSupportingTypeSystems().bitvector.set_bits()) {
6863ce57e01SGongyu Deng     request.TryCompleteCurrentArg(
6873ce57e01SGongyu Deng         Language::GetNameForLanguageType(static_cast<lldb::LanguageType>(bit)));
6883ce57e01SGongyu Deng   }
6893ce57e01SGongyu Deng }
69066fa73faSGongyu Deng 
69166fa73faSGongyu Deng void CommandCompletions::FrameIndexes(CommandInterpreter &interpreter,
69266fa73faSGongyu Deng                                       CompletionRequest &request,
69366fa73faSGongyu Deng                                       SearchFilter *searcher) {
69466fa73faSGongyu Deng   const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
69566fa73faSGongyu Deng   if (!exe_ctx.HasProcessScope())
69666fa73faSGongyu Deng     return;
69766fa73faSGongyu Deng 
69866fa73faSGongyu Deng   lldb::ThreadSP thread_sp = exe_ctx.GetThreadSP();
69966fa73faSGongyu Deng   const uint32_t frame_num = thread_sp->GetStackFrameCount();
70066fa73faSGongyu Deng   for (uint32_t i = 0; i < frame_num; ++i) {
70166fa73faSGongyu Deng     lldb::StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(i);
70266fa73faSGongyu Deng     StreamString strm;
70366fa73faSGongyu Deng     frame_sp->Dump(&strm, false, true);
70466fa73faSGongyu Deng     request.TryCompleteCurrentArg(std::to_string(i), strm.GetString());
70566fa73faSGongyu Deng   }
70666fa73faSGongyu Deng }
707b2b7dbb4SGongyu Deng 
708b2b7dbb4SGongyu Deng void CommandCompletions::StopHookIDs(CommandInterpreter &interpreter,
709b2b7dbb4SGongyu Deng                                      CompletionRequest &request,
710b2b7dbb4SGongyu Deng                                      SearchFilter *searcher) {
711b2b7dbb4SGongyu Deng   const lldb::TargetSP target_sp =
712b2b7dbb4SGongyu Deng       interpreter.GetExecutionContext().GetTargetSP();
713b2b7dbb4SGongyu Deng   if (!target_sp)
714b2b7dbb4SGongyu Deng     return;
715b2b7dbb4SGongyu Deng 
716b2b7dbb4SGongyu Deng   const size_t num = target_sp->GetNumStopHooks();
717b2b7dbb4SGongyu Deng   for (size_t idx = 0; idx < num; ++idx) {
718b2b7dbb4SGongyu Deng     StreamString strm;
719b2b7dbb4SGongyu Deng     // The value 11 is an offset to make the completion description looks
720b2b7dbb4SGongyu Deng     // neater.
721b2b7dbb4SGongyu Deng     strm.SetIndentLevel(11);
722b2b7dbb4SGongyu Deng     const Target::StopHookSP stophook_sp = target_sp->GetStopHookAtIndex(idx);
723b2b7dbb4SGongyu Deng     stophook_sp->GetDescription(&strm, lldb::eDescriptionLevelInitial);
724b2b7dbb4SGongyu Deng     request.TryCompleteCurrentArg(std::to_string(stophook_sp->GetID()),
725b2b7dbb4SGongyu Deng                                   strm.GetString());
726b2b7dbb4SGongyu Deng   }
727b2b7dbb4SGongyu Deng }
728a952fe23SGongyu Deng 
729a952fe23SGongyu Deng void CommandCompletions::ThreadIndexes(CommandInterpreter &interpreter,
730a952fe23SGongyu Deng                                        CompletionRequest &request,
731a952fe23SGongyu Deng                                        SearchFilter *searcher) {
732a952fe23SGongyu Deng   const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
733a952fe23SGongyu Deng   if (!exe_ctx.HasProcessScope())
734a952fe23SGongyu Deng     return;
735a952fe23SGongyu Deng 
736a952fe23SGongyu Deng   ThreadList &threads = exe_ctx.GetProcessPtr()->GetThreadList();
737a952fe23SGongyu Deng   lldb::ThreadSP thread_sp;
738a952fe23SGongyu Deng   for (uint32_t idx = 0; (thread_sp = threads.GetThreadAtIndex(idx)); ++idx) {
739a952fe23SGongyu Deng     StreamString strm;
740a952fe23SGongyu Deng     thread_sp->GetStatus(strm, 0, 1, 1, true);
741a952fe23SGongyu Deng     request.TryCompleteCurrentArg(std::to_string(thread_sp->GetIndexID()),
742a952fe23SGongyu Deng                                   strm.GetString());
743a952fe23SGongyu Deng   }
744a952fe23SGongyu Deng }
7454f3559dbSGongyu Deng 
7464f3559dbSGongyu Deng void CommandCompletions::WatchPointIDs(CommandInterpreter &interpreter,
7474f3559dbSGongyu Deng                                        CompletionRequest &request,
7484f3559dbSGongyu Deng                                        SearchFilter *searcher) {
7494f3559dbSGongyu Deng   const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
7504f3559dbSGongyu Deng   if (!exe_ctx.HasTargetScope())
7514f3559dbSGongyu Deng     return;
7524f3559dbSGongyu Deng 
7534f3559dbSGongyu Deng   const WatchpointList &wp_list = exe_ctx.GetTargetPtr()->GetWatchpointList();
7544f3559dbSGongyu Deng   const size_t wp_num = wp_list.GetSize();
7554f3559dbSGongyu Deng   for (size_t idx = 0; idx < wp_num; ++idx) {
7564f3559dbSGongyu Deng     const lldb::WatchpointSP wp_sp = wp_list.GetByIndex(idx);
7574f3559dbSGongyu Deng     StreamString strm;
7584f3559dbSGongyu Deng     wp_sp->Dump(&strm);
7594f3559dbSGongyu Deng     request.TryCompleteCurrentArg(std::to_string(wp_sp->GetID()),
7604f3559dbSGongyu Deng                                   strm.GetString());
7614f3559dbSGongyu Deng   }
7624f3559dbSGongyu Deng }
763