1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
//===---- Query.cpp - clang-query query -----------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "Query.h"
#include "QuerySession.h"
#include "clang/AST/ASTDumper.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/TextDiagnostic.h"
#include "clang/Tooling/NodeIntrospection.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang::ast_matchers;
using namespace clang::ast_matchers::dynamic;
namespace clang {
namespace query {
Query::~Query() {}
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"
" match MATCHER, m MATCHER "
"Match the loaded ASTs against the given matcher.\n"
" 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) "
"Set whether to bind the root matcher to \"root\".\n"
" set print-matcher (true|false) "
"Set whether to print the current matcher,\n"
" set traversal <kind> "
"Set traversal kind of clang-query session. Available kinds are:\n"
" AsIs "
"Print and match the AST as clang sees it. This mode is the "
"default.\n"
" IgnoreUnlessSpelledInSource "
"Omit AST nodes unless spelled in the source.\n"
" set output <feature> "
"Set whether to output only <feature> content.\n"
" enable output <feature> "
"Enable <feature> content non-exclusively.\n"
" disable output <feature> "
"Disable <feature> content non-exclusively.\n"
" quit, q "
"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"
" detailed-ast "
"Detailed AST output for bound nodes.\n"
" srcloc "
"Source locations and ranges for bound nodes.\n"
" dump "
"Detailed AST output for bound nodes (alias of detailed-ast).\n\n";
return true;
}
bool QuitQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
QS.Terminate = true;
return true;
}
namespace {
struct CollectBoundNodes : MatchFinder::MatchCallback {
std::vector<BoundNodes> &Bindings;
CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {}
void run(const MatchFinder::MatchResult &Result) override {
Bindings.push_back(Result.Nodes);
}
};
void dumpLocations(llvm::raw_ostream &OS, DynTypedNode Node, ASTContext &Ctx,
const DiagnosticsEngine &Diags, SourceManager const &SM) {
auto Locs = clang::tooling::NodeIntrospection::GetLocations(Node);
auto PrintLocations = [](llvm::raw_ostream &OS, auto Iter, auto End) {
auto CommonEntry = Iter->first;
auto Scout = Iter;
SmallVector<std::string> LocationStrings;
while (Scout->first == CommonEntry) {
LocationStrings.push_back(
tooling::LocationCallFormatterCpp::format(*Iter->second));
if (Scout == End)
break;
++Scout;
if (Scout->first == CommonEntry)
++Iter;
}
llvm::sort(LocationStrings);
for (auto &LS : LocationStrings) {
OS << " * \"" << LS << "\"\n";
}
return Iter;
};
TextDiagnostic TD(OS, Ctx.getLangOpts(), &Diags.getDiagnosticOptions());
for (auto Iter = Locs.LocationAccessors.begin();
Iter != Locs.LocationAccessors.end(); ++Iter) {
if (!Iter->first.isValid())
continue;
TD.emitDiagnostic(FullSourceLoc(Iter->first, SM), DiagnosticsEngine::Note,
"source locations here", None, None);
Iter = PrintLocations(OS, Iter, Locs.LocationAccessors.end());
OS << '\n';
}
for (auto Iter = Locs.RangeAccessors.begin();
Iter != Locs.RangeAccessors.end(); ++Iter) {
if (!Iter->first.getBegin().isValid())
continue;
if (SM.getPresumedLineNumber(Iter->first.getBegin()) !=
SM.getPresumedLineNumber(Iter->first.getEnd()))
continue;
TD.emitDiagnostic(FullSourceLoc(Iter->first.getBegin(), SM),
DiagnosticsEngine::Note,
"source ranges here " + Iter->first.printToString(SM),
CharSourceRange::getTokenRange(Iter->first), None);
Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end());
}
for (auto Iter = Locs.RangeAccessors.begin();
Iter != Locs.RangeAccessors.end(); ++Iter) {
if (!Iter->first.getBegin().isValid())
continue;
if (SM.getPresumedLineNumber(Iter->first.getBegin()) ==
SM.getPresumedLineNumber(Iter->first.getEnd()))
continue;
TD.emitDiagnostic(
FullSourceLoc(Iter->first.getBegin(), SM), DiagnosticsEngine::Note,
"source range " + Iter->first.printToString(SM) + " starting here...",
CharSourceRange::getTokenRange(Iter->first), None);
auto ColNum = SM.getPresumedColumnNumber(Iter->first.getEnd());
auto LastLineLoc = Iter->first.getEnd().getLocWithOffset(-(ColNum - 1));
TD.emitDiagnostic(FullSourceLoc(Iter->first.getEnd(), SM),
DiagnosticsEngine::Note, "... ending here",
CharSourceRange::getTokenRange(
SourceRange(LastLineLoc, Iter->first.getEnd())),
None);
Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end());
}
OS << "\n";
}
} // namespace
bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
unsigned MatchCount = 0;
for (auto &AST : QS.ASTs) {
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;
}
auto &Ctx = AST->getASTContext();
const auto &SM = Ctx.getSourceManager();
Ctx.getParentMapContext().setTraversalKind(QS.TK);
Finder.matchAST(Ctx);
if (QS.PrintMatcher) {
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";
}
for (auto MI = Matches.begin(), ME = Matches.end(); MI != ME; ++MI) {
OS << "\nMatch #" << ++MatchCount << ":\n\n";
for (auto BI = MI->getMap().begin(), BE = MI->getMap().end(); BI != BE;
++BI) {
if (QS.DiagOutput) {
clang::SourceRange R = BI->second.getSourceRange();
if (R.isValid()) {
TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(),
&AST->getDiagnostics().getDiagnosticOptions());
TD.emitDiagnostic(
FullSourceLoc(R.getBegin(), AST->getSourceManager()),
DiagnosticsEngine::Note, "\"" + BI->first + "\" binds here",
CharSourceRange::getTokenRange(R), None);
}
}
if (QS.PrintOutput) {
OS << "Binding for \"" << BI->first << "\":\n";
BI->second.print(OS, AST->getASTContext().getPrintingPolicy());
OS << "\n";
}
if (QS.DetailedASTOutput) {
OS << "Binding for \"" << BI->first << "\":\n";
const ASTContext &Ctx = AST->getASTContext();
ASTDumper Dumper(OS, Ctx, AST->getDiagnostics().getShowColors());
Dumper.SetTraversalKind(QS.TK);
Dumper.Visit(BI->second);
OS << "\n";
}
if (QS.SrcLocOutput) {
OS << "\n \"" << BI->first << "\" Source locations\n";
OS << " " << std::string(19 + BI->first.size(), '-') << '\n';
dumpLocations(OS, BI->second, Ctx, AST->getDiagnostics(), SM);
OS << "\n";
}
}
if (MI->getMap().empty())
OS << "No bindings.\n";
}
}
OS << MatchCount << (MatchCount == 1 ? " match.\n" : " matches.\n");
return true;
}
bool LetQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
if (Value) {
QS.NamedValues[Name] = Value;
} else {
QS.NamedValues.erase(Name);
}
return true;
}
#ifndef _MSC_VER
const QueryKind SetQueryKind<bool>::value;
const QueryKind SetQueryKind<OutputKind>::value;
#endif
} // namespace query
} // namespace clang
|