blob: 705e87651a8ca1ce57c99c3d79d87a058366ecdf [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandCompletions.cpp ----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Jim Ingham558ce122010-06-30 05:02:46 +000010#include <sys/stat.h>
Greg Claytonfd184262011-02-05 02:27:52 +000011#if defined(__APPLE__) || defined(__linux__)
Jim Ingham7cc478b2010-07-02 00:45:55 +000012#include <pwd.h>
Greg Claytonfd184262011-02-05 02:27:52 +000013#endif
Jim Ingham558ce122010-06-30 05:02:46 +000014
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000015#include "llvm/ADT/SmallString.h"
Zachary Turner2cc5a182017-03-13 00:41:01 +000016#include "llvm/ADT/StringSet.h"
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000017
Greg Clayton9b62fd22011-02-01 05:15:22 +000018#include "lldb/Core/FileSpecList.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/Module.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000020#include "lldb/Core/PluginManager.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000021#include "lldb/Host/FileSystem.h"
Greg Clayton9b62fd22011-02-01 05:15:22 +000022#include "lldb/Interpreter/CommandCompletions.h"
23#include "lldb/Interpreter/CommandInterpreter.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000024#include "lldb/Interpreter/OptionValueProperties.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Symbol/CompileUnit.h"
Greg Claytonf21fead2013-05-14 23:43:18 +000026#include "lldb/Symbol/Variable.h"
Greg Clayton9b62fd22011-02-01 05:15:22 +000027#include "lldb/Target/Target.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000028#include "lldb/Utility/Args.h"
Zachary Turner5713a052017-03-22 18:40:07 +000029#include "lldb/Utility/FileSpec.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000030#include "lldb/Utility/StreamString.h"
Zachary Turner2cc5a182017-03-13 00:41:01 +000031#include "lldb/Utility/TildeExpressionResolver.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Zachary Turner190fadc2016-03-22 17:58:09 +000033#include "llvm/ADT/SmallString.h"
Zachary Turner7d86ee52017-03-08 17:56:08 +000034#include "llvm/Support/FileSystem.h"
Zachary Turner2cc5a182017-03-13 00:41:01 +000035#include "llvm/Support/Path.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000036
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037using namespace lldb_private;
38
39CommandCompletions::CommonCompletionElement
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 CommandCompletions::g_common_completions[] = {
41 {eCustomCompletion, nullptr},
42 {eSourceFileCompletion, CommandCompletions::SourceFiles},
43 {eDiskFileCompletion, CommandCompletions::DiskFiles},
44 {eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
45 {eSymbolCompletion, CommandCompletions::Symbols},
46 {eModuleCompletion, CommandCompletions::Modules},
47 {eSettingsNameCompletion, CommandCompletions::SettingsNames},
48 {ePlatformPluginCompletion, CommandCompletions::PlatformPluginNames},
49 {eArchitectureCompletion, CommandCompletions::ArchitectureNames},
50 {eVariablePathCompletion, CommandCompletions::VariablePath},
51 {eNoCompletion, nullptr} // This one has to be last in the list.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052};
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054bool CommandCompletions::InvokeCommonCompletionCallbacks(
55 CommandInterpreter &interpreter, uint32_t completion_mask,
Raphael Isemanna2e76c02018-07-13 18:28:14 +000056 CompletionRequest &request, SearchFilter *searcher) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 bool handled = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 if (completion_mask & eCustomCompletion)
60 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 for (int i = 0;; i++) {
63 if (g_common_completions[i].type == eNoCompletion)
64 break;
65 else if ((g_common_completions[i].type & completion_mask) ==
66 g_common_completions[i].type &&
67 g_common_completions[i].callback != nullptr) {
68 handled = true;
Raphael Isemanna2e76c02018-07-13 18:28:14 +000069 g_common_completions[i].callback(interpreter, request, searcher);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 }
72 return handled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075int CommandCompletions::SourceFiles(CommandInterpreter &interpreter,
Raphael Isemanna2e76c02018-07-13 18:28:14 +000076 CompletionRequest &request,
77 SearchFilter *searcher) {
78 request.SetWordComplete(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 // Find some way to switch "include support files..."
Raphael Isemanna2e76c02018-07-13 18:28:14 +000080 SourceFileCompleter completer(interpreter, false, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +000081
82 if (searcher == nullptr) {
83 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
84 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
85 completer.DoCompletion(&null_searcher);
86 } else {
87 completer.DoCompletion(searcher);
88 }
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +000089 return request.GetNumberOfMatches();
Jim Ingham558ce122010-06-30 05:02:46 +000090}
91
Zachary Turner2cc5a182017-03-13 00:41:01 +000092static int DiskFilesOrDirectories(const llvm::Twine &partial_name,
Raphael Isemanna2e76c02018-07-13 18:28:14 +000093 bool only_directories, StringList &matches,
Zachary Turner2cc5a182017-03-13 00:41:01 +000094 TildeExpressionResolver &Resolver) {
95 matches.Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +000096
Zachary Turner2cc5a182017-03-13 00:41:01 +000097 llvm::SmallString<256> CompletionBuffer;
98 llvm::SmallString<256> Storage;
99 partial_name.toVector(CompletionBuffer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100
Zachary Turner2cc5a182017-03-13 00:41:01 +0000101 if (CompletionBuffer.size() >= PATH_MAX)
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000102 return matches.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103
Zachary Turner2cc5a182017-03-13 00:41:01 +0000104 namespace path = llvm::sys::path;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105
Zachary Turner2cc5a182017-03-13 00:41:01 +0000106 llvm::StringRef SearchDir;
107 llvm::StringRef PartialItem;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108
Zachary Turner2cc5a182017-03-13 00:41:01 +0000109 if (CompletionBuffer.startswith("~")) {
110 llvm::StringRef Buffer(CompletionBuffer);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000111 size_t FirstSep =
112 Buffer.find_if([](char c) { return path::is_separator(c); });
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113
Zachary Turner2cc5a182017-03-13 00:41:01 +0000114 llvm::StringRef Username = Buffer.take_front(FirstSep);
115 llvm::StringRef Remainder;
116 if (FirstSep != llvm::StringRef::npos)
117 Remainder = Buffer.drop_front(FirstSep + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118
Stella Stamenovab3f44ad2018-12-10 17:23:28 +0000119 llvm::SmallString<256> Resolved;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000120 if (!Resolver.ResolveExact(Username, Resolved)) {
121 // We couldn't resolve it as a full username. If there were no slashes
122 // then this might be a partial username. We try to resolve it as such
123 // but after that, we're done regardless of any matches.
124 if (FirstSep == llvm::StringRef::npos) {
125 llvm::StringSet<> MatchSet;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000126 Resolver.ResolvePartial(Username, MatchSet);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000127 for (const auto &S : MatchSet) {
128 Resolved = S.getKey();
129 path::append(Resolved, path::get_separator());
130 matches.AppendString(Resolved);
131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 }
Zachary Turner0734e6a2017-03-12 20:01:37 +0000133 return matches.GetSize();
Zachary Turner2cc5a182017-03-13 00:41:01 +0000134 }
135
Adrian Prantl05097242018-04-30 16:49:04 +0000136 // If there was no trailing slash, then we're done as soon as we resolve
137 // the expression to the correct directory. Otherwise we need to continue
Zachary Turner2cc5a182017-03-13 00:41:01 +0000138 // looking for matches within that directory.
139 if (FirstSep == llvm::StringRef::npos) {
140 // Make sure it ends with a separator.
141 path::append(CompletionBuffer, path::get_separator());
Zachary Turner2cc5a182017-03-13 00:41:01 +0000142 matches.AppendString(CompletionBuffer);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000143 return matches.GetSize();
Zachary Turner2cc5a182017-03-13 00:41:01 +0000144 }
145
146 // We want to keep the form the user typed, so we special case this to
147 // search in the fully resolved directory, but CompletionBuffer keeps the
148 // unmodified form that the user typed.
149 Storage = Resolved;
Pavel Labath88ec2e42018-06-29 10:27:18 +0000150 llvm::StringRef RemainderDir = path::parent_path(Remainder);
Raphael Isemann4621e0b2018-06-18 20:11:38 +0000151 if (!RemainderDir.empty()) {
152 // Append the remaining path to the resolved directory.
153 Storage.append(path::get_separator());
154 Storage.append(RemainderDir);
155 }
Raphael Isemann17843882018-01-22 09:17:16 +0000156 SearchDir = Storage;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000157 } else {
158 SearchDir = path::parent_path(CompletionBuffer);
Zachary Turnerd5bd3a12017-03-12 18:18:50 +0000159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160
Zachary Turner2cc5a182017-03-13 00:41:01 +0000161 size_t FullPrefixLen = CompletionBuffer.size();
Zachary Turner0734e6a2017-03-12 20:01:37 +0000162
Zachary Turner2cc5a182017-03-13 00:41:01 +0000163 PartialItem = path::filename(CompletionBuffer);
Frederic Riss78a10a72018-08-31 23:03:28 +0000164
165 // path::filename() will return "." when the passed path ends with a
166 // directory separator. We have to filter those out, but only when the
167 // "." doesn't come from the completion request itself.
168 if (PartialItem == "." && path::is_separator(CompletionBuffer.back()))
Zachary Turner2cc5a182017-03-13 00:41:01 +0000169 PartialItem = llvm::StringRef();
Zachary Turner0734e6a2017-03-12 20:01:37 +0000170
Zachary Turner426d1372017-04-15 02:44:53 +0000171 if (SearchDir.empty()) {
172 llvm::sys::fs::current_path(Storage);
173 SearchDir = Storage;
174 }
Zachary Turner2cc5a182017-03-13 00:41:01 +0000175 assert(!PartialItem.contains(path::get_separator()));
Zachary Turner0734e6a2017-03-12 20:01:37 +0000176
Zachary Turner2cc5a182017-03-13 00:41:01 +0000177 // SearchDir now contains the directory to search in, and Prefix contains the
178 // text we want to match against items in that directory.
179
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000180 FileSystem &fs = FileSystem::Instance();
Zachary Turner2cc5a182017-03-13 00:41:01 +0000181 std::error_code EC;
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000182 llvm::vfs::directory_iterator Iter = fs.DirBegin(SearchDir, EC);
183 llvm::vfs::directory_iterator End;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000184 for (; Iter != End && !EC; Iter.increment(EC)) {
185 auto &Entry = *Iter;
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000186 llvm::ErrorOr<llvm::vfs::Status> Status = fs.GetStatus(Entry.path());
187
188 if (!Status)
189 continue;
Zachary Turner2cc5a182017-03-13 00:41:01 +0000190
191 auto Name = path::filename(Entry.path());
192
193 // Omit ".", ".."
194 if (Name == "." || Name == ".." || !Name.startswith(PartialItem))
195 continue;
196
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000197 bool is_dir = Status->isDirectory();
Zachary Turner2cc5a182017-03-13 00:41:01 +0000198
199 // If it's a symlink, then we treat it as a directory as long as the target
200 // is a directory.
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000201 if (Status->isSymlink()) {
202 FileSpec symlink_filespec(Entry.path());
203 FileSpec resolved_filespec;
204 auto error = fs.ResolveSymbolicLink(symlink_filespec, resolved_filespec);
205 if (error.Success())
206 is_dir = fs.IsDirectory(symlink_filespec);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000207 }
Jonas Devlieghereedaf2bc2018-12-04 17:58:21 +0000208
Zachary Turner2cc5a182017-03-13 00:41:01 +0000209 if (only_directories && !is_dir)
210 continue;
211
212 // Shrink it back down so that it just has the original prefix the user
213 // typed and remove the part of the name which is common to the located
214 // item and what the user typed.
215 CompletionBuffer.resize(FullPrefixLen);
216 Name = Name.drop_front(PartialItem.size());
217 CompletionBuffer.append(Name);
218
219 if (is_dir) {
Zachary Turner2cc5a182017-03-13 00:41:01 +0000220 path::append(CompletionBuffer, path::get_separator());
221 }
222
223 matches.AppendString(CompletionBuffer);
224 }
Zachary Turner0734e6a2017-03-12 20:01:37 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 return matches.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227}
228
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000229static int DiskFilesOrDirectories(CompletionRequest &request,
230 bool only_directories) {
231 request.SetWordComplete(false);
Jonas Devlieghere72787ac2018-11-09 01:59:28 +0000232 StandardTildeExpressionResolver resolver;
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000233 StringList matches;
234 DiskFilesOrDirectories(request.GetCursorArgumentPrefix(), only_directories,
235 matches, resolver);
236 request.AddCompletions(matches);
237 return request.GetNumberOfMatches();
238}
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240int CommandCompletions::DiskFiles(CommandInterpreter &interpreter,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000241 CompletionRequest &request,
242 SearchFilter *searcher) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000243 return DiskFilesOrDirectories(request, /*only_dirs*/ false);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000244}
245
246int CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name,
247 StringList &matches,
248 TildeExpressionResolver &Resolver) {
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000249 return DiskFilesOrDirectories(partial_file_name, false, matches, Resolver);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250}
251
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000252int CommandCompletions::DiskDirectories(CommandInterpreter &interpreter,
253 CompletionRequest &request,
254 SearchFilter *searcher) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000255 return DiskFilesOrDirectories(request, /*only_dirs*/ true);
Zachary Turner2cc5a182017-03-13 00:41:01 +0000256}
257
258int CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name,
259 StringList &matches,
260 TildeExpressionResolver &Resolver) {
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000261 return DiskFilesOrDirectories(partial_file_name, true, matches, Resolver);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262}
263
264int CommandCompletions::Modules(CommandInterpreter &interpreter,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000265 CompletionRequest &request,
266 SearchFilter *searcher) {
267 request.SetWordComplete(true);
268 ModuleCompleter completer(interpreter, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269
270 if (searcher == nullptr) {
271 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
272 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
273 completer.DoCompletion(&null_searcher);
274 } else {
275 completer.DoCompletion(searcher);
276 }
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000277 return request.GetNumberOfMatches();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278}
279
280int CommandCompletions::Symbols(CommandInterpreter &interpreter,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000281 CompletionRequest &request,
282 SearchFilter *searcher) {
283 request.SetWordComplete(true);
284 SymbolCompleter completer(interpreter, request);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285
286 if (searcher == nullptr) {
287 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
288 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
289 completer.DoCompletion(&null_searcher);
290 } else {
291 completer.DoCompletion(searcher);
292 }
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000293 return request.GetNumberOfMatches();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294}
295
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000296int CommandCompletions::SettingsNames(CommandInterpreter &interpreter,
297 CompletionRequest &request,
298 SearchFilter *searcher) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 // Cache the full setting name list
300 static StringList g_property_names;
301 if (g_property_names.GetSize() == 0) {
302 // Generate the full setting name list on demand
303 lldb::OptionValuePropertiesSP properties_sp(
304 interpreter.GetDebugger().GetValueProperties());
305 if (properties_sp) {
306 StreamString strm;
307 properties_sp->DumpValue(nullptr, strm, OptionValue::eDumpOptionName);
308 const std::string &str = strm.GetString();
309 g_property_names.SplitIntoLines(str.c_str(), str.size());
310 }
311 }
312
313 size_t exact_matches_idx = SIZE_MAX;
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000314 StringList matches;
315 g_property_names.AutoComplete(request.GetCursorArgumentPrefix(), matches,
316 exact_matches_idx);
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000317 request.SetWordComplete(exact_matches_idx != SIZE_MAX);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000318 request.AddCompletions(matches);
319 return request.GetNumberOfMatches();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320}
321
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000322int CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter,
323 CompletionRequest &request,
324 SearchFilter *searcher) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000325 StringList new_matches;
326 std::size_t num_matches = PluginManager::AutoCompletePlatformName(
327 request.GetCursorArgumentPrefix(), new_matches);
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000328 request.SetWordComplete(num_matches == 1);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000329 request.AddCompletions(new_matches);
330 return request.GetNumberOfMatches();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331}
332
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000333int CommandCompletions::ArchitectureNames(CommandInterpreter &interpreter,
334 CompletionRequest &request,
335 SearchFilter *searcher) {
336 const uint32_t num_matches = ArchSpec::AutoComplete(request);
337 request.SetWordComplete(num_matches == 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 return num_matches;
339}
340
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000341int CommandCompletions::VariablePath(CommandInterpreter &interpreter,
342 CompletionRequest &request,
343 SearchFilter *searcher) {
344 return Variable::AutoComplete(interpreter.GetExecutionContext(), request);
Greg Claytonf21fead2013-05-14 23:43:18 +0000345}
346
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000347CommandCompletions::Completer::Completer(CommandInterpreter &interpreter,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000348 CompletionRequest &request)
349 : m_interpreter(interpreter), m_request(request) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000351CommandCompletions::Completer::~Completer() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352
353//----------------------------------------------------------------------
354// SourceFileCompleter
355//----------------------------------------------------------------------
356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357CommandCompletions::SourceFileCompleter::SourceFileCompleter(
358 CommandInterpreter &interpreter, bool include_support_files,
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000359 CompletionRequest &request)
360 : CommandCompletions::Completer(interpreter, request),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 m_include_support_files(include_support_files), m_matching_files() {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000362 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 m_file_name = partial_spec.GetFilename().GetCString();
364 m_dir_name = partial_spec.GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365}
366
Jim Ingham4911d362018-09-07 18:43:04 +0000367lldb::SearchDepth CommandCompletions::SourceFileCompleter::GetDepth() {
368 return lldb::eSearchDepthCompUnit;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369}
370
371Searcher::CallbackReturn
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000372CommandCompletions::SourceFileCompleter::SearchCallback(SearchFilter &filter,
373 SymbolContext &context,
374 Address *addr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 bool complete) {
376 if (context.comp_unit != nullptr) {
377 if (m_include_support_files) {
378 FileSpecList supporting_files = context.comp_unit->GetSupportFiles();
379 for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++) {
380 const FileSpec &sfile_spec =
381 supporting_files.GetFileSpecAtIndex(sfiles);
382 const char *sfile_file_name = sfile_spec.GetFilename().GetCString();
383 const char *sfile_dir_name = sfile_spec.GetFilename().GetCString();
384 bool match = false;
385 if (m_file_name && sfile_file_name &&
386 strstr(sfile_file_name, m_file_name) == sfile_file_name)
387 match = true;
388 if (match && m_dir_name && sfile_dir_name &&
389 strstr(sfile_dir_name, m_dir_name) != sfile_dir_name)
390 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 if (match) {
393 m_matching_files.AppendIfUnique(sfile_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 }
396 } else {
397 const char *cur_file_name = context.comp_unit->GetFilename().GetCString();
398 const char *cur_dir_name = context.comp_unit->GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 bool match = false;
401 if (m_file_name && cur_file_name &&
402 strstr(cur_file_name, m_file_name) == cur_file_name)
403 match = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 if (match && m_dir_name && cur_dir_name &&
406 strstr(cur_dir_name, m_dir_name) != cur_dir_name)
407 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 if (match) {
410 m_matching_files.AppendIfUnique(context.comp_unit);
411 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 }
414 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415}
416
417size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418CommandCompletions::SourceFileCompleter::DoCompletion(SearchFilter *filter) {
419 filter->Search(*this);
420 // Now convert the filelist to completions:
421 for (size_t i = 0; i < m_matching_files.GetSize(); i++) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000422 m_request.AddCompletion(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
424 }
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000425 return m_request.GetNumberOfMatches();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426}
427
428//----------------------------------------------------------------------
429// SymbolCompleter
430//----------------------------------------------------------------------
431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432static bool regex_chars(const char comp) {
433 return (comp == '[' || comp == ']' || comp == '(' || comp == ')' ||
434 comp == '{' || comp == '}' || comp == '+' || comp == '.' ||
435 comp == '*' || comp == '|' || comp == '^' || comp == '$' ||
436 comp == '\\' || comp == '?');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437}
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439CommandCompletions::SymbolCompleter::SymbolCompleter(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000440 CommandInterpreter &interpreter, CompletionRequest &request)
441 : CommandCompletions::Completer(interpreter, request) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 std::string regex_str;
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000443 if (!m_request.GetCursorArgumentPrefix().empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 regex_str.append("^");
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000445 regex_str.append(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 } else {
447 // Match anything since the completion string is empty
448 regex_str.append(".");
449 }
450 std::string::iterator pos =
451 find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
452 while (pos < regex_str.end()) {
453 pos = regex_str.insert(pos, '\\');
454 pos = find_if(pos + 2, regex_str.end(), regex_chars);
455 }
Zachary Turner95eae422016-09-21 16:01:28 +0000456 m_regex.Compile(regex_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457}
458
Jim Ingham4911d362018-09-07 18:43:04 +0000459lldb::SearchDepth CommandCompletions::SymbolCompleter::GetDepth() {
460 return lldb::eSearchDepthModule;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461}
462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463Searcher::CallbackReturn CommandCompletions::SymbolCompleter::SearchCallback(
464 SearchFilter &filter, SymbolContext &context, Address *addr,
465 bool complete) {
466 if (context.module_sp) {
467 SymbolContextList sc_list;
468 const bool include_symbols = true;
469 const bool include_inlines = true;
470 const bool append = true;
471 context.module_sp->FindFunctions(m_regex, include_symbols, include_inlines,
472 append, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 SymbolContext sc;
475 // Now add the functions & symbols to the list - only add if unique:
476 for (uint32_t i = 0; i < sc_list.GetSize(); i++) {
477 if (sc_list.GetContextAtIndex(i, sc)) {
478 ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
479 if (!func_name.IsEmpty())
480 m_match_set.insert(func_name);
481 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 }
484 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485}
486
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487size_t CommandCompletions::SymbolCompleter::DoCompletion(SearchFilter *filter) {
488 filter->Search(*this);
489 collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
490 for (pos = m_match_set.begin(); pos != end; pos++)
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000491 m_request.AddCompletion((*pos).GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000493 return m_request.GetNumberOfMatches();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494}
495
496//----------------------------------------------------------------------
497// ModuleCompleter
498//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499CommandCompletions::ModuleCompleter::ModuleCompleter(
Raphael Isemanna2e76c02018-07-13 18:28:14 +0000500 CommandInterpreter &interpreter, CompletionRequest &request)
501 : CommandCompletions::Completer(interpreter, request) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000502 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 m_file_name = partial_spec.GetFilename().GetCString();
504 m_dir_name = partial_spec.GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505}
506
Jim Ingham4911d362018-09-07 18:43:04 +0000507lldb::SearchDepth CommandCompletions::ModuleCompleter::GetDepth() {
508 return lldb::eSearchDepthModule;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509}
510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511Searcher::CallbackReturn CommandCompletions::ModuleCompleter::SearchCallback(
512 SearchFilter &filter, SymbolContext &context, Address *addr,
513 bool complete) {
514 if (context.module_sp) {
515 const char *cur_file_name =
516 context.module_sp->GetFileSpec().GetFilename().GetCString();
517 const char *cur_dir_name =
518 context.module_sp->GetFileSpec().GetDirectory().GetCString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 bool match = false;
521 if (m_file_name && cur_file_name &&
522 strstr(cur_file_name, m_file_name) == cur_file_name)
523 match = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000524
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 if (match && m_dir_name && cur_dir_name &&
526 strstr(cur_dir_name, m_dir_name) != cur_dir_name)
527 match = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 if (match) {
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000530 m_request.AddCompletion(cur_file_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 }
533 return Searcher::eCallbackReturnContinue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534}
535
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536size_t CommandCompletions::ModuleCompleter::DoCompletion(SearchFilter *filter) {
537 filter->Search(*this);
Raphael Isemann1a6d7ab2018-07-27 18:42:46 +0000538 return m_request.GetNumberOfMatches();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539}