clang-tools 6.0.0

QueryParser.cpp
Go to the documentation of this file.
1 //===---- QueryParser.cpp - clang-query command parser --------------------===//
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 #include "QueryParser.h"
11 #include "Query.h"
12 #include "QuerySession.h"
13 #include "clang/ASTMatchers/Dynamic/Parser.h"
14 #include "clang/Basic/CharInfo.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include <set>
18
19 using namespace llvm;
20 using namespace clang::ast_matchers::dynamic;
21
22 namespace clang {
23 namespace query {
24
25 // Lex any amount of whitespace followed by a "word" (any sequence of
26 // non-whitespace characters) from the start of region [Begin,End). If no word
27 // is found before End, return StringRef(). Begin is adjusted to exclude the
28 // lexed region.
29 StringRef QueryParser::lexWord() {
30 while (true) {
31 if (Begin == End)
32 return StringRef(Begin, 0);
33
34 if (!isWhitespace(*Begin))
35 break;
36
37 ++Begin;
38 }
39
40 const char *WordBegin = Begin;
41
42 while (true) {
43 ++Begin;
44
45 if (Begin == End || isWhitespace(*Begin))
46 return StringRef(WordBegin, Begin - WordBegin);
47 }
48 }
49
50 // This is the StringSwitch-alike used by lexOrCompleteWord below. See that
51 // function for details.
52 template <typename T> struct QueryParser::LexOrCompleteWord {
53 StringSwitch<T> Switch;
54
55 QueryParser *P;
56 StringRef Word;
57 // Set to the completion point offset in Word, or StringRef::npos if
58 // completion point not in Word.
59 size_t WordCompletionPos;
60
61 LexOrCompleteWord(QueryParser *P, StringRef Word, size_t WCP)
62 : Switch(Word), P(P), Word(Word), WordCompletionPos(WCP) {}
63
64 template <unsigned N>
65 LexOrCompleteWord &Case(const char (&S)[N], const T &Value,
66 bool IsCompletion = true) {
67 StringRef CaseStr(S, N - 1);
68
69 if (WordCompletionPos == StringRef::npos)
70 Switch.Case(S, Value);
71 else if (N != 1 && IsCompletion && WordCompletionPos <= CaseStr.size() &&
72 CaseStr.substr(0, WordCompletionPos) ==
73 Word.substr(0, WordCompletionPos))
74 P->Completions.push_back(LineEditor::Completion(
75 (CaseStr.substr(WordCompletionPos) + " ").str(), CaseStr));
76 return *this;
77 }
78
79 T Default(const T &Value) const { return Switch.Default(Value); }
80 };
81
82 // Lexes a word and stores it in Word. Returns a LexOrCompleteWord<T> object
83 // that can be used like a llvm::StringSwitch<T>, but adds cases as possible
84 // completions if the lexed word contains the completion point.
85 template <typename T>
86 QueryParser::LexOrCompleteWord<T>
87 QueryParser::lexOrCompleteWord(StringRef &Word) {
88 Word = lexWord();
89 size_t WordCompletionPos = StringRef::npos;
90 if (CompletionPos && CompletionPos <= Word.data() + Word.size()) {
91 if (CompletionPos < Word.data())
92 WordCompletionPos = 0;
93 else
94 WordCompletionPos = CompletionPos - Word.data();
95 }
96 return LexOrCompleteWord<T>(this, Word, WordCompletionPos);
97 }
98
99 QueryRef QueryParser::parseSetBool(bool QuerySession::*Var) {
100 StringRef ValStr;
101 unsigned Value = lexOrCompleteWord<unsigned>(ValStr)
102 .Case("false", 0)
103 .Case("true", 1)
104 .Default(~0u);
105 if (Value == ~0u) {
106 return new InvalidQuery("expected 'true' or 'false', got '" + ValStr + "'");
107 }
108 return new SetQuery<bool>(Var, Value);
109 }
110
111 QueryRef QueryParser::parseSetOutputKind() {
112 StringRef ValStr;
113 unsigned OutKind = lexOrCompleteWord<unsigned>(ValStr)
114 .Case("diag", OK_Diag)
115 .Case("print", OK_Print)
116 .Case("dump", OK_Dump)
117 .Default(~0u);
118 if (OutKind == ~0u) {
119 return new InvalidQuery("expected 'diag', 'print' or 'dump', got '" +
120 ValStr + "'");
121 }
122 return new SetQuery<OutputKind>(&QuerySession::OutKind, OutputKind(OutKind));
123 }
124
125 QueryRef QueryParser::endQuery(QueryRef Q) {
126 const char *Extra = Begin;
127 if (!lexWord().empty())
128 return new InvalidQuery("unexpected extra input: '" +
129 StringRef(Extra, End - Extra) + "'");
130 return Q;
131 }
132
133 namespace {
134
135 enum ParsedQueryKind {
136 PQK_Invalid,
137 PQK_NoOp,
138 PQK_Help,
139 PQK_Let,
140 PQK_Match,
141 PQK_Set,
142 PQK_Unlet,
143 PQK_Quit
144 };
145
146 enum ParsedQueryVariable { PQV_Invalid, PQV_Output, PQV_BindRoot };
147
148 QueryRef makeInvalidQueryFromDiagnostics(const Diagnostics &Diag) {
149 std::string ErrStr;
150 llvm::raw_string_ostream OS(ErrStr);
151 Diag.printToStreamFull(OS);
152 return new InvalidQuery(OS.str());
153 }
154
155 } // namespace
156
157 QueryRef QueryParser::completeMatcherExpression() {
158 std::vector<MatcherCompletion> Comps = Parser::completeExpression(
159 StringRef(Begin, End - Begin), CompletionPos - Begin, nullptr,
160 &QS.NamedValues);
161 for (auto I = Comps.begin(), E = Comps.end(); I != E; ++I) {
162 Completions.push_back(LineEditor::Completion(I->TypedText, I->MatcherDecl));
163 }
164 return QueryRef();
165 }
166
167 QueryRef QueryParser::doParse() {
168 StringRef CommandStr;
169 ParsedQueryKind QKind = lexOrCompleteWord<ParsedQueryKind>(CommandStr)
170 .Case("", PQK_NoOp)
171 .Case("help", PQK_Help)
172 .Case("m", PQK_Match, /*IsCompletion=*/false)
173 .Case("let", PQK_Let)
174 .Case("match", PQK_Match)
175 .Case("set", PQK_Set)
176 .Case("unlet", PQK_Unlet)
177 .Case("quit", PQK_Quit)
178 .Default(PQK_Invalid);
179
180 switch (QKind) {
181 case PQK_NoOp:
182 return new NoOpQuery;
183
184 case PQK_Help:
185 return endQuery(new HelpQuery);
186
187 case PQK_Quit:
188 return endQuery(new QuitQuery);
189
190 case PQK_Let: {
191 StringRef Name = lexWord();
192
193 if (Name.empty())
194 return new InvalidQuery("expected variable name");
195
196 if (CompletionPos)
197 return completeMatcherExpression();
198
199 Diagnostics Diag;
200 ast_matchers::dynamic::VariantValue Value;
201 if (!Parser::parseExpression(StringRef(Begin, End - Begin), nullptr,
202 &QS.NamedValues, &Value, &Diag)) {
203 return makeInvalidQueryFromDiagnostics(Diag);
204 }
205
206 return new LetQuery(Name, Value);
207 }
208
209 case PQK_Match: {
210 if (CompletionPos)
211 return completeMatcherExpression();
212
213 Diagnostics Diag;
214 Optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression(
215 StringRef(Begin, End - Begin), nullptr, &QS.NamedValues, &Diag);
216 if (!Matcher) {
217 return makeInvalidQueryFromDiagnostics(Diag);
218 }
219 return new MatchQuery(*Matcher);
220 }
221
222 case PQK_Set: {
223 StringRef VarStr;
224 ParsedQueryVariable Var = lexOrCompleteWord<ParsedQueryVariable>(VarStr)
225 .Case("output", PQV_Output)
226 .Case("bind-root", PQV_BindRoot)
227 .Default(PQV_Invalid);
228 if (VarStr.empty())
229 return new InvalidQuery("expected variable name");
230 if (Var == PQV_Invalid)
231 return new InvalidQuery("unknown variable: '" + VarStr + "'");
232
233 QueryRef Q;
234 switch (Var) {
235 case PQV_Output:
236 Q = parseSetOutputKind();
237 break;
238 case PQV_BindRoot:
239 Q = parseSetBool(&QuerySession::BindRoot);
240 break;
241 case PQV_Invalid:
242 llvm_unreachable("Invalid query kind");
243 }
244
245 return endQuery(Q);
246 }
247
248 case PQK_Unlet: {
249 StringRef Name = lexWord();
250
251 if (Name.empty())
252 return new InvalidQuery("expected variable name");
253
254 return endQuery(new LetQuery(Name, VariantValue()));
255 }
256
257 case PQK_Invalid:
258 return new InvalidQuery("unknown command: " + CommandStr);
259 }
260
261 llvm_unreachable("Invalid query kind");
262 }
263
264 QueryRef QueryParser::parse(StringRef Line, const QuerySession &QS) {
265 return QueryParser(Line, QS).doParse();
266 }
267
268 std::vector<LineEditor::Completion>
269 QueryParser::complete(StringRef Line, size_t Pos, const QuerySession &QS) {
270 QueryParser P(Line, QS);
271 P.CompletionPos = Line.data() + Pos;
272
273 P.doParse();
274 return P.Completions;
275 }
276
277 } // namespace query
278 } // namespace clang
llvm
Definition: ClangTidyOptions.cpp:31
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
clang::query::NoOpQuery
No-op query (i.e. a blank line).
Definition: Query.h:61
clang::query::QuerySession
Represents the state for a particular clang-query session.
Definition: QuerySession.h:25
clang::query::OK_Diag
Definition: Query.h:21
clang::query::QueryParser::LexOrCompleteWord::Default
T Default(const T &Value) const
Definition: QueryParser.cpp:79
Query.h
QueryParser.h
clang::query::@416::ParsedQueryKind
ParsedQueryKind
Definition: QueryParser.cpp:135
clang::query::InvalidQuery
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:51
QuerySession.h
clang::query::@416::ParsedQueryVariable
ParsedQueryVariable
Definition: QueryParser.cpp:146
clang::query::QueryParser::LexOrCompleteWord
Definition: QueryParser.cpp:52
clang::query::QueryRef
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:48
clang::query::MatchQuery
Query for "match MATCHER".
Definition: Query.h:85
clang::query::OutputKind
OutputKind
Definition: Query.h:21
clang::query::OK_Print
Definition: Query.h:21
clang::query::QueryParser::LexOrCompleteWord::WordCompletionPos
size_t WordCompletionPos
Definition: QueryParser.cpp:59
clang::query::QueryParser::LexOrCompleteWord::Case
LexOrCompleteWord & Case(const char(&S)[N], const T &Value, bool IsCompletion=true)
Definition: QueryParser.cpp:65
clang::query::QueryParser::LexOrCompleteWord::Word
StringRef Word
Definition: QueryParser.cpp:56
clang::query::QueryParser::LexOrCompleteWord::Switch
StringSwitch< T > Switch
Definition: QueryParser.cpp:53
clang
Definition: AndroidTidyModule.cpp:28
Line
int Line
Definition: PreprocessorTracker.cpp:527
clang::query::QuitQuery
Query for "quit".
Definition: Query.h:77
clang::query::OK_Dump
Definition: Query.h:21
clang::query::SetQuery
Query for "set VAR VALUE".
Definition: Query.h:117
clang::query::QueryParser::LexOrCompleteWord::P
QueryParser * P
Definition: QueryParser.cpp:55
dynamic
clang::query::LetQuery
Definition: Query.h:95
clang::query::QueryParser
Definition: QueryParser.h:23
clang::query::HelpQuery
Query for "help".
Definition: Query.h:69
clang::query::QueryParser::LexOrCompleteWord::LexOrCompleteWord
LexOrCompleteWord(QueryParser *P, StringRef Word, size_t WCP)
Definition: QueryParser.cpp:61