kbobyrev updated this revision to Diff 379317. kbobyrev added a comment. Mention the ScratchBuffer explicitly in the test.
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D111698/new/ https://reviews.llvm.org/D111698 Files: clang-tools-extra/clangd/IncludeCleaner.cpp clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp +++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp @@ -167,6 +167,29 @@ UnorderedElementsAre("\"unused.h\"", "\"dir/unused.h\"")); } +TEST(IncludeCleaner, MacroPastingOperator) { + TestTU TU; + TU.Filename = "foo.cpp"; + TU.Code = R"cpp( + #include "macro_pasting_operator.h" + + using flags::FLAGS_FOO; + )cpp"; + // The pasting operator in combination with DEFINE_FLAG will create + // ScratchBuffer with `flags::FLAGS_FOO` that will have FileID but not + // FileEntry. + TU.AdditionalFiles["macro_pasting_operator.h"] = R"cpp( + #define DEFINE_FLAG(X) \ + namespace flags { \ + int FLAGS_##X; \ + } \ + + DEFINE_FLAG(FOO) + )cpp"; + ParsedAST AST = TU.build(); + EXPECT_THAT(computeUnusedIncludes(AST), testing::IsEmpty()); +} + } // namespace } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/IncludeCleaner.cpp =================================================================== --- clang-tools-extra/clangd/IncludeCleaner.cpp +++ clang-tools-extra/clangd/IncludeCleaner.cpp @@ -121,7 +121,8 @@ if (!Macros.insert(FID).second) return; const auto &Exp = SM.getSLocEntry(FID).getExpansion(); - add(Exp.getSpellingLoc()); + if (!SM.isWrittenInScratchSpace(Exp.getSpellingLoc())) + add(Exp.getSpellingLoc()); add(Exp.getExpansionLocStart()); add(Exp.getExpansionLocEnd()); }
Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp +++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp @@ -167,6 +167,29 @@ UnorderedElementsAre("\"unused.h\"", "\"dir/unused.h\"")); } +TEST(IncludeCleaner, MacroPastingOperator) { + TestTU TU; + TU.Filename = "foo.cpp"; + TU.Code = R"cpp( + #include "macro_pasting_operator.h" + + using flags::FLAGS_FOO; + )cpp"; + // The pasting operator in combination with DEFINE_FLAG will create + // ScratchBuffer with `flags::FLAGS_FOO` that will have FileID but not + // FileEntry. + TU.AdditionalFiles["macro_pasting_operator.h"] = R"cpp( + #define DEFINE_FLAG(X) \ + namespace flags { \ + int FLAGS_##X; \ + } \ + + DEFINE_FLAG(FOO) + )cpp"; + ParsedAST AST = TU.build(); + EXPECT_THAT(computeUnusedIncludes(AST), testing::IsEmpty()); +} + } // namespace } // namespace clangd } // namespace clang Index: clang-tools-extra/clangd/IncludeCleaner.cpp =================================================================== --- clang-tools-extra/clangd/IncludeCleaner.cpp +++ clang-tools-extra/clangd/IncludeCleaner.cpp @@ -121,7 +121,8 @@ if (!Macros.insert(FID).second) return; const auto &Exp = SM.getSLocEntry(FID).getExpansion(); - add(Exp.getSpellingLoc()); + if (!SM.isWrittenInScratchSpace(Exp.getSpellingLoc())) + add(Exp.getSpellingLoc()); add(Exp.getExpansionLocStart()); add(Exp.getExpansionLocEnd()); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits