Skip to content

Commit eed4a4d

Browse files
committed
[clang-tidy] Correct the include-cleaner-check diagnostic message for missing-includes.
We should print the symbol name rather than the header name in the message. Differential Revision: https://reviews.llvm.org/D153013
1 parent 015323f commit eed4a4d

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace clang::tidy::misc {
4545

4646
namespace {
4747
struct MissingIncludeInfo {
48-
SourceLocation SymRefLocation;
48+
include_cleaner::SymbolReference SymRef;
4949
include_cleaner::Header Missing;
5050
};
5151
} // namespace
@@ -134,7 +134,7 @@ void IncludeCleanerCheck::check(const MatchFinder::MatchResult &Result) {
134134
if (!Satisfied && !Providers.empty() &&
135135
Ref.RT == include_cleaner::RefType::Explicit &&
136136
!shouldIgnore(Providers.front()))
137-
Missing.push_back({Ref.RefLocation, Providers.front()});
137+
Missing.push_back({Ref, Providers.front()});
138138
});
139139

140140
std::vector<const include_cleaner::Include *> Unused;
@@ -190,9 +190,9 @@ void IncludeCleanerCheck::check(const MatchFinder::MatchResult &Result) {
190190
if (auto Replacement =
191191
HeaderIncludes.insert(llvm::StringRef{Spelling}.trim("\"<>"),
192192
Angled, tooling::IncludeDirective::Include))
193-
diag(SM->getSpellingLoc(Inc.SymRefLocation),
194-
"no header providing %0 is directly included")
195-
<< Spelling
193+
diag(SM->getSpellingLoc(Inc.SymRef.RefLocation),
194+
"no header providing \"%0\" is directly included")
195+
<< Inc.SymRef.Target.name()
196196
<< FixItHint::CreateInsertion(
197197
SM->getComposedLoc(SM->getMainFileID(),
198198
Replacement->getOffset()),

clang-tools-extra/clangd/IncludeCleaner.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,6 @@ llvm::StringRef getResolvedPath(const include_cleaner::Header &SymProvider) {
144144
llvm_unreachable("Unknown header kind");
145145
}
146146

147-
std::string getSymbolName(const include_cleaner::Symbol &Sym) {
148-
switch (Sym.kind()) {
149-
case include_cleaner::Symbol::Macro:
150-
return Sym.macro().Name->getName().str();
151-
case include_cleaner::Symbol::Declaration:
152-
return llvm::dyn_cast<NamedDecl>(&Sym.declaration())
153-
->getQualifiedNameAsString();
154-
}
155-
llvm_unreachable("Unknown symbol kind");
156-
}
157-
158147
std::vector<Diag> generateMissingIncludeDiagnostics(
159148
ParsedAST &AST, llvm::ArrayRef<MissingIncludeDiagInfo> MissingIncludes,
160149
llvm::StringRef Code, HeaderFilter IgnoreHeaders) {
@@ -200,7 +189,7 @@ std::vector<Diag> generateMissingIncludeDiagnostics(
200189
Diag &D = Result.emplace_back();
201190
D.Message =
202191
llvm::formatv("No header providing \"{0}\" is directly included",
203-
getSymbolName(SymbolWithMissingInclude.Symbol));
192+
SymbolWithMissingInclude.Symbol.name());
204193
D.Name = "missing-includes";
205194
D.Source = Diag::DiagSource::Clangd;
206195
D.File = AST.tuPath();

clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct Symbol {
6767

6868
const Decl &declaration() const { return *std::get<Declaration>(Storage); }
6969
struct Macro macro() const { return std::get<Macro>(Storage); }
70+
std::string name() const;
7071

7172
private:
7273
// Order must match Kind enum!

clang-tools-extra/include-cleaner/lib/Types.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515

1616
namespace clang::include_cleaner {
1717

18+
std::string Symbol::name() const {
19+
switch (kind()) {
20+
case include_cleaner::Symbol::Macro:
21+
return macro().Name->getName().str();
22+
case include_cleaner::Symbol::Declaration:
23+
return llvm::dyn_cast<NamedDecl>(&declaration())
24+
->getQualifiedNameAsString();
25+
}
26+
llvm_unreachable("Unknown symbol kind");
27+
}
28+
1829
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Symbol &S) {
1930
switch (S.kind()) {
2031
case Symbol::Declaration:

clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
// CHECK-FIXES: {{^}}
1111
int BarResult = bar();
1212
int BazResult = baz();
13-
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: no header providing "baz.h" is directly included [misc-include-cleaner]
13+
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: no header providing "baz" is directly included [misc-include-cleaner]
1414
std::string HelloString;
15-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: no header providing <string> is directly included [misc-include-cleaner]
15+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: no header providing "std::string" is directly included [misc-include-cleaner]
1616
int FooBarResult = foobar();
17-
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: no header providing "public.h" is directly included [misc-include-cleaner]
17+
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: no header providing "foobar" is directly included [misc-include-cleaner]

0 commit comments

Comments
 (0)