1c9af18a5SGreg Clayton //===-- CommandCompletions.cpp ----------------------------------*- C++ -*-===//
2c9af18a5SGreg Clayton //
3c9af18a5SGreg Clayton //                     The LLVM Compiler Infrastructure
4c9af18a5SGreg Clayton //
5c9af18a5SGreg Clayton // This file is distributed under the University of Illinois Open Source
6c9af18a5SGreg Clayton // License. See LICENSE.TXT for details.
7c9af18a5SGreg Clayton //
8c9af18a5SGreg Clayton //===----------------------------------------------------------------------===//
9c9af18a5SGreg Clayton 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
11c9af18a5SGreg Clayton 
12c9af18a5SGreg Clayton // C Includes
13558ce124SJim Ingham #include <sys/stat.h>
14fd18426eSGreg Clayton #if defined(__APPLE__) || defined(__linux__)
157cc478b9SJim Ingham #include <pwd.h>
16fd18426eSGreg Clayton #endif
17558ce124SJim Ingham 
18c9af18a5SGreg Clayton // C++ Includes
19c9af18a5SGreg Clayton // Other libraries and framework includes
20c9af18a5SGreg Clayton // Project includes
2153239f00SGreg Clayton #include "lldb/Host/FileSpec.h"
229b62fd2fSGreg Clayton #include "lldb/Core/FileSpecList.h"
23ab65b34fSGreg Clayton #include "lldb/Core/PluginManager.h"
241f746071SGreg Clayton #include "lldb/Core/Module.h"
259b62fd2fSGreg Clayton #include "lldb/Interpreter/Args.h"
269b62fd2fSGreg Clayton #include "lldb/Interpreter/CommandCompletions.h"
279b62fd2fSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
281f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
29f21feadcSGreg Clayton #include "lldb/Symbol/Variable.h"
309b62fd2fSGreg Clayton #include "lldb/Target/Target.h"
319b62fd2fSGreg Clayton #include "lldb/Utility/CleanUp.h"
32c9af18a5SGreg Clayton 
333f559740SZachary Turner #include "llvm/ADT/SmallString.h"
343f559740SZachary Turner 
35c9af18a5SGreg Clayton using namespace lldb_private;
36c9af18a5SGreg Clayton 
37c9af18a5SGreg Clayton CommandCompletions::CommonCompletionElement
38c9af18a5SGreg Clayton CommandCompletions::g_common_completions[] =
39c9af18a5SGreg Clayton {
40c9af18a5SGreg Clayton     {eCustomCompletion,          NULL},
41c9af18a5SGreg Clayton     {eSourceFileCompletion,      CommandCompletions::SourceFiles},
42558ce124SJim Ingham     {eDiskFileCompletion,        CommandCompletions::DiskFiles},
43558ce124SJim Ingham     {eDiskDirectoryCompletion,   CommandCompletions::DiskDirectories},
44c9af18a5SGreg Clayton     {eSymbolCompletion,          CommandCompletions::Symbols},
45c9af18a5SGreg Clayton     {eModuleCompletion,          CommandCompletions::Modules},
463df9a8dfSCaroline Tice     {eSettingsNameCompletion,    CommandCompletions::SettingsNames},
47ab65b34fSGreg Clayton     {ePlatformPluginCompletion,  CommandCompletions::PlatformPluginNames},
48ab65b34fSGreg Clayton     {eArchitectureCompletion,    CommandCompletions::ArchitectureNames},
49f21feadcSGreg Clayton     {eVariablePathCompletion,    CommandCompletions::VariablePath},
50c9af18a5SGreg Clayton     {eNoCompletion,              NULL}      // This one has to be last in the list.
51c9af18a5SGreg Clayton };
52c9af18a5SGreg Clayton 
53c9af18a5SGreg Clayton bool
546611103cSGreg Clayton CommandCompletions::InvokeCommonCompletionCallbacks
556611103cSGreg Clayton (
566611103cSGreg Clayton     CommandInterpreter &interpreter,
576611103cSGreg Clayton     uint32_t completion_mask,
58c9af18a5SGreg Clayton     const char *completion_str,
59c9af18a5SGreg Clayton     int match_start_point,
60c9af18a5SGreg Clayton     int max_return_elements,
61c9af18a5SGreg Clayton     SearchFilter *searcher,
62558ce124SJim Ingham     bool &word_complete,
636611103cSGreg Clayton     StringList &matches
646611103cSGreg Clayton )
65c9af18a5SGreg Clayton {
66c9af18a5SGreg Clayton     bool handled = false;
67c9af18a5SGreg Clayton 
68c9af18a5SGreg Clayton     if (completion_mask & eCustomCompletion)
69c9af18a5SGreg Clayton         return false;
70c9af18a5SGreg Clayton 
71c9af18a5SGreg Clayton     for (int i = 0; ; i++)
72c9af18a5SGreg Clayton     {
73c9af18a5SGreg Clayton         if (g_common_completions[i].type == eNoCompletion)
74c9af18a5SGreg Clayton             break;
75c9af18a5SGreg Clayton          else if ((g_common_completions[i].type & completion_mask) == g_common_completions[i].type
76c9af18a5SGreg Clayton                    && g_common_completions[i].callback != NULL)
77c9af18a5SGreg Clayton          {
78c9af18a5SGreg Clayton             handled = true;
796611103cSGreg Clayton             g_common_completions[i].callback (interpreter,
806611103cSGreg Clayton                                               completion_str,
81c9af18a5SGreg Clayton                                               match_start_point,
82c9af18a5SGreg Clayton                                               max_return_elements,
83c9af18a5SGreg Clayton                                               searcher,
84558ce124SJim Ingham                                               word_complete,
85c9af18a5SGreg Clayton                                               matches);
86c9af18a5SGreg Clayton         }
87c9af18a5SGreg Clayton     }
88c9af18a5SGreg Clayton     return handled;
89c9af18a5SGreg Clayton }
90c9af18a5SGreg Clayton 
91c9af18a5SGreg Clayton int
926611103cSGreg Clayton CommandCompletions::SourceFiles
936611103cSGreg Clayton (
946611103cSGreg Clayton     CommandInterpreter &interpreter,
956611103cSGreg Clayton     const char *partial_file_name,
96c9af18a5SGreg Clayton     int match_start_point,
97c9af18a5SGreg Clayton     int max_return_elements,
98c9af18a5SGreg Clayton     SearchFilter *searcher,
99558ce124SJim Ingham     bool &word_complete,
1006611103cSGreg Clayton     StringList &matches
1016611103cSGreg Clayton )
102c9af18a5SGreg Clayton {
103558ce124SJim Ingham     word_complete = true;
104c9af18a5SGreg Clayton     // Find some way to switch "include support files..."
1056611103cSGreg Clayton     SourceFileCompleter completer (interpreter,
1066611103cSGreg Clayton                                    false,
1076611103cSGreg Clayton                                    partial_file_name,
1086611103cSGreg Clayton                                    match_start_point,
1096611103cSGreg Clayton                                    max_return_elements,
110c9af18a5SGreg Clayton                                    matches);
111c9af18a5SGreg Clayton 
112c9af18a5SGreg Clayton     if (searcher == NULL)
113c9af18a5SGreg Clayton     {
1142976d00aSJim Ingham         lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
115*33df7cd3SJim Ingham         SearchFilterForUnconstrainedSearches null_searcher (target_sp);
116c9af18a5SGreg Clayton         completer.DoCompletion (&null_searcher);
117c9af18a5SGreg Clayton     }
118c9af18a5SGreg Clayton     else
119c9af18a5SGreg Clayton     {
120c9af18a5SGreg Clayton         completer.DoCompletion (searcher);
121c9af18a5SGreg Clayton     }
122c9af18a5SGreg Clayton     return matches.GetSize();
123c9af18a5SGreg Clayton }
124c9af18a5SGreg Clayton 
125b2f1fb29SVirgile Bello typedef struct DiskFilesOrDirectoriesBaton
126b2f1fb29SVirgile Bello {
127b2f1fb29SVirgile Bello     const char *remainder;
128b2f1fb29SVirgile Bello     char *partial_name_copy;
129b2f1fb29SVirgile Bello     bool only_directories;
130b2f1fb29SVirgile Bello     bool *saw_directory;
131b2f1fb29SVirgile Bello     StringList *matches;
132b2f1fb29SVirgile Bello     char *end_ptr;
133b2f1fb29SVirgile Bello     size_t baselen;
1340b5e5f43SMichael Sartain } DiskFilesOrDirectoriesBaton;
135b2f1fb29SVirgile Bello 
136b2f1fb29SVirgile Bello FileSpec::EnumerateDirectoryResult DiskFilesOrDirectoriesCallback(void *baton, FileSpec::FileType file_type, const FileSpec &spec)
137b2f1fb29SVirgile Bello {
138b2f1fb29SVirgile Bello     const char *name = spec.GetFilename().AsCString();
139b2f1fb29SVirgile Bello 
140b2f1fb29SVirgile Bello     const DiskFilesOrDirectoriesBaton *parameters = (DiskFilesOrDirectoriesBaton*)baton;
141b2f1fb29SVirgile Bello     char *end_ptr = parameters->end_ptr;
142b2f1fb29SVirgile Bello     char *partial_name_copy = parameters->partial_name_copy;
143b2f1fb29SVirgile Bello     const char *remainder = parameters->remainder;
144b2f1fb29SVirgile Bello 
145b2f1fb29SVirgile Bello     // Omit ".", ".." and any . files if the match string doesn't start with .
146b2f1fb29SVirgile Bello     if (name[0] == '.')
147b2f1fb29SVirgile Bello     {
148b2f1fb29SVirgile Bello         if (name[1] == '\0')
149b2f1fb29SVirgile Bello             return FileSpec::eEnumerateDirectoryResultNext;
150b2f1fb29SVirgile Bello         else if (name[1] == '.' && name[2] == '\0')
151b2f1fb29SVirgile Bello             return FileSpec::eEnumerateDirectoryResultNext;
152b2f1fb29SVirgile Bello         else if (remainder[0] != '.')
153b2f1fb29SVirgile Bello             return FileSpec::eEnumerateDirectoryResultNext;
154b2f1fb29SVirgile Bello     }
155b2f1fb29SVirgile Bello 
156b2f1fb29SVirgile Bello     // If we found a directory, we put a "/" at the end of the name.
157b2f1fb29SVirgile Bello 
158b2f1fb29SVirgile Bello     if (remainder[0] == '\0' || strstr(name, remainder) == name)
159b2f1fb29SVirgile Bello     {
160b2f1fb29SVirgile Bello         if (strlen(name) + parameters->baselen >= PATH_MAX)
161b2f1fb29SVirgile Bello             return FileSpec::eEnumerateDirectoryResultNext;
162b2f1fb29SVirgile Bello 
163b2f1fb29SVirgile Bello         strcpy(end_ptr, name);
164b2f1fb29SVirgile Bello 
165b2f1fb29SVirgile Bello         bool isa_directory = false;
166b2f1fb29SVirgile Bello         if (file_type == FileSpec::eFileTypeDirectory)
167b2f1fb29SVirgile Bello             isa_directory = true;
168b2f1fb29SVirgile Bello         else if (file_type == FileSpec::eFileTypeSymbolicLink)
169b2f1fb29SVirgile Bello         {
170b2f1fb29SVirgile Bello             struct stat stat_buf;
171b2f1fb29SVirgile Bello             if ((stat(partial_name_copy, &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode))
172b2f1fb29SVirgile Bello                 isa_directory = true;
173b2f1fb29SVirgile Bello         }
174b2f1fb29SVirgile Bello 
175b2f1fb29SVirgile Bello         if (isa_directory)
176b2f1fb29SVirgile Bello         {
177b2f1fb29SVirgile Bello             *parameters->saw_directory = true;
178b2f1fb29SVirgile Bello             size_t len = strlen(parameters->partial_name_copy);
179b2f1fb29SVirgile Bello             partial_name_copy[len] = '/';
180b2f1fb29SVirgile Bello             partial_name_copy[len + 1] = '\0';
181b2f1fb29SVirgile Bello         }
182b2f1fb29SVirgile Bello         if (parameters->only_directories && !isa_directory)
183b2f1fb29SVirgile Bello             return FileSpec::eEnumerateDirectoryResultNext;
184b2f1fb29SVirgile Bello         parameters->matches->AppendString(partial_name_copy);
185b2f1fb29SVirgile Bello     }
186b2f1fb29SVirgile Bello 
187b2f1fb29SVirgile Bello     return FileSpec::eEnumerateDirectoryResultNext;
188b2f1fb29SVirgile Bello }
189b2f1fb29SVirgile Bello 
190558ce124SJim Ingham static int
191558ce124SJim Ingham DiskFilesOrDirectories
192558ce124SJim Ingham (
193558ce124SJim Ingham     const char *partial_file_name,
194558ce124SJim Ingham     bool only_directories,
195558ce124SJim Ingham     bool &saw_directory,
196558ce124SJim Ingham     StringList &matches
197558ce124SJim Ingham )
198558ce124SJim Ingham {
199558ce124SJim Ingham     // I'm going to  use the "glob" function with GLOB_TILDE for user directory expansion.
200558ce124SJim Ingham     // If it is not defined on your host system, you'll need to implement it yourself...
201558ce124SJim Ingham 
202c7bece56SGreg Clayton     size_t partial_name_len = strlen(partial_file_name);
203558ce124SJim Ingham 
204558ce124SJim Ingham     if (partial_name_len >= PATH_MAX)
205558ce124SJim Ingham         return matches.GetSize();
206558ce124SJim Ingham 
207558ce124SJim Ingham     // This copy of the string will be cut up into the directory part, and the remainder.  end_ptr
208558ce124SJim Ingham     // below will point to the place of the remainder in this string.  Then when we've resolved the
209558ce124SJim Ingham     // containing directory, and opened it, we'll read the directory contents and overwrite the
210558ce124SJim Ingham     // partial_name_copy starting from end_ptr with each of the matches.  Thus we will preserve
211558ce124SJim Ingham     // the form the user originally typed.
212558ce124SJim Ingham 
213558ce124SJim Ingham     char partial_name_copy[PATH_MAX];
214021b605dSBenjamin Kramer     memcpy(partial_name_copy, partial_file_name, partial_name_len);
215558ce124SJim Ingham     partial_name_copy[partial_name_len] = '\0';
216558ce124SJim Ingham 
217710dd5aeSGreg Clayton     // We'll need to save a copy of the remainder for comparison, which we do here.
218558ce124SJim Ingham     char remainder[PATH_MAX];
219558ce124SJim Ingham 
220558ce124SJim Ingham     // end_ptr will point past the last / in partial_name_copy, or if there is no slash to the beginning of the string.
221558ce124SJim Ingham     char *end_ptr;
222558ce124SJim Ingham 
223558ce124SJim Ingham     end_ptr = strrchr(partial_name_copy, '/');
224558ce124SJim Ingham 
225558ce124SJim Ingham     // This will store the resolved form of the containing directory
2263f559740SZachary Turner     llvm::SmallString<64> containing_part;
227558ce124SJim Ingham 
228558ce124SJim Ingham     if (end_ptr == NULL)
229558ce124SJim Ingham     {
230558ce124SJim Ingham         // There's no directory.  If the thing begins with a "~" then this is a bare
231558ce124SJim Ingham         // user name.
232558ce124SJim Ingham         if (*partial_name_copy == '~')
233558ce124SJim Ingham         {
234558ce124SJim Ingham             // Nothing here but the user name.  We could just put a slash on the end,
2357cc478b9SJim Ingham             // but for completeness sake we'll resolve the user name and only put a slash
236558ce124SJim Ingham             // on the end if it exists.
2373f559740SZachary Turner             llvm::SmallString<64> resolved_username(partial_name_copy);
2383f559740SZachary Turner             FileSpec::ResolveUsername (resolved_username);
2397cc478b9SJim Ingham 
2407cc478b9SJim Ingham            // Not sure how this would happen, a username longer than PATH_MAX?  Still...
2413f559740SZachary Turner             if (resolved_username.size() == 0)
2427cc478b9SJim Ingham             {
2437cc478b9SJim Ingham                 // The user name didn't resolve, let's look in the password database for matches.
2447cc478b9SJim Ingham                 // The user name database contains duplicates, and is not in alphabetical order, so
2457cc478b9SJim Ingham                 // we'll use a set to manage that for us.
24684363078SJim Ingham                 FileSpec::ResolvePartialUsername (partial_name_copy, matches);
24784363078SJim Ingham                 if (matches.GetSize() > 0)
2487cc478b9SJim Ingham                     saw_directory = true;
2497cc478b9SJim Ingham                 return matches.GetSize();
2507cc478b9SJim Ingham             }
25184363078SJim Ingham             else
25284363078SJim Ingham             {
253558ce124SJim Ingham                 //The thing exists, put a '/' on the end, and return it...
254558ce124SJim Ingham                 // FIXME: complete user names here:
255558ce124SJim Ingham                 partial_name_copy[partial_name_len] = '/';
256558ce124SJim Ingham                 partial_name_copy[partial_name_len+1] = '\0';
257558ce124SJim Ingham                 matches.AppendString(partial_name_copy);
258021b605dSBenjamin Kramer                 saw_directory = true;
259558ce124SJim Ingham                 return matches.GetSize();
260558ce124SJim Ingham             }
26184363078SJim Ingham         }
262558ce124SJim Ingham         else
263558ce124SJim Ingham         {
264558ce124SJim Ingham             // The containing part is the CWD, and the whole string is the remainder.
2653f559740SZachary Turner             containing_part = ".";
266558ce124SJim Ingham             strcpy(remainder, partial_name_copy);
267558ce124SJim Ingham             end_ptr = partial_name_copy;
268558ce124SJim Ingham         }
269558ce124SJim Ingham     }
270558ce124SJim Ingham     else
271558ce124SJim Ingham     {
272558ce124SJim Ingham         if (end_ptr == partial_name_copy)
273558ce124SJim Ingham         {
274558ce124SJim Ingham             // We're completing a file or directory in the root volume.
2753f559740SZachary Turner             containing_part = "/";
276558ce124SJim Ingham         }
277558ce124SJim Ingham         else
278558ce124SJim Ingham         {
2793f559740SZachary Turner             containing_part.append(partial_name_copy, end_ptr);
280558ce124SJim Ingham         }
281558ce124SJim Ingham         // Push end_ptr past the final "/" and set remainder.
282558ce124SJim Ingham         end_ptr++;
283558ce124SJim Ingham         strcpy(remainder, end_ptr);
284558ce124SJim Ingham     }
285558ce124SJim Ingham 
2867cc478b9SJim Ingham     // Look for a user name in the containing part, and if it's there, resolve it and stick the
287558ce124SJim Ingham     // result back into the containing_part:
288558ce124SJim Ingham 
289558ce124SJim Ingham     if (*partial_name_copy == '~')
290558ce124SJim Ingham     {
2913f559740SZachary Turner         FileSpec::ResolveUsername(containing_part);
292558ce124SJim Ingham         // User name doesn't exist, we're not getting any further...
2933f559740SZachary Turner         if (containing_part.empty())
294558ce124SJim Ingham             return matches.GetSize();
295558ce124SJim Ingham     }
296558ce124SJim Ingham 
297558ce124SJim Ingham     // Okay, containing_part is now the directory we want to open and look for files:
298558ce124SJim Ingham 
299558ce124SJim Ingham     size_t baselen = end_ptr - partial_name_copy;
300558ce124SJim Ingham 
301b2f1fb29SVirgile Bello     DiskFilesOrDirectoriesBaton parameters;
302b2f1fb29SVirgile Bello     parameters.remainder = remainder;
303b2f1fb29SVirgile Bello     parameters.partial_name_copy = partial_name_copy;
304b2f1fb29SVirgile Bello     parameters.only_directories = only_directories;
305b2f1fb29SVirgile Bello     parameters.saw_directory = &saw_directory;
306b2f1fb29SVirgile Bello     parameters.matches = &matches;
307b2f1fb29SVirgile Bello     parameters.end_ptr = end_ptr;
308b2f1fb29SVirgile Bello     parameters.baselen = baselen;
309558ce124SJim Ingham 
3103f559740SZachary Turner     FileSpec::EnumerateDirectory(containing_part.c_str(), true, true, true, DiskFilesOrDirectoriesCallback, &parameters);
311558ce124SJim Ingham 
312558ce124SJim Ingham     return matches.GetSize();
313558ce124SJim Ingham }
314558ce124SJim Ingham 
315558ce124SJim Ingham int
316558ce124SJim Ingham CommandCompletions::DiskFiles
317558ce124SJim Ingham (
318558ce124SJim Ingham     CommandInterpreter &interpreter,
319558ce124SJim Ingham     const char *partial_file_name,
320558ce124SJim Ingham     int match_start_point,
321558ce124SJim Ingham     int max_return_elements,
322558ce124SJim Ingham     SearchFilter *searcher,
323558ce124SJim Ingham     bool &word_complete,
324558ce124SJim Ingham     StringList &matches
325558ce124SJim Ingham )
326558ce124SJim Ingham {
327558ce124SJim Ingham 
328558ce124SJim Ingham     int ret_val = DiskFilesOrDirectories (partial_file_name,
329558ce124SJim Ingham                                           false,
330558ce124SJim Ingham                                           word_complete,
331558ce124SJim Ingham                                           matches);
332558ce124SJim Ingham     word_complete = !word_complete;
333558ce124SJim Ingham     return ret_val;
334558ce124SJim Ingham }
335558ce124SJim Ingham 
336558ce124SJim Ingham int
337558ce124SJim Ingham CommandCompletions::DiskDirectories
338558ce124SJim Ingham (
339558ce124SJim Ingham     CommandInterpreter &interpreter,
340558ce124SJim Ingham     const char *partial_file_name,
341558ce124SJim Ingham     int match_start_point,
342558ce124SJim Ingham     int max_return_elements,
343558ce124SJim Ingham     SearchFilter *searcher,
344558ce124SJim Ingham     bool &word_complete,
345558ce124SJim Ingham     StringList &matches
346558ce124SJim Ingham )
347558ce124SJim Ingham {
348558ce124SJim Ingham     int ret_val =  DiskFilesOrDirectories (partial_file_name,
349558ce124SJim Ingham                                            true,
350558ce124SJim Ingham                                            word_complete,
351558ce124SJim Ingham                                            matches);
352558ce124SJim Ingham     word_complete = false;
353558ce124SJim Ingham     return ret_val;
354558ce124SJim Ingham }
355558ce124SJim Ingham 
356c9af18a5SGreg Clayton int
3576611103cSGreg Clayton CommandCompletions::Modules
3586611103cSGreg Clayton (
3596611103cSGreg Clayton     CommandInterpreter &interpreter,
3606611103cSGreg Clayton     const char *partial_file_name,
361c9af18a5SGreg Clayton     int match_start_point,
362c9af18a5SGreg Clayton     int max_return_elements,
363c9af18a5SGreg Clayton     SearchFilter *searcher,
364558ce124SJim Ingham     bool &word_complete,
3656611103cSGreg Clayton     StringList &matches
3666611103cSGreg Clayton )
367c9af18a5SGreg Clayton {
368558ce124SJim Ingham     word_complete = true;
3696611103cSGreg Clayton     ModuleCompleter completer (interpreter,
3706611103cSGreg Clayton                                partial_file_name,
3716611103cSGreg Clayton                                match_start_point,
3726611103cSGreg Clayton                                max_return_elements,
3736611103cSGreg Clayton                                matches);
374c9af18a5SGreg Clayton 
375c9af18a5SGreg Clayton     if (searcher == NULL)
376c9af18a5SGreg Clayton     {
3772976d00aSJim Ingham         lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
378*33df7cd3SJim Ingham         SearchFilterForUnconstrainedSearches null_searcher (target_sp);
379c9af18a5SGreg Clayton         completer.DoCompletion (&null_searcher);
380c9af18a5SGreg Clayton     }
381c9af18a5SGreg Clayton     else
382c9af18a5SGreg Clayton     {
383c9af18a5SGreg Clayton         completer.DoCompletion (searcher);
384c9af18a5SGreg Clayton     }
385c9af18a5SGreg Clayton     return matches.GetSize();
386c9af18a5SGreg Clayton }
387c9af18a5SGreg Clayton 
388c9af18a5SGreg Clayton int
3896611103cSGreg Clayton CommandCompletions::Symbols
3906611103cSGreg Clayton (
3916611103cSGreg Clayton     CommandInterpreter &interpreter,
3926611103cSGreg Clayton     const char *partial_file_name,
393c9af18a5SGreg Clayton     int match_start_point,
394c9af18a5SGreg Clayton     int max_return_elements,
395c9af18a5SGreg Clayton     SearchFilter *searcher,
396558ce124SJim Ingham     bool &word_complete,
3976611103cSGreg Clayton     StringList &matches)
398c9af18a5SGreg Clayton {
399558ce124SJim Ingham     word_complete = true;
4006611103cSGreg Clayton     SymbolCompleter completer (interpreter,
4016611103cSGreg Clayton                                partial_file_name,
4026611103cSGreg Clayton                                match_start_point,
4036611103cSGreg Clayton                                max_return_elements,
4046611103cSGreg Clayton                                matches);
405c9af18a5SGreg Clayton 
406c9af18a5SGreg Clayton     if (searcher == NULL)
407c9af18a5SGreg Clayton     {
4082976d00aSJim Ingham         lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
409*33df7cd3SJim Ingham         SearchFilterForUnconstrainedSearches null_searcher (target_sp);
410c9af18a5SGreg Clayton         completer.DoCompletion (&null_searcher);
411c9af18a5SGreg Clayton     }
412c9af18a5SGreg Clayton     else
413c9af18a5SGreg Clayton     {
414c9af18a5SGreg Clayton         completer.DoCompletion (searcher);
415c9af18a5SGreg Clayton     }
416c9af18a5SGreg Clayton     return matches.GetSize();
417c9af18a5SGreg Clayton }
418c9af18a5SGreg Clayton 
4193df9a8dfSCaroline Tice int
4203df9a8dfSCaroline Tice CommandCompletions::SettingsNames (CommandInterpreter &interpreter,
4213df9a8dfSCaroline Tice                                    const char *partial_setting_name,
4223df9a8dfSCaroline Tice                                    int match_start_point,
4233df9a8dfSCaroline Tice                                    int max_return_elements,
4243df9a8dfSCaroline Tice                                    SearchFilter *searcher,
4253df9a8dfSCaroline Tice                                    bool &word_complete,
4263df9a8dfSCaroline Tice                                    StringList &matches)
4273df9a8dfSCaroline Tice {
42867cc0636SGreg Clayton     // Cache the full setting name list
42967cc0636SGreg Clayton     static StringList g_property_names;
43067cc0636SGreg Clayton     if (g_property_names.GetSize() == 0)
43167cc0636SGreg Clayton     {
43267cc0636SGreg Clayton         // Generate the full setting name list on demand
43367cc0636SGreg Clayton         lldb::OptionValuePropertiesSP properties_sp (interpreter.GetDebugger().GetValueProperties());
43467cc0636SGreg Clayton         if (properties_sp)
43567cc0636SGreg Clayton         {
43667cc0636SGreg Clayton             StreamString strm;
43767cc0636SGreg Clayton             properties_sp->DumpValue(NULL, strm, OptionValue::eDumpOptionName);
43867cc0636SGreg Clayton             const std::string &str = strm.GetString();
43967cc0636SGreg Clayton             g_property_names.SplitIntoLines(str.c_str(), str.size());
44067cc0636SGreg Clayton         }
44167cc0636SGreg Clayton     }
4423df9a8dfSCaroline Tice 
44367cc0636SGreg Clayton     size_t exact_matches_idx = SIZE_MAX;
44467cc0636SGreg Clayton     const size_t num_matches = g_property_names.AutoComplete (partial_setting_name, matches, exact_matches_idx);
44567cc0636SGreg Clayton     word_complete = exact_matches_idx != SIZE_MAX;
44667cc0636SGreg Clayton     return num_matches;
4473df9a8dfSCaroline Tice }
4483df9a8dfSCaroline Tice 
449ab65b34fSGreg Clayton 
450ab65b34fSGreg Clayton int
451ab65b34fSGreg Clayton CommandCompletions::PlatformPluginNames (CommandInterpreter &interpreter,
452ab65b34fSGreg Clayton                                          const char *partial_name,
453ab65b34fSGreg Clayton                                          int match_start_point,
454ab65b34fSGreg Clayton                                          int max_return_elements,
455ab65b34fSGreg Clayton                                          SearchFilter *searcher,
456ab65b34fSGreg Clayton                                          bool &word_complete,
457ab65b34fSGreg Clayton                                          lldb_private::StringList &matches)
458ab65b34fSGreg Clayton {
459ab65b34fSGreg Clayton     const uint32_t num_matches = PluginManager::AutoCompletePlatformName(partial_name, matches);
460ab65b34fSGreg Clayton     word_complete = num_matches == 1;
461ab65b34fSGreg Clayton     return num_matches;
462ab65b34fSGreg Clayton }
463ab65b34fSGreg Clayton 
464ab65b34fSGreg Clayton int
465ab65b34fSGreg Clayton CommandCompletions::ArchitectureNames (CommandInterpreter &interpreter,
466ab65b34fSGreg Clayton                                        const char *partial_name,
467ab65b34fSGreg Clayton                                        int match_start_point,
468ab65b34fSGreg Clayton                                        int max_return_elements,
469ab65b34fSGreg Clayton                                        SearchFilter *searcher,
470ab65b34fSGreg Clayton                                        bool &word_complete,
471ab65b34fSGreg Clayton                                        lldb_private::StringList &matches)
472ab65b34fSGreg Clayton {
473ab65b34fSGreg Clayton     const uint32_t num_matches = ArchSpec::AutoComplete (partial_name, matches);
474ab65b34fSGreg Clayton     word_complete = num_matches == 1;
475ab65b34fSGreg Clayton     return num_matches;
476ab65b34fSGreg Clayton }
477ab65b34fSGreg Clayton 
478ab65b34fSGreg Clayton 
479f21feadcSGreg Clayton int
480f21feadcSGreg Clayton CommandCompletions::VariablePath (CommandInterpreter &interpreter,
481f21feadcSGreg Clayton                                   const char *partial_name,
482f21feadcSGreg Clayton                                   int match_start_point,
483f21feadcSGreg Clayton                                   int max_return_elements,
484f21feadcSGreg Clayton                                   SearchFilter *searcher,
485f21feadcSGreg Clayton                                   bool &word_complete,
486f21feadcSGreg Clayton                                   lldb_private::StringList &matches)
487f21feadcSGreg Clayton {
488f21feadcSGreg Clayton     return Variable::AutoComplete (interpreter.GetExecutionContext(), partial_name, matches, word_complete);
489f21feadcSGreg Clayton }
490f21feadcSGreg Clayton 
491f21feadcSGreg Clayton 
4926611103cSGreg Clayton CommandCompletions::Completer::Completer
4936611103cSGreg Clayton (
4946611103cSGreg Clayton     CommandInterpreter &interpreter,
495c9af18a5SGreg Clayton     const char *completion_str,
496c9af18a5SGreg Clayton     int match_start_point,
497c9af18a5SGreg Clayton     int max_return_elements,
498c9af18a5SGreg Clayton     StringList &matches
499c9af18a5SGreg Clayton ) :
5006611103cSGreg Clayton     m_interpreter (interpreter),
501c9af18a5SGreg Clayton     m_completion_str (completion_str),
502c9af18a5SGreg Clayton     m_match_start_point (match_start_point),
503c9af18a5SGreg Clayton     m_max_return_elements (max_return_elements),
504c9af18a5SGreg Clayton     m_matches (matches)
505c9af18a5SGreg Clayton {
506c9af18a5SGreg Clayton }
507c9af18a5SGreg Clayton 
508c9af18a5SGreg Clayton CommandCompletions::Completer::~Completer ()
509c9af18a5SGreg Clayton {
510c9af18a5SGreg Clayton 
511c9af18a5SGreg Clayton }
512c9af18a5SGreg Clayton 
513c9af18a5SGreg Clayton //----------------------------------------------------------------------
514c9af18a5SGreg Clayton // SourceFileCompleter
515c9af18a5SGreg Clayton //----------------------------------------------------------------------
516c9af18a5SGreg Clayton 
5176611103cSGreg Clayton CommandCompletions::SourceFileCompleter::SourceFileCompleter
5186611103cSGreg Clayton (
5196611103cSGreg Clayton     CommandInterpreter &interpreter,
520c9af18a5SGreg Clayton     bool include_support_files,
521c9af18a5SGreg Clayton     const char *completion_str,
522c9af18a5SGreg Clayton     int match_start_point,
523c9af18a5SGreg Clayton     int max_return_elements,
524c9af18a5SGreg Clayton     StringList &matches
525c9af18a5SGreg Clayton ) :
5266611103cSGreg Clayton     CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches),
527c9af18a5SGreg Clayton     m_include_support_files (include_support_files),
528c9af18a5SGreg Clayton     m_matching_files()
529c9af18a5SGreg Clayton {
530274060b6SGreg Clayton     FileSpec partial_spec (m_completion_str.c_str(), false);
531c9af18a5SGreg Clayton     m_file_name = partial_spec.GetFilename().GetCString();
532c9af18a5SGreg Clayton     m_dir_name = partial_spec.GetDirectory().GetCString();
533c9af18a5SGreg Clayton }
534c9af18a5SGreg Clayton 
535c9af18a5SGreg Clayton Searcher::Depth
536c9af18a5SGreg Clayton CommandCompletions::SourceFileCompleter::GetDepth()
537c9af18a5SGreg Clayton {
538c9af18a5SGreg Clayton     return eDepthCompUnit;
539c9af18a5SGreg Clayton }
540c9af18a5SGreg Clayton 
541c9af18a5SGreg Clayton Searcher::CallbackReturn
542c9af18a5SGreg Clayton CommandCompletions::SourceFileCompleter::SearchCallback (
543c9af18a5SGreg Clayton     SearchFilter &filter,
544c9af18a5SGreg Clayton     SymbolContext &context,
545c9af18a5SGreg Clayton     Address *addr,
546c9af18a5SGreg Clayton     bool complete
547c9af18a5SGreg Clayton )
548c9af18a5SGreg Clayton {
549c9af18a5SGreg Clayton     if (context.comp_unit != NULL)
550c9af18a5SGreg Clayton     {
551c9af18a5SGreg Clayton         if (m_include_support_files)
552c9af18a5SGreg Clayton         {
553c9af18a5SGreg Clayton             FileSpecList supporting_files = context.comp_unit->GetSupportFiles();
554c9af18a5SGreg Clayton             for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++)
555c9af18a5SGreg Clayton             {
556c9af18a5SGreg Clayton                 const FileSpec &sfile_spec = supporting_files.GetFileSpecAtIndex(sfiles);
557c9af18a5SGreg Clayton                 const char *sfile_file_name = sfile_spec.GetFilename().GetCString();
558c9af18a5SGreg Clayton                 const char *sfile_dir_name = sfile_spec.GetFilename().GetCString();
559c9af18a5SGreg Clayton                 bool match = false;
560c9af18a5SGreg Clayton                 if (m_file_name && sfile_file_name
561c9af18a5SGreg Clayton                     && strstr (sfile_file_name, m_file_name) == sfile_file_name)
562c9af18a5SGreg Clayton                     match = true;
563c9af18a5SGreg Clayton                 if (match && m_dir_name && sfile_dir_name
564c9af18a5SGreg Clayton                     && strstr (sfile_dir_name, m_dir_name) != sfile_dir_name)
565c9af18a5SGreg Clayton                     match = false;
566c9af18a5SGreg Clayton 
567c9af18a5SGreg Clayton                 if (match)
568c9af18a5SGreg Clayton                 {
569c9af18a5SGreg Clayton                     m_matching_files.AppendIfUnique(sfile_spec);
570c9af18a5SGreg Clayton                 }
571c9af18a5SGreg Clayton             }
572c9af18a5SGreg Clayton 
573c9af18a5SGreg Clayton         }
574c9af18a5SGreg Clayton         else
575c9af18a5SGreg Clayton         {
576c9af18a5SGreg Clayton             const char *cur_file_name = context.comp_unit->GetFilename().GetCString();
577c9af18a5SGreg Clayton             const char *cur_dir_name = context.comp_unit->GetDirectory().GetCString();
578c9af18a5SGreg Clayton 
579c9af18a5SGreg Clayton             bool match = false;
580c9af18a5SGreg Clayton             if (m_file_name && cur_file_name
581c9af18a5SGreg Clayton                 && strstr (cur_file_name, m_file_name) == cur_file_name)
582c9af18a5SGreg Clayton                 match = true;
583c9af18a5SGreg Clayton 
584c9af18a5SGreg Clayton             if (match && m_dir_name && cur_dir_name
585c9af18a5SGreg Clayton                 && strstr (cur_dir_name, m_dir_name) != cur_dir_name)
586c9af18a5SGreg Clayton                 match = false;
587c9af18a5SGreg Clayton 
588c9af18a5SGreg Clayton             if (match)
589c9af18a5SGreg Clayton             {
590c9af18a5SGreg Clayton                 m_matching_files.AppendIfUnique(context.comp_unit);
591c9af18a5SGreg Clayton             }
592c9af18a5SGreg Clayton         }
593c9af18a5SGreg Clayton     }
594c9af18a5SGreg Clayton     return Searcher::eCallbackReturnContinue;
595c9af18a5SGreg Clayton }
596c9af18a5SGreg Clayton 
597c9af18a5SGreg Clayton size_t
598c9af18a5SGreg Clayton CommandCompletions::SourceFileCompleter::DoCompletion (SearchFilter *filter)
599c9af18a5SGreg Clayton {
600c9af18a5SGreg Clayton     filter->Search (*this);
601c9af18a5SGreg Clayton     // Now convert the filelist to completions:
602c9af18a5SGreg Clayton     for (size_t i = 0; i < m_matching_files.GetSize(); i++)
603c9af18a5SGreg Clayton     {
604c9af18a5SGreg Clayton         m_matches.AppendString (m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
605c9af18a5SGreg Clayton     }
606c9af18a5SGreg Clayton     return m_matches.GetSize();
607c9af18a5SGreg Clayton 
608c9af18a5SGreg Clayton }
609c9af18a5SGreg Clayton 
610c9af18a5SGreg Clayton //----------------------------------------------------------------------
611c9af18a5SGreg Clayton // SymbolCompleter
612c9af18a5SGreg Clayton //----------------------------------------------------------------------
613c9af18a5SGreg Clayton 
614c9af18a5SGreg Clayton static bool
615c9af18a5SGreg Clayton regex_chars (const char comp)
616c9af18a5SGreg Clayton {
617649f8b9bSGreg Clayton     if (comp == '[' || comp == ']' ||
618649f8b9bSGreg Clayton         comp == '(' || comp == ')' ||
619649f8b9bSGreg Clayton         comp == '{' || comp == '}' ||
620649f8b9bSGreg Clayton         comp == '+' ||
621649f8b9bSGreg Clayton         comp == '.' ||
622649f8b9bSGreg Clayton         comp == '*' ||
623649f8b9bSGreg Clayton         comp == '|' ||
624649f8b9bSGreg Clayton         comp == '^' ||
625649f8b9bSGreg Clayton         comp == '$' ||
626649f8b9bSGreg Clayton         comp == '\\' ||
627649f8b9bSGreg Clayton         comp == '?')
628c9af18a5SGreg Clayton         return true;
629c9af18a5SGreg Clayton     else
630c9af18a5SGreg Clayton         return false;
631c9af18a5SGreg Clayton }
6326611103cSGreg Clayton CommandCompletions::SymbolCompleter::SymbolCompleter
6336611103cSGreg Clayton (
6346611103cSGreg Clayton     CommandInterpreter &interpreter,
635c9af18a5SGreg Clayton     const char *completion_str,
636c9af18a5SGreg Clayton     int match_start_point,
637c9af18a5SGreg Clayton     int max_return_elements,
638c9af18a5SGreg Clayton     StringList &matches
639c9af18a5SGreg Clayton ) :
6406611103cSGreg Clayton     CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches)
641c9af18a5SGreg Clayton {
642649f8b9bSGreg Clayton     std::string regex_str;
643649f8b9bSGreg Clayton     if (completion_str && completion_str[0])
644649f8b9bSGreg Clayton     {
645649f8b9bSGreg Clayton         regex_str.append("^");
646c9af18a5SGreg Clayton         regex_str.append(completion_str);
647649f8b9bSGreg Clayton     }
648649f8b9bSGreg Clayton     else
649649f8b9bSGreg Clayton     {
650649f8b9bSGreg Clayton         // Match anything since the completion string is empty
651649f8b9bSGreg Clayton         regex_str.append(".");
652649f8b9bSGreg Clayton     }
653649f8b9bSGreg Clayton     std::string::iterator pos = find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
654649f8b9bSGreg Clayton     while (pos < regex_str.end())
655649f8b9bSGreg Clayton     {
656c9af18a5SGreg Clayton         pos = regex_str.insert(pos, '\\');
657649f8b9bSGreg Clayton         pos = find_if(pos + 2, regex_str.end(), regex_chars);
658c9af18a5SGreg Clayton     }
659c9af18a5SGreg Clayton     m_regex.Compile(regex_str.c_str());
660c9af18a5SGreg Clayton }
661c9af18a5SGreg Clayton 
662c9af18a5SGreg Clayton Searcher::Depth
663c9af18a5SGreg Clayton CommandCompletions::SymbolCompleter::GetDepth()
664c9af18a5SGreg Clayton {
665c9af18a5SGreg Clayton     return eDepthModule;
666c9af18a5SGreg Clayton }
667c9af18a5SGreg Clayton 
668c9af18a5SGreg Clayton Searcher::CallbackReturn
669c9af18a5SGreg Clayton CommandCompletions::SymbolCompleter::SearchCallback (
670c9af18a5SGreg Clayton     SearchFilter &filter,
671c9af18a5SGreg Clayton     SymbolContext &context,
672c9af18a5SGreg Clayton     Address *addr,
673c9af18a5SGreg Clayton     bool complete
674c9af18a5SGreg Clayton )
675c9af18a5SGreg Clayton {
676c9af18a5SGreg Clayton     if (context.module_sp)
677c9af18a5SGreg Clayton     {
678931180e6SGreg Clayton         SymbolContextList sc_list;
679931180e6SGreg Clayton         const bool include_symbols = true;
6809df05fbbSSean Callanan         const bool include_inlines = true;
681931180e6SGreg Clayton         const bool append = true;
6829df05fbbSSean Callanan         context.module_sp->FindFunctions (m_regex, include_symbols, include_inlines, append, sc_list);
683c9af18a5SGreg Clayton 
684c9af18a5SGreg Clayton         SymbolContext sc;
685c9af18a5SGreg Clayton         // Now add the functions & symbols to the list - only add if unique:
686931180e6SGreg Clayton         for (uint32_t i = 0; i < sc_list.GetSize(); i++)
687c9af18a5SGreg Clayton         {
688931180e6SGreg Clayton             if (sc_list.GetContextAtIndex(i, sc))
689c9af18a5SGreg Clayton             {
690530a413cSJim Ingham                 ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
691530a413cSJim Ingham                 if (!func_name.IsEmpty())
692530a413cSJim Ingham                     m_match_set.insert (func_name);
693c9af18a5SGreg Clayton             }
694c9af18a5SGreg Clayton         }
695c9af18a5SGreg Clayton     }
696c9af18a5SGreg Clayton     return Searcher::eCallbackReturnContinue;
697c9af18a5SGreg Clayton }
698c9af18a5SGreg Clayton 
699c9af18a5SGreg Clayton size_t
700c9af18a5SGreg Clayton CommandCompletions::SymbolCompleter::DoCompletion (SearchFilter *filter)
701c9af18a5SGreg Clayton {
702c9af18a5SGreg Clayton     filter->Search (*this);
703c9af18a5SGreg Clayton     collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
704c9af18a5SGreg Clayton     for (pos = m_match_set.begin(); pos != end; pos++)
705c9af18a5SGreg Clayton         m_matches.AppendString((*pos).GetCString());
706c9af18a5SGreg Clayton 
707c9af18a5SGreg Clayton     return m_matches.GetSize();
708c9af18a5SGreg Clayton }
709c9af18a5SGreg Clayton 
710c9af18a5SGreg Clayton //----------------------------------------------------------------------
711c9af18a5SGreg Clayton // ModuleCompleter
712c9af18a5SGreg Clayton //----------------------------------------------------------------------
7136611103cSGreg Clayton CommandCompletions::ModuleCompleter::ModuleCompleter
7146611103cSGreg Clayton (
7156611103cSGreg Clayton     CommandInterpreter &interpreter,
716c9af18a5SGreg Clayton     const char *completion_str,
717c9af18a5SGreg Clayton     int match_start_point,
718c9af18a5SGreg Clayton     int max_return_elements,
719c9af18a5SGreg Clayton     StringList &matches
720c9af18a5SGreg Clayton ) :
7216611103cSGreg Clayton     CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches)
722c9af18a5SGreg Clayton {
723274060b6SGreg Clayton     FileSpec partial_spec (m_completion_str.c_str(), false);
724c9af18a5SGreg Clayton     m_file_name = partial_spec.GetFilename().GetCString();
725c9af18a5SGreg Clayton     m_dir_name = partial_spec.GetDirectory().GetCString();
726c9af18a5SGreg Clayton }
727c9af18a5SGreg Clayton 
728c9af18a5SGreg Clayton Searcher::Depth
729c9af18a5SGreg Clayton CommandCompletions::ModuleCompleter::GetDepth()
730c9af18a5SGreg Clayton {
731c9af18a5SGreg Clayton     return eDepthModule;
732c9af18a5SGreg Clayton }
733c9af18a5SGreg Clayton 
734c9af18a5SGreg Clayton Searcher::CallbackReturn
735c9af18a5SGreg Clayton CommandCompletions::ModuleCompleter::SearchCallback (
736c9af18a5SGreg Clayton     SearchFilter &filter,
737c9af18a5SGreg Clayton     SymbolContext &context,
738c9af18a5SGreg Clayton     Address *addr,
739c9af18a5SGreg Clayton     bool complete
740c9af18a5SGreg Clayton )
741c9af18a5SGreg Clayton {
742747bcb03SGreg Clayton     if (context.module_sp)
743c9af18a5SGreg Clayton     {
744c9af18a5SGreg Clayton         const char *cur_file_name = context.module_sp->GetFileSpec().GetFilename().GetCString();
745c9af18a5SGreg Clayton         const char *cur_dir_name = context.module_sp->GetFileSpec().GetDirectory().GetCString();
746c9af18a5SGreg Clayton 
747c9af18a5SGreg Clayton         bool match = false;
748c9af18a5SGreg Clayton         if (m_file_name && cur_file_name
749c9af18a5SGreg Clayton             && strstr (cur_file_name, m_file_name) == cur_file_name)
750c9af18a5SGreg Clayton             match = true;
751c9af18a5SGreg Clayton 
752c9af18a5SGreg Clayton         if (match && m_dir_name && cur_dir_name
753c9af18a5SGreg Clayton             && strstr (cur_dir_name, m_dir_name) != cur_dir_name)
754c9af18a5SGreg Clayton             match = false;
755c9af18a5SGreg Clayton 
756c9af18a5SGreg Clayton         if (match)
757c9af18a5SGreg Clayton         {
758c9af18a5SGreg Clayton             m_matches.AppendString (cur_file_name);
759c9af18a5SGreg Clayton         }
760c9af18a5SGreg Clayton     }
761c9af18a5SGreg Clayton     return Searcher::eCallbackReturnContinue;
762c9af18a5SGreg Clayton }
763c9af18a5SGreg Clayton 
764c9af18a5SGreg Clayton size_t
765c9af18a5SGreg Clayton CommandCompletions::ModuleCompleter::DoCompletion (SearchFilter *filter)
766c9af18a5SGreg Clayton {
767c9af18a5SGreg Clayton     filter->Search (*this);
768c9af18a5SGreg Clayton     return m_matches.GetSize();
769c9af18a5SGreg Clayton }
770