Skip to content

Commit 0848e20

Browse files
committed
Refactor CompilerInstanceWithContext::computeDependencies
1 parent 094754c commit 0848e20

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScannerImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class CompilerInstanceWithContext {
180180

181181
// Context - stable directory handling
182182
llvm::SmallVector<StringRef> StableDirs;
183-
PrebuiltModulesAttrsMap PrebuiltModuleVFSMap;
183+
PrebuiltModulesAttrsMap PrebuiltModuleASTMap;
184184

185185
// Compiler Instance
186186
std::unique_ptr<CompilerInstance> CIPtr;

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ llvm::Error CompilerInstanceWithContext::initialize() {
799799
return llvm::make_error<llvm::StringError>(
800800
"Prebuilt module scanning failed", llvm::inconvertibleErrorCode());
801801

802+
PrebuiltModuleASTMap = std::move(*MaybePrebuiltModulesASTMap);
802803
OutputOpts = takeDependencyOutputOptionsFrom(CI);
803804

804805
CI.createTarget();
@@ -813,32 +814,35 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
813814
auto &CI = *CIPtr;
814815
CompilerInvocation Inv(*Invocation);
815816

816-
auto Opts = std::make_unique<DependencyOutputOptions>(*OutputOpts);
817-
auto MDC = std::make_shared<ModuleDepCollector>(
818-
Worker.Service, std::move(Opts), CI, Consumer, Controller, Inv,
819-
PrebuiltModuleVFSMap, StableDirs);
820-
821817
CI.clearDependencyCollectors();
822-
CI.addDependencyCollector(MDC);
823-
824-
std::unique_ptr<FrontendAction> Action =
825-
std::make_unique<GetDependenciesByModuleNameAction>(ModuleName);
826-
auto InputFile = CI.getFrontendOpts().Inputs.begin();
818+
auto MDC = initializeScanInstanceDependencyCollector(
819+
CI, std::make_unique<DependencyOutputOptions>(*OutputOpts), CWD, Consumer,
820+
Worker.Service, *Invocation, Controller, PrebuiltModuleASTMap,
821+
StableDirs);
827822

828823
if (!SrcLocOffset) {
824+
// When SrcLocOffset is zero, we are at the beginning of the fake source
825+
// file. In this case, we call BeginSourceFile to initialize.
826+
std::unique_ptr<FrontendAction> Action =
827+
std::make_unique<GetDependenciesByModuleNameAction>(ModuleName);
828+
auto InputFile = CI.getFrontendOpts().Inputs.begin();
829+
829830
Action->BeginSourceFile(CI, *InputFile);
830-
} else {
831-
CI.getPreprocessor().removePPCallbacks();
832831
}
833832

834833
Preprocessor &PP = CI.getPreprocessor();
835834
SourceManager &SM = PP.getSourceManager();
836835
FileID MainFileID = SM.getMainFileID();
837836
SourceLocation FileStart = SM.getLocForStartOfFile(MainFileID);
838837
SourceLocation IDLocation = FileStart.getLocWithOffset(SrcLocOffset);
839-
if (!SrcLocOffset)
838+
if (!SrcLocOffset) {
839+
// We need to call EnterSourceFile when SrcLocOffset is zero to initialize
840+
// the preprocessor.
840841
PP.EnterSourceFile(MainFileID, nullptr, SourceLocation());
841-
else {
842+
} else {
843+
// When SrcLocOffset is non-zero, the preprocessor has already been
844+
// initialized through a previous call of computeDependencies. We want to
845+
// preserve the PP's state, hence we do not call EnterSourceFile again.
842846
auto DCs = CI.getDependencyCollectors();
843847
for (auto &DC : DCs) {
844848
DC->attachToPreprocessor(PP);
@@ -878,6 +882,8 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
878882
// if (!ID.empty())
879883
// Consumer.handleIncludeTreeID(std::move(ID));
880884

885+
// Remove the PPCallbacks since they are going out of scope.
886+
CI.getPreprocessor().removePPCallbacks();
881887
return llvm::Error::success();
882888
}
883889

0 commit comments

Comments
 (0)