[clangd] Cleanup unneeded use of shared_ptr. NFC

This commit is contained in:
Sam McCall 2021-12-15 02:13:26 +01:00
parent 4299d8d0ce
commit 6917f87b3c
14 changed files with 50 additions and 59 deletions

View File

@ -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 {

View File

@ -84,8 +84,7 @@ public:
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();

View File

@ -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

View File

@ -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());

View File

@ -134,8 +134,7 @@ public:
/// 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

View File

@ -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);

View File

@ -115,8 +115,7 @@ public:
/// 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

View File

@ -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 different 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(

View File

@ -100,8 +100,9 @@ public:
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 @@ private:
// 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;

View File

@ -159,16 +159,15 @@ public:
// 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;

View File

@ -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);

View File

@ -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();
}

View File

@ -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);

View File

@ -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);