[PATCH] D93325: Add srcloc output to clang-query
This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rG8d018c79ee5f: Add srcloc output to clang-query (authored by stephenkelly). Changed prior to commit: https://reviews.llvm.org/D93325?vs=339583=340351#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D93325/new/ https://reviews.llvm.org/D93325 Files: clang-tools-extra/clang-query/CMakeLists.txt clang-tools-extra/clang-query/Query.cpp clang-tools-extra/clang-query/Query.h clang-tools-extra/clang-query/QueryParser.cpp clang-tools-extra/clang-query/QuerySession.h clang-tools-extra/unittests/clang-query/QueryParserTest.cpp Index: clang-tools-extra/unittests/clang-query/QueryParserTest.cpp === --- clang-tools-extra/unittests/clang-query/QueryParserTest.cpp +++ clang-tools-extra/unittests/clang-query/QueryParserTest.cpp @@ -218,7 +218,7 @@ EXPECT_EQ("output", Comps[0].DisplayText); Comps = QueryParser::complete("enable output ", 14, QS); - ASSERT_EQ(4u, Comps.size()); + ASSERT_EQ(5u, Comps.size()); EXPECT_EQ("diag ", Comps[0].TypedText); EXPECT_EQ("diag", Comps[0].DisplayText); @@ -226,8 +226,10 @@ EXPECT_EQ("print", Comps[1].DisplayText); EXPECT_EQ("detailed-ast ", Comps[2].TypedText); EXPECT_EQ("detailed-ast", Comps[2].DisplayText); - EXPECT_EQ("dump ", Comps[3].TypedText); - EXPECT_EQ("dump", Comps[3].DisplayText); + EXPECT_EQ("srcloc ", Comps[3].TypedText); + EXPECT_EQ("srcloc", Comps[3].DisplayText); + EXPECT_EQ("dump ", Comps[4].TypedText); + EXPECT_EQ("dump", Comps[4].DisplayText); Comps = QueryParser::complete("set traversal ", 14, QS); ASSERT_EQ(2u, Comps.size()); Index: clang-tools-extra/clang-query/QuerySession.h === --- clang-tools-extra/clang-query/QuerySession.h +++ clang-tools-extra/clang-query/QuerySession.h @@ -25,14 +25,15 @@ public: QuerySession(llvm::ArrayRef> ASTs) : ASTs(ASTs), PrintOutput(false), DiagOutput(true), -DetailedASTOutput(false), BindRoot(true), PrintMatcher(false), -Terminate(false), TK(TK_AsIs) {} +DetailedASTOutput(false), SrcLocOutput(false), BindRoot(true), +PrintMatcher(false), Terminate(false), TK(TK_AsIs) {} llvm::ArrayRef> ASTs; bool PrintOutput; bool DiagOutput; bool DetailedASTOutput; + bool SrcLocOutput; bool BindRoot; bool PrintMatcher; Index: clang-tools-extra/clang-query/QueryParser.cpp === --- clang-tools-extra/clang-query/QueryParser.cpp +++ clang-tools-extra/clang-query/QueryParser.cpp @@ -108,6 +108,7 @@ .Case("diag", OK_Diag) .Case("print", OK_Print) .Case("detailed-ast", OK_DetailedAST) + .Case("srcloc", OK_SrcLoc) .Case("dump", OK_DetailedAST) .Default(~0u); if (OutKind == ~0u) { @@ -123,6 +124,8 @@ return new QueryType(::DiagOutput); case OK_Print: return new QueryType(::PrintOutput); + case OK_SrcLoc: +return new QueryType(::SrcLocOutput); } llvm_unreachable("Invalid output kind"); Index: clang-tools-extra/clang-query/Query.h === --- clang-tools-extra/clang-query/Query.h +++ clang-tools-extra/clang-query/Query.h @@ -18,7 +18,7 @@ namespace clang { namespace query { -enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST }; +enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST, OK_SrcLoc }; enum QueryKind { QK_Invalid, Index: clang-tools-extra/clang-query/Query.cpp === --- clang-tools-extra/clang-query/Query.cpp +++ clang-tools-extra/clang-query/Query.cpp @@ -12,6 +12,7 @@ #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/TextDiagnostic.h" +#include "clang/Tooling/NodeIntrospection.h" #include "llvm/Support/raw_ostream.h" using namespace clang::ast_matchers; @@ -66,6 +67,8 @@ "Diagnostic location for bound nodes.\n" " detailed-ast " "Detailed AST output for bound nodes.\n" +" srcloc" +"Source locations and ranges for bound nodes.\n" " dump " "Detailed AST output for bound nodes (alias of detailed-ast).\n\n"; return true; @@ -86,6 +89,90 @@ } }; +void dumpLocations(llvm::raw_ostream , DynTypedNode Node, ASTContext , + const DiagnosticsEngine , SourceManager const ) { + auto Locs = clang::tooling::NodeIntrospection::GetLocations(Node); + + auto PrintLocations = [](llvm::raw_ostream , auto Iter, auto End) { +
[PATCH] D93325: Add srcloc output to clang-query
aaron.ballman accepted this revision. aaron.ballman added a comment. This revision is now accepted and ready to land. LGTM aside from the nits from @njames93 and I. Comment at: clang-tools-extra/clang-query/Query.cpp:96 + + auto printLocations = [](auto , auto Iter, auto End) { +auto CommonEntry = Iter->first; Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D93325/new/ https://reviews.llvm.org/D93325 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D93325: Add srcloc output to clang-query
njames93 added inline comments. Comment at: clang-tools-extra/clang-query/Query.cpp:106-108 + if (Scout->first == CommonEntry) { +++Iter; + } nit: Elide braces. Comment at: clang-tools-extra/clang-query/Query.cpp:122 + +TextDiagnostic TD(OS, Ctx.getLangOpts(), ()); +TD.emitDiagnostic(FullSourceLoc(Iter->first, SM), DiagnosticsEngine::Note, Can this be brought out the loop and re-used for each of the 3 loops? Comment at: clang-tools-extra/clang-query/QueryParser.cpp:128 + case OK_SrcLoc: +return new QueryType(::SrcLocOutput); } Can worry about detecting Introspection support in a follow-up Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D93325/new/ https://reviews.llvm.org/D93325 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D93325: Add srcloc output to clang-query
steveire updated this revision to Diff 339583. steveire added a comment. Herald added a subscriber: mgrang. Update Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D93325/new/ https://reviews.llvm.org/D93325 Files: clang-tools-extra/clang-query/CMakeLists.txt clang-tools-extra/clang-query/Query.cpp clang-tools-extra/clang-query/Query.h clang-tools-extra/clang-query/QueryParser.cpp clang-tools-extra/clang-query/QuerySession.h clang-tools-extra/unittests/clang-query/QueryParserTest.cpp Index: clang-tools-extra/unittests/clang-query/QueryParserTest.cpp === --- clang-tools-extra/unittests/clang-query/QueryParserTest.cpp +++ clang-tools-extra/unittests/clang-query/QueryParserTest.cpp @@ -218,7 +218,7 @@ EXPECT_EQ("output", Comps[0].DisplayText); Comps = QueryParser::complete("enable output ", 14, QS); - ASSERT_EQ(4u, Comps.size()); + ASSERT_EQ(5u, Comps.size()); EXPECT_EQ("diag ", Comps[0].TypedText); EXPECT_EQ("diag", Comps[0].DisplayText); @@ -226,8 +226,10 @@ EXPECT_EQ("print", Comps[1].DisplayText); EXPECT_EQ("detailed-ast ", Comps[2].TypedText); EXPECT_EQ("detailed-ast", Comps[2].DisplayText); - EXPECT_EQ("dump ", Comps[3].TypedText); - EXPECT_EQ("dump", Comps[3].DisplayText); + EXPECT_EQ("srcloc ", Comps[3].TypedText); + EXPECT_EQ("srcloc", Comps[3].DisplayText); + EXPECT_EQ("dump ", Comps[4].TypedText); + EXPECT_EQ("dump", Comps[4].DisplayText); Comps = QueryParser::complete("set traversal ", 14, QS); ASSERT_EQ(2u, Comps.size()); Index: clang-tools-extra/clang-query/QuerySession.h === --- clang-tools-extra/clang-query/QuerySession.h +++ clang-tools-extra/clang-query/QuerySession.h @@ -25,14 +25,15 @@ public: QuerySession(llvm::ArrayRef> ASTs) : ASTs(ASTs), PrintOutput(false), DiagOutput(true), -DetailedASTOutput(false), BindRoot(true), PrintMatcher(false), -Terminate(false), TK(TK_AsIs) {} +DetailedASTOutput(false), SrcLocOutput(false), BindRoot(true), +PrintMatcher(false), Terminate(false), TK(TK_AsIs) {} llvm::ArrayRef> ASTs; bool PrintOutput; bool DiagOutput; bool DetailedASTOutput; + bool SrcLocOutput; bool BindRoot; bool PrintMatcher; Index: clang-tools-extra/clang-query/QueryParser.cpp === --- clang-tools-extra/clang-query/QueryParser.cpp +++ clang-tools-extra/clang-query/QueryParser.cpp @@ -108,6 +108,7 @@ .Case("diag", OK_Diag) .Case("print", OK_Print) .Case("detailed-ast", OK_DetailedAST) + .Case("srcloc", OK_SrcLoc) .Case("dump", OK_DetailedAST) .Default(~0u); if (OutKind == ~0u) { @@ -123,6 +124,8 @@ return new QueryType(::DiagOutput); case OK_Print: return new QueryType(::PrintOutput); + case OK_SrcLoc: +return new QueryType(::SrcLocOutput); } llvm_unreachable("Invalid output kind"); Index: clang-tools-extra/clang-query/Query.h === --- clang-tools-extra/clang-query/Query.h +++ clang-tools-extra/clang-query/Query.h @@ -18,7 +18,7 @@ namespace clang { namespace query { -enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST }; +enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST, OK_SrcLoc }; enum QueryKind { QK_Invalid, Index: clang-tools-extra/clang-query/Query.cpp === --- clang-tools-extra/clang-query/Query.cpp +++ clang-tools-extra/clang-query/Query.cpp @@ -12,6 +12,7 @@ #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/TextDiagnostic.h" +#include "clang/Tooling/NodeIntrospection.h" #include "llvm/Support/raw_ostream.h" using namespace clang::ast_matchers; @@ -66,6 +67,8 @@ "Diagnostic location for bound nodes.\n" " detailed-ast " "Detailed AST output for bound nodes.\n" +" srcloc" +"Source locations and ranges for bound nodes.\n" " dump " "Detailed AST output for bound nodes (alias of detailed-ast).\n\n"; return true; @@ -86,6 +89,92 @@ } }; +void dumpLocations(llvm::raw_ostream , DynTypedNode Node, ASTContext , + const DiagnosticsEngine , SourceManager const ) { + auto Locs = clang::tooling::NodeIntrospection::GetLocations(Node); + + auto printLocations = [](auto , auto Iter, auto End) { +auto CommonEntry = Iter->first; +auto Scout = Iter; +SmallVector LocationStrings; +while (Scout->first == CommonEntry) { + LocationStrings.push_back( +
[PATCH] D93325: Add srcloc output to clang-query
steveire added a comment. @aaron.ballman ping? Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D93325/new/ https://reviews.llvm.org/D93325 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D93325: Add srcloc output to clang-query
steveire updated this revision to Diff 336690. steveire added a comment. Update Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D93325/new/ https://reviews.llvm.org/D93325 Files: clang-tools-extra/clang-query/CMakeLists.txt clang-tools-extra/clang-query/Query.cpp clang-tools-extra/clang-query/Query.h clang-tools-extra/clang-query/QueryParser.cpp clang-tools-extra/clang-query/QuerySession.h clang-tools-extra/unittests/clang-query/QueryParserTest.cpp Index: clang-tools-extra/unittests/clang-query/QueryParserTest.cpp === --- clang-tools-extra/unittests/clang-query/QueryParserTest.cpp +++ clang-tools-extra/unittests/clang-query/QueryParserTest.cpp @@ -218,7 +218,7 @@ EXPECT_EQ("output", Comps[0].DisplayText); Comps = QueryParser::complete("enable output ", 14, QS); - ASSERT_EQ(4u, Comps.size()); + ASSERT_EQ(5u, Comps.size()); EXPECT_EQ("diag ", Comps[0].TypedText); EXPECT_EQ("diag", Comps[0].DisplayText); @@ -226,8 +226,10 @@ EXPECT_EQ("print", Comps[1].DisplayText); EXPECT_EQ("detailed-ast ", Comps[2].TypedText); EXPECT_EQ("detailed-ast", Comps[2].DisplayText); - EXPECT_EQ("dump ", Comps[3].TypedText); - EXPECT_EQ("dump", Comps[3].DisplayText); + EXPECT_EQ("srcloc ", Comps[3].TypedText); + EXPECT_EQ("srcloc", Comps[3].DisplayText); + EXPECT_EQ("dump ", Comps[4].TypedText); + EXPECT_EQ("dump", Comps[4].DisplayText); Comps = QueryParser::complete("set traversal ", 14, QS); ASSERT_EQ(2u, Comps.size()); Index: clang-tools-extra/clang-query/QuerySession.h === --- clang-tools-extra/clang-query/QuerySession.h +++ clang-tools-extra/clang-query/QuerySession.h @@ -25,14 +25,15 @@ public: QuerySession(llvm::ArrayRef> ASTs) : ASTs(ASTs), PrintOutput(false), DiagOutput(true), -DetailedASTOutput(false), BindRoot(true), PrintMatcher(false), -Terminate(false), TK(TK_AsIs) {} +DetailedASTOutput(false), SrcLocOutput(false), BindRoot(true), +PrintMatcher(false), Terminate(false), TK(TK_AsIs) {} llvm::ArrayRef> ASTs; bool PrintOutput; bool DiagOutput; bool DetailedASTOutput; + bool SrcLocOutput; bool BindRoot; bool PrintMatcher; Index: clang-tools-extra/clang-query/QueryParser.cpp === --- clang-tools-extra/clang-query/QueryParser.cpp +++ clang-tools-extra/clang-query/QueryParser.cpp @@ -108,6 +108,7 @@ .Case("diag", OK_Diag) .Case("print", OK_Print) .Case("detailed-ast", OK_DetailedAST) + .Case("srcloc", OK_SrcLoc) .Case("dump", OK_DetailedAST) .Default(~0u); if (OutKind == ~0u) { @@ -123,6 +124,8 @@ return new QueryType(::DiagOutput); case OK_Print: return new QueryType(::PrintOutput); + case OK_SrcLoc: +return new QueryType(::SrcLocOutput); } llvm_unreachable("Invalid output kind"); Index: clang-tools-extra/clang-query/Query.h === --- clang-tools-extra/clang-query/Query.h +++ clang-tools-extra/clang-query/Query.h @@ -18,7 +18,7 @@ namespace clang { namespace query { -enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST }; +enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST, OK_SrcLoc }; enum QueryKind { QK_Invalid, Index: clang-tools-extra/clang-query/Query.cpp === --- clang-tools-extra/clang-query/Query.cpp +++ clang-tools-extra/clang-query/Query.cpp @@ -12,6 +12,7 @@ #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/TextDiagnostic.h" +#include "clang/Tooling/NodeIntrospection.h" #include "llvm/Support/raw_ostream.h" using namespace clang::ast_matchers; @@ -66,6 +67,8 @@ "Diagnostic location for bound nodes.\n" " detailed-ast " "Detailed AST output for bound nodes.\n" +" srcloc" +"Source locations and ranges for bound nodes.\n" " dump " "Detailed AST output for bound nodes (alias of detailed-ast).\n\n"; return true; @@ -86,6 +89,88 @@ } }; +void dumpLocations(llvm::raw_ostream , DynTypedNode Node, ASTContext , + const DiagnosticsEngine , SourceManager const ) { + auto Locs = clang::tooling::NodeIntrospection::GetLocations(Node); + + auto printLocations = [](auto , auto Iter, auto End) { +auto CommonEntry = Iter->first; +auto Scout = Iter; +while (Scout->first == CommonEntry) { + OS << " * " + << "\"" + +tooling::LocationCallFormatterCpp::format(Iter->second.get()) + +
[PATCH] D93325: Add srcloc output to clang-query
steveire updated this revision to Diff 336652. steveire added a comment. Herald added a subscriber: mgorny. Herald added a project: clang-tools-extra. Update Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D93325/new/ https://reviews.llvm.org/D93325 Files: clang-tools-extra/clang-query/CMakeLists.txt clang-tools-extra/clang-query/Query.cpp clang-tools-extra/clang-query/Query.h clang-tools-extra/clang-query/QueryParser.cpp clang-tools-extra/clang-query/QuerySession.h Index: clang-tools-extra/clang-query/QuerySession.h === --- clang-tools-extra/clang-query/QuerySession.h +++ clang-tools-extra/clang-query/QuerySession.h @@ -25,14 +25,15 @@ public: QuerySession(llvm::ArrayRef> ASTs) : ASTs(ASTs), PrintOutput(false), DiagOutput(true), -DetailedASTOutput(false), BindRoot(true), PrintMatcher(false), -Terminate(false), TK(TK_AsIs) {} +DetailedASTOutput(false), SrcLocOutput(false), BindRoot(true), +PrintMatcher(false), Terminate(false), TK(TK_AsIs) {} llvm::ArrayRef> ASTs; bool PrintOutput; bool DiagOutput; bool DetailedASTOutput; + bool SrcLocOutput; bool BindRoot; bool PrintMatcher; Index: clang-tools-extra/clang-query/QueryParser.cpp === --- clang-tools-extra/clang-query/QueryParser.cpp +++ clang-tools-extra/clang-query/QueryParser.cpp @@ -108,6 +108,7 @@ .Case("diag", OK_Diag) .Case("print", OK_Print) .Case("detailed-ast", OK_DetailedAST) + .Case("srcloc", OK_SrcLoc) .Case("dump", OK_DetailedAST) .Default(~0u); if (OutKind == ~0u) { @@ -123,6 +124,8 @@ return new QueryType(::DiagOutput); case OK_Print: return new QueryType(::PrintOutput); + case OK_SrcLoc: +return new QueryType(::SrcLocOutput); } llvm_unreachable("Invalid output kind"); Index: clang-tools-extra/clang-query/Query.h === --- clang-tools-extra/clang-query/Query.h +++ clang-tools-extra/clang-query/Query.h @@ -18,7 +18,7 @@ namespace clang { namespace query { -enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST }; +enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST, OK_SrcLoc }; enum QueryKind { QK_Invalid, Index: clang-tools-extra/clang-query/Query.cpp === --- clang-tools-extra/clang-query/Query.cpp +++ clang-tools-extra/clang-query/Query.cpp @@ -12,6 +12,7 @@ #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/TextDiagnostic.h" +#include "clang/Tooling/NodeIntrospection.h" #include "llvm/Support/raw_ostream.h" using namespace clang::ast_matchers; @@ -66,6 +67,8 @@ "Diagnostic location for bound nodes.\n" " detailed-ast " "Detailed AST output for bound nodes.\n" +" srcloc" +"Source locations and ranges for bound nodes.\n" " dump " "Detailed AST output for bound nodes (alias of detailed-ast).\n\n"; return true; @@ -86,6 +89,88 @@ } }; +void dumpLocations(llvm::raw_ostream , DynTypedNode Node, ASTContext , + const DiagnosticsEngine , SourceManager const ) { + auto Locs = clang::tooling::NodeIntrospection::GetLocations(Node); + + auto printLocations = [](auto , auto Iter, auto End) { +auto CommonEntry = Iter->first; +auto Scout = Iter; +while (Scout->first == CommonEntry) { + OS << " * " + << "\"" + +tooling::LocationCallFormatterCpp::format(Iter->second.get()) + +"\"\n"; + if (Scout == End) +break; + ++Scout; + if (Scout->first == CommonEntry) { +++Iter; + } +} + }; + + for (auto Iter = Locs.LocationAccessors.begin(); + Iter != Locs.LocationAccessors.end(); ++Iter) { +if (!Iter->first.isValid()) + continue; + +TextDiagnostic TD(OS, Ctx.getLangOpts(), ()); +TD.emitDiagnostic(FullSourceLoc(Iter->first, SM), DiagnosticsEngine::Note, + "source locations here", None, None); + +printLocations(OS, Iter, Locs.LocationAccessors.end()); +OS << '\n'; + } + + for (auto Iter = Locs.RangeAccessors.begin(); + Iter != Locs.RangeAccessors.end(); ++Iter) { + +if (!Iter->first.getBegin().isValid()) + continue; + +if (SM.getPresumedLineNumber(Iter->first.getBegin()) != +SM.getPresumedLineNumber(Iter->first.getEnd())) + continue; + +TextDiagnostic TD(OS, Ctx.getLangOpts(), ()); +TD.emitDiagnostic(FullSourceLoc(Iter->first.getBegin(), SM), + DiagnosticsEngine::Note,
[PATCH] D93325: Add srcloc output to clang-query
aaron.ballman added a comment. Thank you for this, it looks really useful! Comment at: clang-tools-extra/clang-query/Query.cpp:94 +void dumpLocations(llvm::raw_ostream , ast_type_traits::DynTypedNode node, + ASTContext , const DiagnosticsEngine , Comment at: clang-tools-extra/clang-query/Query.cpp:97 + SourceManager const ) { + auto locs = clang::tooling::NodeLocationIntrospection::GetLocations(node); + It should be named `Locs` per the usual coding conventions. (I'll hold my nose on the use of `auto` here, but if it got spelled out since you're touching the line anyway, that would not be a terrible thing.) Comment at: clang-tools-extra/clang-query/Query.cpp:99 + + for (auto it = locs.LocationAccessors.begin(); + it != locs.LocationAccessors.end(); ++it) { `it` -> `Iter` Comment at: clang-tools-extra/clang-query/Query.cpp:106 +TD.emitDiagnostic(FullSourceLoc(it->first, SM), DiagnosticsEngine::Note, + "SourceLocations here", None, None); + `SourceLocations` -> `source locations` ? Comment at: clang-tools-extra/clang-query/Query.cpp:108 + +auto commonLoc = it->first; +auto scout = it; `CommonLoc` and the type should definitely be spelled out. Comment at: clang-tools-extra/clang-query/Query.cpp:109 +auto commonLoc = it->first; +auto scout = it; +while (scout->first == commonLoc) { `Scout` Comment at: clang-tools-extra/clang-query/Query.cpp:123 + + for (auto it = locs.RangeAccessors.begin(); it != locs.RangeAccessors.end(); + ++it) { `it` -> `Iter` Comment at: clang-tools-extra/clang-query/Query.cpp:136 + DiagnosticsEngine::Note, + "SourceRanges here " + it->first.printToString(SM), + CharSourceRange::getTokenRange(it->first), None); `SourceRanges` -> `source ranges` ? Comment at: clang-tools-extra/clang-query/Query.cpp:139-140 + +auto commonLoc = it->first; +auto scout = it; +while (scout->first == commonLoc) { Same here as above. Given how common this code seems to be with the first block of code, would it make sense to turn some of this code into a lambda? Comment at: clang-tools-extra/clang-query/Query.cpp:152 + } + for (auto it = locs.RangeAccessors.begin(); it != locs.RangeAccessors.end(); + ++it) { `it` -> `Iter` Comment at: clang-tools-extra/clang-query/Query.cpp:165 +FullSourceLoc(it->first.getBegin(), SM), DiagnosticsEngine::Note, +"SourceRange " + it->first.printToString(SM) + " starting here...", +CharSourceRange::getTokenRange(it->first), None); `SourceRange` -> `source range` ? Comment at: clang-tools-extra/clang-query/Query.cpp:168-169 + +auto colNum = SM.getPresumedColumnNumber(it->first.getEnd()); +auto lastLineLoc = it->first.getEnd().getLocWithOffset(-(colNum - 1)); + More names to correct for conventions. Can you also spell out at least the location datatype? Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D93325/new/ https://reviews.llvm.org/D93325 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D93325: Add srcloc output to clang-query
steveire created this revision. steveire added a reviewer: aaron.ballman. steveire requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D93325 Files: clang-tools-extra/clang-query/Query.cpp clang-tools-extra/clang-query/Query.h clang-tools-extra/clang-query/QueryParser.cpp clang-tools-extra/clang-query/QuerySession.h Index: clang-tools-extra/clang-query/QuerySession.h === --- clang-tools-extra/clang-query/QuerySession.h +++ clang-tools-extra/clang-query/QuerySession.h @@ -25,14 +25,15 @@ public: QuerySession(llvm::ArrayRef> ASTs) : ASTs(ASTs), PrintOutput(false), DiagOutput(true), -DetailedASTOutput(false), BindRoot(true), PrintMatcher(false), -Terminate(false), TK(ast_type_traits::TK_AsIs) {} +DetailedASTOutput(false), SrcLocOutput(false), BindRoot(true), +PrintMatcher(false), Terminate(false), TK(ast_type_traits::TK_AsIs) {} llvm::ArrayRef> ASTs; bool PrintOutput; bool DiagOutput; bool DetailedASTOutput; + bool SrcLocOutput; bool BindRoot; bool PrintMatcher; Index: clang-tools-extra/clang-query/QueryParser.cpp === --- clang-tools-extra/clang-query/QueryParser.cpp +++ clang-tools-extra/clang-query/QueryParser.cpp @@ -108,6 +108,7 @@ .Case("diag", OK_Diag) .Case("print", OK_Print) .Case("detailed-ast", OK_DetailedAST) + .Case("srcloc", OK_SrcLoc) .Case("dump", OK_DetailedAST) .Default(~0u); if (OutKind == ~0u) { @@ -123,6 +124,8 @@ return new QueryType(::DiagOutput); case OK_Print: return new QueryType(::PrintOutput); + case OK_SrcLoc: +return new QueryType(::SrcLocOutput); } llvm_unreachable("Invalid output kind"); Index: clang-tools-extra/clang-query/Query.h === --- clang-tools-extra/clang-query/Query.h +++ clang-tools-extra/clang-query/Query.h @@ -18,7 +18,7 @@ namespace clang { namespace query { -enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST }; +enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST, OK_SrcLoc }; enum QueryKind { QK_Invalid, Index: clang-tools-extra/clang-query/Query.cpp === --- clang-tools-extra/clang-query/Query.cpp +++ clang-tools-extra/clang-query/Query.cpp @@ -12,6 +12,7 @@ #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/TextDiagnostic.h" +#include "clang/Tooling/NodeLocationIntrospection.h" #include "llvm/Support/raw_ostream.h" using namespace clang::ast_matchers; @@ -68,6 +69,8 @@ "Diagnostic location for bound nodes.\n" " detailed-ast " "Detailed AST output for bound nodes.\n" +" srcloc" +"Source locations and ranges for bound nodes.\n" " dump " "Detailed AST output for bound nodes (alias of detailed-ast).\n\n"; return true; @@ -88,6 +91,105 @@ } }; +void dumpLocations(llvm::raw_ostream , ast_type_traits::DynTypedNode node, + ASTContext , const DiagnosticsEngine , + SourceManager const ) { + auto locs = clang::tooling::NodeLocationIntrospection::GetLocations(node); + + for (auto it = locs.LocationAccessors.begin(); + it != locs.LocationAccessors.end(); ++it) { +if (!it->first.isValid()) + continue; + +TextDiagnostic TD(OS, Ctx.getLangOpts(), ()); +TD.emitDiagnostic(FullSourceLoc(it->first, SM), DiagnosticsEngine::Note, + "SourceLocations here", None, None); + +auto commonLoc = it->first; +auto scout = it; +while (scout->first == commonLoc) { + OS << " * " + << "\"" + it->second + "()\"\n"; + if (scout == locs.LocationAccessors.end()) +break; + ++scout; + if (scout->first == commonLoc) { +++it; + } +} +OS << '\n'; + } + + for (auto it = locs.RangeAccessors.begin(); it != locs.RangeAccessors.end(); + ++it) { + +if (!it->first.getBegin().isValid()) + continue; + +if (SM.getPresumedLineNumber(it->first.getBegin()) != +SM.getPresumedLineNumber(it->first.getEnd())) + continue; + +TextDiagnostic TD(OS, Ctx.getLangOpts(), ()); +TD.emitDiagnostic(FullSourceLoc(it->first.getBegin(), SM), + DiagnosticsEngine::Note, + "SourceRanges here " + it->first.printToString(SM), + CharSourceRange::getTokenRange(it->first), None); + +auto commonLoc = it->first; +auto scout = it; +while