[Sema][SVE] Don't allow sizeless objects to be thrown
authorRichard Sandiford <richard.sandiford@arm.com>
Tue, 3 Mar 2020 10:45:45 +0000 (10:45 +0000)
committerRichard Sandiford <richard.sandiford@arm.com>
Tue, 17 Mar 2020 11:52:37 +0000 (11:52 +0000)
Summary:
The same rules for throwing and catching incomplete types also apply
to sizeless types.  This patch enforces that for throw statements.
It also make sure that we use "sizeless type" rather "incomplete type"
in the associated message.  (Both are correct, but "sizeless type" is
more specific and hopefully more user-friendly.)

The SVE ACLE simply extends the rule for incomplete types to
sizeless types.  However, throwing pointers to sizeless types
should not pose any real difficulty, so as an extension,
the clang implementation allows that.

Reviewers: sdesmalen, efriedma, rovka, rjmccall

Subscribers: tschuett, rkruppe, psnobl, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D76088

clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaExprCXX.cpp
clang/test/SemaCXX/sizeless-1.cpp

index b33e11e..1b82aba 100644 (file)
@@ -7120,6 +7120,8 @@ def err_throw_incomplete : Error<
   "cannot throw object of incomplete type %0">;
 def err_throw_incomplete_ptr : Error<
   "cannot throw pointer to object of incomplete type %0">;
+def err_throw_sizeless : Error<
+  "cannot throw object of sizeless type %0">;
 def warn_throw_underaligned_obj : Warning<
   "underaligned exception object thrown">,
   InGroup<UnderalignedExceptionObject>;
index f81aabb..5969b5a 100644 (file)
@@ -956,6 +956,11 @@ bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc,
                             E->getSourceRange()))
       return true;
 
+    if (!isPointer && Ty->isSizelessType()) {
+      Diag(ThrowLoc, diag::err_throw_sizeless) << Ty << E->getSourceRange();
+      return true;
+    }
+
     if (RequireNonAbstractType(ThrowLoc, ExceptionObjectTy,
                                diag::err_throw_abstract_type, E))
       return true;
index 876f593..af20755 100644 (file)
@@ -395,6 +395,9 @@ void cxx_only(int sel) {
   local_int16 = static_cast<svint16_t>(local_int8); // expected-error {{static_cast from 'svint8_t' (aka '__SVInt8_t') to 'svint16_t' (aka '__SVInt16_t') is not allowed}}
   sel = static_cast<int>(local_int8);               // expected-error {{static_cast from 'svint8_t' (aka '__SVInt8_t') to 'int' is not allowed}}
 
+  throw local_int8; // expected-error {{cannot throw object of sizeless type 'svint8_t'}}
+  throw global_int8_ptr;
+
   local_int8.~__SVInt8_t(); // expected-error {{object expression of non-scalar type 'svint8_t' (aka '__SVInt8_t') cannot be used in a pseudo-destructor expression}}
 
   (void)svint8_t();