Skip to content

Commit fce17c2

Browse files
committed
[clang] Don't silently inherit the VFS from FileManager (llvm#164323)
Since llvm#158381 the `CompilerInstance` is aware of the VFS and co-owns it. To reduce scope of that PR, the VFS was being inherited from the `FileManager` during `setFileManager()` if it wasn't configured before. However, the implementation of that setter was buggy. This PR fixes the bug, and moves us closer to the long-term goal of `CompilerInstance` requiring the VFS to be configured explicitly and owned by the instance.
1 parent 0a45a4f commit fce17c2

File tree

14 files changed

+24
-8
lines changed

14 files changed

+24
-8
lines changed

clang-tools-extra/clang-include-fixer/IncludeFixer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ bool IncludeFixerActionFactory::runInvocation(
9090

9191
// Set up Clang.
9292
CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
93+
Compiler.setVirtualFileSystem(Files->getVirtualFileSystemPtr());
9394
Compiler.setFileManager(Files);
9495

9596
// Create the compiler's actual diagnostics engine. We want to drop all

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,11 @@ class ASTUnit {
488488
return *PPOpts;
489489
}
490490

491+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> getVirtualFileSystemPtr() {
492+
// FIXME: Don't defer VFS ownership to the FileManager.
493+
return FileMgr->getVirtualFileSystemPtr();
494+
}
495+
491496
const FileManager &getFileManager() const { return *FileMgr; }
492497
FileManager &getFileManager() { return *FileMgr; }
493498

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,13 @@ class CompilerInstance : public ModuleLoader {
504504
FileMgr.resetWithoutRelease();
505505
}
506506

507-
/// Replace the current file manager and virtual file system.
507+
/// Replace the current file manager.
508508
void setFileManager(FileManager *Value);
509509

510+
/// @}
511+
/// @name Output Manager
512+
/// @{
513+
510514
/// Set the output manager.
511515
void setOutputBackend(IntrusiveRefCntPtr<llvm::vfs::OutputBackend> NewOutputs);
512516

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,7 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
16401640
AST->Reader = nullptr;
16411641

16421642
// Create a file manager object to provide access to and cache the filesystem.
1643+
Clang->setVirtualFileSystem(AST->getVirtualFileSystemPtr());
16431644
Clang->setFileManager(&AST->getFileManager());
16441645

16451646
// Create the source manager.
@@ -2277,6 +2278,7 @@ void ASTUnit::CodeComplete(
22772278
"IR inputs not support here!");
22782279

22792280
// Use the source and file managers that we were given.
2281+
Clang->setVirtualFileSystem(FileMgr.getVirtualFileSystemPtr());
22802282
Clang->setFileManager(&FileMgr);
22812283
Clang->setSourceManager(&SourceMgr);
22822284

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,6 @@ bool CompilerInstance::createTarget() {
173173
}
174174

175175
void CompilerInstance::setFileManager(FileManager *Value) {
176-
if (!hasVirtualFileSystem())
177-
setVirtualFileSystem(Value->getVirtualFileSystemPtr());
178176
assert(Value == nullptr ||
179177
getVirtualFileSystemPtr() == Value->getVirtualFileSystemPtr());
180178
FileMgr = Value;

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
985985

986986
// Set the shared objects, these are reset when we finish processing the
987987
// file, otherwise the CompilerInstance will happily destroy them.
988+
CI.setVirtualFileSystem(AST->getVirtualFileSystemPtr());
988989
CI.setFileManager(&AST->getFileManager());
989990
CI.setSourceManager(&AST->getSourceManager());
990991
CI.setPreprocessor(AST->getPreprocessorPtr());

clang/lib/Frontend/PrecompiledPreamble.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,12 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
479479
Diagnostics.Reset();
480480
ProcessWarningOptions(Diagnostics, Clang->getDiagnosticOpts(), *VFS);
481481

482-
Clang->createVirtualFileSystem(VFS, Diagnostics.getClient());
483-
484482
// Create a file manager object to provide access to and cache the filesystem.
483+
Clang->createVirtualFileSystem(VFS);
485484
Clang->createFileManager();
486485

487486
// Create the source manager.
488-
Clang->setSourceManager(
489-
new SourceManager(Diagnostics, Clang->getFileManager()));
487+
Clang->createSourceManager();
490488

491489
auto PreambleDepCollector = std::make_shared<PreambleDependencyCollector>();
492490
Clang->addDependencyCollector(PreambleDepCollector);

clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void ModelInjector::onBodySynthesis(const NamedDecl *D) {
9393

9494
// The instance wants to take ownership, however DisableFree frontend option
9595
// is set to true to avoid double free issues
96+
Instance.setVirtualFileSystem(CI.getVirtualFileSystemPtr());
9697
Instance.setFileManager(&CI.getFileManager());
9798
Instance.setSourceManager(&SM);
9899
Instance.setPreprocessor(CI.getPreprocessorPtr());

clang/lib/Tooling/Tooling.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ bool FrontendActionFactory::runInvocation(
445445
DiagnosticConsumer *DiagConsumer) {
446446
// Create a compiler instance to handle the actual work.
447447
CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
448+
Compiler.setVirtualFileSystem(Files->getVirtualFileSystemPtr());
448449
Compiler.setFileManager(Files);
449450

450451
// The FrontendAction can have lifetime requirements for Compiler or its

clang/tools/clang-installapi/ClangInstallAPI.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static bool run(ArrayRef<const char *> Args, const char *ProgName) {
113113

114114
// Set up compilation.
115115
std::unique_ptr<CompilerInstance> CI(new CompilerInstance());
116+
CI->setVirtualFileSystem(FM->getVirtualFileSystemPtr());
116117
CI->setFileManager(FM.get());
117118
CI->createDiagnostics();
118119
if (!CI->hasDiagnostics())

0 commit comments

Comments
 (0)