1 //===---- Query.cpp - clang-query query -----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Query.h"
10 #include "QuerySession.h"
11 #include "clang/AST/ASTDumper.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 print-matcher (true|false)    "
45         "Set whether to print the current matcher,\n"
46         "  set output <feature>              "
47         "Set whether to output only <feature> content.\n"
48         "  enable output <feature>           "
49         "Enable <feature> content non-exclusively.\n"
50         "  disable output <feature>          "
51         "Disable <feature> content non-exclusively.\n"
52         "  quit, q                           "
53         "Terminates the query session.\n\n"
54         "Several commands accept a <feature> parameter. The available features "
55         "are:\n\n"
56         "  print                             "
57         "Pretty-print bound nodes.\n"
58         "  diag                              "
59         "Diagnostic location for bound nodes.\n"
60         "  detailed-ast                      "
61         "Detailed AST output for bound nodes.\n"
62         "  dump                              "
63         "Detailed AST output for bound nodes (alias of detailed-ast).\n\n";
64   return true;
65 }
66 
67 bool QuitQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
68   QS.Terminate = true;
69   return true;
70 }
71 
72 namespace {
73 
74 struct CollectBoundNodes : MatchFinder::MatchCallback {
75   std::vector<BoundNodes> &Bindings;
76   CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {}
77   void run(const MatchFinder::MatchResult &Result) override {
78     Bindings.push_back(Result.Nodes);
79   }
80 };
81 
82 } // namespace
83 
84 bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
85   unsigned MatchCount = 0;
86 
87   for (auto &AST : QS.ASTs) {
88     MatchFinder Finder;
89     std::vector<BoundNodes> Matches;
90     DynTypedMatcher MaybeBoundMatcher = Matcher;
91     if (QS.BindRoot) {
92       llvm::Optional<DynTypedMatcher> M = Matcher.tryBind("root");
93       if (M)
94         MaybeBoundMatcher = *M;
95     }
96     CollectBoundNodes Collect(Matches);
97     if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) {
98       OS << "Not a valid top-level matcher.\n";
99       return false;
100     }
101     Finder.matchAST(AST->getASTContext());
102 
103     if (QS.PrintMatcher) {
104       SmallVector<StringRef, 4> Lines;
105       Source.split(Lines, "\n");
106       auto FirstLine = Lines[0];
107       Lines.erase(Lines.begin(), Lines.begin() + 1);
108       while (!Lines.empty() && Lines.back().empty()) {
109         Lines.resize(Lines.size() - 1);
110       }
111       unsigned MaxLength = FirstLine.size();
112       std::string PrefixText = "Matcher: ";
113       OS << "\n  " << PrefixText << FirstLine;
114 
115       for (auto Line : Lines) {
116         OS << "\n" << std::string(PrefixText.size() + 2, ' ') << Line;
117         MaxLength = std::max<int>(MaxLength, Line.rtrim().size());
118       }
119 
120       OS << "\n"
121          << "  " << std::string(PrefixText.size() + MaxLength, '=') << "\n\n";
122     }
123 
124     for (auto MI = Matches.begin(), ME = Matches.end(); MI != ME; ++MI) {
125       OS << "\nMatch #" << ++MatchCount << ":\n\n";
126 
127       for (auto BI = MI->getMap().begin(), BE = MI->getMap().end(); BI != BE;
128            ++BI) {
129         if (QS.DiagOutput) {
130           clang::SourceRange R = BI->second.getSourceRange();
131           if (R.isValid()) {
132             TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(),
133                               &AST->getDiagnostics().getDiagnosticOptions());
134             TD.emitDiagnostic(
135                 FullSourceLoc(R.getBegin(), AST->getSourceManager()),
136                 DiagnosticsEngine::Note, "\"" + BI->first + "\" binds here",
137                 CharSourceRange::getTokenRange(R), None);
138           }
139         }
140         if (QS.PrintOutput) {
141           OS << "Binding for \"" << BI->first << "\":\n";
142           BI->second.print(OS, AST->getASTContext().getPrintingPolicy());
143           OS << "\n";
144         }
145         if (QS.DetailedASTOutput) {
146           OS << "Binding for \"" << BI->first << "\":\n";
147           const ASTContext &Ctx = AST->getASTContext();
148           const SourceManager &SM = Ctx.getSourceManager();
149           ASTDumper Dumper(OS, &Ctx.getCommentCommandTraits(), &SM,
150                 SM.getDiagnostics().getShowColors(), Ctx.getPrintingPolicy());
151           Dumper.Visit(BI->second);
152           OS << "\n";
153         }
154       }
155 
156       if (MI->getMap().empty())
157         OS << "No bindings.\n";
158     }
159   }
160 
161   OS << MatchCount << (MatchCount == 1 ? " match.\n" : " matches.\n");
162   return true;
163 }
164 
165 bool LetQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
166   if (Value) {
167     QS.NamedValues[Name] = Value;
168   } else {
169     QS.NamedValues.erase(Name);
170   }
171   return true;
172 }
173 
174 #ifndef _MSC_VER
175 const QueryKind SetQueryKind<bool>::value;
176 const QueryKind SetQueryKind<OutputKind>::value;
177 #endif
178 
179 } // namespace query
180 } // namespace clang
181