Skip to content

Commit b5fa133

Browse files
jansvoboda11qiongsiwu
authored andcommitted
[clang] Pass VFS into ASTUnit::LoadFromASTFile() (llvm#159166)
This PR makes the `VFS` parameter to `ASTUnit::LoadFromASTFile()` required and explicit, rather than silently defaulting to the real file system. This makes it easy to correctly propagate the fully-configured VFS and load any input files like the rest of the compiler does. (cherry picked from commit cda542d)
1 parent dad6f36 commit b5fa133

File tree

9 files changed

+27
-22
lines changed

9 files changed

+27
-22
lines changed

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,16 +716,15 @@ class ASTUnit {
716716
/// \returns - The initialized ASTUnit or null if the AST failed to load.
717717
static std::unique_ptr<ASTUnit> LoadFromASTFile(
718718
StringRef Filename, const PCHContainerReader &PCHContainerRdr,
719-
WhatToLoad ToLoad, std::shared_ptr<DiagnosticOptions> DiagOpts,
719+
WhatToLoad ToLoad, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
720+
std::shared_ptr<DiagnosticOptions> DiagOpts,
720721
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
721722
const FileSystemOptions &FileSystemOpts,
722723
const HeaderSearchOptions &HSOpts, const LangOptions *LangOpts = nullptr,
723724
bool OnlyLocalDecls = false,
724725
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
725726
bool AllowASTWithCompilerErrors = false,
726-
bool UserFilesAreVolatile = false,
727-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
728-
llvm::vfs::getRealFileSystem());
727+
bool UserFilesAreVolatile = false);
729728

730729
private:
731730
/// Helper function for \c LoadFromCompilerInvocation() and

clang/lib/CrossTU/CrossTranslationUnit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ CrossTranslationUnitContext::ASTLoader::loadFromDump(StringRef ASTDumpPath) {
567567
DiagnosticIDs::create(), *DiagOpts, DiagClient);
568568
return ASTUnit::LoadFromASTFile(
569569
ASTDumpPath, CI.getPCHContainerOperations()->getRawReader(),
570-
ASTUnit::LoadEverything, DiagOpts, Diags, CI.getFileSystemOpts(),
571-
CI.getHeaderSearchOpts());
570+
ASTUnit::LoadEverything, CI.getVirtualFileSystemPtr(), DiagOpts, Diags,
571+
CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
572572
}
573573

574574
/// Load the AST from a source-file, which is supposed to be located inside the

clang/lib/Frontend/ASTMerge.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ void ASTMergeAction::ExecuteAction() {
4747
/*ShouldOwnClient=*/true);
4848
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
4949
ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything,
50-
nullptr, Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
50+
CI.getVirtualFileSystemPtr(), nullptr, Diags, CI.getFileSystemOpts(),
51+
CI.getHeaderSearchOpts());
5152

5253
if (!Unit)
5354
continue;

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,12 +799,13 @@ void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
799799

800800
std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
801801
StringRef Filename, const PCHContainerReader &PCHContainerRdr,
802-
WhatToLoad ToLoad, std::shared_ptr<DiagnosticOptions> DiagOpts,
802+
WhatToLoad ToLoad, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
803+
std::shared_ptr<DiagnosticOptions> DiagOpts,
803804
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
804805
const FileSystemOptions &FileSystemOpts, const HeaderSearchOptions &HSOpts,
805806
const LangOptions *LangOpts, bool OnlyLocalDecls,
806807
CaptureDiagsKind CaptureDiagnostics, bool AllowASTWithCompilerErrors,
807-
bool UserFilesAreVolatile, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
808+
bool UserFilesAreVolatile) {
808809
std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
809810

810811
// Recover resources if we crash before exiting this method.

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,8 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
904904

905905
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
906906
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadPreprocessorOnly,
907-
nullptr, ASTDiags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
907+
CI.getVirtualFileSystemPtr(), nullptr, ASTDiags, CI.getFileSystemOpts(),
908+
CI.getHeaderSearchOpts());
908909
if (!AST)
909910
return false;
910911

@@ -971,9 +972,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
971972
StringRef InputFile = Input.getFile();
972973

973974
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
974-
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, nullptr,
975-
Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts(),
976-
&CI.getLangOpts());
975+
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything,
976+
CI.getVirtualFileSystemPtr(), nullptr, Diags, CI.getFileSystemOpts(),
977+
CI.getHeaderSearchOpts(), &CI.getLangOpts());
977978

978979
if (!AST)
979980
return false;

clang/tools/c-index-test/core_main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,13 @@ static bool printSourceSymbolsFromModule(StringRef modulePath,
369369

370370
HeaderSearchOptions HSOpts;
371371

372+
auto VFS = llvm::vfs::getRealFileSystem();
373+
372374
auto DiagOpts = std::make_shared<DiagnosticOptions>();
373375
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
374-
CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
375-
*DiagOpts);
376+
CompilerInstance::createDiagnostics(*VFS, *DiagOpts);
376377
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
377-
modulePath, *pchRdr, ASTUnit::LoadASTOnly, DiagOpts, Diags,
378+
modulePath, *pchRdr, ASTUnit::LoadASTOnly, VFS, DiagOpts, Diags,
378379
FileSystemOpts, HSOpts, /*LangOpts=*/nullptr,
379380
/*OnlyLocalDecls=*/true, CaptureDiagsKind::None,
380381
/*AllowASTWithCompilerErrors=*/true,

clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ static bool HandleAST(StringRef AstPath) {
157157

158158
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
159159
AstPath, CI->getPCHContainerOperations()->getRawReader(),
160-
ASTUnit::LoadASTOnly, DiagOpts, DiagEngine, CI->getFileSystemOpts(),
161-
CI->getHeaderSearchOpts());
160+
ASTUnit::LoadASTOnly, CI->getVirtualFileSystemPtr(), DiagOpts, DiagEngine,
161+
CI->getFileSystemOpts(), CI->getHeaderSearchOpts());
162162

163163
if (!Unit)
164164
return false;

clang/tools/libclang/CIndex.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4243,13 +4243,14 @@ enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
42434243
FileSystemOptions FileSystemOpts;
42444244
HeaderSearchOptions HSOpts;
42454245

4246+
auto VFS = llvm::vfs::getRealFileSystem();
4247+
42464248
auto DiagOpts = std::make_shared<DiagnosticOptions>();
42474249
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
4248-
CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
4249-
*DiagOpts);
4250+
CompilerInstance::createDiagnostics(*VFS, *DiagOpts);
42504251
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
42514252
ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(),
4252-
ASTUnit::LoadEverything, DiagOpts, Diags, FileSystemOpts, HSOpts,
4253+
ASTUnit::LoadEverything, VFS, DiagOpts, Diags, FileSystemOpts, HSOpts,
42534254
/*LangOpts=*/nullptr, CXXIdx->getOnlyLocalDecls(), CaptureDiagsKind::All,
42544255
/*AllowASTWithCompilerErrors=*/true,
42554256
/*UserFilesAreVolatile=*/true);

clang/unittests/Frontend/ASTUnitTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ TEST_F(ASTUnitTest, SaveLoadPreservesLangOptionsInPrintingPolicy) {
9898

9999
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
100100
ASTFileName, PCHContainerOps->getRawReader(), ASTUnit::LoadEverything,
101-
DiagOpts, Diags, FileSystemOptions(), HSOpts);
101+
llvm::vfs::getRealFileSystem(), DiagOpts, Diags, FileSystemOptions(),
102+
HSOpts);
102103

103104
if (!AU)
104105
FAIL() << "failed to load ASTUnit";

0 commit comments

Comments
 (0)