Skip to content

Commit 970eb2f

Browse files
committed
Update Swift for the FileManager API change
Change Swift to work with the new FileManager API. It now returns and ErrorOr<FileEntry*> instead of just the raw pointer. (cherry picked from commit 257dbcd)
1 parent 4722e1e commit 970eb2f

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ ClangImporter::create(ASTContext &ctx, const ClangImporterOptions &importerOpts,
11721172
bool ClangImporter::addSearchPath(StringRef newSearchPath, bool isFramework,
11731173
bool isSystem) {
11741174
clang::FileManager &fileMgr = Impl.Instance->getFileManager();
1175-
const clang::DirectoryEntry *entry = fileMgr.getDirectory(newSearchPath);
1175+
auto entry = fileMgr.getDirectory(newSearchPath);
11761176
if (!entry)
11771177
return true;
11781178

@@ -1181,8 +1181,8 @@ bool ClangImporter::addSearchPath(StringRef newSearchPath, bool isFramework,
11811181
headerSearchInfo.search_dir_end(),
11821182
[&](const clang::DirectoryLookup &lookup) -> bool {
11831183
if (isFramework)
1184-
return lookup.getFrameworkDir() == entry;
1185-
return lookup.getDir() == entry;
1184+
return lookup.getFrameworkDir() == *entry;
1185+
return lookup.getDir() == *entry;
11861186
});
11871187
if (exists) {
11881188
// Don't bother adding a search path that's already there. Clang would have
@@ -1191,7 +1191,7 @@ bool ClangImporter::addSearchPath(StringRef newSearchPath, bool isFramework,
11911191
}
11921192

11931193
auto kind = isSystem ? clang::SrcMgr::C_System : clang::SrcMgr::C_User;
1194-
headerSearchInfo.AddSearchPath({entry, kind, isFramework},
1194+
headerSearchInfo.AddSearchPath({*entry, kind, isFramework},
11951195
/*isAngled=*/true);
11961196

11971197
// In addition to changing the current preprocessor directly, we still need
@@ -1353,10 +1353,9 @@ bool ClangImporter::importHeader(StringRef header, ModuleDecl *adapter,
13531353
off_t expectedSize, time_t expectedModTime,
13541354
StringRef cachedContents, SourceLoc diagLoc) {
13551355
clang::FileManager &fileManager = Impl.Instance->getFileManager();
1356-
const clang::FileEntry *headerFile = fileManager.getFile(header,
1357-
/*OpenFile=*/true);
1358-
if (headerFile && headerFile->getSize() == expectedSize &&
1359-
headerFile->getModificationTime() == expectedModTime) {
1356+
auto headerFile = fileManager.getFile(header, /*OpenFile=*/true);
1357+
if (headerFile && (*headerFile)->getSize() == expectedSize &&
1358+
(*headerFile)->getModificationTime() == expectedModTime) {
13601359
return importBridgingHeader(header, adapter, diagLoc, false, true);
13611360
}
13621361

@@ -1389,8 +1388,7 @@ bool ClangImporter::importBridgingHeader(StringRef header, ModuleDecl *adapter,
13891388
}
13901389

13911390
clang::FileManager &fileManager = Impl.Instance->getFileManager();
1392-
const clang::FileEntry *headerFile = fileManager.getFile(header,
1393-
/*OpenFile=*/true);
1391+
auto headerFile = fileManager.getFile(header, /*OpenFile=*/true);
13941392
if (!headerFile) {
13951393
Impl.SwiftContext.Diags.diagnose(diagLoc, diag::bridging_header_missing,
13961394
header);
@@ -1466,9 +1464,10 @@ std::string ClangImporter::getBridgingHeaderContents(StringRef headerPath,
14661464
return "";
14671465
}
14681466

1469-
const clang::FileEntry *fileInfo = fileManager.getFile(headerPath);
1470-
fileSize = fileInfo->getSize();
1471-
fileModTime = fileInfo->getModificationTime();
1467+
if (auto fileInfo = fileManager.getFile(headerPath)) {
1468+
fileSize = (*fileInfo)->getSize();
1469+
fileModTime = (*fileInfo)->getModificationTime();
1470+
}
14721471
return result;
14731472
}
14741473

@@ -2411,8 +2410,7 @@ void ClangImporter::lookupBridgingHeaderDecls(
24112410
bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
24122411
llvm::function_ref<bool(ClangNode)> filter,
24132412
llvm::function_ref<void(Decl*)> receiver) const {
2414-
const clang::FileEntry *File =
2415-
getClangPreprocessor().getFileManager().getFile(Filename);
2413+
auto File = getClangPreprocessor().getFileManager().getFile(Filename);
24162414
if (!File)
24172415
return true;
24182416

@@ -2421,7 +2419,7 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
24212419
auto &ClangPP = getClangPreprocessor();
24222420

24232421
// Look up the header in the includes of the bridging header.
2424-
if (Impl.BridgeHeaderFiles.count(File)) {
2422+
if (Impl.BridgeHeaderFiles.count(*File)) {
24252423
auto headerFilter = [&](ClangNode ClangN) -> bool {
24262424
if (ClangN.isNull())
24272425
return false;
@@ -2430,7 +2428,7 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
24302428
if (ClangLoc.isInvalid())
24312429
return false;
24322430

2433-
if (ClangSM.getFileEntryForID(ClangSM.getFileID(ClangLoc)) != File)
2431+
if (ClangSM.getFileEntryForID(ClangSM.getFileID(ClangLoc)) != *File)
24342432
return false;
24352433

24362434
return filter(ClangN);
@@ -2440,7 +2438,7 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
24402438
return false;
24412439
}
24422440

2443-
clang::FileID FID = ClangSM.translateFile(File);
2441+
clang::FileID FID = ClangSM.translateFile(*File);
24442442
if (FID.isInvalid())
24452443
return false;
24462444

lib/Index/IndexRecord.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ static void addModuleDependencies(ArrayRef<ModuleDecl::ImportedModule> imports,
405405
case FileUnitKind::DWARFModule:
406406
case FileUnitKind::ClangModule: {
407407
auto *LFU = cast<LoadedFile>(FU);
408-
if (auto *F = fileMgr.getFile(LFU->getFilename())) {
408+
if (auto F = fileMgr.getFile(LFU->getFilename())) {
409409
std::string moduleName = mod->getNameStr();
410410
bool withoutUnitName = true;
411411
if (FU->getKind() == FileUnitKind::ClangModule) {
@@ -434,7 +434,7 @@ static void addModuleDependencies(ArrayRef<ModuleDecl::ImportedModule> imports,
434434
}
435435
clang::index::writer::OpaqueModule opaqMod =
436436
moduleNameScratch.createString(moduleName);
437-
unitWriter.addASTFileDependency(F, mod->isSystemModule(), opaqMod,
437+
unitWriter.addASTFileDependency(*F, mod->isSystemModule(), opaqMod,
438438
withoutUnitName);
439439
}
440440
break;
@@ -547,15 +547,15 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
547547
/*MainFile=*/nullptr, isSystem, /*IsModuleUnit=*/true,
548548
isDebugCompilation, targetTriple, sysrootPath, getModuleInfoFromOpaqueModule);
549549

550-
const clang::FileEntry *FE = fileMgr.getFile(filename);
550+
auto FE = fileMgr.getFile(filename);
551551
bool isSystemModule = module->isSystemModule();
552552
for (auto &pair : records) {
553553
std::string &recordFile = pair.first;
554554
std::string &groupName = pair.second;
555555
if (recordFile.empty())
556556
continue;
557557
clang::index::writer::OpaqueModule mod = &groupName;
558-
unitWriter.addRecordFile(recordFile, FE, isSystemModule, mod);
558+
unitWriter.addRecordFile(recordFile, *FE, isSystemModule, mod);
559559
}
560560

561561
ModuleDecl::ImportFilter importFilter;
@@ -585,15 +585,16 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
585585
auto &fileMgr = clangCI.getFileManager();
586586
auto *module = primarySourceFile->getParentModule();
587587
bool isSystem = module->isSystemModule();
588-
auto *mainFile = fileMgr.getFile(primarySourceFile->getFilename());
588+
auto mainFile = fileMgr.getFile(primarySourceFile->getFilename());
589589
// FIXME: Get real values for the following.
590590
StringRef swiftVersion;
591591
StringRef sysrootPath = clangCI.getHeaderSearchOpts().Sysroot;
592592

593-
IndexUnitWriter unitWriter(fileMgr, indexStorePath,
594-
"swift", swiftVersion, indexUnitToken, module->getNameStr(),
595-
mainFile, isSystem, /*isModuleUnit=*/false, isDebugCompilation,
596-
targetTriple, sysrootPath, getModuleInfoFromOpaqueModule);
593+
IndexUnitWriter unitWriter(
594+
fileMgr, indexStorePath, "swift", swiftVersion, indexUnitToken,
595+
module->getNameStr(), mainFile ? *mainFile : nullptr, isSystem,
596+
/*isModuleUnit=*/false, isDebugCompilation, targetTriple, sysrootPath,
597+
getModuleInfoFromOpaqueModule);
597598

598599
// Module dependencies.
599600
ModuleDecl::ImportFilter importFilter;
@@ -612,9 +613,11 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
612613

613614
recordSourceFile(primarySourceFile, indexStorePath, diags,
614615
[&](StringRef recordFile, StringRef filename) {
615-
unitWriter.addRecordFile(recordFile, fileMgr.getFile(filename),
616-
module->isSystemModule(), /*Module=*/nullptr);
617-
});
616+
auto file = fileMgr.getFile(filename);
617+
unitWriter.addRecordFile(
618+
recordFile, file ? *file : nullptr,
619+
module->isSystemModule(), /*Module=*/nullptr);
620+
});
618621

619622
std::string error;
620623
if (unitWriter.write(error)) {

0 commit comments

Comments
 (0)