whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 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 | |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 14 | #include "llvm-c-test.h" |
| 15 | #include "llvm-c/Core.h" |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 16 | #include "llvm-c/DebugInfo.h" |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 18 | #include <string.h> |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 19 | |
Galina Kistanova | 5f8c84c | 2017-12-16 02:54:17 +0000 | [diff] [blame] | 20 | int llvm_test_dibuilder(void) { |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 21 | const char *Filename = "debuginfo.c"; |
| 22 | LLVMModuleRef M = LLVMModuleCreateWithName(Filename); |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 23 | LLVMDIBuilderRef DIB = LLVMCreateDIBuilder(M); |
| 24 | |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 25 | LLVMMetadataRef File = LLVMDIBuilderCreateFile(DIB, Filename, |
| 26 | strlen(Filename), ".", 1); |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 27 | |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 28 | LLVMMetadataRef CompileUnit = LLVMDIBuilderCreateCompileUnit(DIB, |
Robert Widmann | b02fe64 | 2018-04-23 13:51:43 +0000 | [diff] [blame] | 29 | LLVMDWARFSourceLanguageC, File, "llvm-c-test", 11, 0, NULL, 0, 0, |
| 30 | NULL, 0, LLVMDWARFEmissionFull, 0, 0, 0); |
| 31 | |
| 32 | LLVMMetadataRef Module = |
| 33 | LLVMDIBuilderCreateModule(DIB, CompileUnit, |
| 34 | "llvm-c-test", 11, |
| 35 | "", 0, |
| 36 | "/test/include/llvm-c-test.h", 27, |
| 37 | "", 0); |
| 38 | |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 39 | LLVMMetadataRef Int64Ty = |
| 40 | LLVMDIBuilderCreateBasicType(DIB, "Int64", 5, 64, 0); |
Robert Widmann | 21fc15d | 2018-04-23 22:31:49 +0000 | [diff] [blame^] | 41 | LLVMMetadataRef GlobalVarValueExpr = |
| 42 | LLVMDIBuilderCreateConstantValueExpression(DIB, 0); |
| 43 | LLVMDIBuilderCreateGlobalVariableExpression(DIB, Module, "global", 6, |
| 44 | "", 0, File, 1, Int64Ty, |
| 45 | true, GlobalVarValueExpr, |
| 46 | NULL, 0); |
| 47 | |
| 48 | LLVMMetadataRef NameSpace = |
| 49 | LLVMDIBuilderCreateNameSpace(DIB, Module, "NameSpace", 9, false); |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 50 | |
| 51 | LLVMMetadataRef StructDbgElts[] = {Int64Ty, Int64Ty, Int64Ty}; |
| 52 | LLVMMetadataRef StructDbgTy = |
Robert Widmann | b02fe64 | 2018-04-23 13:51:43 +0000 | [diff] [blame] | 53 | LLVMDIBuilderCreateStructType(DIB, NameSpace, "MyStruct", |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 54 | 8, File, 0, 192, 0, 0, NULL, StructDbgElts, 3, |
| 55 | LLVMDWARFSourceLanguageC, NULL, "MyStruct", 8); |
| 56 | |
| 57 | LLVMMetadataRef StructDbgPtrTy = |
| 58 | LLVMDIBuilderCreatePointerType(DIB, StructDbgTy, 192, 0, 0, "", 0); |
| 59 | |
| 60 | LLVMAddNamedMetadataOperand(M, "FooType", |
| 61 | LLVMMetadataAsValue(LLVMGetModuleContext(M), StructDbgPtrTy)); |
| 62 | |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 63 | |
Robert Widmann | 6978db7 | 2018-04-23 14:29:33 +0000 | [diff] [blame] | 64 | LLVMTypeRef FooParamTys[] = { |
| 65 | LLVMInt64Type(), |
| 66 | LLVMInt64Type(), |
| 67 | LLVMVectorType(LLVMInt64Type(), 10), |
| 68 | }; |
| 69 | LLVMTypeRef FooFuncTy = LLVMFunctionType(LLVMInt64Type(), FooParamTys, 3, 0); |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 70 | LLVMValueRef FooFunction = LLVMAddFunction(M, "foo", FooFuncTy); |
Robert Widmann | 12e367b | 2018-04-22 19:24:44 +0000 | [diff] [blame] | 71 | LLVMBasicBlockRef FooEntryBlock = LLVMAppendBasicBlock(FooFunction, "entry"); |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 72 | |
Robert Widmann | 6978db7 | 2018-04-23 14:29:33 +0000 | [diff] [blame] | 73 | LLVMMetadataRef Subscripts[] = { |
| 74 | LLVMDIBuilderGetOrCreateSubrange(DIB, 0, 10), |
| 75 | }; |
| 76 | LLVMMetadataRef VectorTy = |
| 77 | LLVMDIBuilderCreateVectorType(DIB, 64 * 10, 0, |
| 78 | Int64Ty, Subscripts, 1); |
| 79 | |
| 80 | |
| 81 | LLVMMetadataRef ParamTypes[] = {Int64Ty, Int64Ty, VectorTy}; |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 82 | LLVMMetadataRef FunctionTy = |
Robert Widmann | 6978db7 | 2018-04-23 14:29:33 +0000 | [diff] [blame] | 83 | LLVMDIBuilderCreateSubroutineType(DIB, File, ParamTypes, 3, 0); |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 84 | LLVMMetadataRef FunctionMetadata = |
| 85 | LLVMDIBuilderCreateFunction(DIB, File, "foo", 3, "foo", 3, |
| 86 | File, 42, FunctionTy, true, true, |
| 87 | 42, 0, false); |
Robert Widmann | 6978db7 | 2018-04-23 14:29:33 +0000 | [diff] [blame] | 88 | |
Robert Widmann | 12e367b | 2018-04-22 19:24:44 +0000 | [diff] [blame] | 89 | LLVMMetadataRef FooParamLocation = |
| 90 | LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0, |
| 91 | FunctionMetadata, NULL); |
| 92 | LLVMMetadataRef FooParamExpression = |
| 93 | LLVMDIBuilderCreateExpression(DIB, NULL, 0); |
| 94 | LLVMMetadataRef FooParamVar1 = |
| 95 | LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "a", 1, 1, File, |
| 96 | 42, Int64Ty, true, 0); |
| 97 | LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false), |
| 98 | FooParamVar1, FooParamExpression, |
| 99 | FooParamLocation, FooEntryBlock); |
| 100 | LLVMMetadataRef FooParamVar2 = |
| 101 | LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "b", 1, 2, File, |
| 102 | 42, Int64Ty, true, 0); |
| 103 | LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false), |
| 104 | FooParamVar2, FooParamExpression, |
| 105 | FooParamLocation, FooEntryBlock); |
Robert Widmann | 6978db7 | 2018-04-23 14:29:33 +0000 | [diff] [blame] | 106 | LLVMMetadataRef FooParamVar3 = |
| 107 | LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "c", 1, 3, File, |
| 108 | 42, VectorTy, true, 0); |
| 109 | LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false), |
| 110 | FooParamVar3, FooParamExpression, |
| 111 | FooParamLocation, FooEntryBlock); |
| 112 | |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 113 | LLVMSetSubprogram(FooFunction, FunctionMetadata); |
| 114 | |
| 115 | LLVMMetadataRef FooLexicalBlock = |
| 116 | LLVMDIBuilderCreateLexicalBlock(DIB, FunctionMetadata, File, 42, 0); |
| 117 | |
Robert Widmann | 21fc15d | 2018-04-23 22:31:49 +0000 | [diff] [blame^] | 118 | LLVMBasicBlockRef FooVarBlock = LLVMAppendBasicBlock(FooFunction, "vars"); |
| 119 | LLVMMetadataRef FooVarsLocation = |
| 120 | LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 43, 0, |
| 121 | FunctionMetadata, NULL); |
| 122 | LLVMMetadataRef FooVar1 = |
| 123 | LLVMDIBuilderCreateAutoVariable(DIB, FooLexicalBlock, "d", 1, File, |
| 124 | 43, Int64Ty, true, 0, 0); |
| 125 | LLVMValueRef FooVal1 = LLVMConstInt(LLVMInt64Type(), 0, false); |
| 126 | LLVMMetadataRef FooVarValueExpr = |
| 127 | LLVMDIBuilderCreateConstantValueExpression(DIB, 0); |
| 128 | |
| 129 | LLVMDIBuilderInsertDbgValueAtEnd(DIB, FooVal1, FooVar1, FooVarValueExpr, |
| 130 | FooVarsLocation, FooVarBlock); |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 131 | |
| 132 | LLVMDIBuilderFinalize(DIB); |
| 133 | |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 134 | char *MStr = LLVMPrintModuleToString(M); |
| 135 | puts(MStr); |
| 136 | LLVMDisposeMessage(MStr); |
| 137 | |
| 138 | LLVMDisposeDIBuilder(DIB); |
| 139 | LLVMDisposeModule(M); |
| 140 | |
| 141 | return 0; |
| 142 | } |