blob: 469a6bbbadf659730d665f010183dd244c5cc4f2 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandCompletions.cpp ----------------------------------*- C++ -*-===//
2//
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
Jim Ingham558ce122010-06-30 05:02:46 +00009#include <sys/stat.h>
Greg Claytonfd184262011-02-05 02:27:52 +000010#if defined(__APPLE__) || defined(__linux__)
Jim Ingham7cc478b2010-07-02 00:45:55 +000011#include <pwd.h>
Greg Claytonfd184262011-02-05 02:27:52 +000012#endif
Jim Ingham558ce122010-06-30 05:02:46 +000013
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000014#include "llvm/ADT/SmallString.h"
Zachary Turner2cc5a182017-03-13 00:41:01 +000015#include "llvm/ADT/StringSet.h"
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000016
Greg Clayton9b62fd22011-02-01 05:15:22 +000017#include "lldb/Core/FileSpecList.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/Module.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000019#include "lldb/Core/PluginManager.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000020#include "lldb/Host/FileSystem.h"
Greg Clayton9b62fd22011-02-01 05:15:22 +000021#include "lldb/Interpreter/CommandCompletions.h"
22#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000023#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Symbol/CompileUnit.h"
Greg Claytonf21fead2013-05-14 23:43:18 +000025#include "lldb/Symbol/Variable.h"
Greg Clayton9b62fd22011-02-01 05:15:22 +000026#include "lldb/Target/Target.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000027#include "lldb/Utility/Args.h"
Zachary Turner5713a052017-03-22 18:40:07 +000028#include "lldb/Utility/FileSpec.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000029#include "lldb/Utility/StreamString.h"
Zachary Turner2cc5a182017-03-13 00:41:01 +000030#include "lldb/Utility/TildeExpressionResolver.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Zachary Turner190fadc2016-03-22 17:58:09 +000032#include "llvm/ADT/SmallString.h"
Zachary Turner7d86ee52017-03-08 17:56:08 +000033#include "llvm/Support/FileSystem.h"
Zachary Turner2cc5a182017-03-13 00:41:01 +000034#include "llvm/Support/Path.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000035
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036using namespace lldb_private;
37
38CommandCompletions::CommonCompletionElement
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 CommandCompletions::g_common_completions[] = {
40 {eCustomCompletion, nullptr},
41 {eSourceFileCompletion, CommandCompletions::SourceFiles},
42 {eDiskFileCompletion, CommandCompletions::DiskFiles},
43 {eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
44 {eSymbolCompletion, CommandCompletions::Symbols},
45 {eModuleCompletion, CommandCompletions::Modules},
46 {eSettingsNameCompletion, CommandCompletions::SettingsNames},
47 {ePlatformPluginCompletion, CommandCompletions::PlatformPluginNames},
48 {eArchitectureCompletion, CommandCompletions::ArchitectureNames},
49 {eVariablePathCompletion, CommandCompletions::VariablePath},
50 {eNoCompletion, nullptr} // This one has to be last in the list.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051};
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053bool CommandCompletions::InvokeCommonCompletionCallbacks(
54 CommandInterpreter &interpreter, uint32_t completion_mask,
Raphael Isemanna2e76c02018-07-13 18:28:14 +000055 CompletionRequest &request, SearchFilter *searcher) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 bool handled = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 if (completion_mask & eCustomCompletion)
59 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 for (int i = 0;; i++) {
62 if (g_common_completions[i].type == eNoCompletion)
63 break;
64 else if ((g_common_completions[i].type & completion_mask) ==
65 g_common_completions[i].type &&
66 g_common_completions[i].callback != nullptr) {
67 handled = true;
Raphael Isemanna2e76c02018-07-13 18:28:14 +000068 g_common_completions[i].callback(interpreter, request, searcher);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 }
71 return handled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072}
73
Raphael Isemannae34ed22019-08-22 07:41:23 +000074void CommandCompletions::SourceFiles(CommandInterpreter &interpreter,
75 CompletionRequest &request,
76 SearchFilter *searcher) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 // Find some way to switch "include support files..."
Raphael Isemanna2e76c02018-07-13 18:28:14 +000078 SourceFileCompleter completer(interpreter, false, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +000079
80 if (searcher == nullptr) {
81 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
82 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
83 completer.DoCompletion(&null_searcher);
84 } else {
85 completer.DoCompletion(searcher);
86 }
Jim Ingham558ce122010-06-30 05:02:46 +000087}
88
Raphael Isemannae34ed22019-08-22 07:41:23 +000089static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
Raphael Isemann5edee822019-08-27 11:32:22 +000090 bool only_directories,
91 CompletionRequest &request,
Raphael Isemannae34ed22019-08-22 07:41:23 +000092 TildeExpressionResolver &Resolver) {
Zachary Turner2cc5a182017-03-13 00:41:01 +000093 llvm::SmallString<256> CompletionBuffer;
94 llvm::SmallString<256> Storage;
95 partial_name.toVector(CompletionBuffer);
Kate Stoneb9c1b512016-09-06 20:57:50 +000096
Zachary Turner2cc5a182017-03-13 00:41:01 +000097 if (CompletionBuffer.size() >= PATH_MAX)
Raphael Isemannae34ed22019-08-22 07:41:23 +000098 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099
Zachary Turner2cc5a182017-03-13 00:41:01 +0000100 namespace path = llvm::sys::path;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101
Zachary Turner2cc5a182017-03-13 00:41:01 +0000102 llvm::StringRef SearchDir;
103 llvm::StringRef PartialItem;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104
Zachary Turner2cc5a182017-03-13 00:41:01 +0000105 if (CompletionBuffer.startswith("~")) {
106 llvm::StringRef Buffer(CompletionBuffer);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000107 size_t FirstSep =
108 Buffer.find_if([](char c) { return path::is_separator(c); });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109
Zachary Turner2cc5a182017-03-13 00:41:01 +0000110 llvm::StringRef Username = Buffer.take_front(FirstSep);
111 llvm::StringRef Remainder;
112 if (FirstSep != llvm::StringRef::npos)
113 Remainder = Buffer.drop_front(FirstSep + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114
Stella Stamenovab3f44ad2018-12-10 17:23:28 +0000115 llvm::SmallString<256> Resolved;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000116 if (!Resolver.ResolveExact(Username, Resolved)) {
117 // We couldn't resolve it as a full username. If there were no slashes
118 // then this might be a partial username. We try to resolve it as such
119 // but after that, we're done regardless of any matches.
120 if (FirstSep == llvm::StringRef::npos) {
121 llvm::StringSet<> MatchSet;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000122 Resolver.ResolvePartial(Username, MatchSet);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000123 for (const auto &S : MatchSet) {
124 Resolved = S.getKey();
125 path::append(Resolved, path::get_separator());
Raphael Isemann5edee822019-08-27 11:32:22 +0000126 request.AddCompletion(Resolved, "", CompletionMode::Partial);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000127 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 }
Raphael Isemannae34ed22019-08-22 07:41:23 +0000129 return;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000130 }
131
Adrian Prantl05097242018-04-30 16:49:04 +0000132 // If there was no trailing slash, then we're done as soon as we resolve
133 // the expression to the correct directory. Otherwise we need to continue
Zachary Turner2cc5a182017-03-13 00:41:01 +0000134 // looking for matches within that directory.
135 if (FirstSep == llvm::StringRef::npos) {
136 // Make sure it ends with a separator.
137 path::append(CompletionBuffer, path::get_separator());
Raphael Isemann5edee822019-08-27 11:32:22 +0000138 request.AddCompletion(CompletionBuffer, "", CompletionMode::Partial);
Raphael Isemannae34ed22019-08-22 07:41:23 +0000139 return;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000140 }
141
142 // We want to keep the form the user typed, so we special case this to
143 // search in the fully resolved directory, but CompletionBuffer keeps the
144 // unmodified form that the user typed.
145 Storage = Resolved;
Pavel Labath88ec2e42018-06-29 10:27:18 +0000146 llvm::StringRef RemainderDir = path::parent_path(Remainder);
Raphael Isemann4621e0b2018-06-18 20:11:38 +0000147 if (!RemainderDir.empty()) {
148 // Append the remaining path to the resolved directory.
149 Storage.append(path::get_separator());
150 Storage.append(RemainderDir);
151 }
Raphael Isemann17843882018-01-22 09:17:16 +0000152 SearchDir = Storage;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000153 } else {
154 SearchDir = path::parent_path(CompletionBuffer);
Zachary Turnerd5bd3a12017-03-12 18:18:50 +0000155 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156
Zachary Turner2cc5a182017-03-13 00:41:01 +0000157 size_t FullPrefixLen = CompletionBuffer.size();
Zachary Turner0734e6a2017-03-12 20:01:37 +0000158
Zachary Turner2cc5a182017-03-13 00:41:01 +0000159 PartialItem = path::filename(CompletionBuffer);
Frederic Riss78a10a72018-08-31 23:03:28 +0000160
161 // path::filename() will return "." when the passed path ends with a
162 // directory separator. We have to filter those out, but only when the
163 // "." doesn't come from the completion request itself.
164 if (PartialItem == "." && path::is_separator(CompletionBuffer.back()))
Zachary Turner2cc5a182017-03-13 00:41:01 +0000165 PartialItem = llvm::StringRef();
Zachary Turner0734e6a2017-03-12 20:01:37 +0000166
Zachary Turner426d1372017-04-15 02:44:53 +0000167 if (SearchDir.empty()) {
168 llvm::sys::fs::current_path(Storage);
169 SearchDir = Storage;
170 }
Zachary Turner2cc5a182017-03-13 00:41:01 +0000171 assert(!PartialItem.contains(path::get_separator()));
Zachary Turner0734e6a2017-03-12 20:01:37 +0000172
Zachary Turner2cc5a182017-03-13 00:41:01 +0000173 // SearchDir now contains the directory to search in, and Prefix contains the
174 // text we want to match against items in that directory.
175
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000176 FileSystem &fs = FileSystem::Instance();
Zachary Turner2cc5a182017-03-13 00:41:01 +0000177 std::error_code EC;
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000178 llvm::vfs::directory_iterator Iter = fs.DirBegin(SearchDir, EC);
179 llvm::vfs::directory_iterator End;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000180 for (; Iter != End && !EC; Iter.increment(EC)) {
181 auto &Entry = *Iter;
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000182 llvm::ErrorOr<llvm::vfs::Status> Status = fs.GetStatus(Entry.path());
183
184 if (!Status)
185 continue;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000186
187 auto Name = path::filename(Entry.path());
188
189 // Omit ".", ".."
190 if (Name == "." || Name == ".." || !Name.startswith(PartialItem))
191 continue;
192
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000193 bool is_dir = Status->isDirectory();
Zachary Turner2cc5a182017-03-13 00:41:01 +0000194
195 // If it's a symlink, then we treat it as a directory as long as the target
196 // is a directory.
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000197 if (Status->isSymlink()) {
198 FileSpec symlink_filespec(Entry.path());
199 FileSpec resolved_filespec;
200 auto error = fs.ResolveSymbolicLink(symlink_filespec, resolved_filespec);
201 if (error.Success())
202 is_dir = fs.IsDirectory(symlink_filespec);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000203 }
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000204
Zachary Turner2cc5a182017-03-13 00:41:01 +0000205 if (only_directories && !is_dir)
206 continue;
207
208 // Shrink it back down so that it just has the original prefix the user
209 // typed and remove the part of the name which is common to the located
210 // item and what the user typed.
211 CompletionBuffer.resize(FullPrefixLen);
212 Name = Name.drop_front(PartialItem.size());
213 CompletionBuffer.append(Name);
214
215 if (is_dir) {
Zachary Turner2cc5a182017-03-13 00:41:01 +0000216 path::append(CompletionBuffer, path::get_separator());
217 }
218
Raphael Isemann5edee822019-08-27 11:32:22 +0000219 CompletionMode mode =
220 is_dir ? CompletionMode::Partial : CompletionMode::Normal;
221 request.AddCompletion(CompletionBuffer, "", mode);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000222 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223}
224
Raphael Isemann5edee822019-08-27 11:32:22 +0000225static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
226 bool only_directories, StringList &matches,
227 TildeExpressionResolver &Resolver) {
228 CompletionResult result;
229 std::string partial_name_str = partial_name.str();
230 CompletionRequest request(partial_name_str, partial_name_str.size(), result);
231 DiskFilesOrDirectories(partial_name, only_directories, request, Resolver);
232 result.GetMatches(matches);
233}
234
Raphael Isemannae34ed22019-08-22 07:41:23 +0000235static void DiskFilesOrDirectories(CompletionRequest &request,
236 bool only_directories) {
Jonas Devlieghere72787ac2018-11-09 01:59:28 +0000237 StandardTildeExpressionResolver resolver;
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000238 DiskFilesOrDirectories(request.GetCursorArgumentPrefix(), only_directories,
Raphael Isemann5edee822019-08-27 11:32:22 +0000239 request, resolver);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000240}
241
Raphael Isemannae34ed22019-08-22 07:41:23 +0000242void CommandCompletions::DiskFiles(CommandInterpreter &interpreter,
243 CompletionRequest &request,
244 SearchFilter *searcher) {
245 DiskFilesOrDirectories(request, /*only_dirs*/ false);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000246}
247
Raphael Isemannae34ed22019-08-22 07:41:23 +0000248void CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name,
249 StringList &matches,
250 TildeExpressionResolver &Resolver) {
251 DiskFilesOrDirectories(partial_file_name, false, matches, Resolver);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252}
253
Raphael Isemannae34ed22019-08-22 07:41:23 +0000254void CommandCompletions::DiskDirectories(CommandInterpreter &interpreter,
255 CompletionRequest &request,
256 SearchFilter *searcher) {
257 DiskFilesOrDirectories(request, /*only_dirs*/ true);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000258}
259
Raphael Isemannae34ed22019-08-22 07:41:23 +0000260void CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name,
261 StringList &matches,
262 TildeExpressionResolver &Resolver) {
263 DiskFilesOrDirectories(partial_file_name, true, matches, Resolver);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264}
265
Raphael Isemannae34ed22019-08-22 07:41:23 +0000266void CommandCompletions::Modules(CommandInterpreter &interpreter,
267 CompletionRequest &request,
268 SearchFilter *searcher) {
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000269 ModuleCompleter completer(interpreter, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270
271 if (searcher == nullptr) {
272 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
273 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
274 completer.DoCompletion(&null_searcher);
275 } else {
276 completer.DoCompletion(searcher);
277 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278}
279
Raphael Isemannae34ed22019-08-22 07:41:23 +0000280void CommandCompletions::Symbols(CommandInterpreter &interpreter,
281 CompletionRequest &request,
282 SearchFilter *searcher) {
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000283 SymbolCompleter completer(interpreter, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284
285 if (searcher == nullptr) {
286 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
287 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
288 completer.DoCompletion(&null_searcher);
289 } else {
290 completer.DoCompletion(searcher);
291 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292}
293
Raphael Isemannae34ed22019-08-22 07:41:23 +0000294void CommandCompletions::SettingsNames(CommandInterpreter &interpreter,
295 CompletionRequest &request,
296 SearchFilter *searcher) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 // Cache the full setting name list
298 static StringList g_property_names;
299 if (g_property_names.GetSize() == 0) {
300 // Generate the full setting name list on demand
301 lldb::OptionValuePropertiesSP properties_sp(
302 interpreter.GetDebugger().GetValueProperties());
303 if (properties_sp) {
304 StreamString strm;
305 properties_sp->DumpValue(nullptr, strm, OptionValue::eDumpOptionName);
306 const std::string &str = strm.GetString();
307 g_property_names.SplitIntoLines(str.c_str(), str.size());
308 }
309 }
310
Raphael Isemann93ca36d2019-09-23 08:59:21 +0000311 for (const std::string &s : g_property_names)
312 request.TryCompleteCurrentArg(s);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313}
314
Raphael Isemannae34ed22019-08-22 07:41:23 +0000315void CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter,
316 CompletionRequest &request,
317 SearchFilter *searcher) {
318 PluginManager::AutoCompletePlatformName(request.GetCursorArgumentPrefix(),
319 request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320}
321
Raphael Isemannae34ed22019-08-22 07:41:23 +0000322void CommandCompletions::ArchitectureNames(CommandInterpreter &interpreter,
323 CompletionRequest &request,
324 SearchFilter *searcher) {
325 ArchSpec::AutoComplete(request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326}
327
Raphael Isemannae34ed22019-08-22 07:41:23 +0000328void CommandCompletions::VariablePath(CommandInterpreter &interpreter,
329 CompletionRequest &request,
330 SearchFilter *searcher) {
331 Variable::AutoComplete(interpreter.GetExecutionContext(), request);
Greg Claytonf21fead2013-05-14 23:43:18 +0000332}
333
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000334CommandCompletions::Completer::Completer(CommandInterpreter &interpreter,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000335 CompletionRequest &request)
336 : m_interpreter(interpreter), m_request(request) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000338CommandCompletions::Completer::~Completer() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340// SourceFileCompleter
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342CommandCompletions::SourceFileCompleter::SourceFileCompleter(
343 CommandInterpreter &interpreter, bool include_support_files,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000344 CompletionRequest &request)
345 : CommandCompletions::Completer(interpreter, request),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 m_include_support_files(include_support_files), m_matching_files() {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000347 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 m_file_name = partial_spec.GetFilename().GetCString();
349 m_dir_name = partial_spec.GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350}
351
Jim Ingham4911d362018-09-07 18:43:04 +0000352lldb::SearchDepth CommandCompletions::SourceFileCompleter::GetDepth() {
353 return lldb::eSearchDepthCompUnit;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354}
355
356Searcher::CallbackReturn
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000357CommandCompletions::SourceFileCompleter::SearchCallback(SearchFilter &filter,
358 SymbolContext &context,
Raphael Isemann95e264f2019-10-10 11:26:51 +0000359 Address *addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 if (context.comp_unit != nullptr) {
361 if (m_include_support_files) {
362 FileSpecList supporting_files = context.comp_unit->GetSupportFiles();
363 for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++) {
364 const FileSpec &sfile_spec =
365 supporting_files.GetFileSpecAtIndex(sfiles);
366 const char *sfile_file_name = sfile_spec.GetFilename().GetCString();
367 const char *sfile_dir_name = sfile_spec.GetFilename().GetCString();
368 bool match = false;
369 if (m_file_name && sfile_file_name &&
370 strstr(sfile_file_name, m_file_name) == sfile_file_name)
371 match = true;
372 if (match && m_dir_name && sfile_dir_name &&
373 strstr(sfile_dir_name, m_dir_name) != sfile_dir_name)
374 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 if (match) {
377 m_matching_files.AppendIfUnique(sfile_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 }
380 } else {
381 const char *cur_file_name = context.comp_unit->GetFilename().GetCString();
382 const char *cur_dir_name = context.comp_unit->GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 bool match = false;
385 if (m_file_name && cur_file_name &&
386 strstr(cur_file_name, m_file_name) == cur_file_name)
387 match = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 if (match && m_dir_name && cur_dir_name &&
390 strstr(cur_dir_name, m_dir_name) != cur_dir_name)
391 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 if (match) {
394 m_matching_files.AppendIfUnique(context.comp_unit);
395 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 }
398 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399}
400
Raphael Isemannae34ed22019-08-22 07:41:23 +0000401void CommandCompletions::SourceFileCompleter::DoCompletion(
402 SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 filter->Search(*this);
404 // Now convert the filelist to completions:
405 for (size_t i = 0; i < m_matching_files.GetSize(); i++) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000406 m_request.AddCompletion(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
408 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409}
410
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411// SymbolCompleter
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413static bool regex_chars(const char comp) {
414 return (comp == '[' || comp == ']' || comp == '(' || comp == ')' ||
415 comp == '{' || comp == '}' || comp == '+' || comp == '.' ||
416 comp == '*' || comp == '|' || comp == '^' || comp == '$' ||
417 comp == '\\' || comp == '?');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418}
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420CommandCompletions::SymbolCompleter::SymbolCompleter(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000421 CommandInterpreter &interpreter, CompletionRequest &request)
422 : CommandCompletions::Completer(interpreter, request) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 std::string regex_str;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000424 if (!m_request.GetCursorArgumentPrefix().empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 regex_str.append("^");
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000426 regex_str.append(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 } else {
428 // Match anything since the completion string is empty
429 regex_str.append(".");
430 }
431 std::string::iterator pos =
432 find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
433 while (pos < regex_str.end()) {
434 pos = regex_str.insert(pos, '\\');
435 pos = find_if(pos + 2, regex_str.end(), regex_chars);
436 }
Jan Kratochvilf9d90bc2019-08-20 09:24:20 +0000437 m_regex = RegularExpression(regex_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438}
439
Jim Ingham4911d362018-09-07 18:43:04 +0000440lldb::SearchDepth CommandCompletions::SymbolCompleter::GetDepth() {
441 return lldb::eSearchDepthModule;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442}
443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444Searcher::CallbackReturn CommandCompletions::SymbolCompleter::SearchCallback(
Raphael Isemann95e264f2019-10-10 11:26:51 +0000445 SearchFilter &filter, SymbolContext &context, Address *addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 if (context.module_sp) {
447 SymbolContextList sc_list;
448 const bool include_symbols = true;
449 const bool include_inlines = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 context.module_sp->FindFunctions(m_regex, include_symbols, include_inlines,
Adrian Prantl1ad655e2019-10-17 19:56:40 +0000451 sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 SymbolContext sc;
454 // Now add the functions & symbols to the list - only add if unique:
455 for (uint32_t i = 0; i < sc_list.GetSize(); i++) {
456 if (sc_list.GetContextAtIndex(i, sc)) {
457 ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Jonas Devlieghere07b1a2b2019-07-31 17:58:00 +0000458 // Ensure that the function name matches the regex. This is more than a
459 // sanity check. It is possible that the demangled function name does
460 // not start with the prefix, for example when it's in an anonymous
461 // namespace.
462 if (!func_name.IsEmpty() && m_regex.Execute(func_name.GetStringRef()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 m_match_set.insert(func_name);
464 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466 }
467 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468}
469
Raphael Isemannae34ed22019-08-22 07:41:23 +0000470void CommandCompletions::SymbolCompleter::DoCompletion(SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 filter->Search(*this);
472 collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
473 for (pos = m_match_set.begin(); pos != end; pos++)
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000474 m_request.AddCompletion((*pos).GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475}
476
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477// ModuleCompleter
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478CommandCompletions::ModuleCompleter::ModuleCompleter(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000479 CommandInterpreter &interpreter, CompletionRequest &request)
480 : CommandCompletions::Completer(interpreter, request) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000481 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482 m_file_name = partial_spec.GetFilename().GetCString();
483 m_dir_name = partial_spec.GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484}
485
Jim Ingham4911d362018-09-07 18:43:04 +0000486lldb::SearchDepth CommandCompletions::ModuleCompleter::GetDepth() {
487 return lldb::eSearchDepthModule;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488}
489
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490Searcher::CallbackReturn CommandCompletions::ModuleCompleter::SearchCallback(
Raphael Isemann95e264f2019-10-10 11:26:51 +0000491 SearchFilter &filter, SymbolContext &context, Address *addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 if (context.module_sp) {
493 const char *cur_file_name =
494 context.module_sp->GetFileSpec().GetFilename().GetCString();
495 const char *cur_dir_name =
496 context.module_sp->GetFileSpec().GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 bool match = false;
499 if (m_file_name && cur_file_name &&
500 strstr(cur_file_name, m_file_name) == cur_file_name)
501 match = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 if (match && m_dir_name && cur_dir_name &&
504 strstr(cur_dir_name, m_dir_name) != cur_dir_name)
505 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 if (match) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000508 m_request.AddCompletion(cur_file_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 }
511 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512}
513
Raphael Isemannae34ed22019-08-22 07:41:23 +0000514void CommandCompletions::ModuleCompleter::DoCompletion(SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 filter->Search(*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516}