[clang-tools-extra] 64fe045 - [clangd] PreamblePatch should be no-op if includes arent patched

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 16 08:12:21 PDT 2021


Author: Kadir Cetinkaya
Date: 2021-09-16T17:11:31+02:00
New Revision: 64fe0458866d7bcadacb776a1fa7d827ba8ab916

URL: https://github.com/llvm/llvm-project/commit/64fe0458866d7bcadacb776a1fa7d827ba8ab916
DIFF: https://github.com/llvm/llvm-project/commit/64fe0458866d7bcadacb776a1fa7d827ba8ab916.diff

LOG: [clangd] PreamblePatch should be no-op if includes arent patched

Don't create a useless functional patch with only filename in it when
there is only include directives to be patched but they're not
requested.

Differential Revision: https://reviews.llvm.org/D109880

Added: 
    

Modified: 
    clang-tools-extra/clangd/Preamble.cpp
    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 72a53b2fdac5..0feb40113309 100644
--- a/clang-tools-extra/clangd/Preamble.cpp
+++ b/clang-tools-extra/clangd/Preamble.cpp
@@ -459,7 +459,8 @@ PreamblePatch PreamblePatch::create(llvm::StringRef FileName,
   bool IncludesChanged = BaselineScan->Includes != ModifiedScan->Includes;
   bool DirectivesChanged =
       BaselineScan->TextualDirectives != ModifiedScan->TextualDirectives;
-  if (!IncludesChanged && !DirectivesChanged)
+  if ((PatchType == PatchType::MacroDirectives || !IncludesChanged) &&
+      !DirectivesChanged)
     return PreamblePatch::unmodified(Baseline);
 
   PreamblePatch PP;

diff  --git a/clang-tools-extra/clangd/unittests/PreambleTests.cpp b/clang-tools-extra/clangd/unittests/PreambleTests.cpp
index bd9f9893adf9..6001533005c9 100644
--- a/clang-tools-extra/clangd/unittests/PreambleTests.cpp
+++ b/clang-tools-extra/clangd/unittests/PreambleTests.cpp
@@ -554,6 +554,20 @@ TEST(PreamblePatch, MacroLoc) {
   auto AST = createPatchedAST(Baseline, Modified);
   ASSERT_TRUE(AST);
 }
+
+TEST(PreamblePatch, NoopWhenNotRequested) {
+  llvm::StringLiteral Baseline = "#define M\nint num = M;";
+  llvm::StringLiteral Modified = "#define M\n#include <foo.h>\nint num = M;";
+  auto TU = TestTU::withCode(Baseline);
+  auto BaselinePreamble = TU.preamble();
+  ASSERT_TRUE(BaselinePreamble);
+
+  TU.Code = Modified.str();
+  MockFS FS;
+  auto PP = PreamblePatch::createMacroPatch(testPath(TU.Filename),
+                                            TU.inputs(FS), *BaselinePreamble);
+  EXPECT_TRUE(PP.text().empty());
+}
 } // namespace
 } // namespace clangd
 } // namespace clang


        


More information about the cfe-commits mailing list