Unverified Commit ecd3e678 authored by Kadir Cetinkaya's avatar Kadir Cetinkaya
Browse files

[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
parent c39cb1c0
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -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<Diag> Diags, IncludeStructure Includes,
                           MainFileMacros Macros,
                           std::unique_ptr<PreambleFileStatusCache> 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<const PreambleData>
buildPreamble(PathRef FileName, CompilerInvocation &CI,
              std::shared_ptr<const PreambleData> 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<Diag> Diags = PreambleDiagnostics.take();
    return std::make_shared<PreambleData>(
        Inputs.Version, std::move(*BuiltPreamble), std::move(Diags),
        Inputs, std::move(*BuiltPreamble), std::move(Diags),
        SerializedDeclsCollector.takeIncludes(),
        SerializedDeclsCollector.takeMacros(), std::move(StatCache),
        SerializedDeclsCollector.takeCanonicalIncludes());
+1 −2
Original line number Diff line number Diff line
@@ -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<Diag> Diags, IncludeStructure Includes,
               MainFileMacros Macros,
               std::unique_ptr<PreambleFileStatusCache> StatCache,
@@ -80,7 +80,6 @@ using PreambleParsedCallback =
std::shared_ptr<const PreambleData>
buildPreamble(PathRef FileName, CompilerInvocation &CI,
              std::shared_ptr<const PreambleData> OldPreamble,
              const tooling::CompileCommand &OldCompileCommand,
              const ParseInputs &Inputs, bool StoreInMemory,
              PreambleParsedCallback PreambleCallback);

+1 −3
Original line number Diff line number Diff line
@@ -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<std::mutex> Lock(Mutex);
@@ -445,8 +444,7 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags) {
        Inputs.ForceRebuild ? std::shared_ptr<const PreambleData>()
                            : getPossiblyStalePreamble();
    std::shared_ptr<const PreambleData> NewPreamble = buildPreamble(
        FileName, *Invocation, OldPreamble, OldCommand, Inputs,
        StorePreambleInMemory,
        FileName, *Invocation, OldPreamble, Inputs, StorePreambleInMemory,
        [this, Version(Inputs.Version)](
            ASTContext &Ctx, std::shared_ptr<clang::Preprocessor> PP,
            const CanonicalIncludes &CanonIncludes) {
+10 −10
Original line number Diff line number Diff line
@@ -286,15 +286,15 @@ TEST(FileIndexTest, RebuildWithPreamble) {

  FileIndex Index;
  bool IndexUpdated = false;
  buildPreamble(
      FooCpp, *CI, /*OldPreamble=*/nullptr, tooling::CompileCommand(), PI,
  buildPreamble(FooCpp, *CI, /*OldPreamble=*/nullptr, PI,
                /*StoreInMemory=*/true,
                [&](ASTContext &Ctx, std::shared_ptr<Preprocessor> PP,
                    const CanonicalIncludes &CanonIncludes) {
        EXPECT_FALSE(IndexUpdated) << "Expected only a single index update";
                  EXPECT_FALSE(IndexUpdated)
                      << "Expected only a single index update";
                  IndexUpdated = true;
        Index.updatePreamble(FooCpp, /*Version=*/"null", Ctx, std::move(PP),
                             CanonIncludes);
                  Index.updatePreamble(FooCpp, /*Version=*/"null", Ctx,
                                       std::move(PP), CanonIncludes);
                });
  ASSERT_TRUE(IndexUpdated);

+1 −2
Original line number Diff line number Diff line
@@ -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);