aboutsummaryrefslogtreecommitdiffstats
path: root/lib/VMCore/Constants.cpp
diff options
context:
space:
mode:
authorAnton Korobeynikov <asl@math.spbu.ru>2008-06-29 17:57:03 +0000
committerAnton Korobeynikov <asl@math.spbu.ru>2008-06-29 17:57:03 +0000
commit6d116bc7ced56a820d33b0dd35ee36af8a810eab (patch)
treeb9d056c655001ef42c66a3395d01233d2a499537 /lib/VMCore/Constants.cpp
parent28a2b54580b28be10c536def7f49b5599adf3631 (diff)
downloadexternal_llvm-6d116bc7ced56a820d33b0dd35ee36af8a810eab.zip
external_llvm-6d116bc7ced56a820d33b0dd35ee36af8a810eab.tar.gz
external_llvm-6d116bc7ced56a820d33b0dd35ee36af8a810eab.tar.bz2
Revert (52748 and friends):
Move GetConstantStringInfo to lib/Analysis. Remove string output routine from Constant. Update all callers. Change debug intrinsic api slightly to accomodate move of routine, these now return values instead of strings. This unbreaks llvm-gcc bootstrap. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@52884 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/Constants.cpp')
-rw-r--r--lib/VMCore/Constants.cpp42
1 files changed, 41 insertions, 1 deletions
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp
index 7a08b5f..b9976a7 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 "";
+}