1 /*===-- debuginfo.c - tool for testing libLLVM and llvm-c API -------------===*\
3 |* The LLVM Compiler Infrastructure *|
5 |* This file is distributed under the University of Illinois Open Source *|
6 |* License. See LICENSE.TXT for details. *|
8 |*===----------------------------------------------------------------------===*|
10 |* Tests for the LLVM C DebugInfo API *|
12 \*===----------------------------------------------------------------------===*/
14 #include "llvm-c-test.h"
15 #include "llvm-c/Core.h"
16 #include "llvm-c/DebugInfo.h"
20 static LLVMMetadataRef
21 declare_objc_class(LLVMDIBuilderRef DIB
, LLVMMetadataRef File
) {
22 LLVMMetadataRef Decl
= LLVMDIBuilderCreateStructType(DIB
, File
, "TestClass", 9, File
, 42, 64, 0, LLVMDIFlagObjcClassComplete
, NULL
, NULL
, 0, 0, NULL
, NULL
, 0);
23 LLVMMetadataRef SuperDecl
= LLVMDIBuilderCreateStructType(DIB
, File
, "TestSuperClass", 14, File
, 42, 64, 0, LLVMDIFlagObjcClassComplete
, NULL
, NULL
, 0, 0, NULL
, NULL
, 0);
24 LLVMDIBuilderCreateInheritance(DIB
, Decl
, SuperDecl
, 0, 0, 0);
25 LLVMMetadataRef TestProperty
=
26 LLVMDIBuilderCreateObjCProperty(DIB
, "test", 4, File
, 42, "getTest", 7, "setTest", 7, 0x20 /*copy*/ | 0x40 /*nonatomic*/, SuperDecl
);
27 LLVMDIBuilderCreateObjCIVar(DIB
, "_test", 5, File
, 42, 64, 0, 64, LLVMDIFlagPublic
, SuperDecl
, TestProperty
);
31 int llvm_test_dibuilder(void) {
32 const char *Filename
= "debuginfo.c";
33 LLVMModuleRef M
= LLVMModuleCreateWithName(Filename
);
34 LLVMDIBuilderRef DIB
= LLVMCreateDIBuilder(M
);
36 LLVMMetadataRef File
= LLVMDIBuilderCreateFile(DIB
, Filename
,
37 strlen(Filename
), ".", 1);
39 LLVMMetadataRef CompileUnit
= LLVMDIBuilderCreateCompileUnit(DIB
,
40 LLVMDWARFSourceLanguageC
, File
, "llvm-c-test", 11, 0, NULL
, 0, 0,
41 NULL
, 0, LLVMDWARFEmissionFull
, 0, 0, 0);
43 LLVMMetadataRef Module
=
44 LLVMDIBuilderCreateModule(DIB
, CompileUnit
,
47 "/test/include/llvm-c-test.h", 27,
50 LLVMMetadataRef OtherModule
=
51 LLVMDIBuilderCreateModule(DIB
, CompileUnit
,
52 "llvm-c-test-import", 18,
54 "/test/include/llvm-c-test-import.h", 34,
56 LLVMMetadataRef ImportedModule
=
57 LLVMDIBuilderCreateImportedModuleFromModule(DIB
, Module
, OtherModule
,
59 LLVMDIBuilderCreateImportedModuleFromAlias(DIB
, Module
, ImportedModule
,
62 LLVMMetadataRef ClassTy
= declare_objc_class(DIB
, File
);
63 LLVMMetadataRef GlobalClassValueExpr
=
64 LLVMDIBuilderCreateConstantValueExpression(DIB
, 0);
65 LLVMDIBuilderCreateGlobalVariableExpression(DIB
, Module
, "globalClass", 11,
66 "", 0, File
, 1, ClassTy
,
67 true, GlobalClassValueExpr
,
70 LLVMMetadataRef Int64Ty
=
71 LLVMDIBuilderCreateBasicType(DIB
, "Int64", 5, 64, 0, LLVMDIFlagZero
);
72 LLVMMetadataRef Int64TypeDef
=
73 LLVMDIBuilderCreateTypedef(DIB
, Int64Ty
, "int64_t", 7, File
, 42, File
);
75 LLVMMetadataRef GlobalVarValueExpr
=
76 LLVMDIBuilderCreateConstantValueExpression(DIB
, 0);
77 LLVMDIBuilderCreateGlobalVariableExpression(DIB
, Module
, "global", 6,
78 "", 0, File
, 1, Int64TypeDef
,
79 true, GlobalVarValueExpr
,
82 LLVMMetadataRef NameSpace
=
83 LLVMDIBuilderCreateNameSpace(DIB
, Module
, "NameSpace", 9, false);
85 LLVMMetadataRef StructDbgElts
[] = {Int64Ty
, Int64Ty
, Int64Ty
};
86 LLVMMetadataRef StructDbgTy
=
87 LLVMDIBuilderCreateStructType(DIB
, NameSpace
, "MyStruct",
88 8, File
, 0, 192, 0, 0, NULL
, StructDbgElts
, 3,
89 LLVMDWARFSourceLanguageC
, NULL
, "MyStruct", 8);
91 LLVMMetadataRef StructDbgPtrTy
=
92 LLVMDIBuilderCreatePointerType(DIB
, StructDbgTy
, 192, 0, 0, "", 0);
94 LLVMAddNamedMetadataOperand(M
, "FooType",
95 LLVMMetadataAsValue(LLVMGetModuleContext(M
), StructDbgPtrTy
));
98 LLVMTypeRef FooParamTys
[] = {
101 LLVMVectorType(LLVMInt64Type(), 10),
103 LLVMTypeRef FooFuncTy
= LLVMFunctionType(LLVMInt64Type(), FooParamTys
, 3, 0);
104 LLVMValueRef FooFunction
= LLVMAddFunction(M
, "foo", FooFuncTy
);
105 LLVMBasicBlockRef FooEntryBlock
= LLVMAppendBasicBlock(FooFunction
, "entry");
107 LLVMMetadataRef Subscripts
[] = {
108 LLVMDIBuilderGetOrCreateSubrange(DIB
, 0, 10),
110 LLVMMetadataRef VectorTy
=
111 LLVMDIBuilderCreateVectorType(DIB
, 64 * 10, 0,
112 Int64Ty
, Subscripts
, 1);
115 LLVMMetadataRef ParamTypes
[] = {Int64Ty
, Int64Ty
, VectorTy
};
116 LLVMMetadataRef FunctionTy
=
117 LLVMDIBuilderCreateSubroutineType(DIB
, File
, ParamTypes
, 3, 0);
119 LLVMMetadataRef ReplaceableFunctionMetadata
=
120 LLVMDIBuilderCreateReplaceableCompositeType(DIB
, 0x15, "foo", 3,
126 LLVMMetadataRef FooParamLocation
=
127 LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0,
128 ReplaceableFunctionMetadata
, NULL
);
129 LLVMMetadataRef FunctionMetadata
=
130 LLVMDIBuilderCreateFunction(DIB
, File
, "foo", 3, "foo", 3,
131 File
, 42, FunctionTy
, true, true,
133 LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata
, FunctionMetadata
);
135 LLVMMetadataRef FooParamExpression
=
136 LLVMDIBuilderCreateExpression(DIB
, NULL
, 0);
137 LLVMMetadataRef FooParamVar1
=
138 LLVMDIBuilderCreateParameterVariable(DIB
, FunctionMetadata
, "a", 1, 1, File
,
139 42, Int64Ty
, true, 0);
140 LLVMDIBuilderInsertDeclareAtEnd(DIB
, LLVMConstInt(LLVMInt64Type(), 0, false),
141 FooParamVar1
, FooParamExpression
,
142 FooParamLocation
, FooEntryBlock
);
143 LLVMMetadataRef FooParamVar2
=
144 LLVMDIBuilderCreateParameterVariable(DIB
, FunctionMetadata
, "b", 1, 2, File
,
145 42, Int64Ty
, true, 0);
146 LLVMDIBuilderInsertDeclareAtEnd(DIB
, LLVMConstInt(LLVMInt64Type(), 0, false),
147 FooParamVar2
, FooParamExpression
,
148 FooParamLocation
, FooEntryBlock
);
149 LLVMMetadataRef FooParamVar3
=
150 LLVMDIBuilderCreateParameterVariable(DIB
, FunctionMetadata
, "c", 1, 3, File
,
151 42, VectorTy
, true, 0);
152 LLVMDIBuilderInsertDeclareAtEnd(DIB
, LLVMConstInt(LLVMInt64Type(), 0, false),
153 FooParamVar3
, FooParamExpression
,
154 FooParamLocation
, FooEntryBlock
);
156 LLVMSetSubprogram(FooFunction
, FunctionMetadata
);
158 LLVMMetadataRef FooLexicalBlock
=
159 LLVMDIBuilderCreateLexicalBlock(DIB
, FunctionMetadata
, File
, 42, 0);
161 LLVMBasicBlockRef FooVarBlock
= LLVMAppendBasicBlock(FooFunction
, "vars");
162 LLVMMetadataRef FooVarsLocation
=
163 LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 43, 0,
164 FunctionMetadata
, NULL
);
165 LLVMMetadataRef FooVar1
=
166 LLVMDIBuilderCreateAutoVariable(DIB
, FooLexicalBlock
, "d", 1, File
,
167 43, Int64Ty
, true, 0, 0);
168 LLVMValueRef FooVal1
= LLVMConstInt(LLVMInt64Type(), 0, false);
169 LLVMMetadataRef FooVarValueExpr
=
170 LLVMDIBuilderCreateConstantValueExpression(DIB
, 0);
172 LLVMDIBuilderInsertDbgValueAtEnd(DIB
, FooVal1
, FooVar1
, FooVarValueExpr
,
173 FooVarsLocation
, FooVarBlock
);
175 LLVMDIBuilderFinalize(DIB
);
177 char *MStr
= LLVMPrintModuleToString(M
);
179 LLVMDisposeMessage(MStr
);
181 LLVMDisposeDIBuilder(DIB
);
182 LLVMDisposeModule(M
);