1 //===-- CommandCompletions.h ------------------------------------*- 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 
10 #ifndef lldb_CommandCompletions_h_
11 #define lldb_CommandCompletions_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <set>
16 
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/lldb-private.h"
20 #include "lldb/Core/SearchFilter.h"
21 #include "lldb/Core/FileSpecList.h"
22 #include "lldb/Core/RegularExpression.h"
23 
24 namespace lldb_private
25 {
26 class CommandCompletions
27 {
28 public:
29 
30     //----------------------------------------------------------------------
31     // This is the command completion callback that is used to complete the argument of the option
32     // it is bound to (in the OptionDefinition table below).  Return the total number of matches.
33     //----------------------------------------------------------------------
34     typedef int (*CompletionCallback) (CommandInterpreter &interpreter,
35                                        const char *completion_str,          // This is the argument we are completing
36                                        int match_start_point,               // This is the point in the list of matches that you should start returning elements
37                                        int max_return_elements,             // This is the number of matches requested.
38                                        lldb_private::SearchFilter *searcher,// A search filter to limit the search...
39                                        bool &word_complete,
40                                        lldb_private::StringList &matches);  // The array of matches we return.
41     typedef enum
42     {
43         eNoCompletion             = 0u,
44         eSourceFileCompletion     = (1u << 0),
45         eDiskFileCompletion       = (1u << 1),
46         eDiskDirectoryCompletion  = (1u << 2),
47         eSymbolCompletion         = (1u << 3),
48         eModuleCompletion         = (1u << 4),
49         eSettingsNameCompletion   = (1u << 5),
50         ePlatformPluginCompletion = (1u << 6),
51         eArchitectureCompletion   = (1u << 7),
52         eVariablePathCompletion   = (1u << 8),
53         // This item serves two purposes.  It is the last element in the enum,
54         // so you can add custom enums starting from here in your Option class.
55         // Also if you & in this bit the base code will not process the option.
56         eCustomCompletion         = (1u << 9)
57 
58     } CommonCompletionTypes;
59 
60     struct CommonCompletionElement
61     {
62         uint32_t type;
63         CompletionCallback callback;
64     };
65 
66     static bool InvokeCommonCompletionCallbacks (CommandInterpreter &interpreter,
67                                                  uint32_t completion_mask,
68                                                  const char *completion_str,
69                                                  int match_start_point,
70                                                  int max_return_elements,
71                                                  SearchFilter *searcher,
72                                                  bool &word_complete,
73                                                  StringList &matches);
74 
75     //----------------------------------------------------------------------
76     // These are the generic completer functions:
77     //----------------------------------------------------------------------
78     static int
79     DiskFiles (CommandInterpreter &interpreter,
80                const char *partial_file_name,
81                int match_start_point,
82                int max_return_elements,
83                SearchFilter *searcher,
84                bool &word_complete,
85                StringList &matches);
86     static int
87     DiskDirectories (CommandInterpreter &interpreter,
88                      const char *partial_file_name,
89                      int match_start_point,
90                      int max_return_elements,
91                      SearchFilter *searcher,
92                      bool &word_complete,
93                      StringList &matches);
94 
95     static int
96     SourceFiles (CommandInterpreter &interpreter,
97                  const char *partial_file_name,
98                  int match_start_point,
99                  int max_return_elements,
100                  SearchFilter *searcher,
101                  bool &word_complete,
102                  StringList &matches);
103 
104     static int
105     Modules (CommandInterpreter &interpreter,
106              const char *partial_file_name,
107              int match_start_point,
108              int max_return_elements,
109              SearchFilter *searcher,
110              bool &word_complete,
111              lldb_private::StringList &matches);
112 
113     static int
114     Symbols (CommandInterpreter &interpreter,
115              const char *partial_file_name,
116              int match_start_point,
117              int max_return_elements,
118              SearchFilter *searcher,
119              bool &word_complete,
120              lldb_private::StringList &matches);
121 
122     static int
123     SettingsNames (CommandInterpreter &interpreter,
124                    const char *partial_file_name,
125                    int match_start_point,
126                    int max_return_elements,
127                    SearchFilter *searcher,
128                    bool &word_complete,
129                    lldb_private::StringList &matches);
130 
131     static int
132     PlatformPluginNames (CommandInterpreter &interpreter,
133                          const char *partial_file_name,
134                          int match_start_point,
135                          int max_return_elements,
136                          SearchFilter *searcher,
137                          bool &word_complete,
138                          lldb_private::StringList &matches);
139 
140 
141     static int
142     ArchitectureNames (CommandInterpreter &interpreter,
143                        const char *partial_file_name,
144                        int match_start_point,
145                        int max_return_elements,
146                        SearchFilter *searcher,
147                        bool &word_complete,
148                        lldb_private::StringList &matches);
149 
150     static int
151     VariablePath (CommandInterpreter &interpreter,
152                   const char *partial_file_name,
153                   int match_start_point,
154                   int max_return_elements,
155                   SearchFilter *searcher,
156                   bool &word_complete,
157                   lldb_private::StringList &matches);
158 
159     //----------------------------------------------------------------------
160     // The Completer class is a convenient base class for building searchers
161     // that go along with the SearchFilter passed to the standard Completer
162     // functions.
163     //----------------------------------------------------------------------
164     class Completer : public Searcher
165     {
166     public:
167         Completer (CommandInterpreter &interpreter,
168                    const char *completion_str,
169                    int match_start_point,
170                    int max_return_elements,
171                    StringList &matches);
172 
173         virtual ~Completer ();
174 
175         virtual CallbackReturn
176         SearchCallback (SearchFilter &filter,
177                         SymbolContext &context,
178                         Address *addr,
179                         bool complete) = 0;
180 
181         virtual Depth
182         GetDepth () = 0;
183 
184         virtual size_t
185         DoCompletion (SearchFilter *filter) = 0;
186 
187         protected:
188             CommandInterpreter &m_interpreter;
189             std::string m_completion_str;
190             int m_match_start_point;
191             int m_max_return_elements;
192             StringList &m_matches;
193         private:
194             DISALLOW_COPY_AND_ASSIGN (Completer);
195     };
196 
197     //----------------------------------------------------------------------
198     // SouceFileCompleter implements the source file completer
199     //----------------------------------------------------------------------
200     class SourceFileCompleter : public Completer
201     {
202     public:
203 
204         SourceFileCompleter (CommandInterpreter &interpreter,
205                              bool include_support_files,
206                              const char *completion_str,
207                              int match_start_point,
208                              int max_return_elements,
209                              StringList &matches);
210 
211         virtual Searcher::Depth GetDepth ();
212 
213         virtual Searcher::CallbackReturn
214         SearchCallback (SearchFilter &filter,
215                         SymbolContext &context,
216                         Address *addr,
217                         bool complete);
218 
219         size_t
220         DoCompletion (SearchFilter *filter);
221 
222     private:
223         bool m_include_support_files;
224         FileSpecList m_matching_files;
225         const char *m_file_name;
226         const char *m_dir_name;
227         DISALLOW_COPY_AND_ASSIGN (SourceFileCompleter);
228 
229     };
230 
231     //----------------------------------------------------------------------
232     // ModuleCompleter implements the module completer
233     //----------------------------------------------------------------------
234     class ModuleCompleter : public Completer
235     {
236     public:
237 
238         ModuleCompleter (CommandInterpreter &interpreter,
239                          const char *completion_str,
240                          int match_start_point,
241                          int max_return_elements,
242                          StringList &matches);
243 
244         virtual Searcher::Depth GetDepth ();
245 
246         virtual Searcher::CallbackReturn
247         SearchCallback (SearchFilter &filter,
248                         SymbolContext &context,
249                         Address *addr,
250                         bool complete);
251 
252         size_t
253         DoCompletion (SearchFilter *filter);
254 
255     private:
256         const char *m_file_name;
257         const char *m_dir_name;
258         DISALLOW_COPY_AND_ASSIGN (ModuleCompleter);
259 
260     };
261 
262     //----------------------------------------------------------------------
263     // SymbolCompleter implements the symbol completer
264     //----------------------------------------------------------------------
265     class SymbolCompleter : public Completer
266     {
267     public:
268 
269         SymbolCompleter (CommandInterpreter &interpreter,
270                          const char *completion_str,
271                          int match_start_point,
272                          int max_return_elements,
273                          StringList &matches);
274 
275         virtual Searcher::Depth GetDepth ();
276 
277         virtual Searcher::CallbackReturn
278         SearchCallback (SearchFilter &filter,
279                         SymbolContext &context,
280                         Address *addr,
281                         bool complete);
282 
283         size_t
284         DoCompletion (SearchFilter *filter);
285 
286     private:
287 //        struct NameCmp {
288 //            bool operator() (const ConstString& lhs, const ConstString& rhs) const
289 //            {
290 //                return lhs < rhs;
291 //            }
292 //        };
293 
294         RegularExpression m_regex;
295         typedef std::set<ConstString> collection;
296         collection m_match_set;
297         DISALLOW_COPY_AND_ASSIGN (SymbolCompleter);
298 
299     };
300 
301 private:
302     static CommonCompletionElement g_common_completions[];
303 
304 };
305 
306 } // namespace lldb_private
307 #endif  // lldb_CommandCompletions_h_
308