[llvm] r186559 - Fix a regression I introduced back in r178147.
Rafael Espindola
rafael.espindola at gmail.com
Wed Jul 17 19:42:40 PDT 2013
Author: rafael
Date: Wed Jul 17 21:42:40 2013
New Revision: 186559
URL: http://llvm.org/viewvc/llvm-project?rev=186559&view=rev
Log:
Fix a regression I introduced back in r178147.
We don't want cast and dyn_cast to work on temporaries. They don't extend
lifetime like a direct bind to a reference would, so they can introduce
hard to find bugs.
I added tests to make sure we don't regress this. Thanks to Eli Friedman for
noticing this and for his suggestions on how to test it.
Modified:
llvm/trunk/include/llvm/Support/Casting.h
llvm/trunk/unittests/Support/Casting.cpp
Modified: llvm/trunk/include/llvm/Support/Casting.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Casting.h?rev=186559&r1=186558&r2=186559&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Casting.h (original)
+++ llvm/trunk/include/llvm/Support/Casting.h Wed Jul 17 21:42:40 2013
@@ -206,7 +206,10 @@ template<class To, class FromTy> struct
}
};
-
+template <class X> struct is_simple_type {
+ static const bool value =
+ is_same<X, typename simplify_type<X>::SimpleType>::value;
+};
// cast<X> - Return the argument parameter cast to the specified type. This
// casting operator asserts that the type is correct, so it does not return null
@@ -216,10 +219,12 @@ template<class To, class FromTy> struct
// cast<Instruction>(myVal)->getParent()
//
template <class X, class Y>
-inline typename cast_retty<X, const Y>::ret_type cast(const Y &Val) {
+inline typename enable_if_c<!is_simple_type<Y>::value,
+ typename cast_retty<X, const Y>::ret_type>::type
+cast(const Y &Val) {
assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
- return cast_convert_val<X, const Y,
- typename simplify_type<const Y>::SimpleType>::doit(Val);
+ return cast_convert_val<
+ X, const Y, typename simplify_type<const Y>::SimpleType>::doit(Val);
}
template <class X, class Y>
@@ -230,10 +235,7 @@ inline typename cast_retty<X, Y>::ret_ty
}
template <class X, class Y>
-inline typename enable_if<
- is_same<Y, typename simplify_type<Y>::SimpleType>,
- typename cast_retty<X, Y*>::ret_type
->::type cast(Y *Val) {
+inline typename cast_retty<X, Y *>::ret_type cast(Y *Val) {
assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
return cast_convert_val<X, Y*,
typename simplify_type<Y*>::SimpleType>::doit(Val);
@@ -259,7 +261,9 @@ inline typename cast_retty<X, Y*>::ret_t
//
template <class X, class Y>
-inline typename cast_retty<X, const Y>::ret_type dyn_cast(const Y &Val) {
+inline typename enable_if_c<!is_simple_type<Y>::value,
+ typename cast_retty<X, const Y>::ret_type>::type
+dyn_cast(const Y &Val) {
return isa<X>(Val) ? cast<X>(Val) : 0;
}
@@ -269,10 +273,7 @@ inline typename cast_retty<X, Y>::ret_ty
}
template <class X, class Y>
-inline typename enable_if<
- is_same<Y, typename simplify_type<Y>::SimpleType>,
- typename cast_retty<X, Y*>::ret_type
->::type dyn_cast(Y *Val) {
+inline typename cast_retty<X, Y *>::ret_type dyn_cast(Y *Val) {
return isa<X>(Val) ? cast<X>(Val) : 0;
}
Modified: llvm/trunk/unittests/Support/Casting.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Casting.cpp?rev=186559&r1=186558&r2=186559&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Casting.cpp (original)
+++ llvm/trunk/unittests/Support/Casting.cpp Wed Jul 17 21:42:40 2013
@@ -10,10 +10,15 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/IR/User.h"
#include "gtest/gtest.h"
#include <cstdlib>
namespace llvm {
+// Used to test illegal cast. If a cast doesn't match any of the "real" ones,
+// it will match this one.
+struct IllegalCast;
+template <typename T> IllegalCast *cast(...) { return 0; }
// set up two example classes
// with conversion facility
@@ -60,10 +65,25 @@ foo *bar::naz() {
bar *fub();
+
+template <> struct simplify_type<foo> {
+ typedef int SimpleType;
+ static SimpleType getSimplifiedValue(foo &Val) { return 0; }
+};
+
} // End llvm namespace
using namespace llvm;
+
+// Test the peculiar behavior of Use in simplify_type.
+int Check1[is_same<simplify_type<Use>::SimpleType, Value *>::value ? 1 : -1];
+int Check2[is_same<simplify_type<Use *>::SimpleType, Value *>::value ? 1 : -1];
+
+// Test that a regular class behaves as expected.
+int Check3[is_same<simplify_type<foo>::SimpleType, int>::value ? 1 : -1];
+int Check4[is_same<simplify_type<foo *>::SimpleType, foo *>::value ? 1 : -1];
+
namespace {
const foo *null_foo = NULL;
@@ -203,3 +223,8 @@ TEST(CastingTest, InferredUpcastTakesPre
} // end namespace inferred_upcasting
} // end anonymous namespace
+// Test that we reject casts of temporaries (and so the illegal cast gets used).
+namespace TemporaryCast {
+struct pod {};
+IllegalCast *testIllegalCast() { return cast<foo>(pod()); }
+}
More information about the llvm-commits
mailing list