#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 <cstdlib>
namespace llvm {
struct IllegalCast;
template <typename T> IllegalCast *cast(...) { … }
struct bar { … };
struct foo { … };
struct base { … };
struct derived : public base { … };
struct derived_nocast : public base { … };
template <> struct isa_impl<foo, bar> { … };
isa_impl<foo, T>;
foo *bar::baz() { … }
foo *bar::caz() { … }
foo *bar::daz() { … }
foo *bar::naz() { … }
bar *fub();
template <> struct simplify_type<foo> { … };
struct T1 { … };
struct T2 { … };
template <> struct CastInfo<T2, T1> : public OptionalValueCast<T2, T1> { … };
struct T3 { … };
template <> struct CastInfo<T3, T1 *> : public ValueFromPointerCast<T3, T1> { … };
struct T4 { … };
template <> struct ValueIsPresent<T3> { … };
template <> struct CastInfo<T4, T3> { … };
}
usingnamespacellvm;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
namespace {
const foo *null_foo = …;
bar B;
extern bar &B1;
bar &B1 = …;
extern const bar *B2;
const bar &B3 = …;
const bar *const B4 = …;
TEST(CastingTest, isa) { … }
TEST(CastingTest, isa_and_nonnull) { … }
TEST(CastingTest, cast) { … }
TEST(CastingTest, cast_or_null) { … }
TEST(CastingTest, dyn_cast) { … }
TEST(CastingTest, dyn_cast_or_null) { … }
TEST(CastingTest, dyn_cast_value_types) { … }
TEST(CastingTest, dyn_cast_if_present) { … }
TEST(CastingTest, isa_check_predicates) { … }
std::unique_ptr<derived> newd() { … }
std::unique_ptr<base> newb() { … }
TEST(CastingTest, unique_dyn_cast) { … }
const bar *B2 = …;
}
bar *llvm::fub() { … }
namespace {
namespace inferred_upcasting {
class Base { … };
class Derived : public Base { … };
TEST(CastingTest, UpcastIsInferred) { … }
class UseInferredUpcast { … };
TEST(CastingTest, InferredUpcastTakesPrecedence) { … }
}
}
namespace {
namespace pointer_wrappers {
struct Base { … };
struct Derived : Base { … };
class PTy { … };
}
}
namespace llvm {
template <> struct ValueIsPresent<pointer_wrappers::PTy> { … };
template <> struct ValueIsPresent<const pointer_wrappers::PTy> { … };
template <> struct simplify_type<pointer_wrappers::PTy> { … };
template <> struct simplify_type<const pointer_wrappers::PTy> { … };
}
namespace {
namespace pointer_wrappers {
pointer_wrappers::Base B;
pointer_wrappers::Derived D;
pointer_wrappers::PTy MN(nullptr);
pointer_wrappers::PTy MB(&B);
pointer_wrappers::PTy MD(&D);
const pointer_wrappers::PTy CN(nullptr);
const pointer_wrappers::PTy CB(&B);
const pointer_wrappers::PTy CD(&D);
TEST(CastingTest, smart_isa) { … }
TEST(CastingTest, smart_cast) { … }
TEST(CastingTest, smart_cast_or_null) { … }
TEST(CastingTest, smart_dyn_cast) { … }
TEST(CastingTest, smart_dyn_cast_or_null) { … }
}
#ifndef NDEBUG
namespace assertion_checks {
struct Base {
virtual ~Base() {}
};
struct Derived : public Base {
static bool classof(const Base *B) { return false; }
};
TEST(CastingTest, assertion_check_const_ref) {
const Base B;
EXPECT_DEATH((void)cast<Derived>(B), "argument of incompatible type")
<< "Invalid cast of const ref did not cause an abort()";
}
TEST(CastingTest, assertion_check_ref) {
Base B;
EXPECT_DEATH((void)cast<Derived>(B), "argument of incompatible type")
<< "Invalid cast of const ref did not cause an abort()";
}
TEST(CastingTest, assertion_check_ptr) {
Base B;
EXPECT_DEATH((void)cast<Derived>(&B), "argument of incompatible type")
<< "Invalid cast of const ref did not cause an abort()";
}
TEST(CastingTest, assertion_check_unique_ptr) {
auto B = std::make_unique<Base>();
EXPECT_DEATH((void)cast<Derived>(std::move(B)),
"argument of incompatible type")
<< "Invalid cast of const ref did not cause an abort()";
}
}
#endif
}