Skip to content

Commit 371eccd

Browse files
authored
[clang-query] Remove support for srcloc output (#92442)
This functionality was added about three years ago, but has been in a significantly broken state since it was added. It has begun to cause a maintenance burden for work in Clang (largely due to the complexity of having two levels of code generation involved), and the original author is unable to help maintain it. Because it only worked under limited circumstances and because of the maintenance burden, it is being removed. If someone wishes to resurrect the functionality, they should hopefully be able to do so from this one commit. Fixes #82591
1 parent a68d20e commit 371eccd

File tree

25 files changed

+22
-3213
lines changed

25 files changed

+22
-3213
lines changed

clang-tools-extra/clang-query/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ clang_target_link_libraries(clangQuery
2020
clangBasic
2121
clangDynamicASTMatchers
2222
clangFrontend
23-
clangTooling
2423
clangSerialization
2524
)
2625

clang-tools-extra/clang-query/Query.cpp

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "clang/ASTMatchers/ASTMatchFinder.h"
1414
#include "clang/Frontend/ASTUnit.h"
1515
#include "clang/Frontend/TextDiagnostic.h"
16-
#include "clang/Tooling/NodeIntrospection.h"
1716
#include "llvm/Support/raw_ostream.h"
1817
#include <optional>
1918

@@ -69,8 +68,6 @@ bool HelpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
6968
"Diagnostic location for bound nodes.\n"
7069
" detailed-ast "
7170
"Detailed AST output for bound nodes.\n"
72-
" srcloc "
73-
"Source locations and ranges for bound nodes.\n"
7471
" dump "
7572
"Detailed AST output for bound nodes (alias of detailed-ast).\n\n";
7673
return true;
@@ -91,90 +88,6 @@ struct CollectBoundNodes : MatchFinder::MatchCallback {
9188
}
9289
};
9390

94-
void dumpLocations(llvm::raw_ostream &OS, DynTypedNode Node, ASTContext &Ctx,
95-
const DiagnosticsEngine &Diags, SourceManager const &SM) {
96-
auto Locs = clang::tooling::NodeIntrospection::GetLocations(Node);
97-
98-
auto PrintLocations = [](llvm::raw_ostream &OS, auto Iter, auto End) {
99-
auto CommonEntry = Iter->first;
100-
auto Scout = Iter;
101-
SmallVector<std::string> LocationStrings;
102-
while (Scout->first == CommonEntry) {
103-
LocationStrings.push_back(
104-
tooling::LocationCallFormatterCpp::format(*Iter->second));
105-
if (Scout == End)
106-
break;
107-
++Scout;
108-
if (Scout->first == CommonEntry)
109-
++Iter;
110-
}
111-
llvm::sort(LocationStrings);
112-
for (auto &LS : LocationStrings) {
113-
OS << " * \"" << LS << "\"\n";
114-
}
115-
return Iter;
116-
};
117-
118-
TextDiagnostic TD(OS, Ctx.getLangOpts(), &Diags.getDiagnosticOptions());
119-
120-
for (auto Iter = Locs.LocationAccessors.begin();
121-
Iter != Locs.LocationAccessors.end(); ++Iter) {
122-
if (!Iter->first.isValid())
123-
continue;
124-
125-
TD.emitDiagnostic(FullSourceLoc(Iter->first, SM), DiagnosticsEngine::Note,
126-
"source locations here", std::nullopt, std::nullopt);
127-
128-
Iter = PrintLocations(OS, Iter, Locs.LocationAccessors.end());
129-
OS << '\n';
130-
}
131-
132-
for (auto Iter = Locs.RangeAccessors.begin();
133-
Iter != Locs.RangeAccessors.end(); ++Iter) {
134-
135-
if (!Iter->first.getBegin().isValid())
136-
continue;
137-
138-
if (SM.getPresumedLineNumber(Iter->first.getBegin()) !=
139-
SM.getPresumedLineNumber(Iter->first.getEnd()))
140-
continue;
141-
142-
TD.emitDiagnostic(
143-
FullSourceLoc(Iter->first.getBegin(), SM), DiagnosticsEngine::Note,
144-
"source ranges here " + Iter->first.printToString(SM),
145-
CharSourceRange::getTokenRange(Iter->first), std::nullopt);
146-
147-
Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end());
148-
}
149-
for (auto Iter = Locs.RangeAccessors.begin();
150-
Iter != Locs.RangeAccessors.end(); ++Iter) {
151-
152-
if (!Iter->first.getBegin().isValid())
153-
continue;
154-
155-
if (SM.getPresumedLineNumber(Iter->first.getBegin()) ==
156-
SM.getPresumedLineNumber(Iter->first.getEnd()))
157-
continue;
158-
159-
TD.emitDiagnostic(
160-
FullSourceLoc(Iter->first.getBegin(), SM), DiagnosticsEngine::Note,
161-
"source range " + Iter->first.printToString(SM) + " starting here...",
162-
CharSourceRange::getTokenRange(Iter->first), std::nullopt);
163-
164-
auto ColNum = SM.getPresumedColumnNumber(Iter->first.getEnd());
165-
auto LastLineLoc = Iter->first.getEnd().getLocWithOffset(-(ColNum - 1));
166-
167-
TD.emitDiagnostic(FullSourceLoc(Iter->first.getEnd(), SM),
168-
DiagnosticsEngine::Note, "... ending here",
169-
CharSourceRange::getTokenRange(
170-
SourceRange(LastLineLoc, Iter->first.getEnd())),
171-
std::nullopt);
172-
173-
Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end());
174-
}
175-
OS << "\n";
176-
}
177-
17891
} // namespace
17992

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

