2013-11-08 08:08:23 +08:00
|
|
|
//===---- QueryParser.cpp - clang-query command parser --------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-11-08 08:08:23 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "QueryParser.h"
|
|
|
|
#include "Query.h"
|
|
|
|
#include "QuerySession.h"
|
|
|
|
#include "clang/ASTMatchers/Dynamic/Parser.h"
|
|
|
|
#include "clang/Basic/CharInfo.h"
|
2014-01-08 04:05:01 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2014-02-01 09:42:46 +08:00
|
|
|
#include <set>
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang::ast_matchers::dynamic;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace query {
|
|
|
|
|
|
|
|
// Lex any amount of whitespace followed by a "word" (any sequence of
|
|
|
|
// non-whitespace characters) from the start of region [Begin,End). If no word
|
|
|
|
// is found before End, return StringRef(). Begin is adjusted to exclude the
|
|
|
|
// lexed region.
|
2014-02-01 09:42:46 +08:00
|
|
|
StringRef QueryParser::lexWord() {
|
2019-12-19 06:35:46 +08:00
|
|
|
Line = Line.drop_while([](char c) {
|
|
|
|
// Don't trim newlines.
|
|
|
|
return StringRef(" \t\v\f\r").contains(c);
|
|
|
|
});
|
2013-11-08 08:08:23 +08:00
|
|
|
|
2019-01-09 06:27:08 +08:00
|
|
|
if (Line.empty())
|
|
|
|
// Even though the Line is empty, it contains a pointer and
|
|
|
|
// a (zero) length. The pointer is used in the LexOrCompleteWord
|
|
|
|
// code completion.
|
|
|
|
return Line;
|
2013-11-08 08:08:23 +08:00
|
|
|
|
2019-12-19 06:35:46 +08:00
|
|
|
StringRef Word;
|
|
|
|
if (Line.front() == '#')
|
|
|
|
Word = Line.substr(0, 1);
|
|
|
|
else
|
|
|
|
Word = Line.take_until(isWhitespace);
|
2018-10-03 16:21:54 +08:00
|
|
|
|
2019-01-09 06:27:08 +08:00
|
|
|
Line = Line.drop_front(Word.size());
|
|
|
|
return Word;
|
2013-11-08 08:08:23 +08:00
|
|
|
}
|
|
|
|
|
2014-02-01 09:42:46 +08:00
|
|
|
// This is the StringSwitch-alike used by lexOrCompleteWord below. See that
|
|
|
|
// function for details.
|
|
|
|
template <typename T> struct QueryParser::LexOrCompleteWord {
|
2018-02-27 04:21:30 +08:00
|
|
|
StringRef Word;
|
2014-02-01 09:42:46 +08:00
|
|
|
StringSwitch<T> Switch;
|
|
|
|
|
|
|
|
QueryParser *P;
|
|
|
|
// Set to the completion point offset in Word, or StringRef::npos if
|
|
|
|
// completion point not in Word.
|
|
|
|
size_t WordCompletionPos;
|
|
|
|
|
2018-02-27 04:21:30 +08:00
|
|
|
// Lexes a word and stores it in Word. Returns a LexOrCompleteWord<T> object
|
|
|
|
// that can be used like a llvm::StringSwitch<T>, but adds cases as possible
|
|
|
|
// completions if the lexed word contains the completion point.
|
|
|
|
LexOrCompleteWord(QueryParser *P, StringRef &OutWord)
|
|
|
|
: Word(P->lexWord()), Switch(Word), P(P),
|
|
|
|
WordCompletionPos(StringRef::npos) {
|
|
|
|
OutWord = Word;
|
|
|
|
if (P->CompletionPos && P->CompletionPos <= Word.data() + Word.size()) {
|
|
|
|
if (P->CompletionPos < Word.data())
|
|
|
|
WordCompletionPos = 0;
|
|
|
|
else
|
|
|
|
WordCompletionPos = P->CompletionPos - Word.data();
|
|
|
|
}
|
|
|
|
}
|
2014-02-01 09:42:46 +08:00
|
|
|
|
2018-02-27 04:21:30 +08:00
|
|
|
LexOrCompleteWord &Case(llvm::StringLiteral CaseStr, const T &Value,
|
2014-02-01 09:42:46 +08:00
|
|
|
bool IsCompletion = true) {
|
|
|
|
|
|
|
|
if (WordCompletionPos == StringRef::npos)
|
2018-02-27 04:21:30 +08:00
|
|
|
Switch.Case(CaseStr, Value);
|
|
|
|
else if (CaseStr.size() != 0 && IsCompletion && WordCompletionPos <= CaseStr.size() &&
|
2014-02-01 09:42:46 +08:00
|
|
|
CaseStr.substr(0, WordCompletionPos) ==
|
|
|
|
Word.substr(0, WordCompletionPos))
|
|
|
|
P->Completions.push_back(LineEditor::Completion(
|
|
|
|
(CaseStr.substr(WordCompletionPos) + " ").str(), CaseStr));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-02-27 04:21:30 +08:00
|
|
|
T Default(T Value) { return Switch.Default(Value); }
|
2014-02-01 09:42:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
QueryRef QueryParser::parseSetBool(bool QuerySession::*Var) {
|
|
|
|
StringRef ValStr;
|
2018-02-27 04:21:30 +08:00
|
|
|
unsigned Value = LexOrCompleteWord<unsigned>(this, ValStr)
|
2016-11-08 15:50:19 +08:00
|
|
|
.Case("false", 0)
|
|
|
|
.Case("true", 1)
|
|
|
|
.Default(~0u);
|
2013-11-08 08:08:23 +08:00
|
|
|
if (Value == ~0u) {
|
|
|
|
return new InvalidQuery("expected 'true' or 'false', got '" + ValStr + "'");
|
|
|
|
}
|
|
|
|
return new SetQuery<bool>(Var, Value);
|
|
|
|
}
|
|
|
|
|
2018-10-30 02:59:56 +08:00
|
|
|
template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() {
|
2014-02-01 09:42:46 +08:00
|
|
|
StringRef ValStr;
|
2018-02-27 04:21:30 +08:00
|
|
|
unsigned OutKind = LexOrCompleteWord<unsigned>(this, ValStr)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Case("diag", OK_Diag)
|
|
|
|
.Case("print", OK_Print)
|
2018-10-25 04:33:45 +08:00
|
|
|
.Case("detailed-ast", OK_DetailedAST)
|
|
|
|
.Case("dump", OK_DetailedAST)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Default(~0u);
|
|
|
|
if (OutKind == ~0u) {
|
2018-10-25 04:33:45 +08:00
|
|
|
return new InvalidQuery(
|
|
|
|
"expected 'diag', 'print', 'detailed-ast' or 'dump', got '" + ValStr +
|
|
|
|
"'");
|
2013-11-08 08:08:23 +08:00
|
|
|
}
|
2018-10-25 04:33:55 +08:00
|
|
|
|
|
|
|
switch (OutKind) {
|
|
|
|
case OK_DetailedAST:
|
2018-10-30 02:59:56 +08:00
|
|
|
return new QueryType(&QuerySession::DetailedASTOutput);
|
2018-10-25 04:33:55 +08:00
|
|
|
case OK_Diag:
|
2018-10-30 02:59:56 +08:00
|
|
|
return new QueryType(&QuerySession::DiagOutput);
|
2018-10-25 04:33:55 +08:00
|
|
|
case OK_Print:
|
2018-10-30 02:59:56 +08:00
|
|
|
return new QueryType(&QuerySession::PrintOutput);
|
2018-10-25 04:33:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid output kind");
|
2013-11-08 08:08:23 +08:00
|
|
|
}
|
|
|
|
|
2014-02-01 09:42:46 +08:00
|
|
|
QueryRef QueryParser::endQuery(QueryRef Q) {
|
2019-12-19 06:35:46 +08:00
|
|
|
StringRef Extra = Line;
|
|
|
|
StringRef ExtraTrimmed = Extra.drop_while(
|
|
|
|
[](char c) { return StringRef(" \t\v\f\r").contains(c); });
|
|
|
|
|
|
|
|
if ((!ExtraTrimmed.empty() && ExtraTrimmed[0] == '\n') ||
|
|
|
|
(ExtraTrimmed.size() >= 2 && ExtraTrimmed[0] == '\r' &&
|
|
|
|
ExtraTrimmed[1] == '\n'))
|
|
|
|
Q->RemainingContent = Extra;
|
|
|
|
else {
|
|
|
|
StringRef TrailingWord = lexWord();
|
|
|
|
if (!TrailingWord.empty() && TrailingWord.front() == '#') {
|
|
|
|
Line = Line.drop_until([](char c) { return c == '\n'; });
|
|
|
|
Line = Line.drop_while([](char c) { return c == '\n'; });
|
|
|
|
return endQuery(Q);
|
|
|
|
}
|
|
|
|
if (!TrailingWord.empty()) {
|
|
|
|
return new InvalidQuery("unexpected extra input: '" + Extra + "'");
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 08:08:23 +08:00
|
|
|
return Q;
|
|
|
|
}
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
enum ParsedQueryKind {
|
|
|
|
PQK_Invalid,
|
2018-10-03 16:21:54 +08:00
|
|
|
PQK_Comment,
|
2013-11-08 08:08:23 +08:00
|
|
|
PQK_NoOp,
|
|
|
|
PQK_Help,
|
2014-04-23 22:04:52 +08:00
|
|
|
PQK_Let,
|
2013-11-08 08:08:23 +08:00
|
|
|
PQK_Match,
|
2014-04-23 22:04:52 +08:00
|
|
|
PQK_Set,
|
|
|
|
PQK_Unlet,
|
2018-10-30 02:59:56 +08:00
|
|
|
PQK_Quit,
|
|
|
|
PQK_Enable,
|
|
|
|
PQK_Disable
|
2013-11-08 08:08:23 +08:00
|
|
|
};
|
|
|
|
|
2018-10-20 17:13:59 +08:00
|
|
|
enum ParsedQueryVariable {
|
|
|
|
PQV_Invalid,
|
|
|
|
PQV_Output,
|
|
|
|
PQV_BindRoot,
|
|
|
|
PQV_PrintMatcher
|
|
|
|
};
|
2013-11-08 08:08:23 +08:00
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
QueryRef makeInvalidQueryFromDiagnostics(const Diagnostics &Diag) {
|
|
|
|
std::string ErrStr;
|
|
|
|
llvm::raw_string_ostream OS(ErrStr);
|
|
|
|
Diag.printToStreamFull(OS);
|
|
|
|
return new InvalidQuery(OS.str());
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
} // namespace
|
2014-04-23 22:04:52 +08:00
|
|
|
|
|
|
|
QueryRef QueryParser::completeMatcherExpression() {
|
|
|
|
std::vector<MatcherCompletion> Comps = Parser::completeExpression(
|
2019-01-09 06:27:08 +08:00
|
|
|
Line, CompletionPos - Line.begin(), nullptr, &QS.NamedValues);
|
2016-12-14 23:29:23 +08:00
|
|
|
for (auto I = Comps.begin(), E = Comps.end(); I != E; ++I) {
|
2014-04-23 22:04:52 +08:00
|
|
|
Completions.push_back(LineEditor::Completion(I->TypedText, I->MatcherDecl));
|
|
|
|
}
|
|
|
|
return QueryRef();
|
|
|
|
}
|
|
|
|
|
2014-02-01 09:42:46 +08:00
|
|
|
QueryRef QueryParser::doParse() {
|
|
|
|
StringRef CommandStr;
|
2018-02-27 04:21:30 +08:00
|
|
|
ParsedQueryKind QKind = LexOrCompleteWord<ParsedQueryKind>(this, CommandStr)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Case("", PQK_NoOp)
|
2018-10-03 16:21:54 +08:00
|
|
|
.Case("#", PQK_Comment, /*IsCompletion=*/false)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Case("help", PQK_Help)
|
2018-10-02 04:34:21 +08:00
|
|
|
.Case("l", PQK_Let, /*IsCompletion=*/false)
|
2014-04-23 22:04:52 +08:00
|
|
|
.Case("let", PQK_Let)
|
2018-10-02 04:34:15 +08:00
|
|
|
.Case("m", PQK_Match, /*IsCompletion=*/false)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Case("match", PQK_Match)
|
2018-10-03 15:52:44 +08:00
|
|
|
.Case("q", PQK_Quit, /*IsCompletion=*/false)
|
2018-10-02 04:34:15 +08:00
|
|
|
.Case("quit", PQK_Quit)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Case("set", PQK_Set)
|
2018-10-30 02:59:56 +08:00
|
|
|
.Case("enable", PQK_Enable)
|
|
|
|
.Case("disable", PQK_Disable)
|
2014-04-23 22:04:52 +08:00
|
|
|
.Case("unlet", PQK_Unlet)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Default(PQK_Invalid);
|
|
|
|
|
|
|
|
switch (QKind) {
|
2018-10-03 16:21:54 +08:00
|
|
|
case PQK_Comment:
|
2013-11-08 08:08:23 +08:00
|
|
|
case PQK_NoOp:
|
2019-12-19 06:35:46 +08:00
|
|
|
Line = Line.drop_until([](char c) { return c == '\n'; });
|
|
|
|
Line = Line.drop_while([](char c) { return c == '\n'; });
|
|
|
|
if (Line.empty())
|
|
|
|
return new NoOpQuery;
|
|
|
|
return doParse();
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
case PQK_Help:
|
2014-02-01 09:42:46 +08:00
|
|
|
return endQuery(new HelpQuery);
|
2013-11-08 08:08:23 +08:00
|
|
|
|
2015-08-06 19:56:57 +08:00
|
|
|
case PQK_Quit:
|
|
|
|
return endQuery(new QuitQuery);
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
case PQK_Let: {
|
|
|
|
StringRef Name = lexWord();
|
|
|
|
|
|
|
|
if (Name.empty())
|
|
|
|
return new InvalidQuery("expected variable name");
|
|
|
|
|
|
|
|
if (CompletionPos)
|
|
|
|
return completeMatcherExpression();
|
|
|
|
|
|
|
|
Diagnostics Diag;
|
|
|
|
ast_matchers::dynamic::VariantValue Value;
|
2019-01-09 06:27:08 +08:00
|
|
|
if (!Parser::parseExpression(Line, nullptr, &QS.NamedValues, &Value,
|
|
|
|
&Diag)) {
|
2014-04-23 22:04:52 +08:00
|
|
|
return makeInvalidQueryFromDiagnostics(Diag);
|
|
|
|
}
|
|
|
|
|
2019-12-19 06:35:46 +08:00
|
|
|
auto *Q = new LetQuery(Name, Value);
|
|
|
|
Q->RemainingContent = Line;
|
|
|
|
return Q;
|
2014-04-23 22:04:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
case PQK_Match: {
|
2014-04-23 22:04:52 +08:00
|
|
|
if (CompletionPos)
|
|
|
|
return completeMatcherExpression();
|
|
|
|
|
|
|
|
Diagnostics Diag;
|
2019-12-29 22:48:47 +08:00
|
|
|
auto MatcherSource = Line.ltrim();
|
2019-12-19 06:35:46 +08:00
|
|
|
auto OrigMatcherSource = MatcherSource;
|
2014-04-23 22:04:52 +08:00
|
|
|
Optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression(
|
2018-10-20 17:13:59 +08:00
|
|
|
MatcherSource, nullptr, &QS.NamedValues, &Diag);
|
2014-04-23 22:04:52 +08:00
|
|
|
if (!Matcher) {
|
|
|
|
return makeInvalidQueryFromDiagnostics(Diag);
|
2013-11-08 08:08:23 +08:00
|
|
|
}
|
2019-12-19 06:35:46 +08:00
|
|
|
auto ActualSource = OrigMatcherSource.slice(0, OrigMatcherSource.size() -
|
|
|
|
MatcherSource.size());
|
|
|
|
auto *Q = new MatchQuery(ActualSource, *Matcher);
|
|
|
|
Q->RemainingContent = MatcherSource;
|
|
|
|
return Q;
|
2013-11-08 08:08:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case PQK_Set: {
|
2014-02-01 09:42:46 +08:00
|
|
|
StringRef VarStr;
|
2018-10-20 17:13:59 +08:00
|
|
|
ParsedQueryVariable Var =
|
|
|
|
LexOrCompleteWord<ParsedQueryVariable>(this, VarStr)
|
|
|
|
.Case("output", PQV_Output)
|
|
|
|
.Case("bind-root", PQV_BindRoot)
|
|
|
|
.Case("print-matcher", PQV_PrintMatcher)
|
|
|
|
.Default(PQV_Invalid);
|
2013-11-08 08:08:23 +08:00
|
|
|
if (VarStr.empty())
|
|
|
|
return new InvalidQuery("expected variable name");
|
|
|
|
if (Var == PQV_Invalid)
|
|
|
|
return new InvalidQuery("unknown variable: '" + VarStr + "'");
|
|
|
|
|
|
|
|
QueryRef Q;
|
|
|
|
switch (Var) {
|
|
|
|
case PQV_Output:
|
2018-10-30 02:59:56 +08:00
|
|
|
Q = parseSetOutputKind<SetExclusiveOutputQuery>();
|
2013-11-08 08:08:23 +08:00
|
|
|
break;
|
|
|
|
case PQV_BindRoot:
|
2014-02-01 09:42:46 +08:00
|
|
|
Q = parseSetBool(&QuerySession::BindRoot);
|
2013-11-08 08:08:23 +08:00
|
|
|
break;
|
2018-10-20 17:13:59 +08:00
|
|
|
case PQV_PrintMatcher:
|
|
|
|
Q = parseSetBool(&QuerySession::PrintMatcher);
|
|
|
|
break;
|
2013-11-08 08:08:23 +08:00
|
|
|
case PQV_Invalid:
|
|
|
|
llvm_unreachable("Invalid query kind");
|
|
|
|
}
|
|
|
|
|
2014-02-01 09:42:46 +08:00
|
|
|
return endQuery(Q);
|
2013-11-08 08:08:23 +08:00
|
|
|
}
|
2018-10-30 02:59:56 +08:00
|
|
|
case PQK_Enable:
|
|
|
|
case PQK_Disable: {
|
|
|
|
StringRef VarStr;
|
|
|
|
ParsedQueryVariable Var =
|
|
|
|
LexOrCompleteWord<ParsedQueryVariable>(this, VarStr)
|
|
|
|
.Case("output", PQV_Output)
|
|
|
|
.Default(PQV_Invalid);
|
|
|
|
if (VarStr.empty())
|
|
|
|
return new InvalidQuery("expected variable name");
|
|
|
|
if (Var == PQV_Invalid)
|
|
|
|
return new InvalidQuery("unknown variable: '" + VarStr + "'");
|
|
|
|
|
|
|
|
QueryRef Q;
|
|
|
|
|
|
|
|
if (QKind == PQK_Enable)
|
|
|
|
Q = parseSetOutputKind<EnableOutputQuery>();
|
|
|
|
else if (QKind == PQK_Disable)
|
|
|
|
Q = parseSetOutputKind<DisableOutputQuery>();
|
|
|
|
else
|
|
|
|
llvm_unreachable("Invalid query kind");
|
|
|
|
return endQuery(Q);
|
|
|
|
}
|
2013-11-08 08:08:23 +08:00
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
case PQK_Unlet: {
|
|
|
|
StringRef Name = lexWord();
|
|
|
|
|
|
|
|
if (Name.empty())
|
|
|
|
return new InvalidQuery("expected variable name");
|
|
|
|
|
2014-04-24 00:01:44 +08:00
|
|
|
return endQuery(new LetQuery(Name, VariantValue()));
|
2014-04-23 22:04:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
case PQK_Invalid:
|
|
|
|
return new InvalidQuery("unknown command: " + CommandStr);
|
|
|
|
}
|
2013-11-08 09:08:12 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Invalid query kind");
|
2013-11-08 08:08:23 +08:00
|
|
|
}
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
QueryRef QueryParser::parse(StringRef Line, const QuerySession &QS) {
|
|
|
|
return QueryParser(Line, QS).doParse();
|
2014-02-01 09:42:46 +08:00
|
|
|
}
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
std::vector<LineEditor::Completion>
|
|
|
|
QueryParser::complete(StringRef Line, size_t Pos, const QuerySession &QS) {
|
|
|
|
QueryParser P(Line, QS);
|
2014-02-01 09:42:46 +08:00
|
|
|
P.CompletionPos = Line.data() + Pos;
|
|
|
|
|
|
|
|
P.doParse();
|
|
|
|
return P.Completions;
|
|
|
|
}
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
} // namespace query
|
|
|
|
} // namespace clang
|