[llvm] 23d6f31 - Add unit test coverage for cast<T> assertion failures on invalid cast

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 17 17:36:37 PDT 2022


Author: David Blaikie
Date: 2022-06-18T00:36:12Z
New Revision: 23d6f31a90a1631e7202a6ae6aac19c8245a8da1

URL: https://github.com/llvm/llvm-project/commit/23d6f31a90a1631e7202a6ae6aac19c8245a8da1
DIFF: https://github.com/llvm/llvm-project/commit/23d6f31a90a1631e7202a6ae6aac19c8245a8da1.diff

LOG: Add unit test coverage for cast<T> assertion failures on invalid cast

Added: 
    

Modified: 
    llvm/unittests/Support/Casting.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Support/Casting.cpp b/llvm/unittests/Support/Casting.cpp
index 086548f1e212..93a5e67db0f4 100644
--- a/llvm/unittests/Support/Casting.cpp
+++ b/llvm/unittests/Support/Casting.cpp
@@ -496,4 +496,42 @@ TEST(CastingTest, smart_dyn_cast_or_null) {
 }
 
 } // end namespace pointer_wrappers
+
+#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()";
+}
+
+} // end namespace assertion_checks
+#endif
 } // end namespace


        


More information about the llvm-commits mailing list