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" |
Stephen Kelly | 4a5b01d | 2018-10-20 09:13:59 | [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 | [diff] [blame] | 46 | " set output <feature> " |
| 47 | "Set whether to output only <feature> content.\n" |
Stephen Kelly | 42668a4 | 2018-10-03 07:52:44 | [diff] [blame] | 48 | " quit, q " |
Stephen Kelly | 4c3d7a9 | 2018-10-24 20:33:14 | [diff] [blame] | 49 | "Terminates the query session.\n\n" |
| 50 | "Several commands accept a <feature> parameter. The available features " |
| 51 | "are:\n\n" |
| 52 | " print " |
| 53 | "Pretty-print bound nodes.\n" |
| 54 | " diag " |
| 55 | "Diagnostic location for bound nodes.\n" |
Stephen Kelly | 51707b2 | 2018-10-24 20:33:45 | [diff] [blame^] | 56 | " detailed-ast " |
| 57 | "Detailed AST output for bound nodes.\n" |
Stephen Kelly | 4c3d7a9 | 2018-10-24 20:33:14 | [diff] [blame] | 58 | " dump " |
Stephen Kelly | 51707b2 | 2018-10-24 20:33:45 | [diff] [blame^] | 59 | "Detailed AST output for bound nodes (alias of detailed-ast).\n\n"; |
Aaron Ballman | 5890717 | 2015-08-06 11:56:57 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool QuitQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 64 | QS.Terminate = true; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
| 68 | namespace { |
| 69 | |
| 70 | struct CollectBoundNodes : MatchFinder::MatchCallback { |
| 71 | std::vector<BoundNodes> &Bindings; |
| 72 | CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {} |
Alexander Kornienko | 87638f6 | 2015-04-11 07:59:33 | [diff] [blame] | 73 | void run(const MatchFinder::MatchResult &Result) override { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 74 | Bindings.push_back(Result.Nodes); |
| 75 | } |
| 76 | }; |
| 77 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 | [diff] [blame] | 78 | } // namespace |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 79 | |
| 80 | bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 81 | unsigned MatchCount = 0; |
| 82 | |
David Blaikie | 35013fa | 2014-04-25 15:21:43 | [diff] [blame] | 83 | for (auto &AST : QS.ASTs) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 84 | MatchFinder Finder; |
| 85 | std::vector<BoundNodes> Matches; |
| 86 | DynTypedMatcher MaybeBoundMatcher = Matcher; |
| 87 | if (QS.BindRoot) { |
| 88 | llvm::Optional<DynTypedMatcher> M = Matcher.tryBind("root"); |
| 89 | if (M) |
| 90 | MaybeBoundMatcher = *M; |
| 91 | } |
| 92 | CollectBoundNodes Collect(Matches); |
| 93 | if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) { |
| 94 | OS << "Not a valid top-level matcher.\n"; |
| 95 | return false; |
| 96 | } |
| 97 | Finder.matchAST(AST->getASTContext()); |
| 98 | |
Stephen Kelly | 4a5b01d | 2018-10-20 09:13:59 | [diff] [blame] | 99 | if (QS.PrintMatcher) { |
| 100 | std::string prefixText = "Matcher: "; |
| 101 | OS << "\n " << prefixText << Source << "\n"; |
| 102 | OS << " " << std::string(prefixText.size() + Source.size(), '=') << '\n'; |
| 103 | } |
| 104 | |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 | [diff] [blame] | 105 | for (auto MI = Matches.begin(), ME = Matches.end(); MI != ME; ++MI) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 106 | OS << "\nMatch #" << ++MatchCount << ":\n\n"; |
| 107 | |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 | [diff] [blame] | 108 | for (auto BI = MI->getMap().begin(), BE = MI->getMap().end(); BI != BE; |
| 109 | ++BI) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 110 | switch (QS.OutKind) { |
| 111 | case OK_Diag: { |
| 112 | clang::SourceRange R = BI->second.getSourceRange(); |
| 113 | if (R.isValid()) { |
| 114 | TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(), |
| 115 | &AST->getDiagnostics().getDiagnosticOptions()); |
Peter Smith | d34a65d | 2017-06-27 10:04:04 | [diff] [blame] | 116 | TD.emitDiagnostic( |
| 117 | FullSourceLoc(R.getBegin(), AST->getSourceManager()), |
| 118 | DiagnosticsEngine::Note, "\"" + BI->first + "\" binds here", |
| 119 | CharSourceRange::getTokenRange(R), None); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 120 | } |
| 121 | break; |
| 122 | } |
| 123 | case OK_Print: { |
| 124 | OS << "Binding for \"" << BI->first << "\":\n"; |
| 125 | BI->second.print(OS, AST->getASTContext().getPrintingPolicy()); |
| 126 | OS << "\n"; |
| 127 | break; |
| 128 | } |
Stephen Kelly | 51707b2 | 2018-10-24 20:33:45 | [diff] [blame^] | 129 | case OK_DetailedAST: { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 130 | OS << "Binding for \"" << BI->first << "\":\n"; |
| 131 | BI->second.dump(OS, AST->getSourceManager()); |
| 132 | OS << "\n"; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (MI->getMap().empty()) |
| 139 | OS << "No bindings.\n"; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | OS << MatchCount << (MatchCount == 1 ? " match.\n" : " matches.\n"); |
| 144 | return true; |
| 145 | } |
| 146 | |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 | [diff] [blame] | 147 | bool LetQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 148 | if (Value) { |
| 149 | QS.NamedValues[Name] = Value; |
| 150 | } else { |
| 151 | QS.NamedValues.erase(Name); |
| 152 | } |
| 153 | return true; |
| 154 | } |
| 155 | |
Peter Collingbourne | c7ae610 | 2013-11-08 08:54:53 | [diff] [blame] | 156 | #ifndef _MSC_VER |
| 157 | const QueryKind SetQueryKind<bool>::value; |
| 158 | const QueryKind SetQueryKind<OutputKind>::value; |
| 159 | #endif |
| 160 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 161 | } // namespace query |
| 162 | } // namespace clang |