Index: include/llvm-c/Core.h =================================================================== --- include/llvm-c/Core.h +++ include/llvm-c/Core.h @@ -930,6 +930,44 @@ LLVMValueRef Val); /** + * Return the directory of the debug location for this value, which must be + * an llvm::Instruction, llvm::GlobalVariable, or llvm::Function. + * + * @see llvm::Instruction::getDebugLoc() + * @see llvm::GlobalVariable::getDebugInfo() + * @see llvm::Function::getSubprogram() + */ +const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length); + +/** + * Return the filename of the debug location for this value, which must be + * an llvm::Instruction, llvm::GlobalVariable, or llvm::Function. + * + * @see llvm::Instruction::getDebugLoc() + * @see llvm::GlobalVariable::getDebugInfo() + * @see llvm::Function::getSubprogram() + */ +const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length); + +/** + * Return the line number of the debug location for this value, which must be + * an llvm::Instruction, llvm::GlobalVariable, or llvm::Function. + * + * @see llvm::Instruction::getDebugLoc() + * @see llvm::GlobalVariable::getDebugInfo() + * @see llvm::Function::getSubprogram() + */ +int LLVMGetDebugLocLine(LLVMValueRef Val); + +/** + * Return the column number of the debug location for this value, which must be + * an llvm::Instruction. + * + * @see llvm::Instruction::getDebugLoc() + */ +int LLVMGetDebugLocColumn(LLVMValueRef Val); + +/** * Add a function to a module under a specified name. * * @see llvm::Function::Create() 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" @@ -1157,6 +1158,88 @@ N->addOperand(extractMDNode(unwrap(Val))); } +const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) { + if (isa(unwrap(Val))) { + StringRef S = unwrap(Val)->getDebugLoc()->getDirectory(); + *Length = S.size(); + return S.data(); + } else if (isa(unwrap(Val))) { + SmallVector GVEs; + unwrap(Val)->getDebugInfo(GVEs); + if (GVEs.size() != 0) { + StringRef S = GVEs[0]->getVariable()->getDirectory(); + *Length = S.size(); + return S.data(); + } else { + *Length = 0; + return nullptr; + } + } else if (isa(unwrap(Val))) { + const DISubprogram *DI = unwrap(Val)->getSubprogram(); + if (DI) { + StringRef S = DI->getDirectory(); + *Length = S.size(); + return S.data(); + } else { + *Length = 0; + return nullptr; + } + } + assert(0 && "Expected Instruction, GlobalVariable or Function"); +} + +const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) { + if (isa(unwrap(Val))) { + StringRef S = unwrap(Val)->getDebugLoc()->getFilename(); + *Length = S.size(); + return S.data(); + } else if (isa(unwrap(Val))) { + SmallVector GVEs; + unwrap(Val)->getDebugInfo(GVEs); + if (GVEs.size() != 0) { + StringRef S = GVEs[0]->getVariable()->getFilename(); + *Length = S.size(); + return S.data(); + } else { + *Length = 0; + return nullptr; + } + } else if (isa(unwrap(Val))) { + const DISubprogram *DI = unwrap(Val)->getSubprogram(); + if (DI) { + StringRef S = DI->getFilename(); + *Length = S.size(); + return S.data(); + } else { + *Length = 0; + return nullptr; + } + } + assert(0 && "Expected Instruction, GlobalVariable or Function"); +} + +int LLVMGetDebugLocLine(LLVMValueRef Val) { + if (isa(unwrap(Val))) { + const DebugLoc &L = unwrap(Val)->getDebugLoc(); + return (L ? L->getLine() : 0); + } else if (isa(unwrap(Val))) { + SmallVector GVEs; + unwrap(Val)->getDebugInfo(GVEs); + return (GVEs.size() > 0 ? GVEs[0]->getVariable()->getLine() : 0); + } else if (isa(unwrap(Val))) { + const DISubprogram *DI = unwrap(Val)->getSubprogram(); + return (DI ? DI->getLine() : 0); + } + assert(0 && "Expected Instruction, GlobalVariable or Function"); +} + +int LLVMGetDebugLocColumn(LLVMValueRef Val) { + Instruction *I = unwrap(Val); + assert(I && "Expected Instruction"); + const DebugLoc &L = I->getDebugLoc(); + return (L ? L->getColumn() : 0); +} + /*--.. Operations on scalar constants ......................................--*/ LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,