1 //===---- 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 
17 using namespace clang::ast_matchers;
18 using namespace clang::ast_matchers::dynamic;
19 
20 namespace clang {
21 namespace query {
22 
23 Query::~Query() {}
24 
25 bool InvalidQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
26   OS << ErrStr << "\n";
27   return false;
28 }
29 
30 bool NoOpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
31   return true;
32 }
33 
34 bool HelpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
35   OS << "Available commands:\n\n"
36         "  match MATCHER, m MATCHER          "
37         "Match the loaded ASTs against the given matcher.\n"
38         "  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)        "
43         "Set whether to bind the root matcher to \"root\".\n"
44         "  set output (diag|print|dump)      "
45         "Set whether to print bindings as diagnostics,\n"
46         "                                    "
47         "AST pretty prints or AST dumps.\n\n";
48   return true;
49 }
50 
51 namespace {
52 
53 struct CollectBoundNodes : MatchFinder::MatchCallback {
54   std::vector<BoundNodes> &Bindings;
55   CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {}
56   void run(const MatchFinder::MatchResult &Result) override {
57     Bindings.push_back(Result.Nodes);
58   }
59 };
60 
61 }  // namespace
62 
63 bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
64   unsigned MatchCount = 0;
65 
66   for (auto &AST : QS.ASTs) {
67     MatchFinder Finder;
68     std::vector<BoundNodes> Matches;
69     DynTypedMatcher MaybeBoundMatcher = Matcher;
70     if (QS.BindRoot) {
71       llvm::Optional<DynTypedMatcher> M = Matcher.tryBind("root");
72       if (M)
73         MaybeBoundMatcher = *M;
74     }
75     CollectBoundNodes Collect(Matches);
76     if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) {
77       OS << "Not a valid top-level matcher.\n";
78       return false;
79     }
80     Finder.matchAST(AST->getASTContext());
81 
82     for (std::vector<BoundNodes>::iterator MI = Matches.begin(),
83                                            ME = Matches.end();
84          MI != ME; ++MI) {
85       OS << "\nMatch #" << ++MatchCount << ":\n\n";
86 
87       for (BoundNodes::IDToNodeMap::const_iterator BI = MI->getMap().begin(),
88                                                    BE = MI->getMap().end();
89            BI != BE; ++BI) {
90         switch (QS.OutKind) {
91         case OK_Diag: {
92           clang::SourceRange R = BI->second.getSourceRange();
93           if (R.isValid()) {
94             TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(),
95                               &AST->getDiagnostics().getDiagnosticOptions());
96             TD.emitDiagnostic(
97                 R.getBegin(), DiagnosticsEngine::Note,
98                 "\"" + BI->first + "\" binds here",
99                 CharSourceRange::getTokenRange(R),
100                 None, &AST->getSourceManager());
101           }
102           break;
103         }
104         case OK_Print: {
105           OS << "Binding for \"" << BI->first << "\":\n";
106           BI->second.print(OS, AST->getASTContext().getPrintingPolicy());
107           OS << "\n";
108           break;
109         }
110         case OK_Dump: {
111           OS << "Binding for \"" << BI->first << "\":\n";
112           BI->second.dump(OS, AST->getSourceManager());
113           OS << "\n";
114           break;
115         }
116         }
117       }
118 
119       if (MI->getMap().empty())
120         OS << "No bindings.\n";
121     }
122   }
123 
124   OS << MatchCount << (MatchCount == 1 ? " match.\n" : " matches.\n");
125   return true;
126 }
127 
128 bool LetQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
129   if (Value) {
130     QS.NamedValues[Name] = Value;
131   } else {
132     QS.NamedValues.erase(Name);
133   }
134   return true;
135 }
136 
137 #ifndef _MSC_VER
138 const QueryKind SetQueryKind<bool>::value;
139 const QueryKind SetQueryKind<OutputKind>::value;
140 #endif
141 
142 } // namespace query
143 } // namespace clang
144