From: Kadir Cetinkaya Date: Thu, 14 May 2020 10:23:21 +0000 (+0200) Subject: [clangd] Add buildPreamble to TestTU X-Git-Tag: llvmorg-12-init~4741 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=478f6fb2001698eb102ddce9500ff0885eaaeaab;p=platform%2Fupstream%2Fllvm.git [clangd] Add buildPreamble to TestTU Summary: Depends on D77644. Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D79930 --- diff --git a/clang-tools-extra/clangd/unittests/PreambleTests.cpp b/clang-tools-extra/clangd/unittests/PreambleTests.cpp index bb471ab..75aad72 100644 --- a/clang-tools-extra/clangd/unittests/PreambleTests.cpp +++ b/clang-tools-extra/clangd/unittests/PreambleTests.cpp @@ -36,37 +36,19 @@ MATCHER_P2(Distance, File, D, "") { 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.preamble(); + // 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.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp index 824c4cc..b478142 100644 --- a/clang-tools-extra/clangd/unittests/TestTU.cpp +++ b/clang-tools-extra/clangd/unittests/TestTU.cpp @@ -66,14 +66,24 @@ ParseInputs TestTU::inputs() const { return Inputs; } +std::shared_ptr TestTU::preamble() 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()) { diff --git a/clang-tools-extra/clangd/unittests/TestTU.h b/clang-tools-extra/clangd/unittests/TestTU.h index 2be294f..57017f4 100644 --- a/clang-tools-extra/clangd/unittests/TestTU.h +++ b/clang-tools-extra/clangd/unittests/TestTU.h @@ -68,6 +68,7 @@ struct TestTU { // 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 preamble() const; ParseInputs inputs() const; SymbolSlab headerSymbols() const; RefSlab headerRefs() const;