@@ -355,13 +355,14 @@ makeDepscanDaemonPath(StringRef Mode, const DepscanSharing &Sharing) {
355
355
return std::nullopt;
356
356
}
357
357
358
- static Expected<llvm::cas::CASID> scanAndUpdateCC1Inline (
359
- const char *Exec, ArrayRef<const char *> InputArgs,
360
- StringRef WorkingDirectory, SmallVectorImpl<const char *> &OutputArgs,
361
- bool ProduceIncludeTree, bool &DiagnosticErrorOccurred,
362
- llvm::function_ref<const char *(const Twine &)> SaveArg,
363
- const CASOptions &CASOpts, std::shared_ptr<llvm::cas::ObjectStore> DB,
364
- std::shared_ptr<llvm::cas::ActionCache> Cache);
358
+ static int
359
+ scanAndUpdateCC1Inline (const char *Exec, ArrayRef<const char *> InputArgs,
360
+ StringRef WorkingDirectory,
361
+ SmallVectorImpl<const char *> &OutputArgs,
362
+ bool ProduceIncludeTree,
363
+ llvm::function_ref<const char *(const Twine &)> SaveArg,
364
+ const CASOptions &CASOpts, DiagnosticsEngine &Diag,
365
+ std::optional<llvm::cas::CASID> &RootID);
365
366
366
367
static Expected<llvm::cas::CASID> scanAndUpdateCC1InlineWithTool (
367
368
tooling::dependencies::DependencyScanningTool &Tool,
@@ -370,14 +371,17 @@ static Expected<llvm::cas::CASID> scanAndUpdateCC1InlineWithTool(
370
371
SmallVectorImpl<const char *> &OutputArgs, llvm::cas::ObjectStore &DB,
371
372
llvm::function_ref<const char *(const Twine &)> SaveArg);
372
373
373
- static llvm::Expected<llvm::cas::CASID> scanAndUpdateCC1UsingDaemon (
374
+ static int scanAndUpdateCC1UsingDaemon (
374
375
const char *Exec, ArrayRef<const char *> OldArgs,
375
376
StringRef WorkingDirectory, SmallVectorImpl<const char *> &NewArgs,
376
- std::string &DiagnosticOutput, StringRef Path,
377
- const DepscanSharing &Sharing,
377
+ StringRef Path, const DepscanSharing &Sharing, DiagnosticsEngine &Diag,
378
378
llvm::function_ref<const char *(const Twine &)> SaveArg,
379
- llvm::cas::ObjectStore &CAS ) {
379
+ const CASOptions &CASOpts, std::optional< llvm::cas::CASID> &Root ) {
380
380
using namespace clang ::cc1depscand;
381
+ auto reportScanFailure = [&](Error E) {
382
+ Diag.Report (diag::err_cas_depscan_failed) << std::move (E);
383
+ return 1 ;
384
+ };
381
385
382
386
// FIXME: Skip some of this if -fcas-fs has been passed.
383
387
@@ -387,12 +391,12 @@ static llvm::Expected<llvm::cas::CASID> scanAndUpdateCC1UsingDaemon(
387
391
? ScanDaemon::connectToDaemonAndShakeHands (Path)
388
392
: ScanDaemon::constructAndShakeHands (Path, Exec, Sharing);
389
393
if (!Daemon)
390
- return Daemon.takeError ();
394
+ return reportScanFailure ( Daemon.takeError () );
391
395
CC1DepScanDProtocol Comms (*Daemon);
392
396
393
397
// llvm::dbgs() << "sending request...\n";
394
398
if (auto E = Comms.putCommand (WorkingDirectory, OldArgs))
395
- return std::move (E);
399
+ return reportScanFailure ( std::move (E) );
396
400
397
401
llvm::BumpPtrAllocator Alloc;
398
402
llvm::StringSaver Saver (Alloc);
@@ -401,23 +405,32 @@ static llvm::Expected<llvm::cas::CASID> scanAndUpdateCC1UsingDaemon(
401
405
StringRef FailedReason;
402
406
StringRef RootID;
403
407
StringRef DiagOut;
404
- if ( auto E = Comms.getScanResult (Saver, Result, FailedReason, RootID,
405
- RawNewArgs, DiagOut)) {
406
- DiagnosticOutput = DiagOut;
407
- return std::move (E) ;
408
- }
409
- DiagnosticOutput = DiagOut ;
408
+ auto E = Comms.getScanResult (Saver, Result, FailedReason, RootID, RawNewArgs ,
409
+ DiagOut);
410
+ // Send the diagnostics to std::err.
411
+ llvm::errs () << DiagOut ;
412
+ if (E)
413
+ return reportScanFailure ( std::move (E)) ;
410
414
411
415
if (Result != CC1DepScanDProtocol::SuccessResult)
412
- return llvm::createStringError ( llvm::inconvertibleErrorCode (),
413
- " depscan daemon failed: " + FailedReason);
416
+ return reportScanFailure (
417
+ llvm::createStringError ( " depscan daemon failed: " + FailedReason) );
414
418
415
419
// FIXME: Avoid this duplication.
416
420
NewArgs.resize (RawNewArgs.size ());
417
421
for (int I = 0 , E = RawNewArgs.size (); I != E; ++I)
418
422
NewArgs[I] = SaveArg (RawNewArgs[I]);
419
423
420
- return CAS.parseID (RootID);
424
+ // Create CAS after daemon returns the result so daemon can perform corrupted
425
+ // CAS recovery.
426
+ auto [CAS, _] = CASOpts.getOrCreateDatabases (Diag);
427
+ if (!CAS)
428
+ return 1 ;
429
+
430
+ if (auto E = CAS->parseID (RootID).moveInto (Root))
431
+ return reportScanFailure (std::move (E));
432
+
433
+ return 0 ;
421
434
}
422
435
423
436
// FIXME: This is a copy of Command::writeResponseFile. Command is too deeply
@@ -444,8 +457,6 @@ static int scanAndUpdateCC1(const char *Exec, ArrayRef<const char *> OldArgs,
444
457
DiagnosticsEngine &Diag,
445
458
const llvm::opt::ArgList &Args,
446
459
const CASOptions &CASOpts,
447
- std::shared_ptr<llvm::cas::ObjectStore> DB,
448
- std::shared_ptr<llvm::cas::ActionCache> Cache,
449
460
std::optional<llvm::cas::CASID> &RootID) {
450
461
using namespace clang ::driver;
451
462
@@ -511,25 +522,14 @@ static int scanAndUpdateCC1(const char *Exec, ArrayRef<const char *> OldArgs,
511
522
if (ProduceIncludeTree)
512
523
Sharing.CASArgs .push_back (" -fdepscan-include-tree" );
513
524
514
- std::string DiagnosticOutput;
515
- bool DiagnosticErrorOccurred = false ;
516
- auto ScanAndUpdate = [&]() {
517
- if (std::optional<std::string> DaemonPath =
518
- makeDepscanDaemonPath (Mode, Sharing))
519
- return scanAndUpdateCC1UsingDaemon (Exec, OldArgs, WorkingDirectory,
520
- NewArgs, DiagnosticOutput, *DaemonPath,
521
- Sharing, SaveArg, *DB);
522
- return scanAndUpdateCC1Inline (Exec, OldArgs, WorkingDirectory, NewArgs,
523
- ProduceIncludeTree, DiagnosticErrorOccurred,
524
- SaveArg, CASOpts, DB, Cache);
525
- };
526
- if (llvm::Error E = ScanAndUpdate ().moveInto (RootID)) {
527
- Diag.Report (diag::err_cas_depscan_failed) << std::move (E);
528
- if (!DiagnosticOutput.empty ())
529
- llvm::errs () << DiagnosticOutput;
530
- return 1 ;
531
- }
532
- return DiagnosticErrorOccurred;
525
+ if (auto DaemonPath = makeDepscanDaemonPath (Mode, Sharing))
526
+ return scanAndUpdateCC1UsingDaemon (Exec, OldArgs, WorkingDirectory, NewArgs,
527
+ *DaemonPath, Sharing, Diag, SaveArg,
528
+ CASOpts, RootID);
529
+
530
+ return scanAndUpdateCC1Inline (Exec, OldArgs, WorkingDirectory, NewArgs,
531
+ ProduceIncludeTree, SaveArg, CASOpts, Diag,
532
+ RootID);
533
533
}
534
534
535
535
int cc1depscan_main (ArrayRef<const char *> Argv, const char *Argv0,
@@ -590,12 +590,8 @@ int cc1depscan_main(ArrayRef<const char *> Argv, const char *Argv0,
590
590
CompilerInvocation::ParseCASArgs (CASOpts, ParsedCC1Args, Diags);
591
591
CASOpts.ensurePersistentCAS ();
592
592
593
- auto [CAS, Cache] = CASOpts.getOrCreateDatabases (Diags);
594
- if (!CAS || !Cache)
595
- return 1 ;
596
-
597
593
if (int Ret = scanAndUpdateCC1 (Argv0, CC1Args->getValues (), NewArgs, Diags,
598
- Args, CASOpts, CAS, Cache, RootID))
594
+ Args, CASOpts, RootID))
599
595
return Ret;
600
596
601
597
// FIXME: Use OutputBackend to OnDisk only now.
@@ -841,7 +837,8 @@ void ScanServer::start(bool Exclusive, ArrayRef<const char *> CASArgs) {
841
837
ExitOnErr (llvm::cas::validateOnDiskUnifiedCASDatabasesIfNeeded (
842
838
CASPath, /* CheckHash=*/ true ,
843
839
/* AllowRecovery=*/ true ,
844
- /* Force=*/ false , findLLVMCasBinary (Argv0, LLVMCasStorage)));
840
+ /* Force=*/ getenv (" LLVM_CAS_FORCE_VALIDATION" ),
841
+ findLLVMCasBinary (Argv0, LLVMCasStorage)));
845
842
});
846
843
847
844
// Check the pidfile.
@@ -1108,13 +1105,18 @@ static Expected<llvm::cas::CASID> scanAndUpdateCC1InlineWithTool(
1108
1105
return *Root;
1109
1106
}
1110
1107
1111
- static Expected<llvm::cas::CASID> scanAndUpdateCC1Inline (
1112
- const char *Exec, ArrayRef<const char *> InputArgs,
1113
- StringRef WorkingDirectory, SmallVectorImpl<const char *> &OutputArgs,
1114
- bool ProduceIncludeTree, bool &DiagnosticErrorOccurred,
1115
- llvm::function_ref<const char *(const Twine &)> SaveArg,
1116
- const CASOptions &CASOpts, std::shared_ptr<llvm::cas::ObjectStore> DB,
1117
- std::shared_ptr<llvm::cas::ActionCache> Cache) {
1108
+ static int
1109
+ scanAndUpdateCC1Inline (const char *Exec, ArrayRef<const char *> InputArgs,
1110
+ StringRef WorkingDirectory,
1111
+ SmallVectorImpl<const char *> &OutputArgs,
1112
+ bool ProduceIncludeTree,
1113
+ llvm::function_ref<const char *(const Twine &)> SaveArg,
1114
+ const CASOptions &CASOpts, DiagnosticsEngine &Diag,
1115
+ std::optional<llvm::cas::CASID> &RootID) {
1116
+ auto [DB, Cache] = CASOpts.getOrCreateDatabases (Diag);
1117
+ if (!DB || !Cache)
1118
+ return 1 ;
1119
+
1118
1120
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> FS;
1119
1121
if (!ProduceIncludeTree)
1120
1122
FS = llvm::cantFail (llvm::cas::createCachingOnDiskFileSystem (*DB));
@@ -1138,10 +1140,15 @@ static Expected<llvm::cas::CASID> scanAndUpdateCC1Inline(
1138
1140
auto DiagsConsumer =
1139
1141
std::make_unique<TextDiagnosticPrinter>(llvm::errs (), *DiagOpts, false );
1140
1142
1141
- auto Result = scanAndUpdateCC1InlineWithTool (
1142
- Tool, *DiagsConsumer, /* VerboseOS*/ nullptr , Exec, InputArgs,
1143
- WorkingDirectory, OutputArgs, *DB, SaveArg);
1144
- DiagnosticErrorOccurred = DiagsConsumer->getNumErrors () != 0 ;
1145
- return Result;
1143
+ auto E = scanAndUpdateCC1InlineWithTool (
1144
+ Tool, *DiagsConsumer, /* VerboseOS*/ nullptr , Exec, InputArgs,
1145
+ WorkingDirectory, OutputArgs, *DB, SaveArg)
1146
+ .moveInto (RootID);
1147
+ if (E) {
1148
+ Diag.Report (diag::err_cas_depscan_failed) << std::move (E);
1149
+ return 1 ;
1150
+ }
1151
+
1152
+ return DiagsConsumer->getNumErrors () != 0 ;
1146
1153
}
1147
1154
#endif /* LLVM_ON_UNIX */
0 commit comments