aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/Casting.cpp
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2013-07-18 02:42:40 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2013-07-18 02:42:40 +0000
commit9ed1761aea2189b6a55c14bcbdd1dd7f508031ea (patch)
tree0230e9931022968aab78ac23630a1cce5e0e85a6 /llvm/unittests/Support/Casting.cpp
parent0dcefdfcab8975632c638a12bf252a38e9acd33b (diff)
downloadllvm-9ed1761aea2189b6a55c14bcbdd1dd7f508031ea.zip
llvm-9ed1761aea2189b6a55c14bcbdd1dd7f508031ea.tar.gz
llvm-9ed1761aea2189b6a55c14bcbdd1dd7f508031ea.tar.bz2
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. llvm-svn: 186559
Diffstat (limited to 'llvm/unittests/Support/Casting.cpp')
-rw-r--r--llvm/unittests/Support/Casting.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/llvm/unittests/Support/Casting.cpp b/llvm/unittests/Support/Casting.cpp
index 01583e4..362abee 100644
--- a/llvm/unittests/Support/Casting.cpp
+++ b/llvm/unittests/Support/Casting.cpp
@@ -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, InferredUpcastTakesPrecedence) {
} // 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()); }
+}