 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
58.3% |
7 / 12 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
83.3% |
10 / 12 |
| |
|
Line Coverage: |
97.0% |
32 / 33 |
| |
 |
|
 |
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.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #include "clang/CodeGen/ModuleBuilder.h"
15 : #include "CodeGenModule.h"
16 : #include "clang/CodeGen/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;
27 :
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 625: const CodeGenOptions &CGO, llvm::LLVMContext& C)
40 625: : Diags(diags), CodeGenOpts(CGO), M(new llvm::Module(ModuleName, C)) {}
41 :
599: branch 5 taken
0: branch 6 not taken
0: branch 13 not taken
0: branch 14 not taken
42 599: virtual ~CodeGeneratorImpl() {}
43 :
44 625: virtual llvm::Module* GetModule() {
45 625: return M.get();
46 : }
47 :
48 625: virtual llvm::Module* ReleaseModule() {
49 625: return M.take();
50 : }
51 :
52 625: virtual void Initialize(ASTContext &Context) {
53 625: Ctx = &Context;
54 :
55 625: M->setTargetTriple(Ctx->Target.getTriple().getTriple());
56 625: M->setDataLayout(Ctx->Target.getTargetDescription());
57 625: TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
58 : Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts,
59 625: *M, *TD, Diags));
60 625: }
61 :
62 5785: virtual void HandleTopLevelDecl(DeclGroupRef DG) {
63 : // Make sure to emit all elements of a Decl.
6142: branch 2 taken
5785: branch 3 taken
64 11927: for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
65 6142: Builder->EmitTopLevelDecl(*I);
66 5785: }
67 :
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 1509: virtual void HandleTagDeclDefinition(TagDecl *D) {
73 1509: Builder->UpdateCompletedType(D);
74 1509: }
75 :
76 625: virtual void HandleTranslationUnit(ASTContext &Ctx) {
3: branch 1 taken
622: branch 2 taken
77 625: if (Diags.hasErrorOccurred()) {
78 3: M.reset();
79 3: return;
80 : }
81 :
622: branch 1 taken
0: branch 2 not taken
82 622: if (Builder)
83 622: Builder->Release();
84 : }
85 :
86 256: virtual void CompleteTentativeDefinition(VarDecl *D) {
0: branch 1 not taken
256: branch 2 taken
87 256: if (Diags.hasErrorOccurred())
88 0: return;
89 :
90 256: Builder->EmitTentativeDefinition(D);
91 : }
92 : };
93 : }
94 :
95 : CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
96 : const std::string& ModuleName,
97 : const CodeGenOptions &CGO,
98 625: llvm::LLVMContext& C) {
99 625: return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
100 : }
Generated: 2010-02-10 01:31 by zcov