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,85 @@ N->addOperand(extractMDNode(unwrap(Val))); } +const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) { + assert(Length && "Length must be non-null"); + StringRef S; + if (const auto *I = unwrap(Val)) { + S = I->getDebugLoc()->getDirectory(); + } else if (const auto *GV = unwrap(Val)) { + SmallVector GVEs; + GV->getDebugInfo(GVEs); + if (GVEs.size() == 0) goto No_loc; + const DIGlobalVariable *V = GVEs[0]->getVariable(); + if (!V) goto No_loc; + S = V->getDirectory(); + } else if (const auto *F = unwrap(Val)) { + const DISubprogram *DI = F->getSubprogram(); + if (!DI) goto No_loc; + S = DI->getDirectory(); + } else { + assert(0 && "Expected Instruction, GlobalVariable or Function"); + } + *Length = S.size(); + return S.data(); + No_loc: + *Length = 0; + return nullptr; +} + +const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) { + assert(Length && "Length must not be nullptr"); + 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) { + if (const auto *I = unwrap(Val)) + if (const auto &L = I->getDebugLoc()) + return L->getColumn(); + return 0; +} + /*--.. Operations on scalar constants ......................................--*/ LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,