Skip to content

Commit

Permalink
[LLVM-C] Move DIBuilder Bindings For Block Scopes
Browse files Browse the repository at this point in the history
Summary: Move LLVMDIBuilderCreateFunction , LLVMDIBuilderCreateLexicalBlock, and LLVMDIBuilderCreateLexicalBlockFile from Go to LLVM-C.

Reviewers: whitequark, harlanhaskins, deadalnix

Reviewed By: whitequark, harlanhaskins

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D45352

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@329488 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
CodaFi committed Apr 7, 2018
1 parent 38738ec commit bc866fe
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 67 deletions.
33 changes: 0 additions & 33 deletions bindings/go/llvm/DIBuilderBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,6 @@

using namespace llvm;

LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
LLVMMetadataRef Scope,
LLVMMetadataRef File,
unsigned Line,
unsigned Column) {
DIBuilder *D = unwrap(Dref);
auto *LB = D->createLexicalBlock(unwrap<DILocalScope>(Scope),
unwrap<DIFile>(File), Line, Column);
return wrap(LB);
}

LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
LLVMMetadataRef Scope,
LLVMMetadataRef File,
unsigned Discriminator) {
DIBuilder *D = unwrap(Dref);
return wrap(D->createLexicalBlockFile(unwrap<DILocalScope>(Scope),
unwrap<DIFile>(File), Discriminator));
}

LLVMMetadataRef LLVMDIBuilderCreateFunction(
LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
const char *LinkageName, LLVMMetadataRef File, unsigned Line,
LLVMMetadataRef CompositeType, int IsLocalToUnit, int IsDefinition,
unsigned ScopeLine, unsigned Flags, int IsOptimized) {
DIBuilder *D = unwrap(Dref);
return wrap(D->createFunction(
unwrap<DIScope>(Scope), Name, LinkageName,
File ? unwrap<DIFile>(File) : nullptr, Line,
unwrap<DISubroutineType>(CompositeType), IsLocalToUnit, IsDefinition,
ScopeLine, static_cast<DINode::DIFlags>(Flags), IsOptimized));
}

LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty, int AlwaysPreserve,
Expand Down
16 changes: 0 additions & 16 deletions bindings/go/llvm/DIBuilderBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,6 @@ extern "C" {

typedef struct LLVMOpaqueDIBuilder *LLVMDIBuilderRef;

LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef D,
LLVMMetadataRef Scope,
LLVMMetadataRef File,
unsigned Line, unsigned Column);

LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef D,
LLVMMetadataRef Scope,
LLVMMetadataRef File,
unsigned Discriminator);

LLVMMetadataRef LLVMDIBuilderCreateFunction(
LLVMDIBuilderRef D, LLVMMetadataRef Scope, const char *Name,
const char *LinkageName, LLVMMetadataRef File, unsigned Line,
LLVMMetadataRef CompositeType, int IsLocalToUnit, int IsDefinition,
unsigned ScopeLine, unsigned Flags, int IsOptimized);

LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
LLVMDIBuilderRef D, LLVMMetadataRef Scope, const char *Name,
LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty, int AlwaysPreserve,
Expand Down
3 changes: 0 additions & 3 deletions bindings/go/llvm/IRBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,3 @@ LLVMDebugLocMetadata LLVMGetCurrentDebugLocation2(LLVMBuilderRef Bref) {
return md;
}

void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
}
2 changes: 0 additions & 2 deletions bindings/go/llvm/IRBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,

struct LLVMDebugLocMetadata LLVMGetCurrentDebugLocation2(LLVMBuilderRef Bref);

void LLVMSetSubprogram(LLVMValueRef Fn, LLVMMetadataRef SP);

#ifdef __cplusplus
}

