blob: fbd41e97d7a1f7e5004d761f388f8cd583bdb086 [file] [log] [blame]
whitequark8b4e8332017-11-01 22:18:52 +00001/*===-- 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
Harlan Haskins80548aa2018-04-02 00:17:40 +000014#include "llvm-c-test.h"
15#include "llvm-c/Core.h"
whitequark8b4e8332017-11-01 22:18:52 +000016#include "llvm-c/DebugInfo.h"
whitequark8b4e8332017-11-01 22:18:52 +000017#include <stdio.h>
Harlan Haskins80548aa2018-04-02 00:17:40 +000018#include <string.h>
whitequark8b4e8332017-11-01 22:18:52 +000019
Robert Widmanne9654fe2018-05-21 16:27:35 +000020static LLVMMetadataRef
21declare_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
Galina Kistanovacd27af22017-12-16 02:54:17 +000031int llvm_test_dibuilder(void) {
Harlan Haskins80548aa2018-04-02 00:17:40 +000032 const char *Filename = "debuginfo.c";
33 LLVMModuleRef M = LLVMModuleCreateWithName(Filename);
whitequark8b4e8332017-11-01 22:18:52 +000034 LLVMDIBuilderRef DIB = LLVMCreateDIBuilder(M);
35
Harlan Haskins80548aa2018-04-02 00:17:40 +000036 LLVMMetadataRef File = LLVMDIBuilderCreateFile(DIB, Filename,
37 strlen(Filename), ".", 1);
whitequark8b4e8332017-11-01 22:18:52 +000038
Harlan Haskins80548aa2018-04-02 00:17:40 +000039 LLVMMetadataRef CompileUnit = LLVMDIBuilderCreateCompileUnit(DIB,
Robert Widmann2fbfc402018-04-23 13:51:43 +000040 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
Robert Widmanne2e5efb2018-04-28 22:32:07 +000050 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);
whitequark4d8bcba2018-04-29 02:01:34 +000059 LLVMDIBuilderCreateImportedModuleFromAlias(DIB, Module, ImportedModule,
60 File, 42);
Robert Widmanne2e5efb2018-04-28 22:32:07 +000061
Robert Widmanne9654fe2018-05-21 16:27:35 +000062 LLVMMetadataRef ClassTy = declare_objc_class(DIB, File);
63 LLVMMetadataRef GlobalClassValueExpr =
Matthew Vossfff44e62018-10-03 18:44:53 +000064 LLVMDIBuilderCreateConstantValueExpression(DIB, 0);
65 LLVMDIBuilderCreateGlobalVariableExpression(
66 DIB, Module, "globalClass", 11, "", 0, File, 1, ClassTy, true,
67 GlobalClassValueExpr, NULL, 0);
Robert Widmanne9654fe2018-05-21 16:27:35 +000068
Harlan Haskins80548aa2018-04-02 00:17:40 +000069 LLVMMetadataRef Int64Ty =
Matthew Vossfff44e62018-10-03 18:44:53 +000070 LLVMDIBuilderCreateBasicType(DIB, "Int64", 5, 64, 0, LLVMDIFlagZero);
Robert Widmann07e51142018-05-10 21:10:06 +000071 LLVMMetadataRef Int64TypeDef =
72 LLVMDIBuilderCreateTypedef(DIB, Int64Ty, "int64_t", 7, File, 42, File);
73
Robert Widmannbffba312018-04-23 22:31:49 +000074 LLVMMetadataRef GlobalVarValueExpr =
Matthew Vossfff44e62018-10-03 18:44:53 +000075 LLVMDIBuilderCreateConstantValueExpression(DIB, 0);
76 LLVMDIBuilderCreateGlobalVariableExpression(
77 DIB, Module, "global", 6, "", 0, File, 1, Int64TypeDef, true,
78 GlobalVarValueExpr, NULL, 0);
Robert Widmannbffba312018-04-23 22:31:49 +000079
80 LLVMMetadataRef NameSpace =
Matthew Vossfff44e62018-10-03 18:44:53 +000081 LLVMDIBuilderCreateNameSpace(DIB, Module, "NameSpace", 9, false);
Harlan Haskins80548aa2018-04-02 00:17:40 +000082
83 LLVMMetadataRef StructDbgElts[] = {Int64Ty, Int64Ty, Int64Ty};
84 LLVMMetadataRef StructDbgTy =
Robert Widmann2fbfc402018-04-23 13:51:43 +000085 LLVMDIBuilderCreateStructType(DIB, NameSpace, "MyStruct",
Harlan Haskins80548aa2018-04-02 00:17:40 +000086 8, File, 0, 192, 0, 0, NULL, StructDbgElts, 3,
87 LLVMDWARFSourceLanguageC, NULL, "MyStruct", 8);
88
89 LLVMMetadataRef StructDbgPtrTy =
90 LLVMDIBuilderCreatePointerType(DIB, StructDbgTy, 192, 0, 0, "", 0);
91
92 LLVMAddNamedMetadataOperand(M, "FooType",
93 LLVMMetadataAsValue(LLVMGetModuleContext(M), StructDbgPtrTy));
94
Robert Widmannbc866fe2018-04-07 06:07:55 +000095
Robert Widmanncd997a72018-04-23 14:29:33 +000096 LLVMTypeRef FooParamTys[] = {
97 LLVMInt64Type(),
98 LLVMInt64Type(),
99 LLVMVectorType(LLVMInt64Type(), 10),
100 };
101 LLVMTypeRef FooFuncTy = LLVMFunctionType(LLVMInt64Type(), FooParamTys, 3, 0);
Robert Widmannbc866fe2018-04-07 06:07:55 +0000102 LLVMValueRef FooFunction = LLVMAddFunction(M, "foo", FooFuncTy);
Robert Widmannaec06282018-04-22 19:24:44 +0000103 LLVMBasicBlockRef FooEntryBlock = LLVMAppendBasicBlock(FooFunction, "entry");
Robert Widmannbc866fe2018-04-07 06:07:55 +0000104
Robert Widmanncd997a72018-04-23 14:29:33 +0000105 LLVMMetadataRef Subscripts[] = {
106 LLVMDIBuilderGetOrCreateSubrange(DIB, 0, 10),
107 };
108 LLVMMetadataRef VectorTy =
109 LLVMDIBuilderCreateVectorType(DIB, 64 * 10, 0,
110 Int64Ty, Subscripts, 1);
111
112
113 LLVMMetadataRef ParamTypes[] = {Int64Ty, Int64Ty, VectorTy};
Robert Widmannbc866fe2018-04-07 06:07:55 +0000114 LLVMMetadataRef FunctionTy =
Robert Widmanncd997a72018-04-23 14:29:33 +0000115 LLVMDIBuilderCreateSubroutineType(DIB, File, ParamTypes, 3, 0);
Robert Widmann42400512018-05-10 18:09:53 +0000116
117 LLVMMetadataRef ReplaceableFunctionMetadata =
118 LLVMDIBuilderCreateReplaceableCompositeType(DIB, 0x15, "foo", 3,
119 File, File, 42,
120 0, 0, 0,
121 LLVMDIFlagFwdDecl,
122 "", 0);
123
124 LLVMMetadataRef FooParamLocation =
125 LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0,
126 ReplaceableFunctionMetadata, NULL);
Robert Widmannbc866fe2018-04-07 06:07:55 +0000127 LLVMMetadataRef FunctionMetadata =
128 LLVMDIBuilderCreateFunction(DIB, File, "foo", 3, "foo", 3,
129 File, 42, FunctionTy, true, true,
130 42, 0, false);
Robert Widmann42400512018-05-10 18:09:53 +0000131 LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata, FunctionMetadata);
Robert Widmanncd997a72018-04-23 14:29:33 +0000132
Robert Widmannaec06282018-04-22 19:24:44 +0000133 LLVMMetadataRef FooParamExpression =
134 LLVMDIBuilderCreateExpression(DIB, NULL, 0);
135 LLVMMetadataRef FooParamVar1 =
136 LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "a", 1, 1, File,
137 42, Int64Ty, true, 0);
138 LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false),
139 FooParamVar1, FooParamExpression,
140 FooParamLocation, FooEntryBlock);
141 LLVMMetadataRef FooParamVar2 =
142 LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "b", 1, 2, File,
143 42, Int64Ty, true, 0);
144 LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false),
145 FooParamVar2, FooParamExpression,
146 FooParamLocation, FooEntryBlock);
Robert Widmanncd997a72018-04-23 14:29:33 +0000147 LLVMMetadataRef FooParamVar3 =
148 LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "c", 1, 3, File,
149 42, VectorTy, true, 0);
150 LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false),
151 FooParamVar3, FooParamExpression,
152 FooParamLocation, FooEntryBlock);
153
Robert Widmannbc866fe2018-04-07 06:07:55 +0000154 LLVMSetSubprogram(FooFunction, FunctionMetadata);
155
156 LLVMMetadataRef FooLexicalBlock =
157 LLVMDIBuilderCreateLexicalBlock(DIB, FunctionMetadata, File, 42, 0);
158
Robert Widmannbffba312018-04-23 22:31:49 +0000159 LLVMBasicBlockRef FooVarBlock = LLVMAppendBasicBlock(FooFunction, "vars");
160 LLVMMetadataRef FooVarsLocation =
161 LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 43, 0,
162 FunctionMetadata, NULL);
163 LLVMMetadataRef FooVar1 =
164 LLVMDIBuilderCreateAutoVariable(DIB, FooLexicalBlock, "d", 1, File,
165 43, Int64Ty, true, 0, 0);
166 LLVMValueRef FooVal1 = LLVMConstInt(LLVMInt64Type(), 0, false);
167 LLVMMetadataRef FooVarValueExpr =
168 LLVMDIBuilderCreateConstantValueExpression(DIB, 0);
169
170 LLVMDIBuilderInsertDbgValueAtEnd(DIB, FooVal1, FooVar1, FooVarValueExpr,
171 FooVarsLocation, FooVarBlock);
Robert Widmannbc866fe2018-04-07 06:07:55 +0000172
173 LLVMDIBuilderFinalize(DIB);
174
whitequark8b4e8332017-11-01 22:18:52 +0000175 char *MStr = LLVMPrintModuleToString(M);
176 puts(MStr);
177 LLVMDisposeMessage(MStr);
178
179 LLVMDisposeDIBuilder(DIB);
180 LLVMDisposeModule(M);
181
182 return 0;
183}