Skip to content

[NFC] Explicitly pass a VFS when creating DiagnosticsEngine #115852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ bool IncludeFixerActionFactory::runInvocation(

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

Expand Down
6 changes: 3 additions & 3 deletions clang-tools-extra/clangd/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
CIOpts.VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
CIOpts.CC1Args = CC1Args;
CIOpts.RecoverOnError = true;
CIOpts.Diags =
CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false);
CIOpts.Diags = CompilerInstance::createDiagnostics(
*CIOpts.VFS, new DiagnosticOptions, &D, false);
CIOpts.ProbePrecompiled = false;
std::unique_ptr<CompilerInvocation> CI = createInvocation(ArgStrs, CIOpts);
if (!CI)
Expand Down Expand Up @@ -148,7 +148,7 @@ prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
auto Clang = std::make_unique<CompilerInstance>(
std::make_shared<PCHContainerOperations>());
Clang->setInvocation(std::move(CI));
Clang->createDiagnostics(&DiagsClient, false);
Clang->createDiagnostics(*VFS, &DiagsClient, false);

if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
Clang->getInvocation(), Clang->getDiagnostics(), VFS))
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/ModulesBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ bool IsModuleFileUpToDate(PathRef ModuleFilePath,

clang::clangd::IgnoreDiagnostics IgnoreDiags;
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
CompilerInstance::createDiagnostics(new DiagnosticOptions, &IgnoreDiags,
CompilerInstance::createDiagnostics(*VFS, new DiagnosticOptions,
&IgnoreDiags,
/*ShouldOwnClient=*/false);

LangOptions LangOpts;
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/Preamble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,9 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
for (const auto &L : ASTListeners)
L->sawDiagnostic(D, Diag);
});
auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
CompilerInstance::createDiagnostics(&CI.getDiagnosticOpts(),
CompilerInstance::createDiagnostics(*VFS, &CI.getDiagnosticOpts(),
&PreambleDiagnostics,
/*ShouldOwnClient=*/false);
const Config &Cfg = Config::current();
Expand Down Expand Up @@ -651,7 +652,6 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
for (const auto &L : ASTListeners)
L->beforeExecute(CI);
});
auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
llvm::SmallString<32> AbsFileName(FileName);
VFS->makeAbsolute(AbsFileName);
auto StatCache = std::make_shared<PreambleFileStatusCache>(AbsFileName);
Expand Down
19 changes: 10 additions & 9 deletions clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,15 +609,6 @@ TEST_F(PragmaIncludeTest, ExportInUnnamedBuffer) {
)cpp";
Inputs.ExtraFiles["foo.h"] = "";

auto Clang = std::make_unique<CompilerInstance>(
std::make_shared<PCHContainerOperations>());
Clang->createDiagnostics();

Clang->setInvocation(std::make_unique<CompilerInvocation>());
ASSERT_TRUE(CompilerInvocation::CreateFromArgs(
Clang->getInvocation(), {Filename.data()}, Clang->getDiagnostics(),
"clang"));

