aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCongcong Cai <congcongcai0907@163.com>2024-06-11 21:01:52 +0800
committerGitHub <noreply@github.com>2024-06-11 21:01:52 +0800
commit1bae10879d9183c5edfb709c36b55086ebc772f0 (patch)
tree55d1b1b56bf072620835b0da93271dfc012233fc
parentd4c6478cf2c0a8e2bebc66eb7eff4d0f11626d9a (diff)
downloadllvm-1bae10879d9183c5edfb709c36b55086ebc772f0.zip
llvm-1bae10879d9183c5edfb709c36b55086ebc772f0.tar.gz
llvm-1bae10879d9183c5edfb709c36b55086ebc772f0.tar.bz2
[clang-tidy] fix false positives for the functions with the same name as standard library functions in misc-include-cleaner (#94923)
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.
-rw-r--r--clang-tools-extra/docs/ReleaseNotes.rst4
-rw-r--r--clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp8
-rw-r--r--clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp11
-rw-r--r--clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.cpp8
4 files changed, 29 insertions, 2 deletions
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 2dc39d0..6bf70c5 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -337,6 +337,10 @@ Changes in existing checks
<clang-tidy/checks/misc/header-include-cycle>` check by avoiding crash for self
include cycles.
+- 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 78e783a..9148d36 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 0730214..fdcbf25 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 e10ac3f..d5ea96b 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,11 @@ 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]
+
+namespace valid {
+
+namespace gh93335 {
+void log2() {}
+} // namespace gh93335
+
+} // namespace valid