diff --git a/clang-tools-extra/clangd/Preamble.h b/clang-tools-extra/clangd/Preamble.h --- a/clang-tools-extra/clangd/Preamble.h +++ b/clang-tools-extra/clangd/Preamble.h @@ -183,6 +183,7 @@ PreambleBounds ModifiedBounds = {0, false}; const PreambleData *Baseline = nullptr; std::vector PatchedMarks; + MainFileMacros PatchedMacros; }; } // namespace clangd diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp --- a/clang-tools-extra/clangd/Preamble.cpp +++ b/clang-tools-extra/clangd/Preamble.cpp @@ -324,6 +324,7 @@ // Literal lines of the preamble contents. std::vector Lines; PreambleBounds Bounds = {0, false}; + MainFileMacros Macros; }; /// Scans the preprocessor directives in the preamble section of the file by @@ -378,6 +379,8 @@ SP.Bounds = Bounds; PP.addPPCallbacks( std::make_unique(PP, SP.TextualDirectives)); + PP.addPPCallbacks(std::make_unique( + PP.getSourceManager(), SP.Macros)); if (llvm::Error Err = Action.Execute()) return std::move(Err); Action.EndSourceFile(); @@ -879,6 +882,7 @@ PP.PatchedDiags = patchDiags(Baseline.Diags, *BaselineScan, *ModifiedScan); PP.PatchedMarks = getPragmaMarks(*ModifiedScan); + PP.PatchedMacros = std::move(ModifiedScan->Macros); dlog("Created preamble patch: {0}", Patch.str()); Patch.flush(); return PP; @@ -938,8 +942,7 @@ MainFileMacros PreamblePatch::mainFileMacros() const { if (PatchContents.empty()) return Baseline->Macros; - // FIXME: Patch main file macros. - return MainFileMacros(); + return PatchedMacros; } } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/unittests/PreambleTests.cpp b/clang-tools-extra/clangd/unittests/PreambleTests.cpp --- a/clang-tools-extra/clangd/unittests/PreambleTests.cpp +++ b/clang-tools-extra/clangd/unittests/PreambleTests.cpp @@ -31,7 +31,6 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/VirtualFileSystem.h" -#include "llvm/Testing/Support/SupportHelpers.h" #include "gmock/gmock.h" #include "gtest/gtest-matchers.h" #include "gtest/gtest.h" @@ -856,11 +855,12 @@ ]] #pragma $y[[mark YY ]] +#define BAZ #endif)cpp"); auto AST = createPatchedAST(Code.code(), NewCode.code()); - // FIXME: Macros and marks have locations that need to be patched. - EXPECT_THAT(AST->getMacros().Names, IsEmpty()); + EXPECT_THAT(AST->getMacros().Names.keys(), + UnorderedElementsAreArray({"FOO", "BAR", "BAZ"})); EXPECT_THAT(AST->getMarks(), UnorderedElementsAre(Mark(NewCode.range("x"), " XX"), Mark(NewCode.range("y"), " YY")));