X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=unittests%2FSupport%2FCasting.cpp;h=88c7d19aa464bfe1af80f0d6fc1e4a1a9d407757;hb=0e9c68e6bc8768143308b0162e900ba8bd10dc01;hp=22badb47c7ce4711996712bf039a179e13b79c5e;hpb=08993c03abed359171d12b50ab38da0ac331cfb7;p=oota-llvm.git diff --git a/unittests/Support/Casting.cpp b/unittests/Support/Casting.cpp index 22badb47c7c..88c7d19aa46 100644 --- a/unittests/Support/Casting.cpp +++ b/unittests/Support/Casting.cpp @@ -1,4 +1,4 @@ -//===---------- llvm/unittest/Support/Casting.cpp - Casting tests --------===// +//===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===// // // The LLVM Compiler Infrastructure // @@ -7,21 +7,94 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Support/raw_ostream.h" -#include "llvm/Support/Debug.h" -#define DEBUG_CAST_OPERATORS #include "llvm/Support/Casting.h" - +#include "llvm/IR/User.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" #include +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 IllegalCast *cast(...) { return nullptr; } + +// set up two example classes +// with conversion facility +// +struct bar { + bar() {} + struct foo *baz(); + struct foo *caz(); + struct foo *daz(); + struct foo *naz(); +private: + bar(const bar &); +}; +struct foo { + void ext() const; + /* static bool classof(const bar *X) { + cerr << "Classof: " << X << "\n"; + return true; + }*/ +}; + +template <> struct isa_impl { + static inline bool doit(const bar &Val) { + dbgs() << "Classof: " << &Val << "\n"; + return true; + } +}; + +foo *bar::baz() { + return cast(this); +} + +foo *bar::caz() { + return cast_or_null(this); +} + +foo *bar::daz() { + return dyn_cast(this); +} + +foo *bar::naz() { + return dyn_cast_or_null(this); +} + + +bar *fub(); + +template <> struct simplify_type { + 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. +static_assert(std::is_same::SimpleType, Value *>::value, + "Use doesn't simplify correctly!"); +static_assert(std::is_same::SimpleType, Value *>::value, + "Use doesn't simplify correctly!"); + +// Test that a regular class behaves as expected. +static_assert(std::is_same::SimpleType, int>::value, + "Unexpected simplify_type result!"); +static_assert(std::is_same::SimpleType, foo *>::value, + "Unexpected simplify_type result!"); + namespace { -const foo *null_foo = NULL; +const foo *null_foo = nullptr; +bar B; extern bar &B1; +bar &B1 = B; extern const bar *B2; // test various configurations of const const bar &B3 = B1; @@ -41,12 +114,15 @@ TEST(CastingTest, cast) { EXPECT_NE(F3, null_foo); const foo *F4 = cast(B2); EXPECT_NE(F4, null_foo); - const foo &F8 = cast(B3); - EXPECT_NE(&F8, null_foo); - const foo *F9 = cast(B4); - EXPECT_NE(F9, null_foo); - foo *F10 = cast(fub()); - EXPECT_EQ(F10, null_foo); + const foo &F5 = cast(B3); + EXPECT_NE(&F5, null_foo); + const foo *F6 = cast(B4); + EXPECT_NE(F6, null_foo); + // Can't pass null pointer to cast<>. + // foo *F7 = cast(fub()); + // EXPECT_EQ(F7, null_foo); + foo *F8 = B1.baz(); + EXPECT_NE(F8, null_foo); } TEST(CastingTest, cast_or_null) { @@ -58,9 +134,101 @@ TEST(CastingTest, cast_or_null) { EXPECT_NE(F13, null_foo); const foo *F14 = cast_or_null(fub()); // Shouldn't print. EXPECT_EQ(F14, null_foo); + foo *F15 = B1.caz(); + EXPECT_NE(F15, null_foo); } -bar B; -bar &B1 = B; +TEST(CastingTest, dyn_cast) { + const foo *F1 = dyn_cast(B2); + EXPECT_NE(F1, null_foo); + const foo *F2 = dyn_cast(B2); + EXPECT_NE(F2, null_foo); + const foo *F3 = dyn_cast(B4); + EXPECT_NE(F3, null_foo); + // Can't pass null pointer to dyn_cast<>. + // foo *F4 = dyn_cast(fub()); + // EXPECT_EQ(F4, null_foo); + foo *F5 = B1.daz(); + EXPECT_NE(F5, null_foo); +} + +TEST(CastingTest, dyn_cast_or_null) { + const foo *F1 = dyn_cast_or_null(B2); + EXPECT_NE(F1, null_foo); + const foo *F2 = dyn_cast_or_null(B2); + EXPECT_NE(F2, null_foo); + const foo *F3 = dyn_cast_or_null(B4); + EXPECT_NE(F3, null_foo); + foo *F4 = dyn_cast_or_null(fub()); + EXPECT_EQ(F4, null_foo); + foo *F5 = B1.naz(); + EXPECT_NE(F5, null_foo); +} + +// These lines are errors... +//foo *F20 = cast(B2); // Yields const foo* +//foo &F21 = cast(B3); // Yields const foo& +//foo *F22 = cast(B4); // Yields const foo* +//foo &F23 = cast_or_null(B1); +//const foo &F24 = cast_or_null(B3); + const bar *B2 = &B; } // anonymous namespace + +bar *llvm::fub() { return nullptr; } + +namespace { +namespace inferred_upcasting { +// This test case verifies correct behavior of inferred upcasts when the +// types are statically known to be OK to upcast. This is the case when, +// for example, Derived inherits from Base, and we do `isa(Derived)`. + +// Note: This test will actually fail to compile without inferred +// upcasting. + +class Base { +public: + // No classof. We are testing that the upcast is inferred. + Base() {} +}; + +class Derived : public Base { +public: + Derived() {} +}; + +// Even with no explicit classof() in Base, we should still be able to cast +// Derived to its base class. +TEST(CastingTest, UpcastIsInferred) { + Derived D; + EXPECT_TRUE(isa(D)); + Base *BP = dyn_cast(&D); + EXPECT_TRUE(BP != nullptr); +} + + +// This test verifies that the inferred upcast takes precedence over an +// explicitly written one. This is important because it verifies that the +// dynamic check gets optimized away. +class UseInferredUpcast { +public: + int Dummy; + static bool classof(const UseInferredUpcast *) { + return false; + } +}; + +TEST(CastingTest, InferredUpcastTakesPrecedence) { + UseInferredUpcast UIU; + // Since the explicit classof() returns false, this will fail if the + // explicit one is used. + EXPECT_TRUE(isa(&UIU)); +} + +} // 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(pod()); } +}