diff options
author | Jim Laskey <jlaskey@mac.com> | 2006-03-08 18:11:07 +0000 |
---|---|---|
committer | Jim Laskey <jlaskey@mac.com> | 2006-03-08 18:11:07 +0000 |
commit | 21b6c9d6477c8df3f884c3f1ebeaaa44dd53aafe (patch) | |
tree | 6a270d094423a48490d9b95eb38e8b7429e38bc9 /lib/VMCore | |
parent | 1ffd41ab9927be0fb2d2ddaf1fed6c85f00140d9 (diff) |
Get rid of the multiple copies of getStringValue. Now a Constant:: method.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26616 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/Constants.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index c8a5f356bc..1beb5bc9b7 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -1712,3 +1712,43 @@ void Constant::clearAllValueMaps() { (*I)->destroyConstantImpl(); Constants.clear(); } + +/// 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. +/// +std::string Constant::getStringValue(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. + 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 (Constant *C = dyn_cast<Constant>(this)) { + if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) + return GV->getStringValue(Offset); + else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { + 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))->getRawValue(); + return CE->getOperand(0)->getStringValue(Offset); + } + } + } + } + return ""; +} + |