Expand Down
14 changes: 7 additions & 7 deletions bindings/go/llvm/dibuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,16 @@ func (d *DIBuilder) CreateFunction(diScope Metadata, f DIFunction) Metadata {
result := C.LLVMDIBuilderCreateFunction(
d.ref,
diScope.C,
name,
linkageName,
name, C.size_t(len(f.Name)),
linkageName, C.size_t(len(f.LinkageName)),
f.File.C,
C.unsigned(f.Line),
f.Type.C,
boolToCInt(f.LocalToUnit),
boolToCInt(f.IsDefinition),
C.LLVMBool(boolToCInt(f.LocalToUnit)),
C.LLVMBool(boolToCInt(f.IsDefinition)),
C.unsigned(f.ScopeLine),
C.unsigned(f.Flags),
boolToCInt(f.Optimized),
C.LLVMDIFlags(f.Flags),
C.LLVMBool(boolToCInt(f.Optimized)),
)
return Metadata{C: result}
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func (d *DIBuilder) CreateStructType(scope Metadata, t DIStructType) Metadata {
C.unsigned(0), // Optional Objective-C runtime version.
t.VTableHolder.C,
uniqueID,
C.uint64_t(len(t.UniqueID)),
C.size_t(len(t.UniqueID)),
)
return Metadata{C: result}
}
Expand Down
64 changes: 64 additions & 0 deletions include/llvm-c/DebugInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,56 @@ LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
size_t FilenameLen, const char *Directory,
size_t DirectoryLen);

/**
* Create a new descriptor for the specified subprogram.
* \param Builder The \c DIBuilder.
* \param Scope Function scope.
* \param Name Function name.
* \param NameLen Length of enumeration name.
* \param LinkageName Mangled function name.
* \param LinkageNameLen Length of linkage name.
* \param File File where this variable is defined.
* \param LineNo Line number.
* \param Ty Function type.
* \param IsLocalToUnit True if this function is not externally visible.
* \param IsDefinition True if this is a function definition.
* \param ScopeLine Set to the beginning of the scope this starts
* \param Flags E.g.: \c LLVMDIFlagLValueReference. These flags are
* used to emit dwarf attributes.
* \param IsOptimized True if optimization is ON.
*/
LLVMMetadataRef LLVMDIBuilderCreateFunction(
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized);

/**
* Create a descriptor for a lexical block with the specified parent context.
* \param Builder The \c DIBuilder.
* \param Scope Parent lexical block.
* \param File Source file.
* \param Line The line in the source file.
* \param Column The column in the source file.
*/
LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
LLVMMetadataRef File, unsigned Line, unsigned Column);

/**
* Create a descriptor for a lexical block with a new file attached.
* \param Builder The \c DIBuilder.
* \param Scope Lexical block.
* \param File Source file.
* \param Discriminator DWARF path discriminator value.
*/
LLVMMetadataRef
LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
LLVMMetadataRef Scope,
LLVMMetadataRef File,
unsigned Discriminator);

/**
* Creates a new DebugLocation that describes a source location.
* \param Line The line in the source file.
Expand Down Expand Up @@ -560,6 +610,20 @@ LLVMMetadataRef
LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
LLVMMetadataRef Type);

/**
* Get the metadata of the subprogram attached to a function.
*
* @see llvm::Function::getSubprogram()
*/
LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func);

/**
* Set the subprogram attached to a function.
*
* @see llvm::Function::setSubprogram()
*/
void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP);

#ifdef __cplusplus
} /* end extern "C" */
#endif
Expand Down
40 changes: 40 additions & 0 deletions lib/IR/DebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,38 @@ LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
StringRef(Directory, DirectoryLen)));
}

LLVMMetadataRef LLVMDIBuilderCreateFunction(
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
return wrap(unwrap(Builder)->createFunction(
unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty),
IsLocalToUnit, IsDefinition, ScopeLine, map_from_llvmDIFlags(Flags),
IsOptimized, nullptr, nullptr, nullptr));
}


LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
LLVMMetadataRef File, unsigned Line, unsigned Col) {
return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
unwrapDI<DIFile>(File),
Line, Col));
}

LLVMMetadataRef
LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
LLVMMetadataRef Scope,
LLVMMetadataRef File,
unsigned Discriminator) {
return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
unwrapDI<DIFile>(File),
Discriminator));
}

LLVMMetadataRef
LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
unsigned Column, LLVMMetadataRef Scope,
Expand Down Expand Up @@ -942,3 +974,11 @@ LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
return wrap(unwrap(Builder)->createSubroutineType(
Elts, map_from_llvmDIFlags(Flags)));
}

LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
return wrap(unwrap<Function>(Func)->getSubprogram());
}

void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
}
22 changes: 16 additions & 6 deletions test/Bindings/llvm-c/debug_info.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
; CHECK: ; ModuleID = 'debuginfo.c'
; CHECK-NEXT: source_filename = "debuginfo.c"

; CHECK: declare !dbg !7 i64 @foo(i64, i64)

; CHECK: declare !dbg !10 i64 @foo_inner_scope(i64, i64)

; CHECK: !llvm.dbg.cu = !{!0}
; CHECK-NEXT: !FooType = !{!2}
; CHECK-NEXT: !FooType = !{!3}

; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "llvm-c-test", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false)
; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "llvm-c-test", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false)
; CHECK-NEXT: !1 = !DIFile(filename: "debuginfo.c", directory: ".")
; CHECK-NEXT: !2 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !3, size: 192, dwarfAddressSpace: 0)
; CHECK-NEXT: !3 = !DICompositeType(tag: DW_TAG_structure_type, name: "MyStruct", file: !1, size: 192, elements: !4, runtimeLang: DW_LANG_C89, identifier: "MyStruct")
; CHECK-NEXT: !4 = !{!5, !5, !5}
; CHECK-NEXT: !5 = !DIBasicType(name: "Int64", size: 64)
; CHECK-NEXT: !2 = !{}
; CHECK-NEXT: !3 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !4, size: 192, dwarfAddressSpace: 0)
; CHECK-NEXT: !4 = !DICompositeType(tag: DW_TAG_structure_type, name: "MyStruct", file: !1, size: 192, elements: !5, runtimeLang: DW_LANG_C89, identifier: "MyStruct")
; CHECK-NEXT: !5 = !{!6, !6, !6}
; CHECK-NEXT: !6 = !DIBasicType(name: "Int64", size: 64)
; 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)
; CHECK-NEXT: !8 = !DISubroutineType(types: !9)
; CHECK-NEXT: !9 = !{!6, !6}
; 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)
; CHECK-NEXT: !11 = distinct !DILexicalBlock(scope: !7, file: !1, line: 42)
28 changes: 28 additions & 0 deletions tools/llvm-c-test/debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ int llvm_test_dibuilder(void) {
LLVMAddNamedMetadataOperand(M, "FooType",
LLVMMetadataAsValue(LLVMGetModuleContext(M), StructDbgPtrTy));


LLVMTypeRef FooParamTys[] = { LLVMInt64Type(), LLVMInt64Type() };
LLVMTypeRef FooFuncTy = LLVMFunctionType(LLVMInt64Type(), FooParamTys, 2, 0);
LLVMValueRef FooFunction = LLVMAddFunction(M, "foo", FooFuncTy);

LLVMMetadataRef ParamTypes[] = {Int64Ty, Int64Ty};
LLVMMetadataRef FunctionTy =
LLVMDIBuilderCreateSubroutineType(DIB, File, ParamTypes, 2, 0);
LLVMMetadataRef FunctionMetadata =
LLVMDIBuilderCreateFunction(DIB, File, "foo", 3, "foo", 3,
File, 42, FunctionTy, true, true,
42, 0, false);
LLVMSetSubprogram(FooFunction, FunctionMetadata);

LLVMMetadataRef FooLexicalBlock =
LLVMDIBuilderCreateLexicalBlock(DIB, FunctionMetadata, File, 42, 0);

LLVMValueRef InnerFooFunction =
LLVMAddFunction(M, "foo_inner_scope", FooFuncTy);
LLVMMetadataRef InnerFunctionMetadata =
LLVMDIBuilderCreateFunction(DIB, FooLexicalBlock, "foo_inner_scope", 15,
"foo_inner_scope", 15,
File, 42, FunctionTy, true, true,
42, 0, false);
LLVMSetSubprogram(InnerFooFunction, InnerFunctionMetadata);

LLVMDIBuilderFinalize(DIB);

char *MStr = LLVMPrintModuleToString(M);
puts(MStr);
LLVMDisposeMessage(MStr);
Expand Down

0 comments on commit bc866fe

Please sign in to comment.