blob: 2d594d004d923589768b615c29e16e45617ed669 [file] [log] [blame]
whitequark789164d2017-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 Haskinsb7881bb2018-04-02 00:17:40 +000014#include "llvm-c-test.h"
15#include "llvm-c/Core.h"
whitequark789164d2017-11-01 22:18:52 +000016#include "llvm-c/DebugInfo.h"
whitequark789164d2017-11-01 22:18:52 +000017#include <stdio.h>
Harlan Haskinsb7881bb2018-04-02 00:17:40 +000018#include <string.h>
whitequark789164d2017-11-01 22:18:52 +000019
Galina Kistanova5f8c84c2017-12-16 02:54:17 +000020int llvm_test_dibuilder(void) {
Harlan Haskinsb7881bb2018-04-02 00:17:40 +000021 const char *Filename = "debuginfo.c";
22 LLVMModuleRef M = LLVMModuleCreateWithName(Filename);
whitequark789164d2017-11-01 22:18:52 +000023 LLVMDIBuilderRef DIB = LLVMCreateDIBuilder(M);
24
Harlan Haskinsb7881bb2018-04-02 00:17:40 +000025 LLVMMetadataRef File = LLVMDIBuilderCreateFile(DIB, Filename,
26 strlen(Filename), ".", 1);
whitequark789164d2017-11-01 22:18:52 +000027
Harlan Haskinsb7881bb2018-04-02 00:17:40 +000028 LLVMMetadataRef CompileUnit = LLVMDIBuilderCreateCompileUnit(DIB,
whitequark789164d2017-11-01 22:18:52 +000029 LLVMDWARFSourceLanguageC, File,"llvm-c-test", 11, 0, NULL, 0, 0,
30 NULL, 0, LLVMDWARFEmissionFull, 0, 0, 0);
31
Harlan Haskinsb7881bb2018-04-02 00:17:40 +000032 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 Widmannf53050f2018-04-07 06:07:55 +000047
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
whitequark789164d2017-11-01 22:18:52 +000075 char *MStr = LLVMPrintModuleToString(M);
76 puts(MStr);
77 LLVMDisposeMessage(MStr);
78
79 LLVMDisposeDIBuilder(DIB);
80 LLVMDisposeModule(M);
81
82 return 0;
83}