Skip to content

Commit

Permalink
[Sema][SVE] Don't allow sizeless objects to be thrown
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rsandifo-arm committed Mar 17, 2020
1 parent f5e0f8b commit c47f971
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -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>;
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Sema/SemaExprCXX.cpp
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions clang/test/SemaCXX/sizeless-1.cpp
Expand Up @@ -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();
Expand Down

0 comments on commit c47f971

Please sign in to comment.