Skip to content

[clang-query] Remove support for srcloc output #92442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion clang-tools-extra/clang-query/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ clang_target_link_libraries(clangQuery
clangBasic
clangDynamicASTMatchers
clangFrontend
clangTooling
clangSerialization
)

Expand Down
98 changes: 1 addition & 97 deletions clang-tools-extra/clang-query/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#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"
#include <optional>

Expand Down Expand Up @@ -69,8 +68,6 @@ bool HelpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
"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;
Expand All @@ -91,90 +88,6 @@ struct CollectBoundNodes : MatchFinder::MatchCallback {
}
};

void dumpLocations(llvm::raw_ostream &OS, DynTypedNode Node, ASTContext &Ctx,
const DiagnosticsEngine &Diags, SourceManager const &SM) {
auto Locs = clang::tooling::NodeIntrospection::GetLocations(Node);

auto PrintLocations = [](llvm::raw_ostream &OS, auto Iter, auto End) {
auto CommonEntry = Iter->first;
auto Scout = Iter;
SmallVector<std::string> LocationStrings;
while (Scout->first == CommonEntry) {
LocationStrings.push_back(
tooling::LocationCallFormatterCpp::format(*Iter->second));
if (Scout == End)
break;
++Scout;
if (Scout->first == CommonEntry)
++Iter;
}
llvm::sort(LocationStrings);
for (auto &LS : LocationStrings) {
OS << " * \"" << LS << "\"\n";
}
return Iter;
};

TextDiagnostic TD(OS, Ctx.getLangOpts(), &Diags.getDiagnosticOptions());

for (auto Iter = Locs.LocationAccessors.begin();
Iter != Locs.LocationAccessors.end(); ++Iter) {
if (!Iter->first.isValid())
continue;

TD.emitDiagnostic(FullSourceLoc(Iter->first, SM), DiagnosticsEngine::Note,
"source locations here", std::nullopt, std::nullopt);

Iter = 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;

TD.emitDiagnostic(
FullSourceLoc(Iter->first.getBegin(), SM), DiagnosticsEngine::Note,
"source ranges here " + Iter->first.printToString(SM),
CharSourceRange::getTokenRange(Iter->first), std::nullopt);

Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end());
}
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;

TD.emitDiagnostic(
FullSourceLoc(Iter->first.getBegin(), SM), DiagnosticsEngine::Note,
"source range " + Iter->first.printToString(SM) + " starting here...",
CharSourceRange::getTokenRange(Iter->first), std::nullopt);

auto ColNum = SM.getPresumedColumnNumber(Iter->first.getEnd());
auto LastLineLoc = Iter->first.getEnd().getLocWithOffset(-(ColNum - 1));

TD.emitDiagnostic(FullSourceLoc(Iter->first.getEnd(), SM),
DiagnosticsEngine::Note, "... ending here",
CharSourceRange::getTokenRange(
SourceRange(LastLineLoc, Iter->first.getEnd())),
std::nullopt);

Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end());
}
OS << "\n";
}

} // namespace

bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
Expand All @@ -195,8 +108,7 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
return false;
}

auto &Ctx = AST->getASTContext();
const auto &SM = Ctx.getSourceManager();
ASTContext &Ctx = AST->getASTContext();
Ctx.getParentMapContext().setTraversalKind(QS.TK);
Finder.matchAST(Ctx);

Expand Down Expand Up @@ -244,19 +156,11 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
}
if (QS.DetailedASTOutput) {
OS << "Binding for \"" << BI->first << "\":\n";
const ASTContext &Ctx = AST->getASTContext();
ASTDumper Dumper(OS, Ctx, AST->getDiagnostics().getShowColors());
Dumper.SetTraversalKind(QS.TK);
Dumper.Visit(BI->second);
OS << "\n";
}
if (QS.SrcLocOutput) {
OS << "\n \"" << BI->first << "\" Source locations\n";
OS << " " << std::string(19 + BI->first.size(), '-') << '\n';

dumpLocations(OS, BI->second, Ctx, AST->getDiagnostics(), SM);
OS << "\n";
}
}

if (MI->getMap().empty())
Expand Down
3 changes: 1 addition & 2 deletions clang-tools-extra/clang-query/Query.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace clang {
namespace query {

enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST, OK_SrcLoc };
enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST };

