Author: Haojian Wu
Date: 2023-06-15T15:26:48+02:00
New Revision: eed4a4d02e4f33cc2f2a9980466d1c7a1cf37398

URL: 
https://github.com/llvm/llvm-project/commit/eed4a4d02e4f33cc2f2a9980466d1c7a1cf37398
DIFF: 
https://github.com/llvm/llvm-project/commit/eed4a4d02e4f33cc2f2a9980466d1c7a1cf37398.diff

LOG: [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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
    clang-tools-extra/clangd/IncludeCleaner.cpp
    clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
    clang-tools-extra/include-cleaner/lib/Types.cpp
    clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp 
b/clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
index 49e7581d801d9..b9f44c96818db 100644
--- a/clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
@@ -45,7 +45,7 @@ namespace clang::tidy::misc {
 
 namespace {
 struct MissingIncludeInfo {
-  SourceLocation SymRefLocation;
+  include_cleaner::SymbolReference SymRef;
   include_cleaner::Header Missing;
 };
 } // namespace
@@ -134,7 +134,7 @@ void IncludeCleanerCheck::check(const 
MatchFinder::MatchResult &Result) {
              if (!Satisfied && !Providers.empty() &&
                  Ref.RT == include_cleaner::RefType::Explicit &&
                  !shouldIgnore(Providers.front()))
-               Missing.push_back({Ref.RefLocation, Providers.front()});
+               Missing.push_back({Ref, Providers.front()});
            });
 
   std::vector<const include_cleaner::Include *> Unused;
@@ -190,9 +190,9 @@ void IncludeCleanerCheck::check(const 
MatchFinder::MatchResult &Result) {
     if (auto Replacement =
             HeaderIncludes.insert(llvm::StringRef{Spelling}.trim("\"<>"),
                                   Angled, tooling::IncludeDirective::Include))
-      diag(SM->getSpellingLoc(Inc.SymRefLocation),
-           "no header providing %0 is directly included")
-          << Spelling
+      diag(SM->getSpellingLoc(Inc.SymRef.RefLocation),
+           "no header providing \"%0\" is directly included")
+          << Inc.SymRef.Target.name()
           << FixItHint::CreateInsertion(
                  SM->getComposedLoc(SM->getMainFileID(),
                                     Replacement->getOffset()),

diff  --git a/clang-tools-extra/clangd/IncludeCleaner.cpp 
b/clang-tools-extra/clangd/IncludeCleaner.cpp
index 95f6064b769a7..8f1be544bcfa1 100644
--- a/clang-tools-extra/clangd/IncludeCleaner.cpp
+++ b/clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -144,17 +144,6 @@ llvm::StringRef getResolvedPath(const 
include_cleaner::Header &SymProvider) {
   llvm_unreachable("Unknown header kind");
 }
 
-std::string getSymbolName(const include_cleaner::Symbol &Sym) {
-  switch (Sym.kind()) {
-  case include_cleaner::Symbol::Macro:
-    return Sym.macro().Name->getName().str();
-  case include_cleaner::Symbol::Declaration:
-    return llvm::dyn_cast<NamedDecl>(&Sym.declaration())
-        ->getQualifiedNameAsString();
-  }
-  llvm_unreachable("Unknown symbol kind");
-}
-
 std::vector<Diag> generateMissingIncludeDiagnostics(
     ParsedAST &AST, llvm::ArrayRef<MissingIncludeDiagInfo> MissingIncludes,
     llvm::StringRef Code, HeaderFilter IgnoreHeaders) {
@@ -200,7 +189,7 @@ std::vector<Diag> generateMissingIncludeDiagnostics(
     Diag &D = Result.emplace_back();
     D.Message =
         llvm::formatv("No header providing \"{0}\" is directly included",
-                      getSymbolName(SymbolWithMissingInclude.Symbol));
+                      SymbolWithMissingInclude.Symbol.name());
     D.Name = "missing-includes";
     D.Source = Diag::DiagSource::Clangd;
     D.File = AST.tuPath();

diff  --git 
a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h 
b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
index 05cb96ebec1ff..39055db5a8587 100644
--- a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
+++ b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
@@ -67,6 +67,7 @@ struct Symbol {
 
   const Decl &declaration() const { return *std::get<Declaration>(Storage); }
   struct Macro macro() const { return std::get<Macro>(Storage); }
+  std::string name() const;
 
 private:
   // Order must match Kind enum!

diff  --git a/clang-tools-extra/include-cleaner/lib/Types.cpp 
b/clang-tools-extra/include-cleaner/lib/Types.cpp
index 3d7ca9bdeb4e3..a5ba4a0d47f6c 100644
--- a/clang-tools-extra/include-cleaner/lib/Types.cpp
+++ b/clang-tools-extra/include-cleaner/lib/Types.cpp
@@ -15,6 +15,17 @@
 
 namespace clang::include_cleaner {
 
+std::string Symbol::name() const {
+  switch (kind()) {
+  case include_cleaner::Symbol::Macro:
+    return macro().Name->getName().str();
+  case include_cleaner::Symbol::Declaration:
+    return llvm::dyn_cast<NamedDecl>(&declaration())
+        ->getQualifiedNameAsString();
+  }
+  llvm_unreachable("Unknown symbol kind");
+}
+
 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Symbol &S) {
   switch (S.kind()) {
   case Symbol::Declaration:

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.cpp
index ed12600d96911..aef115d59bbef 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.cpp
@@ -10,8 +10,8 @@
 // CHECK-FIXES: {{^}}
 int BarResult = bar();
 int BazResult = baz();
-// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: no header providing "baz.h" is 
directly included [misc-include-cleaner]
+// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: no header providing "baz" is 
directly included [misc-include-cleaner]
 std::string HelloString;
-// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: no header providing <string> is 
directly included [misc-include-cleaner]
+// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: no header providing "std::string" 
is directly included [misc-include-cleaner]
 int FooBarResult = foobar();
-// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: no header providing "public.h" is 
directly included [misc-include-cleaner]
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: no header providing "foobar" is 
directly included [misc-include-cleaner]


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to