[clang-tools-extra] 9c88812 - [clangd] Patch PragmaMarks in preamble section of the file
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 15 01:06:18 PDT 2023
Author: Kadir Cetinkaya
Date: 2023-03-15T09:00:14+01:00
New Revision: 9c888120e3c8cc93f9950590a9192002e7c3d08b
URL: https://github.com/llvm/llvm-project/commit/9c888120e3c8cc93f9950590a9192002e7c3d08b
DIFF: https://github.com/llvm/llvm-project/commit/9c888120e3c8cc93f9950590a9192002e7c3d08b.diff
LOG: [clangd] Patch PragmaMarks in preamble section of the file
Differential Revision: https://reviews.llvm.org/D146026
Added:
Modified:
clang-tools-extra/clangd/Preamble.cpp
clang-tools-extra/clangd/Preamble.h
clang-tools-extra/clangd/unittests/PreambleTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp
index 94e6839a7f49..85138eeae783 100644
--- a/clang-tools-extra/clangd/Preamble.cpp
+++ b/clang-tools-extra/clangd/Preamble.cpp
@@ -15,7 +15,9 @@
#include "Protocol.h"
#include "SourceCode.h"
#include "clang-include-cleaner/Record.h"
+#include "index/CanonicalIncludes.h"
#include "support/Logger.h"
+#include "support/Path.h"
#include "support/ThreadsafeFS.h"
#include "support/Trace.h"
#include "clang/AST/DeclTemplate.h"
@@ -27,6 +29,7 @@
#include "clang/Basic/TokenKinds.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/FrontendActions.h"
+#include "clang/Frontend/PrecompiledPreamble.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/PPCallbacks.h"
@@ -42,6 +45,7 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Casting.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormatVariadic.h"
@@ -50,10 +54,12 @@
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <cstddef>
+#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <system_error>
+#include <tuple>
#include <utility>
#include <vector>
@@ -318,6 +324,7 @@ struct ScannedPreamble {
// Literal lines of the preamble contents.
std::vector<llvm::StringRef> Lines;
PreambleBounds Bounds = {0, false};
+ std::vector<PragmaMark> Marks;
};
/// Scans the preprocessor directives in the preamble section of the file by
@@ -372,6 +379,8 @@ scanPreamble(llvm::StringRef Contents, const tooling::CompileCommand &Cmd) {
SP.Bounds = Bounds;
PP.addPPCallbacks(
std::make_unique<DirectiveCollector>(PP, SP.TextualDirectives));
+ PP.addPPCallbacks(
+ collectPragmaMarksCallback(PP.getSourceManager(), SP.Marks));
if (llvm::Error Err = Action.Execute())
return std::move(Err);
Action.EndSourceFile();
@@ -849,6 +858,7 @@ PreamblePatch PreamblePatch::create(llvm::StringRef FileName,
}
PP.PatchedDiags = patchDiags(Baseline.Diags, *BaselineScan, *ModifiedScan);
+ PP.PatchedMarks = std::move(ModifiedScan->Marks);
dlog("Created preamble patch: {0}", Patch.str());
Patch.flush();
return PP;
@@ -902,8 +912,7 @@ bool PreamblePatch::preserveDiagnostics() const {
llvm::ArrayRef<PragmaMark> PreamblePatch::marks() const {
if (PatchContents.empty())
return Baseline->Marks;
- // FIXME: Patch pragma marks.
- return {};
+ return PatchedMarks;
}
MainFileMacros PreamblePatch::mainFileMacros() const {
diff --git a/clang-tools-extra/clangd/Preamble.h b/clang-tools-extra/clangd/Preamble.h
index c0cccebd487a..6b189777b4d5 100644
--- a/clang-tools-extra/clangd/Preamble.h
+++ b/clang-tools-extra/clangd/Preamble.h
@@ -182,6 +182,7 @@ class PreamblePatch {
std::vector<Diag> PatchedDiags;
PreambleBounds ModifiedBounds = {0, false};
const PreambleData *Baseline = nullptr;
+ std::vector<PragmaMark> PatchedMarks;
};
} // namespace clangd
diff --git a/clang-tools-extra/clangd/unittests/PreambleTests.cpp b/clang-tools-extra/clangd/unittests/PreambleTests.cpp
index bc32671e6a97..fd094fa93881 100644
--- a/clang-tools-extra/clangd/unittests/PreambleTests.cpp
+++ b/clang-tools-extra/clangd/unittests/PreambleTests.cpp
@@ -829,6 +829,10 @@ x>)");
}
}
+MATCHER_P2(Mark, Range, Text, "") {
+ return std::tie(arg.Rng, arg.Trivia) == std::tie(Range, Text);
+}
+
TEST(PreamblePatch, MacroAndMarkHandling) {
Config Cfg;
Cfg.Diagnostics.AllowStalePreamble = true;
@@ -847,13 +851,18 @@ TEST(PreamblePatch, MacroAndMarkHandling) {
#ifndef FOO
#define FOO
#define BAR
-#pragma mark XX
+#pragma $x[[mark XX
+]]
+#pragma $y[[mark YY
+]]
#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->getMarks(), IsEmpty());
+ EXPECT_THAT(AST->getMarks(),
+ UnorderedElementsAre(Mark(NewCode.range("x"), " XX"),
+ Mark(NewCode.range("y"), " YY")));
}
}
More information about the cfe-commits
mailing list