enum QueryKind {
QK_Invalid,
Expand Down Expand Up @@ -149,7 +149,6 @@ struct SetExclusiveOutputQuery : Query {
QS.DiagOutput = false;
QS.DetailedASTOutput = false;
QS.PrintOutput = false;
QS.SrcLocOutput = false;
QS.*Var = true;
return true;
}
Expand Down
26 changes: 9 additions & 17 deletions clang-tools-extra/clang-query/QueryParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "QuerySession.h"
#include "clang/ASTMatchers/Dynamic/Parser.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Tooling/NodeIntrospection.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include <optional>
Expand Down Expand Up @@ -104,19 +103,16 @@ QueryRef QueryParser::parseSetBool(bool QuerySession::*Var) {

template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() {
StringRef ValStr;
bool HasIntrospection = tooling::NodeIntrospection::hasIntrospectionSupport();
unsigned OutKind =
LexOrCompleteWord<unsigned>(this, ValStr)
.Case("diag", OK_Diag)
.Case("print", OK_Print)
.Case("detailed-ast", OK_DetailedAST)
.Case("srcloc", OK_SrcLoc, /*IsCompletion=*/HasIntrospection)
.Case("dump", OK_DetailedAST)
.Default(~0u);
unsigned OutKind = LexOrCompleteWord<unsigned>(this, ValStr)
.Case("diag", OK_Diag)
.Case("print", OK_Print)
.Case("detailed-ast", OK_DetailedAST)
.Case("dump", OK_DetailedAST)
.Default(~0u);
if (OutKind == ~0u) {
return new InvalidQuery("expected 'diag', 'print', 'detailed-ast'" +
StringRef(HasIntrospection ? ", 'srcloc'" : "") +
" or 'dump', got '" + ValStr + "'");
return new InvalidQuery("expected 'diag', 'print', 'detailed-ast' or "
"'dump', got '" +
ValStr + "'");
}

switch (OutKind) {
Expand All @@ -126,10 +122,6 @@ template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() {
return new QueryType(&QuerySession::DiagOutput);
case OK_Print:
return new QueryType(&QuerySession::PrintOutput);
case OK_SrcLoc:
if (HasIntrospection)
return new QueryType(&QuerySession::SrcLocOutput);
return new InvalidQuery("'srcloc' output support is not available.");
}

llvm_unreachable("Invalid output kind");
Expand Down
5 changes: 2 additions & 3 deletions clang-tools-extra/clang-query/QuerySession.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ class QuerySession {
public:
QuerySession(llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs)
: ASTs(ASTs), PrintOutput(false), DiagOutput(true),
DetailedASTOutput(false), SrcLocOutput(false), BindRoot(true),
PrintMatcher(false), Terminate(false), TK(TK_AsIs) {}
DetailedASTOutput(false), BindRoot(true), PrintMatcher(false),
Terminate(false), TK(TK_AsIs) {}

llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs;

bool PrintOutput;
bool DiagOutput;
bool DetailedASTOutput;
bool SrcLocOutput;

bool BindRoot;
bool PrintMatcher;
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ Improvements to clang-query
from an external file, allowing the cost of reading the compilation database
and building the AST to be imposed just once for faster prototyping.

- Removed support for ``enable output srcloc``. Fixes #GH82591

Improvements to clang-rename
----------------------------

Expand Down
32 changes: 7 additions & 25 deletions clang-tools-extra/unittests/clang-query/QueryParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "QueryParser.h"
#include "Query.h"
#include "QuerySession.h"
#include "clang/Tooling/NodeIntrospection.h"
#include "llvm/LineEditor/LineEditor.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -61,7 +60,6 @@ TEST_F(QueryParserTest, Quit) {

TEST_F(QueryParserTest, Set) {

bool HasIntrospection = tooling::NodeIntrospection::hasIntrospectionSupport();
QueryRef Q = parse("set");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("expected variable name", cast<InvalidQuery>(Q)->ErrStr);
Expand All @@ -72,27 +70,17 @@ TEST_F(QueryParserTest, Set) {

Q = parse("set output");
ASSERT_TRUE(isa<InvalidQuery>(Q));
if (HasIntrospection)
EXPECT_EQ(
"expected 'diag', 'print', 'detailed-ast', 'srcloc' or 'dump', got ''",
cast<InvalidQuery>(Q)->ErrStr);
else
EXPECT_EQ("expected 'diag', 'print', 'detailed-ast' or 'dump', got ''",
cast<InvalidQuery>(Q)->ErrStr);
EXPECT_EQ("expected 'diag', 'print', 'detailed-ast' or 'dump', got ''",
cast<InvalidQuery>(Q)->ErrStr);

Q = parse("set bind-root true foo");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("unexpected extra input: ' foo'", cast<InvalidQuery>(Q)->ErrStr);

Q = parse("set output foo");
ASSERT_TRUE(isa<InvalidQuery>(Q));
if (HasIntrospection)
EXPECT_EQ("expected 'diag', 'print', 'detailed-ast', 'srcloc' or 'dump', "
"got 'foo'",
cast<InvalidQuery>(Q)->ErrStr);
else
EXPECT_EQ("expected 'diag', 'print', 'detailed-ast' or 'dump', got 'foo'",
cast<InvalidQuery>(Q)->ErrStr);
EXPECT_EQ("expected 'diag', 'print', 'detailed-ast' or 'dump', got 'foo'",
cast<InvalidQuery>(Q)->ErrStr);

Q = parse("set output dump");
ASSERT_TRUE(isa<SetExclusiveOutputQuery >(Q));
Expand Down Expand Up @@ -232,23 +220,17 @@ TEST_F(QueryParserTest, Complete) {
EXPECT_EQ("output ", Comps[0].TypedText);
EXPECT_EQ("output", Comps[0].DisplayText);

bool HasIntrospection = tooling::NodeIntrospection::hasIntrospectionSupport();

Comps = QueryParser::complete("enable output ", 14, QS);
ASSERT_EQ(HasIntrospection ? 5u : 4u, Comps.size());
ASSERT_EQ(4u, Comps.size());

EXPECT_EQ("diag ", Comps[0].TypedText);
EXPECT_EQ("diag", Comps[0].DisplayText);
EXPECT_EQ("print ", Comps[1].TypedText);
EXPECT_EQ("print", Comps[1].DisplayText);
EXPECT_EQ("detailed-ast ", Comps[2].TypedText);
EXPECT_EQ("detailed-ast", Comps[2].DisplayText);
if (HasIntrospection) {
EXPECT_EQ("srcloc ", Comps[3].TypedText);
EXPECT_EQ("srcloc", Comps[3].DisplayText);
}
EXPECT_EQ("dump ", Comps[HasIntrospection ? 4 : 3].TypedText);
EXPECT_EQ("dump", Comps[HasIntrospection ? 4 : 3].DisplayText);
EXPECT_EQ("dump ", Comps[3].TypedText);
EXPECT_EQ("dump", Comps[3].DisplayText);

Comps = QueryParser::complete("set traversal ", 14, QS);
ASSERT_EQ(2u, Comps.size());
Expand Down
5 changes: 0 additions & 5 deletions clang/docs/tools/clang-formatted-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ clang/include/clang/Tooling/CompilationDatabasePluginRegistry.h
clang/include/clang/Tooling/DiagnosticsYaml.h
clang/include/clang/Tooling/Execution.h
clang/include/clang/Tooling/JSONCompilationDatabase.h
clang/include/clang/Tooling/NodeIntrospection.h
clang/include/clang/Tooling/Refactoring.h
clang/include/clang/Tooling/StandaloneExecution.h
clang/include/clang/Tooling/ToolExecutorPluginRegistry.h
Expand Down Expand Up @@ -562,15 +561,11 @@ clang/lib/Tooling/Execution.cpp
clang/lib/Tooling/ExpandResponseFilesCompilationDatabase.cpp
clang/lib/Tooling/FixIt.cpp
clang/lib/Tooling/GuessTargetAndModeCompilationDatabase.cpp
clang/lib/Tooling/NodeIntrospection.cpp
clang/lib/Tooling/StandaloneExecution.cpp
clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp
clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
clang/lib/Tooling/DumpTool/APIData.h
clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.h
clang/lib/Tooling/DumpTool/ClangSrcLocDump.cpp
clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
clang/lib/Tooling/Inclusions/IncludeStyle.cpp
clang/lib/Tooling/Inclusions/StandardLibrary.cpp
Expand Down
Loading
Loading