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 | |
| 39 | LLVMMetadataRef NameSpace = |
| 40 | LLVMDIBuilderCreateNameSpace(DIB, Module, "NameSpace", 9, false); |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 41 | |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 42 | LLVMMetadataRef Int64Ty = |
| 43 | LLVMDIBuilderCreateBasicType(DIB, "Int64", 5, 64, 0); |
| 44 | |
| 45 | LLVMMetadataRef StructDbgElts[] = {Int64Ty, Int64Ty, Int64Ty}; |
| 46 | LLVMMetadataRef StructDbgTy = |
Robert Widmann | b02fe64 | 2018-04-23 13:51:43 +0000 | [diff] [blame^] | 47 | LLVMDIBuilderCreateStructType(DIB, NameSpace, "MyStruct", |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 48 | 8, File, 0, 192, 0, 0, NULL, StructDbgElts, 3, |
| 49 | LLVMDWARFSourceLanguageC, NULL, "MyStruct", 8); |
| 50 | |
| 51 | LLVMMetadataRef StructDbgPtrTy = |
| 52 | LLVMDIBuilderCreatePointerType(DIB, StructDbgTy, 192, 0, 0, "", 0); |
| 53 | |
| 54 | LLVMAddNamedMetadataOperand(M, "FooType", |
| 55 | LLVMMetadataAsValue(LLVMGetModuleContext(M), StructDbgPtrTy)); |
| 56 | |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 57 | |
| 58 | LLVMTypeRef FooParamTys[] = { LLVMInt64Type(), LLVMInt64Type() }; |
| 59 | LLVMTypeRef FooFuncTy = LLVMFunctionType(LLVMInt64Type(), FooParamTys, 2, 0); |
| 60 | LLVMValueRef FooFunction = LLVMAddFunction(M, "foo", FooFuncTy); |
Robert Widmann | 12e367b | 2018-04-22 19:24:44 +0000 | [diff] [blame] | 61 | LLVMBasicBlockRef FooEntryBlock = LLVMAppendBasicBlock(FooFunction, "entry"); |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 62 | |
| 63 | LLVMMetadataRef ParamTypes[] = {Int64Ty, Int64Ty}; |
| 64 | LLVMMetadataRef FunctionTy = |
| 65 | LLVMDIBuilderCreateSubroutineType(DIB, File, ParamTypes, 2, 0); |
| 66 | LLVMMetadataRef FunctionMetadata = |
| 67 | LLVMDIBuilderCreateFunction(DIB, File, "foo", 3, "foo", 3, |
| 68 | File, 42, FunctionTy, true, true, |
| 69 | 42, 0, false); |
Robert Widmann | 12e367b | 2018-04-22 19:24:44 +0000 | [diff] [blame] | 70 | LLVMMetadataRef FooParamLocation = |
| 71 | LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0, |
| 72 | FunctionMetadata, NULL); |
| 73 | LLVMMetadataRef FooParamExpression = |
| 74 | LLVMDIBuilderCreateExpression(DIB, NULL, 0); |
| 75 | LLVMMetadataRef FooParamVar1 = |
| 76 | LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "a", 1, 1, File, |
| 77 | 42, Int64Ty, true, 0); |
| 78 | LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false), |
| 79 | FooParamVar1, FooParamExpression, |
| 80 | FooParamLocation, FooEntryBlock); |
| 81 | LLVMMetadataRef FooParamVar2 = |
| 82 | LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "b", 1, 2, File, |
| 83 | 42, Int64Ty, true, 0); |
| 84 | LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false), |
| 85 | FooParamVar2, FooParamExpression, |
| 86 | FooParamLocation, FooEntryBlock); |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 87 | LLVMSetSubprogram(FooFunction, FunctionMetadata); |
| 88 | |
| 89 | LLVMMetadataRef FooLexicalBlock = |
| 90 | LLVMDIBuilderCreateLexicalBlock(DIB, FunctionMetadata, File, 42, 0); |
| 91 | |
| 92 | LLVMValueRef InnerFooFunction = |
| 93 | LLVMAddFunction(M, "foo_inner_scope", FooFuncTy); |
| 94 | LLVMMetadataRef InnerFunctionMetadata = |
| 95 | LLVMDIBuilderCreateFunction(DIB, FooLexicalBlock, "foo_inner_scope", 15, |
| 96 | "foo_inner_scope", 15, |
| 97 | File, 42, FunctionTy, true, true, |
| 98 | 42, 0, false); |
| 99 | LLVMSetSubprogram(InnerFooFunction, InnerFunctionMetadata); |
| 100 | |
| 101 | LLVMDIBuilderFinalize(DIB); |
| 102 | |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 103 | char *MStr = LLVMPrintModuleToString(M); |
| 104 | puts(MStr); |
| 105 | LLVMDisposeMessage(MStr); |
| 106 | |
| 107 | LLVMDisposeDIBuilder(DIB); |
| 108 | LLVMDisposeModule(M); |
| 109 | |
| 110 | return 0; |
| 111 | } |