2013-11-08 08:08:23 +08:00
|
|
|
//===---- QueryParser.cpp - clang-query command parser --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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() {
|
2013-11-08 08:08:23 +08:00
|
|
|
while (true) {
|
|
|
|
if (Begin == End)
|
2014-02-01 09:42:46 +08:00
|
|
|
return StringRef(Begin, 0);
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
if (!isWhitespace(*Begin))
|
|
|
|
break;
|
|
|
|
|
|
|
|
++Begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *WordBegin = Begin;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
++Begin;
|
|
|
|
|
|
|
|
if (Begin == End || isWhitespace(*Begin))
|
|
|
|
return StringRef(WordBegin, Begin - WordBegin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
StringSwitch<T> Switch;
|
|
|
|
|
|
|
|
QueryParser *P;
|
|
|
|
StringRef Word;
|
|
|
|
// Set to the completion point offset in Word, or StringRef::npos if
|
|
|
|
// completion point not in Word.
|
|
|
|
size_t WordCompletionPos;
|
|
|
|
|
|
|
|
LexOrCompleteWord(QueryParser *P, StringRef Word, size_t WCP)
|
|
|
|
: Switch(Word), P(P), Word(Word), WordCompletionPos(WCP) {}
|
|
|
|
|
|
|
|
template <unsigned N>
|
|
|
|
LexOrCompleteWord &Case(const char (&S)[N], const T &Value,
|
|
|
|
bool IsCompletion = true) {
|
|
|
|
StringRef CaseStr(S, N - 1);
|
|
|
|
|
|
|
|
if (WordCompletionPos == StringRef::npos)
|
|
|
|
Switch.Case(S, Value);
|
|
|
|
else if (N != 1 && IsCompletion && WordCompletionPos <= CaseStr.size() &&
|
|
|
|
CaseStr.substr(0, WordCompletionPos) ==
|
|
|
|
Word.substr(0, WordCompletionPos))
|
|
|
|
P->Completions.push_back(LineEditor::Completion(
|
|
|
|
(CaseStr.substr(WordCompletionPos) + " ").str(), CaseStr));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
T Default(const T &Value) const { return Switch.Default(Value); }
|
2014-02-01 09:42:46 +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.
|
|
|
|
template <typename T>
|
|
|
|
QueryParser::LexOrCompleteWord<T>
|
|
|
|
QueryParser::lexOrCompleteWord(StringRef &Word) {
|
|
|
|
Word = lexWord();
|
|
|
|
size_t WordCompletionPos = StringRef::npos;
|
|
|
|
if (CompletionPos && CompletionPos <= Word.data() + Word.size()) {
|
|
|
|
if (CompletionPos < Word.data())
|
|
|
|
WordCompletionPos = 0;
|
|
|
|
else
|
|
|
|
WordCompletionPos = CompletionPos - Word.data();
|
|
|
|
}
|
|
|
|
return LexOrCompleteWord<T>(this, Word, WordCompletionPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryRef QueryParser::parseSetBool(bool QuerySession::*Var) {
|
|
|
|
StringRef ValStr;
|
|
|
|
unsigned Value = lexOrCompleteWord<unsigned>(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);
|
|
|
|
}
|
|
|
|
|
2014-02-01 09:42:46 +08:00
|
|
|
QueryRef QueryParser::parseSetOutputKind() {
|
|
|
|
StringRef ValStr;
|
|
|
|
unsigned OutKind = lexOrCompleteWord<unsigned>(ValStr)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Case("diag", OK_Diag)
|
|
|
|
.Case("print", OK_Print)
|
|
|
|
.Case("dump", OK_Dump)
|
|
|
|
.Default(~0u);
|
|
|
|
if (OutKind == ~0u) {
|
|
|
|
return new InvalidQuery("expected 'diag', 'print' or 'dump', got '" +
|
|
|
|
ValStr + "'");
|
|
|
|
}
|
|
|
|
return new SetQuery<OutputKind>(&QuerySession::OutKind, OutputKind(OutKind));
|
|
|
|
}
|
|
|
|
|
2014-02-01 09:42:46 +08:00
|
|
|
QueryRef QueryParser::endQuery(QueryRef Q) {
|
2013-11-08 08:08:23 +08:00
|
|
|
const char *Extra = Begin;
|
2014-02-01 09:42:46 +08:00
|
|
|
if (!lexWord().empty())
|
2013-11-08 08:08:23 +08:00
|
|
|
return new InvalidQuery("unexpected extra input: '" +
|
|
|
|
StringRef(Extra, End - Extra) + "'");
|
|
|
|
return Q;
|
|
|
|
}
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
enum ParsedQueryKind {
|
|
|
|
PQK_Invalid,
|
|
|
|
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,
|
2015-08-06 19:56:57 +08:00
|
|
|
PQK_Quit
|
2013-11-08 08:08:23 +08:00
|
|
|
};
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
enum ParsedQueryVariable { PQV_Invalid, PQV_Output, PQV_BindRoot };
|
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(
|
2014-08-13 05:22:58 +08:00
|
|
|
StringRef(Begin, End - Begin), CompletionPos - 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;
|
|
|
|
ParsedQueryKind QKind = lexOrCompleteWord<ParsedQueryKind>(CommandStr)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Case("", PQK_NoOp)
|
|
|
|
.Case("help", PQK_Help)
|
2014-02-01 09:42:46 +08:00
|
|
|
.Case("m", PQK_Match, /*IsCompletion=*/false)
|
2014-04-23 22:04:52 +08:00
|
|
|
.Case("let", PQK_Let)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Case("match", PQK_Match)
|
|
|
|
.Case("set", PQK_Set)
|
2014-04-23 22:04:52 +08:00
|
|
|
.Case("unlet", PQK_Unlet)
|
2015-08-06 19:56:57 +08:00
|
|
|
.Case("quit", PQK_Quit)
|
2013-11-08 08:08:23 +08:00
|
|
|
.Default(PQK_Invalid);
|
|
|
|
|
|
|
|
switch (QKind) {
|
|
|
|
case PQK_NoOp:
|
|
|
|
return new NoOpQuery;
|
|
|
|
|
|
|
|
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;
|
2014-08-13 05:22:58 +08:00
|
|
|
if (!Parser::parseExpression(StringRef(Begin, End - Begin), nullptr,
|
|
|
|
&QS.NamedValues, &Value, &Diag)) {
|
2014-04-23 22:04:52 +08:00
|
|
|
return makeInvalidQueryFromDiagnostics(Diag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new LetQuery(Name, Value);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
Optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression(
|
2014-08-13 05:22:58 +08:00
|
|
|
StringRef(Begin, End - Begin), 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
|
|
|
}
|
2014-04-23 22:04:52 +08:00
|
|
|
return new MatchQuery(*Matcher);
|
2013-11-08 08:08:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case PQK_Set: {
|
2014-02-01 09:42:46 +08:00
|
|
|
StringRef VarStr;
|
|
|
|
ParsedQueryVariable Var = lexOrCompleteWord<ParsedQueryVariable>(VarStr)
|
|
|
|
.Case("output", PQV_Output)
|
|
|
|
.Case("bind-root", PQV_BindRoot)
|
|
|
|
.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:
|
2014-02-01 09:42:46 +08:00
|
|
|
Q = parseSetOutputKind();
|
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;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|