https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/173149
>From cf4dc85e58c3f6147e3ccbf0f3b5be41e7539350 Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Fri, 19 Dec 2025 21:00:43 +0800 Subject: [PATCH 1/2] [clang-tidy][include-cleaner] Fix false positive for stdlib-like macros --- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ .../include-cleaner/lib/LocateSymbol.cpp | 7 ++++--- .../unittests/LocateSymbolTest.cpp | 21 ++++++++++++++----- .../checkers/misc/include-cleaner.c | 7 +++++++ 4 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.c diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 924b2c03cfd18..b8a642ee50f8f 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -517,6 +517,10 @@ Changes in existing checks - Improved :doc:`misc-header-include-cycle <clang-tidy/checks/misc/header-include-cycle>` check performance. +- Improved :doc:`misc-include-cleaner + <clang-tidy/checks/misc/include-cleaner>` check by avoiding warnings when a + standard library symbol is redefined within the codebase. + - Improved :doc:`modernize-avoid-c-arrays <clang-tidy/checks/modernize/avoid-c-arrays>` to not diagnose array types which are part of an implicit instantiation of a template. diff --git a/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp b/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp index b7433305152f9..e50d8ee58d817 100644 --- a/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp +++ b/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp @@ -56,10 +56,11 @@ std::vector<Hinted<SymbolLocation>> locateDecl(const Decl &D) { std::vector<Hinted<SymbolLocation>> locateMacro(const Macro &M, const tooling::stdlib::Lang L) { - // FIXME: Should we also provide physical locations? + std::vector<Hinted<SymbolLocation>> Result; + Result.push_back({M.Definition, Hints::CompleteSymbol}); if (auto SS = tooling::stdlib::Symbol::named("", M.Name->getName(), L)) - return {{*SS, Hints::CompleteSymbol}}; - return {{M.Definition, Hints::CompleteSymbol}}; + Result.push_back({*SS, Hints::CompleteSymbol}); + return Result; } } // namespace diff --git a/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp b/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp index 1e7baf142a75a..0884fd1db8ad7 100644 --- a/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp +++ b/clang-tools-extra/include-cleaner/unittests/LocateSymbolTest.cpp @@ -43,10 +43,12 @@ struct LocateExample { TestAST AST; public: - LocateExample(llvm::StringRef AnnotatedCode) - : Target(AnnotatedCode), AST([this] { + LocateExample(llvm::StringRef AnnotatedCode, + std::vector<const char *> ExtraArgs = {"-std=c++17"}) + : Target(AnnotatedCode), AST([&] { TestInputs Inputs(Target.code()); - Inputs.ExtraArgs.push_back("-std=c++17"); + for (auto Arg : ExtraArgs) + Inputs.ExtraArgs.push_back(Arg); return Inputs; }()) {} @@ -126,9 +128,10 @@ TEST(LocateSymbol, Stdlib) { ElementsAre(*tooling::stdlib::Symbol::named("std::", "vector"))); } { - LocateExample Test("#define assert(x)\nvoid foo() { assert(true); }"); + LocateExample Test("#define ^assert(x)\nvoid foo() { assert(true); }"); EXPECT_THAT(locateSymbol(Test.findMacro("assert"), Test.langOpts()), - ElementsAre(*tooling::stdlib::Symbol::named("", "assert"))); + ElementsAre(Test.points().front(), + *tooling::stdlib::Symbol::named("", "assert"))); } } @@ -139,6 +142,14 @@ TEST(LocateSymbol, Macros) { ElementsAreArray(Test.points())); } +TEST(LocateSymbol, MacroI_C) { + LocateExample Test("#define ^I 42", {"-x", "c", "-std=c99"}); + EXPECT_THAT(locateSymbol(Test.findMacro("I"), Test.langOpts()), + ElementsAre(Test.points().front(), + *tooling::stdlib::Symbol::named( + "", "I", tooling::stdlib::Lang::C))); +} + MATCHER_P2(HintedSymbol, Symbol, Hint, "") { return std::tie(arg.Hint, arg) == std::tie(Hint, Symbol); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.c b/clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.c new file mode 100644 index 0000000000000..5f7d323ad79e7 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.c @@ -0,0 +1,7 @@ +// RUN: %check_clang_tidy %s misc-include-cleaner %t + +#define I 42 +void f(void) { I; } + +#define H 42 +void g(void) { H; } >From 2b522cacb68a50d48aef04f2aba7cb5279d20e8f Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Sun, 21 Dec 2025 17:30:20 +0800 Subject: [PATCH 2/2] Fix docs --- clang-tools-extra/docs/ReleaseNotes.rst | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index b8a642ee50f8f..111c6e740f7b4 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -517,10 +517,6 @@ Changes in existing checks - Improved :doc:`misc-header-include-cycle <clang-tidy/checks/misc/header-include-cycle>` check performance. -- Improved :doc:`misc-include-cleaner - <clang-tidy/checks/misc/include-cleaner>` check by avoiding warnings when a - standard library symbol is redefined within the codebase. - - Improved :doc:`modernize-avoid-c-arrays <clang-tidy/checks/modernize/avoid-c-arrays>` to not diagnose array types which are part of an implicit instantiation of a template. @@ -659,10 +655,11 @@ Removed checks Miscellaneous ^^^^^^^^^^^^^ -Improvements to include-fixer ------------------------------ +Improvements to clang-include-cleaner +------------------------------------- -The improvements are... +- Fixed a problem where macros in the codebase with names matching standard + library symbols were mistakenly identified as standard library symbols. Improvements to clang-include-fixer ----------------------------------- _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
