diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 924b2c03cfd18..111c6e740f7b4 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -655,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 ----------------------------------- diff --git a/clang-tools-extra/docs/ReleaseNotesTemplate.txt b/clang-tools-extra/docs/ReleaseNotesTemplate.txt index 69c3bcf67b8db..d05c91a7dfdff 100644 --- a/clang-tools-extra/docs/ReleaseNotesTemplate.txt +++ b/clang-tools-extra/docs/ReleaseNotesTemplate.txt @@ -109,8 +109,8 @@ Removed checks Miscellaneous ^^^^^^^^^^^^^ -Improvements to include-fixer ------------------------------ +Improvements to clang-include-cleaner +------------------------------------- Improvements to clang-include-fixer ----------------------------------- 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> locateDecl(const Decl &D) { std::vector> locateMacro(const Macro &M, const tooling::stdlib::Lang L) { - // FIXME: Should we also provide physical locations? + std::vector> 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..ccd3c3f34eb4c 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, + llvm::ArrayRef 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.str()); 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; }