Skip to content

Commit 8230eaa

Browse files
committed
[CGDebugInfo] Access the current working directory from the VFS
...instead of calling `llvm::sys::fs::current_path()` directly. Differential Revision: https://reviews.llvm.org/D130443
1 parent 110d422 commit 8230eaa

File tree

11 files changed

+109
-42
lines changed

11 files changed

+109
-42
lines changed

clang/include/clang/CodeGen/ModuleBuilder.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
#define LLVM_CLANG_CODEGEN_MODULEBUILDER_H
1515

1616
#include "clang/AST/ASTConsumer.h"
17+
#include "clang/Basic/LLVM.h"
1718

1819
namespace llvm {
1920
class Constant;
2021
class LLVMContext;
2122
class Module;
2223
class StringRef;
24+
25+
namespace vfs {
26+
class FileSystem;
27+
}
2328
}
2429

2530
namespace clang {
@@ -98,10 +103,11 @@ class CodeGenerator : public ASTConsumer {
98103
/// the allocated CodeGenerator instance.
99104
CodeGenerator *CreateLLVMCodeGen(DiagnosticsEngine &Diags,
100105
llvm::StringRef ModuleName,
106+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
101107
const HeaderSearchOptions &HeaderSearchOpts,
102108
const PreprocessorOptions &PreprocessorOpts,
103109
const CodeGenOptions &CGO,
104-
llvm::LLVMContext& C,
110+
llvm::LLVMContext &C,
105111
CoverageSourceInfo *CoverageInfo = nullptr);
106112

107113
} // end namespace clang

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,11 @@ StringRef CGDebugInfo::getCurrentDirname() {
491491

492492
if (!CWDName.empty())
493493
return CWDName;
494-
SmallString<256> CWD;
495-
llvm::sys::fs::current_path(CWD);
496-
return CWDName = internString(CWD);
494+
llvm::ErrorOr<std::string> CWD =
495+
CGM.getFileSystem()->getCurrentWorkingDirectory();
496+
if (!CWD)
497+
return StringRef();
498+
return CWDName = internString(*CWD);
497499
}
498500

499501
void CGDebugInfo::CreateCompileUnit() {

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ namespace clang {
146146

147147
public:
148148
BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags,
149+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
149150
const HeaderSearchOptions &HeaderSearchOpts,
150151
const PreprocessorOptions &PPOpts,
151152
const CodeGenOptions &CodeGenOpts,
@@ -159,8 +160,8 @@ namespace clang {
159160
AsmOutStream(std::move(OS)), Context(nullptr),
160161
LLVMIRGeneration("irgen", "LLVM IR Generation Time"),
161162
LLVMIRGenerationRefCount(0),
162-
Gen(CreateLLVMCodeGen(Diags, InFile, HeaderSearchOpts, PPOpts,
163-
CodeGenOpts, C, CoverageInfo)),
163+
Gen(CreateLLVMCodeGen(Diags, InFile, std::move(FS), HeaderSearchOpts,
164+
PPOpts, CodeGenOpts, C, CoverageInfo)),
164165
LinkModules(std::move(LinkModules)) {
165166
TimerIsEnabled = CodeGenOpts.TimePasses;
166167
llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
@@ -171,6 +172,7 @@ namespace clang {
171172
// to use the clang diagnostic handler for IR input files. It avoids
172173
// initializing the OS field.
173174
BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags,
175+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
174176
const HeaderSearchOptions &HeaderSearchOpts,
175177
const PreprocessorOptions &PPOpts,
176178
const CodeGenOptions &CodeGenOpts,
@@ -183,8 +185,8 @@ namespace clang {
183185
Context(nullptr),
184186
LLVMIRGeneration("irgen", "LLVM IR Generation Time"),
185187
LLVMIRGenerationRefCount(0),
186-
Gen(CreateLLVMCodeGen(Diags, "", HeaderSearchOpts, PPOpts,
187-
CodeGenOpts, C, CoverageInfo)),
188+
Gen(CreateLLVMCodeGen(Diags, "", std::move(FS), HeaderSearchOpts,
189+
PPOpts, CodeGenOpts, C, CoverageInfo)),
188190
LinkModules(std::move(LinkModules)), CurLinkModule(Module) {
189191
TimerIsEnabled = CodeGenOpts.TimePasses;
190192
llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
@@ -1053,10 +1055,10 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
10531055
CI.getPreprocessor());
10541056

10551057
std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
1056-
BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(),
1057-
CI.getPreprocessorOpts(), CI.getCodeGenOpts(), CI.getTargetOpts(),
1058-
CI.getLangOpts(), std::string(InFile), std::move(LinkModules),
1059-
std::move(OS), *VMContext, CoverageInfo));
1058+
BA, CI.getDiagnostics(), &CI.getVirtualFileSystem(),
1059+
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(), CI.getCodeGenOpts(),
1060+
CI.getTargetOpts(), CI.getLangOpts(), std::string(InFile),
1061+
std::move(LinkModules), std::move(OS), *VMContext, CoverageInfo));
10601062
BEConsumer = Result.get();
10611063

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

11871189
// Set clang diagnostic handler. To do this we need to create a fake
11881190
// BackendConsumer.
1189-
BackendConsumer Result(BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(),
1190-
CI.getPreprocessorOpts(), CI.getCodeGenOpts(),
1191-
CI.getTargetOpts(), CI.getLangOpts(), TheModule.get(),
1191+
BackendConsumer Result(BA, CI.getDiagnostics(), &CI.getVirtualFileSystem(),
1192+
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(),
1193+
CI.getCodeGenOpts(), CI.getTargetOpts(),
1194+
CI.getLangOpts(), TheModule.get(),
11921195
std::move(LinkModules), *VMContext, nullptr);
11931196
// PR44896: Force DiscardValueNames as false. DiscardValueNames cannot be
11941197
// true here because the valued names are needed for reading textual IR.

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,18 @@ static CGCXXABI *createCXXABI(CodeGenModule &CGM) {
9595
llvm_unreachable("invalid C++ ABI kind");
9696
}
9797

98-
CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
98+
CodeGenModule::CodeGenModule(ASTContext &C,
99+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
100+
const HeaderSearchOptions &HSO,
99101
const PreprocessorOptions &PPO,
100102
const CodeGenOptions &CGO, llvm::Module &M,
101103
DiagnosticsEngine &diags,
102104
CoverageSourceInfo *CoverageInfo)
103-
: Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO),
104-
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
105-
Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
106-
VMContext(M.getContext()), Types(*this), VTables(*this),
107-
SanitizerMD(new SanitizerMetadata(*this)) {
105+
: Context(C), LangOpts(C.getLangOpts()), FS(std::move(FS)),
106+
HeaderSearchOpts(HSO), PreprocessorOpts(PPO), CodeGenOpts(CGO),
107+
TheModule(M), Diags(diags), Target(C.getTargetInfo()),
108+
ABI(createCXXABI(*this)), VMContext(M.getContext()), Types(*this),
109+
VTables(*this), SanitizerMD(new SanitizerMetadata(*this)) {
108110

109111
// Initialize the type cache.
110112
llvm::LLVMContext &LLVMContext = M.getContext();

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class DataLayout;
4747
class FunctionType;
4848
class LLVMContext;
4949
class IndexedInstrProfReader;
50+
51+
namespace vfs {
52+
class FileSystem;
53+
}
5054
}
5155

5256
namespace clang {
@@ -293,6 +297,7 @@ class CodeGenModule : public CodeGenTypeCache {
293297
private:
294298
ASTContext &Context;
295299
const LangOptions &LangOpts;
300+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS; // Only used for debug info.
296301
const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
297302
const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
298303
const CodeGenOptions &CodeGenOpts;
@@ -573,7 +578,8 @@ class CodeGenModule : public CodeGenTypeCache {
573578
llvm::DenseMap<GlobalDecl, uint16_t> PtrAuthDiscriminatorHashes;
574579

575580
public:
576-
CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts,
581+
CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
582+
const HeaderSearchOptions &headersearchopts,
577583
const PreprocessorOptions &ppopts,
578584
const CodeGenOptions &CodeGenOpts, llvm::Module &M,
579585
DiagnosticsEngine &Diags,
@@ -695,6 +701,9 @@ class CodeGenModule : public CodeGenTypeCache {
695701

696702
ASTContext &getContext() const { return Context; }
697703
const LangOptions &getLangOpts() const { return LangOpts; }
704+
const IntrusiveRefCntPtr<llvm::vfs::FileSystem> &getFileSystem() const {
705+
return FS;
706+
}
698707
const HeaderSearchOptions &getHeaderSearchOpts()
699708
const { return HeaderSearchOpts; }
700709
const PreprocessorOptions &getPreprocessorOpts()

clang/lib/CodeGen/ModuleBuilder.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/IR/DataLayout.h"
2424
#include "llvm/IR/LLVMContext.h"
2525
#include "llvm/IR/Module.h"
26+
#include "llvm/Support/VirtualFileSystem.h"
2627
#include <memory>
2728

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

7577
public:
7678
CodeGeneratorImpl(DiagnosticsEngine &diags, llvm::StringRef ModuleName,
79+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
7780
const HeaderSearchOptions &HSO,
7881
const PreprocessorOptions &PPO, const CodeGenOptions &CGO,
7982
llvm::LLVMContext &C,
8083
CoverageSourceInfo *CoverageInfo = nullptr)
81-
: Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
84+
: Diags(diags), Ctx(nullptr), FS(std::move(FS)), HeaderSearchOpts(HSO),
8285
PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
8386
CoverageInfo(CoverageInfo),
8487
M(new llvm::Module(ExpandModuleName(ModuleName, CGO), C)) {
@@ -151,7 +154,7 @@ namespace {
151154
if (auto TVSDKVersion =
152155
Ctx->getTargetInfo().getDarwinTargetVariantSDKVersion())
153156
M->setDarwinTargetVariantSDKVersion(*TVSDKVersion);
154-
Builder.reset(new CodeGen::CodeGenModule(Context, HeaderSearchOpts,
157+
Builder.reset(new CodeGen::CodeGenModule(Context, FS, HeaderSearchOpts,
155158
PreprocessorOpts, CodeGenOpts,
156159
*M, Diags, CoverageInfo));
157160

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

352-
CodeGenerator *clang::CreateLLVMCodeGen(
353-
DiagnosticsEngine &Diags, llvm::StringRef ModuleName,
354-
const HeaderSearchOptions &HeaderSearchOpts,
355-
const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO,
356-
llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo) {
357-
return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
358-
PreprocessorOpts, CGO, C, CoverageInfo);
355+
CodeGenerator *
356+
clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags, llvm::StringRef ModuleName,
357+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
358+
const HeaderSearchOptions &HeaderSearchOpts,
359+
const PreprocessorOptions &PreprocessorOpts,
360+
const CodeGenOptions &CGO, llvm::LLVMContext &C,
361+
CoverageSourceInfo *CoverageInfo) {
362+
return new CodeGeneratorImpl(Diags, ModuleName, std::move(FS),
363+
HeaderSearchOpts, PreprocessorOpts, CGO, C,
364+
CoverageInfo);
359365
}

clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class PCHContainerGenerator : public ASTConsumer {
4545
const std::string OutputFileName;
4646
ASTContext *Ctx;
4747
ModuleMap &MMap;
48+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
4849
const HeaderSearchOptions &HeaderSearchOpts;
4950
const PreprocessorOptions &PreprocessorOpts;
5051
CodeGenOptions CodeGenOpts;
@@ -144,6 +145,7 @@ class PCHContainerGenerator : public ASTConsumer {
144145
: Diags(CI.getDiagnostics()), MainFileName(MainFileName),
145146
OutputFileName(OutputFileName), Ctx(nullptr),
146147
MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
148+
FS(&CI.getVirtualFileSystem()),
147149
HeaderSearchOpts(CI.getHeaderSearchOpts()),
148150
PreprocessorOpts(CI.getPreprocessorOpts()),
149151
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
@@ -173,7 +175,7 @@ class PCHContainerGenerator : public ASTConsumer {
173175
M.reset(new llvm::Module(MainFileName, *VMContext));
174176
M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
175177
Builder.reset(new CodeGen::CodeGenModule(
176-
*Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
178+
*Ctx, FS, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
177179

178180
// Prepare CGDebugInfo to emit debug info for a clang module.
179181
auto *DI = Builder->getModuleDebugInfo();

clang/tools/clang-import-test/clang-import-test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,9 @@ std::unique_ptr<CodeGenerator> BuildCodeGen(CompilerInstance &CI,
230230
llvm::LLVMContext &LLVMCtx) {
231231
StringRef ModuleName("$__module");
232232
return std::unique_ptr<CodeGenerator>(CreateLLVMCodeGen(
233-
CI.getDiagnostics(), ModuleName, CI.getHeaderSearchOpts(),
234-
CI.getPreprocessorOpts(), CI.getCodeGenOpts(), LLVMCtx));
233+
CI.getDiagnostics(), ModuleName, &CI.getVirtualFileSystem(),
234+
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(), CI.getCodeGenOpts(),
235+
LLVMCtx));
235236
}
236237
} // namespace init_convenience
237238

clang/unittests/CodeGen/TestCompiler.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ struct TestCompiler {
5656

5757
compiler.createASTContext();
5858

59-
CG.reset(CreateLLVMCodeGen(compiler.getDiagnostics(),
60-
"main-module",
61-
compiler.getHeaderSearchOpts(),
62-
compiler.getPreprocessorOpts(),
63-
compiler.getCodeGenOpts(),
64-
Context));
59+
CG.reset(CreateLLVMCodeGen(
60+
compiler.getDiagnostics(), "main-module",
61+
&compiler.getVirtualFileSystem(), compiler.getHeaderSearchOpts(),
62+
compiler.getPreprocessorOpts(), compiler.getCodeGenOpts(), Context));
6563
}
6664

6765
void init(const char *TestProgram,

clang/unittests/Frontend/CodeGenActionTest.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "clang/CodeGen/BackendUtil.h"
1616
#include "clang/Frontend/CompilerInstance.h"
1717
#include "clang/Lex/PreprocessorOptions.h"
18+
#include "llvm/Support/FormatVariadic.h"
1819
#include "gtest/gtest.h"
1920

2021
using namespace llvm;
@@ -76,4 +77,40 @@ TEST(CodeGenTest, CodeGenFromIRMemBuffer) {
7677
bool Success = Compiler.ExecuteAction(Action);
7778
EXPECT_TRUE(Success);
7879
}
80+
81+
TEST(CodeGenTest, DebugInfoCWDCodeGen) {
82+
// Check that debug info is accessing the current working directory from the
83+
// VFS instead of calling \p llvm::sys::fs::current_path() directly.
84+
85+
auto VFS = std::make_unique<llvm::vfs::InMemoryFileSystem>();
86+
VFS->setCurrentWorkingDirectory("/in-memory-fs-cwd");
87+
auto Sept = llvm::sys::path::get_separator();
88+
std::string TestPath =
89+
std::string(llvm::formatv("{0}in-memory-fs-cwd{0}test.cpp", Sept));
90+
VFS->addFile(TestPath, 0, llvm::MemoryBuffer::getMemBuffer("int x;\n"));
91+
92+
auto Invocation = std::make_shared<CompilerInvocation>();
93+
Invocation->getFrontendOpts().Inputs.push_back(
94+
FrontendInputFile("test.cpp", Language::CXX));
95+
Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
96+
Invocation->getTargetOpts().Triple = "x86_64-unknown-linux-gnu";
97+
Invocation->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo);
98+
CompilerInstance Compiler;
99+
100+
SmallString<256> IRBuffer;
101+
Compiler.setOutputStream(std::make_unique<raw_svector_ostream>(IRBuffer));
102+
Compiler.setInvocation(std::move(Invocation));
103+
Compiler.createDiagnostics();
104+
Compiler.createFileManager(std::move(VFS));
105+
106+
EmitLLVMAction Action;
107+
bool Success = Compiler.ExecuteAction(Action);
108+
EXPECT_TRUE(Success);
109+
110+
SmallString<128> RealCWD;
111+
llvm::sys::fs::current_path(RealCWD);
112+
EXPECT_TRUE(!RealCWD.empty());
113+
EXPECT_FALSE(IRBuffer.str().contains(RealCWD));
114+
EXPECT_TRUE(IRBuffer.str().contains("in-memory-fs-cwd"));
115+
}
79116
}

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,9 @@ ClangExpressionParser::ClangExpressionParser(
722722
m_llvm_context = std::make_unique<LLVMContext>();
723723
m_code_generator.reset(CreateLLVMCodeGen(
724724
m_compiler->getDiagnostics(), module_name,
725-
m_compiler->getHeaderSearchOpts(), m_compiler->getPreprocessorOpts(),
726-
m_compiler->getCodeGenOpts(), *m_llvm_context));
725+
&m_compiler->getVirtualFileSystem(), m_compiler->getHeaderSearchOpts(),
726+
m_compiler->getPreprocessorOpts(), m_compiler->getCodeGenOpts(),
727+
*m_llvm_context));
727728
}
728729

729730
ClangExpressionParser::~ClangExpressionParser() = default;

0 commit comments

Comments
 (0)