blob: a06f9a4db5397da570db2a4ec664dd4266dde495 [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,
90 bool only_directories, StringList &matches,
91 TildeExpressionResolver &Resolver) {
Zachary Turner2cc5a182017-03-13 00:41:01 +000092 matches.Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +000093
Zachary Turner2cc5a182017-03-13 00:41:01 +000094 llvm::SmallString<256> CompletionBuffer;
95 llvm::SmallString<256> Storage;
96 partial_name.toVector(CompletionBuffer);
Kate Stoneb9c1b512016-09-06 20:57:50 +000097
Zachary Turner2cc5a182017-03-13 00:41:01 +000098 if (CompletionBuffer.size() >= PATH_MAX)
Raphael Isemannae34ed22019-08-22 07:41:23 +000099 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100
Zachary Turner2cc5a182017-03-13 00:41:01 +0000101 namespace path = llvm::sys::path;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102
Zachary Turner2cc5a182017-03-13 00:41:01 +0000103 llvm::StringRef SearchDir;
104 llvm::StringRef PartialItem;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105
Zachary Turner2cc5a182017-03-13 00:41:01 +0000106 if (CompletionBuffer.startswith("~")) {
107 llvm::StringRef Buffer(CompletionBuffer);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000108 size_t FirstSep =
109 Buffer.find_if([](char c) { return path::is_separator(c); });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110
Zachary Turner2cc5a182017-03-13 00:41:01 +0000111 llvm::StringRef Username = Buffer.take_front(FirstSep);
112 llvm::StringRef Remainder;
113 if (FirstSep != llvm::StringRef::npos)
114 Remainder = Buffer.drop_front(FirstSep + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115
Stella Stamenovab3f44ad2018-12-10 17:23:28 +0000116 llvm::SmallString<256> Resolved;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000117 if (!Resolver.ResolveExact(Username, Resolved)) {
118 // We couldn't resolve it as a full username. If there were no slashes
119 // then this might be a partial username. We try to resolve it as such
120 // but after that, we're done regardless of any matches.
121 if (FirstSep == llvm::StringRef::npos) {
122 llvm::StringSet<> MatchSet;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000123 Resolver.ResolvePartial(Username, MatchSet);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000124 for (const auto &S : MatchSet) {
125 Resolved = S.getKey();
126 path::append(Resolved, path::get_separator());
127 matches.AppendString(Resolved);
128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 }
Raphael Isemannae34ed22019-08-22 07:41:23 +0000130 return;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000131 }
132
Adrian Prantl05097242018-04-30 16:49:04 +0000133 // If there was no trailing slash, then we're done as soon as we resolve
134 // the expression to the correct directory. Otherwise we need to continue
Zachary Turner2cc5a182017-03-13 00:41:01 +0000135 // looking for matches within that directory.
136 if (FirstSep == llvm::StringRef::npos) {
137 // Make sure it ends with a separator.
138 path::append(CompletionBuffer, path::get_separator());
Zachary Turner2cc5a182017-03-13 00:41:01 +0000139 matches.AppendString(CompletionBuffer);
Raphael Isemannae34ed22019-08-22 07:41:23 +0000140 return;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000141 }
142
143 // We want to keep the form the user typed, so we special case this to
144 // search in the fully resolved directory, but CompletionBuffer keeps the
145 // unmodified form that the user typed.
146 Storage = Resolved;
Pavel Labath88ec2e42018-06-29 10:27:18 +0000147 llvm::StringRef RemainderDir = path::parent_path(Remainder);
Raphael Isemann4621e0b2018-06-18 20:11:38 +0000148 if (!RemainderDir.empty()) {
149 // Append the remaining path to the resolved directory.
150 Storage.append(path::get_separator());
151 Storage.append(RemainderDir);
152 }
Raphael Isemann17843882018-01-22 09:17:16 +0000153 SearchDir = Storage;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000154 } else {
155 SearchDir = path::parent_path(CompletionBuffer);
Zachary Turnerd5bd3a12017-03-12 18:18:50 +0000156 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157
Zachary Turner2cc5a182017-03-13 00:41:01 +0000158 size_t FullPrefixLen = CompletionBuffer.size();
Zachary Turner0734e6a2017-03-12 20:01:37 +0000159
Zachary Turner2cc5a182017-03-13 00:41:01 +0000160 PartialItem = path::filename(CompletionBuffer);
Frederic Riss78a10a72018-08-31 23:03:28 +0000161
162 // path::filename() will return "." when the passed path ends with a
163 // directory separator. We have to filter those out, but only when the
164 // "." doesn't come from the completion request itself.
165 if (PartialItem == "." && path::is_separator(CompletionBuffer.back()))
Zachary Turner2cc5a182017-03-13 00:41:01 +0000166 PartialItem = llvm::StringRef();
Zachary Turner0734e6a2017-03-12 20:01:37 +0000167
Zachary Turner426d1372017-04-15 02:44:53 +0000168 if (SearchDir.empty()) {
169 llvm::sys::fs::current_path(Storage);
170 SearchDir = Storage;
171 }
Zachary Turner2cc5a182017-03-13 00:41:01 +0000172 assert(!PartialItem.contains(path::get_separator()));
Zachary Turner0734e6a2017-03-12 20:01:37 +0000173
Zachary Turner2cc5a182017-03-13 00:41:01 +0000174 // SearchDir now contains the directory to search in, and Prefix contains the
175 // text we want to match against items in that directory.
176
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000177 FileSystem &fs = FileSystem::Instance();
Zachary Turner2cc5a182017-03-13 00:41:01 +0000178 std::error_code EC;
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000179 llvm::vfs::directory_iterator Iter = fs.DirBegin(SearchDir, EC);
180 llvm::vfs::directory_iterator End;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000181 for (; Iter != End && !EC; Iter.increment(EC)) {
182 auto &Entry = *Iter;
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000183 llvm::ErrorOr<llvm::vfs::Status> Status = fs.GetStatus(Entry.path());
184
185 if (!Status)
186 continue;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000187
188 auto Name = path::filename(Entry.path());
189
190 // Omit ".", ".."
191 if (Name == "." || Name == ".." || !Name.startswith(PartialItem))
192 continue;
193
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000194 bool is_dir = Status->isDirectory();
Zachary Turner2cc5a182017-03-13 00:41:01 +0000195
196 // If it's a symlink, then we treat it as a directory as long as the target
197 // is a directory.
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000198 if (Status->isSymlink()) {
199 FileSpec symlink_filespec(Entry.path());
200 FileSpec resolved_filespec;
201 auto error = fs.ResolveSymbolicLink(symlink_filespec, resolved_filespec);
202 if (error.Success())
203 is_dir = fs.IsDirectory(symlink_filespec);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000204 }
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000205
Zachary Turner2cc5a182017-03-13 00:41:01 +0000206 if (only_directories && !is_dir)
207 continue;
208
209 // Shrink it back down so that it just has the original prefix the user
210 // typed and remove the part of the name which is common to the located
211 // item and what the user typed.
212 CompletionBuffer.resize(FullPrefixLen);
213 Name = Name.drop_front(PartialItem.size());
214 CompletionBuffer.append(Name);
215
216 if (is_dir) {
Zachary Turner2cc5a182017-03-13 00:41:01 +0000217 path::append(CompletionBuffer, path::get_separator());
218 }
219
220 matches.AppendString(CompletionBuffer);
221 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222}
223
Raphael Isemannae34ed22019-08-22 07:41:23 +0000224static void DiskFilesOrDirectories(CompletionRequest &request,
225 bool only_directories) {
Jonas Devlieghere72787ac2018-11-09 01:59:28 +0000226 StandardTildeExpressionResolver resolver;
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000227 StringList matches;
228 DiskFilesOrDirectories(request.GetCursorArgumentPrefix(), only_directories,
229 matches, resolver);
230 request.AddCompletions(matches);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000231}
232
Raphael Isemannae34ed22019-08-22 07:41:23 +0000233void CommandCompletions::DiskFiles(CommandInterpreter &interpreter,
234 CompletionRequest &request,
235 SearchFilter *searcher) {
236 DiskFilesOrDirectories(request, /*only_dirs*/ false);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000237}
238
Raphael Isemannae34ed22019-08-22 07:41:23 +0000239void CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name,
240 StringList &matches,
241 TildeExpressionResolver &Resolver) {
242 DiskFilesOrDirectories(partial_file_name, false, matches, Resolver);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243}
244
Raphael Isemannae34ed22019-08-22 07:41:23 +0000245void CommandCompletions::DiskDirectories(CommandInterpreter &interpreter,
246 CompletionRequest &request,
247 SearchFilter *searcher) {
248 DiskFilesOrDirectories(request, /*only_dirs*/ true);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000249}
250
Raphael Isemannae34ed22019-08-22 07:41:23 +0000251void CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name,
252 StringList &matches,
253 TildeExpressionResolver &Resolver) {
254 DiskFilesOrDirectories(partial_file_name, true, matches, Resolver);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255}
256
Raphael Isemannae34ed22019-08-22 07:41:23 +0000257void CommandCompletions::Modules(CommandInterpreter &interpreter,
258 CompletionRequest &request,
259 SearchFilter *searcher) {
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000260 ModuleCompleter completer(interpreter, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261
262 if (searcher == nullptr) {
263 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
264 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
265 completer.DoCompletion(&null_searcher);
266 } else {
267 completer.DoCompletion(searcher);
268 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269}
270
Raphael Isemannae34ed22019-08-22 07:41:23 +0000271void CommandCompletions::Symbols(CommandInterpreter &interpreter,
272 CompletionRequest &request,
273 SearchFilter *searcher) {
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000274 SymbolCompleter completer(interpreter, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275
276 if (searcher == nullptr) {
277 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
278 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
279 completer.DoCompletion(&null_searcher);
280 } else {
281 completer.DoCompletion(searcher);
282 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283}
284
Raphael Isemannae34ed22019-08-22 07:41:23 +0000285void CommandCompletions::SettingsNames(CommandInterpreter &interpreter,
286 CompletionRequest &request,
287 SearchFilter *searcher) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 // Cache the full setting name list
289 static StringList g_property_names;
290 if (g_property_names.GetSize() == 0) {
291 // Generate the full setting name list on demand
292 lldb::OptionValuePropertiesSP properties_sp(
293 interpreter.GetDebugger().GetValueProperties());
294 if (properties_sp) {
295 StreamString strm;
296 properties_sp->DumpValue(nullptr, strm, OptionValue::eDumpOptionName);
297 const std::string &str = strm.GetString();
298 g_property_names.SplitIntoLines(str.c_str(), str.size());
299 }
300 }
301
Raphael Isemannb8639f52019-08-19 08:15:46 +0000302 for (const std::string &s : g_property_names) {
Raphael Isemannae34ed22019-08-22 07:41:23 +0000303 if (llvm::StringRef(s).startswith(request.GetCursorArgumentPrefix()))
Raphael Isemannb8639f52019-08-19 08:15:46 +0000304 request.AddCompletion(s);
Raphael Isemannb8639f52019-08-19 08:15:46 +0000305 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306}
307
Raphael Isemannae34ed22019-08-22 07:41:23 +0000308void CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter,
309 CompletionRequest &request,
310 SearchFilter *searcher) {
311 PluginManager::AutoCompletePlatformName(request.GetCursorArgumentPrefix(),
312 request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313}
314
Raphael Isemannae34ed22019-08-22 07:41:23 +0000315void CommandCompletions::ArchitectureNames(CommandInterpreter &interpreter,
316 CompletionRequest &request,
317 SearchFilter *searcher) {
318 ArchSpec::AutoComplete(request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319}
320
Raphael Isemannae34ed22019-08-22 07:41:23 +0000321void CommandCompletions::VariablePath(CommandInterpreter &interpreter,
322 CompletionRequest &request,
323 SearchFilter *searcher) {
324 Variable::AutoComplete(interpreter.GetExecutionContext(), request);
Greg Claytonf21fead2013-05-14 23:43:18 +0000325}
326
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000327CommandCompletions::Completer::Completer(CommandInterpreter &interpreter,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000328 CompletionRequest &request)
329 : m_interpreter(interpreter), m_request(request) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000331CommandCompletions::Completer::~Completer() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333// SourceFileCompleter
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335CommandCompletions::SourceFileCompleter::SourceFileCompleter(
336 CommandInterpreter &interpreter, bool include_support_files,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000337 CompletionRequest &request)
338 : CommandCompletions::Completer(interpreter, request),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 m_include_support_files(include_support_files), m_matching_files() {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000340 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 m_file_name = partial_spec.GetFilename().GetCString();
342 m_dir_name = partial_spec.GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343}
344
Jim Ingham4911d362018-09-07 18:43:04 +0000345lldb::SearchDepth CommandCompletions::SourceFileCompleter::GetDepth() {
346 return lldb::eSearchDepthCompUnit;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347}
348
349Searcher::CallbackReturn
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000350CommandCompletions::SourceFileCompleter::SearchCallback(SearchFilter &filter,
351 SymbolContext &context,
352 Address *addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 bool complete) {
354 if (context.comp_unit != nullptr) {
355 if (m_include_support_files) {
356 FileSpecList supporting_files = context.comp_unit->GetSupportFiles();
357 for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++) {
358 const FileSpec &sfile_spec =
359 supporting_files.GetFileSpecAtIndex(sfiles);
360 const char *sfile_file_name = sfile_spec.GetFilename().GetCString();
361 const char *sfile_dir_name = sfile_spec.GetFilename().GetCString();
362 bool match = false;
363 if (m_file_name && sfile_file_name &&
364 strstr(sfile_file_name, m_file_name) == sfile_file_name)
365 match = true;
366 if (match && m_dir_name && sfile_dir_name &&
367 strstr(sfile_dir_name, m_dir_name) != sfile_dir_name)
368 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 if (match) {
371 m_matching_files.AppendIfUnique(sfile_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373 }
374 } else {
375 const char *cur_file_name = context.comp_unit->GetFilename().GetCString();
376 const char *cur_dir_name = context.comp_unit->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) {
388 m_matching_files.AppendIfUnique(context.comp_unit);
389 }
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) {
408 return (comp == '[' || comp == ']' || comp == '(' || comp == ')' ||
409 comp == '{' || comp == '}' || comp == '+' || comp == '.' ||
410 comp == '*' || comp == '|' || comp == '^' || comp == '$' ||
411 comp == '\\' || comp == '?');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414CommandCompletions::SymbolCompleter::SymbolCompleter(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000415 CommandInterpreter &interpreter, CompletionRequest &request)
416 : CommandCompletions::Completer(interpreter, request) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417 std::string regex_str;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000418 if (!m_request.GetCursorArgumentPrefix().empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 regex_str.append("^");
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000420 regex_str.append(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 } else {
422 // Match anything since the completion string is empty
423 regex_str.append(".");
424 }
425 std::string::iterator pos =
426 find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
427 while (pos < regex_str.end()) {
428 pos = regex_str.insert(pos, '\\');
429 pos = find_if(pos + 2, regex_str.end(), regex_chars);
430 }
Jan Kratochvilf9d90bc2019-08-20 09:24:20 +0000431 m_regex = RegularExpression(regex_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432}
433
Jim Ingham4911d362018-09-07 18:43:04 +0000434lldb::SearchDepth CommandCompletions::SymbolCompleter::GetDepth() {
435 return lldb::eSearchDepthModule;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436}
437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438Searcher::CallbackReturn CommandCompletions::SymbolCompleter::SearchCallback(
439 SearchFilter &filter, SymbolContext &context, Address *addr,
440 bool complete) {
441 if (context.module_sp) {
442 SymbolContextList sc_list;
443 const bool include_symbols = true;
444 const bool include_inlines = true;
445 const bool append = true;
446 context.module_sp->FindFunctions(m_regex, include_symbols, include_inlines,
447 append, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 SymbolContext sc;
450 // Now add the functions & symbols to the list - only add if unique:
451 for (uint32_t i = 0; i < sc_list.GetSize(); i++) {
452 if (sc_list.GetContextAtIndex(i, sc)) {
453 ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Jonas Devlieghere07b1a2b2019-07-31 17:58:00 +0000454 // Ensure that the function name matches the regex. This is more than a
455 // sanity check. It is possible that the demangled function name does
456 // not start with the prefix, for example when it's in an anonymous
457 // namespace.
458 if (!func_name.IsEmpty() && m_regex.Execute(func_name.GetStringRef()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 m_match_set.insert(func_name);
460 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 }
463 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464}
465
Raphael Isemannae34ed22019-08-22 07:41:23 +0000466void CommandCompletions::SymbolCompleter::DoCompletion(SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 filter->Search(*this);
468 collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
469 for (pos = m_match_set.begin(); pos != end; pos++)
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000470 m_request.AddCompletion((*pos).GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471}
472
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473// ModuleCompleter
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474CommandCompletions::ModuleCompleter::ModuleCompleter(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000475 CommandInterpreter &interpreter, CompletionRequest &request)
476 : CommandCompletions::Completer(interpreter, request) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000477 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 m_file_name = partial_spec.GetFilename().GetCString();
479 m_dir_name = partial_spec.GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480}
481
Jim Ingham4911d362018-09-07 18:43:04 +0000482lldb::SearchDepth CommandCompletions::ModuleCompleter::GetDepth() {
483 return lldb::eSearchDepthModule;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484}
485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486Searcher::CallbackReturn CommandCompletions::ModuleCompleter::SearchCallback(
487 SearchFilter &filter, SymbolContext &context, Address *addr,
488 bool complete) {
489 if (context.module_sp) {
490 const char *cur_file_name =
491 context.module_sp->GetFileSpec().GetFilename().GetCString();
492 const char *cur_dir_name =
493 context.module_sp->GetFileSpec().GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 bool match = false;
496 if (m_file_name && cur_file_name &&
497 strstr(cur_file_name, m_file_name) == cur_file_name)
498 match = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 if (match && m_dir_name && cur_dir_name &&
501 strstr(cur_dir_name, m_dir_name) != cur_dir_name)
502 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 if (match) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000505 m_request.AddCompletion(cur_file_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 }
508 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509}
510
Raphael Isemannae34ed22019-08-22 07:41:23 +0000511void CommandCompletions::ModuleCompleter::DoCompletion(SearchFilter *filter) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 filter->Search(*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513}