Skip to content

[CGDebugInfo] Access the current working directory from the VFS #5092

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
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
8 changes: 7 additions & 1 deletion clang/include/clang/CodeGen/ModuleBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
#define LLVM_CLANG_CODEGEN_MODULEBUILDER_H

#include "clang/AST/ASTConsumer.h"
#include "clang/Basic/LLVM.h"

namespace llvm {
class Constant;
class LLVMContext;
class Module;
class StringRef;

namespace vfs {
class FileSystem;
}
}

namespace clang {
Expand Down Expand Up @@ -98,10 +103,11 @@ class CodeGenerator : public ASTConsumer {
/// the allocated CodeGenerator instance.
CodeGenerator *CreateLLVMCodeGen(DiagnosticsEngine &Diags,
llvm::StringRef ModuleName,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const HeaderSearchOptions &HeaderSearchOpts,
const PreprocessorOptions &PreprocessorOpts,
const CodeGenOptions &CGO,
llvm::LLVMContext& C,
llvm::LLVMContext &C,
CoverageSourceInfo *CoverageInfo = nullptr);

} // end namespace clang
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,11 @@ StringRef CGDebugInfo::getCurrentDirname() {

if (!CWDName.empty())
return CWDName;
SmallString<256> CWD;
llvm::sys::fs::current_path(CWD);
return CWDName = internString(CWD);
llvm::ErrorOr<std::string> CWD =
CGM.getFileSystem()->getCurrentWorkingDirectory();
if (!CWD)
return StringRef();
return CWDName = internString(*CWD);
}

void CGDebugInfo::CreateCompileUnit() {
Expand Down
25 changes: 14 additions & 11 deletions clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ namespace clang {

public:
BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const HeaderSearchOptions &HeaderSearchOpts,
const PreprocessorOptions &PPOpts,
const CodeGenOptions &CodeGenOpts,
Expand All @@ -159,8 +160,8 @@ namespace clang {
AsmOutStream(std::move(OS)), Context(nullptr),
LLVMIRGeneration("irgen", "LLVM IR Generation Time"),
LLVMIRGenerationRefCount(0),
Gen(CreateLLVMCodeGen(Diags, InFile, HeaderSearchOpts, PPOpts,
CodeGenOpts, C, CoverageInfo)),
Gen(CreateLLVMCodeGen(Diags, InFile, std::move(FS), HeaderSearchOpts,
PPOpts, CodeGenOpts, C, CoverageInfo)),
LinkModules(std::move(LinkModules)) {
TimerIsEnabled = CodeGenOpts.TimePasses;
llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
Expand All @@ -171,6 +172,7 @@ namespace clang {
// to use the clang diagnostic handler for IR input files. It avoids
// initializing the OS field.
BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const HeaderSearchOptions &HeaderSearchOpts,
const PreprocessorOptions &PPOpts,
const CodeGenOptions &CodeGenOpts,
Expand All @@ -183,8 +185,8 @@ namespace clang {
Context(nullptr),
LLVMIRGeneration("irgen", "LLVM IR Generation Time"),
LLVMIRGenerationRefCount(0),
Gen(CreateLLVMCodeGen(Diags, "", HeaderSearchOpts, PPOpts,
CodeGenOpts, C, CoverageInfo)),
Gen(CreateLLVMCodeGen(Diags, "", std::move(FS), HeaderSearchOpts,
PPOpts, CodeGenOpts, C, CoverageInfo)),
LinkModules(std::move(LinkModules)), CurLinkModule(Module) {
TimerIsEnabled = CodeGenOpts.TimePasses;
llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
Expand Down Expand Up @@ -1053,10 +1055,10 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
CI.getPreprocessor());

std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(),
CI.getPreprocessorOpts(), CI.getCodeGenOpts(), CI.getTargetOpts(),
CI.getLangOpts(), std::string(InFile), std::move(LinkModules),
std::move(OS), *VMContext, CoverageInfo));
BA, CI.getDiagnostics(), &CI.getVirtualFileSystem(),
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(), CI.getCodeGenOpts(),
CI.getTargetOpts(), CI.getLangOpts(), std::string(InFile),
std::move(LinkModules), std::move(OS), *VMContext, CoverageInfo));
BEConsumer = Result.get();

// Enable generating macro debug info only when debug info is not disabled and
Expand Down Expand Up @@ -1186,9 +1188,10 @@ void CodeGenAction::ExecuteAction() {

// Set clang diagnostic handler. To do this we need to create a fake
// BackendConsumer.
BackendConsumer Result(BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(),
CI.getPreprocessorOpts(), CI.getCodeGenOpts(),
CI.getTargetOpts(), CI.getLangOpts(), TheModule.get(),
BackendConsumer Result(BA, CI.getDiagnostics(), &CI.getVirtualFileSystem(),
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(),
CI.getCodeGenOpts(), CI.getTargetOpts(),
CI.getLangOpts(), TheModule.get(),
std::move(LinkModules), *VMContext, nullptr);
// PR44896: Force DiscardValueNames as false. DiscardValueNames cannot be
// true here because the valued names are needed for reading textual IR.
Expand Down
14 changes: 8 additions & 6 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,18 @@ static CGCXXABI *createCXXABI(CodeGenModule &CGM) {
llvm_unreachable("invalid C++ ABI kind");
}

CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
CodeGenModule::CodeGenModule(ASTContext &C,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const HeaderSearchOptions &HSO,
const PreprocessorOptions &PPO,
const CodeGenOptions &CGO, llvm::Module &M,
DiagnosticsEngine &diags,
CoverageSourceInfo *CoverageInfo)
: Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO),
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
VMContext(M.getContext()), Types(*this), VTables(*this),
SanitizerMD(new SanitizerMetadata(*this)) {
: Context(C), LangOpts(C.getLangOpts()), FS(std::move(FS)),
HeaderSearchOpts(HSO), PreprocessorOpts(PPO), CodeGenOpts(CGO),
TheModule(M), Diags(diags), Target(C.getTargetInfo()),
ABI(createCXXABI(*this)), VMContext(M.getContext()), Types(*this),
VTables(*this), SanitizerMD(new SanitizerMetadata(*this)) {

// Initialize the type cache.
llvm::LLVMContext &LLVMContext = M.getContext();
Expand Down
11 changes: 10 additions & 1 deletion clang/lib/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class DataLayout;
class FunctionType;
class LLVMContext;
class IndexedInstrProfReader;

namespace vfs {
class FileSystem;
}
}

namespace clang {
Expand Down Expand Up @@ -293,6 +297,7 @@ class CodeGenModule : public CodeGenTypeCache {
private:
ASTContext &Context;
const LangOptions &LangOpts;
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS; // Only used for debug info.
const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
const CodeGenOptions &CodeGenOpts;
Expand Down Expand Up @@ -573,7 +578,8 @@ class CodeGenModule : public CodeGenTypeCache {
llvm::DenseMap<GlobalDecl, uint16_t> PtrAuthDiscriminatorHashes;

public:
CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts,
CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const HeaderSearchOptions &headersearchopts,
const PreprocessorOptions &ppopts,
const CodeGenOptions &CodeGenOpts, llvm::Module &M,
DiagnosticsEngine &Diags,
Expand Down Expand Up @@ -695,6 +701,9 @@ class CodeGenModule : public CodeGenTypeCache {

ASTContext &getContext() const { return Context; }
const LangOptions &getLangOpts() const { return LangOpts; }
const IntrusiveRefCntPtr<llvm::vfs::FileSystem> &getFileSystem() const {
return FS;
}
const HeaderSearchOptions &getHeaderSearchOpts()
const { return HeaderSearchOpts; }
const PreprocessorOptions &getPreprocessorOpts()
Expand Down
24 changes: 15 additions & 9 deletions clang/lib/CodeGen/ModuleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <memory>

using namespace clang;
Expand All @@ -32,6 +33,7 @@ namespace {
class CodeGeneratorImpl : public CodeGenerator {
DiagnosticsEngine &Diags;
ASTContext *Ctx;
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS; // Only used for debug info.
const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
const CodeGenOptions CodeGenOpts; // Intentionally copied in.
Expand Down Expand Up @@ -74,11 +76,12 @@ namespace {

public:
CodeGeneratorImpl(DiagnosticsEngine &diags, llvm::StringRef ModuleName,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const HeaderSearchOptions &HSO,
const PreprocessorOptions &PPO, const CodeGenOptions &CGO,
llvm::LLVMContext &C,
CoverageSourceInfo *CoverageInfo = nullptr)
: Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
: Diags(diags), Ctx(nullptr), FS(std::move(FS)), HeaderSearchOpts(HSO),
PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
CoverageInfo(CoverageInfo),
M(new llvm::Module(ExpandModuleName(ModuleName, CGO), C)) {
Expand Down Expand Up @@ -151,7 +154,7 @@ namespace {
if (auto TVSDKVersion =
Ctx->getTargetInfo().getDarwinTargetVariantSDKVersion())
M->setDarwinTargetVariantSDKVersion(*TVSDKVersion);
Builder.reset(new CodeGen::CodeGenModule(Context, HeaderSearchOpts,
Builder.reset(new CodeGen::CodeGenModule(Context, FS, HeaderSearchOpts,
PreprocessorOpts, CodeGenOpts,
*M, Diags, CoverageInfo));

Expand Down Expand Up @@ -349,11 +352,14 @@ llvm::Module *CodeGenerator::StartModule(llvm::StringRef ModuleName,
return static_cast<CodeGeneratorImpl*>(this)->StartModule(ModuleName, C);
}

CodeGenerator *clang::CreateLLVMCodeGen(
DiagnosticsEngine &Diags, llvm::StringRef ModuleName,
const HeaderSearchOptions &HeaderSearchOpts,
const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO,
llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo) {
return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
PreprocessorOpts, CGO, C, CoverageInfo);
CodeGenerator *
clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags, llvm::StringRef ModuleName,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const HeaderSearchOptions &HeaderSearchOpts,
const PreprocessorOptions &PreprocessorOpts,
const CodeGenOptions &CGO, llvm::LLVMContext &C,
CoverageSourceInfo *CoverageInfo) {
return new CodeGeneratorImpl(Diags, ModuleName, std::move(FS),
HeaderSearchOpts, PreprocessorOpts, CGO, C,
CoverageInfo);
}
4 changes: 3 additions & 1 deletion clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class PCHContainerGenerator : public ASTConsumer {
const std::string OutputFileName;
ASTContext *Ctx;
ModuleMap &MMap;
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
const HeaderSearchOptions &HeaderSearchOpts;
const PreprocessorOptions &PreprocessorOpts;
CodeGenOptions CodeGenOpts;
Expand Down Expand Up @@ -144,6 +145,7 @@ class PCHContainerGenerator : public ASTConsumer {
: Diags(CI.getDiagnostics()), MainFileName(MainFileName),
OutputFileName(OutputFileName), Ctx(nullptr),
MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
FS(&CI.getVirtualFileSystem()),
HeaderSearchOpts(CI.getHeaderSearchOpts()),
PreprocessorOpts(CI.getPreprocessorOpts()),
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
Expand Down Expand Up @@ -173,7 +175,7 @@ class PCHContainerGenerator : public ASTConsumer {
M.reset(new llvm::Module(MainFileName, *VMContext));
M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
Builder.reset(new CodeGen::CodeGenModule(
*Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
*Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));

// Prepare CGDebugInfo to emit debug info for a clang module.
auto *DI = Builder->getModuleDebugInfo();
Expand Down
5 changes: 3 additions & 2 deletions clang/tools/clang-import-test/clang-import-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ std::unique_ptr<CodeGenerator> BuildCodeGen(CompilerInstance &CI,
llvm::LLVMContext &LLVMCtx) {
StringRef ModuleName("$__module");
return std::unique_ptr<CodeGenerator>(CreateLLVMCodeGen(
CI.getDiagnostics(), ModuleName, CI.getHeaderSearchOpts(),
CI.getPreprocessorOpts(), CI.getCodeGenOpts(), LLVMCtx));
CI.getDiagnostics(), ModuleName, &CI.getVirtualFileSystem(),
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(), CI.getCodeGenOpts(),
LLVMCtx));
}
} // namespace init_convenience

Expand Down
10 changes: 4 additions & 6 deletions clang/unittests/CodeGen/TestCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ struct TestCompiler {

compiler.createASTContext();

CG.reset(CreateLLVMCodeGen(compiler.getDiagnostics(),
"main-module",
compiler.getHeaderSearchOpts(),
compiler.getPreprocessorOpts(),
compiler.getCodeGenOpts(),
Context));
CG.reset(CreateLLVMCodeGen(
compiler.getDiagnostics(), "main-module",
&compiler.getVirtualFileSystem(), compiler.getHeaderSearchOpts(),
compiler.getPreprocessorOpts(), compiler.getCodeGenOpts(), Context));
}

void init(const char *TestProgram,
Expand Down
37 changes: 37 additions & 0 deletions clang/unittests/Frontend/CodeGenActionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "clang/CodeGen/BackendUtil.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "llvm/Support/FormatVariadic.h"
#include "gtest/gtest.h"

using namespace llvm;
Expand Down Expand Up @@ -76,4 +77,40 @@ TEST(CodeGenTest, CodeGenFromIRMemBuffer) {
bool Success = Compiler.ExecuteAction(Action);
EXPECT_TRUE(Success);
}

TEST(CodeGenTest, DebugInfoCWDCodeGen) {
// Check that debug info is accessing the current working directory from the
// VFS instead of calling \p llvm::sys::fs::current_path() directly.

auto VFS = std::make_unique<llvm::vfs::InMemoryFileSystem>();
VFS->setCurrentWorkingDirectory("/in-memory-fs-cwd");
auto Sept = llvm::sys::path::get_separator();
std::string TestPath =
std::string(llvm::formatv("{0}in-memory-fs-cwd{0}test.cpp", Sept));
VFS->addFile(TestPath, 0, llvm::MemoryBuffer::getMemBuffer("int x;\n"));

auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile("test.cpp", Language::CXX));
Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
Invocation->getTargetOpts().Triple = "x86_64-unknown-linux-gnu";
Invocation->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo);
CompilerInstance Compiler;

SmallString<256> IRBuffer;
Compiler.setOutputStream(std::make_unique<raw_svector_ostream>(IRBuffer));
Compiler.setInvocation(std::move(Invocation));
Compiler.createDiagnostics();
Compiler.createFileManager(std::move(VFS));

EmitLLVMAction Action;
bool Success = Compiler.ExecuteAction(Action);
EXPECT_TRUE(Success);

SmallString<128> RealCWD;
llvm::sys::fs::current_path(RealCWD);
EXPECT_TRUE(!RealCWD.empty());
EXPECT_FALSE(IRBuffer.str().contains(RealCWD));
EXPECT_TRUE(IRBuffer.str().contains("in-memory-fs-cwd"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,9 @@ ClangExpressionParser::ClangExpressionParser(
m_llvm_context = std::make_unique<LLVMContext>();
m_code_generator.reset(CreateLLVMCodeGen(
m_compiler->getDiagnostics(), module_name,
m_compiler->getHeaderSearchOpts(), m_compiler->getPreprocessorOpts(),
m_compiler->getCodeGenOpts(), *m_llvm_context));
&m_compiler->getVirtualFileSystem(), m_compiler->getHeaderSearchOpts(),
m_compiler->getPreprocessorOpts(), m_compiler->getCodeGenOpts(),
*m_llvm_context));
}

ClangExpressionParser::~ClangExpressionParser() = default;
Expand Down