Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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 | |
| 22 | namespace lldb_private { |
| 23 | class TildeExpressionResolver; |
| 24 | class CommandCompletions { |
| 25 | public: |
| 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 int (*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 int DiskFiles(CommandInterpreter &interpreter, |
| 61 | CompletionRequest &request, SearchFilter *searcher); |
| 62 | |
| 63 | static int DiskFiles(const llvm::Twine &partial_file_name, |
| 64 | StringList &matches, TildeExpressionResolver &Resolver); |
| 65 | |
| 66 | static int DiskDirectories(CommandInterpreter &interpreter, |
| 67 | CompletionRequest &request, |
| 68 | SearchFilter *searcher); |
| 69 | |
| 70 | static int DiskDirectories(const llvm::Twine &partial_file_name, |
| 71 | StringList &matches, |
| 72 | TildeExpressionResolver &Resolver); |
| 73 | |
| 74 | static int SourceFiles(CommandInterpreter &interpreter, |
| 75 | CompletionRequest &request, SearchFilter *searcher); |
| 76 | |
| 77 | static int Modules(CommandInterpreter &interpreter, |
| 78 | CompletionRequest &request, SearchFilter *searcher); |
| 79 | |
| 80 | static int Symbols(CommandInterpreter &interpreter, |
| 81 | CompletionRequest &request, SearchFilter *searcher); |
| 82 | |
| 83 | static int SettingsNames(CommandInterpreter &interpreter, |
| 84 | CompletionRequest &request, SearchFilter *searcher); |
| 85 | |
| 86 | static int PlatformPluginNames(CommandInterpreter &interpreter, |
| 87 | CompletionRequest &request, |
| 88 | SearchFilter *searcher); |
| 89 | |
| 90 | static int ArchitectureNames(CommandInterpreter &interpreter, |
| 91 | CompletionRequest &request, |
| 92 | SearchFilter *searcher); |
| 93 | |
| 94 | static int 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, bool complete) override = 0; |
| 107 | |
| 108 | lldb::SearchDepth GetDepth() override = 0; |
| 109 | |
| 110 | virtual size_t 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, |
| 131 | bool complete) override; |
| 132 | |
| 133 | size_t DoCompletion(SearchFilter *filter) override; |
| 134 | |
| 135 | private: |
| 136 | bool m_include_support_files; |
| 137 | FileSpecList m_matching_files; |
| 138 | const char *m_file_name; |
| 139 | const char *m_dir_name; |
| 140 | |
| 141 | DISALLOW_COPY_AND_ASSIGN(SourceFileCompleter); |
| 142 | }; |
| 143 | |
| 144 | // ModuleCompleter implements the module completer |
| 145 | class ModuleCompleter : public Completer { |
| 146 | public: |
| 147 | ModuleCompleter(CommandInterpreter &interpreter, |
| 148 | CompletionRequest &request); |
| 149 | |
| 150 | lldb::SearchDepth GetDepth() override; |
| 151 | |
| 152 | Searcher::CallbackReturn SearchCallback(SearchFilter &filter, |
| 153 | SymbolContext &context, |
| 154 | Address *addr, |
| 155 | bool complete) override; |
| 156 | |
| 157 | size_t DoCompletion(SearchFilter *filter) override; |
| 158 | |
| 159 | private: |
| 160 | const char *m_file_name; |
| 161 | const char *m_dir_name; |
| 162 | |
| 163 | DISALLOW_COPY_AND_ASSIGN(ModuleCompleter); |
| 164 | }; |
| 165 | |
| 166 | // SymbolCompleter implements the symbol completer |
| 167 | class SymbolCompleter : public Completer { |
| 168 | public: |
| 169 | SymbolCompleter(CommandInterpreter &interpreter, |
| 170 | CompletionRequest &request); |
| 171 | |
| 172 | lldb::SearchDepth GetDepth() override; |
| 173 | |
| 174 | Searcher::CallbackReturn SearchCallback(SearchFilter &filter, |
| 175 | SymbolContext &context, |
| 176 | Address *addr, |
| 177 | bool complete) override; |
| 178 | |
| 179 | size_t DoCompletion(SearchFilter *filter) override; |
| 180 | |
| 181 | private: |
| 182 | // struct NameCmp { |
| 183 | // bool operator() (const ConstString& lhs, const ConstString& |
| 184 | // rhs) const |
| 185 | // { |
| 186 | // return lhs < rhs; |
| 187 | // } |
| 188 | // }; |
| 189 | |
| 190 | RegularExpression m_regex; |
| 191 | typedef std::set<ConstString> collection; |
| 192 | collection m_match_set; |
| 193 | |
| 194 | DISALLOW_COPY_AND_ASSIGN(SymbolCompleter); |
| 195 | }; |
| 196 | |
| 197 | private: |
| 198 | static CommonCompletionElement g_common_completions[]; |
| 199 | }; |
| 200 | |
| 201 | } // namespace lldb_private |
| 202 | |
| 203 | #endif // lldb_CommandCompletions_h_ |