1//===-- CommandCompletions.h ------------------------------------*- C++ -*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef lldb_CommandCompletions_h_
10#define lldb_CommandCompletions_h_
11
12#include <set>
13
14#include "lldb/Core/FileSpecList.h"
15#include "lldb/Core/SearchFilter.h"
16#include "lldb/Utility/CompletionRequest.h"
17#include "lldb/Utility/RegularExpression.h"
18#include "lldb/lldb-private.h"
19
20#include "llvm/ADT/Twine.h"
21
22namespace lldb_private {
23class TildeExpressionResolver;
24class CommandCompletions {
25public:
26  // This is the command completion callback that is used to complete the
27  // argument of the option it is bound to (in the OptionDefinition table
28  // below).  Return the total number of matches.
29  typedef void (*CompletionCallback)(CommandInterpreter &interpreter,
30                                     CompletionRequest &request,
31                                     // A search filter to limit the search...
32                                     lldb_private::SearchFilter *searcher);
33  enum CommonCompletionTypes {
34    eNoCompletion = 0u,
35    eSourceFileCompletion = (1u << 0),
36    eDiskFileCompletion = (1u << 1),
37    eDiskDirectoryCompletion = (1u << 2),
38    eSymbolCompletion = (1u << 3),
39    eModuleCompletion = (1u << 4),
40    eSettingsNameCompletion = (1u << 5),
41    ePlatformPluginCompletion = (1u << 6),
42    eArchitectureCompletion = (1u << 7),
43    eVariablePathCompletion = (1u << 8),
44    // This item serves two purposes.  It is the last element in the enum, so
45    // you can add custom enums starting from here in your Option class. Also
46    // if you & in this bit the base code will not process the option.
47    eCustomCompletion = (1u << 9)
48  };
49
50  struct CommonCompletionElement {
51    uint32_t type;
52    CompletionCallback callback;
53  };
54
55  static bool InvokeCommonCompletionCallbacks(
56      CommandInterpreter &interpreter, uint32_t completion_mask,
57      lldb_private::CompletionRequest &request, SearchFilter *searcher);
58
59  // These are the generic completer functions:
60  static void DiskFiles(CommandInterpreter &interpreter,
61                        CompletionRequest &request, SearchFilter *searcher);
62
63  static void DiskFiles(const llvm::Twine &partial_file_name,
64                        StringList &matches, TildeExpressionResolver &Resolver);
65
66  static void DiskDirectories(CommandInterpreter &interpreter,
67                              CompletionRequest &request,
68                              SearchFilter *searcher);
69
70  static void DiskDirectories(const llvm::Twine &partial_file_name,
71                              StringList &matches,
72                              TildeExpressionResolver &Resolver);
73
74  static void SourceFiles(CommandInterpreter &interpreter,
75                          CompletionRequest &request, SearchFilter *searcher);
76
77  static void Modules(CommandInterpreter &interpreter,
78                      CompletionRequest &request, SearchFilter *searcher);
79
80  static void Symbols(CommandInterpreter &interpreter,
81                      CompletionRequest &request, SearchFilter *searcher);
82
83  static void SettingsNames(CommandInterpreter &interpreter,
84                            CompletionRequest &request, SearchFilter *searcher);
85
86  static void PlatformPluginNames(CommandInterpreter &interpreter,
87                                  CompletionRequest &request,
88                                  SearchFilter *searcher);
89
90  static void ArchitectureNames(CommandInterpreter &interpreter,
91                                CompletionRequest &request,
92                                SearchFilter *searcher);
93
94  static void VariablePath(CommandInterpreter &interpreter,
95                           CompletionRequest &request, SearchFilter *searcher);
96
97  // The Completer class is a convenient base class for building searchers that
98  // go along with the SearchFilter passed to the standard Completer functions.
99  class Completer : public Searcher {
100  public:
101    Completer(CommandInterpreter &interpreter, CompletionRequest &request);
102
103    ~Completer() override;
104
105    CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context,
106                                  Address *addr) override = 0;
107
108    lldb::SearchDepth GetDepth() override = 0;
109
110    virtual void DoCompletion(SearchFilter *filter) = 0;
111
112  protected:
113    CommandInterpreter &m_interpreter;
114    CompletionRequest &m_request;
115
116  private:
117    DISALLOW_COPY_AND_ASSIGN(Completer);
118  };
119
120  // SourceFileCompleter implements the source file completer
121  class SourceFileCompleter : public Completer {
122  public:
123    SourceFileCompleter(CommandInterpreter &interpreter,
124                        bool include_support_files, CompletionRequest &request);
125
126    lldb::SearchDepth GetDepth() override;
127
128    Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
129                                            SymbolContext &context,
130                                            Address *addr) override;
131
132    void DoCompletion(SearchFilter *filter) override;
133
134  private:
135    bool m_include_support_files;
136    FileSpecList m_matching_files;
137    const char *m_file_name;
138    const char *m_dir_name;
139
140    DISALLOW_COPY_AND_ASSIGN(SourceFileCompleter);
141  };
142
143  // ModuleCompleter implements the module completer
144  class ModuleCompleter : public Completer {
145  public:
146    ModuleCompleter(CommandInterpreter &interpreter,
147                    CompletionRequest &request);
148
149    lldb::SearchDepth GetDepth() override;
150
151    Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
152                                            SymbolContext &context,
153                                            Address *addr) override;
154
155    void DoCompletion(SearchFilter *filter) override;
156
157  private:
158    const char *m_file_name;
159    const char *m_dir_name;
160
161    DISALLOW_COPY_AND_ASSIGN(ModuleCompleter);
162  };
163
164  // SymbolCompleter implements the symbol completer
165  class SymbolCompleter : public Completer {
166  public:
167    SymbolCompleter(CommandInterpreter &interpreter,
168                    CompletionRequest &request);
169
170    lldb::SearchDepth GetDepth() override;
171
172    Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
173                                            SymbolContext &context,
174                                            Address *addr) override;
175
176    void DoCompletion(SearchFilter *filter) override;
177
178  private:
179    RegularExpression m_regex;
180    typedef std::set<ConstString> collection;
181    collection m_match_set;
182
183    DISALLOW_COPY_AND_ASSIGN(SymbolCompleter);
184  };
185
186private:
187  static CommonCompletionElement g_common_completions[];
188};
189
190} // namespace lldb_private
191
192#endif // lldb_CommandCompletions_h_
193