Skip to content

Commit 64fe045

Browse files
committed
[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
1 parent abe8b35 commit 64fe045

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clang-tools-extra/clangd/Preamble.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ PreamblePatch PreamblePatch::create(llvm::StringRef FileName,
459459
bool IncludesChanged = BaselineScan->Includes != ModifiedScan->Includes;
460460
bool DirectivesChanged =
461461
BaselineScan->TextualDirectives != ModifiedScan->TextualDirectives;
462-
if (!IncludesChanged && !DirectivesChanged)
462+
if ((PatchType == PatchType::MacroDirectives || !IncludesChanged) &&
463+
!DirectivesChanged)
463464
return PreamblePatch::unmodified(Baseline);
464465

465466
PreamblePatch PP;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,20 @@ TEST(PreamblePatch, MacroLoc) {
554554
auto AST = createPatchedAST(Baseline, Modified);
555555
ASSERT_TRUE(AST);
556556
}
557+
558+
TEST(PreamblePatch, NoopWhenNotRequested) {
559+
llvm::StringLiteral Baseline = "#define M\nint num = M;";
560+
llvm::StringLiteral Modified = "#define M\n#include <foo.h>\nint num = M;";
561+
auto TU = TestTU::withCode(Baseline);
562+
auto BaselinePreamble = TU.preamble();
563+
ASSERT_TRUE(BaselinePreamble);
564+
565+
TU.Code = Modified.str();
566+
MockFS FS;
567+
auto PP = PreamblePatch::createMacroPatch(testPath(TU.Filename),
568+
TU.inputs(FS), *BaselinePreamble);
569+
EXPECT_TRUE(PP.text().empty());
570+
}
557571
} // namespace
558572
} // namespace clangd
559573
} // namespace clang

0 commit comments

Comments
 (0)