ilya-biryukov updated this revision to Diff 148599. ilya-biryukov added a comment.
- Rebase, fix merge conflicts - Simpler implemenataion of the Cache - s/IdleASTs/ASTCache Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D47063 Files: clangd/ClangdUnit.cpp clangd/ClangdUnit.h clangd/TUScheduler.cpp clangd/TUScheduler.h test/clangd/trace.test unittests/clangd/FileIndexTests.cpp
Index: unittests/clangd/FileIndexTests.cpp =================================================================== --- unittests/clangd/FileIndexTests.cpp +++ unittests/clangd/FileIndexTests.cpp @@ -11,6 +11,7 @@ #include "TestFS.h" #include "TestTU.h" #include "index/FileIndex.h" +#include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/PCHContainerOperations.h" #include "clang/Lex/Preprocessor.h" #include "clang/Tooling/CompilationDatabase.h" @@ -210,15 +211,15 @@ auto FooH = testPath("foo.h"); FileIndex Index; bool IndexUpdated = false; - CppFile File("foo.cpp", /*StorePreambleInMemory=*/true, - std::make_shared<PCHContainerOperations>(), - [&Index, &IndexUpdated](PathRef FilePath, ASTContext &Ctx, - std::shared_ptr<Preprocessor> PP) { - EXPECT_FALSE(IndexUpdated) - << "Expected only a single index update"; - IndexUpdated = true; - Index.update(FilePath, &Ctx, std::move(PP)); - }); + ASTBuilder Builder("foo.cpp", /*StorePreambleInMemory=*/true, + std::make_shared<PCHContainerOperations>(), + [&Index, &IndexUpdated](PathRef FilePath, ASTContext &Ctx, + std::shared_ptr<Preprocessor> PP) { + EXPECT_FALSE(IndexUpdated) + << "Expected only a single index update"; + IndexUpdated = true; + Index.update(FilePath, &Ctx, std::move(PP)); + }); // Preparse ParseInputs. ParseInputs PI; @@ -243,7 +244,9 @@ )cpp"; // Rebuild the file. - File.rebuild(std::move(PI)); + auto CI = Builder.buildCompilerInvocation(PI); + Builder.buildPreamble(*CI, /*OldPreamble=*/nullptr, tooling::CompileCommand(), + PI); ASSERT_TRUE(IndexUpdated); // Check the index contains symbols from the preamble, but not from the main Index: test/clangd/trace.test =================================================================== --- test/clangd/trace.test +++ test/clangd/trace.test @@ -8,14 +8,14 @@ # CHECK: "args": { # CHECK: "File": "{{.*(/|\\)}}foo.c" # CHECK: }, -# CHECK: "name": "Preamble", +# CHECK: "name": "BuildPreamble", # CHECK: "ph": "X", # CHECK: } # CHECK: { # CHECK: "args": { # CHECK: "File": "{{.*(/|\\)}}foo.c" # CHECK: }, -# CHECK: "name": "Build", +# CHECK: "name": "BuildAST", # CHECK: "ph": "X", # CHECK: } # CHECK: }, Index: clangd/TUScheduler.h =================================================================== --- clangd/TUScheduler.h +++ clangd/TUScheduler.h @@ -42,6 +42,15 @@ /// within a bounded amount of time. }; +/// Configuration of the AST retention policy. This only covers retention of +/// *idle* ASTs. If queue has operations requiring the AST, they might be +/// kept in memory. +struct ASTRetentionParams { + /// Maximum number of ASTs to be retained in memory when there are no pending + /// requests for them. + unsigned MaxRetainedASTs = 3; +}; + /// Handles running tasks for ClangdServer and managing the resources (e.g., /// preambles and ASTs) for opened files. /// TUScheduler is not thread-safe, only one thread should be providing updates @@ -53,7 +62,8 @@ public: TUScheduler(unsigned AsyncThreadsCount, bool StorePreamblesInMemory, PreambleParsedCallback PreambleCallback, - std::chrono::steady_clock::duration UpdateDebounce); + std::chrono::steady_clock::duration UpdateDebounce, + ASTRetentionParams RetentionConfig = {}); ~TUScheduler(); /// Returns estimated memory usage for each of the currently open files. @@ -99,11 +109,18 @@ /// This class stores per-file data in the Files map. struct FileData; +public: + /// Responsible for retaining and rebuilding idle ASTs. An implementation is + /// an LRU cache. + class ASTCache; + +private: const bool StorePreamblesInMemory; const std::shared_ptr<PCHContainerOperations> PCHOps; const PreambleParsedCallback PreambleCallback; Semaphore Barrier; llvm::StringMap<std::unique_ptr<FileData>> Files; + std::unique_ptr<ASTCache> IdleASTs; // None when running tasks synchronously and non-None when running tasks // asynchronously. llvm::Optional<AsyncTaskRunner> PreambleTasks; Index: clangd/TUScheduler.cpp =================================================================== --- clangd/TUScheduler.cpp +++ clangd/TUScheduler.cpp @@ -45,16 +45,82 @@ #include "TUScheduler.h" #include "Logger.h" #include "Trace.h" +#include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/PCHContainerOperations.h" +#include "llvm/ADT/ScopeExit.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Path.h" +#include <algorithm> #include <memory> #include <queue> #include <thread> namespace clang { namespace clangd { using std::chrono::steady_clock; + +namespace { +class ASTWorker; +} + +/// Provides an LRU cache of ASTs. +class TUScheduler::ASTCache { +public: + using Key = const ASTWorker *; + + ASTCache(unsigned MaxRetainedASTs) : MaxRetainedASTs(MaxRetainedASTs) {} + + /// Returns result of getUsedBytes() for the AST cached by \p K. + /// If no AST is cached, 0 is returned. + bool getUsedBytes(Key K) { + std::lock_guard<std::mutex> Lock(Mut); + auto It = findByKey(K); + if (It == LRU.end() || !It->second) + return 0; + return It->second->getUsedBytes(); + } + + /// Store the value in the pool, possibly removing the last used AST. + void put(Key K, std::unique_ptr<ParsedAST> V) { + std::unique_lock<std::mutex> Lock(Mut); + LRU.insert(LRU.begin(), {K, std::move(V)}); + if (LRU.size() <= MaxRetainedASTs) + return; + // We're past the limit, remove the last element. + std::unique_ptr<ParsedAST> ForCleanup = std::move(LRU.back().second); + LRU.pop_back(); + // AST destructor may need to run, make sure it happens outside the lock. + Lock.unlock(); + ForCleanup.reset(); + } + + /// Returns the cached value for \p K, or null if the value is not in the + /// cache anymore. + std::unique_ptr<ParsedAST> take(Key K) { + std::unique_lock<std::mutex> Lock(Mut); + auto Existing = findByKey(K); + if (Existing == LRU.end()) + return nullptr; + std::unique_ptr<ParsedAST> V = std::move(Existing->second); + LRU.erase(Existing); + return V; + } + +private: + using KVPair = std::pair<Key, std::unique_ptr<ParsedAST>>; + + std::vector<KVPair>::iterator findByKey(Key K) { + return std::find_if(LRU.begin(), LRU.end(), + [K](const KVPair &P) { return P.first == K; }); + } + + std::mutex Mut; + unsigned MaxRetainedASTs; + /// Items sorted in LRU order, i.e. first item is the most recently accessed + /// one. + std::vector<KVPair> LRU; /* GUARDED_BY(Mut) */ +}; + namespace { class ASTWorkerHandle; @@ -70,17 +136,19 @@ /// worker. class ASTWorker { friend class ASTWorkerHandle; - ASTWorker(llvm::StringRef File, Semaphore &Barrier, CppFile AST, bool RunSync, + ASTWorker(ASTBuilder Builder, TUScheduler::ASTCache &LRUCache, + Semaphore &Barrier, bool RunSync, steady_clock::duration UpdateDebounce); public: /// Create a new ASTWorker and return a handle to it. /// The processing thread is spawned using \p Tasks. However, when \p Tasks /// is null, all requests will be processed on the calling thread /// synchronously instead. \p Barrier is acquired when processing each /// request, it is be used to limit the number of actively running threads. - static ASTWorkerHandle Create(llvm::StringRef File, AsyncTaskRunner *Tasks, - Semaphore &Barrier, CppFile AST, + static ASTWorkerHandle Create(ASTBuilder Builder, + TUScheduler::ASTCache &IdleASTs, + AsyncTaskRunner *Tasks, Semaphore &Barrier, steady_clock::duration UpdateDebounce); ~ASTWorker(); @@ -119,22 +187,23 @@ llvm::Optional<WantDiagnostics> UpdateType; }; - const std::string File; + /// Handles retention of ASTs. + TUScheduler::ASTCache &IdleASTs; const bool RunSync; - // Time to wait after an update to see whether another update obsoletes it. + /// Time to wait after an update to see whether another update obsoletes it. const steady_clock::duration UpdateDebounce; Semaphore &Barrier; - // AST and FileInputs are only accessed on the processing thread from run(). - CppFile AST; - // Inputs, corresponding to the current state of AST. + /// Inputs, corresponding to the current state of AST. ParseInputs FileInputs; - // Guards members used by both TUScheduler and the worker thread. + /// CompilerInvocation used for FileInputs. + std::unique_ptr<CompilerInvocation> Invocation; + ASTBuilder Builder; + /// Size of the last AST + /// Guards members used by both TUScheduler and the worker thread. mutable std::mutex Mutex; std::shared_ptr<const PreambleData> LastBuiltPreamble; /* GUARDED_BY(Mutex) */ - // Result of getUsedBytes() after the last rebuild or read of AST. - std::size_t LastASTSize; /* GUARDED_BY(Mutex) */ - // Set to true to signal run() to finish processing. + /// Set to true to signal run() to finish processing. bool Done; /* GUARDED_BY(Mutex) */ std::deque<Request> Requests; /* GUARDED_BY(Mutex) */ mutable std::condition_variable RequestsCV; @@ -182,27 +251,30 @@ std::shared_ptr<ASTWorker> Worker; }; -ASTWorkerHandle ASTWorker::Create(llvm::StringRef File, AsyncTaskRunner *Tasks, - Semaphore &Barrier, CppFile AST, +ASTWorkerHandle ASTWorker::Create(ASTBuilder Builder, + TUScheduler::ASTCache &IdleASTs, + AsyncTaskRunner *Tasks, Semaphore &Barrier, steady_clock::duration UpdateDebounce) { - std::shared_ptr<ASTWorker> Worker(new ASTWorker( - File, Barrier, std::move(AST), /*RunSync=*/!Tasks, UpdateDebounce)); + std::shared_ptr<ASTWorker> Worker(new ASTWorker(std::move(Builder), IdleASTs, + Barrier, /*RunSync=*/!Tasks, + UpdateDebounce)); if (Tasks) - Tasks->runAsync("worker:" + llvm::sys::path::filename(File), - [Worker]() { Worker->run(); }); + Tasks->runAsync( + "worker:" + llvm::sys::path::filename(Worker->Builder.getFileName()), + [Worker]() { Worker->run(); }); return ASTWorkerHandle(std::move(Worker)); } -ASTWorker::ASTWorker(llvm::StringRef File, Semaphore &Barrier, CppFile AST, - bool RunSync, steady_clock::duration UpdateDebounce) - : File(File), RunSync(RunSync), UpdateDebounce(UpdateDebounce), - Barrier(Barrier), AST(std::move(AST)), Done(false) { - if (RunSync) - return; -} +ASTWorker::ASTWorker(ASTBuilder Builder, TUScheduler::ASTCache &LRUCache, + Semaphore &Barrier, bool RunSync, + steady_clock::duration UpdateDebounce) + : IdleASTs(LRUCache), RunSync(RunSync), UpdateDebounce(UpdateDebounce), + Barrier(Barrier), Builder(std::move(Builder)), Done(false) {} ASTWorker::~ASTWorker() { + // Make sure we remove the cached AST, if any. + IdleASTs.take(this); #ifndef NDEBUG std::lock_guard<std::mutex> Lock(Mutex); assert(Done && "handle was not destroyed"); @@ -213,20 +285,39 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags, UniqueFunction<void(std::vector<Diag>)> OnUpdated) { auto Task = [=](decltype(OnUpdated) OnUpdated) mutable { + tooling::CompileCommand OldCommand = std::move(FileInputs.CompileCommand); FileInputs = Inputs; - auto Diags = AST.rebuild(std::move(Inputs)); + // Remove the old AST if it's still in cache. + IdleASTs.take(this); + + log("Updating file " + Builder.getFileName() + " with command [" + + Inputs.CompileCommand.Directory + "] " + + llvm::join(Inputs.CompileCommand.CommandLine, " ")); + // Rebuild the preamble and the AST. + Invocation = Builder.buildCompilerInvocation(Inputs); + if (!Invocation) + return; + std::shared_ptr<const PreambleData> NewPreamble = Builder.buildPreamble( + *Invocation, getPossiblyStalePreamble(), OldCommand, Inputs); { std::lock_guard<std::mutex> Lock(Mutex); - if (AST.getPreamble()) - LastBuiltPreamble = AST.getPreamble(); - LastASTSize = AST.getUsedBytes(); + if (NewPreamble) + LastBuiltPreamble = NewPreamble; } + // Build the AST for diagnostics. + llvm::Optional<ParsedAST> AST = + Builder.buildAST(llvm::make_unique<CompilerInvocation>(*Invocation), + NewPreamble, Inputs); // We want to report the diagnostics even if this update was cancelled. // It seems more useful than making the clients wait indefinitely if they // spam us with updates. - if (Diags && WantDiags != WantDiagnostics::No) - OnUpdated(std::move(*Diags)); + if (WantDiags != WantDiagnostics::No && AST) { + OnUpdated(AST->getDiagnostics()); + } + // Stash the AST in the cache for further use. + IdleASTs.put(this, + AST ? llvm::make_unique<ParsedAST>(std::move(*AST)) : nullptr); }; startTask("Update", Bind(Task, std::move(OnUpdated)), WantDiags); @@ -236,20 +327,24 @@ llvm::StringRef Name, UniqueFunction<void(llvm::Expected<InputsAndAST>)> Action) { auto Task = [=](decltype(Action) Action) { - ParsedAST *ActualAST = AST.getAST(); - if (!ActualAST) { - Action(llvm::make_error<llvm::StringError>("invalid AST", - llvm::errc::invalid_argument)); - return; + std::unique_ptr<ParsedAST> AST = IdleASTs.take(this); + if (!AST) { + // Try rebuilding the AST. + llvm::Optional<ParsedAST> NewAST = + Invocation ? Builder.buildAST( + llvm::make_unique<CompilerInvocation>(*Invocation), + getPossiblyStalePreamble(), FileInputs) + : llvm::None; + if (NewAST) + AST = llvm::make_unique<ParsedAST>(std::move(*NewAST)); } - Action(InputsAndAST{FileInputs, *ActualAST}); - - // Size of the AST might have changed after reads too, e.g. if some decls - // were deserialized from preamble. - std::lock_guard<std::mutex> Lock(Mutex); - LastASTSize = ActualAST->getUsedBytes(); + if (!AST) + return Action(llvm::make_error<llvm::StringError>( + "invalid AST", llvm::errc::invalid_argument)); + Action(InputsAndAST{FileInputs, *AST}); + // Stash the AST in the cache for further use. + IdleASTs.put(this, std::move(AST)); }; - startTask(Name, Bind(Task, std::move(Action)), /*UpdateType=*/llvm::None); } @@ -261,8 +356,13 @@ } std::size_t ASTWorker::getUsedBytes() const { - std::lock_guard<std::mutex> Lock(Mutex); - return LastASTSize; + // Note that we don't report the size of ASTs currently used for processing + // the in-flight requests. We used this information for debugging purposes + // only, so this should be fine. + std::size_t Result = IdleASTs.getUsedBytes(this); + if (auto Preamble = getPossiblyStalePreamble()) + Result += Preamble->Preamble.getSize(); + return Result; } void ASTWorker::stop() { @@ -278,7 +378,8 @@ llvm::Optional<WantDiagnostics> UpdateType) { if (RunSync) { assert(!Done && "running a task after stop()"); - trace::Span Tracer(Name + ":" + llvm::sys::path::filename(File)); + trace::Span Tracer(Name + ":" + + llvm::sys::path::filename(Builder.getFileName())); Task(); return; } @@ -415,10 +516,12 @@ TUScheduler::TUScheduler(unsigned AsyncThreadsCount, bool StorePreamblesInMemory, PreambleParsedCallback PreambleCallback, - steady_clock::duration UpdateDebounce) + std::chrono::steady_clock::duration UpdateDebounce, + ASTRetentionParams RetentionConfig) : StorePreamblesInMemory(StorePreamblesInMemory), PCHOps(std::make_shared<PCHContainerOperations>()), PreambleCallback(std::move(PreambleCallback)), Barrier(AsyncThreadsCount), + IdleASTs(llvm::make_unique<ASTCache>(RetentionConfig.MaxRetainedASTs)), UpdateDebounce(UpdateDebounce) { if (0 < AsyncThreadsCount) { PreambleTasks.emplace(); @@ -454,9 +557,9 @@ if (!FD) { // Create a new worker to process the AST-related tasks. ASTWorkerHandle Worker = ASTWorker::Create( - File, WorkerThreads ? WorkerThreads.getPointer() : nullptr, Barrier, - CppFile(File, StorePreamblesInMemory, PCHOps, PreambleCallback), - UpdateDebounce); + ASTBuilder(File, StorePreamblesInMemory, PCHOps, PreambleCallback), + *IdleASTs, WorkerThreads ? WorkerThreads.getPointer() : nullptr, + Barrier, UpdateDebounce); FD = std::unique_ptr<FileData>(new FileData{ Inputs.Contents, Inputs.CompileCommand, std::move(Worker)}); } else { Index: clangd/ClangdUnit.h =================================================================== --- clangd/ClangdUnit.h +++ clangd/ClangdUnit.h @@ -48,6 +48,7 @@ std::vector<serialization::DeclID> TopLevelDeclIDs, std::vector<Diag> Diags, std::vector<Inclusion> Inclusions); + tooling::CompileCommand CompileCommand; PrecompiledPreamble Preamble; std::vector<serialization::DeclID> TopLevelDeclIDs; std::vector<Diag> Diags; @@ -130,45 +131,40 @@ using PreambleParsedCallback = std::function<void( PathRef Path, ASTContext &, std::shared_ptr<clang::Preprocessor>)>; -/// Manages resources, required by clangd. Allows to rebuild file with new -/// contents, and provides AST and Preamble for it. -class CppFile { +/// A helper class that handles building preambles and ASTs for a file. Also +/// adds some logging. +class ASTBuilder { public: - CppFile(PathRef FileName, bool StorePreamblesInMemory, - std::shared_ptr<PCHContainerOperations> PCHs, - PreambleParsedCallback PreambleCallback); - - /// Rebuild the AST and the preamble. - /// Returns a list of diagnostics or llvm::None, if an error occured. - llvm::Optional<std::vector<Diag>> rebuild(ParseInputs &&Inputs); - /// Returns the last built preamble. - const std::shared_ptr<const PreambleData> &getPreamble() const; - /// Returns the last built AST. - ParsedAST *getAST() const; - /// Returns an estimated size, in bytes, currently occupied by the AST and the - /// Preamble. - std::size_t getUsedBytes() const; + ASTBuilder(PathRef FileName, bool StorePreambleInMemory, + std::shared_ptr<PCHContainerOperations> PCHs, + PreambleParsedCallback PreambleCallback); -private: - /// Build a new preamble for \p Inputs. If the current preamble can be reused, - /// it is returned instead. - /// This method is const to ensure we don't incidentally modify any fields. + PathRef getFileName() const; + + /// Builds compiler invocation that could be used to build AST or preamble. + std::unique_ptr<CompilerInvocation> + buildCompilerInvocation(const ParseInputs &Inputs) const; + + /// Rebuild the preamble for the new inputs unless the old one can be reused. + /// If \p OldPreamble can be reused, it is returned unchanged. + /// If \p OldPreamble is null, always builds the preamble. std::shared_ptr<const PreambleData> - rebuildPreamble(CompilerInvocation &CI, - const tooling::CompileCommand &Command, - IntrusiveRefCntPtr<vfs::FileSystem> FS, - llvm::MemoryBuffer &ContentsBuffer) const; - - const Path FileName; - const bool StorePreamblesInMemory; - - /// The last CompileCommand used to build AST and Preamble. - tooling::CompileCommand Command; - /// The last parsed AST. - llvm::Optional<ParsedAST> AST; - /// The last built Preamble. - std::shared_ptr<const PreambleData> Preamble; - /// Utility class required by clang + buildPreamble(CompilerInvocation &CI, + std::shared_ptr<const PreambleData> OldPreamble, + const tooling::CompileCommand &OldCompileCommand, + const ParseInputs &Inputs) const; + + /// Builds the AST using an existing \p Preamble. Note that \p Preamble is + /// always used, without the checks that it is not outdated. + /// To get the correct preamble, use buildPreamble. + llvm::Optional<ParsedAST> + buildAST(std::unique_ptr<CompilerInvocation> CI, + std::shared_ptr<const PreambleData> Preamble, + const ParseInputs &Inputs) const; + +private: + Path FileName; + bool StorePreambleInMemory; std::shared_ptr<PCHContainerOperations> PCHs; /// This is called after the file is parsed. This can be nullptr if there is /// no callback. Index: clangd/ClangdUnit.cpp =================================================================== --- clangd/ClangdUnit.cpp +++ clangd/ClangdUnit.cpp @@ -195,8 +195,12 @@ ASTDiags.EndSourceFile(); std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls(); + std::vector<Diag> Diags = ASTDiags.take(); + // Add diagnostics from the preamble, if any. + if (Preamble) + Diags.insert(Diags.begin(), Preamble->Diags.begin(), Preamble->Diags.end()); return ParsedAST(std::move(Preamble), std::move(Clang), std::move(Action), - std::move(ParsedDecls), ASTDiags.take(), + std::move(ParsedDecls), std::move(Diags), std::move(Inclusions)); } @@ -288,19 +292,16 @@ assert(this->Action); } -CppFile::CppFile(PathRef FileName, bool StorePreamblesInMemory, - std::shared_ptr<PCHContainerOperations> PCHs, - PreambleParsedCallback PreambleCallback) - : FileName(FileName), StorePreamblesInMemory(StorePreamblesInMemory), - PCHs(std::move(PCHs)), PreambleCallback(std::move(PreambleCallback)) { - log("Created CppFile for " + FileName); -} +ASTBuilder::ASTBuilder(PathRef FileName, bool StorePreambleInMemory, + std::shared_ptr<PCHContainerOperations> PCHs, + PreambleParsedCallback PreambleCallback) + : FileName(FileName), StorePreambleInMemory(StorePreambleInMemory), + PCHs(std::move(PCHs)), PreambleCallback(std::move(PreambleCallback)) {} -llvm::Optional<std::vector<Diag>> CppFile::rebuild(ParseInputs &&Inputs) { - log("Rebuilding file " + FileName + " with command [" + - Inputs.CompileCommand.Directory + "] " + - llvm::join(Inputs.CompileCommand.CommandLine, " ")); +PathRef ASTBuilder::getFileName() const { return FileName; } +std::unique_ptr<CompilerInvocation> +ASTBuilder::buildCompilerInvocation(const ParseInputs &Inputs) const { std::vector<const char *> ArgStrs; for (const auto &S : Inputs.CompileCommand.CommandLine) ArgStrs.push_back(S.c_str()); @@ -311,97 +312,46 @@ // working dirs. } - // Prepare CompilerInvocation. - std::unique_ptr<CompilerInvocation> CI; - { - // FIXME(ibiryukov): store diagnostics from CommandLine when we start - // reporting them. - IgnoreDiagnostics IgnoreDiagnostics; - IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = - CompilerInstance::createDiagnostics(new DiagnosticOptions, - &IgnoreDiagnostics, false); - CI = createInvocationFromCommandLine(ArgStrs, CommandLineDiagsEngine, - Inputs.FS); - if (!CI) { - log("Could not build CompilerInvocation for file " + FileName); - AST = llvm::None; - Preamble = nullptr; - return llvm::None; - } - // createInvocationFromCommandLine sets DisableFree. - CI->getFrontendOpts().DisableFree = false; - CI->getLangOpts()->CommentOpts.ParseAllComments = true; - } - - std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = - llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, FileName); - - // Compute updated Preamble. - std::shared_ptr<const PreambleData> NewPreamble = - rebuildPreamble(*CI, Inputs.CompileCommand, Inputs.FS, *ContentsBuffer); - - // Remove current AST to avoid wasting memory. - AST = llvm::None; - // Compute updated AST. - llvm::Optional<ParsedAST> NewAST; - { - trace::Span Tracer("Build"); - SPAN_ATTACH(Tracer, "File", FileName); - NewAST = ParsedAST::Build(std::move(CI), NewPreamble, - std::move(ContentsBuffer), PCHs, Inputs.FS); - } - - std::vector<Diag> Diagnostics; - if (NewAST) { - // Collect diagnostics from both the preamble and the AST. - if (NewPreamble) - Diagnostics = NewPreamble->Diags; - Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(), - NewAST->getDiagnostics().end()); + // FIXME(ibiryukov): store diagnostics from CommandLine when we start + // reporting them. + IgnoreDiagnostics IgnoreDiagnostics; + IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = + CompilerInstance::createDiagnostics(new DiagnosticOptions, + &IgnoreDiagnostics, false); + std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine( + ArgStrs, CommandLineDiagsEngine, Inputs.FS); + if (!CI) { + log("Could not build CompilerInvocation for file " + FileName); + return nullptr; } - - // Write the results of rebuild into class fields. - Command = std::move(Inputs.CompileCommand); - Preamble = std::move(NewPreamble); - AST = std::move(NewAST); - return Diagnostics; -} - -const std::shared_ptr<const PreambleData> &CppFile::getPreamble() const { - return Preamble; -} - -ParsedAST *CppFile::getAST() const { - // We could add mutable to AST instead of const_cast here, but that would also - // allow writing to AST from const methods. - return AST ? const_cast<ParsedAST *>(AST.getPointer()) : nullptr; -} - -std::size_t CppFile::getUsedBytes() const { - std::size_t Total = 0; - if (AST) - Total += AST->getUsedBytes(); - if (StorePreamblesInMemory && Preamble) - Total += Preamble->Preamble.getSize(); - return Total; + // createInvocationFromCommandLine sets DisableFree. + CI->getFrontendOpts().DisableFree = false; + CI->getLangOpts()->CommentOpts.ParseAllComments = true; + return CI; } std::shared_ptr<const PreambleData> -CppFile::rebuildPreamble(CompilerInvocation &CI, - const tooling::CompileCommand &Command, - IntrusiveRefCntPtr<vfs::FileSystem> FS, - llvm::MemoryBuffer &ContentsBuffer) const { - const auto &OldPreamble = this->Preamble; - auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), &ContentsBuffer, 0); - if (OldPreamble && compileCommandsAreEqual(this->Command, Command) && - OldPreamble->Preamble.CanReuse(CI, &ContentsBuffer, Bounds, FS.get())) { +ASTBuilder::buildPreamble(CompilerInvocation &CI, + std::shared_ptr<const PreambleData> OldPreamble, + const tooling::CompileCommand &OldCompileCommand, + const ParseInputs &Inputs) const { + // Note that we don't need to copy the input contents, preamble can live + // without those. + auto ContentsBuffer = llvm::MemoryBuffer::getMemBuffer(Inputs.Contents); + auto Bounds = + ComputePreambleBounds(*CI.getLangOpts(), ContentsBuffer.get(), 0); + + if (OldPreamble && + compileCommandsAreEqual(Inputs.CompileCommand, OldCompileCommand) && + OldPreamble->Preamble.CanReuse(CI, ContentsBuffer.get(), Bounds, + Inputs.FS.get())) { log("Reusing preamble for file " + Twine(FileName)); return OldPreamble; } log("Preamble for file " + Twine(FileName) + " cannot be reused. Attempting to rebuild it."); - trace::Span Tracer("Preamble"); + trace::Span Tracer("BuildPreamble"); SPAN_ATTACH(Tracer, "File", FileName); StoreDiags PreambleDiagnostics; IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine = @@ -415,17 +365,16 @@ CppFilePreambleCallbacks SerializedDeclsCollector(FileName, PreambleCallback); auto BuiltPreamble = PrecompiledPreamble::Build( - CI, &ContentsBuffer, Bounds, *PreambleDiagsEngine, FS, PCHs, - /*StoreInMemory=*/StorePreamblesInMemory, SerializedDeclsCollector); + CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, Inputs.FS, PCHs, + /*StoreInMemory=*/StorePreambleInMemory, SerializedDeclsCollector); // When building the AST for the main file, we do want the function // bodies. CI.getFrontendOpts().SkipFunctionBodies = false; if (BuiltPreamble) { log("Built preamble of size " + Twine(BuiltPreamble->getSize()) + " for file " + Twine(FileName)); - return std::make_shared<PreambleData>( std::move(*BuiltPreamble), SerializedDeclsCollector.takeTopLevelDeclIDs(), @@ -436,6 +385,22 @@ } } +llvm::Optional<ParsedAST> +ASTBuilder::buildAST(std::unique_ptr<CompilerInvocation> CI, + std::shared_ptr<const PreambleData> Preamble, + const ParseInputs &Inputs) const { + trace::Span Tracer("BuildAST"); + SPAN_ATTACH(Tracer, "File", FileName); + llvm::Optional<ParsedAST> AST; + { + trace::Span ParseTracer("ParseAST"); + AST = ParsedAST::Build( + std::move(CI), std::move(Preamble), + llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents), PCHs, Inputs.FS); + } + return AST; +} + SourceLocation clangd::getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos, const FileID FID) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits