Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [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 +0000 | [diff] [blame] | 23 | Query::~Query() {} |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [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 +0000 | [diff] [blame] | 36 | " match MATCHER, m MATCHER " |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 37 | "Match the loaded ASTs against the given matcher.\n" |
Samuel Benzaquen | e39269e | 2015-02-27 17:53:23 +0000 | [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 +0000 | [diff] [blame] | 43 | "Set whether to bind the root matcher to \"root\".\n" |
Stephen Kelly | 4a5b01d | 2018-10-20 09:13:59 +0000 | [diff] [blame] | 44 | " set print-matcher (true|false) " |
| 45 | "Set whether to print the current matcher,\n" |
Stephen Kelly | 4c3d7a9 | 2018-10-24 20:33:14 +0000 | [diff] [blame] | 46 | " set output <feature> " |
| 47 | "Set whether to output only <feature> content.\n" |
Stephen Kelly | a49fe5d | 2018-10-29 18:59:56 +0000 | [diff] [blame^] | 48 | " enable output <feature> " |
| 49 | "Enable <feature> content non-exclusively.\n" |
| 50 | " disable output <feature> " |
| 51 | "Disable <feature> content non-exclusively.\n" |
Stephen Kelly | 42668a4 | 2018-10-03 07:52:44 +0000 | [diff] [blame] | 52 | " quit, q " |
Stephen Kelly | 4c3d7a9 | 2018-10-24 20:33:14 +0000 | [diff] [blame] | 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" |
Stephen Kelly | 51707b2 | 2018-10-24 20:33:45 +0000 | [diff] [blame] | 60 | " detailed-ast " |
| 61 | "Detailed AST output for bound nodes.\n" |
Stephen Kelly | 4c3d7a9 | 2018-10-24 20:33:14 +0000 | [diff] [blame] | 62 | " dump " |
Stephen Kelly | 51707b2 | 2018-10-24 20:33:45 +0000 | [diff] [blame] | 63 | "Detailed AST output for bound nodes (alias of detailed-ast).\n\n"; |
Aaron Ballman | 5890717 | 2015-08-06 11:56:57 +0000 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | |
| 67 | bool QuitQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 68 | QS.Terminate = true; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 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) {} |
Alexander Kornienko | 87638f6 | 2015-04-11 07:59:33 +0000 | [diff] [blame] | 77 | void run(const MatchFinder::MatchResult &Result) override { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 78 | Bindings.push_back(Result.Nodes); |
| 79 | } |
| 80 | }; |
| 81 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 82 | } // namespace |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 83 | |
| 84 | bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 85 | unsigned MatchCount = 0; |
| 86 | |
David Blaikie | 35013fa | 2014-04-25 15:21:43 +0000 | [diff] [blame] | 87 | for (auto &AST : QS.ASTs) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 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 | |
Stephen Kelly | 4a5b01d | 2018-10-20 09:13:59 +0000 | [diff] [blame] | 103 | if (QS.PrintMatcher) { |
| 104 | std::string prefixText = "Matcher: "; |
| 105 | OS << "\n " << prefixText << Source << "\n"; |
| 106 | OS << " " << std::string(prefixText.size() + Source.size(), '=') << '\n'; |
| 107 | } |
| 108 | |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 109 | for (auto MI = Matches.begin(), ME = Matches.end(); MI != ME; ++MI) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 110 | OS << "\nMatch #" << ++MatchCount << ":\n\n"; |
| 111 | |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 112 | for (auto BI = MI->getMap().begin(), BE = MI->getMap().end(); BI != BE; |
| 113 | ++BI) { |
Stephen Kelly | 70d7717 | 2018-10-24 20:33:55 +0000 | [diff] [blame] | 114 | if (QS.DiagOutput) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 115 | clang::SourceRange R = BI->second.getSourceRange(); |
| 116 | if (R.isValid()) { |
| 117 | TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(), |
| 118 | &AST->getDiagnostics().getDiagnosticOptions()); |
Peter Smith | d34a65d | 2017-06-27 10:04:04 +0000 | [diff] [blame] | 119 | TD.emitDiagnostic( |
| 120 | FullSourceLoc(R.getBegin(), AST->getSourceManager()), |
| 121 | DiagnosticsEngine::Note, "\"" + BI->first + "\" binds here", |
| 122 | CharSourceRange::getTokenRange(R), None); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 123 | } |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 124 | } |
Stephen Kelly | 70d7717 | 2018-10-24 20:33:55 +0000 | [diff] [blame] | 125 | if (QS.PrintOutput) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 126 | OS << "Binding for \"" << BI->first << "\":\n"; |
| 127 | BI->second.print(OS, AST->getASTContext().getPrintingPolicy()); |
| 128 | OS << "\n"; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 129 | } |
Stephen Kelly | 70d7717 | 2018-10-24 20:33:55 +0000 | [diff] [blame] | 130 | if (QS.DetailedASTOutput) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 131 | OS << "Binding for \"" << BI->first << "\":\n"; |
| 132 | BI->second.dump(OS, AST->getSourceManager()); |
| 133 | OS << "\n"; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | if (MI->getMap().empty()) |
| 138 | OS << "No bindings.\n"; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | OS << MatchCount << (MatchCount == 1 ? " match.\n" : " matches.\n"); |
| 143 | return true; |
| 144 | } |
| 145 | |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 146 | bool LetQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 147 | if (Value) { |
| 148 | QS.NamedValues[Name] = Value; |
| 149 | } else { |
| 150 | QS.NamedValues.erase(Name); |
| 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
Peter Collingbourne | c7ae610 | 2013-11-08 08:54:53 +0000 | [diff] [blame] | 155 | #ifndef _MSC_VER |
| 156 | const QueryKind SetQueryKind<bool>::value; |
| 157 | const QueryKind SetQueryKind<OutputKind>::value; |
| 158 | #endif |
| 159 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 160 | } // namespace query |
| 161 | } // namespace clang |