Index: bindings/ocaml/llvm/llvm.ml =================================================================== --- bindings/ocaml/llvm/llvm.ml +++ bindings/ocaml/llvm/llvm.ml @@ -563,6 +563,10 @@ external metadata : llvalue -> llmdkind -> llvalue option = "llvm_metadata" external set_metadata : llvalue -> llmdkind -> llvalue -> unit = "llvm_set_metadata" external clear_metadata : llvalue -> llmdkind -> unit = "llvm_clear_metadata" +external get_debug_loc_directory : llvalue -> string option = "llvm_get_debug_loc_directory" +external get_debug_loc_filename : llvalue -> string option = "llvm_get_debug_loc_filename" +external get_debug_loc_line : llvalue -> int = "llvm_get_debug_loc_line" +external get_debug_loc_column : llvalue -> int = "llvm_get_debug_loc_column" (*--... Operations on metadata .......,.....................................--*) external mdstring : llcontext -> string -> llvalue = "llvm_mdstring" @@ -779,6 +783,10 @@ let fold_right_globals f m init = fold_right_global_range f (global_end m) (At_start m) init +external get_global_debug_loc_directory : llvalue -> string option = "llvm_get_global_debug_loc_directory" +external get_global_debug_loc_filename : llvalue -> string option = "llvm_get_global_debug_loc_filename" +external get_global_debug_loc_line : llvalue -> int = "llvm_get_global_debug_loc_line" + (*--... Operations on aliases ..............................................--*) external add_alias : llmodule -> lltype -> llvalue -> string -> llvalue = "llvm_add_alias" @@ -864,6 +872,10 @@ let remove_string_function_attr f k i = llvm_remove_string_function_attr f k (AttrIndex.to_int i) +external get_function_debug_loc_directory : llvalue -> string option = "llvm_get_function_debug_loc_directory" +external get_function_debug_loc_filename : llvalue -> string option = "llvm_get_function_debug_loc_filename" +external get_function_debug_loc_line : llvalue -> int = "llvm_get_function_debug_loc_line" + (*--... Operations on params ...............................................--*) external params : llvalue -> llvalue array = "llvm_params" external param : llvalue -> int -> llvalue = "llvm_param" Index: bindings/ocaml/llvm/llvm.mli =================================================================== --- bindings/ocaml/llvm/llvm.mli +++ bindings/ocaml/llvm/llvm.mli @@ -889,6 +889,11 @@ instruction [i]. See the function [llvm::Instruction::setMetadata]. *) val clear_metadata : llvalue -> llmdkind -> unit +val get_debug_loc_directory : llvalue -> string option +val get_debug_loc_filename : llvalue -> string option +val get_debug_loc_line : llvalue -> int +val get_debug_loc_column : llvalue -> int + (** {7 Operations on metadata} *) @@ -1512,6 +1517,10 @@ See the method [llvm::GlobalVariable::setExternallyInitialized]. *) val set_externally_initialized : bool -> llvalue -> unit +val get_global_debug_loc_directory : llvalue -> string option +val get_global_debug_loc_filename : llvalue -> string option +val get_global_debug_loc_line : llvalue -> int + (** {7 Operations on aliases} *) @@ -1619,6 +1628,10 @@ from the function [f] at position [i]. *) val remove_string_function_attr : llvalue -> string -> AttrIndex.t -> unit +val get_function_debug_loc_directory : llvalue -> string option +val get_function_debug_loc_filename : llvalue -> string option +val get_function_debug_loc_line : llvalue -> int + (** {7 Operations on params} *) Index: bindings/ocaml/llvm/llvm_ocaml.c =================================================================== --- bindings/ocaml/llvm/llvm_ocaml.c +++ bindings/ocaml/llvm/llvm_ocaml.c @@ -814,6 +814,42 @@ return Val_unit; } +/* llvalue -> string option */ +CAMLprim value llvm_get_debug_loc_directory(LLVMValueRef Inst) { + CAMLparam0(); + const char *S = LLVMGetDebugLocDirectory(Inst); + if (S) { + CAMLlocal1(result); + result = caml_alloc_small(1, 0); + Store_field(result, 0, caml_copy_string(S)); + CAMLreturn(result); + } + CAMLreturn(Val_int(0)); +} + +/* llvalue -> string option */ +CAMLprim value llvm_get_debug_loc_filename(LLVMValueRef Inst) { + CAMLparam0(); + const char *S = LLVMGetDebugLocFilename(Inst); + if (S) { + CAMLlocal1(result); + result = caml_alloc_small(1, 0); + Store_field(result, 0, caml_copy_string(S)); + CAMLreturn(result); + } + CAMLreturn(Val_int(0)); +} + +/* llvalue -> int */ +CAMLprim value llvm_get_debug_loc_line(LLVMValueRef Inst) { + return Val_int(LLVMGetDebugLocLine(Inst)); +} + +/* llvalue -> int */ +CAMLprim value llvm_get_debug_loc_column(LLVMValueRef Inst) { + return Val_int(LLVMGetDebugLocColumn(Inst)); +} + /*--... Operations on metadata .............................................--*/ @@ -1336,6 +1372,37 @@ return Val_unit; } +/* llvalue -> string option */ +CAMLprim value llvm_get_global_debug_loc_directory(LLVMValueRef GlobalVar) { + CAMLparam0(); + const char *S = LLVMGetGlobalDebugLocDirectory(GlobalVar); + if (S) { + CAMLlocal1(result); + result = caml_alloc_small(1, 0); + Store_field(result, 0, caml_copy_string(S)); + CAMLreturn(result); + } + CAMLreturn(Val_int(0)); +} + +/* llvalue -> string option */ +CAMLprim value llvm_get_global_debug_loc_filename(LLVMValueRef GlobalVar) { + CAMLparam0(); + const char *S = LLVMGetGlobalDebugLocFilename(GlobalVar); + if (S) { + CAMLlocal1(result); + result = caml_alloc_small(1, 0); + Store_field(result, 0, caml_copy_string(S)); + CAMLreturn(result); + } + CAMLreturn(Val_int(0)); +} + +/* llvalue -> int */ +CAMLprim value llvm_get_global_debug_loc_line(LLVMValueRef GlobalVar) { + return Val_int(LLVMGetGlobalDebugLocLine(GlobalVar)); +} + /*--... Operations on aliases ..............................................--*/ CAMLprim LLVMValueRef llvm_add_alias(LLVMModuleRef M, LLVMTypeRef Ty, @@ -1456,6 +1523,37 @@ return Val_unit; } +/* llvalue -> string option */ +CAMLprim value llvm_get_function_debug_loc_directory(LLVMValueRef Func) { + CAMLparam0(); + const char *S = LLVMGetFunctionDebugLocDirectory(Func); + if (S) { + CAMLlocal1(result); + result = caml_alloc_small(1, 0); + Store_field(result, 0, caml_copy_string(S)); + CAMLreturn(result); + } + CAMLreturn(Val_int(0)); +} + +/* llvalue -> string option */ +CAMLprim value llvm_get_function_debug_loc_filename(LLVMValueRef Func) { + CAMLparam0(); + const char *S = LLVMGetFunctionDebugLocFilename(Func); + if (S) { + CAMLlocal1(result); + result = caml_alloc_small(1, 0); + Store_field(result, 0, caml_copy_string(S)); + CAMLreturn(result); + } + CAMLreturn(Val_int(0)); +} + +/* llvalue -> int */ +CAMLprim value llvm_get_function_debug_loc_line(LLVMValueRef Func) { + return Val_int(LLVMGetFunctionDebugLocLine(Func)); +} + /*--... Operations on parameters ...........................................--*/ DEFINE_ITERATORS(param, Param, LLVMValueRef, LLVMValueRef, LLVMGetParamParent) Index: include/llvm-c/Core.h =================================================================== --- include/llvm-c/Core.h +++ include/llvm-c/Core.h @@ -2168,6 +2168,12 @@ LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar); void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit); +const char *LLVMGetGlobalDebugLocDirectory(LLVMValueRef Inst); + +const char *LLVMGetGlobalDebugLocFilename(LLVMValueRef Inst); + +int LLVMGetGlobalDebugLocLine(LLVMValueRef Inst); + /** * @} */ @@ -2346,6 +2352,12 @@ void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A, const char *V); +const char *LLVMGetFunctionDebugLocDirectory(LLVMValueRef Inst); + +const char *LLVMGetFunctionDebugLocFilename(LLVMValueRef Inst); + +int LLVMGetFunctionDebugLocLine(LLVMValueRef Inst); + /** * @defgroup LLVMCCoreValueFunctionParameters Function Parameters * @@ -2761,6 +2773,14 @@ */ void LLVMSetMetadata(LLVMValueRef Val, unsigned KindID, LLVMValueRef Node); +const char *LLVMGetDebugLocDirectory(LLVMValueRef Inst); + +const char *LLVMGetDebugLocFilename(LLVMValueRef Inst); + +int LLVMGetDebugLocLine(LLVMValueRef Inst); + +int LLVMGetDebugLocColumn(LLVMValueRef Inst); + /** * Obtain the basic block to which an instruction belongs. * Index: lib/IR/Core.cpp =================================================================== --- lib/IR/Core.cpp +++ lib/IR/Core.cpp @@ -17,6 +17,7 @@ #include "llvm/IR/Attributes.h" #include "llvm/IR/CallSite.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/DiagnosticPrinter.h" @@ -872,6 +873,34 @@ unwrap(Inst)->setMetadata(KindID, N); } +const char *LLVMGetDebugLocDirectory(LLVMValueRef Inst) { + Instruction *I = unwrap(Inst); + assert(I && "Expected Instruction"); + const DebugLoc &L = I->getDebugLoc(); + return (L ? L->getDirectory().data() : nullptr); +} + +const char *LLVMGetDebugLocFilename(LLVMValueRef Inst) { + Instruction *I = unwrap(Inst); + assert(I && "Expected Instruction"); + const DebugLoc &L = I->getDebugLoc(); + return (L ? L->getFilename().data() : nullptr); +} + +int LLVMGetDebugLocLine(LLVMValueRef Inst) { + Instruction *I = unwrap(Inst); + assert(I && "Expected Instruction"); + const DebugLoc &L = I->getDebugLoc(); + return (L ? L->getLine() : 0); +} + +int LLVMGetDebugLocColumn(LLVMValueRef Inst) { + Instruction *I = unwrap(Inst); + assert(I && "Expected Instruction"); + const DebugLoc &L = I->getDebugLoc(); + return (L ? L->getColumn() : 0); +} + /*--.. Conversion functions ................................................--*/ #define LLVM_DEFINE_VALUE_CAST(name) \ @@ -2007,6 +2036,36 @@ unwrap(GlobalVar)->setExternallyInitialized(IsExtInit); } +const char *LLVMGetGlobalDebugLocDirectory(LLVMValueRef GlobalVar) { + GlobalVariable *GV = unwrap(GlobalVar); + assert(GV && "Expected GlobalVariable"); + SmallVector GVEs; + GV->getDebugInfo(GVEs); + if (GVEs.size() == 0) return nullptr; + DIGlobalVariable *DI = GVEs[0]->getVariable(); + return DI->getDirectory().data(); +} + +const char *LLVMGetGlobalDebugLocFilename(LLVMValueRef GlobalVar) { + GlobalVariable *GV = unwrap(GlobalVar); + assert(GV && "Expected GlobalVariable"); + SmallVector GVEs; + GV->getDebugInfo(GVEs); + if (GVEs.size() == 0) return nullptr; + DIGlobalVariable *DI = GVEs[0]->getVariable(); + return DI->getFilename().data(); +} + +int LLVMGetGlobalDebugLocLine(LLVMValueRef GlobalVar) { + GlobalVariable *GV = unwrap(GlobalVar); + assert(GV && "Expected GlobalVariable"); + SmallVector GVEs; + GV->getDebugInfo(GVEs); + if (GVEs.size() == 0) return 0; + DIGlobalVariable *DI = GVEs[0]->getVariable(); + return DI->getLine(); +} + /*--.. Operations on aliases ......................................--*/ LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, @@ -2197,6 +2256,27 @@ Func->addAttribute(AttributeList::FunctionIndex, Attr); } +const char *LLVMGetFunctionDebugLocDirectory(LLVMValueRef Func) { + Function *F = unwrap(Func); + assert(F && "Expected Function"); + const DISubprogram *DI = F->getSubprogram(); + return (DI ? DI->getDirectory().data() : nullptr); +} + +const char *LLVMGetFunctionDebugLocFilename(LLVMValueRef Func) { + Function *F = unwrap(Func); + assert(F && "Expected Function"); + const DISubprogram *DI = F->getSubprogram(); + return (DI ? DI->getFilename().data() : nullptr); +} + +int LLVMGetFunctionDebugLocLine(LLVMValueRef Func) { + Function *F = unwrap(Func); + assert(F && "Expected Function"); + const DISubprogram *DI = F->getSubprogram(); + return (DI ? DI->getLine() : 0); +} + /*--.. Operations on parameters ............................................--*/ unsigned LLVMCountParams(LLVMValueRef FnRef) {