From ecd3e678bbb11cf899603037ec2c5949b8d7fa6c Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Wed, 11 Mar 2020 16:34:01 +0100 Subject: [PATCH] [clangd] Populate PreambleData::CompileCommand and make use of it inside buildPreamble Reviewers: sammccall Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D75996 --- clang-tools-extra/clangd/Preamble.cpp | 12 +++++++----- clang-tools-extra/clangd/Preamble.h | 3 +-- clang-tools-extra/clangd/TUScheduler.cpp | 4 +--- .../clangd/unittests/FileIndexTests.cpp | 20 ++++++++++---------- clang-tools-extra/clangd/unittests/TestTU.cpp | 3 +-- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp index 86fa7905..bd92b8c 100644 --- a/clang-tools-extra/clangd/Preamble.cpp +++ b/clang-tools-extra/clangd/Preamble.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "Preamble.h" +#include "Compiler.h" #include "Logger.h" #include "Trace.h" #include "clang/Basic/SourceLocation.h" @@ -75,13 +76,14 @@ private: } // namespace -PreambleData::PreambleData(llvm::StringRef Version, +PreambleData::PreambleData(const ParseInputs &Inputs, PrecompiledPreamble Preamble, std::vector Diags, IncludeStructure Includes, MainFileMacros Macros, std::unique_ptr StatCache, CanonicalIncludes CanonIncludes) - : Version(Version), Preamble(std::move(Preamble)), Diags(std::move(Diags)), + : Version(Inputs.Version), CompileCommand(Inputs.CompileCommand), + Preamble(std::move(Preamble)), Diags(std::move(Diags)), Includes(std::move(Includes)), Macros(std::move(Macros)), StatCache(std::move(StatCache)), CanonIncludes(std::move(CanonIncludes)) { } @@ -89,7 +91,6 @@ PreambleData::PreambleData(llvm::StringRef Version, std::shared_ptr buildPreamble(PathRef FileName, CompilerInvocation &CI, std::shared_ptr OldPreamble, - const tooling::CompileCommand &OldCompileCommand, const ParseInputs &Inputs, bool StoreInMemory, PreambleParsedCallback PreambleCallback) { // Note that we don't need to copy the input contents, preamble can live @@ -100,7 +101,8 @@ buildPreamble(PathRef FileName, CompilerInvocation &CI, ComputePreambleBounds(*CI.getLangOpts(), ContentsBuffer.get(), 0); if (OldPreamble && - compileCommandsAreEqual(Inputs.CompileCommand, OldCompileCommand) && + compileCommandsAreEqual(Inputs.CompileCommand, + OldPreamble->CompileCommand) && OldPreamble->Preamble.CanReuse(CI, ContentsBuffer.get(), Bounds, Inputs.FS.get())) { vlog("Reusing preamble version {0} for version {1} of {2}", @@ -155,7 +157,7 @@ buildPreamble(PathRef FileName, CompilerInvocation &CI, BuiltPreamble->getSize(), FileName, Inputs.Version); std::vector Diags = PreambleDiagnostics.take(); return std::make_shared( - Inputs.Version, std::move(*BuiltPreamble), std::move(Diags), + Inputs, std::move(*BuiltPreamble), std::move(Diags), SerializedDeclsCollector.takeIncludes(), SerializedDeclsCollector.takeMacros(), std::move(StatCache), SerializedDeclsCollector.takeCanonicalIncludes()); diff --git a/clang-tools-extra/clangd/Preamble.h b/clang-tools-extra/clangd/Preamble.h index 1517daa..c2049cd 100644 --- a/clang-tools-extra/clangd/Preamble.h +++ b/clang-tools-extra/clangd/Preamble.h @@ -43,7 +43,7 @@ namespace clangd { /// As we must avoid re-parsing the preamble, any information that can only /// be obtained during parsing must be eagerly captured and stored here. struct PreambleData { - PreambleData(llvm::StringRef Version, PrecompiledPreamble Preamble, + PreambleData(const ParseInputs &Inputs, PrecompiledPreamble Preamble, std::vector Diags, IncludeStructure Includes, MainFileMacros Macros, std::unique_ptr StatCache, @@ -80,7 +80,6 @@ using PreambleParsedCallback = std::shared_ptr buildPreamble(PathRef FileName, CompilerInvocation &CI, std::shared_ptr OldPreamble, - const tooling::CompileCommand &OldCompileCommand, const ParseInputs &Inputs, bool StoreInMemory, PreambleParsedCallback PreambleCallback); diff --git a/clang-tools-extra/clangd/TUScheduler.cpp b/clang-tools-extra/clangd/TUScheduler.cpp index 7188d0b..e5a0019 100644 --- a/clang-tools-extra/clangd/TUScheduler.cpp +++ b/clang-tools-extra/clangd/TUScheduler.cpp @@ -403,7 +403,6 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags) { std::tie(PrevInputs->CompileCommand, PrevInputs->Contents) == std::tie(Inputs.CompileCommand, Inputs.Contents); - tooling::CompileCommand OldCommand = PrevInputs->CompileCommand; bool RanCallbackForPrevInputs = RanASTCallback; { std::lock_guard Lock(Mutex); @@ -445,8 +444,7 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags) { Inputs.ForceRebuild ? std::shared_ptr() : getPossiblyStalePreamble(); std::shared_ptr NewPreamble = buildPreamble( - FileName, *Invocation, OldPreamble, OldCommand, Inputs, - StorePreambleInMemory, + FileName, *Invocation, OldPreamble, Inputs, StorePreambleInMemory, [this, Version(Inputs.Version)]( ASTContext &Ctx, std::shared_ptr PP, const CanonicalIncludes &CanonIncludes) { diff --git a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp index 7c9aa38..b020c2f 100644 --- a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp @@ -286,16 +286,16 @@ TEST(FileIndexTest, RebuildWithPreamble) { FileIndex Index; bool IndexUpdated = false; - buildPreamble( - FooCpp, *CI, /*OldPreamble=*/nullptr, tooling::CompileCommand(), PI, - /*StoreInMemory=*/true, - [&](ASTContext &Ctx, std::shared_ptr PP, - const CanonicalIncludes &CanonIncludes) { - EXPECT_FALSE(IndexUpdated) << "Expected only a single index update"; - IndexUpdated = true; - Index.updatePreamble(FooCpp, /*Version=*/"null", Ctx, std::move(PP), - CanonIncludes); - }); + buildPreamble(FooCpp, *CI, /*OldPreamble=*/nullptr, PI, + /*StoreInMemory=*/true, + [&](ASTContext &Ctx, std::shared_ptr PP, + const CanonicalIncludes &CanonIncludes) { + EXPECT_FALSE(IndexUpdated) + << "Expected only a single index update"; + IndexUpdated = true; + Index.updatePreamble(FooCpp, /*Version=*/"null", Ctx, + std::move(PP), CanonIncludes); + }); ASSERT_TRUE(IndexUpdated); // Check the index contains symbols from the preamble, but not from the main diff --git a/clang-tools-extra/clangd/unittests/TestTU.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp index 18f0589..909c125 100644 --- a/clang-tools-extra/clangd/unittests/TestTU.cpp +++ b/clang-tools-extra/clangd/unittests/TestTU.cpp @@ -67,8 +67,7 @@ ParsedAST TestTU::build() const { assert(CI && "Failed to build compilation invocation."); auto Preamble = buildPreamble(FullFilename, *CI, - /*OldPreamble=*/nullptr, - /*OldCompileCommand=*/Inputs.CompileCommand, Inputs, + /*OldPreamble=*/nullptr, Inputs, /*StoreInMemory=*/true, /*PreambleCallback=*/nullptr); auto AST = buildAST(FullFilename, std::move(CI), Diags.take(), Inputs, Preamble); -- 2.7.4