[clangd] Patch PragmaMarks in preamble section of the file
authorKadir Cetinkaya <kadircet@google.com>
Tue, 14 Mar 2023 09:45:12 +0000 (10:45 +0100)
committerKadir Cetinkaya <kadircet@google.com>
Wed, 15 Mar 2023 08:00:14 +0000 (09:00 +0100)
Differential Revision: https://reviews.llvm.org/D146026

clang-tools-extra/clangd/Preamble.cpp
clang-tools-extra/clangd/Preamble.h
clang-tools-extra/clangd/unittests/PreambleTests.cpp

index 94e6839..85138ee 100644 (file)
@@ -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"
 #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 {
index c0ccceb..6b18977 100644 (file)
@@ -182,6 +182,7 @@ private:
   std::vector<Diag> PatchedDiags;
   PreambleBounds ModifiedBounds = {0, false};
   const PreambleData *Baseline = nullptr;
+  std::vector<PragmaMark> PatchedMarks;
 };
 
 } // namespace clangd
index bc32671..fd094fa 100644 (file)
@@ -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")));
   }
 }