llvm.org GIT mirror llvm / aec0628
[LLVM-C] Add DIBuilder Bindings For Variable Creation Summary: Wrap LLVMDIBuilderCreateAutoVariable, LLVMDIBuilderCreateParameterVariable, LLVMDIBuilderCreateExpression, and move and correct LLVMDIBuilderInsertDeclareBefore and LLVMDIBuilderInsertDeclareAtEnd from the Go bindings to the C bindings. Reviewers: harlanhaskins, whitequark, deadalnix Reviewed By: harlanhaskins, whitequark Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D45928 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330555 91177308-0d34-0410-b5e6-96231b3b80d8 Robert Widmann 1 year, 4 months ago
7 changed file(s) with 160 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
1717 #include "llvm/IR/Module.h"
1818
1919 using namespace llvm;
20
21 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
22 LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
23 LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty, int AlwaysPreserve,
24 unsigned Flags, uint32_t AlignInBits) {
25 DIBuilder *D = unwrap(Dref);
26 return wrap(
27 D->createAutoVariable(unwrap(Scope), Name, unwrap(File),
28 Line, unwrap(Ty), AlwaysPreserve,
29 static_cast(Flags), AlignInBits));
30 }
31
32 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
33 LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
34 unsigned ArgNo, LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty,
35 int AlwaysPreserve, unsigned Flags) {
36 DIBuilder *D = unwrap(Dref);
37 return wrap(D->createParameterVariable(
38 unwrap(Scope), Name, ArgNo, unwrap(File), Line,
39 unwrap(Ty), AlwaysPreserve, static_cast(Flags)));
40 }
4120
4221 LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,
4322 LLVMMetadataRef Ty, const char *Name,
7554 return wrap(A.get());
7655 }
7756
78 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
79 int64_t *Addr, size_t Length) {
80 DIBuilder *D = unwrap(Dref);
81 return wrap(D->createExpression(ArrayRef(Addr, Length)));
82 }
83
84 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
85 LLVMValueRef Storage,
86 LLVMMetadataRef VarInfo,
87 LLVMMetadataRef Expr,
88 LLVMBasicBlockRef Block) {
89 // Fail immediately here until the llgo folks update their bindings. The
90 // called function is going to assert out anyway.
91 llvm_unreachable("DIBuilder API change requires a DebugLoc");
92
93 DIBuilder *D = unwrap(Dref);
94 Instruction *Instr = D->insertDeclare(
95 unwrap(Storage), unwrap(VarInfo),
96 unwrap(Expr), /* DebugLoc */ nullptr, unwrap(Block));
97 return wrap(Instr);
98 }
99
10057 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
10158 LLVMValueRef Val,
10259 LLVMMetadataRef VarInfo,
2727
2828 typedef struct LLVMOpaqueDIBuilder *LLVMDIBuilderRef;
2929
30 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
31 LLVMDIBuilderRef D, LLVMMetadataRef Scope, const char *Name,
32 LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty, int AlwaysPreserve,
33 unsigned Flags, uint32_t AlignInBits);
34
35 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
36 LLVMDIBuilderRef D, LLVMMetadataRef Scope, const char *Name, unsigned ArgNo,
37 LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty, int AlwaysPreserve,
38 unsigned Flags);
39
4030 LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef D,
4131 LLVMMetadataRef Ty, const char *Name,
4232 LLVMMetadataRef File, unsigned Line,
5343 LLVMMetadataRef *Data,
5444 size_t Length);
5545
56 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
57 int64_t *Addr, size_t Length);
58
59 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef D,
60 LLVMValueRef Storage,
61 LLVMMetadataRef VarInfo,
62 LLVMMetadataRef Expr,
63 LLVMBasicBlockRef Block);
64
6546 LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef D, LLVMValueRef Val,
6647 LLVMMetadataRef VarInfo,
6748 LLVMMetadataRef Expr,
238238 result := C.LLVMDIBuilderCreateAutoVariable(
239239 d.ref,
240240 scope.C,
241 name,
241 name, C.size_t(len(v.Name)),
242242 v.File.C,
243243 C.unsigned(v.Line),
244244 v.Type.C,
245 boolToCInt(v.AlwaysPreserve),
246 C.unsigned(v.Flags),
245 C.LLVMBool(boolToCInt(v.AlwaysPreserve)),
246 C.LLVMDIFlags(v.Flags),
247247 C.uint32_t(v.AlignInBits),
248248 )
249249 return Metadata{C: result}
270270 result := C.LLVMDIBuilderCreateParameterVariable(
271271 d.ref,
272272 scope.C,
273 name,
273 name, C.size_t(len(v.Name)),
274274 C.unsigned(v.ArgNo),
275275 v.File.C,
276276 C.unsigned(v.Line),
277277 v.Type.C,
278 boolToCInt(v.AlwaysPreserve),
279 C.unsigned(v.Flags),
278 C.LLVMBool(boolToCInt(v.AlwaysPreserve)),
279 C.LLVMDIFlags(v.Flags),
280280 )
281281 return Metadata{C: result}
282282 }
563563 // InsertDeclareAtEnd inserts a call to llvm.dbg.declare at the end of the
564564 // specified basic block for the given value and associated debug metadata.
565565 func (d *DIBuilder) InsertDeclareAtEnd(v Value, diVarInfo, expr Metadata, bb BasicBlock) Value {
566 result := C.LLVMDIBuilderInsertDeclareAtEnd(d.ref, v.C, diVarInfo.C, expr.C, bb.C)
566 result := C.LLVMDIBuilderInsertDeclareAtEnd(d.ref, v.C, diVarInfo.C, expr.C, nil, bb.C)
567567 return Value{C: result}
568568 }
569569
610610 LLVMMetadataRef Type);
611611
612612 /**
613 * Create a new descriptor for the specified variable which has a complex
614 * address expression for its address.
615 * \param Builder The DIBuilder.
616 * \param Addr An array of complex address operations.
617 * \param Length Length of the address operation array.
618 */
619 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
620 int64_t *Addr, size_t Length);
621
622 /**
623 * Insert a new llvm.dbg.declare intrinsic call before the given instruction.
624 * \param Builder The DIBuilder.
625 * \param Storage The storage of the variable to declare.
626 * \param VarInfo The variable's debug info descriptor.
627 * \param Expr A complex location expression for the variable.
628 * \param DebugLoc Debug info location.
629 * \param Instr Instruction acting as a location for the new intrinsic.
630 */
631 LLVMValueRef LLVMDIBuilderInsertDeclareBefore(
632 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
633 LLVMMetadataRef Expr, LLVMMetadataRef DebugLoc, LLVMValueRef Instr);
634
635 /**
636 * Insert a new llvm.dbg.declare intrinsic call at the end of the given basic
637 * block. If the basic block has a terminator instruction, the intrinsic is
638 * inserted before that terminator instruction.
639 * \param Builder The DIBuilder.
640 * \param Storage The storage of the variable to declare.
641 * \param VarInfo The variable's debug info descriptor.
642 * \param Expr A complex location expression for the variable.
643 * \param DebugLoc Debug info location.
644 * \param Block Basic block acting as a location for the new intrinsic.
645 */
646 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
647 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
648 LLVMMetadataRef Expr, LLVMMetadataRef DebugLoc, LLVMBasicBlockRef Block);
649
650 /**
651 * Create a new descriptor for a local auto variable.
652 * \param Builder The DIBuilder.
653 * \param Scope The local scope the variable is declared in.
654 * \param Name Variable name.
655 * \param NameLen Length of variable name.
656 * \param File File where this variable is defined.
657 * \param LineNo Line number.
658 * \param Ty Metadata describing the type of the variable.
659 * \param AlwaysPreserve If true, this descriptor will survive optimizations.
660 * \param Flags Flags.
661 * \param AlignInBits Variable alignment.
662 */
663 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
664 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
665 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
666 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits);
667
668 /**
669 * Create a new descriptor for a function parameter variable.
670 * \param Builder The DIBuilder.
671 * \param Scope The local scope the variable is declared in.
672 * \param Name Variable name.
673 * \param NameLen Length of variable name.
674 * \param ArgNo Unique argument number for this variable; starts at 1.
675 * \param File File where this variable is defined.
676 * \param LineNo Line number.
677 * \param Ty Metadata describing the type of the variable.
678 * \param AlwaysPreserve If true, this descriptor will survive optimizations.
679 * \param Flags Flags.
680 */
681 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
682 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
683 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
684 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags);
685
686 /**
613687 * Get the metadata of the subprogram attached to a function.
614688 *
615689 * @see llvm::Function::getSubprogram()
995995 Elts, map_from_llvmDIFlags(Flags)));
996996 }
997997
998 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
999 int64_t *Addr, size_t Length) {
1000 return wrap(unwrap(Builder)->createExpression(ArrayRef(Addr,
1001 Length)));
1002 }
1003
1004 LLVMValueRef LLVMDIBuilderInsertDeclareBefore(
1005 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1006 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMValueRef Instr) {
1007 return wrap(unwrap(Builder)->insertDeclare(
1008 unwrap(Storage), unwrap(VarInfo),
1009 unwrap(Expr), unwrap(DL),
1010 unwrap(Instr)));
1011 }
1012
1013 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
1014 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1015 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
1016 return wrap(unwrap(Builder)->insertDeclare(
1017 unwrap(Storage), unwrap(VarInfo),
1018 unwrap(Expr), unwrap(DL),
1019 unwrap(Block)));
1020 }
1021
1022 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
1023 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1024 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
1025 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
1026 return wrap(unwrap(Builder)->createAutoVariable(
1027 unwrap(Scope), {Name, NameLen}, unwrap(File),
1028 LineNo, unwrap(Ty), AlwaysPreserve,
1029 map_from_llvmDIFlags(Flags), AlignInBits));
1030 }
1031
1032 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
1033 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1034 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
1035 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
1036 return wrap(unwrap(Builder)->createParameterVariable(
1037 unwrap(Scope), Name, ArgNo, unwrap(File),
1038 LineNo, unwrap(Ty), AlwaysPreserve,
1039 map_from_llvmDIFlags(Flags)));
1040 }
1041
9981042 LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
9991043 return wrap(unwrap(Func)->getSubprogram());
10001044 }
22 ; CHECK: ; ModuleID = 'debuginfo.c'
33 ; CHECK-NEXT: source_filename = "debuginfo.c"
44
5 ; CHECK: declare !dbg !7 i64 @foo(i64, i64)
5 ; CHECK: define i64 @foo(i64, i64) !dbg !7 {
6 ; CHECK-NEXT: entry:
7 ; CHECK-NEXT: call void @llvm.dbg.declare(metadata i64 0, metadata !11, metadata !DIExpression()), !dbg !13
8 ; CHECK-NEXT: call void @llvm.dbg.declare(metadata i64 0, metadata !12, metadata !DIExpression()), !dbg !13
9 ; CHECK-NEXT: }
610
7 ; CHECK: declare !dbg !10 i64 @foo_inner_scope(i64, i64)
11 ; CHECK: declare void @llvm.dbg.declare(metadata, metadata, metadata) #0
12
13 ; CHECK: declare !dbg !14 i64 @foo_inner_scope(i64, i64)
814
915 ; CHECK: !llvm.dbg.cu = !{!0}
1016 ; CHECK-NEXT: !FooType = !{!3}
1117
12 ; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "llvm-c-test", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false)
18 ; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "llvm-c-test", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false)
1319 ; CHECK-NEXT: !1 = !DIFile(filename: "debuginfo.c", directory: ".")
1420 ; CHECK-NEXT: !2 = !{}
1521 ; CHECK-NEXT: !3 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !4, size: 192, dwarfAddressSpace: 0)
1622 ; CHECK-NEXT: !4 = !DICompositeType(tag: DW_TAG_structure_type, name: "MyStruct", file: !1, size: 192, elements: !5, runtimeLang: DW_LANG_C89, identifier: "MyStruct")
1723 ; CHECK-NEXT: !5 = !{!6, !6, !6}
1824 ; CHECK-NEXT: !6 = !DIBasicType(name: "Int64", size: 64)
19 ; CHECK-NEXT: !7 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: !1, file: !1, line: 42, type: !8, isLocal: true, isDefinition: true, scopeLine: 42, isOptimized: false, unit: !0, variables: !2)
25 ; CHECK-NEXT: !7 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: !1, file: !1, line: 42, type: !8, isLocal: true, isDefinition: true, scopeLine: 42, isOptimized: false, unit: !0, variables: !10)
2026 ; CHECK-NEXT: !8 = !DISubroutineType(types: !9)
2127 ; CHECK-NEXT: !9 = !{!6, !6}
22 ; CHECK-NEXT: !10 = distinct !DISubprogram(name: "foo_inner_scope", linkageName: "foo_inner_scope", scope: !11, file: !1, line: 42, type: !8, isLocal: true, isDefinition: true, scopeLine: 42, isOptimized: false, unit: !0, variables: !2)
23 ; CHECK-NEXT: !11 = distinct !DILexicalBlock(scope: !7, file: !1, line: 42)
28 ; CHECK-NEXT: !10 = !{!11, !12}
29 ; CHECK-NEXT: !11 = !DILocalVariable(name: "a", arg: 1, scope: !7, file: !1, line: 42, type: !6)
30 ; CHECK-NEXT: !12 = !DILocalVariable(name: "b", arg: 2, scope: !7, file: !1, line: 42, type: !6)
31 ; CHECK-NEXT: !13 = !DILocation(line: 42, scope: !7)
32 ; CHECK-NEXT: !14 = distinct !DISubprogram(name: "foo_inner_scope", linkageName: "foo_inner_scope", scope: !15, file: !1, line: 42, type: !8, isLocal: true, isDefinition: true, scopeLine: 42, isOptimized: false, unit: !0, variables: !2)
33 ; CHECK-NEXT: !15 = distinct !DILexicalBlock(scope: !7, file: !1, line: 42)
34
4747 LLVMTypeRef FooParamTys[] = { LLVMInt64Type(), LLVMInt64Type() };
4848 LLVMTypeRef FooFuncTy = LLVMFunctionType(LLVMInt64Type(), FooParamTys, 2, 0);
4949 LLVMValueRef FooFunction = LLVMAddFunction(M, "foo", FooFuncTy);
50 LLVMBasicBlockRef FooEntryBlock = LLVMAppendBasicBlock(FooFunction, "entry");
5051
5152 LLVMMetadataRef ParamTypes[] = {Int64Ty, Int64Ty};
5253 LLVMMetadataRef FunctionTy =
5556 LLVMDIBuilderCreateFunction(DIB, File, "foo", 3, "foo", 3,
5657 File, 42, FunctionTy, true, true,
5758 42, 0, false);
59 LLVMMetadataRef FooParamLocation =
60 LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0,
61 FunctionMetadata, NULL);
62 LLVMMetadataRef FooParamExpression =
63 LLVMDIBuilderCreateExpression(DIB, NULL, 0);
64 LLVMMetadataRef FooParamVar1 =
65 LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "a", 1, 1, File,
66 42, Int64Ty, true, 0);
67 LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false),
68 FooParamVar1, FooParamExpression,
69 FooParamLocation, FooEntryBlock);
70 LLVMMetadataRef FooParamVar2 =
71 LLVMDIBuilderCreateParameterVariable(DIB, FunctionMetadata, "b", 1, 2, File,
72 42, Int64Ty, true, 0);
73 LLVMDIBuilderInsertDeclareAtEnd(DIB, LLVMConstInt(LLVMInt64Type(), 0, false),
74 FooParamVar2, FooParamExpression,
75 FooParamLocation, FooEntryBlock);
5876 LLVMSetSubprogram(FooFunction, FunctionMetadata);
5977
6078 LLVMMetadataRef FooLexicalBlock =