@@ -401,18 +401,13 @@ appendSymbolicInterfaceToIndexStorePath(SmallVectorImpl<char> &resultingPath) {
401
401
llvm::sys::path::append (resultingPath, " interfaces" );
402
402
}
403
403
404
- static bool initSymbolicInterfaceStorePath (StringRef storePath,
405
- std::string &error) {
406
- using namespace llvm ::sys;
404
+ static llvm::Error initSymbolicInterfaceStorePath (StringRef storePath) {
407
405
SmallString<128 > subPath = storePath;
408
406
appendSymbolicInterfaceToIndexStorePath (subPath);
409
- std::error_code ec = fs::create_directories (subPath);
410
- if (ec) {
411
- llvm::raw_string_ostream err (error);
412
- err << " failed to create directory '" << subPath << " ': " << ec.message ();
413
- return true ;
414
- }
415
- return false ;
407
+ std::error_code ec = llvm::sys::fs::create_directories (subPath);
408
+ if (ec)
409
+ return llvm::errorCodeToError (ec);
410
+ return llvm::Error::success ();
416
411
}
417
412
418
413
static void appendSymbolicInterfaceClangModuleFilename (
@@ -423,32 +418,29 @@ static void appendSymbolicInterfaceClangModuleFilename(
423
418
}
424
419
425
420
// FIXME (Alex): Share code with IndexUnitWriter in LLVM after refactoring it.
426
- static Optional<bool >
427
- isFileUpToDateForOutputFile (StringRef filePath,
428
- Optional<StringRef> timeCompareFilePath,
429
- std::string &error) {
421
+ static llvm::Expected<bool >
422
+ isFileUpToDateForOutputFile (StringRef filePath, StringRef timeCompareFilePath) {
423
+ auto makeError = [](StringRef path, std::error_code ec) -> llvm::Error {
424
+ std::string error;
425
+ llvm::raw_string_ostream (error)
426
+ << " could not access path '" << path << " ': " << ec.message ();
427
+ return llvm::createStringError (ec, error.c_str ());
428
+ };
430
429
llvm::sys::fs::file_status unitStat;
431
430
if (std::error_code ec = llvm::sys::fs::status (filePath, unitStat)) {
432
- if (ec != std::errc::no_such_file_or_directory) {
433
- llvm::raw_string_ostream err (error);
434
- err << " could not access path '" << filePath << " ': " << ec.message ();
435
- return {};
436
- }
431
+ if (ec != std::errc::no_such_file_or_directory)
432
+ return makeError (filePath, ec);
437
433
return false ;
438
434
}
439
435
440
- if (! timeCompareFilePath)
436
+ if (timeCompareFilePath. empty () )
441
437
return true ;
442
438
443
439
llvm::sys::fs::file_status compareStat;
444
440
if (std::error_code ec =
445
- llvm::sys::fs::status (*timeCompareFilePath, compareStat)) {
446
- if (ec != std::errc::no_such_file_or_directory) {
447
- llvm::raw_string_ostream err (error);
448
- err << " could not access path '" << *timeCompareFilePath
449
- << " ': " << ec.message ();
450
- return {};
451
- }
441
+ llvm::sys::fs::status (timeCompareFilePath, compareStat)) {
442
+ if (ec != std::errc::no_such_file_or_directory)
443
+ return makeError (timeCompareFilePath, ec);
452
444
return true ;
453
445
}
454
446
@@ -477,9 +469,11 @@ static void emitSymbolicInterfaceForClangModule(
477
469
return ;
478
470
479
471
// Make sure the `interfaces` directory is created.
480
- std::string error;
481
- if (initSymbolicInterfaceStorePath (indexStorePath, error)) {
482
- diags.diagnose (SourceLoc (), diag::error_create_index_dir, error);
472
+ if (auto err = initSymbolicInterfaceStorePath (indexStorePath)) {
473
+ llvm::handleAllErrors (std::move (err), [&](const llvm::ECError &ec) {
474
+ diags.diagnose (SourceLoc (), diag::error_create_symbolic_interfaces_dir,
475
+ ec.convertToErrorCode ().message ());
476
+ });
483
477
return ;
484
478
}
485
479
@@ -494,10 +488,16 @@ static void emitSymbolicInterfaceForClangModule(
494
488
interfaceOutputPath);
495
489
496
490
// Check if the symbolic interface file is already up to date.
497
- auto upToDate = isFileUpToDateForOutputFile (
498
- interfaceOutputPath, StringRef (ModFile->FileName ), error);
491
+ std::string error;
492
+ auto upToDate =
493
+ isFileUpToDateForOutputFile (interfaceOutputPath, ModFile->FileName );
499
494
if (!upToDate) {
500
- diags.diagnose (SourceLoc (), diag::error_index_failed_status_check, error);
495
+ llvm::handleAllErrors (
496
+ upToDate.takeError (), [&](const llvm::StringError &ec) {
497
+ diags.diagnose (SourceLoc (),
498
+ diag::error_symbolic_interfaces_failed_status_check,
499
+ ec.getMessage ());
500
+ });
501
501
return ;
502
502
}
503
503
if (M->getASTContext ().LangOpts .EnableIndexingSystemModuleRemarks ) {
@@ -508,43 +508,44 @@ static void emitSymbolicInterfaceForClangModule(
508
508
return ;
509
509
510
510
// Output the interface to a temporary file first.
511
- SmallString<128 > tempOutputPath;
512
- tempOutputPath = llvm::sys::path::parent_path (interfaceOutputPath);
513
- llvm::sys::path::append (tempOutputPath,
514
- llvm::sys::path::filename (interfaceOutputPath));
511
+ SmallString<128 > tempOutputPath = interfaceOutputPath;
515
512
tempOutputPath += " -%%%%%%%%" ;
516
513
int tempFD;
517
514
if (llvm::sys::fs::createUniqueFile (tempOutputPath.str (), tempFD,
518
515
tempOutputPath)) {
519
516
llvm::raw_string_ostream errOS (error);
520
517
errOS << " failed to create temporary file: " << tempOutputPath;
521
- diags.diagnose (SourceLoc (), diag::error_write_index_record, errOS.str ());
518
+ diags.diagnose (SourceLoc (), diag::error_write_symbolic_interface,
519
+ errOS.str ());
522
520
return ;
523
521
}
524
522
525
523
llvm::raw_fd_ostream os (tempFD, /* shouldClose=*/ true );
526
- std::unique_ptr<ASTPrinter> printer;
527
- printer.reset (new StreamPrinter (os));
528
- ide::printSymbolicSwiftClangModuleInterface (M, *printer, clangModule);
524
+ StreamPrinter printer (os);
525
+ ide::printSymbolicSwiftClangModuleInterface (M, printer, clangModule);
529
526
os.close ();
530
527
531
528
if (os.has_error ()) {
532
529
llvm::raw_string_ostream errOS (error);
533
530
errOS << " failed to write '" << tempOutputPath
534
531
<< " ': " << os.error ().message ();
535
- diags.diagnose (SourceLoc (), diag::error_write_index_record, errOS.str ());
532
+ diags.diagnose (SourceLoc (), diag::error_write_symbolic_interface,
533
+ errOS.str ());
536
534
os.clear_error ();
535
+ llvm::sys::fs::remove (tempOutputPath);
537
536
return ;
538
537
}
539
538
540
539
// Move the resulting output to the destination symbolic interface file.
541
540
std::error_code ec = llvm::sys::fs::rename (
542
- /* from=*/ tempOutputPath. c_str () , /* to=*/ interfaceOutputPath. c_str () );
541
+ /* from=*/ tempOutputPath, /* to=*/ interfaceOutputPath);
543
542
if (ec) {
544
543
llvm::raw_string_ostream errOS (error);
545
544
errOS << " failed to rename '" << tempOutputPath << " ' to '"
546
545
<< interfaceOutputPath << " ': " << ec.message ();
547
- diags.diagnose (SourceLoc (), diag::error_write_index_record, errOS.str ());
546
+ diags.diagnose (SourceLoc (), diag::error_write_symbolic_interface,
547
+ errOS.str ());
548
+ llvm::sys::fs::remove (tempOutputPath);
548
549
return ;
549
550
}
550
551
}
@@ -637,8 +638,10 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
637
638
clang::index::emitIndexDataForModuleFile (clangMod,
638
639
clangCI, unitWriter);
639
640
// Emit the symbolic interface file in addition to index data.
640
- emitSymbolicInterfaceForClangModule (
641
- clangModUnit, mod, clangMod, indexStorePath, clangCI, diags);
641
+ if (indexClangModules)
642
+ emitSymbolicInterfaceForClangModule (clangModUnit, mod, clangMod,
643
+ indexStorePath, clangCI,
644
+ diags);
642
645
}
643
646
} else {
644
647
// Serialized AST file.
@@ -673,8 +676,9 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
673
676
SmallVector<ImportedModule, 4 > imports;
674
677
mod->getImportedModules (imports,
675
678
ModuleDecl::ImportFilterKind::Exported);
676
- emitTransitiveClangSymbolicInterfacesForSwiftModuleImports (
677
- imports, indexStorePath, clangCI, diags);
679
+ if (indexClangModules)
680
+ emitTransitiveClangSymbolicInterfacesForSwiftModuleImports (
681
+ imports, indexStorePath, clangCI, diags);
678
682
}
679
683
}
680
684
clang::index::writer::OpaqueModule opaqMod =
0 commit comments