diff options
author | Jay Foad <jay.foad@gmail.com> | 2011-01-11 15:07:38 +0000 |
---|---|---|
committer | Jay Foad <jay.foad@gmail.com> | 2011-01-11 15:07:38 +0000 |
commit | 67c619ba3eae68dcdb3f9340d82b33173aa0c256 (patch) | |
tree | 3c0745fca275d5e4e31f1da2a5a84553fe69bd4a | |
parent | 65fdded3197461232d8428af7ddd0107e4a9f946 (diff) | |
download | external_llvm-67c619ba3eae68dcdb3f9340d82b33173aa0c256.zip external_llvm-67c619ba3eae68dcdb3f9340d82b33173aa0c256.tar.gz external_llvm-67c619ba3eae68dcdb3f9340d82b33173aa0c256.tar.bz2 |
FixedNumOperandTraits and VariadicOperandTraits assumed that, given a
"this" pointer for any subclass of User, you could static_cast it to
User* and then reinterpret_cast that to Use* to get the end of the
operand list. This isn't a safe assumption in general, because the
static_cast might adjust the "this" pointer. Fixed by having these
OperandTraits classes take an extra template parameter, which is the
subclass of User. This is groundwork for PR889.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123235 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/Constants.h | 15 | ||||
-rw-r--r-- | include/llvm/GlobalAlias.h | 3 | ||||
-rw-r--r-- | include/llvm/GlobalVariable.h | 3 | ||||
-rw-r--r-- | include/llvm/InstrTypes.h | 8 | ||||
-rw-r--r-- | include/llvm/Instructions.h | 28 | ||||
-rw-r--r-- | include/llvm/OperandTraits.h | 18 | ||||
-rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.cpp | 3 | ||||
-rw-r--r-- | lib/VMCore/ConstantsContext.h | 30 |
8 files changed, 67 insertions, 41 deletions
diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 0fe9358..229ef9c 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -405,7 +405,8 @@ public: }; template <> -struct OperandTraits<ConstantArray> : public VariadicOperandTraits<> { +struct OperandTraits<ConstantArray> : + public VariadicOperandTraits<ConstantArray> { }; DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant) @@ -454,7 +455,8 @@ public: }; template <> -struct OperandTraits<ConstantStruct> : public VariadicOperandTraits<> { +struct OperandTraits<ConstantStruct> : + public VariadicOperandTraits<ConstantStruct> { }; DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant) @@ -511,7 +513,8 @@ public: }; template <> -struct OperandTraits<ConstantVector> : public VariadicOperandTraits<> { +struct OperandTraits<ConstantVector> : + public VariadicOperandTraits<ConstantVector> { }; DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant) @@ -592,7 +595,8 @@ public: }; template <> -struct OperandTraits<BlockAddress> : public FixedNumOperandTraits<2> { +struct OperandTraits<BlockAddress> : + public FixedNumOperandTraits<BlockAddress, 2> { }; DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(BlockAddress, Value) @@ -871,7 +875,8 @@ private: }; template <> -struct OperandTraits<ConstantExpr> : public VariadicOperandTraits<1> { +struct OperandTraits<ConstantExpr> : + public VariadicOperandTraits<ConstantExpr, 1> { }; DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant) diff --git a/include/llvm/GlobalAlias.h b/include/llvm/GlobalAlias.h index 9867c51..f4af5b1 100644 --- a/include/llvm/GlobalAlias.h +++ b/include/llvm/GlobalAlias.h @@ -89,7 +89,8 @@ public: }; template <> -struct OperandTraits<GlobalAlias> : public FixedNumOperandTraits<1> { +struct OperandTraits<GlobalAlias> : + public FixedNumOperandTraits<GlobalAlias, 1> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalAlias, Value) diff --git a/include/llvm/GlobalVariable.h b/include/llvm/GlobalVariable.h index 597583b..1769c66 100644 --- a/include/llvm/GlobalVariable.h +++ b/include/llvm/GlobalVariable.h @@ -169,7 +169,8 @@ public: }; template <> -struct OperandTraits<GlobalVariable> : public OptionalOperandTraits<> { +struct OperandTraits<GlobalVariable> : + public OptionalOperandTraits<GlobalVariable> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value) diff --git a/include/llvm/InstrTypes.h b/include/llvm/InstrTypes.h index 25b63a5..9dcf688 100644 --- a/include/llvm/InstrTypes.h +++ b/include/llvm/InstrTypes.h @@ -128,7 +128,8 @@ public: }; template <> -struct OperandTraits<UnaryInstruction> : public FixedNumOperandTraits<1> { +struct OperandTraits<UnaryInstruction> : + public FixedNumOperandTraits<UnaryInstruction, 1> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value) @@ -432,7 +433,8 @@ public: }; template <> -struct OperandTraits<BinaryOperator> : public FixedNumOperandTraits<2> { +struct OperandTraits<BinaryOperator> : + public FixedNumOperandTraits<BinaryOperator, 2> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value) @@ -903,7 +905,7 @@ private: // FIXME: these are redundant if CmpInst < BinaryOperator template <> -struct OperandTraits<CmpInst> : public FixedNumOperandTraits<2> { +struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value) diff --git a/include/llvm/Instructions.h b/include/llvm/Instructions.h index 7a53c42..941227f 100644 --- a/include/llvm/Instructions.h +++ b/include/llvm/Instructions.h @@ -262,7 +262,7 @@ private: }; template <> -struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> { +struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value) @@ -524,7 +524,8 @@ public: }; template <> -struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> { +struct OperandTraits<GetElementPtrInst> : + public VariadicOperandTraits<GetElementPtrInst, 1> { }; template<typename RandomAccessIterator> @@ -1087,7 +1088,7 @@ private: }; template <> -struct OperandTraits<CallInst> : public VariadicOperandTraits<1> { +struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> { }; template<typename RandomAccessIterator> @@ -1195,7 +1196,7 @@ public: }; template <> -struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> { +struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value) @@ -1292,7 +1293,8 @@ public: }; template <> -struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> { +struct OperandTraits<ExtractElementInst> : + public FixedNumOperandTraits<ExtractElementInst, 2> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value) @@ -1350,7 +1352,8 @@ public: }; template <> -struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> { +struct OperandTraits<InsertElementInst> : + public FixedNumOperandTraits<InsertElementInst, 3> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value) @@ -1407,7 +1410,8 @@ public: }; template <> -struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> { +struct OperandTraits<ShuffleVectorInst> : + public FixedNumOperandTraits<ShuffleVectorInst, 3> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value) @@ -1751,7 +1755,8 @@ public: }; template <> -struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> { +struct OperandTraits<InsertValueInst> : + public FixedNumOperandTraits<InsertValueInst, 2> { }; template<typename RandomAccessIterator> @@ -2032,7 +2037,7 @@ public: }; template <> -struct OperandTraits<ReturnInst> : public VariadicOperandTraits<> { +struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value) @@ -2125,7 +2130,8 @@ private: }; template <> -struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {}; +struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> { +}; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value) @@ -2616,7 +2622,7 @@ private: }; template <> -struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> { +struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> { }; template<typename RandomAccessIterator> diff --git a/include/llvm/OperandTraits.h b/include/llvm/OperandTraits.h index b614ccb..dbdb1a1 100644 --- a/include/llvm/OperandTraits.h +++ b/include/llvm/OperandTraits.h @@ -27,12 +27,12 @@ namespace llvm { /// when it is a prefix to the User object, and the number of Use objects is /// known at compile time. -template <unsigned ARITY> +template <typename SubClass, unsigned ARITY> struct FixedNumOperandTraits { - static Use *op_begin(User* U) { + static Use *op_begin(SubClass* U) { return reinterpret_cast<Use*>(U) - ARITY; } - static Use *op_end(User* U) { + static Use *op_end(SubClass* U) { return reinterpret_cast<Use*>(U); } static unsigned operands(const User*) { @@ -57,8 +57,8 @@ struct FixedNumOperandTraits { /// OptionalOperandTraits - when the number of operands may change at runtime. /// Naturally it may only decrease, because the allocations may not change. -template <unsigned ARITY = 1> -struct OptionalOperandTraits : public FixedNumOperandTraits<ARITY> { +template <typename SubClass, unsigned ARITY = 1> +struct OptionalOperandTraits : public FixedNumOperandTraits<SubClass, ARITY> { static unsigned operands(const User *U) { return U->getNumOperands(); } @@ -72,12 +72,12 @@ struct OptionalOperandTraits : public FixedNumOperandTraits<ARITY> { /// when it is a prefix to the User object, and the number of Use objects is /// only known at allocation time. -template <unsigned MINARITY = 0> +template <typename SubClass, unsigned MINARITY = 0> struct VariadicOperandTraits { - static Use *op_begin(User* U) { - return reinterpret_cast<Use*>(U) - U->getNumOperands(); + static Use *op_begin(SubClass* U) { + return reinterpret_cast<Use*>(U) - static_cast<User*>(U)->getNumOperands(); } - static Use *op_end(User* U) { + static Use *op_end(SubClass* U) { return reinterpret_cast<Use*>(U); } static unsigned operands(const User *U) { diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 7a71b26..adcad74 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -162,7 +162,8 @@ namespace { // FIXME: can we inherit this from ConstantExpr? template <> -struct OperandTraits<ConstantPlaceHolder> : public FixedNumOperandTraits<1> { +struct OperandTraits<ConstantPlaceHolder> : + public FixedNumOperandTraits<ConstantPlaceHolder, 1> { }; } diff --git a/lib/VMCore/ConstantsContext.h b/lib/VMCore/ConstantsContext.h index 1c04c3e..ffc673f 100644 --- a/lib/VMCore/ConstantsContext.h +++ b/lib/VMCore/ConstantsContext.h @@ -239,54 +239,64 @@ struct CompareConstantExpr : public ConstantExpr { }; template <> -struct OperandTraits<UnaryConstantExpr> : public FixedNumOperandTraits<1> { +struct OperandTraits<UnaryConstantExpr> : + public FixedNumOperandTraits<UnaryConstantExpr, 1> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value) template <> -struct OperandTraits<BinaryConstantExpr> : public FixedNumOperandTraits<2> { +struct OperandTraits<BinaryConstantExpr> : + public FixedNumOperandTraits<BinaryConstantExpr, 2> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value) template <> -struct OperandTraits<SelectConstantExpr> : public FixedNumOperandTraits<3> { +struct OperandTraits<SelectConstantExpr> : + public FixedNumOperandTraits<SelectConstantExpr, 3> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value) template <> -struct OperandTraits<ExtractElementConstantExpr> : public FixedNumOperandTraits<2> { +struct OperandTraits<ExtractElementConstantExpr> : + public FixedNumOperandTraits<ExtractElementConstantExpr, 2> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value) template <> -struct OperandTraits<InsertElementConstantExpr> : public FixedNumOperandTraits<3> { +struct OperandTraits<InsertElementConstantExpr> : + public FixedNumOperandTraits<InsertElementConstantExpr, 3> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value) template <> -struct OperandTraits<ShuffleVectorConstantExpr> : public FixedNumOperandTraits<3> { +struct OperandTraits<ShuffleVectorConstantExpr> : + public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value) template <> -struct OperandTraits<ExtractValueConstantExpr> : public FixedNumOperandTraits<1> { +struct OperandTraits<ExtractValueConstantExpr> : + public FixedNumOperandTraits<ExtractValueConstantExpr, 1> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value) template <> -struct OperandTraits<InsertValueConstantExpr> : public FixedNumOperandTraits<2> { +struct OperandTraits<InsertValueConstantExpr> : + public FixedNumOperandTraits<InsertValueConstantExpr, 2> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value) template <> -struct OperandTraits<GetElementPtrConstantExpr> : public VariadicOperandTraits<1> { +struct OperandTraits<GetElementPtrConstantExpr> : + public VariadicOperandTraits<GetElementPtrConstantExpr, 1> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value) template <> -struct OperandTraits<CompareConstantExpr> : public FixedNumOperandTraits<2> { +struct OperandTraits<CompareConstantExpr> : + public FixedNumOperandTraits<CompareConstantExpr, 2> { }; DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value) |