198-
auto &Ctx = AST->getASTContext();
199-
const auto &SM = Ctx.getSourceManager();
111+
ASTContext &Ctx = AST->getASTContext();
200112
Ctx.getParentMapContext().setTraversalKind(QS.TK);
201113
Finder.matchAST(Ctx);
202114

@@ -244,19 +156,11 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
244156
}
245157
if (QS.DetailedASTOutput) {
246158
OS << "Binding for \"" << BI->first << "\":\n";
247-
const ASTContext &Ctx = AST->getASTContext();
248159
ASTDumper Dumper(OS, Ctx, AST->getDiagnostics().getShowColors());
249160
Dumper.SetTraversalKind(QS.TK);
250161
Dumper.Visit(BI->second);
251162
OS << "\n";
252163
}
253-
if (QS.SrcLocOutput) {
254-
OS << "\n \"" << BI->first << "\" Source locations\n";
255-
OS << " " << std::string(19 + BI->first.size(), '-') << '\n';
256-
257-
dumpLocations(OS, BI->second, Ctx, AST->getDiagnostics(), SM);
258-
OS << "\n";
259-
}
260164
}
261165

262166
if (MI->getMap().empty())

clang-tools-extra/clang-query/Query.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
namespace clang {
1818
namespace query {
1919

20-
enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST, OK_SrcLoc };
20+
enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST };
2121

