1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 11:13:28 +01:00
llvm-mirror/unittests/Support/ExtensibleRTTITest.cpp
Lang Hames ac99f6d165 [Support] Add support RTTI support for open class hierarchies.
This patch extracts the RTTI part of llvm::ErrorInfo into its own class
(RTTIExtends) so that it can be used in other non-error hierarchies, and makes
it compatible with the existing LLVM RTTI function templates (isa, cast,
dyn_cast, dyn_cast_or_null) by adding the classof method.

Differential Revision: https://reviews.llvm.org/D39111
2020-04-13 12:52:44 -07:00

87 lines
2.0 KiB
C++

//===------ unittests/ExtensibleRTTITest.cpp - Extensible RTTI Tests ------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/ExtensibleRTTI.h"
#include "llvm/Support/Casting.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
class MyBaseType : public RTTIExtends<MyBaseType, RTTIRoot> {
public:
static char ID;
};
class MyDerivedType : public RTTIExtends<MyDerivedType, MyBaseType> {
public:
static char ID;
};
class MyOtherDerivedType : public RTTIExtends<MyOtherDerivedType, MyBaseType> {
public:
static char ID;
};
class MyDeeperDerivedType
: public RTTIExtends<MyDeeperDerivedType, MyDerivedType> {
public:
static char ID;
};
char MyBaseType::ID = 0;
char MyDerivedType::ID = 0;
char MyOtherDerivedType::ID = 0;
char MyDeeperDerivedType::ID = 0;
TEST(ExtensibleRTTI, isa) {
MyBaseType B;
MyDerivedType D;
MyDeeperDerivedType DD;
EXPECT_TRUE(isa<MyBaseType>(B));
EXPECT_FALSE(isa<MyDerivedType>(B));
EXPECT_FALSE(isa<MyOtherDerivedType>(B));
EXPECT_FALSE(isa<MyDeeperDerivedType>(B));
EXPECT_TRUE(isa<MyBaseType>(D));
EXPECT_TRUE(isa<MyDerivedType>(D));
EXPECT_FALSE(isa<MyOtherDerivedType>(D));
EXPECT_FALSE(isa<MyDeeperDerivedType>(D));
EXPECT_TRUE(isa<MyBaseType>(DD));
EXPECT_TRUE(isa<MyDerivedType>(DD));
EXPECT_FALSE(isa<MyOtherDerivedType>(DD));
EXPECT_TRUE(isa<MyDeeperDerivedType>(DD));
}
TEST(ExtensibleRTTI, cast) {
MyDerivedType D;
MyBaseType &BD = D;
cast<MyBaseType>(D);
cast<MyBaseType>(BD);
cast<MyDerivedType>(BD);
}
TEST(ExtensibleRTTI, dyn_cast) {
MyBaseType B;
MyDerivedType D;
MyBaseType &BD = D;
EXPECT_EQ(dyn_cast<MyDerivedType>(&B), nullptr);
EXPECT_EQ(dyn_cast<MyDerivedType>(&D), &D);
EXPECT_EQ(dyn_cast<MyBaseType>(&BD), &BD);
EXPECT_EQ(dyn_cast<MyDerivedType>(&BD), &D);
}
} // namespace