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, |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 29 | LLVMDWARFSourceLanguageC, File,"llvm-c-test", 11, 0, NULL, 0, 0, |
| 30 | NULL, 0, LLVMDWARFEmissionFull, 0, 0, 0); |
| 31 | |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 32 | LLVMMetadataRef Int64Ty = |
| 33 | LLVMDIBuilderCreateBasicType(DIB, "Int64", 5, 64, 0); |
| 34 | |
| 35 | LLVMMetadataRef StructDbgElts[] = {Int64Ty, Int64Ty, Int64Ty}; |
| 36 | LLVMMetadataRef StructDbgTy = |
| 37 | LLVMDIBuilderCreateStructType(DIB, CompileUnit, "MyStruct", |
| 38 | 8, File, 0, 192, 0, 0, NULL, StructDbgElts, 3, |
| 39 | LLVMDWARFSourceLanguageC, NULL, "MyStruct", 8); |
| 40 | |
| 41 | LLVMMetadataRef StructDbgPtrTy = |
| 42 | LLVMDIBuilderCreatePointerType(DIB, StructDbgTy, 192, 0, 0, "", 0); |
| 43 | |
| 44 | LLVMAddNamedMetadataOperand(M, "FooType", |
| 45 | LLVMMetadataAsValue(LLVMGetModuleContext(M), StructDbgPtrTy)); |
| 46 | |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame^] | 47 | |
| 48 | LLVMTypeRef FooParamTys[] = { LLVMInt64Type(), LLVMInt64Type() }; |
| 49 | LLVMTypeRef FooFuncTy = LLVMFunctionType(LLVMInt64Type(), FooParamTys, 2, 0); |
| 50 | LLVMValueRef FooFunction = LLVMAddFunction(M, "foo", FooFuncTy); |
| 51 | |
| 52 | LLVMMetadataRef ParamTypes[] = {Int64Ty, Int64Ty}; |
| 53 | LLVMMetadataRef FunctionTy = |
| 54 | LLVMDIBuilderCreateSubroutineType(DIB, File, ParamTypes, 2, 0); |
| 55 | LLVMMetadataRef FunctionMetadata = |
| 56 | LLVMDIBuilderCreateFunction(DIB, File, "foo", 3, "foo", 3, |
| 57 | File, 42, FunctionTy, true, true, |
| 58 | 42, 0, false); |
| 59 | LLVMSetSubprogram(FooFunction, FunctionMetadata); |
| 60 | |
| 61 | LLVMMetadataRef FooLexicalBlock = |
| 62 | LLVMDIBuilderCreateLexicalBlock(DIB, FunctionMetadata, File, 42, 0); |
| 63 | |
| 64 | LLVMValueRef InnerFooFunction = |
| 65 | LLVMAddFunction(M, "foo_inner_scope", FooFuncTy); |
| 66 | LLVMMetadataRef InnerFunctionMetadata = |
| 67 | LLVMDIBuilderCreateFunction(DIB, FooLexicalBlock, "foo_inner_scope", 15, |
| 68 | "foo_inner_scope", 15, |
| 69 | File, 42, FunctionTy, true, true, |
| 70 | 42, 0, false); |
| 71 | LLVMSetSubprogram(InnerFooFunction, InnerFunctionMetadata); |
| 72 | |
| 73 | LLVMDIBuilderFinalize(DIB); |
| 74 | |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 75 | char *MStr = LLVMPrintModuleToString(M); |
| 76 | puts(MStr); |
| 77 | LLVMDisposeMessage(MStr); |
| 78 | |
| 79 | LLVMDisposeDIBuilder(DIB); |
| 80 | LLVMDisposeModule(M); |
| 81 | |
| 82 | return 0; |
| 83 | } |