1 //===---- QueryParser.cpp - clang-query command parser --------------------===//
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 #include "QueryParser.h"
10 #include "Query.h"
11 #include "QuerySession.h"
12 #include "clang/ASTMatchers/Dynamic/Parser.h"
13 #include "clang/Basic/CharInfo.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include <set>
17 
18 using namespace llvm;
19 using namespace clang::ast_matchers::dynamic;
20 
21 namespace clang {
22 namespace query {
23 
24 // Lex any amount of whitespace followed by a "word" (any sequence of
25 // non-whitespace characters) from the start of region [Begin,End).  If no word
26 // is found before End, return StringRef().  Begin is adjusted to exclude the
27 // lexed region.
lexWord()28 StringRef QueryParser::lexWord() {
29   Line = Line.drop_while([](char c) {
30     // Don't trim newlines.
31     return StringRef(" \t\v\f\r").contains(c);
32   });
33 
34   if (Line.empty())
35     // Even though the Line is empty, it contains a pointer and
36     // a (zero) length. The pointer is used in the LexOrCompleteWord
37     // code completion.
38     return Line;
39 
40   StringRef Word;
41   if (Line.front() == '#')
42     Word = Line.substr(0, 1);
43   else
44     Word = Line.take_until(isWhitespace);
45 
46   Line = Line.drop_front(Word.size());
47   return Word;
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   StringRef Word;
54   StringSwitch<T> Switch;
55 
56   QueryParser *P;
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   // Lexes a word and stores it in Word. Returns a LexOrCompleteWord<T> object
62   // that can be used like a llvm::StringSwitch<T>, but adds cases as possible
63   // completions if the lexed word contains the completion point.
LexOrCompleteWordclang::query::QueryParser::LexOrCompleteWord64   LexOrCompleteWord(QueryParser *P, StringRef &OutWord)
65       : Word(P->lexWord()), Switch(Word), P(P),
66         WordCompletionPos(StringRef::npos) {
67     OutWord = Word;
68     if (P->CompletionPos && P->CompletionPos <= Word.data() + Word.size()) {
69       if (P->CompletionPos < Word.data())
70         WordCompletionPos = 0;
71       else
72         WordCompletionPos = P->CompletionPos - Word.data();
73     }
74   }
75 
Caseclang::query::QueryParser::LexOrCompleteWord76   LexOrCompleteWord &Case(llvm::StringLiteral CaseStr, const T &Value,
77                           bool IsCompletion = true) {
78 
79     if (WordCompletionPos == StringRef::npos)
80       Switch.Case(CaseStr, Value);
81     else if (CaseStr.size() != 0 && IsCompletion && WordCompletionPos <= CaseStr.size() &&
82              CaseStr.substr(0, WordCompletionPos) ==
83                  Word.substr(0, WordCompletionPos))
84       P->Completions.push_back(LineEditor::Completion(
85           (CaseStr.substr(WordCompletionPos) + " ").str(),
86           std::string(CaseStr)));
87     return *this;
88   }
89 
Defaultclang::query::QueryParser::LexOrCompleteWord90   T Default(T Value) { return Switch.Default(Value); }
91 };
92 
parseSetBool(bool QuerySession::* Var)93 QueryRef QueryParser::parseSetBool(bool QuerySession::*Var) {
94   StringRef ValStr;
95   unsigned Value = LexOrCompleteWord<unsigned>(this, ValStr)
96                        .Case("false", 0)
97                        .Case("true", 1)
98                        .Default(~0u);
99   if (Value == ~0u) {
100     return new InvalidQuery("expected 'true' or 'false', got '" + ValStr + "'");
101   }
102   return new SetQuery<bool>(Var, Value);
103 }
104 
parseSetOutputKind()105 template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() {
106   StringRef ValStr;
107   unsigned OutKind = LexOrCompleteWord<unsigned>(this, ValStr)
108                          .Case("diag", OK_Diag)
109                          .Case("print", OK_Print)
110                          .Case("detailed-ast", OK_DetailedAST)
111                          .Case("dump", OK_DetailedAST)
112                          .Default(~0u);
113   if (OutKind == ~0u) {
114     return new InvalidQuery(
115         "expected 'diag', 'print', 'detailed-ast' or 'dump', got '" + ValStr +
116         "'");
117   }
118 
119   switch (OutKind) {
120   case OK_DetailedAST:
121     return new QueryType(&QuerySession::DetailedASTOutput);
122   case OK_Diag:
123     return new QueryType(&QuerySession::DiagOutput);
124   case OK_Print:
125     return new QueryType(&QuerySession::PrintOutput);
126   }
127 
128   llvm_unreachable("Invalid output kind");
129 }
130 
parseSetTraversalKind(ast_type_traits::TraversalKind QuerySession::* Var)131 QueryRef QueryParser::parseSetTraversalKind(
132     ast_type_traits::TraversalKind QuerySession::*Var) {
133   StringRef ValStr;
134   unsigned Value =
135       LexOrCompleteWord<unsigned>(this, ValStr)
136           .Case("AsIs", ast_type_traits::TK_AsIs)
137           .Case("IgnoreUnlessSpelledInSource",
138                 ast_type_traits::TK_IgnoreUnlessSpelledInSource)
139           .Default(~0u);
140   if (Value == ~0u) {
141     return new InvalidQuery("expected traversal kind, got '" + ValStr + "'");
142   }
143   return new SetQuery<ast_type_traits::TraversalKind>(
144       Var, static_cast<ast_type_traits::TraversalKind>(Value));
145 }
146 
endQuery(QueryRef Q)147 QueryRef QueryParser::endQuery(QueryRef Q) {
148   StringRef Extra = Line;
149   StringRef ExtraTrimmed = Extra.drop_while(
150       [](char c) { return StringRef(" \t\v\f\r").contains(c); });
151 
152   if ((!ExtraTrimmed.empty() && ExtraTrimmed[0] == '\n') ||
153       (ExtraTrimmed.size() >= 2 && ExtraTrimmed[0] == '\r' &&
154        ExtraTrimmed[1] == '\n'))
155     Q->RemainingContent = Extra;
156   else {
157     StringRef TrailingWord = lexWord();
158     if (!TrailingWord.empty() && TrailingWord.front() == '#') {
159       Line = Line.drop_until([](char c) { return c == '\n'; });
160       Line = Line.drop_while([](char c) { return c == '\n'; });
161       return endQuery(Q);
162     }
163     if (!TrailingWord.empty()) {
164       return new InvalidQuery("unexpected extra input: '" + Extra + "'");
165     }
166   }
167   return Q;
168 }
169 
170 namespace {
171 
172 enum ParsedQueryKind {
173   PQK_Invalid,
174   PQK_Comment,
175   PQK_NoOp,
176   PQK_Help,
177   PQK_Let,
178   PQK_Match,
179   PQK_Set,
180   PQK_Unlet,
181   PQK_Quit,
182   PQK_Enable,
183   PQK_Disable
184 };
185 
186 enum ParsedQueryVariable {
187   PQV_Invalid,
188   PQV_Output,
189   PQV_BindRoot,
190   PQV_PrintMatcher,
191   PQV_Traversal
192 };
193 
makeInvalidQueryFromDiagnostics(const Diagnostics & Diag)194 QueryRef makeInvalidQueryFromDiagnostics(const Diagnostics &Diag) {
195   std::string ErrStr;
196   llvm::raw_string_ostream OS(ErrStr);
197   Diag.printToStreamFull(OS);
198   return new InvalidQuery(OS.str());
199 }
200 
201 } // namespace
202 
completeMatcherExpression()203 QueryRef QueryParser::completeMatcherExpression() {
204   std::vector<MatcherCompletion> Comps = Parser::completeExpression(
205       Line, CompletionPos - Line.begin(), nullptr, &QS.NamedValues);
206   for (auto I = Comps.begin(), E = Comps.end(); I != E; ++I) {
207     Completions.push_back(LineEditor::Completion(I->TypedText, I->MatcherDecl));
208   }
209   return QueryRef();
210 }
211 
doParse()212 QueryRef QueryParser::doParse() {
213   StringRef CommandStr;
214   ParsedQueryKind QKind = LexOrCompleteWord<ParsedQueryKind>(this, CommandStr)
215                               .Case("", PQK_NoOp)
216                               .Case("#", PQK_Comment, /*IsCompletion=*/false)
217                               .Case("help", PQK_Help)
218                               .Case("l", PQK_Let, /*IsCompletion=*/false)
219                               .Case("let", PQK_Let)
220                               .Case("m", PQK_Match, /*IsCompletion=*/false)
221                               .Case("match", PQK_Match)
222                               .Case("q", PQK_Quit,  /*IsCompletion=*/false)
223                               .Case("quit", PQK_Quit)
224                               .Case("set", PQK_Set)
225                               .Case("enable", PQK_Enable)
226                               .Case("disable", PQK_Disable)
227                               .Case("unlet", PQK_Unlet)
228                               .Default(PQK_Invalid);
229 
230   switch (QKind) {
231   case PQK_Comment:
232   case PQK_NoOp:
233     Line = Line.drop_until([](char c) { return c == '\n'; });
234     Line = Line.drop_while([](char c) { return c == '\n'; });
235     if (Line.empty())
236       return new NoOpQuery;
237     return doParse();
238 
239   case PQK_Help:
240     return endQuery(new HelpQuery);
241 
242   case PQK_Quit:
243     return endQuery(new QuitQuery);
244 
245   case PQK_Let: {
246     StringRef Name = lexWord();
247 
248     if (Name.empty())
249       return new InvalidQuery("expected variable name");
250 
251     if (CompletionPos)
252       return completeMatcherExpression();
253 
254     Diagnostics Diag;
255     ast_matchers::dynamic::VariantValue Value;
256     if (!Parser::parseExpression(Line, nullptr, &QS.NamedValues, &Value,
257                                  &Diag)) {
258       return makeInvalidQueryFromDiagnostics(Diag);
259     }
260 
261     auto *Q = new LetQuery(Name, Value);
262     Q->RemainingContent = Line;
263     return Q;
264   }
265 
266   case PQK_Match: {
267     if (CompletionPos)
268       return completeMatcherExpression();
269 
270     Diagnostics Diag;
271     auto MatcherSource = Line.ltrim();
272     auto OrigMatcherSource = MatcherSource;
273     Optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression(
274         MatcherSource, nullptr, &QS.NamedValues, &Diag);
275     if (!Matcher) {
276       return makeInvalidQueryFromDiagnostics(Diag);
277     }
278     auto ActualSource = OrigMatcherSource.slice(0, OrigMatcherSource.size() -
279                                                        MatcherSource.size());
280     auto *Q = new MatchQuery(ActualSource, *Matcher);
281     Q->RemainingContent = MatcherSource;
282     return Q;
283   }
284 
285   case PQK_Set: {
286     StringRef VarStr;
287     ParsedQueryVariable Var =
288         LexOrCompleteWord<ParsedQueryVariable>(this, VarStr)
289             .Case("output", PQV_Output)
290             .Case("bind-root", PQV_BindRoot)
291             .Case("print-matcher", PQV_PrintMatcher)
292             .Case("traversal", PQV_Traversal)
293             .Default(PQV_Invalid);
294     if (VarStr.empty())
295       return new InvalidQuery("expected variable name");
296     if (Var == PQV_Invalid)
297       return new InvalidQuery("unknown variable: '" + VarStr + "'");
298 
299     QueryRef Q;
300     switch (Var) {
301     case PQV_Output:
302       Q = parseSetOutputKind<SetExclusiveOutputQuery>();
303       break;
304     case PQV_BindRoot:
305       Q = parseSetBool(&QuerySession::BindRoot);
306       break;
307     case PQV_PrintMatcher:
308       Q = parseSetBool(&QuerySession::PrintMatcher);
309       break;
310     case PQV_Traversal:
311       Q = parseSetTraversalKind(&QuerySession::TK);
312       break;
313     case PQV_Invalid:
314       llvm_unreachable("Invalid query kind");
315     }
316 
317     return endQuery(Q);
318   }
319   case PQK_Enable:
320   case PQK_Disable: {
321     StringRef VarStr;
322     ParsedQueryVariable Var =
323         LexOrCompleteWord<ParsedQueryVariable>(this, VarStr)
324             .Case("output", PQV_Output)
325             .Default(PQV_Invalid);
326     if (VarStr.empty())
327       return new InvalidQuery("expected variable name");
328     if (Var == PQV_Invalid)
329       return new InvalidQuery("unknown variable: '" + VarStr + "'");
330 
331     QueryRef Q;
332 
333     if (QKind == PQK_Enable)
334       Q = parseSetOutputKind<EnableOutputQuery>();
335     else if (QKind == PQK_Disable)
336       Q = parseSetOutputKind<DisableOutputQuery>();
337     else
338       llvm_unreachable("Invalid query kind");
339     return endQuery(Q);
340   }
341 
342   case PQK_Unlet: {
343     StringRef Name = lexWord();
344 
345     if (Name.empty())
346       return new InvalidQuery("expected variable name");
347 
348     return endQuery(new LetQuery(Name, VariantValue()));
349   }
350 
351   case PQK_Invalid:
352     return new InvalidQuery("unknown command: " + CommandStr);
353   }
354 
355   llvm_unreachable("Invalid query kind");
356 }
357 
parse(StringRef Line,const QuerySession & QS)358 QueryRef QueryParser::parse(StringRef Line, const QuerySession &QS) {
359   return QueryParser(Line, QS).doParse();
360 }
361 
362 std::vector<LineEditor::Completion>
complete(StringRef Line,size_t Pos,const QuerySession & QS)363 QueryParser::complete(StringRef Line, size_t Pos, const QuerySession &QS) {
364   QueryParser P(Line, QS);
365   P.CompletionPos = Line.data() + Pos;
366 
367   P.doParse();
368   return P.Completions;
369 }
370 
371 } // namespace query
372 } // namespace clang
373