Author: Sam McCall Date: 2021-12-15T02:13:50+01:00 New Revision: 6917f87b3c7c3ea927f8834302a5e9c82fc40a9b
URL: https://github.com/llvm/llvm-project/commit/6917f87b3c7c3ea927f8834302a5e9c82fc40a9b DIFF: https://github.com/llvm/llvm-project/commit/6917f87b3c7c3ea927f8834302a5e9c82fc40a9b.diff LOG: [clangd] Cleanup unneeded use of shared_ptr. NFC Added: Modified: clang-tools-extra/clangd/ClangdServer.cpp clang-tools-extra/clangd/Preamble.cpp clang-tools-extra/clangd/Preamble.h clang-tools-extra/clangd/TUScheduler.cpp clang-tools-extra/clangd/TUScheduler.h clang-tools-extra/clangd/index/FileIndex.cpp clang-tools-extra/clangd/index/FileIndex.h clang-tools-extra/clangd/index/SymbolCollector.cpp clang-tools-extra/clangd/index/SymbolCollector.h clang-tools-extra/clangd/tool/Check.cpp clang-tools-extra/clangd/unittests/FileIndexTests.cpp clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp clang-tools-extra/clangd/unittests/TestTU.cpp clang-tools-extra/clangd/unittests/TestWorkspace.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp index ae486d176ba91..e106c31ce0df7 100644 --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -71,10 +71,10 @@ struct UpdateIndexCallbacks : public ParsingCallbacks { : FIndex(FIndex), ServerCallbacks(ServerCallbacks) {} void onPreambleAST(PathRef Path, llvm::StringRef Version, ASTContext &Ctx, - std::shared_ptr<clang::Preprocessor> PP, + Preprocessor &PP, const CanonicalIncludes &CanonIncludes) override { if (FIndex) - FIndex->updatePreamble(Path, Version, Ctx, std::move(PP), CanonIncludes); + FIndex->updatePreamble(Path, Version, Ctx, PP, CanonIncludes); } void onMainAST(PathRef Path, ParsedAST &AST, PublishFn Publish) override { diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp index 82d16b9c0e7cb..34f3caa5e34b5 100644 --- a/clang-tools-extra/clangd/Preamble.cpp +++ b/clang-tools-extra/clangd/Preamble.cpp @@ -84,8 +84,7 @@ class CppFilePreambleCallbacks : public PreambleCallbacks { void AfterExecute(CompilerInstance &CI) override { if (ParsedCallback) { trace::Span Tracer("Running PreambleCallback"); - ParsedCallback(CI.getASTContext(), CI.getPreprocessorPtr(), - CanonIncludes); + ParsedCallback(CI.getASTContext(), CI.getPreprocessor(), CanonIncludes); } const SourceManager &SM = CI.getSourceManager(); diff --git a/clang-tools-extra/clangd/Preamble.h b/clang-tools-extra/clangd/Preamble.h index bfc8ab8cd66a5..75edee5acad56 100644 --- a/clang-tools-extra/clangd/Preamble.h +++ b/clang-tools-extra/clangd/Preamble.h @@ -72,9 +72,8 @@ struct PreambleData { bool MainIsIncludeGuarded = false; }; -using PreambleParsedCallback = - std::function<void(ASTContext &, std::shared_ptr<clang::Preprocessor>, - const CanonicalIncludes &)>; +using PreambleParsedCallback = std::function<void(ASTContext &, Preprocessor &, + const CanonicalIncludes &)>; /// Build a preamble for the new inputs unless an old one can be reused. /// If \p PreambleCallback is set, it will be run on top of the AST while diff --git a/clang-tools-extra/clangd/TUScheduler.cpp b/clang-tools-extra/clangd/TUScheduler.cpp index e87aa59ec6f5c..33df549d238de 100644 --- a/clang-tools-extra/clangd/TUScheduler.cpp +++ b/clang-tools-extra/clangd/TUScheduler.cpp @@ -977,11 +977,9 @@ void PreambleThread::build(Request Req) { LatestBuild = clang::clangd::buildPreamble( FileName, *Req.CI, Inputs, StoreInMemory, - [this, Version(Inputs.Version)](ASTContext &Ctx, - std::shared_ptr<clang::Preprocessor> PP, + [this, Version(Inputs.Version)](ASTContext &Ctx, Preprocessor &PP, const CanonicalIncludes &CanonIncludes) { - Callbacks.onPreambleAST(FileName, Version, Ctx, std::move(PP), - CanonIncludes); + Callbacks.onPreambleAST(FileName, Version, Ctx, PP, CanonIncludes); }); if (LatestBuild && isReliable(LatestBuild->CompileCommand)) HeaderIncluders.update(FileName, LatestBuild->Includes.allHeaders()); diff --git a/clang-tools-extra/clangd/TUScheduler.h b/clang-tools-extra/clangd/TUScheduler.h index 02b602173ae67..379bddb66c17d 100644 --- a/clang-tools-extra/clangd/TUScheduler.h +++ b/clang-tools-extra/clangd/TUScheduler.h @@ -134,8 +134,7 @@ class ParsingCallbacks { /// contains only AST nodes from the #include directives at the start of the /// file. AST node in the current file should be observed on onMainAST call. virtual void onPreambleAST(PathRef Path, llvm::StringRef Version, - ASTContext &Ctx, - std::shared_ptr<clang::Preprocessor> PP, + ASTContext &Ctx, Preprocessor &PP, const CanonicalIncludes &) {} /// The argument function is run under the critical section guarding against diff --git a/clang-tools-extra/clangd/index/FileIndex.cpp b/clang-tools-extra/clangd/index/FileIndex.cpp index eb9648c2a6ae1..8960602a01901 100644 --- a/clang-tools-extra/clangd/index/FileIndex.cpp +++ b/clang-tools-extra/clangd/index/FileIndex.cpp @@ -45,7 +45,7 @@ namespace clang { namespace clangd { namespace { -SlabTuple indexSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP, +SlabTuple indexSymbols(ASTContext &AST, Preprocessor &PP, llvm::ArrayRef<Decl *> DeclsToIndex, const MainFileMacros *MacroRefsToIndex, const CanonicalIncludes &Includes, bool IsIndexMainAST, @@ -77,7 +77,7 @@ SlabTuple indexSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP, SymbolCollector Collector(std::move(CollectorOpts)); Collector.setPreprocessor(PP); - index::indexTopLevelDecls(AST, *PP, DeclsToIndex, Collector, IndexOpts); + index::indexTopLevelDecls(AST, PP, DeclsToIndex, Collector, IndexOpts); if (MacroRefsToIndex) Collector.handleMacros(*MacroRefsToIndex); @@ -219,18 +219,18 @@ FileShardedIndex::getShard(llvm::StringRef Uri) const { SlabTuple indexMainDecls(ParsedAST &AST) { return indexSymbols( - AST.getASTContext(), AST.getPreprocessorPtr(), - AST.getLocalTopLevelDecls(), &AST.getMacros(), AST.getCanonicalIncludes(), + AST.getASTContext(), AST.getPreprocessor(), AST.getLocalTopLevelDecls(), + &AST.getMacros(), AST.getCanonicalIncludes(), /*IsIndexMainAST=*/true, AST.version(), /*CollectMainFileRefs=*/true); } SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST, - std::shared_ptr<Preprocessor> PP, + Preprocessor &PP, const CanonicalIncludes &Includes) { std::vector<Decl *> DeclsToIndex( AST.getTranslationUnitDecl()->decls().begin(), AST.getTranslationUnitDecl()->decls().end()); - return indexSymbols(AST, std::move(PP), DeclsToIndex, + return indexSymbols(AST, PP, DeclsToIndex, /*MainFileMacros=*/nullptr, Includes, /*IsIndexMainAST=*/false, Version, /*CollectMainFileRefs=*/false); @@ -424,12 +424,11 @@ FileIndex::FileIndex() MainFileIndex(std::make_unique<MemIndex>()) {} void FileIndex::updatePreamble(PathRef Path, llvm::StringRef Version, - ASTContext &AST, - std::shared_ptr<Preprocessor> PP, + ASTContext &AST, Preprocessor &PP, const CanonicalIncludes &Includes) { IndexFileIn IF; std::tie(IF.Symbols, std::ignore, IF.Relations) = - indexHeaderSymbols(Version, AST, std::move(PP), Includes); + indexHeaderSymbols(Version, AST, PP, Includes); FileShardedIndex ShardedIndex(std::move(IF)); for (auto Uri : ShardedIndex.getAllSources()) { auto IF = ShardedIndex.getShard(Uri); diff --git a/clang-tools-extra/clangd/index/FileIndex.h b/clang-tools-extra/clangd/index/FileIndex.h index d46a874645689..99b3e95830eb3 100644 --- a/clang-tools-extra/clangd/index/FileIndex.h +++ b/clang-tools-extra/clangd/index/FileIndex.h @@ -115,8 +115,7 @@ class FileIndex : public MergedIndex { /// Update preamble symbols of file \p Path with all declarations in \p AST /// and macros in \p PP. void updatePreamble(PathRef Path, llvm::StringRef Version, ASTContext &AST, - std::shared_ptr<Preprocessor> PP, - const CanonicalIncludes &Includes); + Preprocessor &PP, const CanonicalIncludes &Includes); /// Update symbols and references from main file \p Path with /// `indexMainDecls`. @@ -163,7 +162,7 @@ SlabTuple indexMainDecls(ParsedAST &AST); /// Index declarations from \p AST and macros from \p PP that are declared in /// included headers. SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST, - std::shared_ptr<Preprocessor> PP, + Preprocessor &PP, const CanonicalIncludes &Includes); /// Takes slabs coming from a TU (multiple files) and shards them per diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp index 5171e79f30721..639003d667f45 100644 --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -186,7 +186,7 @@ class SymbolCollector::HeaderFileURICache { // Weird double-indirect access to PP, which might not be ready yet when // HeaderFiles is created but will be by the time it's used. // (IndexDataConsumer::setPreprocessor can happen before or after initialize) - const std::shared_ptr<Preprocessor> &PP; + Preprocessor *&PP; const SourceManager &SM; const CanonicalIncludes *Includes; llvm::StringRef FallbackDir; @@ -195,8 +195,7 @@ class SymbolCollector::HeaderFileURICache { llvm::DenseMap<FileID, llvm::StringRef> CacheFIDToInclude; public: - HeaderFileURICache(const std::shared_ptr<Preprocessor> &PP, - const SourceManager &SM, + HeaderFileURICache(Preprocessor *&PP, const SourceManager &SM, const SymbolCollector::Options &Opts) : PP(PP), SM(SM), Includes(Opts.Includes), FallbackDir(Opts.FallbackDir) { } @@ -304,7 +303,7 @@ SymbolCollector::~SymbolCollector() = default; void SymbolCollector::initialize(ASTContext &Ctx) { ASTCtx = &Ctx; HeaderFileURIs = std::make_unique<HeaderFileURICache>( - PP, ASTCtx->getSourceManager(), Opts); + this->PP, ASTCtx->getSourceManager(), Opts); CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>(); CompletionTUInfo = std::make_unique<CodeCompletionTUInfo>(CompletionAllocator); @@ -365,7 +364,7 @@ bool SymbolCollector::handleDeclOccurrence( const Decl *D, index::SymbolRoleSet Roles, llvm::ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc, index::IndexDataConsumer::ASTNodeInfo ASTNode) { - assert(ASTCtx && PP.get() && HeaderFileURIs); + assert(ASTCtx && PP && HeaderFileURIs); assert(CompletionAllocator && CompletionTUInfo); assert(ASTNode.OrigD); // Indexing API puts canonical decl into D, which might not have a valid @@ -486,7 +485,7 @@ bool SymbolCollector::handleDeclOccurrence( } void SymbolCollector::handleMacros(const MainFileMacros &MacroRefsToIndex) { - assert(HeaderFileURIs && PP.get()); + assert(HeaderFileURIs && PP); const auto &SM = PP->getSourceManager(); const auto *MainFileEntry = SM.getFileEntryForID(SM.getMainFileID()); assert(MainFileEntry); @@ -533,7 +532,7 @@ bool SymbolCollector::handleMacroOccurrence(const IdentifierInfo *Name, const MacroInfo *MI, index::SymbolRoleSet Roles, SourceLocation Loc) { - assert(PP.get()); + assert(PP); // Builtin macros don't have useful locations and aren't needed in completion. if (MI->isBuiltinMacro()) return true; @@ -805,7 +804,7 @@ const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID, // Add completion info. // FIXME: we may want to choose a diff erent redecl, or combine from several. - assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); + assert(ASTCtx && PP && "ASTContext and Preprocessor must be set."); // We use the primary template, as clang does during code completion. CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0); const auto *CCS = SymbolCompletion.CreateCodeCompletionString( diff --git a/clang-tools-extra/clangd/index/SymbolCollector.h b/clang-tools-extra/clangd/index/SymbolCollector.h index be5fb4b84871c..a451a81ed29cb 100644 --- a/clang-tools-extra/clangd/index/SymbolCollector.h +++ b/clang-tools-extra/clangd/index/SymbolCollector.h @@ -100,8 +100,9 @@ class SymbolCollector : public index::IndexDataConsumer { void initialize(ASTContext &Ctx) override; void setPreprocessor(std::shared_ptr<Preprocessor> PP) override { - this->PP = std::move(PP); + this->PP = PP.get(); } + void setPreprocessor(Preprocessor &PP) { this->PP = &PP; } bool handleDeclOccurrence(const Decl *D, index::SymbolRoleSet Roles, @@ -153,7 +154,7 @@ class SymbolCollector : public index::IndexDataConsumer { // All relations collected from the AST. RelationSlab::Builder Relations; ASTContext *ASTCtx; - std::shared_ptr<Preprocessor> PP; + Preprocessor *PP = nullptr; std::shared_ptr<GlobalCodeCompletionAllocator> CompletionAllocator; std::unique_ptr<CodeCompletionTUInfo> CompletionTUInfo; Options Opts; diff --git a/clang-tools-extra/clangd/tool/Check.cpp b/clang-tools-extra/clangd/tool/Check.cpp index 65638dc88a60f..bcbcd65b01222 100644 --- a/clang-tools-extra/clangd/tool/Check.cpp +++ b/clang-tools-extra/clangd/tool/Check.cpp @@ -159,16 +159,15 @@ class Checker { // Build preamble and AST, and index them. bool buildAST() { log("Building preamble..."); - Preamble = - buildPreamble(File, *Invocation, Inputs, /*StoreInMemory=*/true, - [&](ASTContext &Ctx, std::shared_ptr<Preprocessor> PP, - const CanonicalIncludes &Includes) { - if (!Opts.BuildDynamicSymbolIndex) - return; - log("Indexing headers..."); - Index.updatePreamble(File, /*Version=*/"null", Ctx, - std::move(PP), Includes); - }); + Preamble = buildPreamble(File, *Invocation, Inputs, /*StoreInMemory=*/true, + [&](ASTContext &Ctx, Preprocessor &PP, + const CanonicalIncludes &Includes) { + if (!Opts.BuildDynamicSymbolIndex) + return; + log("Indexing headers..."); + Index.updatePreamble(File, /*Version=*/"null", + Ctx, PP, Includes); + }); if (!Preamble) { elog("Failed to build preamble"); return false; diff --git a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp index a88900c47884b..7ce47e5dbed07 100644 --- a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp @@ -173,7 +173,7 @@ void update(FileIndex &M, llvm::StringRef Basename, llvm::StringRef Code) { File.HeaderCode = std::string(Code); auto AST = File.build(); M.updatePreamble(testPath(File.Filename), /*Version=*/"null", - AST.getASTContext(), AST.getPreprocessorPtr(), + AST.getASTContext(), AST.getPreprocessor(), AST.getCanonicalIncludes()); } @@ -310,13 +310,13 @@ TEST(FileIndexTest, RebuildWithPreamble) { bool IndexUpdated = false; buildPreamble(FooCpp, *CI, PI, /*StoreInMemory=*/true, - [&](ASTContext &Ctx, std::shared_ptr<Preprocessor> PP, + [&](ASTContext &Ctx, Preprocessor &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); + Index.updatePreamble(FooCpp, /*Version=*/"null", Ctx, PP, + CanonIncludes); }); ASSERT_TRUE(IndexUpdated); @@ -416,7 +416,7 @@ TEST(FileIndexTest, Relations) { auto AST = TU.build(); FileIndex Index; Index.updatePreamble(testPath(TU.Filename), /*Version=*/"null", - AST.getASTContext(), AST.getPreprocessorPtr(), + AST.getASTContext(), AST.getPreprocessor(), AST.getCanonicalIncludes()); SymbolID A = findSymbol(TU.headerSymbols(), "A").ID; uint32_t Results = 0; @@ -537,7 +537,7 @@ TEST(FileIndexTest, StalePreambleSymbolsDeleted) { File.HeaderCode = "int a;"; auto AST = File.build(); M.updatePreamble(testPath(File.Filename), /*Version=*/"null", - AST.getASTContext(), AST.getPreprocessorPtr(), + AST.getASTContext(), AST.getPreprocessor(), AST.getCanonicalIncludes()); EXPECT_THAT(runFuzzyFind(M, ""), UnorderedElementsAre(QName("a"))); @@ -545,7 +545,7 @@ TEST(FileIndexTest, StalePreambleSymbolsDeleted) { File.HeaderCode = "int b;"; AST = File.build(); M.updatePreamble(testPath(File.Filename), /*Version=*/"null", - AST.getASTContext(), AST.getPreprocessorPtr(), + AST.getASTContext(), AST.getPreprocessor(), AST.getCanonicalIncludes()); EXPECT_THAT(runFuzzyFind(M, ""), UnorderedElementsAre(QName("b"))); } @@ -690,8 +690,8 @@ TEST(FileIndexTest, Profile) { auto FileName = testPath("foo.cpp"); auto AST = TestTU::withHeaderCode("int a;").build(); FI.updateMain(FileName, AST); - FI.updatePreamble(FileName, "v1", AST.getASTContext(), - AST.getPreprocessorPtr(), AST.getCanonicalIncludes()); + FI.updatePreamble(FileName, "v1", AST.getASTContext(), AST.getPreprocessor(), + AST.getCanonicalIncludes()); llvm::BumpPtrAllocator Alloc; MemoryTree MT(&Alloc); diff --git a/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp b/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp index 833df03a20eae..605f398a3fd56 100644 --- a/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp +++ b/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp @@ -1122,8 +1122,7 @@ TEST_F(TUSchedulerTests, AsyncPreambleThread) { BlockPreambleThread(llvm::StringRef BlockVersion, Notification &N) : BlockVersion(BlockVersion), N(N) {} void onPreambleAST(PathRef Path, llvm::StringRef Version, ASTContext &Ctx, - std::shared_ptr<clang::Preprocessor> PP, - const CanonicalIncludes &) override { + Preprocessor &, const CanonicalIncludes &) override { if (Version == BlockVersion) N.wait(); } diff --git a/clang-tools-extra/clangd/unittests/TestTU.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp index a848a50730caa..989c7737ff596 100644 --- a/clang-tools-extra/clangd/unittests/TestTU.cpp +++ b/clang-tools-extra/clangd/unittests/TestTU.cpp @@ -159,7 +159,7 @@ ParsedAST TestTU::build() const { SymbolSlab TestTU::headerSymbols() const { auto AST = build(); return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(), - AST.getPreprocessorPtr(), + AST.getPreprocessor(), AST.getCanonicalIncludes())); } @@ -172,7 +172,7 @@ std::unique_ptr<SymbolIndex> TestTU::index() const { auto AST = build(); auto Idx = std::make_unique<FileIndex>(); Idx->updatePreamble(testPath(Filename), /*Version=*/"null", - AST.getASTContext(), AST.getPreprocessorPtr(), + AST.getASTContext(), AST.getPreprocessor(), AST.getCanonicalIncludes()); Idx->updateMain(testPath(Filename), AST); return std::move(Idx); diff --git a/clang-tools-extra/clangd/unittests/TestWorkspace.cpp b/clang-tools-extra/clangd/unittests/TestWorkspace.cpp index 84e61323ee5de..59869d187a5aa 100644 --- a/clang-tools-extra/clangd/unittests/TestWorkspace.cpp +++ b/clang-tools-extra/clangd/unittests/TestWorkspace.cpp @@ -19,7 +19,7 @@ std::unique_ptr<SymbolIndex> TestWorkspace::index() { continue; TU.Code = Input.second.Code; TU.Filename = Input.first().str(); - TU.preamble([&](ASTContext &Ctx, std::shared_ptr<clang::Preprocessor> PP, + TU.preamble([&](ASTContext &Ctx, Preprocessor &PP, const CanonicalIncludes &CanonIncludes) { Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP, CanonIncludes); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits