Skip to content

Commit b041249

Browse files
committed
Merge commit '30633f308941' from llvm.org/main into next
(cherry picked from commit 95ea104)
1 parent 53682c4 commit b041249

37 files changed

+185
-123
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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,10 +1179,14 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
11791179
// Ensure that Clang has a FileManager with the right VFS, which may have
11801180
// changed above in AddImplicitPreamble. If VFS is nullptr, rely on
11811181
// createFileManager to create one.
1182-
if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS)
1182+
if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS) {
1183+
Clang->setVirtualFileSystem(std::move(VFS));
11831184
Clang->setFileManager(&*FileMgr);
1184-
else
1185-
FileMgr = Clang->createFileManager(std::move(VFS));
1185+
} else {
1186+
Clang->setVirtualFileSystem(std::move(VFS));
1187+
Clang->createFileManager();
1188+
FileMgr = Clang->getFileManagerPtr().get();
1189+
}
11861190

11871191
// Recover resources if we crash before exiting this method.
11881192
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>

clang/lib/Frontend/ChainedIncludesSource.cpp

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

125125
auto Clang = std::make_unique<CompilerInstance>(
126126
std::move(CInvok), CI.getPCHContainerOperations());
127+
Clang->createVirtualFileSystem();
127128
Clang->setDiagnostics(Diags.get());
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: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ bool CompilerInstance::createTarget() {
172172
return true;
173173
}
174174

175-
llvm::vfs::FileSystem &CompilerInstance::getVirtualFileSystem() const {
176-
return getFileManager().getVirtualFileSystem();
177-
}
178-
179175
void CompilerInstance::setFileManager(FileManager *Value) {
176+
if (!hasVirtualFileSystem())
177+
setVirtualFileSystem(Value->getVirtualFileSystemPtr());
178+
assert(Value == nullptr ||
179+
getVirtualFileSystemPtr() == Value->getVirtualFileSystemPtr());
180180
FileMgr = Value;
181181
}
182182

@@ -287,6 +287,20 @@ static void collectVFSEntries(CompilerInstance &CI,
287287
MDC->addFile(E.VPath, E.RPath);
288288
}
289289

290+
void CompilerInstance::createVirtualFileSystem(
291+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS, DiagnosticConsumer *DC) {
292+
DiagnosticOptions DiagOpts;
293+
DiagnosticsEngine Diags(new DiagnosticIDs, DiagOpts, DC,
294+
/*ShouldOwnClient=*/false);
295+
296+
VFS = createVFSFromCompilerInvocation(getInvocation(), Diags,
297+
std::move(BaseFS), CAS);
298+
// FIXME: Should this go into createVFSFromCompilerInvocation?
299+
if (getFrontendOpts().ShowStats)
300+
VFS =
301+
llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(std::move(VFS));
302+
}
303+
290304
// Diagnostics
291305
static void SetUpDiagnosticLog(DiagnosticOptions &DiagOpts,
292306
const CodeGenOptions *CodeGenOpts,
@@ -338,11 +352,10 @@ static void SetupSerializedDiagnostics(DiagnosticOptions &DiagOpts,
338352
}
339353
}
340354

341-
void CompilerInstance::createDiagnostics(llvm::vfs::FileSystem &VFS,
342-
DiagnosticConsumer *Client,
355+
void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
343356
bool ShouldOwnClient) {
344-
Diagnostics = createDiagnostics(VFS, getDiagnosticOpts(), Client,
345-
ShouldOwnClient, &getCodeGenOpts());
357+
Diagnostics = createDiagnostics(getVirtualFileSystem(), getDiagnosticOpts(),
358+
Client, ShouldOwnClient, &getCodeGenOpts());
346359
}
347360

348361
IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
@@ -381,17 +394,9 @@ IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
381394

382395
// File Manager
383396

384-
FileManager *CompilerInstance::createFileManager(
385-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
386-
if (!VFS)
387-
VFS = FileMgr ? &FileMgr->getVirtualFileSystem()
388-
: createVFSFromCompilerInvocation(getInvocation(),
389-
getDiagnostics(), CAS);
390-
assert(VFS && "FileManager has no VFS?");
391-
if (getFrontendOpts().ShowStats)
392-
VFS =
393-
llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(std::move(VFS));
394-
FileMgr = new FileManager(getFileSystemOpts(), std::move(VFS));
397+
FileManager *CompilerInstance::createFileManager() {
398+
assert(VFS && "CompilerInstance needs a VFS for creating FileManager");
399+
FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(getFileSystemOpts(), VFS);
395400
return FileMgr.get();
396401
}
397402

@@ -1388,20 +1393,21 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
13881393
auto &Inv = Instance.getInvocation();
13891394

13901395
if (ThreadSafeConfig) {
1391-
Instance.createFileManager(ThreadSafeConfig->getVFS());
1396+
Instance.setVirtualFileSystem(ThreadSafeConfig->getVFS());
1397+
Instance.createFileManager();
13921398
} else if (FrontendOpts.ModulesShareFileManager) {
1399+
Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
13931400
Instance.setFileManager(&getFileManager());
13941401
} else {
1395-
Instance.createFileManager(&getVirtualFileSystem());
1402+
Instance.setVirtualFileSystem(&getVirtualFileSystem());
1403+
Instance.createFileManager();
13961404
}
13971405

13981406
if (ThreadSafeConfig) {
1399-
Instance.createDiagnostics(Instance.getVirtualFileSystem(),
1400-
&ThreadSafeConfig->getDiagConsumer(),
1407+
Instance.createDiagnostics(&ThreadSafeConfig->getDiagConsumer(),
14011408
/*ShouldOwnClient=*/false);
14021409
} else {
14031410
Instance.createDiagnostics(
1404-
Instance.getVirtualFileSystem(),
14051411
new ForwardingDiagnosticConsumer(getDiagnosticClient()),
14061412
/*ShouldOwnClient=*/true);
14071413
}

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,9 +1527,13 @@ static std::string serializeXRayInstrumentationBundle(const XRayInstrSet &S) {
15271527
static IntrusiveRefCntPtr<llvm::vfs::FileSystem>
15281528
createBaseFS(const FileSystemOptions &FSOpts, const FrontendOptions &FEOpts,
15291529
const CASOptions &CASOpts, DiagnosticsEngine &Diags,
1530+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
15301531
std::shared_ptr<llvm::cas::ObjectStore> OverrideCAS) {
1532+
if (!OverrideCAS)
1533+
return BaseFS;
1534+
15311535
if (FSOpts.CASFileSystemRootID.empty() && FEOpts.CASIncludeTreeID.empty())
1532-
return llvm::vfs::getRealFileSystem();
1536+
return BaseFS;
15331537

15341538
// If no CAS was provided, create one with CASOptions.
15351539
std::shared_ptr<llvm::cas::ObjectStore> CAS = std::move(OverrideCAS);
@@ -2214,7 +2218,8 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
22142218
}
22152219

22162220
if (!Opts.ProfileInstrumentUsePath.empty()) {
2217-
auto FS = createBaseFS(FSOpts, FEOpts, CASOpts, Diags, nullptr);
2221+
auto FS = createBaseFS(FSOpts, FEOpts, CASOpts, Diags,
2222+
llvm::vfs::getRealFileSystem(), nullptr);
22182223
setPGOUseInstrumentor(Opts, Opts.ProfileInstrumentUsePath, *FS, Diags);
22192224
}
22202225

@@ -5880,17 +5885,19 @@ clang::createVFSFromCompilerInvocation(
58805885
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
58815886
std::shared_ptr<llvm::cas::ObjectStore> OverrideCAS) {
58825887
return createVFSFromCompilerInvocation(
5883-
CI, Diags,
5884-
createBaseFS(CI.getFileSystemOpts(), CI.getFrontendOpts(),
5885-
CI.getCASOpts(), Diags, std::move(OverrideCAS)));
5888+
CI, Diags, llvm::vfs::getRealFileSystem(), std::move(OverrideCAS));
58865889
}
58875890

58885891
IntrusiveRefCntPtr<llvm::vfs::FileSystem>
58895892
clang::createVFSFromCompilerInvocation(
58905893
const CompilerInvocation &CI, DiagnosticsEngine &Diags,
5891-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
5892-
return createVFSFromOverlayFiles(CI.getHeaderSearchOpts().VFSOverlayFiles,
5893-
Diags, std::move(BaseFS));
5894+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
5895+
std::shared_ptr<llvm::cas::ObjectStore> OverrideCAS) {
5896+
return createVFSFromOverlayFiles(
5897+
CI.getHeaderSearchOpts().VFSOverlayFiles, Diags,
5898+
createBaseFS(CI.getFileSystemOpts(), CI.getFrontendOpts(),
5899+
CI.getCASOpts(), Diags, std::move(BaseFS),
5900+
std::move(OverrideCAS)));
58945901
}
58955902

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

0 commit comments

Comments
 (0)