blob: 3e83bbc92b22759acd12f5cbe5522320e6c05688 [file] [log] [blame]
Peter Collingbourne8b1265b2013-11-08 00:08:231//===---- Query.cpp - clang-query query -----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Query.h"
11#include "QuerySession.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Frontend/ASTUnit.h"
14#include "clang/Frontend/TextDiagnostic.h"
15#include "llvm/Support/raw_ostream.h"
16
17using namespace clang::ast_matchers;
18using namespace clang::ast_matchers::dynamic;
19
20namespace clang {
21namespace query {
22
David Blaikiee04a3da2015-10-20 21:45:5223Query::~Query() {}
Peter Collingbourne8b1265b2013-11-08 00:08:2324
25bool InvalidQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
26 OS << ErrStr << "\n";
27 return false;
28}
29
30bool NoOpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
31 return true;
32}
33
34bool HelpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
35 OS << "Available commands:\n\n"
Samuel Benzaquene39269e2015-02-27 17:53:2336 " match MATCHER, m MATCHER "
Peter Collingbourne8b1265b2013-11-08 00:08:2337 "Match the loaded ASTs against the given matcher.\n"
Samuel Benzaquene39269e2015-02-27 17:53:2338 " let NAME MATCHER, l NAME MATCHER "
39 "Give a matcher expression a name, to be used later\n"
40 " "
41 "as part of other expressions.\n"
42 " set bind-root (true|false) "
Peter Collingbourne8b1265b2013-11-08 00:08:2343 "Set whether to bind the root matcher to \"root\".\n"
Stephen Kelly4a5b01d2018-10-20 09:13:5944 " set print-matcher (true|false) "
45 "Set whether to print the current matcher,\n"
Stephen Kelly4c3d7a92018-10-24 20:33:1446 " set output <feature> "
47 "Set whether to output only <feature> content.\n"
Stephen Kelly42668a42018-10-03 07:52:4448 " quit, q "
Stephen Kelly4c3d7a92018-10-24 20:33:1449 "Terminates the query session.\n\n"
50 "Several commands accept a <feature> parameter. The available features "
51 "are:\n\n"
52 " print "
53 "Pretty-print bound nodes.\n"
54 " diag "
55 "Diagnostic location for bound nodes.\n"
56 " dump "
57 "Detailed AST output for bound nodes.\n\n";
Aaron Ballman58907172015-08-06 11:56:5758 return true;
59}
60
61bool QuitQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
62 QS.Terminate = true;
Peter Collingbourne8b1265b2013-11-08 00:08:2363 return true;
64}
65
66namespace {
67
68struct CollectBoundNodes : MatchFinder::MatchCallback {
69 std::vector<BoundNodes> &Bindings;
70 CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {}
Alexander Kornienko87638f62015-04-11 07:59:3371 void run(const MatchFinder::MatchResult &Result) override {
Peter Collingbourne8b1265b2013-11-08 00:08:2372 Bindings.push_back(Result.Nodes);
73 }
74};
75
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:1976} // namespace
Peter Collingbourne8b1265b2013-11-08 00:08:2377
78bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
79 unsigned MatchCount = 0;
80
David Blaikie35013fa2014-04-25 15:21:4381 for (auto &AST : QS.ASTs) {
Peter Collingbourne8b1265b2013-11-08 00:08:2382 MatchFinder Finder;
83 std::vector<BoundNodes> Matches;
84 DynTypedMatcher MaybeBoundMatcher = Matcher;
85 if (QS.BindRoot) {
86 llvm::Optional<DynTypedMatcher> M = Matcher.tryBind("root");
87 if (M)
88 MaybeBoundMatcher = *M;
89 }
90 CollectBoundNodes Collect(Matches);
91 if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) {
92 OS << "Not a valid top-level matcher.\n";
93 return false;
94 }
95 Finder.matchAST(AST->getASTContext());
96
Stephen Kelly4a5b01d2018-10-20 09:13:5997 if (QS.PrintMatcher) {
98 std::string prefixText = "Matcher: ";
99 OS << "\n " << prefixText << Source << "\n";
100 OS << " " << std::string(prefixText.size() + Source.size(), '=') << '\n';
101 }
102
Piotr Padlewski08124b12016-12-14 15:29:23103 for (auto MI = Matches.begin(), ME = Matches.end(); MI != ME; ++MI) {
Peter Collingbourne8b1265b2013-11-08 00:08:23104 OS << "\nMatch #" << ++MatchCount << ":\n\n";
105
Piotr Padlewski08124b12016-12-14 15:29:23106 for (auto BI = MI->getMap().begin(), BE = MI->getMap().end(); BI != BE;
107 ++BI) {
Peter Collingbourne8b1265b2013-11-08 00:08:23108 switch (QS.OutKind) {
109 case OK_Diag: {
110 clang::SourceRange R = BI->second.getSourceRange();
111 if (R.isValid()) {
112 TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(),
113 &AST->getDiagnostics().getDiagnosticOptions());
Peter Smithd34a65d2017-06-27 10:04:04114 TD.emitDiagnostic(
115 FullSourceLoc(R.getBegin(), AST->getSourceManager()),
116 DiagnosticsEngine::Note, "\"" + BI->first + "\" binds here",
117 CharSourceRange::getTokenRange(R), None);
Peter Collingbourne8b1265b2013-11-08 00:08:23118 }
119 break;
120 }
121 case OK_Print: {
122 OS << "Binding for \"" << BI->first << "\":\n";
123 BI->second.print(OS, AST->getASTContext().getPrintingPolicy());
124 OS << "\n";
125 break;
126 }
127 case OK_Dump: {
128 OS << "Binding for \"" << BI->first << "\":\n";
129 BI->second.dump(OS, AST->getSourceManager());
130 OS << "\n";
131 break;
132 }
133 }
134 }
135
136 if (MI->getMap().empty())
137 OS << "No bindings.\n";
138 }
139 }
140
141 OS << MatchCount << (MatchCount == 1 ? " match.\n" : " matches.\n");
142 return true;
143}
144
Samuel Benzaquen1f6066c2014-04-23 14:04:52145bool LetQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
146 if (Value) {
147 QS.NamedValues[Name] = Value;
148 } else {
149 QS.NamedValues.erase(Name);
150 }
151 return true;
152}
153
Peter Collingbournec7ae6102013-11-08 08:54:53154#ifndef _MSC_VER
155const QueryKind SetQueryKind<bool>::value;
156const QueryKind SetQueryKind<OutputKind>::value;
157#endif
158
Peter Collingbourne8b1265b2013-11-08 00:08:23159} // namespace query
160} // namespace clang