blob: f00a35d3d81b124f8a2a8b5d5c1fb40a23b4212e [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 Isemannb8639f52019-08-19 08:15:46 +0000311 for (const std::string &s : g_property_names) {
Raphael Isemannae34ed22019-08-22 07:41:23 +0000312 if (llvm::StringRef(s).startswith(request.GetCursorArgumentPrefix()))
Raphael Isemannb8639f52019-08-19 08:15:46 +0000313 request.AddCompletion(s);
Raphael Isemannb8639f52019-08-19 08:15:46 +0000314 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315}
316
Raphael Isemannae34ed22019-08-22 07:41:23 +0000317void CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter,
318 CompletionRequest &request,
319 SearchFilter *searcher) {
320 PluginManager::AutoCompletePlatformName(request.GetCursorArgumentPrefix(),
321 request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322}
323
Raphael Isemannae34ed22019-08-22 07:41:23 +0000324void CommandCompletions::ArchitectureNames(CommandInterpreter &interpreter,
325 CompletionRequest &request,
326 SearchFilter *searcher) {
327 ArchSpec::AutoComplete(request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328}
329
Raphael Isemannae34ed22019-08-22 07:41:23 +0000330void CommandCompletions::VariablePath(CommandInterpreter &interpreter,
331 CompletionRequest &request,
332 SearchFilter *searcher) {
333 Variable::AutoComplete(interpreter.GetExecutionContext(), request);
Greg Claytonf21fead2013-05-14 23:43:18 +0000334}
335
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000336CommandCompletions::Completer::Completer(CommandInterpreter &interpreter,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000337 CompletionRequest &request)
338 : m_interpreter(interpreter), m_request(request) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000340CommandCompletions::Completer::~Completer() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342// SourceFileCompleter
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344CommandCompletions::SourceFileCompleter::SourceFileCompleter(
345 CommandInterpreter &interpreter, bool include_support_files,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000346 CompletionRequest &request)
347 : CommandCompletions::Completer(interpreter, request),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 m_include_support_files(include_support_files), m_matching_files() {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000349 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 m_file_name = partial_spec.GetFilename().GetCString();
351 m_dir_name = partial_spec.GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352}
353
Jim Ingham4911d362018-09-07 18:43:04 +0000354lldb::SearchDepth CommandCompletions::SourceFileCompleter::GetDepth() {
355 return lldb::eSearchDepthCompUnit;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356}
357
358Searcher::CallbackReturn
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000359CommandCompletions::SourceFileCompleter::SearchCallback(SearchFilter &filter,
360 SymbolContext &context,
361 Address *addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 bool complete) {
363 if (context.comp_unit != nullptr) {
364 if (m_include_support_files) {
365 FileSpecList supporting_files = context.comp_unit->GetSupportFiles();
366 for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++) {
367 const FileSpec &sfile_spec =
368 supporting_files.GetFileSpecAtIndex(sfiles);
369 const char *sfile_file_name = sfile_spec.GetFilename().GetCString();
370 const char *sfile_dir_name = sfile_spec.GetFilename().GetCString();
371 bool match = false;
372 if (m_file_name && sfile_file_name &&
373 strstr(sfile_file_name, m_file_name) == sfile_file_name)
374 match = true;
375 if (match && m_dir_name && sfile_dir_name &&
376 strstr(sfile_dir_name, m_dir_name) != sfile_dir_name)
377 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 if (match) {
380 m_matching_files.AppendIfUnique(sfile_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 }
383 } else {
384 const char *cur_file_name = context.comp_unit->GetFilename().GetCString();
385 const char *cur_dir_name = context.comp_unit->GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 bool match = false;
388 if (m_file_name && cur_file_name &&
389 strstr(cur_file_name, m_file_name) == cur_file_name)
390 match = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 if (match && m_dir_name && cur_dir_name &&
393 strstr(cur_dir_name, m_dir_name) != cur_dir_name)
394 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 if (match) {
397 m_matching_files.AppendIfUnique(context.comp_unit);
398 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 }
401 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402}
403
Raphael Isemannae34ed22019-08-22 07:41:23 +0000404void CommandCompletions::SourceFileCompleter::DoCompletion(
405 SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 filter->Search(*this);
407 // Now convert the filelist to completions:
408 for (size_t i = 0; i < m_matching_files.GetSize(); i++) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000409 m_request.AddCompletion(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
411 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
413
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414// SymbolCompleter
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416static bool regex_chars(const char comp) {
417 return (comp == '[' || comp == ']' || comp == '(' || comp == ')' ||
418 comp == '{' || comp == '}' || comp == '+' || comp == '.' ||
419 comp == '*' || comp == '|' || comp == '^' || comp == '$' ||
420 comp == '\\' || comp == '?');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421}
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423CommandCompletions::SymbolCompleter::SymbolCompleter(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000424 CommandInterpreter &interpreter, CompletionRequest &request)
425 : CommandCompletions::Completer(interpreter, request) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 std::string regex_str;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000427 if (!m_request.GetCursorArgumentPrefix().empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 regex_str.append("^");
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000429 regex_str.append(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 } else {
431 // Match anything since the completion string is empty
432 regex_str.append(".");
433 }
434 std::string::iterator pos =
435 find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
436 while (pos < regex_str.end()) {
437 pos = regex_str.insert(pos, '\\');
438 pos = find_if(pos + 2, regex_str.end(), regex_chars);
439 }
Jan Kratochvilf9d90bc2019-08-20 09:24:20 +0000440 m_regex = RegularExpression(regex_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441}
442
Jim Ingham4911d362018-09-07 18:43:04 +0000443lldb::SearchDepth CommandCompletions::SymbolCompleter::GetDepth() {
444 return lldb::eSearchDepthModule;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445}
446
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447Searcher::CallbackReturn CommandCompletions::SymbolCompleter::SearchCallback(
448 SearchFilter &filter, SymbolContext &context, Address *addr,
449 bool complete) {
450 if (context.module_sp) {
451 SymbolContextList sc_list;
452 const bool include_symbols = true;
453 const bool include_inlines = true;
454 const bool append = true;
455 context.module_sp->FindFunctions(m_regex, include_symbols, include_inlines,
456 append, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 SymbolContext sc;
459 // Now add the functions & symbols to the list - only add if unique:
460 for (uint32_t i = 0; i < sc_list.GetSize(); i++) {
461 if (sc_list.GetContextAtIndex(i, sc)) {
462 ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Jonas Devlieghere07b1a2b2019-07-31 17:58:00 +0000463 // Ensure that the function name matches the regex. This is more than a
464 // sanity check. It is possible that the demangled function name does
465 // not start with the prefix, for example when it's in an anonymous
466 // namespace.
467 if (!func_name.IsEmpty() && m_regex.Execute(func_name.GetStringRef()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 m_match_set.insert(func_name);
469 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 }
472 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473}
474
Raphael Isemannae34ed22019-08-22 07:41:23 +0000475void CommandCompletions::SymbolCompleter::DoCompletion(SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 filter->Search(*this);
477 collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
478 for (pos = m_match_set.begin(); pos != end; pos++)
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000479 m_request.AddCompletion((*pos).GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480}
481
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482// ModuleCompleter
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483CommandCompletions::ModuleCompleter::ModuleCompleter(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000484 CommandInterpreter &interpreter, CompletionRequest &request)
485 : CommandCompletions::Completer(interpreter, request) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000486 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487 m_file_name = partial_spec.GetFilename().GetCString();
488 m_dir_name = partial_spec.GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489}
490
Jim Ingham4911d362018-09-07 18:43:04 +0000491lldb::SearchDepth CommandCompletions::ModuleCompleter::GetDepth() {
492 return lldb::eSearchDepthModule;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493}
494
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495Searcher::CallbackReturn CommandCompletions::ModuleCompleter::SearchCallback(
496 SearchFilter &filter, SymbolContext &context, Address *addr,
497 bool complete) {
498 if (context.module_sp) {
499 const char *cur_file_name =
500 context.module_sp->GetFileSpec().GetFilename().GetCString();
501 const char *cur_dir_name =
502 context.module_sp->GetFileSpec().GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 bool match = false;
505 if (m_file_name && cur_file_name &&
506 strstr(cur_file_name, m_file_name) == cur_file_name)
507 match = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 if (match && m_dir_name && cur_dir_name &&
510 strstr(cur_dir_name, m_dir_name) != cur_dir_name)
511 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 if (match) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000514 m_request.AddCompletion(cur_file_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 }
517 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518}
519
Raphael Isemannae34ed22019-08-22 07:41:23 +0000520void CommandCompletions::ModuleCompleter::DoCompletion(SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 filter->Search(*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522}