Skip to content

Commit af70946

Browse files
jansvoboda11qiongsiwu
authored andcommitted
Merge commit '30633f308941' from llvm.org/main into next
(cherry picked from commit 95ea104) Conflicts: clang/include/clang/Frontend/CompilerInstance.h clang/lib/Frontend/CompilerInstance.cpp
1 parent 3c62cab commit af70946

37 files changed

+184
-126
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ bool IncludeFixerActionFactory::runInvocation(
9494

9595
// Create the compiler's actual diagnostics engine. We want to drop all
9696
// diagnostics here.
97-
Compiler.createDiagnostics(Files->getVirtualFileSystem(),
98-
new clang::IgnoringDiagConsumer,
97+
Compiler.createDiagnostics(new clang::IgnoringDiagConsumer,
9998
/*ShouldOwnClient=*/true);
10099
Compiler.createSourceManager(*Files);
101100

clang-tools-extra/clangd/Compiler.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,9 @@ prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
147147
}
148148

149149
auto Clang = std::make_unique<CompilerInstance>(std::move(CI));
150-
Clang->createDiagnostics(*VFS, &DiagsClient, false);
151-
152-
if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
153-
Clang->getInvocation(), Clang->getDiagnostics(), VFS))
154-
VFS = VFSWithRemapping;
155-
Clang->createFileManager(VFS);
156-
150+
Clang->createVirtualFileSystem(VFS, &DiagsClient);
151+
Clang->createDiagnostics(&DiagsClient, false);
152+
Clang->createFileManager();
157153
if (!Clang->createTarget())
158154
return nullptr;
159155

clang-tools-extra/include-cleaner/unittests/RecordTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,9 +625,10 @@ TEST_F(PragmaIncludeTest, ExportInUnnamedBuffer) {
625625
*Diags, "clang"));
626626

627627
auto Clang = std::make_unique<CompilerInstance>(std::move(Invocation));
628-
Clang->createDiagnostics(*VFS);
628+
Clang->createVirtualFileSystem(VFS);
629+
Clang->createDiagnostics();
629630

