Skip to content

Commit e467b03

Browse files
committed
[clang-tidy] fix false positives for the functions with the same name as standard library functions in misc-include-cleaner
Fixes: #93335
1 parent db78ee0 commit e467b03

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ Changes in existing checks
286286
Additionally, the option `UseHeaderFileExtensions` is removed, so that the
287287
check uses the `HeaderFileExtensions` option unconditionally.
288288

289+
- Improved :doc:`misc-include-cleaner
290+
<clang-tidy/checks/misc/include-cleaner>` check by avoiding false positives for
291+
the functions with the same name as standard library functions.
292+
289293
- Improved :doc:`misc-unused-using-decls
290294
<clang-tidy/checks/misc/unused-using-decls>` check by replacing the local
291295
option `HeaderFileExtensions` by the global option of the same name.

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "clang/AST/DeclTemplate.h"
1515
#include "clang/Tooling/Inclusions/StandardLibrary.h"
1616
#include "llvm/Support/Casting.h"
17+
#include "llvm/Support/raw_ostream.h"
1718
#include <utility>
1819
#include <vector>
1920

@@ -40,8 +41,11 @@ Hints declHints(const Decl *D) {
4041
std::vector<Hinted<SymbolLocation>> locateDecl(const Decl &D) {
4142
std::vector<Hinted<SymbolLocation>> Result;
4243
// FIXME: Should we also provide physical locations?
43-
if (auto SS = tooling::stdlib::Recognizer()(&D))
44-
return {{*SS, Hints::CompleteSymbol}};
44+
if (auto SS = tooling::stdlib::Recognizer()(&D)) {
45+
Result.push_back({*SS, Hints::CompleteSymbol});
46+
if (!D.hasBody())
47+
return Result;
48+
}
4549
// FIXME: Signal foreign decls, e.g. a forward declaration not owned by a
4650
// library. Some useful signals could be derived by checking the DeclContext.
4751
// Most incidental forward decls look like:

clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,17 @@ TEST_F(HeadersForSymbolTest, StandardHeaders) {
628628
tooling::stdlib::Header::named("<assert.h>")));
629629
}
630630

631+
TEST_F(HeadersForSymbolTest, NonStandardHeaders) {
632+
Inputs.Code = "void assert() {}";
633+
buildAST();
634+
EXPECT_THAT(
635+
headersFor("assert"),
636+
// Respect the ordering from the stdlib mapping.
637+
UnorderedElementsAre(physicalHeader("input.mm"),
638+
tooling::stdlib::Header::named("<cassert>"),
639+
tooling::stdlib::Header::named("<assert.h>")));
640+
}
641+
631642
TEST_F(HeadersForSymbolTest, ExporterNoNameMatch) {
632643
Inputs.Code = R"cpp(
633644
#include "exporter/foo.h"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ std::string HelloString;
1515
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: no header providing "std::string" is directly included [misc-include-cleaner]
1616
int FooBarResult = foobar();
1717
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: no header providing "foobar" is directly included [misc-include-cleaner]
18+
19+
void log2() {}

0 commit comments

Comments
 (0)