From 7c57c41d5f8e700151c5eeb2b7e0e65f4d3149a1 Mon Sep 17 00:00:00 2001 From: Jeffrey Yasskin Date: Tue, 17 Nov 2009 00:43:13 +0000 Subject: [PATCH] In GlobalVariable::setInitializer, assert that the initializer has the right type. llvm-svn: 89014 --- llvm/include/llvm/GlobalVariable.h | 16 ++++------------ llvm/lib/VMCore/Globals.cpp | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/llvm/include/llvm/GlobalVariable.h b/llvm/include/llvm/GlobalVariable.h index 428ce90fefcd..68bd1b3eab15 100644 --- a/llvm/include/llvm/GlobalVariable.h +++ b/llvm/include/llvm/GlobalVariable.h @@ -99,18 +99,10 @@ public: assert(hasInitializer() && "GV doesn't have initializer!"); return static_cast(Op<0>().get()); } - inline void setInitializer(Constant *CPV) { - if (CPV == 0) { - if (hasInitializer()) { - Op<0>().set(0); - NumOperands = 0; - } - } else { - if (!hasInitializer()) - NumOperands = 1; - Op<0>().set(CPV); - } - } + /// setInitializer - Sets the initializer for this global variable, removing + /// any existing initializer if InitVal==NULL. If this GV has type T*, the + /// initializer must have type T. + void setInitializer(Constant *InitVal); /// If the value is a global constant, its value is immutable throughout the /// runtime execution of the program. Assigning a value into the constant diff --git a/llvm/lib/VMCore/Globals.cpp b/llvm/lib/VMCore/Globals.cpp index 2d7d1b960b66..94bf3dea9ab3 100644 --- a/llvm/lib/VMCore/Globals.cpp +++ b/llvm/lib/VMCore/Globals.cpp @@ -171,6 +171,21 @@ void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, this->setOperand(0, cast(To)); } +void GlobalVariable::setInitializer(Constant *InitVal) { + if (InitVal == 0) { + if (hasInitializer()) { + Op<0>().set(0); + NumOperands = 0; + } + } else { + assert(InitVal->getType() == getType()->getElementType() && + "Initializer type must match GlobalVariable type"); + if (!hasInitializer()) + NumOperands = 1; + Op<0>().set(InitVal); + } +} + /// copyAttributesFrom - copy all additional attributes (those not needed to /// create a GlobalVariable) from the GlobalVariable Src to this one. void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { -- GitLab