diff options
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/Constants.cpp | 42 | ||||
-rw-r--r-- | lib/VMCore/IntrinsicInst.cpp | 13 |
2 files changed, 47 insertions, 8 deletions
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 7a08b5fb8f..b9976a7207 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -2658,4 +2658,44 @@ void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, // Delete the old constant! destroyConstant(); -}
\ No newline at end of file +} + + +/// getStringValue - Turn an LLVM constant pointer that eventually points to a +/// global into a string value. Return an empty string if we can't do it. +/// Parameter Chop determines if the result is chopped at the first null +/// terminator. +/// +std::string Constant::getStringValue(bool Chop, unsigned Offset) { + if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) { + if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { + ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); + if (Init->isString()) { + std::string Result = Init->getAsString(); + if (Offset < Result.size()) { + // If we are pointing INTO The string, erase the beginning... + Result.erase(Result.begin(), Result.begin()+Offset); + + // Take off the null terminator, and any string fragments after it. + if (Chop) { + std::string::size_type NullPos = Result.find_first_of((char)0); + if (NullPos != std::string::npos) + Result.erase(Result.begin()+NullPos, Result.end()); + } + return Result; + } + } + } + } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) { + if (CE->getOpcode() == Instruction::GetElementPtr) { + // Turn a gep into the specified offset. + if (CE->getNumOperands() == 3 && + cast<Constant>(CE->getOperand(1))->isNullValue() && + isa<ConstantInt>(CE->getOperand(2))) { + Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue(); + return CE->getOperand(0)->getStringValue(Chop, Offset); + } + } + } + return ""; +} diff --git a/lib/VMCore/IntrinsicInst.cpp b/lib/VMCore/IntrinsicInst.cpp index 6bd9431767..eca606292d 100644 --- a/lib/VMCore/IntrinsicInst.cpp +++ b/lib/VMCore/IntrinsicInst.cpp @@ -28,7 +28,6 @@ #include "llvm/IntrinsicInst.h" #include "llvm/Constants.h" #include "llvm/GlobalVariable.h" -#include "llvm/Analysis/ValueTracking.h" #include "llvm/CodeGen/MachineDebugInfoDesc.h" #include "llvm/CodeGen/MachineModuleInfo.h" using namespace llvm; @@ -59,20 +58,20 @@ Value *DbgInfoIntrinsic::StripCast(Value *C) { /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction. /// -Value *DbgStopPointInst::getFileName() const { +std::string DbgStopPointInst::getFileName() const { // Once the operand indices are verified, update this assert assert(LLVMDebugVersion == (6 << 16) && "Verify operand indices"); GlobalVariable *GV = cast<GlobalVariable>(getContext()); - if (!GV->hasInitializer()) return NULL; + if (!GV->hasInitializer()) return ""; ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer()); - return CS->getOperand(4); + return CS->getOperand(3)->getStringValue(); } -Value *DbgStopPointInst::getDirectory() const { +std::string DbgStopPointInst::getDirectory() const { // Once the operand indices are verified, update this assert assert(LLVMDebugVersion == (6 << 16) && "Verify operand indices"); GlobalVariable *GV = cast<GlobalVariable>(getContext()); - if (!GV->hasInitializer()) return NULL; + if (!GV->hasInitializer()) return ""; ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer()); - return CS->getOperand(4); + return CS->getOperand(4)->getStringValue(); } |