[PATCH] D146028: [clangd] Patch main file macros in preamble
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 15 01:06:22 PDT 2023
This revision was automatically updated to reflect the committed changes.
Closed by commit rG82c8bf8fcc91: [clangd] Patch main file macros in preamble (authored by kadircet).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D146028/new/
https://reviews.llvm.org/D146028
Files:
clang-tools-extra/clangd/Preamble.cpp
clang-tools-extra/clangd/Preamble.h
clang-tools-extra/clangd/unittests/PreambleTests.cpp
Index: clang-tools-extra/clangd/unittests/PreambleTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/PreambleTests.cpp
+++ clang-tools-extra/clangd/unittests/PreambleTests.cpp
@@ -30,7 +30,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"
@@ -855,11 +854,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")));
Index: clang-tools-extra/clangd/Preamble.h
===================================================================
--- clang-tools-extra/clangd/Preamble.h
+++ clang-tools-extra/clangd/Preamble.h
@@ -164,7 +164,7 @@
static constexpr llvm::StringLiteral HeaderName = "__preamble_patch__.h";
llvm::ArrayRef<PragmaMark> marks() const;
- MainFileMacros mainFileMacros() const;
+ const MainFileMacros &mainFileMacros() const;
private:
static PreamblePatch create(llvm::StringRef FileName,
@@ -183,6 +183,7 @@
PreambleBounds ModifiedBounds = {0, false};
const PreambleData *Baseline = nullptr;
std::vector<PragmaMark> PatchedMarks;
+ MainFileMacros PatchedMacros;
};
} // namespace clangd
Index: clang-tools-extra/clangd/Preamble.cpp
===================================================================
--- clang-tools-extra/clangd/Preamble.cpp
+++ clang-tools-extra/clangd/Preamble.cpp
@@ -325,6 +325,7 @@
std::vector<llvm::StringRef> Lines;
PreambleBounds Bounds = {0, false};
std::vector<PragmaMark> Marks;
+ MainFileMacros Macros;
};
/// Scans the preprocessor directives in the preamble section of the file by
@@ -373,14 +374,15 @@
if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0]))
return error("failed BeginSourceFile");
Preprocessor &PP = Clang->getPreprocessor();
+ const auto &SM = PP.getSourceManager();
IncludeStructure Includes;
Includes.collect(*Clang);
ScannedPreamble SP;
SP.Bounds = Bounds;
PP.addPPCallbacks(
std::make_unique<DirectiveCollector>(PP, SP.TextualDirectives));
- PP.addPPCallbacks(
- collectPragmaMarksCallback(PP.getSourceManager(), SP.Marks));
+ PP.addPPCallbacks(collectPragmaMarksCallback(SM, SP.Marks));
+ PP.addPPCallbacks(std::make_unique<CollectMainFileMacros>(SM, SP.Macros));
if (llvm::Error Err = Action.Execute())
return std::move(Err);
Action.EndSourceFile();
@@ -859,6 +861,7 @@
PP.PatchedDiags = patchDiags(Baseline.Diags, *BaselineScan, *ModifiedScan);
PP.PatchedMarks = std::move(ModifiedScan->Marks);
+ PP.PatchedMacros = std::move(ModifiedScan->Macros);
dlog("Created preamble patch: {0}", Patch.str());
Patch.flush();
return PP;
@@ -915,11 +918,10 @@
return PatchedMarks;
}
-MainFileMacros PreamblePatch::mainFileMacros() const {
+const MainFileMacros &PreamblePatch::mainFileMacros() const {
if (PatchContents.empty())
return Baseline->Macros;
- // FIXME: Patch main file macros.
- return MainFileMacros();
+ return PatchedMacros;
}
} // namespace clangd
} // namespace clang
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146028.505388.patch
Type: text/x-patch
Size: 3659 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230315/6b8963b2/attachment.bin>
More information about the cfe-commits
mailing list