diff options
author | Sean Silva <silvas@purdue.edu> | 2012-10-11 23:30:40 +0000 |
---|---|---|
committer | Sean Silva <silvas@purdue.edu> | 2012-10-11 23:30:40 +0000 |
commit | 35dd8779fa5391255dd4530badb2b642b24848cc (patch) | |
tree | 85e4a35784f1923b44231cb7ce0b3a896adbb404 /llvm/unittests/Support | |
parent | bead14e9de1641f3f669548009c4b279f2693274 (diff) | |
download | bcm5719-llvm-35dd8779fa5391255dd4530badb2b642b24848cc.tar.gz bcm5719-llvm-35dd8779fa5391255dd4530badb2b642b24848cc.zip |
Casting.h: Automatically handle isa<Base>(Derived).
Additionally, all such cases are handled with no dynamic check.
All `classof()` of the form
class Foo {
[...]
static bool classof(const Bar *) { return true; }
[...]
}
where Foo is an ancestor of Bar are no longer necessary.
Don't write them!
Note: The exact test is `is_base_of<Foo, Bar>`, which is non-strict, so
that Foo is considered an ancestor of itself.
This leads to the following rule of thumb for LLVM-style RTTI:
The argument type of `classof()` should be a strict ancestor.
For more information about implementing LLVM-style RTTI, see
docs/HowToSetUpLLVMStyleRTTI.rst
llvm-svn: 165765
Diffstat (limited to 'llvm/unittests/Support')
-rw-r--r-- | llvm/unittests/Support/Casting.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/llvm/unittests/Support/Casting.cpp b/llvm/unittests/Support/Casting.cpp index dc0205f325f..ad564aa366d 100644 --- a/llvm/unittests/Support/Casting.cpp +++ b/llvm/unittests/Support/Casting.cpp @@ -153,3 +153,54 @@ const bar *B2 = &B; } // anonymous namespace bar *llvm::fub() { return 0; } + +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<Base>(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<Base>(D)); + Base *BP = dyn_cast<Base>(&D); + EXPECT_TRUE(BP != NULL); +} + + +// 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<UseInferredUpcast>(&UIU)); +} + +} // end namespace inferred_upcasting +} // end anonymous namespace |