blob: e5f29115a8a73ea578773875b3321f8dbe420a5b [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- CommandCompletions.cpp --------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +00009#include "llvm/ADT/SmallString.h"
Zachary Turner2cc5a182017-03-13 00:41:01 +000010#include "llvm/ADT/StringSet.h"
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000011
Greg Clayton9b62fd22011-02-01 05:15:22 +000012#include "lldb/Core/FileSpecList.h"
Greg Clayton1f746072012-08-29 21:13:06 +000013#include "lldb/Core/Module.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000014#include "lldb/Core/PluginManager.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000015#include "lldb/Host/FileSystem.h"
Greg Clayton9b62fd22011-02-01 05:15:22 +000016#include "lldb/Interpreter/CommandCompletions.h"
17#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000018#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Symbol/CompileUnit.h"
Greg Claytonf21fead2013-05-14 23:43:18 +000020#include "lldb/Symbol/Variable.h"
Zachary Turner5713a052017-03-22 18:40:07 +000021#include "lldb/Utility/FileSpec.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000022#include "lldb/Utility/StreamString.h"
Zachary Turner2cc5a182017-03-13 00:41:01 +000023#include "lldb/Utility/TildeExpressionResolver.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
Zachary Turner7d86ee52017-03-08 17:56:08 +000025#include "llvm/Support/FileSystem.h"
Zachary Turner2cc5a182017-03-13 00:41:01 +000026#include "llvm/Support/Path.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000027
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028using namespace lldb_private;
29
30CommandCompletions::CommonCompletionElement
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 CommandCompletions::g_common_completions[] = {
32 {eCustomCompletion, nullptr},
33 {eSourceFileCompletion, CommandCompletions::SourceFiles},
34 {eDiskFileCompletion, CommandCompletions::DiskFiles},
35 {eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
36 {eSymbolCompletion, CommandCompletions::Symbols},
37 {eModuleCompletion, CommandCompletions::Modules},
38 {eSettingsNameCompletion, CommandCompletions::SettingsNames},
39 {ePlatformPluginCompletion, CommandCompletions::PlatformPluginNames},
40 {eArchitectureCompletion, CommandCompletions::ArchitectureNames},
41 {eVariablePathCompletion, CommandCompletions::VariablePath},
42 {eNoCompletion, nullptr} // This one has to be last in the list.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043};
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045bool CommandCompletions::InvokeCommonCompletionCallbacks(
46 CommandInterpreter &interpreter, uint32_t completion_mask,
Raphael Isemanna2e76c02018-07-13 18:28:14 +000047 CompletionRequest &request, SearchFilter *searcher) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 bool handled = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 if (completion_mask & eCustomCompletion)
51 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 for (int i = 0;; i++) {
54 if (g_common_completions[i].type == eNoCompletion)
55 break;
56 else if ((g_common_completions[i].type & completion_mask) ==
57 g_common_completions[i].type &&
58 g_common_completions[i].callback != nullptr) {
59 handled = true;
Raphael Isemanna2e76c02018-07-13 18:28:14 +000060 g_common_completions[i].callback(interpreter, request, searcher);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 }
63 return handled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064}
65
Raphael Isemannae34ed22019-08-22 07:41:23 +000066void CommandCompletions::SourceFiles(CommandInterpreter &interpreter,
67 CompletionRequest &request,
68 SearchFilter *searcher) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 // Find some way to switch "include support files..."
Raphael Isemanna2e76c02018-07-13 18:28:14 +000070 SourceFileCompleter completer(interpreter, false, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +000071
72 if (searcher == nullptr) {
73 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
74 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
75 completer.DoCompletion(&null_searcher);
76 } else {
77 completer.DoCompletion(searcher);
78 }
Jim Ingham558ce122010-06-30 05:02:46 +000079}
80
Raphael Isemannae34ed22019-08-22 07:41:23 +000081static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
Raphael Isemann5edee822019-08-27 11:32:22 +000082 bool only_directories,
83 CompletionRequest &request,
Raphael Isemannae34ed22019-08-22 07:41:23 +000084 TildeExpressionResolver &Resolver) {
Zachary Turner2cc5a182017-03-13 00:41:01 +000085 llvm::SmallString<256> CompletionBuffer;
86 llvm::SmallString<256> Storage;
87 partial_name.toVector(CompletionBuffer);
Kate Stoneb9c1b512016-09-06 20:57:50 +000088
Zachary Turner2cc5a182017-03-13 00:41:01 +000089 if (CompletionBuffer.size() >= PATH_MAX)
Raphael Isemannae34ed22019-08-22 07:41:23 +000090 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091
Zachary Turner2cc5a182017-03-13 00:41:01 +000092 namespace path = llvm::sys::path;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093
Zachary Turner2cc5a182017-03-13 00:41:01 +000094 llvm::StringRef SearchDir;
95 llvm::StringRef PartialItem;
Kate Stoneb9c1b512016-09-06 20:57:50 +000096
Zachary Turner2cc5a182017-03-13 00:41:01 +000097 if (CompletionBuffer.startswith("~")) {
98 llvm::StringRef Buffer(CompletionBuffer);
Zachary Turner5c5091f2017-03-16 22:28:04 +000099 size_t FirstSep =
100 Buffer.find_if([](char c) { return path::is_separator(c); });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101
Zachary Turner2cc5a182017-03-13 00:41:01 +0000102 llvm::StringRef Username = Buffer.take_front(FirstSep);
103 llvm::StringRef Remainder;
104 if (FirstSep != llvm::StringRef::npos)
105 Remainder = Buffer.drop_front(FirstSep + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106
Stella Stamenovab3f44ad2018-12-10 17:23:28 +0000107 llvm::SmallString<256> Resolved;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000108 if (!Resolver.ResolveExact(Username, Resolved)) {
109 // We couldn't resolve it as a full username. If there were no slashes
110 // then this might be a partial username. We try to resolve it as such
111 // but after that, we're done regardless of any matches.
112 if (FirstSep == llvm::StringRef::npos) {
113 llvm::StringSet<> MatchSet;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000114 Resolver.ResolvePartial(Username, MatchSet);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000115 for (const auto &S : MatchSet) {
116 Resolved = S.getKey();
117 path::append(Resolved, path::get_separator());
Raphael Isemann5edee822019-08-27 11:32:22 +0000118 request.AddCompletion(Resolved, "", CompletionMode::Partial);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000119 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 }
Raphael Isemannae34ed22019-08-22 07:41:23 +0000121 return;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000122 }
123
Adrian Prantl05097242018-04-30 16:49:04 +0000124 // If there was no trailing slash, then we're done as soon as we resolve
125 // the expression to the correct directory. Otherwise we need to continue
Zachary Turner2cc5a182017-03-13 00:41:01 +0000126 // looking for matches within that directory.
127 if (FirstSep == llvm::StringRef::npos) {
128 // Make sure it ends with a separator.
129 path::append(CompletionBuffer, path::get_separator());
Raphael Isemann5edee822019-08-27 11:32:22 +0000130 request.AddCompletion(CompletionBuffer, "", CompletionMode::Partial);
Raphael Isemannae34ed22019-08-22 07:41:23 +0000131 return;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000132 }
133
134 // We want to keep the form the user typed, so we special case this to
135 // search in the fully resolved directory, but CompletionBuffer keeps the
136 // unmodified form that the user typed.
137 Storage = Resolved;
Pavel Labath88ec2e42018-06-29 10:27:18 +0000138 llvm::StringRef RemainderDir = path::parent_path(Remainder);
Raphael Isemann4621e0b2018-06-18 20:11:38 +0000139 if (!RemainderDir.empty()) {
140 // Append the remaining path to the resolved directory.
141 Storage.append(path::get_separator());
142 Storage.append(RemainderDir);
143 }
Raphael Isemann17843882018-01-22 09:17:16 +0000144 SearchDir = Storage;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000145 } else {
146 SearchDir = path::parent_path(CompletionBuffer);
Zachary Turnerd5bd3a12017-03-12 18:18:50 +0000147 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148
Zachary Turner2cc5a182017-03-13 00:41:01 +0000149 size_t FullPrefixLen = CompletionBuffer.size();
Zachary Turner0734e6a2017-03-12 20:01:37 +0000150
Zachary Turner2cc5a182017-03-13 00:41:01 +0000151 PartialItem = path::filename(CompletionBuffer);
Frederic Riss78a10a72018-08-31 23:03:28 +0000152
153 // path::filename() will return "." when the passed path ends with a
154 // directory separator. We have to filter those out, but only when the
155 // "." doesn't come from the completion request itself.
156 if (PartialItem == "." && path::is_separator(CompletionBuffer.back()))
Zachary Turner2cc5a182017-03-13 00:41:01 +0000157 PartialItem = llvm::StringRef();
Zachary Turner0734e6a2017-03-12 20:01:37 +0000158
Zachary Turner426d1372017-04-15 02:44:53 +0000159 if (SearchDir.empty()) {
160 llvm::sys::fs::current_path(Storage);
161 SearchDir = Storage;
162 }
Zachary Turner2cc5a182017-03-13 00:41:01 +0000163 assert(!PartialItem.contains(path::get_separator()));
Zachary Turner0734e6a2017-03-12 20:01:37 +0000164
Zachary Turner2cc5a182017-03-13 00:41:01 +0000165 // SearchDir now contains the directory to search in, and Prefix contains the
166 // text we want to match against items in that directory.
167
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000168 FileSystem &fs = FileSystem::Instance();
Zachary Turner2cc5a182017-03-13 00:41:01 +0000169 std::error_code EC;
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000170 llvm::vfs::directory_iterator Iter = fs.DirBegin(SearchDir, EC);
171 llvm::vfs::directory_iterator End;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000172 for (; Iter != End && !EC; Iter.increment(EC)) {
173 auto &Entry = *Iter;
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000174 llvm::ErrorOr<llvm::vfs::Status> Status = fs.GetStatus(Entry.path());
175
176 if (!Status)
177 continue;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000178
179 auto Name = path::filename(Entry.path());
180
181 // Omit ".", ".."
182 if (Name == "." || Name == ".." || !Name.startswith(PartialItem))
183 continue;
184
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000185 bool is_dir = Status->isDirectory();
Zachary Turner2cc5a182017-03-13 00:41:01 +0000186
187 // If it's a symlink, then we treat it as a directory as long as the target
188 // is a directory.
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000189 if (Status->isSymlink()) {
190 FileSpec symlink_filespec(Entry.path());
191 FileSpec resolved_filespec;
192 auto error = fs.ResolveSymbolicLink(symlink_filespec, resolved_filespec);
193 if (error.Success())
194 is_dir = fs.IsDirectory(symlink_filespec);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000195 }
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000196
Zachary Turner2cc5a182017-03-13 00:41:01 +0000197 if (only_directories && !is_dir)
198 continue;
199
200 // Shrink it back down so that it just has the original prefix the user
201 // typed and remove the part of the name which is common to the located
202 // item and what the user typed.
203 CompletionBuffer.resize(FullPrefixLen);
204 Name = Name.drop_front(PartialItem.size());
205 CompletionBuffer.append(Name);
206
207 if (is_dir) {
Zachary Turner2cc5a182017-03-13 00:41:01 +0000208 path::append(CompletionBuffer, path::get_separator());
209 }
210
Raphael Isemann5edee822019-08-27 11:32:22 +0000211 CompletionMode mode =
212 is_dir ? CompletionMode::Partial : CompletionMode::Normal;
213 request.AddCompletion(CompletionBuffer, "", mode);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000214 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215}
216
Raphael Isemann5edee822019-08-27 11:32:22 +0000217static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
218 bool only_directories, StringList &matches,
219 TildeExpressionResolver &Resolver) {
220 CompletionResult result;
221 std::string partial_name_str = partial_name.str();
222 CompletionRequest request(partial_name_str, partial_name_str.size(), result);
223 DiskFilesOrDirectories(partial_name, only_directories, request, Resolver);
224 result.GetMatches(matches);
225}
226
Raphael Isemannae34ed22019-08-22 07:41:23 +0000227static void DiskFilesOrDirectories(CompletionRequest &request,
228 bool only_directories) {
Jonas Devlieghere72787ac2018-11-09 01:59:28 +0000229 StandardTildeExpressionResolver resolver;
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000230 DiskFilesOrDirectories(request.GetCursorArgumentPrefix(), only_directories,
Raphael Isemann5edee822019-08-27 11:32:22 +0000231 request, resolver);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000232}
233
Raphael Isemannae34ed22019-08-22 07:41:23 +0000234void CommandCompletions::DiskFiles(CommandInterpreter &interpreter,
235 CompletionRequest &request,
236 SearchFilter *searcher) {
237 DiskFilesOrDirectories(request, /*only_dirs*/ false);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000238}
239
Raphael Isemannae34ed22019-08-22 07:41:23 +0000240void CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name,
241 StringList &matches,
242 TildeExpressionResolver &Resolver) {
243 DiskFilesOrDirectories(partial_file_name, false, matches, Resolver);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244}
245
Raphael Isemannae34ed22019-08-22 07:41:23 +0000246void CommandCompletions::DiskDirectories(CommandInterpreter &interpreter,
247 CompletionRequest &request,
248 SearchFilter *searcher) {
249 DiskFilesOrDirectories(request, /*only_dirs*/ true);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000250}
251
Raphael Isemannae34ed22019-08-22 07:41:23 +0000252void CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name,
253 StringList &matches,
254 TildeExpressionResolver &Resolver) {
255 DiskFilesOrDirectories(partial_file_name, true, matches, Resolver);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256}
257
Raphael Isemannae34ed22019-08-22 07:41:23 +0000258void CommandCompletions::Modules(CommandInterpreter &interpreter,
259 CompletionRequest &request,
260 SearchFilter *searcher) {
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000261 ModuleCompleter completer(interpreter, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262
263 if (searcher == nullptr) {
264 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
265 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
266 completer.DoCompletion(&null_searcher);
267 } else {
268 completer.DoCompletion(searcher);
269 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270}
271
Raphael Isemannae34ed22019-08-22 07:41:23 +0000272void CommandCompletions::Symbols(CommandInterpreter &interpreter,
273 CompletionRequest &request,
274 SearchFilter *searcher) {
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000275 SymbolCompleter completer(interpreter, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276
277 if (searcher == nullptr) {
278 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
279 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
280 completer.DoCompletion(&null_searcher);
281 } else {
282 completer.DoCompletion(searcher);
283 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284}
285
Raphael Isemannae34ed22019-08-22 07:41:23 +0000286void CommandCompletions::SettingsNames(CommandInterpreter &interpreter,
287 CompletionRequest &request,
288 SearchFilter *searcher) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 // Cache the full setting name list
290 static StringList g_property_names;
291 if (g_property_names.GetSize() == 0) {
292 // Generate the full setting name list on demand
293 lldb::OptionValuePropertiesSP properties_sp(
294 interpreter.GetDebugger().GetValueProperties());
295 if (properties_sp) {
296 StreamString strm;
297 properties_sp->DumpValue(nullptr, strm, OptionValue::eDumpOptionName);
Benjamin Krameradcd0262020-01-28 20:23:46 +0100298 const std::string &str = std::string(strm.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 g_property_names.SplitIntoLines(str.c_str(), str.size());
300 }
301 }
302
Raphael Isemann93ca36d2019-09-23 08:59:21 +0000303 for (const std::string &s : g_property_names)
304 request.TryCompleteCurrentArg(s);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305}
306
Raphael Isemannae34ed22019-08-22 07:41:23 +0000307void CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter,
308 CompletionRequest &request,
309 SearchFilter *searcher) {
310 PluginManager::AutoCompletePlatformName(request.GetCursorArgumentPrefix(),
311 request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312}
313
Raphael Isemannae34ed22019-08-22 07:41:23 +0000314void CommandCompletions::ArchitectureNames(CommandInterpreter &interpreter,
315 CompletionRequest &request,
316 SearchFilter *searcher) {
317 ArchSpec::AutoComplete(request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318}
319
Raphael Isemannae34ed22019-08-22 07:41:23 +0000320void CommandCompletions::VariablePath(CommandInterpreter &interpreter,
321 CompletionRequest &request,
322 SearchFilter *searcher) {
323 Variable::AutoComplete(interpreter.GetExecutionContext(), request);
Greg Claytonf21fead2013-05-14 23:43:18 +0000324}
325
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000326CommandCompletions::Completer::Completer(CommandInterpreter &interpreter,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000327 CompletionRequest &request)
328 : m_interpreter(interpreter), m_request(request) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000330CommandCompletions::Completer::~Completer() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332// SourceFileCompleter
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334CommandCompletions::SourceFileCompleter::SourceFileCompleter(
335 CommandInterpreter &interpreter, bool include_support_files,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000336 CompletionRequest &request)
337 : CommandCompletions::Completer(interpreter, request),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 m_include_support_files(include_support_files), m_matching_files() {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000339 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340 m_file_name = partial_spec.GetFilename().GetCString();
341 m_dir_name = partial_spec.GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342}
343
Jim Ingham4911d362018-09-07 18:43:04 +0000344lldb::SearchDepth CommandCompletions::SourceFileCompleter::GetDepth() {
345 return lldb::eSearchDepthCompUnit;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346}
347
348Searcher::CallbackReturn
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000349CommandCompletions::SourceFileCompleter::SearchCallback(SearchFilter &filter,
350 SymbolContext &context,
Raphael Isemann95e264f2019-10-10 11:26:51 +0000351 Address *addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 if (context.comp_unit != nullptr) {
353 if (m_include_support_files) {
354 FileSpecList supporting_files = context.comp_unit->GetSupportFiles();
355 for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++) {
356 const FileSpec &sfile_spec =
357 supporting_files.GetFileSpecAtIndex(sfiles);
358 const char *sfile_file_name = sfile_spec.GetFilename().GetCString();
359 const char *sfile_dir_name = sfile_spec.GetFilename().GetCString();
360 bool match = false;
361 if (m_file_name && sfile_file_name &&
362 strstr(sfile_file_name, m_file_name) == sfile_file_name)
363 match = true;
364 if (match && m_dir_name && sfile_dir_name &&
365 strstr(sfile_dir_name, m_dir_name) != sfile_dir_name)
366 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 if (match) {
369 m_matching_files.AppendIfUnique(sfile_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 }
372 } else {
Pavel Labath38870af2019-11-28 16:22:44 +0100373 const char *cur_file_name =
374 context.comp_unit->GetPrimaryFile().GetFilename().GetCString();
375 const char *cur_dir_name =
376 context.comp_unit->GetPrimaryFile().GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 bool match = false;
379 if (m_file_name && cur_file_name &&
380 strstr(cur_file_name, m_file_name) == cur_file_name)
381 match = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 if (match && m_dir_name && cur_dir_name &&
384 strstr(cur_dir_name, m_dir_name) != cur_dir_name)
385 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 if (match) {
Pavel Labath38870af2019-11-28 16:22:44 +0100388 m_matching_files.AppendIfUnique(context.comp_unit->GetPrimaryFile());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 }
392 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393}
394
Raphael Isemannae34ed22019-08-22 07:41:23 +0000395void CommandCompletions::SourceFileCompleter::DoCompletion(
396 SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 filter->Search(*this);
398 // Now convert the filelist to completions:
399 for (size_t i = 0; i < m_matching_files.GetSize(); i++) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000400 m_request.AddCompletion(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
402 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405// SymbolCompleter
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407static bool regex_chars(const char comp) {
Raphael Isemannd752b752019-11-29 12:26:33 +0100408 return llvm::StringRef("[](){}+.*|^$\\?").contains(comp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409}
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000410
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411CommandCompletions::SymbolCompleter::SymbolCompleter(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000412 CommandInterpreter &interpreter, CompletionRequest &request)
413 : CommandCompletions::Completer(interpreter, request) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 std::string regex_str;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000415 if (!m_request.GetCursorArgumentPrefix().empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416 regex_str.append("^");
Benjamin Krameradcd0262020-01-28 20:23:46 +0100417 regex_str.append(std::string(m_request.GetCursorArgumentPrefix()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 } else {
419 // Match anything since the completion string is empty
420 regex_str.append(".");
421 }
422 std::string::iterator pos =
423 find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
424 while (pos < regex_str.end()) {
425 pos = regex_str.insert(pos, '\\');
426 pos = find_if(pos + 2, regex_str.end(), regex_chars);
427 }
Jan Kratochvilf9d90bc2019-08-20 09:24:20 +0000428 m_regex = RegularExpression(regex_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429}
430
Jim Ingham4911d362018-09-07 18:43:04 +0000431lldb::SearchDepth CommandCompletions::SymbolCompleter::GetDepth() {
432 return lldb::eSearchDepthModule;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433}
434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435Searcher::CallbackReturn CommandCompletions::SymbolCompleter::SearchCallback(
Raphael Isemann95e264f2019-10-10 11:26:51 +0000436 SearchFilter &filter, SymbolContext &context, Address *addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437 if (context.module_sp) {
438 SymbolContextList sc_list;
439 const bool include_symbols = true;
440 const bool include_inlines = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 context.module_sp->FindFunctions(m_regex, include_symbols, include_inlines,
Adrian Prantl1ad655e2019-10-17 19:56:40 +0000442 sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 SymbolContext sc;
445 // Now add the functions & symbols to the list - only add if unique:
446 for (uint32_t i = 0; i < sc_list.GetSize(); i++) {
447 if (sc_list.GetContextAtIndex(i, sc)) {
448 ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Jonas Devlieghere07b1a2b2019-07-31 17:58:00 +0000449 // Ensure that the function name matches the regex. This is more than a
450 // sanity check. It is possible that the demangled function name does
451 // not start with the prefix, for example when it's in an anonymous
452 // namespace.
453 if (!func_name.IsEmpty() && m_regex.Execute(func_name.GetStringRef()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454 m_match_set.insert(func_name);
455 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 }
458 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459}
460
Raphael Isemannae34ed22019-08-22 07:41:23 +0000461void CommandCompletions::SymbolCompleter::DoCompletion(SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 filter->Search(*this);
463 collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
464 for (pos = m_match_set.begin(); pos != end; pos++)
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000465 m_request.AddCompletion((*pos).GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466}
467
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468// ModuleCompleter
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469CommandCompletions::ModuleCompleter::ModuleCompleter(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000470 CommandInterpreter &interpreter, CompletionRequest &request)
471 : CommandCompletions::Completer(interpreter, request) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000472 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 m_file_name = partial_spec.GetFilename().GetCString();
474 m_dir_name = partial_spec.GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475}
476
Jim Ingham4911d362018-09-07 18:43:04 +0000477lldb::SearchDepth CommandCompletions::ModuleCompleter::GetDepth() {
478 return lldb::eSearchDepthModule;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479}
480
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481Searcher::CallbackReturn CommandCompletions::ModuleCompleter::SearchCallback(
Raphael Isemann95e264f2019-10-10 11:26:51 +0000482 SearchFilter &filter, SymbolContext &context, Address *addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 if (context.module_sp) {
484 const char *cur_file_name =
485 context.module_sp->GetFileSpec().GetFilename().GetCString();
486 const char *cur_dir_name =
487 context.module_sp->GetFileSpec().GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 bool match = false;
490 if (m_file_name && cur_file_name &&
491 strstr(cur_file_name, m_file_name) == cur_file_name)
492 match = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 if (match && m_dir_name && cur_dir_name &&
495 strstr(cur_dir_name, m_dir_name) != cur_dir_name)
496 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 if (match) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000499 m_request.AddCompletion(cur_file_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 }
502 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503}
504
Raphael Isemannae34ed22019-08-22 07:41:23 +0000505void CommandCompletions::ModuleCompleter::DoCompletion(SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 filter->Search(*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507}