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