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 | |
David Blaikie | e04a3da | 2015-10-20 21:45:52 | [diff] [blame] | 23 | Query::~Query() {} |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 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 | " " |
Aaron Ballman | 5890717 | 2015-08-06 11:56:57 | [diff] [blame] | 47 | "AST pretty prints or AST dumps.\n" |
| 48 | " quit " |
| 49 | "Terminates the query session.\n\n"; |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | bool QuitQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 54 | QS.Terminate = true; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
| 58 | namespace { |
| 59 | |
| 60 | struct CollectBoundNodes : MatchFinder::MatchCallback { |
| 61 | std::vector<BoundNodes> &Bindings; |
| 62 | CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {} |
Alexander Kornienko | 87638f6 | 2015-04-11 07:59:33 | [diff] [blame] | 63 | void run(const MatchFinder::MatchResult &Result) override { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 64 | Bindings.push_back(Result.Nodes); |
| 65 | } |
| 66 | }; |
| 67 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 | [diff] [blame^] | 68 | } // namespace |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 69 | |
| 70 | bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 71 | unsigned MatchCount = 0; |
| 72 | |
David Blaikie | 35013fa | 2014-04-25 15:21:43 | [diff] [blame] | 73 | for (auto &AST : QS.ASTs) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 74 | 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 | |
| 89 | for (std::vector<BoundNodes>::iterator MI = Matches.begin(), |
| 90 | ME = Matches.end(); |
| 91 | MI != ME; ++MI) { |
| 92 | OS << "\nMatch #" << ++MatchCount << ":\n\n"; |
| 93 | |
| 94 | for (BoundNodes::IDToNodeMap::const_iterator BI = MI->getMap().begin(), |
| 95 | BE = MI->getMap().end(); |
| 96 | BI != BE; ++BI) { |
| 97 | switch (QS.OutKind) { |
| 98 | case OK_Diag: { |
| 99 | clang::SourceRange R = BI->second.getSourceRange(); |
| 100 | if (R.isValid()) { |
| 101 | TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(), |
| 102 | &AST->getDiagnostics().getDiagnosticOptions()); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 | [diff] [blame^] | 103 | TD.emitDiagnostic(R.getBegin(), DiagnosticsEngine::Note, |
| 104 | "\"" + BI->first + "\" binds here", |
| 105 | CharSourceRange::getTokenRange(R), None, |
| 106 | &AST->getSourceManager()); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 107 | } |
| 108 | break; |
| 109 | } |
| 110 | case OK_Print: { |
| 111 | OS << "Binding for \"" << BI->first << "\":\n"; |
| 112 | BI->second.print(OS, AST->getASTContext().getPrintingPolicy()); |
| 113 | OS << "\n"; |
| 114 | break; |
| 115 | } |
| 116 | case OK_Dump: { |
| 117 | OS << "Binding for \"" << BI->first << "\":\n"; |
| 118 | BI->second.dump(OS, AST->getSourceManager()); |
| 119 | OS << "\n"; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (MI->getMap().empty()) |
| 126 | OS << "No bindings.\n"; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | OS << MatchCount << (MatchCount == 1 ? " match.\n" : " matches.\n"); |
| 131 | return true; |
| 132 | } |
| 133 | |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 | [diff] [blame] | 134 | bool LetQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 135 | if (Value) { |
| 136 | QS.NamedValues[Name] = Value; |
| 137 | } else { |
| 138 | QS.NamedValues.erase(Name); |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
Peter Collingbourne | c7ae610 | 2013-11-08 08:54:53 | [diff] [blame] | 143 | #ifndef _MSC_VER |
| 144 | const QueryKind SetQueryKind<bool>::value; |
| 145 | const QueryKind SetQueryKind<OutputKind>::value; |
| 146 | #endif |
| 147 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 148 | } // namespace query |
| 149 | } // namespace clang |