blob: 63b4f3579b9e7baba49bbd5d6b8a2a1ab91da8ca [file] [log] [blame]
Peter Collingbourne8b1265b2013-11-08 00:08:23 +00001//===---- 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:52 +000023Query::~Query() {}
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000024
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:23 +000036 " match MATCHER, m MATCHER "
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000037 "Match the loaded ASTs against the given matcher.\n"
Samuel Benzaquene39269e2015-02-27 17:53:23 +000038 " 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:23 +000043 "Set whether to bind the root matcher to \"root\".\n"
Samuel Benzaquene39269e2015-02-27 17:53:23 +000044 " set output (diag|print|dump) "
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000045 "Set whether to print bindings as diagnostics,\n"
Samuel Benzaquene39269e2015-02-27 17:53:23 +000046 " "
Aaron Ballman58907172015-08-06 11:56:57 +000047 "AST pretty prints or AST dumps.\n"
Stephen Kelly42668a42018-10-03 07:52:44 +000048 " quit, q "
Aaron Ballman58907172015-08-06 11:56:57 +000049 "Terminates the query session.\n\n";
50 return true;
51}
52
53bool QuitQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
54 QS.Terminate = true;
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000055 return true;
56}
57
58namespace {
59
60struct CollectBoundNodes : MatchFinder::MatchCallback {
61 std::vector<BoundNodes> &Bindings;
62 CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {}
Alexander Kornienko87638f62015-04-11 07:59:33 +000063 void run(const MatchFinder::MatchResult &Result) override {
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000064 Bindings.push_back(Result.Nodes);
65 }
66};
67
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000068} // namespace
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000069
70bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
71 unsigned MatchCount = 0;
72
David Blaikie35013fa2014-04-25 15:21:43 +000073 for (auto &AST : QS.ASTs) {
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000074 MatchFinder Finder;
75 std::vector<BoundNodes> Matches;
76 DynTypedMatcher MaybeBoundMatcher = Matcher;
77 if (QS.BindRoot) {
78 llvm::Optional<DynTypedMatcher> M = Matcher.tryBind("root");
79 if (M)
80 MaybeBoundMatcher = *M;
81 }
82 CollectBoundNodes Collect(Matches);
83 if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) {
84 OS << "Not a valid top-level matcher.\n";
85 return false;
86 }
87 Finder.matchAST(AST->getASTContext());
88
Piotr Padlewski08124b12016-12-14 15:29:23 +000089 for (auto MI = Matches.begin(), ME = Matches.end(); MI != ME; ++MI) {
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000090 OS << "\nMatch #" << ++MatchCount << ":\n\n";
91
Piotr Padlewski08124b12016-12-14 15:29:23 +000092 for (auto BI = MI->getMap().begin(), BE = MI->getMap().end(); BI != BE;
93 ++BI) {
Peter Collingbourne8b1265b2013-11-08 00:08:23 +000094 switch (QS.OutKind) {
95 case OK_Diag: {
96 clang::SourceRange R = BI->second.getSourceRange();
97 if (R.isValid()) {
98 TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(),
99 &AST->getDiagnostics().getDiagnosticOptions());
Peter Smithd34a65d2017-06-27 10:04:04 +0000100 TD.emitDiagnostic(
101 FullSourceLoc(R.getBegin(), AST->getSourceManager()),
102 DiagnosticsEngine::Note, "\"" + BI->first + "\" binds here",
103 CharSourceRange::getTokenRange(R), None);
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000104 }
105 break;
106 }
107 case OK_Print: {
108 OS << "Binding for \"" << BI->first << "\":\n";
109 BI->second.print(OS, AST->getASTContext().getPrintingPolicy());
110 OS << "\n";
111 break;
112 }
113 case OK_Dump: {
114 OS << "Binding for \"" << BI->first << "\":\n";
115 BI->second.dump(OS, AST->getSourceManager());
116 OS << "\n";
117 break;
118 }
119 }
120 }
121
122 if (MI->getMap().empty())
123 OS << "No bindings.\n";
124 }
125 }
126
127 OS << MatchCount << (MatchCount == 1 ? " match.\n" : " matches.\n");
128 return true;
129}
130
Samuel Benzaquen1f6066c2014-04-23 14:04:52 +0000131bool LetQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
132 if (Value) {
133 QS.NamedValues[Name] = Value;
134 } else {
135 QS.NamedValues.erase(Name);
136 }
137 return true;
138}
139
Peter Collingbournec7ae6102013-11-08 08:54:53 +0000140#ifndef _MSC_VER
141const QueryKind SetQueryKind<bool>::value;
142const QueryKind SetQueryKind<OutputKind>::value;
143#endif
144
Peter Collingbourne8b1265b2013-11-08 00:08:23 +0000145} // namespace query
146} // namespace clang