Add C API bindings for DIBuilder 'Type' APIs
[lldb.git] / llvm / tools / llvm-c-test / debuginfo.c
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
14 #include "llvm-c-test.h"
15 #include "llvm-c/Core.h"
16 #include "llvm-c/DebugInfo.h"
17 #include <stdio.h>
18 #include <string.h>
19
20 int llvm_test_dibuilder(void) {
21   const char *Filename = "debuginfo.c";
22   LLVMModuleRef M = LLVMModuleCreateWithName(Filename);
23   LLVMDIBuilderRef DIB = LLVMCreateDIBuilder(M);
24
25   LLVMMetadataRef File = LLVMDIBuilderCreateFile(DIB, Filename,
26     strlen(Filename), ".", 1);
27
28   LLVMMetadataRef CompileUnit = LLVMDIBuilderCreateCompileUnit(DIB,
29       LLVMDWARFSourceLanguageC, File,"llvm-c-test", 11, 0, NULL, 0, 0,
30       NULL, 0, LLVMDWARFEmissionFull, 0, 0, 0);
31
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
47   char *MStr = LLVMPrintModuleToString(M);
48   puts(MStr);
49   LLVMDisposeMessage(MStr);
50
51   LLVMDisposeDIBuilder(DIB);
52   LLVMDisposeModule(M);
53
54   return 0;
55 }