[analyzer] Use the new registration mechanism on the non-path-sensitive-checkers:
[clang.git] / lib / CodeGen / ModuleBuilder.cpp
blobd41d3ac268a0657fd7baf79bb92d856b225040c6
1 //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This builds an AST and converts it to LLVM Code.
12 //===----------------------------------------------------------------------===//
14 #include "clang/CodeGen/ModuleBuilder.h"
15 #include "CodeGenModule.h"
16 #include "clang/Frontend/CodeGenOptions.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/TargetInfo.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/Module.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/ADT/OwningPtr.h"
26 using namespace clang;
28 namespace {
29 class CodeGeneratorImpl : public CodeGenerator {
30 Diagnostic &Diags;
31 llvm::OwningPtr<const llvm::TargetData> TD;
32 ASTContext *Ctx;
33 const CodeGenOptions CodeGenOpts; // Intentionally copied in.
34 protected:
35 llvm::OwningPtr<llvm::Module> M;
36 llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
37 public:
38 CodeGeneratorImpl(Diagnostic &diags, const std::string& ModuleName,
39 const CodeGenOptions &CGO, llvm::LLVMContext& C)
40 : Diags(diags), CodeGenOpts(CGO), M(new llvm::Module(ModuleName, C)) {}
42 virtual ~CodeGeneratorImpl() {}
44 virtual llvm::Module* GetModule() {
45 return M.get();
48 virtual llvm::Module* ReleaseModule() {
49 return M.take();
52 virtual void Initialize(ASTContext &Context) {
53 Ctx = &Context;
55 M->setTargetTriple(Ctx->Target.getTriple().getTriple());
56 M->setDataLayout(Ctx->Target.getTargetDescription());
57 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
58 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts,
59 *M, *TD, Diags));
62 virtual void HandleTopLevelDecl(DeclGroupRef DG) {
63 // Make sure to emit all elements of a Decl.
64 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
65 Builder->EmitTopLevelDecl(*I);
68 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
69 /// to (e.g. struct, union, enum, class) is completed. This allows the
70 /// client hack on the type, which can occur at any point in the file
71 /// (because these can be defined in declspecs).
72 virtual void HandleTagDeclDefinition(TagDecl *D) {
73 Builder->UpdateCompletedType(D);
75 // In C++, we may have member functions that need to be emitted at this
76 // point.
77 if (Ctx->getLangOptions().CPlusPlus && !D->isDependentContext()) {
78 for (DeclContext::decl_iterator M = D->decls_begin(),
79 MEnd = D->decls_end();
80 M != MEnd; ++M)
81 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
82 if (Method->isThisDeclarationADefinition() &&
83 Method->hasAttr<UsedAttr>())
84 Builder->EmitTopLevelDecl(Method);
88 virtual void HandleTranslationUnit(ASTContext &Ctx) {
89 if (Diags.hasErrorOccurred()) {
90 M.reset();
91 return;
94 if (Builder)
95 Builder->Release();
98 virtual void CompleteTentativeDefinition(VarDecl *D) {
99 if (Diags.hasErrorOccurred())
100 return;
102 Builder->EmitTentativeDefinition(D);
105 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
106 if (Diags.hasErrorOccurred())
107 return;
109 Builder->EmitVTable(RD, DefinitionRequired);
114 CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
115 const std::string& ModuleName,
116 const CodeGenOptions &CGO,
117 llvm::LLVMContext& C) {
118 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);