Skip to content
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
32 changes: 19 additions & 13 deletions clang/include/clang/CodeGen/ModuleBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,21 @@ namespace clang {
class HeaderSearchOptions;
class LangOptions;
class PreprocessorOptions;
class CompilerInstance;

namespace CodeGen {
class CodeGenModule;
class CGDebugInfo;
}

/// The primary public interface to the Clang code generator.
///
/// This is not really an abstract interface.
class CodeGenerator : public ASTConsumer {
virtual void anchor();

protected:
/// Use CreateLLVMCodeGen() below to create an instance of this class.
CodeGenerator() = default;

/// True if we've finished generating IR. This prevents us from generating
/// additional LLVM IR after emitting output in HandleTranslationUnit. This
/// can happen when Clang plugins trigger additional AST deserialization.
Expand All @@ -78,7 +80,7 @@ class CodeGenerator : public ASTConsumer {
///
/// It is illegal to call methods other than GetModule on the
/// CodeGenerator after releasing its module.
llvm::Module *ReleaseModule();
std::unique_ptr<llvm::Module> ReleaseModule();

/// Return debug info code generator.
CodeGen::CGDebugInfo *getCGDebugInfo();
Expand Down Expand Up @@ -109,16 +111,20 @@ class CodeGenerator : public ASTConsumer {
};

/// CreateLLVMCodeGen - Create a CodeGenerator instance.
/// It is the responsibility of the caller to call delete on
/// 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,
CoverageSourceInfo *CoverageInfo = nullptr);
///
/// Remember to call Initialize() if you plan to use this directly.
std::unique_ptr<CodeGenerator>
CreateLLVMCodeGen(const CompilerInstance &CI, llvm::StringRef ModuleName,
llvm::LLVMContext &C,
CoverageSourceInfo *CoverageInfo = nullptr);

std::unique_ptr<CodeGenerator>
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 = nullptr);

namespace CodeGen {
/// Demangle the artificial function name (\param FuncName) used to encode trap
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ BackendConsumer::BackendConsumer(CompilerInstance &CI, BackendAction Action,
: CI(CI), Diags(CI.getDiagnostics()), CodeGenOpts(CI.getCodeGenOpts()),
TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
AsmOutStream(std::move(OS)), FS(VFS), Action(Action),
Gen(CreateLLVMCodeGen(Diags, InFile, std::move(VFS),
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(),
CI.getCodeGenOpts(), C, CoverageInfo)),
Gen(CreateLLVMCodeGen(CI, InFile, C, CoverageInfo)),
LinkModules(std::move(LinkModules)), CurLinkModule(CurLinkModule) {
TimerIsEnabled = CodeGenOpts.TimePasses;
llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
Expand Down
33 changes: 21 additions & 12 deletions clang/lib/CodeGen/ModuleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "clang/Basic/CodeGenOptions.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInstance.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/LLVMContext.h"
Expand All @@ -31,7 +32,7 @@ using namespace clang;
using namespace CodeGen;

namespace {
class CodeGeneratorImpl : public CodeGenerator {
class CodeGeneratorImpl final : public CodeGenerator {
DiagnosticsEngine &Diags;
ASTContext *Ctx;
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS; // Only used for debug info.
Expand Down Expand Up @@ -60,12 +61,8 @@ namespace {
};

CoverageSourceInfo *CoverageInfo;

protected:
std::unique_ptr<llvm::Module> M;
std::unique_ptr<CodeGen::CodeGenModule> Builder;

private:
SmallVector<FunctionDecl *, 8> DeferredInlineMemberFuncDefs;

static llvm::StringRef ExpandModuleName(llvm::StringRef ModuleName,
Expand Down Expand Up @@ -107,8 +104,8 @@ namespace {
return Builder->getModuleDebugInfo();
}

llvm::Module *ReleaseModule() {
return M.release();
std::unique_ptr<llvm::Module> ReleaseModule() {
return std::exchange(M, nullptr);
}

const Decl *GetDeclForMangledName(StringRef MangledName) {
Expand Down Expand Up @@ -143,6 +140,7 @@ namespace {

std::unique_ptr<CodeGenModule> OldBuilder = std::move(Builder);

assert(Ctx && "must call Initialize() before calling StartModule()");
Initialize(*Ctx);

if (OldBuilder)
Expand Down Expand Up @@ -251,6 +249,7 @@ namespace {

// For MSVC compatibility, treat declarations of static data members with
// inline initializers as definitions.
assert(Ctx && "Initialize() not called");
if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()) {
for (Decl *Member : D->decls()) {
if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
Expand Down Expand Up @@ -341,7 +340,7 @@ llvm::Module *CodeGenerator::GetModule() {
return static_cast<CodeGeneratorImpl*>(this)->GetModule();
}

llvm::Module *CodeGenerator::ReleaseModule() {
std::unique_ptr<llvm::Module> CodeGenerator::ReleaseModule() {
return static_cast<CodeGeneratorImpl*>(this)->ReleaseModule();
}

Expand All @@ -368,16 +367,26 @@ llvm::Module *CodeGenerator::StartModule(llvm::StringRef ModuleName,
return static_cast<CodeGeneratorImpl*>(this)->StartModule(ModuleName, C);
}

CodeGenerator *
std::unique_ptr<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);
return std::make_unique<CodeGeneratorImpl>(Diags, ModuleName, std::move(FS),
HeaderSearchOpts, PreprocessorOpts,
CGO, C, CoverageInfo);
}

std::unique_ptr<CodeGenerator>
clang::CreateLLVMCodeGen(const CompilerInstance &CI, StringRef ModuleName,
llvm::LLVMContext &C,
CoverageSourceInfo *CoverageInfo) {
return CreateLLVMCodeGen(CI.getDiagnostics(), ModuleName,
CI.getVirtualFileSystemPtr(),
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(),
CI.getCodeGenOpts(), C, CoverageInfo);
}

namespace clang {
Expand Down
5 changes: 1 addition & 4 deletions clang/tools/clang-import-test/clang-import-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,7 @@ BuildASTContext(CompilerInstance &CI, SelectorTable &ST, Builtin::Context &BC) {
std::unique_ptr<CodeGenerator> BuildCodeGen(CompilerInstance &CI,
llvm::LLVMContext &LLVMCtx) {
StringRef ModuleName("$__module");
return std::unique_ptr<CodeGenerator>(CreateLLVMCodeGen(
CI.getDiagnostics(), ModuleName, CI.getVirtualFileSystemPtr(),
CI.getHeaderSearchOpts(), CI.getPreprocessorOpts(), CI.getCodeGenOpts(),
LLVMCtx));
return CreateLLVMCodeGen(CI, ModuleName, LLVMCtx);
}
} // namespace init_convenience

Expand Down
5 changes: 1 addition & 4 deletions clang/unittests/CodeGen/TestCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ struct TestCompiler {

compiler.createASTContext();

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

void init(const char *TestProgram,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,11 +874,8 @@ ClangExpressionParser::ClangExpressionParser(
std::string module_name("$__lldb_module");

m_llvm_context = std::make_unique<LLVMContext>();
m_code_generator.reset(CreateLLVMCodeGen(
m_compiler->getDiagnostics(), module_name,
m_compiler->getVirtualFileSystemPtr(), m_compiler->getHeaderSearchOpts(),
m_compiler->getPreprocessorOpts(), m_compiler->getCodeGenOpts(),
*m_llvm_context));
m_code_generator =
CreateLLVMCodeGen(*m_compiler, module_name, *m_llvm_context);
}

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