[clangd] PreamblePatch should be no-op if includes arent patched
authorKadir Cetinkaya <kadircet@google.com>
Thu, 16 Sep 2021 09:30:55 +0000 (11:30 +0200)
committerKadir Cetinkaya <kadircet@google.com>
Thu, 16 Sep 2021 15:11:31 +0000 (17:11 +0200)
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

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

index 72a53b2..0feb401 100644 (file)
@@ -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;
index bd9f989..6001533 100644 (file)
@@ -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