Skip to content

[clang-tidy] fix false positives for the functions with the same name as standard library functions in misc-include-cleaner #94923

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

Conversation

HerrCai0907
Copy link
Contributor

Fixes: #93335
For decl with body, we should provide physical locations also. Because it may be the function which have the same name as std library.

… as standard library functions in misc-include-cleaner

Fixes: llvm#93335
@llvmbot
Copy link
Member

llvmbot commented Jun 10, 2024

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Congcong Cai (HerrCai0907)

Changes

Fixes: #93335
For decl with body, we should provide physical locations also. Because it may be the function which have the same name as std library.


Full diff: https://github.com/llvm/llvm-project/pull/94923.diff

4 Files Affected:

  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (modified) clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp (+6-2)
  • (modified) clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp (+11)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.cpp (+2)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3f0d25ec8c752..8c78d872b9a1a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -286,6 +286,10 @@ Changes in existing checks
   Additionally, the option `UseHeaderFileExtensions` is removed, so that the
   check uses the `HeaderFileExtensions` option unconditionally.
 
+- Improved :doc:`misc-include-cleaner
+  <clang-tidy/checks/misc/include-cleaner>` check by avoiding false positives for
+  the functions with the same name as standard library functions.
+
 - Improved :doc:`misc-unused-using-decls
   <clang-tidy/checks/misc/unused-using-decls>` check by replacing the local
   option `HeaderFileExtensions` by the global option of the same name.
diff --git a/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp b/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
index 78e783a62eb27..9148d36a5038f 100644
--- a/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
+++ b/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
@@ -14,6 +14,7 @@
 #include "clang/AST/DeclTemplate.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/raw_ostream.h"
 #include <utility>
 #include <vector>
 
@@ -40,8 +41,11 @@ Hints declHints(const Decl *D) {
 std::vector<Hinted<SymbolLocation>> locateDecl(const Decl &D) {
   std::vector<Hinted<SymbolLocation>> Result;
   // FIXME: Should we also provide physical locations?
-  if (auto SS = tooling::stdlib::Recognizer()(&D))
-    return {{*SS, Hints::CompleteSymbol}};
+  if (auto SS = tooling::stdlib::Recognizer()(&D)) {
+    Result.push_back({*SS, Hints::CompleteSymbol});
+    if (!D.hasBody())
+      return Result;
+  }
   // FIXME: Signal foreign decls, e.g. a forward declaration not owned by a
   // library. Some useful signals could be derived by checking the DeclContext.
   // Most incidental forward decls look like:
diff --git a/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp b/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
index 07302142a13e3..fdcbf25fd628c 100644
--- a/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -628,6 +628,17 @@ TEST_F(HeadersForSymbolTest, StandardHeaders) {
                            tooling::stdlib::Header::named("<assert.h>")));
 }
 
+TEST_F(HeadersForSymbolTest, NonStandardHeaders) {
+  Inputs.Code = "void assert() {}";
+  buildAST();
+  EXPECT_THAT(
+      headersFor("assert"),
+      // Respect the ordering from the stdlib mapping.
+      UnorderedElementsAre(physicalHeader("input.mm"),
+                           tooling::stdlib::Header::named("<cassert>"),
+                           tooling::stdlib::Header::named("<assert.h>")));
+}
+
 TEST_F(HeadersForSymbolTest, ExporterNoNameMatch) {
   Inputs.Code = R"cpp(
     #include "exporter/foo.h"
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 e10ac3f46e2e9..b1e001834db5a 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
@@ -15,3 +15,5 @@ std::string HelloString;
 // 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 "foobar" is directly included [misc-include-cleaner]
+
+void log2() {}

@HerrCai0907 HerrCai0907 requested review from PiotrZSL and 5chmidti June 10, 2024 14:35
@@ -15,3 +15,5 @@ std::string HelloString;
// 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 "foobar" is directly included [misc-include-cleaner]

void log2() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe write a small comment stating it shouldn't warn, or add a reference to the issue number? That would make it easier to understand why this is here

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@HerrCai0907 HerrCai0907 force-pushed the 93335-clang-tidy-misc-include-cleaner-false-positives branch from d23acd2 to 0e3d609 Compare June 11, 2024 08:05
@HerrCai0907 HerrCai0907 merged commit 1bae108 into llvm:main Jun 11, 2024
6 of 8 checks passed
@HerrCai0907 HerrCai0907 deleted the 93335-clang-tidy-misc-include-cleaner-false-positives branch June 11, 2024 13:01
@HerrCai0907 HerrCai0907 mentioned this pull request Jun 13, 2024
kadircet added a commit that referenced this pull request Jun 18, 2024
…ame name as standard library functions in misc-include-cleaner (#94923)"

This reverts commit 1bae108. Also add
some test cases to prevent further regressions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] misc-include-cleaner false positives
4 participants