Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 1 | //===---- Query.cpp - clang-query query -----------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 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 |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Query.h" |
| 10 | #include "QuerySession.h" |
Stephen Kelly | b22d8ae | 2019-12-06 23:26:59 | [diff] [blame] | 11 | #include "clang/AST/ASTDumper.h" |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "clang/Frontend/ASTUnit.h" |
| 14 | #include "clang/Frontend/TextDiagnostic.h" |
Stephen Kelly | 8d018c7 | 2020-12-12 18:39:49 | [diff] [blame] | 15 | #include "clang/Tooling/NodeIntrospection.h" |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | |
| 18 | using namespace clang::ast_matchers; |
| 19 | using namespace clang::ast_matchers::dynamic; |
| 20 | |
| 21 | namespace clang { |
| 22 | namespace query { |
| 23 | |
David Blaikie | e04a3da | 2015-10-20 21:45:52 | [diff] [blame] | 24 | Query::~Query() {} |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 25 | |
| 26 | bool InvalidQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 27 | OS << ErrStr << "\n"; |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | bool NoOpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | bool HelpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 36 | OS << "Available commands:\n\n" |
Samuel Benzaquen | e39269e | 2015-02-27 17:53:23 | [diff] [blame] | 37 | " match MATCHER, m MATCHER " |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 38 | "Match the loaded ASTs against the given matcher.\n" |
Samuel Benzaquen | e39269e | 2015-02-27 17:53:23 | [diff] [blame] | 39 | " let NAME MATCHER, l NAME MATCHER " |
| 40 | "Give a matcher expression a name, to be used later\n" |
| 41 | " " |
| 42 | "as part of other expressions.\n" |
| 43 | " set bind-root (true|false) " |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 44 | "Set whether to bind the root matcher to \"root\".\n" |
Stephen Kelly | 4a5b01d | 2018-10-20 09:13:59 | [diff] [blame] | 45 | " set print-matcher (true|false) " |
| 46 | "Set whether to print the current matcher,\n" |
Stephen Kelly | 10f0f98 | 2019-12-29 19:26:11 | [diff] [blame] | 47 | " set traversal <kind> " |
| 48 | "Set traversal kind of clang-query session. Available kinds are:\n" |
| 49 | " AsIs " |
Stephen Kelly | 551092b | 2020-07-02 19:46:27 | [diff] [blame] | 50 | "Print and match the AST as clang sees it. This mode is the " |
| 51 | "default.\n" |
Stephen Kelly | 10f0f98 | 2019-12-29 19:26:11 | [diff] [blame] | 52 | " IgnoreUnlessSpelledInSource " |
Stephen Kelly | 551092b | 2020-07-02 19:46:27 | [diff] [blame] | 53 | "Omit AST nodes unless spelled in the source.\n" |
Stephen Kelly | 4c3d7a9 | 2018-10-24 20:33:14 | [diff] [blame] | 54 | " set output <feature> " |
| 55 | "Set whether to output only <feature> content.\n" |
Stephen Kelly | a49fe5d | 2018-10-29 18:59:56 | [diff] [blame] | 56 | " enable output <feature> " |
| 57 | "Enable <feature> content non-exclusively.\n" |
| 58 | " disable output <feature> " |
| 59 | "Disable <feature> content non-exclusively.\n" |
Stephen Kelly | 42668a4 | 2018-10-03 07:52:44 | [diff] [blame] | 60 | " quit, q " |
Stephen Kelly | 4c3d7a9 | 2018-10-24 20:33:14 | [diff] [blame] | 61 | "Terminates the query session.\n\n" |
| 62 | "Several commands accept a <feature> parameter. The available features " |
| 63 | "are:\n\n" |
| 64 | " print " |
| 65 | "Pretty-print bound nodes.\n" |
| 66 | " diag " |
| 67 | "Diagnostic location for bound nodes.\n" |
Stephen Kelly | 51707b2 | 2018-10-24 20:33:45 | [diff] [blame] | 68 | " detailed-ast " |
| 69 | "Detailed AST output for bound nodes.\n" |
Stephen Kelly | 8d018c7 | 2020-12-12 18:39:49 | [diff] [blame] | 70 | " srcloc " |
| 71 | "Source locations and ranges for bound nodes.\n" |
Stephen Kelly | 4c3d7a9 | 2018-10-24 20:33:14 | [diff] [blame] | 72 | " dump " |
Stephen Kelly | 51707b2 | 2018-10-24 20:33:45 | [diff] [blame] | 73 | "Detailed AST output for bound nodes (alias of detailed-ast).\n\n"; |
Aaron Ballman | 5890717 | 2015-08-06 11:56:57 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool QuitQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 78 | QS.Terminate = true; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
| 82 | namespace { |
| 83 | |
| 84 | struct CollectBoundNodes : MatchFinder::MatchCallback { |
| 85 | std::vector<BoundNodes> &Bindings; |
| 86 | CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {} |
Alexander Kornienko | 87638f6 | 2015-04-11 07:59:33 | [diff] [blame] | 87 | void run(const MatchFinder::MatchResult &Result) override { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 88 | Bindings.push_back(Result.Nodes); |
| 89 | } |
| 90 | }; |
| 91 | |
Stephen Kelly | 8d018c7 | 2020-12-12 18:39:49 | [diff] [blame] | 92 | void dumpLocations(llvm::raw_ostream &OS, DynTypedNode Node, ASTContext &Ctx, |
| 93 | const DiagnosticsEngine &Diags, SourceManager const &SM) { |
| 94 | auto Locs = clang::tooling::NodeIntrospection::GetLocations(Node); |
| 95 | |
| 96 | auto PrintLocations = [](llvm::raw_ostream &OS, auto Iter, auto End) { |
| 97 | auto CommonEntry = Iter->first; |
| 98 | auto Scout = Iter; |
| 99 | SmallVector<std::string> LocationStrings; |
| 100 | while (Scout->first == CommonEntry) { |
| 101 | LocationStrings.push_back( |
| 102 | tooling::LocationCallFormatterCpp::format(*Iter->second)); |
| 103 | if (Scout == End) |
| 104 | break; |
| 105 | ++Scout; |
| 106 | if (Scout->first == CommonEntry) |
| 107 | ++Iter; |
| 108 | } |
| 109 | llvm::sort(LocationStrings); |
| 110 | for (auto &LS : LocationStrings) { |
| 111 | OS << " * \"" << LS << "\"\n"; |
| 112 | } |
| 113 | return Iter; |
| 114 | }; |
| 115 | |
| 116 | TextDiagnostic TD(OS, Ctx.getLangOpts(), &Diags.getDiagnosticOptions()); |
| 117 | |
| 118 | for (auto Iter = Locs.LocationAccessors.begin(); |
| 119 | Iter != Locs.LocationAccessors.end(); ++Iter) { |
| 120 | if (!Iter->first.isValid()) |
| 121 | continue; |
| 122 | |
| 123 | TD.emitDiagnostic(FullSourceLoc(Iter->first, SM), DiagnosticsEngine::Note, |
| 124 | "source locations here", None, None); |
| 125 | |
| 126 | Iter = PrintLocations(OS, Iter, Locs.LocationAccessors.end()); |
| 127 | OS << '\n'; |
| 128 | } |
| 129 | |
| 130 | for (auto Iter = Locs.RangeAccessors.begin(); |
| 131 | Iter != Locs.RangeAccessors.end(); ++Iter) { |
| 132 | |
| 133 | if (!Iter->first.getBegin().isValid()) |
| 134 | continue; |
| 135 | |
| 136 | if (SM.getPresumedLineNumber(Iter->first.getBegin()) != |
| 137 | SM.getPresumedLineNumber(Iter->first.getEnd())) |
| 138 | continue; |
| 139 | |
| 140 | TD.emitDiagnostic(FullSourceLoc(Iter->first.getBegin(), SM), |
| 141 | DiagnosticsEngine::Note, |
| 142 | "source ranges here " + Iter->first.printToString(SM), |
| 143 | CharSourceRange::getTokenRange(Iter->first), None); |
| 144 | |
| 145 | Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end()); |
| 146 | } |
| 147 | for (auto Iter = Locs.RangeAccessors.begin(); |
| 148 | Iter != Locs.RangeAccessors.end(); ++Iter) { |
| 149 | |
| 150 | if (!Iter->first.getBegin().isValid()) |
| 151 | continue; |
| 152 | |
| 153 | if (SM.getPresumedLineNumber(Iter->first.getBegin()) == |
| 154 | SM.getPresumedLineNumber(Iter->first.getEnd())) |
| 155 | continue; |
| 156 | |
| 157 | TD.emitDiagnostic( |
| 158 | FullSourceLoc(Iter->first.getBegin(), SM), DiagnosticsEngine::Note, |
| 159 | "source range " + Iter->first.printToString(SM) + " starting here...", |
| 160 | CharSourceRange::getTokenRange(Iter->first), None); |
| 161 | |
| 162 | auto ColNum = SM.getPresumedColumnNumber(Iter->first.getEnd()); |
| 163 | auto LastLineLoc = Iter->first.getEnd().getLocWithOffset(-(ColNum - 1)); |
| 164 | |
| 165 | TD.emitDiagnostic(FullSourceLoc(Iter->first.getEnd(), SM), |
| 166 | DiagnosticsEngine::Note, "... ending here", |
| 167 | CharSourceRange::getTokenRange( |
| 168 | SourceRange(LastLineLoc, Iter->first.getEnd())), |
| 169 | None); |
| 170 | |
| 171 | Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end()); |
| 172 | } |
| 173 | OS << "\n"; |
| 174 | } |
| 175 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 | [diff] [blame] | 176 | } // namespace |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 177 | |
| 178 | bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 179 | unsigned MatchCount = 0; |
| 180 | |
David Blaikie | 35013fa | 2014-04-25 15:21:43 | [diff] [blame] | 181 | for (auto &AST : QS.ASTs) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 182 | MatchFinder Finder; |
| 183 | std::vector<BoundNodes> Matches; |
| 184 | DynTypedMatcher MaybeBoundMatcher = Matcher; |
| 185 | if (QS.BindRoot) { |
| 186 | llvm::Optional<DynTypedMatcher> M = Matcher.tryBind("root"); |
| 187 | if (M) |
| 188 | MaybeBoundMatcher = *M; |
| 189 | } |
| 190 | CollectBoundNodes Collect(Matches); |
| 191 | if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) { |
| 192 | OS << "Not a valid top-level matcher.\n"; |
| 193 | return false; |
| 194 | } |
Stephen Kelly | 10f0f98 | 2019-12-29 19:26:11 | [diff] [blame] | 195 | |
Stephen Kelly | 8d018c7 | 2020-12-12 18:39:49 | [diff] [blame] | 196 | auto &Ctx = AST->getASTContext(); |
| 197 | const auto &SM = Ctx.getSourceManager(); |
| 198 | Ctx.getParentMapContext().setTraversalKind(QS.TK); |
| 199 | Finder.matchAST(Ctx); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 200 | |
Stephen Kelly | 4a5b01d | 2018-10-20 09:13:59 | [diff] [blame] | 201 | if (QS.PrintMatcher) { |
Stephen Kelly | f072233 | 2019-12-18 22:35:46 | [diff] [blame] | 202 | SmallVector<StringRef, 4> Lines; |
| 203 | Source.split(Lines, "\n"); |
| 204 | auto FirstLine = Lines[0]; |
| 205 | Lines.erase(Lines.begin(), Lines.begin() + 1); |
| 206 | while (!Lines.empty() && Lines.back().empty()) { |
| 207 | Lines.resize(Lines.size() - 1); |
| 208 | } |
| 209 | unsigned MaxLength = FirstLine.size(); |
| 210 | std::string PrefixText = "Matcher: "; |
| 211 | OS << "\n " << PrefixText << FirstLine; |
| 212 | |
| 213 | for (auto Line : Lines) { |
| 214 | OS << "\n" << std::string(PrefixText.size() + 2, ' ') << Line; |
| 215 | MaxLength = std::max<int>(MaxLength, Line.rtrim().size()); |
| 216 | } |
| 217 | |
| 218 | OS << "\n" |
| 219 | << " " << std::string(PrefixText.size() + MaxLength, '=') << "\n\n"; |
Stephen Kelly | 4a5b01d | 2018-10-20 09:13:59 | [diff] [blame] | 220 | } |
| 221 | |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 | [diff] [blame] | 222 | for (auto MI = Matches.begin(), ME = Matches.end(); MI != ME; ++MI) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 223 | OS << "\nMatch #" << ++MatchCount << ":\n\n"; |
| 224 | |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 | [diff] [blame] | 225 | for (auto BI = MI->getMap().begin(), BE = MI->getMap().end(); BI != BE; |
| 226 | ++BI) { |
Stephen Kelly | 70d7717 | 2018-10-24 20:33:55 | [diff] [blame] | 227 | if (QS.DiagOutput) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 228 | clang::SourceRange R = BI->second.getSourceRange(); |
| 229 | if (R.isValid()) { |
| 230 | TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(), |
| 231 | &AST->getDiagnostics().getDiagnosticOptions()); |
Peter Smith | d34a65d | 2017-06-27 10:04:04 | [diff] [blame] | 232 | TD.emitDiagnostic( |
| 233 | FullSourceLoc(R.getBegin(), AST->getSourceManager()), |
| 234 | DiagnosticsEngine::Note, "\"" + BI->first + "\" binds here", |
| 235 | CharSourceRange::getTokenRange(R), None); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 236 | } |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 237 | } |
Stephen Kelly | 70d7717 | 2018-10-24 20:33:55 | [diff] [blame] | 238 | if (QS.PrintOutput) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 239 | OS << "Binding for \"" << BI->first << "\":\n"; |
| 240 | BI->second.print(OS, AST->getASTContext().getPrintingPolicy()); |
| 241 | OS << "\n"; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 242 | } |
Stephen Kelly | 70d7717 | 2018-10-24 20:33:55 | [diff] [blame] | 243 | if (QS.DetailedASTOutput) { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 244 | OS << "Binding for \"" << BI->first << "\":\n"; |
Stephen Kelly | b22d8ae | 2019-12-06 23:26:59 | [diff] [blame] | 245 | const ASTContext &Ctx = AST->getASTContext(); |
Tom Ritter | d6d36ba | 2021-01-25 13:06:54 | [diff] [blame] | 246 | ASTDumper Dumper(OS, Ctx, AST->getDiagnostics().getShowColors()); |
Stephen Kelly | 10f0f98 | 2019-12-29 19:26:11 | [diff] [blame] | 247 | Dumper.SetTraversalKind(QS.TK); |
Stephen Kelly | b22d8ae | 2019-12-06 23:26:59 | [diff] [blame] | 248 | Dumper.Visit(BI->second); |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 249 | OS << "\n"; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 250 | } |
Stephen Kelly | 8d018c7 | 2020-12-12 18:39:49 | [diff] [blame] | 251 | if (QS.SrcLocOutput) { |
| 252 | OS << "\n \"" << BI->first << "\" Source locations\n"; |
| 253 | OS << " " << std::string(19 + BI->first.size(), '-') << '\n'; |
| 254 | |
| 255 | dumpLocations(OS, BI->second, Ctx, AST->getDiagnostics(), SM); |
| 256 | OS << "\n"; |
| 257 | } |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (MI->getMap().empty()) |
| 261 | OS << "No bindings.\n"; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | OS << MatchCount << (MatchCount == 1 ? " match.\n" : " matches.\n"); |
| 266 | return true; |
| 267 | } |
| 268 | |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 | [diff] [blame] | 269 | bool LetQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const { |
| 270 | if (Value) { |
| 271 | QS.NamedValues[Name] = Value; |
| 272 | } else { |
| 273 | QS.NamedValues.erase(Name); |
| 274 | } |
| 275 | return true; |
| 276 | } |
| 277 | |
Peter Collingbourne | c7ae610 | 2013-11-08 08:54:53 | [diff] [blame] | 278 | #ifndef _MSC_VER |
| 279 | const QueryKind SetQueryKind<bool>::value; |
| 280 | const QueryKind SetQueryKind<OutputKind>::value; |
| 281 | #endif |
| 282 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 | [diff] [blame] | 283 | } // namespace query |
| 284 | } // namespace clang |