Skip to content

Commit ad0c9ed

Browse files
committed
Adding a set of APIs that returns a flag instead of llvm::Erros to DepedencyScanningWorker.
1 parent e329451 commit ad0c9ed

File tree

6 files changed

+104
-59
lines changed

6 files changed

+104
-59
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class DependencyScanningWorker {
125125
/// @param CWD The current working directory used during the scan.
126126
/// @param CommandLine The commandline used for the scan.
127127
/// @return Error if the initializaiton fails.
128-
llvm::Error initializeCompilerInstanceWithContext(
128+
llvm::Error initializeCompilerInstanceWithContextOrError(
129129
StringRef CWD, const std::vector<std::string> &CommandLine);
130130

131131
/// @brief Performaces dependency scanning for the module whose name is
@@ -135,14 +135,27 @@ class DependencyScanningWorker {
135135
/// @param Consumer The dependency consumer that stores the results.
136136
/// @param Controller The controller for the dependency scanning action.
137137
/// @return Error if the scanner incurs errors.
138-
llvm::Error
139-
computeDependenciesByNameWithContext(StringRef ModuleName,
140-
DependencyConsumer &Consumer,
141-
DependencyActionController &Controller);
138+
llvm::Error computeDependenciesByNameWithContextOrError(
139+
StringRef ModuleName, DependencyConsumer &Consumer,
140+
DependencyActionController &Controller);
142141

143142
/// @brief Finalizes the diagnostics engine and deletes the compiler instance.
144143
/// @return Error if errors occur during finalization.
145-
llvm::Error finalizeCompilerInstanceWithContext();
144+
llvm::Error finalizeCompilerInstanceWithContextOrError();
145+
146+
/// The three methods below provides the same functionality as the
147+
/// three methods above. Instead of returning `llvm::Error`s, these
148+
/// three methods return a flag to indicate if the call is successful.
149+
/// The initialization function asks the client for a DiagnosticsConsumer
150+
/// that it direct the diagnostics to.
151+
bool initializeCompilerInstanceWithContext(
152+
StringRef CWD, const std::vector<std::string> &CommandLine,
153+
DiagnosticConsumer *DC = nullptr);
154+
bool
155+
computeDependenciesByNameWithContext(StringRef ModuleName,
156+
DependencyConsumer &Consumer,
157+
DependencyActionController &Controller);
158+
bool finalizeCompilerInstance();
146159

147160
llvm::vfs::FileSystem &getVFS() const { return *BaseFS; }
148161

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ class ModuleDepCollector final : public DependencyCollector {
342342
std::vector<P1689ModuleInfo> RequiredStdCXXModules;
343343

344344
/// A pointer to the preprocessor callback so we can invoke it directly
345-
/// if needed.
345+
/// if needed. The callback is created and added to a Preprocessor instance by
346+
/// attachToPreprocessor and the Preprocessor instance owns it.
346347
ModuleDepCollectorPP *CollectorPPPtr = nullptr;
347348

348349
/// Checks whether the module is known as being prebuilt.

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -705,22 +705,26 @@ bool DependencyScanningAction::runInvocation(
705705
return Result;
706706
}
707707

708-
llvm::Error CompilerInstanceWithContext::initialize() {
709-
std::tie(OverlayFS, CommandLine) = initVFSForByNameScanning(
710-
Worker.BaseFS, CommandLine, CWD, "ScanningByName");
708+
bool CompilerInstanceWithContext::initialize(DiagnosticConsumer *DC) {
709+
if (DC) {
710+
DiagConsumer = DC;
711+
} else {
712+
DiagPrinterWithOS =
713+
std::make_unique<TextDiagnosticsPrinterWithOutput>(CommandLine);
714+
DiagConsumer = &DiagPrinterWithOS->DiagPrinter;
715+
}
711716

712-
DiagPrinterWithOS =
713-
std::make_unique<TextDiagnosticsPrinterWithOutput>(CommandLine);
714717
DiagEngineWithCmdAndOpts = std::make_unique<DignosticsEngineWithDiagOpts>(
715-
CommandLine, OverlayFS, DiagPrinterWithOS->DiagPrinter);
718+
CommandLine, OverlayFS, *DiagConsumer);
719+
720+
std::tie(OverlayFS, CommandLine) = initVFSForByNameScanning(
721+
Worker.BaseFS, CommandLine, CWD, "ScanningByName");
716722

717723
std::tie(Driver, Compilation) = buildCompilation(
718724
CommandLine, *DiagEngineWithCmdAndOpts->DiagEngine, OverlayFS, Alloc);
719725

720-
if (!Compilation) {
721-
return llvm::make_error<llvm::StringError>("Failed to build compilation",
722-
llvm::inconvertibleErrorCode());
723-
}
726+
if (!Compilation)
727+
return false;
724728

725729
assert(Compilation->getJobs().size() &&
726730
"Must have a job list of non-zero size");
@@ -729,10 +733,7 @@ llvm::Error CompilerInstanceWithContext::initialize() {
729733
size_t ArgSize = CommandArgs.size();
730734
assert(ArgSize >= 1 && "Cannot have a command with 0 args");
731735
const char *FirstArg = CommandArgs[0];
732-
if (StringRef(FirstArg) != "-cc1")
733-
return llvm::make_error<llvm::StringError>(
734-
"Incorrect compilation command, missing cc1",
735-
llvm::inconvertibleErrorCode());
736+
assert(StringRef(FirstArg) == "-cc1" && "Requires a cc1 job.");
736737
OriginalInvocation = std::make_unique<CompilerInvocation>();
737738

738739
if (!CompilerInvocation::CreateFromArgs(
@@ -741,9 +742,7 @@ llvm::Error CompilerInstanceWithContext::initialize() {
741742
DiagEngineWithCmdAndOpts->DiagEngine->Report(
742743
diag::err_fe_expected_compiler_job)
743744
<< llvm::join(CommandLine, " ");
744-
return llvm::make_error<llvm::StringError>(
745-
"Cannot create CompilerInvocation from Args",
746-
llvm::inconvertibleErrorCode());
745+
return false;
747746
}
748747

749748
if (any(Worker.Service.getOptimizeArgs() & ScanningOptimizations::Macros))
@@ -759,18 +758,14 @@ llvm::Error CompilerInstanceWithContext::initialize() {
759758

760759
if (!initializeScanCompilerInstance(
761760
CI, OverlayFS, DiagEngineWithCmdAndOpts->DiagEngine->getClient(),
762-
Worker.Service, Worker.DepFS)) {
763-
return llvm::make_error<llvm::StringError>(
764-
"Cannot initialize scanning compiler instance",
765-
llvm::inconvertibleErrorCode());
766-
}
761+
Worker.Service, Worker.DepFS))
762+
return false;
767763

768764
llvm::SmallVector<StringRef> StableDirs = getInitialStableDirs(CI);
769765
auto MaybePrebuiltModulesASTMap =
770766
computePrebuiltModulesASTMap(CI, StableDirs);
771767
if (!MaybePrebuiltModulesASTMap)
772-
return llvm::make_error<llvm::StringError>(
773-
"Prebuilt module scanning failed", llvm::inconvertibleErrorCode());
768+
return false;
774769

775770
PrebuiltModuleASTMap = std::move(*MaybePrebuiltModulesASTMap);
776771
OutputOpts = takeAndUpdateDependencyOutputOptionsFrom(CI);
@@ -782,12 +777,13 @@ llvm::Error CompilerInstanceWithContext::initialize() {
782777
// CompilerInstance::ExecuteAction to perform scanning.
783778
CI.createTarget();
784779

785-
return llvm::Error::success();
780+
return true;
786781
}
787782

788-
llvm::Error CompilerInstanceWithContext::computeDependencies(
783+
bool CompilerInstanceWithContext::computeDependencies(
789784
StringRef ModuleName, DependencyConsumer &Consumer,
790785
DependencyActionController &Controller) {
786+
assert(CIPtr && "CIPtr must be initialized before calling this method");
791787
auto &CI = *CIPtr;
792788

793789
// We create this cleanup object because computeDependencies may exit
@@ -861,18 +857,25 @@ llvm::Error CompilerInstanceWithContext::computeDependencies(
861857
CB->EndOfMainFile();
862858

863859
if (!ModResult)
864-
return llvm::make_error<llvm::StringError>(
865-
DiagPrinterWithOS->DiagnosticsOS.str(), llvm::inconvertibleErrorCode());
860+
return false;
866861

867862
CompilerInvocation ModuleInvocation(*OriginalInvocation);
868863
MDC->applyDiscoveredDependencies(ModuleInvocation);
869864
Consumer.handleBuildCommand(
870865
{CommandLine[0], ModuleInvocation.getCC1CommandLine()});
871866

872-
return llvm::Error::success();
867+
return true;
868+
}
869+
870+
bool CompilerInstanceWithContext::finalize() {
871+
DiagConsumer->finish();
872+
return true;
873873
}
874874

875-
llvm::Error CompilerInstanceWithContext::finalize() {
876-
DiagPrinterWithOS->DiagPrinter.finish();
877-
return llvm::Error::success();
875+
llvm::Error CompilerInstanceWithContext::handleReturnStatus(bool Success) {
876+
assert(DiagPrinterWithOS && "Must use the default DiagnosticConsumer.");
877+
return Success ? llvm::Error::success()
878+
: llvm::make_error<llvm::StringError>(
879+
DiagPrinterWithOS->DiagnosticsOS.str(),
880+
llvm::inconvertibleErrorCode());
878881
}

clang/lib/Tooling/DependencyScanning/DependencyScannerImpl.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ class CompilerInstanceWithContext {
163163

164164
// Context - Diagnostics engine.
165165
std::unique_ptr<TextDiagnosticsPrinterWithOutput> DiagPrinterWithOS;
166+
// DiagConsumer may points to DiagPrinterWithOS->DiagPrinter, or a custom
167+
// DiagnosticConsumer passed in from initialize.
168+
DiagnosticConsumer *DiagConsumer = nullptr;
166169
std::unique_ptr<DignosticsEngineWithDiagOpts> DiagEngineWithCmdAndOpts;
167170

168171
// Context - compiler invocation
@@ -191,11 +194,16 @@ class CompilerInstanceWithContext {
191194
const std::vector<std::string> &CMD)
192195
: Worker(Worker), CWD(CWD), CommandLine(CMD) {};
193196

194-
llvm::Error initialize();
195-
llvm::Error computeDependencies(StringRef ModuleName,
196-
DependencyConsumer &Consumer,
197-
DependencyActionController &Controller);
198-
llvm::Error finalize();
197+
// The three methods below returns false when they fail, with the detail
198+
// accumulated in DiagConsumer.
199+
bool initialize(DiagnosticConsumer *DC);
200+
bool computeDependencies(StringRef ModuleName, DependencyConsumer &Consumer,
201+
DependencyActionController &Controller);
202+
bool finalize();
203+
204+
// The method below turns the return status from the above methods
205+
// into an llvm::Error using a default DiagnosticConsumer.
206+
llvm::Error handleReturnStatus(bool Success);
199207
};
200208
} // namespace dependencies
201209
} // namespace tooling

clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ DependencyScanningTool::getModuleDependencies(
163163
FullDependencyConsumer Consumer(AlreadySeen);
164164
CallbackActionController Controller(LookupModuleOutput);
165165
if (auto Error =
166-
Worker.initializeCompilerInstanceWithContext(CWD, CommandLine))
166+
Worker.initializeCompilerInstanceWithContextOrError(CWD, CommandLine))
167167
return std::move(Error);
168168

169-
auto Result = Worker.computeDependenciesByNameWithContext(
169+
auto Result = Worker.computeDependenciesByNameWithContextOrError(
170170
ModuleName, Consumer, Controller);
171171

172-
if (auto Error = Worker.finalizeCompilerInstanceWithContext())
172+
if (auto Error = Worker.finalizeCompilerInstanceWithContextOrError())
173173
return std::move(Error);
174174

175175
if (Result)
@@ -180,7 +180,7 @@ DependencyScanningTool::getModuleDependencies(
180180

181181
llvm::Error DependencyScanningTool::initializeCompilerInstanceWithContext(
182182
StringRef CWD, const std::vector<std::string> &CommandLine) {
183-
return Worker.initializeCompilerInstanceWithContext(CWD, CommandLine);
183+
return Worker.initializeCompilerInstanceWithContextOrError(CWD, CommandLine);
184184
}
185185

186186
llvm::Expected<TranslationUnitDeps>
@@ -189,7 +189,7 @@ DependencyScanningTool::computeDependenciesByNameWithContext(
189189
LookupModuleOutputCallback LookupModuleOutput) {
190190
FullDependencyConsumer Consumer(AlreadySeen);
191191
CallbackActionController Controller(LookupModuleOutput);
192-
llvm::Error Result = Worker.computeDependenciesByNameWithContext(
192+
llvm::Error Result = Worker.computeDependenciesByNameWithContextOrError(
193193
ModuleName, Consumer, Controller);
194194
if (Result)
195195
return std::move(Result);
@@ -198,7 +198,7 @@ DependencyScanningTool::computeDependenciesByNameWithContext(
198198
}
199199

200200
llvm::Error DependencyScanningTool::finalizeCompilerInstanceWithContext() {
201-
return Worker.finalizeCompilerInstanceWithContext();
201+
return Worker.finalizeCompilerInstanceWithContextOrError();
202202
}
203203

204204
TranslationUnitDeps FullDependencyConsumer::takeTranslationUnitDeps() {

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ DependencyScanningWorker::DependencyScanningWorker(
4444
}
4545

4646
DependencyScanningWorker::~DependencyScanningWorker() = default;
47+
DependencyActionController::~DependencyActionController() = default;
4748

4849
llvm::Error DependencyScanningWorker::computeDependencies(
4950
StringRef WorkingDirectory, const std::vector<std::string> &CommandLine,
@@ -167,24 +168,43 @@ bool DependencyScanningWorker::computeDependencies(
167168
}
168169
}
169170

170-
llvm::Error DependencyScanningWorker::initializeCompilerInstanceWithContext(
171+
llvm::Error
172+
DependencyScanningWorker::initializeCompilerInstanceWithContextOrError(
171173
StringRef CWD, const std::vector<std::string> &CommandLine) {
174+
bool Success = initializeCompilerInstanceWithContext(CWD, CommandLine);
175+
return CIWithContext->handleReturnStatus(Success);
176+
}
177+
178+
llvm::Error
179+
DependencyScanningWorker::computeDependenciesByNameWithContextOrError(
180+
StringRef ModuleName, DependencyConsumer &Consumer,
181+
DependencyActionController &Controller) {
182+
bool Success =
183+
computeDependenciesByNameWithContext(ModuleName, Consumer, Controller);
184+
return CIWithContext->handleReturnStatus(Success);
185+
}
186+
187+
llvm::Error
188+
DependencyScanningWorker::finalizeCompilerInstanceWithContextOrError() {
189+
bool Success = finalizeCompilerInstance();
190+
return CIWithContext->handleReturnStatus(Success);
191+
}
192+
193+
bool DependencyScanningWorker::initializeCompilerInstanceWithContext(
194+
StringRef CWD, const std::vector<std::string> &CommandLine,
195+
DiagnosticConsumer *DC) {
172196
CIWithContext =
173197
std::make_unique<CompilerInstanceWithContext>(*this, CWD, CommandLine);
174-
return CIWithContext->initialize();
198+
return CIWithContext->initialize(DC);
175199
}
176200

177-
llvm::Error DependencyScanningWorker::computeDependenciesByNameWithContext(
201+
bool DependencyScanningWorker::computeDependenciesByNameWithContext(
178202
StringRef ModuleName, DependencyConsumer &Consumer,
179203
DependencyActionController &Controller) {
180204
assert(CIWithContext && "CompilerInstance with context required!");
181205
return CIWithContext->computeDependencies(ModuleName, Consumer, Controller);
182206
}
183207

184-
llvm::Error DependencyScanningWorker::finalizeCompilerInstanceWithContext() {
185-
llvm::Error E = CIWithContext->finalize();
186-
CIWithContext.reset();
187-
return E;
208+
bool DependencyScanningWorker::finalizeCompilerInstance() {
209+
return CIWithContext->finalize();
188210
}
189-
190-
DependencyActionController::~DependencyActionController() {}

0 commit comments

Comments
 (0)