630-
auto *FM = Clang->createFileManager(VFS);
631+
auto *FM = Clang->createFileManager();
631632
ASSERT_TRUE(Clang->ExecuteAction(*Inputs.MakeAction()));
632633
EXPECT_THAT(
633634
PI.getExporters(llvm::cantFail(FM->getFileRef("foo.h")), *FM),

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class CompilerInstance : public ModuleLoader {
8787
/// The options used in this compiler instance.
8888
std::shared_ptr<CompilerInvocation> Invocation;
8989

90+
/// The virtual file system instance.
91+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS;
92+
9093
/// The diagnostics engine instance.
9194
IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
9295

@@ -453,7 +456,31 @@ class CompilerInstance : public ModuleLoader {
453456
/// @name Virtual File System
454457
/// @{
455458

456-
llvm::vfs::FileSystem &getVirtualFileSystem() const;
459+
bool hasVirtualFileSystem() const { return VFS != nullptr; }
460+
461+
/// Create a virtual file system instance based on the invocation.
462+
///
463+
/// @param BaseFS The file system that may be used when configuring the final
464+
/// file system, and act as the underlying file system. Must not
465+
/// be NULL.
466+
/// @param DC If non-NULL, the diagnostic consumer to be used in case
467+
/// configuring the file system emits diagnostics. Note that the
468+
/// DiagnosticsEngine using the consumer won't obey the
469+
/// --warning-suppression-mappings= flag.
470+
void createVirtualFileSystem(IntrusiveRefCntPtr<llvm::vfs::FileSystem>
471+
BaseFS = llvm::vfs::getRealFileSystem(),
472+
DiagnosticConsumer *DC = nullptr);
473+
474+
/// Use the given file system.
475+
void setVirtualFileSystem(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
476+
VFS = std::move(FS);
477+
}
478+
479+
llvm::vfs::FileSystem &getVirtualFileSystem() const { return *VFS; }
480+
481+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> getVirtualFileSystemPtr() const {
482+
return VFS;
483+
}
457484

458485
/// @}
459486
/// @name File Manager
@@ -691,32 +718,31 @@ class CompilerInstance : public ModuleLoader {
691718
/// Note that this routine also replaces the diagnostic client,
692719
/// allocating one if one is not provided.
693720
///
694-
/// \param VFS is used for any IO needed when creating DiagnosticsEngine. It
695-
/// doesn't replace VFS in the CompilerInstance (if any).
696-
///
697721
/// \param Client If non-NULL, a diagnostic client that will be
698722
/// attached to (and, then, owned by) the DiagnosticsEngine inside this AST
699723
/// unit.
700724
///
701725
/// \param ShouldOwnClient If Client is non-NULL, specifies whether
702726
/// the diagnostic object should take ownership of the client.
703-
void createDiagnostics(llvm::vfs::FileSystem &VFS,
704-
DiagnosticConsumer *Client = nullptr,
727+
void createDiagnostics(DiagnosticConsumer *Client = nullptr,
705728
bool ShouldOwnClient = true);
706729

707-
/// Create a DiagnosticsEngine object with a the TextDiagnosticPrinter.
730+
/// Create a DiagnosticsEngine object.
708731
///
709732
/// If no diagnostic client is provided, this creates a
710733
/// DiagnosticConsumer that is owned by the returned diagnostic
711734
/// object, if using directly the caller is responsible for
712735
/// releasing the returned DiagnosticsEngine's client eventually.
713736
///
737+
/// \param VFS The file system used to load the suppression mappings file.
738+
///
714739
/// \param Opts - The diagnostic options; note that the created text
715740
/// diagnostic object contains a reference to these options.
716741
///
717742
/// \param Client If non-NULL, a diagnostic client that will be
718743
/// attached to (and, then, owned by) the returned DiagnosticsEngine
719-
/// object.
744+
/// object. If NULL, the returned DiagnosticsEngine will own a newly-created
745+
/// client.
720746
///
721747
/// \param CodeGenOpts If non-NULL, the code gen options in use, which may be
722748
/// used by some diagnostics printers (for logging purposes only).
@@ -731,8 +757,7 @@ class CompilerInstance : public ModuleLoader {
731757
/// Create the file manager and replace any existing one with it.
732758
///
733759
/// \return The new file manager on success, or null on failure.
734-
FileManager *
735-
createFileManager(IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
760+
FileManager *createFileManager();
736761

737762
/// Create the source manager and replace any existing one with it.
738763
void createSourceManager(FileManager &FileMgr);

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
434434

435435
IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
436436
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
437-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
437+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
438+
std::shared_ptr<llvm::cas::ObjectStore> OverrideCAS = nullptr);
438439

439440
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
440441
createVFSFromOverlayFiles(ArrayRef<std::string> VFSOverlayFiles,

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,10 +1180,12 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
11801180
// Ensure that Clang has a FileManager with the right VFS, which may have
11811181
// changed above in AddImplicitPreamble. If VFS is nullptr, rely on
11821182
// createFileManager to create one.
1183-
if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS)
1183+
if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS) {
1184+
Clang->setVirtualFileSystem(std::move(VFS));
11841185
Clang->setFileManager(FileMgr);
1185-
else {
1186-
Clang->createFileManager(std::move(VFS));
1186+
} else {
1187+
Clang->setVirtualFileSystem(std::move(VFS));
1188+
Clang->createFileManager();
11871189
FileMgr = Clang->getFileManagerPtr();
11881190
}
11891191

clang/lib/Frontend/ChainedIncludesSource.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ clang::createChainedIncludesSource(CompilerInstance &CI,
123123

124124
auto Clang = std::make_unique<CompilerInstance>(
125125
std::move(CInvok), CI.getPCHContainerOperations());
126+
Clang->createVirtualFileSystem();
126127
Clang->setDiagnostics(Diags);
127128
Clang->setTarget(TargetInfo::CreateTargetInfo(
128129
Clang->getDiagnostics(), Clang->getInvocation().getTargetOpts()));

clang/lib/Frontend/CompileJobCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,8 @@ Expected<std::optional<int>> CompileJobCache::replayCachedResult(
613613
std::optional<llvm::cas::CASID> *OutMCOutputID) {
614614
CompilerInstance Clang(std::move(Invok));
615615
llvm::raw_svector_ostream DiagOS(DiagText);
616+
Clang.createVirtualFileSystem(llvm::vfs::getRealFileSystem());
616617
Clang.createDiagnostics(
617-
*llvm::vfs::getRealFileSystem(),
618618
new TextDiagnosticPrinter(DiagOS, Clang.getDiagnosticOpts()));
619619
Clang.setVerboseOutputStream(DiagOS);
620620

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,11 @@ bool CompilerInstance::createTarget() {
173173
return true;
174174
}
175175

176-
llvm::vfs::FileSystem &CompilerInstance::getVirtualFileSystem() const {
177-
return getFileManager().getVirtualFileSystem();
178-
}
179-
180-
void CompilerInstance::setFileManager(
181-
llvm::IntrusiveRefCntPtr<FileManager> Value) {
176+
void CompilerInstance::setFileManager(IntrusiveRefCntPtr<FileManager> Value) {
177+
if (!hasVirtualFileSystem())
178+
setVirtualFileSystem(Value->getVirtualFileSystemPtr());
179+
assert(Value == nullptr ||
180+
getVirtualFileSystemPtr() == Value->getVirtualFileSystemPtr());
182181
FileMgr = std::move(Value);
183182
}
184183

@@ -298,6 +297,20 @@ static void collectVFSEntries(CompilerInstance &CI,
298297
MDC->addFile(E.VPath, E.RPath);
299298
}
300299

300+
void CompilerInstance::createVirtualFileSystem(
301+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS, DiagnosticConsumer *DC) {
302+
DiagnosticOptions DiagOpts;
303+
DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DC,
304+
/*ShouldOwnClient=*/false);
305+
306+
VFS = createVFSFromCompilerInvocation(getInvocation(), Diags,
307+
std::move(BaseFS), CAS);
308+
// FIXME: Should this go into createVFSFromCompilerInvocation?
309+
if (getFrontendOpts().ShowStats)
310+
VFS =
311+
llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(std::move(VFS));
312+
}
313+
301314
// Diagnostics
302315
static void SetUpDiagnosticLog(DiagnosticOptions &DiagOpts,
303316
const CodeGenOptions *CodeGenOpts,
@@ -349,11 +362,10 @@ static void SetupSerializedDiagnostics(DiagnosticOptions &DiagOpts,
349362
}
350363
}
351364

352-
void CompilerInstance::createDiagnostics(llvm::vfs::FileSystem &VFS,
353-
DiagnosticConsumer *Client,
365+
void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
354366
bool ShouldOwnClient) {
355-
Diagnostics = createDiagnostics(VFS, getDiagnosticOpts(), Client,
356-
ShouldOwnClient, &getCodeGenOpts());
367+
Diagnostics = createDiagnostics(getVirtualFileSystem(), getDiagnosticOpts(),
368+
Client, ShouldOwnClient, &getCodeGenOpts());
357369
}
358370

359371
IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
@@ -391,18 +403,9 @@ IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
391403

392404
// File Manager
393405

394-
FileManager *CompilerInstance::createFileManager(
395-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
396-
if (!VFS)
397-
VFS = FileMgr ? &FileMgr->getVirtualFileSystem()
398-
: createVFSFromCompilerInvocation(getInvocation(),
399-
getDiagnostics(), CAS);
400-
assert(VFS && "FileManager has no VFS?");
401-
if (getFrontendOpts().ShowStats)
402-
VFS =
403-
llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(std::move(VFS));
404-
FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(),
405-
std::move(VFS));
406+
FileManager *CompilerInstance::createFileManager() {
407+
assert(VFS && "CompilerInstance needs a VFS for creating FileManager");
408+
FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(), VFS);
406409
return FileMgr.get();
407410
}
408411

@@ -1400,20 +1403,21 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
14001403
auto &Inv = Instance.getInvocation();
14011404

14021405
if (ThreadSafeConfig) {
1403-
Instance.createFileManager(ThreadSafeConfig->getVFS());
1406+
Instance.setVirtualFileSystem(ThreadSafeConfig->getVFS());
1407+
Instance.createFileManager();
14041408
} else if (FrontendOpts.ModulesShareFileManager) {
1409+
Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
14051410
Instance.setFileManager(getFileManagerPtr());
14061411
} else {
1407-
Instance.createFileManager(&getVirtualFileSystem());
1412+
Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
1413+
Instance.createFileManager();
14081414
}
14091415

14101416
if (ThreadSafeConfig) {
1411-
Instance.createDiagnostics(Instance.getVirtualFileSystem(),
1412-
&ThreadSafeConfig->getDiagConsumer(),
1417+
Instance.createDiagnostics(&ThreadSafeConfig->getDiagConsumer(),
14131418
/*ShouldOwnClient=*/false);
14141419
} else {
14151420
Instance.createDiagnostics(
1416-
Instance.getVirtualFileSystem(),
14171421
new ForwardingDiagnosticConsumer(getDiagnosticClient()),
14181422
/*ShouldOwnClient=*/true);
14191423
}

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,9 +1536,13 @@ static std::string serializeXRayInstrumentationBundle(const XRayInstrSet &S) {
15361536
static IntrusiveRefCntPtr<llvm::vfs::FileSystem>
15371537
createBaseFS(const FileSystemOptions &FSOpts, const FrontendOptions &FEOpts,
15381538
const CASOptions &CASOpts, DiagnosticsEngine &Diags,
1539+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
15391540
std::shared_ptr<llvm::cas::ObjectStore> OverrideCAS) {
1541+
if (!OverrideCAS)
1542+
return BaseFS;
1543+
15401544
if (FSOpts.CASFileSystemRootID.empty() && FEOpts.CASIncludeTreeID.empty())
1541-
return llvm::vfs::getRealFileSystem();
1545+
return BaseFS;
15421546

15431547
// If no CAS was provided, create one with CASOptions.
15441548
std::shared_ptr<llvm::cas::ObjectStore> CAS = std::move(OverrideCAS);
@@ -2223,7 +2227,8 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
22232227
}
22242228

22252229
if (!Opts.ProfileInstrumentUsePath.empty()) {
2226-
auto FS = createBaseFS(FSOpts, FEOpts, CASOpts, Diags, nullptr);
2230+
auto FS = createBaseFS(FSOpts, FEOpts, CASOpts, Diags,
2231+
llvm::vfs::getRealFileSystem(), nullptr);
22272232
setPGOUseInstrumentor(Opts, Opts.ProfileInstrumentUsePath, *FS, Diags);
22282233
}
22292234

@@ -5901,17 +5906,19 @@ clang::createVFSFromCompilerInvocation(
59015906
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
59025907
std::shared_ptr<llvm::cas::ObjectStore> OverrideCAS) {
59035908
return createVFSFromCompilerInvocation(
5904-
CI, Diags,
5905-
createBaseFS(CI.getFileSystemOpts(), CI.getFrontendOpts(),
5906-
CI.getCASOpts(), Diags, std::move(OverrideCAS)));
5909+
CI, Diags, llvm::vfs::getRealFileSystem(), std::move(OverrideCAS));
59075910
}
59085911

59095912
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
59105913
clang::createVFSFromCompilerInvocation(
59115914
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
5912-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
5913-
return createVFSFromOverlayFiles(CI.getHeaderSearchOpts().VFSOverlayFiles,
5914-
Diags, std::move(BaseFS));
5915+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
5916+
std::shared_ptr<llvm::cas::ObjectStore> OverrideCAS) {
5917+
return createVFSFromOverlayFiles(
5918+
CI.getHeaderSearchOpts().VFSOverlayFiles, Diags,
5919+
createBaseFS(CI.getFileSystemOpts(), CI.getFrontendOpts(),
5920+
CI.getCASOpts(), Diags, std::move(BaseFS),
5921+
std::move(OverrideCAS)));
59155922
}
59165923

59175924
IntrusiveRefCntPtr<llvm::vfs::FileSystem> clang::createVFSFromOverlayFiles(

0 commit comments

Comments
 (0)