diff --git a/clang-tools-extra/clangd/unittests/PreambleTests.cpp b/clang-tools-extra/clangd/unittests/PreambleTests.cpp --- a/clang-tools-extra/clangd/unittests/PreambleTests.cpp +++ b/clang-tools-extra/clangd/unittests/PreambleTests.cpp @@ -36,35 +36,19 @@ return arg.first() == File && arg.second == D; } -std::shared_ptr -createPreamble(llvm::StringRef Contents = "") { - auto TU = TestTU::withCode(Contents); - // ms-compatibility changes meaning of #import, make sure it is turned off. - TU.ExtraArgs = {"-fno-ms-compatibility"}; - TU.Filename = "preamble.cpp"; - auto PI = TU.inputs(); - IgnoreDiagnostics Diags; - auto CI = buildCompilerInvocation(PI, Diags); - if (!CI) { - ADD_FAILURE() << "failed to build compiler invocation"; - return nullptr; - } - if (auto Preamble = buildPreamble(TU.Filename, *CI, PI, true, nullptr)) - return Preamble; - ADD_FAILURE() << "failed to build preamble"; - return nullptr; -} - // Builds a preamble for BaselineContents, patches it for ModifiedContents and // returns the includes in the patch. IncludeStructure collectPatchedIncludes(llvm::StringRef ModifiedContents, llvm::StringRef BaselineContents, llvm::StringRef MainFileName = "main.cpp") { - auto BaselinePreamble = createPreamble(BaselineContents); - // Create the patch. - auto TU = TestTU::withCode(ModifiedContents); + auto TU = TestTU::withCode(BaselineContents); TU.Filename = MainFileName.str(); + // ms-compatibility changes meaning of #import, make sure it is turned off. + TU.ExtraArgs = {"-fno-ms-compatibility"}; + auto BaselinePreamble = TU.buildPreamble(); + // Create the patch. + TU = TestTU::withCode(ModifiedContents); auto PI = TU.inputs(); auto PP = PreamblePatch::create(testPath(TU.Filename), PI, *BaselinePreamble); // Collect patch contents. diff --git a/clang-tools-extra/clangd/unittests/TestTU.h b/clang-tools-extra/clangd/unittests/TestTU.h --- a/clang-tools-extra/clangd/unittests/TestTU.h +++ b/clang-tools-extra/clangd/unittests/TestTU.h @@ -68,6 +68,7 @@ // By default, build() will report Error diagnostics as GTest errors. // Suppress this behavior by adding an 'error-ok' comment to the code. ParsedAST build() const; + std::shared_ptr buildPreamble() const; ParseInputs inputs() const; SymbolSlab headerSymbols() const; std::unique_ptr index() const; diff --git a/clang-tools-extra/clangd/unittests/TestTU.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp --- a/clang-tools-extra/clangd/unittests/TestTU.cpp +++ b/clang-tools-extra/clangd/unittests/TestTU.cpp @@ -65,14 +65,24 @@ return Inputs; } +std::shared_ptr TestTU::buildPreamble() const { + auto Inputs = inputs(); + IgnoreDiagnostics Diags; + auto CI = buildCompilerInvocation(Inputs, Diags); + assert(CI && "Failed to build compilation invocation."); + return clang::clangd::buildPreamble(testPath(Filename), *CI, Inputs, + /*StoreInMemory=*/true, + /*PreambleCallback=*/nullptr); +} + ParsedAST TestTU::build() const { auto Inputs = inputs(); StoreDiags Diags; auto CI = buildCompilerInvocation(Inputs, Diags); assert(CI && "Failed to build compilation invocation."); - auto Preamble = - buildPreamble(testPath(Filename), *CI, Inputs, - /*StoreInMemory=*/true, /*PreambleCallback=*/nullptr); + auto Preamble = clang::clangd::buildPreamble(testPath(Filename), *CI, Inputs, + /*StoreInMemory=*/true, + /*PreambleCallback=*/nullptr); auto AST = ParsedAST::build(testPath(Filename), Inputs, std::move(CI), Diags.take(), Preamble); if (!AST.hasValue()) {