1 /*===-- debuginfo.c - tool for testing libLLVM and llvm-c API -------------===*\
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 |* Tests for the LLVM C DebugInfo API *|
11 |* *|
12 \*===----------------------------------------------------------------------===*/
13
14 #include "llvm-c-test.h"
15 #include "llvm-c/Core.h"
16 #include "llvm-c/DebugInfo.h"
17 #include <stdio.h>
18 #include <string.h>
19
20 static LLVMMetadataRef
declare_objc_class(LLVMDIBuilderRef DIB,LLVMMetadataRef File)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);
28 return Decl;
29 }
30
llvm_test_dibuilder(void)31 int llvm_test_dibuilder(void) {
32 const char *Filename = "debuginfo.c";
33 LLVMModuleRef M = LLVMModuleCreateWithName(Filename);
34 LLVMDIBuilderRef DIB = LLVMCreateDIBuilder(M);
35
36 LLVMMetadataRef File = LLVMDIBuilderCreateFile(DIB, Filename,
37 strlen(Filename), ".", 1);
38
39 LLVMMetadataRef CompileUnit = LLVMDIBuilderCreateCompileUnit(DIB,
40 LLVMDWARFSourceLanguageC, File, "llvm-c-test", 11, 0, NULL, 0, 0,
41 NULL, 0, LLVMDWARFEmissionFull, 0, 0, 0);
42
43 LLVMMetadataRef Module =
44 LLVMDIBuilderCreateModule(DIB, CompileUnit,
45 "llvm-c-test", 11,
46 "", 0,
47 "/test/include/llvm-c-test.h", 27,
48 "", 0);
49
50 LLVMMetadataRef OtherModule =
51 LLVMDIBuilderCreateModule(DIB, CompileUnit,
52 "llvm-c-test-import", 18,
53 "", 0,
54 "/test/include/llvm-c-test-import.h", 34,
55 "", 0);
56 LLVMMetadataRef ImportedModule =
57 LLVMDIBuilderCreateImportedModuleFromModule(DIB, Module, OtherModule,
58 File, 42);
59 LLVMDIBuilderCreateImportedModuleFromAlias(DIB, Module, ImportedModule,
60 File, 42);
61
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,
68 NULL, 0);
69
70 LLVMMetadataRef Int64Ty =
71 LLVMDIBuilderCreateBasicType(DIB, "Int64", 5, 64, 0);
72 LLVMMetadataRef Int64TypeDef =
73 LLVMDIBuilderCreateTypedef(DIB, Int64Ty, "int64_t", 7, File, 42, File);
74
75 LLVMMetadataRef GlobalVarValueExpr =
76 LLVMDIBuilderCreateConstantValueExpression(DIB, 0);
77 LLVMDIBuilderCreateGlobalVariableExpression(DIB, Module, "global", 6,
78 "", 0, File, 1, Int64TypeDef,
79 true, GlobalVarValueExpr,
80 NULL, 0);
81
82 LLVMMetadataRef NameSpace =
83 LLVMDIBuilderCreateNameSpace(DIB, Module, "NameSpace", 9, false);
84
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);
90
91 LLVMMetadataRef StructDbgPtrTy =
92 LLVMDIBuilderCreatePointerType(DIB, StructDbgTy, 192, 0, 0, "", 0);
93
94 LLVMAddNamedMetadataOperand(M, "FooType",
95 LLVMMetadataAsValue(LLVMGetModuleContext(M), StructDbgPtrTy));
96
97
98 LLVMTypeRef FooParamTys[] = {
99 LLVMInt64Type(),
100 LLVMInt64Type(),
101 LLVMVectorType(LLVMInt64Type(), 10),
102 };
103 LLVMTypeRef FooFuncTy = LLVMFunctionType(LLVMInt64Type(), FooParamTys, 3, 0);
104 LLVMValueRef FooFunction = LLVMAddFunction(M, "foo", FooFuncTy);
105 LLVMBasicBlockRef FooEntryBlock = LLVMAppendBasicBlock(FooFunction, "entry");
106
107 LLVMMetadataRef Subscripts[] = {
108 LLVMDIBuilderGetOrCreateSubrange(DIB, 0, 10),
109 };
110 LLVMMetadataRef VectorTy =
111 LLVMDIBuilderCreateVectorType(DIB, 64 * 10, 0,
112 Int64Ty, Subscripts, 1);
113
114
115 LLVMMetadataRef ParamTypes[] = {Int64Ty, Int64Ty, VectorTy};
116 LLVMMetadataRef FunctionTy =
117 LLVMDIBuilderCreateSubroutineType(DIB, File, ParamTypes, 3, 0);
118
119 LLVMMetadataRef ReplaceableFunctionMetadata =
120 LLVMDIBuilderCreateReplaceableCompositeType(DIB, 0x15, "foo", 3,
121 File, File, 42,
122 0, 0, 0,
123 LLVMDIFlagFwdDecl,
124 "", 0);
125
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,
132 42, 0, false);
133 LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata, FunctionMetadata);
134
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);
155
156 LLVMSetSubprogram(FooFunction, FunctionMetadata);
157
158 LLVMMetadataRef FooLexicalBlock =
159 LLVMDIBuilderCreateLexicalBlock(DIB, FunctionMetadata, File, 42, 0);
160
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);
171
172 LLVMDIBuilderInsertDbgValueAtEnd(DIB, FooVal1, FooVar1, FooVarValueExpr,
173 FooVarsLocation, FooVarBlock);
174
175 LLVMDIBuilderFinalize(DIB);
176
177 char *MStr = LLVMPrintModuleToString(M);
178 puts(MStr);
179 LLVMDisposeMessage(MStr);
180
181 LLVMDisposeDIBuilder(DIB);
182 LLVMDisposeModule(M);
183
184 return 0;
185 }
186