Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 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" |
Samuel Benzaquen | e39269e | 2015-02-27 17:53:23 | [diff] [blame] | 36 | " match MATCHER, m MATCHER " |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 37 | "Match the loaded ASTs against the given matcher.\n" |
Samuel Benzaquen | e39269e | 2015-02-27 17:53:23 | [diff] [blame] | 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) " |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 43 | "Set whether to bind the root matcher to \"root\".\n" |
Samuel Benzaquen | e39269e | 2015-02-27 17:53:23 | [diff] [blame] | 44 | " set output (diag|print|dump) " |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 45 | "Set whether to print bindings as diagnostics,\n" |
Samuel Benzaquen | e39269e | 2015-02-27 17:53:23 | [diff] [blame] | 46 | " " |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 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) {} |
Alexander Kornienko | 87638f6 | 2015-04-11 07:59:33 | [diff] [blame^] | 56 | void run(const MatchFinder::MatchResult &Result) override { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 57 | Bindings.push_back(Result.Nodes); |
| 58 | } |
| 59 | }; |
| 60 | |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 | [diff] [blame] | 61 | } // namespace |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 62 | |
| 63 | bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 64 | unsigned MatchCount = 0; |
| 65 | |
David Blaikie | 35013fa | 2014-04-25 15:21:43 | [diff] [blame] | 66 | for (auto &AST : QS.ASTs) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 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", |
Craig Topper | ae926ad | 2014-08-29 06:05:20 | [diff] [blame] | 99 | CharSourceRange::getTokenRange(R), |
Craig Topper | 2f020e5 | 2014-08-27 06:29:07 | [diff] [blame] | 100 | None, &AST->getSourceManager()); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 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 | |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 | [diff] [blame] | 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 | |
Peter Collingbourne | c7ae610 | 2013-11-08 08:54:53 | [diff] [blame] | 137 | #ifndef _MSC_VER |
| 138 | const QueryKind SetQueryKind<bool>::value; |
| 139 | const QueryKind SetQueryKind<OutputKind>::value; |
| 140 | #endif |
| 141 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 142 | } // namespace query |
| 143 | } // namespace clang |