// Create unnamed memory buffers for all the files.
auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
VFS->addFile(Filename, /*ModificationTime=*/0,
Expand All @@ -626,6 +617,16 @@ TEST_F(PragmaIncludeTest, ExportInUnnamedBuffer) {
VFS->addFile(Extra.getKey(), /*ModificationTime=*/0,
llvm::MemoryBuffer::getMemBufferCopy(Extra.getValue(),
/*BufferName=*/""));

auto Clang = std::make_unique<CompilerInstance>(
std::make_shared<PCHContainerOperations>());
Clang->createDiagnostics(*VFS);

Clang->setInvocation(std::make_unique<CompilerInvocation>());
ASSERT_TRUE(CompilerInvocation::CreateFromArgs(
Clang->getInvocation(), {Filename.data()}, Clang->getDiagnostics(),
"clang"));

auto *FM = Clang->createFileManager(VFS);
ASSERT_TRUE(Clang->ExecuteAction(*Inputs.MakeAction()));
EXPECT_THAT(
Expand Down
15 changes: 10 additions & 5 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,13 +675,17 @@ class CompilerInstance : public ModuleLoader {
/// Note that this routine also replaces the diagnostic client,
/// allocating one if one is not provided.
///
/// \param VFS is used for any IO needed when creating DiagnosticsEngine. It
/// doesn't replace VFS in the CompilerInstance (if any).
///
/// \param Client If non-NULL, a diagnostic client that will be
/// attached to (and, then, owned by) the DiagnosticsEngine inside this AST
/// unit.
///
/// \param ShouldOwnClient If Client is non-NULL, specifies whether
/// the diagnostic object should take ownership of the client.
void createDiagnostics(DiagnosticConsumer *Client = nullptr,
void createDiagnostics(llvm::vfs::FileSystem &VFS,
DiagnosticConsumer *Client = nullptr,
bool ShouldOwnClient = true);

/// Create a DiagnosticsEngine object with a the TextDiagnosticPrinter.
Expand All @@ -702,10 +706,11 @@ class CompilerInstance : public ModuleLoader {
/// used by some diagnostics printers (for logging purposes only).
///
/// \return The new object on success, or null on failure.
static IntrusiveRefCntPtr<DiagnosticsEngine> createDiagnostics(
DiagnosticOptions *Opts, DiagnosticConsumer *Client = nullptr,
bool ShouldOwnClient = true, const CodeGenOptions *CodeGenOpts = nullptr,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
static IntrusiveRefCntPtr<DiagnosticsEngine>
createDiagnostics(llvm::vfs::FileSystem &VFS, DiagnosticOptions *Opts,
DiagnosticConsumer *Client = nullptr,
bool ShouldOwnClient = true,
const CodeGenOptions *CodeGenOpts = nullptr);

/// Create the file manager and replace any existing one with it.
///
Expand Down
30 changes: 14 additions & 16 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,20 @@ static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts,
}
}

void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
void CompilerInstance::createDiagnostics(llvm::vfs::FileSystem &VFS,
DiagnosticConsumer *Client,
bool ShouldOwnClient) {
Diagnostics = createDiagnostics(
&getDiagnosticOpts(), Client, ShouldOwnClient, &getCodeGenOpts(),
FileMgr ? FileMgr->getVirtualFileSystemPtr() : nullptr);
Diagnostics = createDiagnostics(VFS, &getDiagnosticOpts(), Client,
ShouldOwnClient, &getCodeGenOpts());
}

IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
DiagnosticOptions *Opts, DiagnosticConsumer *Client, bool ShouldOwnClient,
const CodeGenOptions *CodeGenOpts,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
llvm::vfs::FileSystem &VFS, DiagnosticOptions *Opts,
DiagnosticConsumer *Client, bool ShouldOwnClient,
const CodeGenOptions *CodeGenOpts) {
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
IntrusiveRefCntPtr<DiagnosticsEngine>
Diags(new DiagnosticsEngine(DiagID, Opts));

if (!VFS)
VFS = llvm::vfs::getRealFileSystem();
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
new DiagnosticsEngine(DiagID, Opts));

// Create the diagnostic client for reporting errors or for
// implementing -verify.
Expand All @@ -372,7 +369,7 @@ IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
Opts->DiagnosticSerializationFile);

// Configure our handling of diagnostics.
ProcessWarningOptions(*Diags, *Opts, *VFS);
ProcessWarningOptions(*Diags, *Opts, VFS);

return Diags;
}
Expand Down Expand Up @@ -1240,9 +1237,10 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
auto &Inv = *Invocation;
Instance.setInvocation(std::move(Invocation));

Instance.createDiagnostics(new ForwardingDiagnosticConsumer(
ImportingInstance.getDiagnosticClient()),
/*ShouldOwnClient=*/true);
Instance.createDiagnostics(
ImportingInstance.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(ImportingInstance.getDiagnosticClient()),
/*ShouldOwnClient=*/true);

if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName))
Instance.getDiagnostics().setSuppressSystemWarnings(false);
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/TargetParser/Host.h"
using namespace clang;
using namespace llvm::opt;
Expand All @@ -32,7 +33,9 @@ clang::createInvocation(ArrayRef<const char *> ArgList,
assert(!ArgList.empty());
auto Diags = Opts.Diags
? std::move(Opts.Diags)
: CompilerInstance::createDiagnostics(new DiagnosticOptions);
: CompilerInstance::createDiagnostics(
Opts.VFS ? *Opts.VFS : *llvm::vfs::getRealFileSystem(),
new DiagnosticOptions);

SmallVector<const char *, 16> Args(ArgList);

Expand Down
1 change: 1 addition & 0 deletions clang/lib/Frontend/Rewrite/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {
Instance.setInvocation(
std::make_shared<CompilerInvocation>(CI.getInvocation()));
Instance.createDiagnostics(
CI.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
/*ShouldOwnClient=*/true);
Instance.getFrontendOpts().DisableFree = false;
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "IncrementalExecutor.h"
#include "IncrementalParser.h"
#include "InterpreterUtils.h"
#include "llvm/Support/VirtualFileSystem.h"
#ifdef __EMSCRIPTEN__
#include "Wasm.h"
#endif // __EMSCRIPTEN__
Expand Down Expand Up @@ -104,7 +105,7 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
CompilerInvocation::GetResourcesPath(Argv[0], nullptr);

// Create the actual diagnostics engine.
Clang->createDiagnostics();
Clang->createDiagnostics(*llvm::vfs::getRealFileSystem());
if (!Clang->hasDiagnostics())
return llvm::createStringError(llvm::errc::not_supported,
"Initialization failed. "
Expand Down
1 change: 1 addition & 0 deletions clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void ModelInjector::onBodySynthesis(const NamedDecl *D) {
CompilerInstance Instance(CI.getPCHContainerOperations());
Instance.setInvocation(std::move(Invocation));
Instance.createDiagnostics(
CI.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
/*ShouldOwnClient=*/true);

Expand Down
32 changes: 17 additions & 15 deletions clang/lib/Testing/TestAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class StoreDiagnostics : public DiagnosticConsumer {
// Provides "empty" ASTContext etc if we fail before parsing gets started.
void createMissingComponents(CompilerInstance &Clang) {
if (!Clang.hasDiagnostics())
Clang.createDiagnostics();
Clang.createDiagnostics(*llvm::vfs::getRealFileSystem());
if (!Clang.hasFileManager())
Clang.createFileManager();
if (!Clang.hasSourceManager())
Expand All @@ -82,9 +82,24 @@ TestAST::TestAST(const TestInputs &In) {
auto RecoverFromEarlyExit =
llvm::make_scope_exit([&] { createMissingComponents(*Clang); });

std::string Filename = In.FileName;
if (Filename.empty())
Filename = getFilenameForTesting(In.Language).str();

// Set up a VFS with only the virtual file visible.
auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
if (auto Err = VFS->setCurrentWorkingDirectory(In.WorkingDir))
ADD_FAILURE() << "Failed to setWD: " << Err.message();
VFS->addFile(Filename, /*ModificationTime=*/0,
llvm::MemoryBuffer::getMemBufferCopy(In.Code, Filename));
for (const auto &Extra : In.ExtraFiles)
VFS->addFile(
Extra.getKey(), /*ModificationTime=*/0,
llvm::MemoryBuffer::getMemBufferCopy(Extra.getValue(), Extra.getKey()));

// Extra error conditions are reported through diagnostics, set that up first.
bool ErrorOK = In.ErrorOK || llvm::StringRef(In.Code).contains("error-ok");
Clang->createDiagnostics(new StoreDiagnostics(Diagnostics, !ErrorOK));
Clang->createDiagnostics(*VFS, new StoreDiagnostics(Diagnostics, !ErrorOK));

// Parse cc1 argv, (typically [-std=c++20 input.cc]) into CompilerInvocation.
std::vector<const char *> Argv;
Expand All @@ -93,9 +108,6 @@ TestAST::TestAST(const TestInputs &In) {
Argv.push_back(S.c_str());
for (const auto &S : In.ExtraArgs)
Argv.push_back(S.c_str());
std::string Filename = In.FileName;
if (Filename.empty())
Filename = getFilenameForTesting(In.Language).str();
Argv.push_back(Filename.c_str());
Clang->setInvocation(std::make_unique<CompilerInvocation>());
if (!CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv,
Expand All @@ -105,16 +117,6 @@ TestAST::TestAST(const TestInputs &In) {
}
assert(!Clang->getInvocation().getFrontendOpts().DisableFree);

// Set up a VFS with only the virtual file visible.
auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
if (auto Err = VFS->setCurrentWorkingDirectory(In.WorkingDir))
ADD_FAILURE() << "Failed to setWD: " << Err.message();
VFS->addFile(Filename, /*ModificationTime=*/0,
llvm::MemoryBuffer::getMemBufferCopy(In.Code, Filename));
for (const auto &Extra : In.ExtraFiles)
VFS->addFile(
Extra.getKey(), /*ModificationTime=*/0,
llvm::MemoryBuffer::getMemBufferCopy(Extra.getValue(), Extra.getKey()));
Clang->createFileManager(VFS);

// Running the FrontendAction creates the other components: SourceManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ class DependencyScanningAction : public tooling::ToolAction {

// Create the compiler's actual diagnostics engine.
sanitizeDiagOpts(ScanInstance.getDiagnosticOpts());
ScanInstance.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
ScanInstance.createDiagnostics(DriverFileMgr->getVirtualFileSystem(),
DiagConsumer, /*ShouldOwnClient=*/false);
if (!ScanInstance.hasDiagnostics())
return false;

Expand Down Expand Up @@ -650,7 +651,7 @@ bool DependencyScanningWorker::computeDependencies(
auto DiagOpts = CreateAndPopulateDiagOpts(FinalCCommandLine);
sanitizeDiagOpts(*DiagOpts);
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
CompilerInstance::createDiagnostics(DiagOpts.release(), &DC,
CompilerInstance::createDiagnostics(*FinalFS, DiagOpts.release(), &DC,
/*ShouldOwnClient=*/false);

// Although `Diagnostics` are used only for command-line parsing, the
Expand Down
9 changes: 6 additions & 3 deletions clang/lib/Tooling/Tooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ bool ToolInvocation::run() {
TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), DiagOpts);
IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics =
CompilerInstance::createDiagnostics(
&*DiagOpts, DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);
Files->getVirtualFileSystem(), &*DiagOpts,
DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);
// Although `Diagnostics` are used only for command-line parsing, the custom
// `DiagConsumer` might expect a `SourceManager` to be present.
SourceManager SrcMgr(*Diagnostics, *Files);
Expand Down Expand Up @@ -456,7 +457,8 @@ bool FrontendActionFactory::runInvocation(
std::unique_ptr<FrontendAction> ScopedToolAction(create());

// Create the compiler's actual diagnostics engine.
Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
Compiler.createDiagnostics(Files->getVirtualFileSystem(), DiagConsumer,
/*ShouldOwnClient=*/false);
if (!Compiler.hasDiagnostics())
return false;

Expand Down Expand Up @@ -652,7 +654,8 @@ class ASTBuilderAction : public ToolAction {
DiagnosticConsumer *DiagConsumer) override {
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
Invocation, std::move(PCHContainerOps),
CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(),
CompilerInstance::createDiagnostics(Files->getVirtualFileSystem(),
&Invocation->getDiagnosticOpts(),
DiagConsumer,
/*ShouldOwnClient=*/false),
Files);
Expand Down
9 changes: 6 additions & 3 deletions clang/tools/c-index-test/core_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"

using namespace clang;
Expand Down Expand Up @@ -219,8 +220,9 @@ static bool printSourceSymbols(const char *Executable,
SmallVector<const char *, 4> ArgsWithProgName;
ArgsWithProgName.push_back(Executable);
ArgsWithProgName.append(Args.begin(), Args.end());
IntrusiveRefCntPtr<DiagnosticsEngine>
Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions));
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
new DiagnosticOptions));
CreateInvocationOptions CIOpts;
CIOpts.Diags = Diags;
CIOpts.ProbePrecompiled = true; // FIXME: historical default. Needed?
Expand Down Expand Up @@ -273,7 +275,8 @@ static bool printSourceSymbolsFromModule(StringRef modulePath,
auto HSOpts = std::make_shared<HeaderSearchOptions>();

IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
CompilerInstance::createDiagnostics(new DiagnosticOptions());
CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
new DiagnosticOptions());
std::unique_ptr<ASTUnit> AU =
ASTUnit::LoadFromASTFile(modulePath, *pchRdr, ASTUnit::LoadASTOnly, Diags,
FileSystemOpts, HSOpts, /*LangOpts=*/nullptr,
Expand Down
4 changes: 3 additions & 1 deletion clang/tools/clang-import-test/clang-import-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/TargetParser/Host.h"

#include <memory>
Expand Down Expand Up @@ -164,7 +165,8 @@ std::unique_ptr<CompilerInstance> BuildCompilerInstance() {
auto Ins = std::make_unique<CompilerInstance>();
auto DC = std::make_unique<TestDiagnosticConsumer>();
const bool ShouldOwnClient = true;
Ins->createDiagnostics(DC.release(), ShouldOwnClient);
Ins->createDiagnostics(*llvm::vfs::getRealFileSystem(), DC.release(),
ShouldOwnClient);

auto Inv = std::make_unique<CompilerInvocation>();

Expand Down
Loading
Loading