2013-11-08 08:08:23 +08:00
|
|
|
//===---- Query.cpp - clang-query query -----------------------------------===//
|
|
|
|
//
|
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 "Query.h"
|
|
|
|
#include "QuerySession.h"
|
2019-12-07 07:26:59 +08:00
|
|
|
#include "clang/AST/ASTDumper.h"
|
2013-11-08 08:08:23 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/TextDiagnostic.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
using namespace clang::ast_matchers::dynamic;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace query {
|
|
|
|
|
2015-10-21 05:45:52 +08:00
|
|
|
Query::~Query() {}
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
bool InvalidQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
|
|
|
|
OS << ErrStr << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NoOpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HelpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
|
|
|
|
OS << "Available commands:\n\n"
|
2015-02-28 01:53:23 +08:00
|
|
|
" match MATCHER, m MATCHER "
|
2013-11-08 08:08:23 +08:00
|
|
|
"Match the loaded ASTs against the given matcher.\n"
|
2015-02-28 01:53:23 +08:00
|
|
|
" let NAME MATCHER, l NAME MATCHER "
|
|
|
|
"Give a matcher expression a name, to be used later\n"
|
|
|
|
" "
|
|
|
|
"as part of other expressions.\n"
|
|
|
|
" set bind-root (true|false) "
|
2013-11-08 08:08:23 +08:00
|
|
|
"Set whether to bind the root matcher to \"root\".\n"
|
2018-10-20 17:13:59 +08:00
|
|
|
" set print-matcher (true|false) "
|
|
|
|
"Set whether to print the current matcher,\n"
|
2019-12-30 03:26:11 +08:00
|
|
|
" set traversal <kind> "
|
|
|
|
"Set traversal kind of clang-query session. Available kinds are:\n"
|
|
|
|
" AsIs "
|
2020-07-03 03:46:27 +08:00
|
|
|
"Print and match the AST as clang sees it. This mode is the "
|
|
|
|
"default.\n"
|
2019-12-30 03:26:11 +08:00
|
|
|
" IgnoreImplicitCastsAndParentheses "
|
|
|
|
"Omit implicit casts and parens in matching and dumping.\n"
|
|
|
|
" IgnoreUnlessSpelledInSource "
|
2020-07-03 03:46:27 +08:00
|
|
|
"Omit AST nodes unless spelled in the source.\n"
|
2018-10-25 04:33:14 +08:00
|
|
|
" set output <feature> "
|
|
|
|
"Set whether to output only <feature> content.\n"
|
2018-10-30 02:59:56 +08:00
|
|
|
" enable output <feature> "
|
|
|
|
"Enable <feature> content non-exclusively.\n"
|
|
|
|
" disable output <feature> "
|
|
|
|
"Disable <feature> content non-exclusively.\n"
|
2018-10-03 15:52:44 +08:00
|
|
|
" quit, q "
|
2018-10-25 04:33:14 +08:00
|
|
|
"Terminates the query session.\n\n"
|
|
|
|
"Several commands accept a <feature> parameter. The available features "
|
|
|
|
"are:\n\n"
|
|
|
|
" print "
|
|
|
|
"Pretty-print bound nodes.\n"
|
|
|
|
" diag "
|
|
|
|
"Diagnostic location for bound nodes.\n"
|
2018-10-25 04:33:45 +08:00
|
|
|
" detailed-ast "
|
|
|
|
"Detailed AST output for bound nodes.\n"
|
2018-10-25 04:33:14 +08:00
|
|
|
" dump "
|
2018-10-25 04:33:45 +08:00
|
|
|
"Detailed AST output for bound nodes (alias of detailed-ast).\n\n";
|
2015-08-06 19:56:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QuitQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
|
|
|
|
QS.Terminate = true;
|
2013-11-08 08:08:23 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct CollectBoundNodes : MatchFinder::MatchCallback {
|
|
|
|
std::vector<BoundNodes> &Bindings;
|
|
|
|
CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {}
|
2015-04-11 15:59:33 +08:00
|
|
|
void run(const MatchFinder::MatchResult &Result) override {
|
2013-11-08 08:08:23 +08:00
|
|
|
Bindings.push_back(Result.Nodes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
} // namespace
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
|
|
|
|
unsigned MatchCount = 0;
|
|
|
|
|
2014-04-25 23:21:43 +08:00
|
|
|
for (auto &AST : QS.ASTs) {
|
2013-11-08 08:08:23 +08:00
|
|
|
MatchFinder Finder;
|
|
|
|
std::vector<BoundNodes> Matches;
|
|
|
|
DynTypedMatcher MaybeBoundMatcher = Matcher;
|
|
|
|
if (QS.BindRoot) {
|
|
|
|
llvm::Optional<DynTypedMatcher> M = Matcher.tryBind("root");
|
|
|
|
if (M)
|
|
|
|
MaybeBoundMatcher = *M;
|
|
|
|
}
|
|
|
|
CollectBoundNodes Collect(Matches);
|
|
|
|
if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) {
|
|
|
|
OS << "Not a valid top-level matcher.\n";
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-30 03:26:11 +08:00
|
|
|
|
|
|
|
AST->getASTContext().getParentMapContext().setTraversalKind(QS.TK);
|
2013-11-08 08:08:23 +08:00
|
|
|
Finder.matchAST(AST->getASTContext());
|
|
|
|
|
2018-10-20 17:13:59 +08:00
|
|
|
if (QS.PrintMatcher) {
|
2019-12-19 06:35:46 +08:00
|
|
|
SmallVector<StringRef, 4> Lines;
|
|
|
|
Source.split(Lines, "\n");
|
|
|
|
auto FirstLine = Lines[0];
|
|
|
|
Lines.erase(Lines.begin(), Lines.begin() + 1);
|
|
|
|
while (!Lines.empty() && Lines.back().empty()) {
|
|
|
|
Lines.resize(Lines.size() - 1);
|
|
|
|
}
|
|
|
|
unsigned MaxLength = FirstLine.size();
|
|
|
|
std::string PrefixText = "Matcher: ";
|
|
|
|
OS << "\n " << PrefixText << FirstLine;
|
|
|
|
|
|
|
|
for (auto Line : Lines) {
|
|
|
|
OS << "\n" << std::string(PrefixText.size() + 2, ' ') << Line;
|
|
|
|
MaxLength = std::max<int>(MaxLength, Line.rtrim().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "\n"
|
|
|
|
<< " " << std::string(PrefixText.size() + MaxLength, '=') << "\n\n";
|
2018-10-20 17:13:59 +08:00
|
|
|
}
|
|
|
|
|
2016-12-14 23:29:23 +08:00
|
|
|
for (auto MI = Matches.begin(), ME = Matches.end(); MI != ME; ++MI) {
|
2013-11-08 08:08:23 +08:00
|
|
|
OS << "\nMatch #" << ++MatchCount << ":\n\n";
|
|
|
|
|
2016-12-14 23:29:23 +08:00
|
|
|
for (auto BI = MI->getMap().begin(), BE = MI->getMap().end(); BI != BE;
|
|
|
|
++BI) {
|
2018-10-25 04:33:55 +08:00
|
|
|
if (QS.DiagOutput) {
|
2013-11-08 08:08:23 +08:00
|
|
|
clang::SourceRange R = BI->second.getSourceRange();
|
|
|
|
if (R.isValid()) {
|
|
|
|
TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(),
|
|
|
|
&AST->getDiagnostics().getDiagnosticOptions());
|
2017-06-27 18:04:04 +08:00
|
|
|
TD.emitDiagnostic(
|
|
|
|
FullSourceLoc(R.getBegin(), AST->getSourceManager()),
|
|
|
|
DiagnosticsEngine::Note, "\"" + BI->first + "\" binds here",
|
|
|
|
CharSourceRange::getTokenRange(R), None);
|
2013-11-08 08:08:23 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-25 04:33:55 +08:00
|
|
|
if (QS.PrintOutput) {
|
2013-11-08 08:08:23 +08:00
|
|
|
OS << "Binding for \"" << BI->first << "\":\n";
|
|
|
|
BI->second.print(OS, AST->getASTContext().getPrintingPolicy());
|
|
|
|
OS << "\n";
|
|
|
|
}
|
2018-10-25 04:33:55 +08:00
|
|
|
if (QS.DetailedASTOutput) {
|
2013-11-08 08:08:23 +08:00
|
|
|
OS << "Binding for \"" << BI->first << "\":\n";
|
2019-12-07 07:26:59 +08:00
|
|
|
const ASTContext &Ctx = AST->getASTContext();
|
2021-01-25 21:06:54 +08:00
|
|
|
ASTDumper Dumper(OS, Ctx, AST->getDiagnostics().getShowColors());
|
2019-12-30 03:26:11 +08:00
|
|
|
Dumper.SetTraversalKind(QS.TK);
|
2019-12-07 07:26:59 +08:00
|
|
|
Dumper.Visit(BI->second);
|
2013-11-08 08:08:23 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MI->getMap().empty())
|
|
|
|
OS << "No bindings.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << MatchCount << (MatchCount == 1 ? " match.\n" : " matches.\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
bool LetQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
|
|
|
|
if (Value) {
|
|
|
|
QS.NamedValues[Name] = Value;
|
|
|
|
} else {
|
|
|
|
QS.NamedValues.erase(Name);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-08 16:54:53 +08:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
const QueryKind SetQueryKind<bool>::value;
|
|
|
|
const QueryKind SetQueryKind<OutputKind>::value;
|
|
|
|
#endif
|
|
|
|
|
2013-11-08 08:08:23 +08:00
|
|
|
} // namespace query
|
|
|
|
} // namespace clang
|