Skip to content

Commit 95ea104

Browse files
committed
Merge commit '30633f308941' from llvm.org/main into next
2 parents 0ceb749 + 30633f3 commit 95ea104

37 files changed

+184
-133
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
@@ -646,9 +646,10 @@ TEST_F(PragmaIncludeTest, ExportInUnnamedBuffer) {
646646
*Diags, "clang"));
647647

648648
auto Clang = std::make_unique<CompilerInstance>(std::move(Invocation));
649-
Clang->createDiagnostics(*VFS);
649+
Clang->createVirtualFileSystem(VFS);
650+
Clang->createDiagnostics();
650651

651-
auto *FM = Clang->createFileManager(VFS);
652+
auto *FM = Clang->createFileManager();
652653
ASSERT_TRUE(Clang->ExecuteAction(*Inputs.MakeAction()));
653654
EXPECT_THAT(
654655
PI.getExporters(llvm::cantFail(FM->getFileRef("foo.h")), *FM),

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 35 additions & 12 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,9 +456,31 @@ class CompilerInstance : public ModuleLoader {
453456
/// @name Virtual File System
454457
/// @{
455458

456-
llvm::vfs::FileSystem &getVirtualFileSystem() const;
457-
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
458-
getVirtualFileSystemPtr() 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+
}
459484

460485
/// @}
461486
/// @name File Manager
@@ -702,32 +727,31 @@ class CompilerInstance : public ModuleLoader {
702727
/// Note that this routine also replaces the diagnostic client,
703728
/// allocating one if one is not provided.
704729
///
705-
/// \param VFS is used for any IO needed when creating DiagnosticsEngine. It
706-
/// doesn't replace VFS in the CompilerInstance (if any).
707-
///
708730
/// \param Client If non-NULL, a diagnostic client that will be
709731
/// attached to (and, then, owned by) the DiagnosticsEngine inside this AST
710732
/// unit.
711733
///
712734
/// \param ShouldOwnClient If Client is non-NULL, specifies whether
713735
/// the diagnostic object should take ownership of the client.
714-
void createDiagnostics(llvm::vfs::FileSystem &VFS,
715-
DiagnosticConsumer *Client = nullptr,
736+
void createDiagnostics(DiagnosticConsumer *Client = nullptr,
716737
bool ShouldOwnClient = true);
717738

718-
/// Create a DiagnosticsEngine object with a the TextDiagnosticPrinter.
739+
/// Create a DiagnosticsEngine object.
719740
///
720741
/// If no diagnostic client is provided, this creates a
721742
/// DiagnosticConsumer that is owned by the returned diagnostic
722743
/// object, if using directly the caller is responsible for
723744
/// releasing the returned DiagnosticsEngine's client eventually.
724745
///
746+
/// \param VFS The file system used to load the suppression mappings file.
747+
///
725748
/// \param Opts - The diagnostic options; note that the created text
726749
/// diagnostic object contains a reference to these options.
727750
///
728751
/// \param Client If non-NULL, a diagnostic client that will be
729752
/// attached to (and, then, owned by) the returned DiagnosticsEngine
730-
/// object.
753+
/// object. If NULL, the returned DiagnosticsEngine will own a newly-created
754+
/// client.
731755
///
732756
/// \param CodeGenOpts If non-NULL, the code gen options in use, which may be
733757
/// used by some diagnostics printers (for logging purposes only).
@@ -742,8 +766,7 @@ class CompilerInstance : public ModuleLoader {
742766
/// Create the file manager and replace any existing one with it.
743767
///
744768
/// \return The new file manager on success, or null on failure.
745-
FileManager *
746-
createFileManager(IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
769+
FileManager *createFileManager();
747770

748771
/// Create the source manager and replace any existing one with it.
749772
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
@@ -1189,10 +1189,12 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
11891189
// Ensure that Clang has a FileManager with the right VFS, which may have
11901190
// changed above in AddImplicitPreamble. If VFS is nullptr, rely on
11911191
// createFileManager to create one.
1192-
if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS)
1192+
if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS) {
1193+
Clang->setVirtualFileSystem(std::move(VFS));
11931194
Clang->setFileManager(FileMgr);
1194-
else {
1195-
Clang->createFileManager(std::move(VFS));
1195+
} else {
1196+
Clang->setVirtualFileSystem(std::move(VFS));
1197+
Clang->createFileManager();
11961198
FileMgr = Clang->getFileManagerPtr();
11971199
}
11981200

clang/lib/Frontend/ChainedIncludesSource.cpp

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

125125
auto Clang = std::make_unique<CompilerInstance>(
126126
std::move(CInvok), CI.getPCHContainerOperations());
127+
Clang->createVirtualFileSystem();
127128
Clang->setDiagnostics(Diags);
128129
Clang->setTarget(TargetInfo::CreateTargetInfo(
129130
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 & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,11 @@ bool CompilerInstance::createTarget() {
173173
return true;
174174
}
175175

176-
llvm::vfs::FileSystem &CompilerInstance::getVirtualFileSystem() const {
177-
return getFileManager().getVirtualFileSystem();
178-
}
179-
180-
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
181-
CompilerInstance::getVirtualFileSystemPtr() const {
182-
return getFileManager().getVirtualFileSystemPtr();
183-
}
184-
185-
void CompilerInstance::setFileManager(
186-
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());
187181
FileMgr = std::move(Value);
188182
}
189183

@@ -296,6 +290,20 @@ static void collectVFSEntries(CompilerInstance &CI,
296290
MDC->addFile(E.VPath, E.RPath);
297291
}
298292

293+
void CompilerInstance::createVirtualFileSystem(
294+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS, DiagnosticConsumer *DC) {
295+
DiagnosticOptions DiagOpts;
296+
DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DC,
297+
/*ShouldOwnClient=*/false);
298+
299+
VFS = createVFSFromCompilerInvocation(getInvocation(), Diags,
300+
std::move(BaseFS), CAS);
301+
// FIXME: Should this go into createVFSFromCompilerInvocation?
302+
if (getFrontendOpts().ShowStats)
303+
VFS =
304+
llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(std::move(VFS));
305+
}
306+
299307
// Diagnostics
300308
static void SetUpDiagnosticLog(DiagnosticOptions &DiagOpts,
301309
const CodeGenOptions *CodeGenOpts,
@@ -347,11 +355,10 @@ static void SetupSerializedDiagnostics(DiagnosticOptions &DiagOpts,
347355
}
348356
}
349357

350-
void CompilerInstance::createDiagnostics(llvm::vfs::FileSystem &VFS,
351-
DiagnosticConsumer *Client,
358+
void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
352359
bool ShouldOwnClient) {
353-
Diagnostics = createDiagnostics(VFS, getDiagnosticOpts(), Client,
354-
ShouldOwnClient, &getCodeGenOpts());
360+
Diagnostics = createDiagnostics(getVirtualFileSystem(), getDiagnosticOpts(),
361+
Client, ShouldOwnClient, &getCodeGenOpts());
355362
}
356363

357364
IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
@@ -389,18 +396,9 @@ IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
389396

390397
// File Manager
391398

392-
FileManager *CompilerInstance::createFileManager(
393-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
394-
if (!VFS)
395-
VFS = FileMgr ? FileMgr->getVirtualFileSystemPtr()
396-
: createVFSFromCompilerInvocation(getInvocation(),
397-
getDiagnostics(), CAS);
398-
assert(VFS && "FileManager has no VFS?");
399-
if (getFrontendOpts().ShowStats)
400-
VFS =
401-
llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(std::move(VFS));
402-
FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(),
403-
std::move(VFS));
399+
FileManager *CompilerInstance::createFileManager() {
400+
assert(VFS && "CompilerInstance needs a VFS for creating FileManager");
401+
FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(), VFS);
404402
return FileMgr.get();
405403
}
406404

@@ -1401,20 +1399,21 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
14011399
auto &Inv = Instance.getInvocation();
14021400

14031401
if (ThreadSafeConfig) {
1404-
Instance.createFileManager(ThreadSafeConfig->getVFS());
1402+
Instance.setVirtualFileSystem(ThreadSafeConfig->getVFS());
1403+
Instance.createFileManager();
14051404
} else if (FrontendOpts.ModulesShareFileManager) {
1405+
Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
14061406
Instance.setFileManager(getFileManagerPtr());
14071407
} else {
1408-
Instance.createFileManager(getVirtualFileSystemPtr());
1408+
Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
1409+
Instance.createFileManager();
14091410
}
14101411

14111412
if (ThreadSafeConfig) {
1412-
Instance.createDiagnostics(Instance.getVirtualFileSystem(),
1413-
&ThreadSafeConfig->getDiagConsumer(),
1413+
Instance.createDiagnostics(&ThreadSafeConfig->getDiagConsumer(),
14141414
/*ShouldOwnClient=*/false);
14151415
} else {
14161416
Instance.createDiagnostics(
1417-
Instance.getVirtualFileSystem(),
14181417
new ForwardingDiagnosticConsumer(getDiagnosticClient()),
14191418
/*ShouldOwnClient=*/true);
14201419
}

clang/lib/Frontend/CompilerInvocation.cpp

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

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

22292233
if (!Opts.ProfileInstrumentUsePath.empty()) {
2230-
auto FS = createBaseFS(FSOpts, FEOpts, CASOpts, Diags, nullptr);
2234+
auto FS = createBaseFS(FSOpts, FEOpts, CASOpts, Diags,
2235+
llvm::vfs::getRealFileSystem(), nullptr);
22312236
setPGOUseInstrumentor(Opts, Opts.ProfileInstrumentUsePath, *FS, Diags);
22322237
}
22332238

@@ -5802,17 +5807,19 @@ clang::createVFSFromCompilerInvocation(
58025807
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
58035808
std::shared_ptr<llvm::cas::ObjectStore> OverrideCAS) {
58045809
return createVFSFromCompilerInvocation(
5805-
CI, Diags,
5806-
createBaseFS(CI.getFileSystemOpts(), CI.getFrontendOpts(),
5807-
CI.getCASOpts(), Diags, std::move(OverrideCAS)));
5810+
CI, Diags, llvm::vfs::getRealFileSystem(), std::move(OverrideCAS));
58085811
}
58095812

58105813
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
58115814
clang::createVFSFromCompilerInvocation(
58125815
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
5813-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
5814-
return createVFSFromOverlayFiles(CI.getHeaderSearchOpts().VFSOverlayFiles,
5815-
Diags, std::move(BaseFS));
5816+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
5817+
std::shared_ptr<llvm::cas::ObjectStore> OverrideCAS) {
5818+
return createVFSFromOverlayFiles(
5819+
CI.getHeaderSearchOpts().VFSOverlayFiles, Diags,
5820+
createBaseFS(CI.getFileSystemOpts(), CI.getFrontendOpts(),
5821+
CI.getCASOpts(), Diags, std::move(BaseFS),
5822+
std::move(OverrideCAS)));
58165823
}
58175824

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

0 commit comments

Comments
 (0)