Author: Sam McCall Date: 2020-04-27T00:14:03+02:00 New Revision: 6880c4dfa3981ec26d44b5f4844b641342a4e3e8
URL: https://github.com/llvm/llvm-project/commit/6880c4dfa3981ec26d44b5f4844b641342a4e3e8 DIFF: https://github.com/llvm/llvm-project/commit/6880c4dfa3981ec26d44b5f4844b641342a4e3e8.diff LOG: [clangd] Fold buildAST into ParsedAST::build. NFCI Added: Modified: clang-tools-extra/clangd/ParsedAST.cpp clang-tools-extra/clangd/ParsedAST.h clang-tools-extra/clangd/TUScheduler.cpp clang-tools-extra/clangd/unittests/TestTU.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/ParsedAST.cpp b/clang-tools-extra/clangd/ParsedAST.cpp index bf09b14b1427..7b2426115df7 100644 --- a/clang-tools-extra/clangd/ParsedAST.cpp +++ b/clang-tools-extra/clangd/ParsedAST.cpp @@ -239,13 +239,22 @@ void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) { } llvm::Optional<ParsedAST> -ParsedAST::build(llvm::StringRef Version, +ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs, std::unique_ptr<clang::CompilerInvocation> CI, llvm::ArrayRef<Diag> CompilerInvocationDiags, - std::shared_ptr<const PreambleData> Preamble, - std::unique_ptr<llvm::MemoryBuffer> Buffer, - llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, - const SymbolIndex *Index, const ParseOptions &Opts) { + std::shared_ptr<const PreambleData> Preamble) { + trace::Span Tracer("BuildAST"); + SPAN_ATTACH(Tracer, "File", Filename); + + auto VFS = Inputs.FS; + if (Preamble && Preamble->StatCache) + VFS = Preamble->StatCache->getConsumingFS(std::move(VFS)); + if (VFS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) { + log("Couldn't set working directory when building the preamble."); + // We proceed anyway, our lit-tests rely on results for non-existing working + // dirs. + } + assert(CI); // Command-line parsing sets DisableFree to true by default, but we don't want // to leak memory in clangd. @@ -255,18 +264,17 @@ ParsedAST::build(llvm::StringRef Version, // Recovery expression currently only works for C++. if (CI->getLangOpts()->CPlusPlus) - CI->getLangOpts()->RecoveryAST = Opts.BuildRecoveryAST; + CI->getLangOpts()->RecoveryAST = Inputs.Opts.BuildRecoveryAST; // This is on-by-default in windows to allow parsing SDK headers, but it // breaks many features. Disable it for the main-file (not preamble). CI->getLangOpts()->DelayedTemplateParsing = false; StoreDiags ASTDiags; - std::string Content = std::string(Buffer->getBuffer()); - std::string Filename = - std::string(Buffer->getBufferIdentifier()); // Absolute. - auto Clang = prepareCompilerInstance(std::move(CI), PreamblePCH, - std::move(Buffer), VFS, ASTDiags); + auto Clang = prepareCompilerInstance( + std::move(CI), PreamblePCH, + llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), VFS, + ASTDiags); if (!Clang) return None; @@ -290,12 +298,12 @@ ParsedAST::build(llvm::StringRef Version, { trace::Span Tracer("ClangTidyInit"); dlog("ClangTidy configuration for file {0}: {1}", Filename, - tidy::configurationAsText(Opts.ClangTidyOpts)); + tidy::configurationAsText(Inputs.Opts.ClangTidyOpts)); tidy::ClangTidyCheckFactories CTFactories; for (const auto &E : tidy::ClangTidyModuleRegistry::entries()) E.instantiate()->addCheckFactories(CTFactories); CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>( - tidy::ClangTidyGlobalOptions(), Opts.ClangTidyOpts)); + tidy::ClangTidyGlobalOptions(), Inputs.Opts.ClangTidyOpts)); CTContext->setDiagnosticsEngine(&Clang->getDiagnostics()); CTContext->setASTContext(&Clang->getASTContext()); CTContext->setCurrentFile(Filename); @@ -345,16 +353,17 @@ ParsedAST::build(llvm::StringRef Version, // (e.g. incomplete type) and attach include insertion fixes to diagnostics. llvm::Optional<IncludeFixer> FixIncludes; auto BuildDir = VFS->getCurrentWorkingDirectory(); - if (Opts.SuggestMissingIncludes && Index && !BuildDir.getError()) { - auto Style = getFormatStyleForFile(Filename, Content, VFS.get()); + if (Inputs.Opts.SuggestMissingIncludes && Inputs.Index && + !BuildDir.getError()) { + auto Style = getFormatStyleForFile(Filename, Inputs.Contents, VFS.get()); auto Inserter = std::make_shared<IncludeInserter>( - Filename, Content, Style, BuildDir.get(), + Filename, Inputs.Contents, Style, BuildDir.get(), &Clang->getPreprocessor().getHeaderSearchInfo()); if (Preamble) { for (const auto &Inc : Preamble->Includes.MainFileIncludes) Inserter->addExisting(Inc); } - FixIncludes.emplace(Filename, Inserter, *Index, + FixIncludes.emplace(Filename, Inserter, *Inputs.Index, /*IndexRequestLimit=*/5); ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl, const clang::Diagnostic &Info) { @@ -434,7 +443,7 @@ ParsedAST::build(llvm::StringRef Version, std::vector<Diag> D = ASTDiags.take(CTContext.getPointer()); Diags.insert(Diags.end(), D.begin(), D.end()); } - return ParsedAST(Version, std::move(Preamble), std::move(Clang), + return ParsedAST(Inputs.Version, std::move(Preamble), std::move(Clang), std::move(Action), std::move(Tokens), std::move(Macros), std::move(ParsedDecls), std::move(Diags), std::move(Includes), std::move(CanonIncludes)); @@ -536,28 +545,5 @@ ParsedAST::ParsedAST(llvm::StringRef Version, assert(this->Action); } -llvm::Optional<ParsedAST> -buildAST(PathRef FileName, std::unique_ptr<CompilerInvocation> Invocation, - llvm::ArrayRef<Diag> CompilerInvocationDiags, - const ParseInputs &Inputs, - std::shared_ptr<const PreambleData> Preamble) { - trace::Span Tracer("BuildAST"); - SPAN_ATTACH(Tracer, "File", FileName); - - auto VFS = Inputs.FS; - if (Preamble && Preamble->StatCache) - VFS = Preamble->StatCache->getConsumingFS(std::move(VFS)); - if (VFS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) { - log("Couldn't set working directory when building the preamble."); - // We proceed anyway, our lit-tests rely on results for non-existing working - // dirs. - } - - return ParsedAST::build( - Inputs.Version, std::move(Invocation), CompilerInvocationDiags, Preamble, - llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, FileName), - std::move(VFS), Inputs.Index, Inputs.Opts); -} - } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/ParsedAST.h b/clang-tools-extra/clangd/ParsedAST.h index 8620f31ff1e5..88fb6c3b2d65 100644 --- a/clang-tools-extra/clangd/ParsedAST.h +++ b/clang-tools-extra/clangd/ParsedAST.h @@ -15,7 +15,6 @@ // - capturing diagnostics for later access // - running clang-tidy checks checks // -// //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H @@ -45,15 +44,14 @@ class SymbolIndex; /// Stores and provides access to parsed AST. class ParsedAST { public: - /// Attempts to run Clang and store parsed AST. If \p Preamble is non-null - /// it is reused during parsing. + /// Attempts to run Clang and store the parsed AST. + /// If \p Preamble is non-null it is reused during parsing. + /// This function does not check if preamble is valid to reuse. static llvm::Optional<ParsedAST> - build(llvm::StringRef Version, std::unique_ptr<clang::CompilerInvocation> CI, + build(llvm::StringRef Filename, const ParseInputs &Inputs, + std::unique_ptr<clang::CompilerInvocation> CI, llvm::ArrayRef<Diag> CompilerInvocationDiags, - std::shared_ptr<const PreambleData> Preamble, - std::unique_ptr<llvm::MemoryBuffer> Buffer, - llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, - const SymbolIndex *Index, const ParseOptions &Opts); + std::shared_ptr<const PreambleData> Preamble); ParsedAST(ParsedAST &&Other); ParsedAST &operator=(ParsedAST &&Other); @@ -141,15 +139,6 @@ class ParsedAST { CanonicalIncludes CanonIncludes; }; -/// Build an AST from provided user inputs. This function does not check if -/// preamble can be reused, as this function expects that \p Preamble is the -/// result of calling buildPreamble. -llvm::Optional<ParsedAST> -buildAST(PathRef FileName, std::unique_ptr<CompilerInvocation> Invocation, - llvm::ArrayRef<Diag> CompilerInvocationDiags, - const ParseInputs &Inputs, - std::shared_ptr<const PreambleData> Preamble); - /// For testing/debugging purposes. Note that this method deserializes all /// unserialized Decls, so use with care. void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS); diff --git a/clang-tools-extra/clangd/TUScheduler.cpp b/clang-tools-extra/clangd/TUScheduler.cpp index 2f2abb59ab3c..1ff6bbaebc24 100644 --- a/clang-tools-extra/clangd/TUScheduler.cpp +++ b/clang-tools-extra/clangd/TUScheduler.cpp @@ -665,9 +665,9 @@ void ASTWorker::runWithAST( // return a compatible preamble as ASTWorker::update blocks. llvm::Optional<ParsedAST> NewAST; if (Invocation) { - NewAST = buildAST(FileName, std::move(Invocation), - CompilerInvocationDiagConsumer.take(), FileInputs, - getPossiblyStalePreamble()); + NewAST = ParsedAST::build(FileName, FileInputs, std::move(Invocation), + CompilerInvocationDiagConsumer.take(), + getPossiblyStalePreamble()); ++ASTBuildCount; } AST = NewAST ? std::make_unique<ParsedAST>(std::move(*NewAST)) : nullptr; @@ -804,8 +804,8 @@ void ASTWorker::generateDiagnostics( llvm::Optional<std::unique_ptr<ParsedAST>> AST = IdleASTs.take(this); if (!AST || !InputsAreLatest) { auto RebuildStartTime = DebouncePolicy::clock::now(); - llvm::Optional<ParsedAST> NewAST = buildAST( - FileName, std::move(Invocation), CIDiags, Inputs, LatestPreamble); + llvm::Optional<ParsedAST> NewAST = ParsedAST::build( + FileName, Inputs, std::move(Invocation), CIDiags, LatestPreamble); auto RebuildDuration = DebouncePolicy::clock::now() - RebuildStartTime; ++ASTBuildCount; // Try to record the AST-build time, to inform future update debouncing. diff --git a/clang-tools-extra/clangd/unittests/TestTU.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp index 640d067261bd..8ff1120452fc 100644 --- a/clang-tools-extra/clangd/unittests/TestTU.cpp +++ b/clang-tools-extra/clangd/unittests/TestTU.cpp @@ -73,8 +73,8 @@ ParsedAST TestTU::build() const { auto Preamble = buildPreamble(testPath(Filename), *CI, Inputs, /*StoreInMemory=*/true, /*PreambleCallback=*/nullptr); - auto AST = buildAST(testPath(Filename), std::move(CI), Diags.take(), Inputs, - Preamble); + auto AST = ParsedAST::build(testPath(Filename), Inputs, std::move(CI), + Diags.take(), Preamble); if (!AST.hasValue()) { ADD_FAILURE() << "Failed to build code:\n" << Code; llvm_unreachable("Failed to build TestTU!"); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits