clang-tools  10.0.0git
QueryParser.cpp
Go to the documentation of this file.
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.
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 
57  // Set to the completion point offset in Word, or StringRef::npos if
58  // completion point not in Word.
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.
64  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 
76  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(), CaseStr));
86  return *this;
87  }
88 
89  T Default(T Value) { return Switch.Default(Value); }
90 };
91 
92 QueryRef QueryParser::parseSetBool(bool QuerySession::*Var) {
93  StringRef ValStr;
94  unsigned Value = LexOrCompleteWord<unsigned>(this, ValStr)
95  .Case("false", 0)
96  .Case("true", 1)
97  .Default(~0u);
98  if (Value == ~0u) {
99  return new InvalidQuery("expected 'true' or 'false', got '" + ValStr + "'");
100  }
101  return new SetQuery<bool>(Var, Value);
102 }
103 
104 template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() {
105  StringRef ValStr;
106  unsigned OutKind = LexOrCompleteWord<unsigned>(this, ValStr)
107  .Case("diag", OK_Diag)
108  .Case("print", OK_Print)
109  .Case("detailed-ast", OK_DetailedAST)
110  .Case("dump", OK_DetailedAST)
111  .Default(~0u);
112  if (OutKind == ~0u) {
113  return new InvalidQuery(
114  "expected 'diag', 'print', 'detailed-ast' or 'dump', got '" + ValStr +
115  "'");
116  }
117 
118  switch (OutKind) {
119  case OK_DetailedAST:
120  return new QueryType(&QuerySession::DetailedASTOutput);
121  case OK_Diag:
122  return new QueryType(&QuerySession::DiagOutput);
123  case OK_Print:
124  return new QueryType(&QuerySession::PrintOutput);
125  }
126 
127  llvm_unreachable("Invalid output kind");
128 }
129 
130 QueryRef QueryParser::endQuery(QueryRef Q) {
131  StringRef Extra = Line;
132  StringRef ExtraTrimmed = Extra.drop_while(
133  [](char c) { return StringRef(" \t\v\f\r").contains(c); });
134 
135  if ((!ExtraTrimmed.empty() && ExtraTrimmed[0] == '\n') ||
136  (ExtraTrimmed.size() >= 2 && ExtraTrimmed[0] == '\r' &&
137  ExtraTrimmed[1] == '\n'))
138  Q->RemainingContent = Extra;
139  else {
140  StringRef TrailingWord = lexWord();
141  if (!TrailingWord.empty() && TrailingWord.front() == '#') {
142  Line = Line.drop_until([](char c) { return c == '\n'; });
143  Line = Line.drop_while([](char c) { return c == '\n'; });
144  return endQuery(Q);
145  }
146  if (!TrailingWord.empty()) {
147  return new InvalidQuery("unexpected extra input: '" + Extra + "'");
148  }
149  }
150  return Q;
151 }
152 
153 namespace {
154 
156  PQK_Invalid,
157  PQK_Comment,
158  PQK_NoOp,
159  PQK_Help,
160  PQK_Let,
161  PQK_Match,
162  PQK_Set,
163  PQK_Unlet,
164  PQK_Quit,
165  PQK_Enable,
166  PQK_Disable
167 };
168 
170  PQV_Invalid,
171  PQV_Output,
172  PQV_BindRoot,
173  PQV_PrintMatcher
174 };
175 
176 QueryRef makeInvalidQueryFromDiagnostics(const Diagnostics &Diag) {
177  std::string ErrStr;
178  llvm::raw_string_ostream OS(ErrStr);
179  Diag.printToStreamFull(OS);
180  return new InvalidQuery(OS.str());
181 }
182 
183 } // namespace
184 
185 QueryRef QueryParser::completeMatcherExpression() {
186  std::vector<MatcherCompletion> Comps = Parser::completeExpression(
187  Line, CompletionPos - Line.begin(), nullptr, &QS.NamedValues);
188  for (auto I = Comps.begin(), E = Comps.end(); I != E; ++I) {
189  Completions.push_back(LineEditor::Completion(I->TypedText, I->MatcherDecl));
190  }
191  return QueryRef();
192 }
193 
194 QueryRef QueryParser::doParse() {
195  StringRef CommandStr;
196  ParsedQueryKind QKind = LexOrCompleteWord<ParsedQueryKind>(this, CommandStr)
197  .Case("", PQK_NoOp)
198  .Case("#", PQK_Comment, /*IsCompletion=*/false)
199  .Case("help", PQK_Help)
200  .Case("l", PQK_Let, /*IsCompletion=*/false)
201  .Case("let", PQK_Let)
202  .Case("m", PQK_Match, /*IsCompletion=*/false)
203  .Case("match", PQK_Match)
204  .Case("q", PQK_Quit, /*IsCompletion=*/false)
205  .Case("quit", PQK_Quit)
206  .Case("set", PQK_Set)
207  .Case("enable", PQK_Enable)
208  .Case("disable", PQK_Disable)
209  .Case("unlet", PQK_Unlet)
210  .Default(PQK_Invalid);
211 
212  switch (QKind) {
213  case PQK_Comment:
214  case PQK_NoOp:
215  Line = Line.drop_until([](char c) { return c == '\n'; });
216  Line = Line.drop_while([](char c) { return c == '\n'; });
217  if (Line.empty())
218  return new NoOpQuery;
219  return doParse();
220 
221  case PQK_Help:
222  return endQuery(new HelpQuery);
223 
224  case PQK_Quit:
225  return endQuery(new QuitQuery);
226 
227  case PQK_Let: {
228  StringRef Name = lexWord();
229 
230  if (Name.empty())
231  return new InvalidQuery("expected variable name");
232 
233  if (CompletionPos)
234  return completeMatcherExpression();
235 
236  Diagnostics Diag;
237  ast_matchers::dynamic::VariantValue Value;
238  if (!Parser::parseExpression(Line, nullptr, &QS.NamedValues, &Value,
239  &Diag)) {
240  return makeInvalidQueryFromDiagnostics(Diag);
241  }
242 
243  auto *Q = new LetQuery(Name, Value);
244  Q->RemainingContent = Line;
245  return Q;
246  }
247 
248  case PQK_Match: {
249  if (CompletionPos)
250  return completeMatcherExpression();
251 
252  Diagnostics Diag;
253  auto MatcherSource = Line.ltrim();
254  auto OrigMatcherSource = MatcherSource;
255  Optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression(
256  MatcherSource, nullptr, &QS.NamedValues, &Diag);
257  if (!Matcher) {
258  return makeInvalidQueryFromDiagnostics(Diag);
259  }
260  auto ActualSource = OrigMatcherSource.slice(0, OrigMatcherSource.size() -
261  MatcherSource.size());
262  auto *Q = new MatchQuery(ActualSource, *Matcher);
263  Q->RemainingContent = MatcherSource;
264  return Q;
265  }
266 
267  case PQK_Set: {
268  StringRef VarStr;
269  ParsedQueryVariable Var =
270  LexOrCompleteWord<ParsedQueryVariable>(this, VarStr)
271  .Case("output", PQV_Output)
272  .Case("bind-root", PQV_BindRoot)
273  .Case("print-matcher", PQV_PrintMatcher)
274  .Default(PQV_Invalid);
275  if (VarStr.empty())
276  return new InvalidQuery("expected variable name");
277  if (Var == PQV_Invalid)
278  return new InvalidQuery("unknown variable: '" + VarStr + "'");
279 
280  QueryRef Q;
281  switch (Var) {
282  case PQV_Output:
283  Q = parseSetOutputKind<SetExclusiveOutputQuery>();
284  break;
285  case PQV_BindRoot:
286  Q = parseSetBool(&QuerySession::BindRoot);
287  break;
288  case PQV_PrintMatcher:
289  Q = parseSetBool(&QuerySession::PrintMatcher);
290  break;
291  case PQV_Invalid:
292  llvm_unreachable("Invalid query kind");
293  }
294 
295  return endQuery(Q);
296  }
297  case PQK_Enable:
298  case PQK_Disable: {
299  StringRef VarStr;
300  ParsedQueryVariable Var =
301  LexOrCompleteWord<ParsedQueryVariable>(this, VarStr)
302  .Case("output", PQV_Output)
303  .Default(PQV_Invalid);
304  if (VarStr.empty())
305  return new InvalidQuery("expected variable name");
306  if (Var == PQV_Invalid)
307  return new InvalidQuery("unknown variable: '" + VarStr + "'");
308 
309  QueryRef Q;
310 
311  if (QKind == PQK_Enable)
312  Q = parseSetOutputKind<EnableOutputQuery>();
313  else if (QKind == PQK_Disable)
314  Q = parseSetOutputKind<DisableOutputQuery>();
315  else
316  llvm_unreachable("Invalid query kind");
317  return endQuery(Q);
318  }
319 
320  case PQK_Unlet: {
321  StringRef Name = lexWord();
322 
323  if (Name.empty())
324  return new InvalidQuery("expected variable name");
325 
326  return endQuery(new LetQuery(Name, VariantValue()));
327  }
328 
329  case PQK_Invalid:
330  return new InvalidQuery("unknown command: " + CommandStr);
331  }
332 
333  llvm_unreachable("Invalid query kind");
334 }
335 
336 QueryRef QueryParser::parse(StringRef Line, const QuerySession &QS) {
337  return QueryParser(Line, QS).doParse();
338 }
339 
340 std::vector<LineEditor::Completion>
341 QueryParser::complete(StringRef Line, size_t Pos, const QuerySession &QS) {
342  QueryParser P(Line, QS);
343  P.CompletionPos = Line.data() + Pos;
344 
345  P.doParse();
346  return P.Completions;
347 }
348 
349 } // namespace query
350 } // namespace clang
Some operations such as code completion produce a set of candidates.
No-op query (i.e. a blank line).
Definition: Query.h:64
Represents the state for a particular clang-query session.
Definition: QuerySession.h:23
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:54
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:51
Query for "match MATCHER".
Definition: Query.h:88
static constexpr llvm::StringLiteral Name
Position Pos
Definition: SourceCode.cpp:772
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Query for "quit".
Definition: Query.h:80
LexOrCompleteWord & Case(llvm::StringLiteral CaseStr, const T &Value, bool IsCompletion=true)
Definition: QueryParser.cpp:76
Query for "set VAR VALUE".
Definition: Query.h:123
const Expr * E
LexOrCompleteWord(QueryParser *P, StringRef &OutWord)
Definition: QueryParser.cpp:64
Query for "help".
Definition: Query.h:72
std::string Word