Skip to content

Commit

Permalink
Add methods to read debug information from IR
Browse files Browse the repository at this point in the history
This was previously pushed to LLVM master, but did not land in LLVM 9
yet:
https://reviews.llvm.org/D63056

The patch was slightly modified to fix some small issues.
  • Loading branch information
aykevl committed Nov 24, 2019
1 parent bad6d01 commit b2db3df
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
106 changes: 106 additions & 0 deletions dibuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,109 @@ func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) {
func (md Metadata) ReplaceAllUsesWith(new Metadata) {
C.LLVMMetadataReplaceAllUsesWith(md.C, new.C)
}

type MetadataKind C.LLVMMetadataKind

const (
MDStringMetadataKind = C.LLVMMDStringMetadataKind
ConstantAsMetadataMetadataKind = C.LLVMConstantAsMetadataMetadataKind
LocalAsMetadataMetadataKind = C.LLVMLocalAsMetadataMetadataKind
DistinctMDOperandPlaceholderMetadataKind = C.LLVMDistinctMDOperandPlaceholderMetadataKind
MDTupleMetadataKind = C.LLVMMDTupleMetadataKind
DILocationMetadataKind = C.LLVMDILocationMetadataKind
DIExpressionMetadataKind = C.LLVMDIExpressionMetadataKind
DIGlobalVariableExpressionMetadataKind = C.LLVMDIGlobalVariableExpressionMetadataKind
GenericDINodeMetadataKind = C.LLVMGenericDINodeMetadataKind
DISubrangeMetadataKind = C.LLVMDISubrangeMetadataKind
DIEnumeratorMetadataKind = C.LLVMDIEnumeratorMetadataKind
DIBasicTypeMetadataKind = C.LLVMDIBasicTypeMetadataKind
DIDerivedTypeMetadataKind = C.LLVMDIDerivedTypeMetadataKind
DICompositeTypeMetadataKind = C.LLVMDICompositeTypeMetadataKind
DISubroutineTypeMetadataKind = C.LLVMDISubroutineTypeMetadataKind
DIFileMetadataKind = C.LLVMDIFileMetadataKind
DICompileUnitMetadataKind = C.LLVMDICompileUnitMetadataKind
DISubprogramMetadataKind = C.LLVMDISubprogramMetadataKind
DILexicalBlockMetadataKind = C.LLVMDILexicalBlockMetadataKind
DILexicalBlockFileMetadataKind = C.LLVMDILexicalBlockFileMetadataKind
DINamespaceMetadataKind = C.LLVMDINamespaceMetadataKind
DIModuleMetadataKind = C.LLVMDIModuleMetadataKind
DITemplateTypeParameterMetadataKind = C.LLVMDITemplateTypeParameterMetadataKind
DITemplateValueParameterMetadataKind = C.LLVMDITemplateValueParameterMetadataKind
DIGlobalVariableMetadataKind = C.LLVMDIGlobalVariableMetadataKind
DILocalVariableMetadataKind = C.LLVMDILocalVariableMetadataKind
DILabelMetadataKind = C.LLVMDILabelMetadataKind
DIObjCPropertyMetadataKind = C.LLVMDIObjCPropertyMetadataKind
DIImportedEntityMetadataKind = C.LLVMDIImportedEntityMetadataKind
DIMacroMetadataKind = C.LLVMDIMacroMetadataKind
DIMacroFileMetadataKind = C.LLVMDIMacroFileMetadataKind
DICommonBlockMetadataKind = C.LLVMDICommonBlockMetadataKind
)

// Kind returns the metadata kind.
func (md Metadata) Kind() MetadataKind {
return MetadataKind(C.LLVMGetMetadataKind(md.C))
}

// FileDirectory returns the directory of a DIFile metadata node, or the empty
// string if there is no directory information.
func (md Metadata) FileDirectory() string {
var length C.unsigned
ptr := C.LLVMDIFileGetDirectory(md.C, &length)
if ptr == nil {
return ""
}
return string(((*[1 << 20]byte)(unsafe.Pointer(ptr)))[:length:length])
}

// FileFilename returns the filename of a DIFile metadata node, or the empty
// string if there is no filename information.
func (md Metadata) FileFilename() string {
var length C.unsigned
ptr := C.LLVMDIFileGetFilename(md.C, &length)
if ptr == nil {
return ""
}
return string(((*[1 << 20]byte)(unsafe.Pointer(ptr)))[:length:length])
}

// FileSource returns the source of a DIFile metadata node.
func (md Metadata) FileSource() string {
var length C.unsigned
ptr := C.LLVMDIFileGetSource(md.C, &length)
if ptr == nil {
return ""
}
return string(((*[1 << 20]byte)(unsafe.Pointer(ptr)))[:length:length])
}

// LocationLine returns the line number of a DILocation.
func (md Metadata) LocationLine() uint {
return uint(C.LLVMDILocationGetLine(md.C))
}

// LocationColumn returns the column (offset from the start of the line) of a
// DILocation.
func (md Metadata) LocationColumn() uint {
return uint(C.LLVMDILocationGetColumn(md.C))
}

// LocationScope returns the local scope associated with this debug location.
func (md Metadata) LocationScope() Metadata {
return Metadata{C.LLVMDILocationGetScope(md.C)}
}

// LocationInlinedAt return the "inline at" location associated with this debug
// location.
func (md Metadata) LocationInlinedAt() Metadata {
return Metadata{C.LLVMDILocationGetInlinedAt(md.C)}
}

// ScopeFile returns the file (DIFile) of a given scope.
func (md Metadata) ScopeFile() Metadata {
return Metadata{C.LLVMDIScopeGetFile(md.C)}
}

// SubprogramLine returns the line number of a DISubprogram.
func (md Metadata) SubprogramLine() uint {
return uint(C.LLVMDISubprogramGetLine(md.C))
}
3 changes: 3 additions & 0 deletions ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (c MemoryBuffer) IsNil() bool { return c.C == nil }
func (c PassManager) IsNil() bool { return c.C == nil }
func (c Use) IsNil() bool { return c.C == nil }
func (c Attribute) IsNil() bool { return c.C == nil }
func (c Metadata) IsNil() bool { return c.C == nil }

// helpers
func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) }
Expand Down Expand Up @@ -1209,6 +1210,8 @@ func (bb BasicBlock) MoveAfter(pos BasicBlock) { C.LLVMMoveBasicBlockAfter(bb.C

// Operations on instructions
func (v Value) InstructionParent() (bb BasicBlock) { bb.C = C.LLVMGetInstructionParent(v.C); return }
func (v Value) InstructionDebugLoc() (md Metadata) { md.C = C.LLVMInstructionGetDebugLoc(v.C); return }
func (v Value) InstructionSetDebugLoc(md Metadata) { C.LLVMInstructionSetDebugLoc(v.C, md.C) }
func (bb BasicBlock) FirstInstruction() (v Value) { v.C = C.LLVMGetFirstInstruction(bb.C); return }
func (bb BasicBlock) LastInstruction() (v Value) { v.C = C.LLVMGetLastInstruction(bb.C); return }
func NextInstruction(v Value) (rv Value) { rv.C = C.LLVMGetNextInstruction(v.C); return }
Expand Down

0 comments on commit b2db3df

Please sign in to comment.