2222
enum QueryKind {
2323
QK_Invalid,
@@ -149,7 +149,6 @@ struct SetExclusiveOutputQuery : Query {
149149
QS.DiagOutput = false;
150150
QS.DetailedASTOutput = false;
151151
QS.PrintOutput = false;
152-
QS.SrcLocOutput = false;
153152
QS.*Var = true;
154153
return true;
155154
}

clang-tools-extra/clang-query/QueryParser.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "QuerySession.h"
1212
#include "clang/ASTMatchers/Dynamic/Parser.h"
1313
#include "clang/Basic/CharInfo.h"
14-
#include "clang/Tooling/NodeIntrospection.h"
1514
#include "llvm/ADT/StringRef.h"
1615
#include "llvm/ADT/StringSwitch.h"
1716
#include <optional>
@@ -104,19 +103,16 @@ QueryRef QueryParser::parseSetBool(bool QuerySession::*Var) {
104103

105104
template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() {
106105
StringRef ValStr;
107-
bool HasIntrospection = tooling::NodeIntrospection::hasIntrospectionSupport();
108-
unsigned OutKind =
109-
LexOrCompleteWord<unsigned>(this, ValStr)
110-
.Case("diag", OK_Diag)
111-
.Case("print", OK_Print)
112-
.Case("detailed-ast", OK_DetailedAST)
113-
.Case("srcloc", OK_SrcLoc, /*IsCompletion=*/HasIntrospection)
114-
.Case("dump", OK_DetailedAST)
115-
.Default(~0u);
106+
unsigned OutKind = LexOrCompleteWord<unsigned>(this, ValStr)
107+
.Case("diag", OK_Diag)
108+
.Case("print", OK_Print)
109+
.Case("detailed-ast", OK_DetailedAST)
110+
.Case("dump", OK_DetailedAST)
111+
.Default(~0u);
116112
if (OutKind == ~0u) {
117-
return new InvalidQuery("expected 'diag', 'print', 'detailed-ast'" +
118-
StringRef(HasIntrospection ? ", 'srcloc'" : "") +
119-
" or 'dump', got '" + ValStr + "'");
113+
return new InvalidQuery("expected 'diag', 'print', 'detailed-ast' or "
114+
"'dump', got '" +
115+
ValStr + "'");
120116
}
121117

122118
switch (OutKind) {
@@ -126,10 +122,6 @@ template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() {
126122
return new QueryType(&QuerySession::DiagOutput);
127123
case OK_Print:
128124
return new QueryType(&QuerySession::PrintOutput);
129-
case OK_SrcLoc:
130-
if (HasIntrospection)
131-
return new QueryType(&QuerySession::SrcLocOutput);
132-
return new InvalidQuery("'srcloc' output support is not available.");
133125
}
134126

135127
llvm_unreachable("Invalid output kind");

clang-tools-extra/clang-query/QuerySession.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ class QuerySession {
2525
public:
2626
QuerySession(llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs)
2727
: ASTs(ASTs), PrintOutput(false), DiagOutput(true),
28-
DetailedASTOutput(false), SrcLocOutput(false), BindRoot(true),
29-
PrintMatcher(false), Terminate(false), TK(TK_AsIs) {}
28+
DetailedASTOutput(false), BindRoot(true), PrintMatcher(false),
29+
Terminate(false), TK(TK_AsIs) {}
3030

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

3333
bool PrintOutput;
3434
bool DiagOutput;
3535
bool DetailedASTOutput;
36-
bool SrcLocOutput;
3736

3837
bool BindRoot;
3938
bool PrintMatcher;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ Improvements to clang-query
9494
from an external file, allowing the cost of reading the compilation database
9595
and building the AST to be imposed just once for faster prototyping.
9696

97+
- Removed support for ``enable output srcloc``. Fixes #GH82591
98+
9799
Improvements to clang-rename
98100
----------------------------
99101

clang-tools-extra/unittests/clang-query/QueryParserTest.cpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "QueryParser.h"
1010
#include "Query.h"
1111
#include "QuerySession.h"
12-
#include "clang/Tooling/NodeIntrospection.h"
1312
#include "llvm/LineEditor/LineEditor.h"
1413
#include "gtest/gtest.h"
1514

@@ -61,7 +60,6 @@ TEST_F(QueryParserTest, Quit) {
6160

6261
TEST_F(QueryParserTest, Set) {
6362

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

7371
Q = parse("set output");
7472
ASSERT_TRUE(isa<InvalidQuery>(Q));
75-
if (HasIntrospection)
76-
EXPECT_EQ(
77-
"expected 'diag', 'print', 'detailed-ast', 'srcloc' or 'dump', got ''",
78-
cast<InvalidQuery>(Q)->ErrStr);
79-
else
80-
EXPECT_EQ("expected 'diag', 'print', 'detailed-ast' or 'dump', got ''",
81-
cast<InvalidQuery>(Q)->ErrStr);
73+
EXPECT_EQ("expected 'diag', 'print', 'detailed-ast' or 'dump', got ''",
74+
cast<InvalidQuery>(Q)->ErrStr);
8275

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

8780
Q = parse("set output foo");
8881
ASSERT_TRUE(isa<InvalidQuery>(Q));
89-
if (HasIntrospection)
90-
EXPECT_EQ("expected 'diag', 'print', 'detailed-ast', 'srcloc' or 'dump', "
91-
"got 'foo'",
92-
cast<InvalidQuery>(Q)->ErrStr);
93-
else
94-
EXPECT_EQ("expected 'diag', 'print', 'detailed-ast' or 'dump', got 'foo'",
95-
cast<InvalidQuery>(Q)->ErrStr);
82+
EXPECT_EQ("expected 'diag', 'print', 'detailed-ast' or 'dump', got 'foo'",
83+
cast<InvalidQuery>(Q)->ErrStr);
9684

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

235-
bool HasIntrospection = tooling::NodeIntrospection::hasIntrospectionSupport();
236-
237223
Comps = QueryParser::complete("enable output ", 14, QS);
238-
ASSERT_EQ(HasIntrospection ? 5u : 4u, Comps.size());
224+
ASSERT_EQ(4u, Comps.size());
239225

240226
EXPECT_EQ("diag ", Comps[0].TypedText);
241227
EXPECT_EQ("diag", Comps[0].DisplayText);
242228
EXPECT_EQ("print ", Comps[1].TypedText);
243229
EXPECT_EQ("print", Comps[1].DisplayText);
244230
EXPECT_EQ("detailed-ast ", Comps[2].TypedText);
245231
EXPECT_EQ("detailed-ast", Comps[2].DisplayText);
246-
if (HasIntrospection) {
247-
EXPECT_EQ("srcloc ", Comps[3].TypedText);
248-
EXPECT_EQ("srcloc", Comps[3].DisplayText);
249-
}
250-
EXPECT_EQ("dump ", Comps[HasIntrospection ? 4 : 3].TypedText);
251-
EXPECT_EQ("dump", Comps[HasIntrospection ? 4 : 3].DisplayText);
232+
EXPECT_EQ("dump ", Comps[3].TypedText);
233+
EXPECT_EQ("dump", Comps[3].DisplayText);
252234

253235
Comps = QueryParser::complete("set traversal ", 14, QS);
254236
ASSERT_EQ(2u, Comps.size());

clang/docs/tools/clang-formatted-files.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ clang/include/clang/Tooling/CompilationDatabasePluginRegistry.h
252252
clang/include/clang/Tooling/DiagnosticsYaml.h
253253
clang/include/clang/Tooling/Execution.h
254254
clang/include/clang/Tooling/JSONCompilationDatabase.h
255-
clang/include/clang/Tooling/NodeIntrospection.h
256255
clang/include/clang/Tooling/Refactoring.h
257256
clang/include/clang/Tooling/StandaloneExecution.h
258257
clang/include/clang/Tooling/ToolExecutorPluginRegistry.h
@@ -562,15 +561,11 @@ clang/lib/Tooling/Execution.cpp
562561
clang/lib/Tooling/ExpandResponseFilesCompilationDatabase.cpp
563562
clang/lib/Tooling/FixIt.cpp
564563
clang/lib/Tooling/GuessTargetAndModeCompilationDatabase.cpp
565-
clang/lib/Tooling/NodeIntrospection.cpp
566564
clang/lib/Tooling/StandaloneExecution.cpp
567565
clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
568566
clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp
569567
clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
570568
clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
571-
clang/lib/Tooling/DumpTool/APIData.h
572-
clang/lib/Tooling/DumpTool/ASTSrcLocProcessor.h
573-
clang/lib/Tooling/DumpTool/ClangSrcLocDump.cpp
574569
clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
575570
clang/lib/Tooling/Inclusions/IncludeStyle.cpp
576571
clang/lib/Tooling/Inclusions/StandardLibrary.cpp

0 commit comments

Comments
 (0)