forked from llvm-mirror/clang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap clang module files in a Mach-O, ELF, or COFF container.
This is a necessary prerequisite for debugging with modules. The .pcm files become containers that hold the serialized AST which allows us to store debug information in the module file that can be shared by all object files that were built importing the module. This reapplies r230044 with a fixed configure+make build and updated dependencies and testcase requirements. Over the last iteration this version adds - missing target requirements for testcases that specify an x86 triple, - a missing clangCodeGen.a dependency to libClang.a in the make build. rdar://problem/19104245 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@230423 91177308-0d34-0410-b5e6-96231b3b80d8
- Loading branch information
1 parent
872f3b0
commit 407c31d
Showing
83 changed files
with
456 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===--- CodeGen/ModuleContainerGenerator.h - Emit .pcm files ---*- C++ -*-===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_CODEGEN_MODULE_CONTAINER_H | ||
#define LLVM_CLANG_CODEGEN_MODULE_CONTAINER_H | ||
|
||
#include "ModuleBuilder.h" | ||
#include <string> | ||
|
||
namespace llvm { | ||
class raw_ostream; | ||
} | ||
|
||
namespace clang { | ||
|
||
class PCHGenerator; | ||
class TargetOptions; | ||
|
||
/// \brief Create a CodeGenerator instance. | ||
/// It is the responsibility of the caller to call delete on | ||
/// the allocated CodeGenerator instance. | ||
CodeGenerator *CreateModuleContainerGenerator( | ||
DiagnosticsEngine &Diags, const std::string &ModuleName, | ||
const CodeGenOptions &CGO, const TargetOptions &TO, const LangOptions &LO, | ||
llvm::raw_ostream *OS, PCHGenerator *PCHGen); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
//===--- CodeGenModuleContainer.cpp - Emit .pcm files ---------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/CodeGen/CodeGenModuleContainer.h" | ||
#include "CodeGenModule.h" | ||
#include "clang/AST/ASTContext.h" | ||
#include "clang/AST/DeclObjC.h" | ||
#include "clang/AST/Expr.h" | ||
#include "clang/AST/RecursiveASTVisitor.h" | ||
#include "clang/Basic/Diagnostic.h" | ||
#include "clang/Basic/TargetInfo.h" | ||
#include "clang/CodeGen/BackendUtil.h" | ||
#include "clang/Frontend/CodeGenOptions.h" | ||
#include "clang/Serialization/ASTWriter.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/IR/Constants.h" | ||
#include "llvm/IR/DataLayout.h" | ||
#include "llvm/IR/LLVMContext.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/Support/TargetRegistry.h" | ||
#include <memory> | ||
using namespace clang; | ||
|
||
namespace { | ||
class ModuleContainerGenerator : public CodeGenerator { | ||
DiagnosticsEngine &Diags; | ||
std::unique_ptr<const llvm::DataLayout> TD; | ||
ASTContext *Ctx; | ||
const CodeGenOptions CodeGenOpts; | ||
const TargetOptions TargetOpts; | ||
const LangOptions LangOpts; | ||
llvm::LLVMContext VMContext; | ||
std::unique_ptr<llvm::Module> M; | ||
std::unique_ptr<CodeGen::CodeGenModule> Builder; | ||
raw_ostream *OS; | ||
SmallVectorImpl<char> *SerializedASTBuffer; | ||
|
||
public: | ||
ModuleContainerGenerator(DiagnosticsEngine &diags, | ||
const std::string &ModuleName, | ||
const CodeGenOptions &CGO, const TargetOptions &TO, | ||
const LangOptions &LO, raw_ostream *OS, | ||
PCHGenerator *PCHGen) | ||
: Diags(diags), CodeGenOpts(CGO), TargetOpts(TO), LangOpts(LO), | ||
M(new llvm::Module(ModuleName, VMContext)), OS(OS) { | ||
PCHGen->RegisterSerializationFinishedCallback( | ||
[&](SmallVectorImpl<char> *Buf){ | ||
SerializedASTBuffer = Buf; | ||
}); | ||
} | ||
|
||
virtual ~ModuleContainerGenerator() {} | ||
llvm::Module *GetModule() override { return M.get(); } | ||
llvm::Module *ReleaseModule() override { return M.release(); } | ||
|
||
/// Lifted from ModuleBuilder. | ||
const Decl *GetDeclForMangledName(StringRef MangledName) override { | ||
GlobalDecl Result; | ||
if (!Builder->lookupRepresentativeDecl(MangledName, Result)) | ||
return nullptr; | ||
const Decl *D = Result.getCanonicalDecl().getDecl(); | ||
if (auto FD = dyn_cast<FunctionDecl>(D)) { | ||
if (FD->hasBody(FD)) | ||
return FD; | ||
} else if (auto TD = dyn_cast<TagDecl>(D)) { | ||
if (auto Def = TD->getDefinition()) | ||
return Def; | ||
} | ||
return D; | ||
} | ||
|
||
void Initialize(ASTContext &Context) override { | ||
Ctx = &Context; | ||
M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple()); | ||
M->setDataLayout(Ctx->getTargetInfo().getTargetDescription()); | ||
TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription())); | ||
Builder.reset( | ||
new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD, Diags)); | ||
} | ||
|
||
/// Emit a container holding the serialized AST. | ||
void HandleTranslationUnit(ASTContext &Ctx) override { | ||
if (Diags.hasErrorOccurred()) { | ||
if (Builder) | ||
Builder->clear(); | ||
M.reset(); | ||
return; | ||
} | ||
|
||
// Finalize the Builder. | ||
if (Builder) | ||
Builder->Release(); | ||
|
||
// Initialize the backend if we haven't done so already. | ||
LLVMInitializeAllTargetInfos(); | ||
LLVMInitializeAllTargets(); | ||
LLVMInitializeAllAsmPrinters(); | ||
LLVMInitializeAllTargetMCs(); | ||
|
||
// Ensure the target exists. | ||
std::string Error; | ||
auto Triple = Ctx.getTargetInfo().getTriple(); | ||
if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error)) | ||
llvm::report_fatal_error(Error); | ||
|
||
// Emit the serialized Clang AST into its own section. | ||
auto Size = SerializedASTBuffer->size(); | ||
auto Int8Ty = llvm::Type::getInt8Ty(VMContext); | ||
auto *Ty = llvm::ArrayType::get(Int8Ty, Size); | ||
auto *Data = llvm::ConstantDataArray::getString(VMContext, | ||
StringRef(SerializedASTBuffer->data(), Size), /*AddNull=*/false); | ||
auto *ASTSym = new llvm::GlobalVariable(*M, Ty, /*constant*/ true, | ||
llvm::GlobalVariable::InternalLinkage, Data, "__clang_ast"); | ||
ASTSym->setAlignment(8); | ||
if (Triple.isOSBinFormatMachO()) | ||
// Include Mach-O segment name. | ||
ASTSym->setSection("__CLANG,__clangast"); | ||
else if (Triple.isOSBinFormatCOFF()) | ||
// Adhere to COFF eight-character limit. | ||
ASTSym->setSection("clangast"); | ||
else | ||
ASTSym->setSection("__clangast"); | ||
|
||
// Use the LLVM backend to emit the pcm. | ||
clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, | ||
Ctx.getTargetInfo().getTargetDescription(), M.get(), | ||
BackendAction::Backend_EmitObj, OS); | ||
|
||
// Make sure the module container hits disk now. | ||
OS->flush(); | ||
|
||
// Free up some memory, in case the process is kept alive. | ||
SerializedASTBuffer->clear(); | ||
} | ||
}; | ||
} | ||
|
||
CodeGenerator *clang::CreateModuleContainerGenerator( | ||
DiagnosticsEngine &Diags, const std::string &ModuleName, | ||
const CodeGenOptions &CGO, const TargetOptions &TO, const LangOptions &LO, | ||
llvm::raw_ostream *OS, PCHGenerator *PCHGen) { | ||
return | ||
new ModuleContainerGenerator(Diags, ModuleName, CGO, TO, LO, OS, PCHGen); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.