rsandifo-arm created this revision. rsandifo-arm added reviewers: sdesmalen, efriedma, rovka, rjmccall. Herald added subscribers: cfe-commits, psnobl, rkruppe, tschuett. Herald added a project: clang.
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.) Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D76088 Files: clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/SemaExprCXX.cpp clang/test/SemaCXX/sizeless-1.cpp Index: clang/test/SemaCXX/sizeless-1.cpp =================================================================== --- clang/test/SemaCXX/sizeless-1.cpp +++ clang/test/SemaCXX/sizeless-1.cpp @@ -395,6 +395,10 @@ 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 1; + throw local_int8; // expected-error {{cannot throw object of sizeless type 'svint8_t'}} + throw global_int8_ptr; // expected-error {{cannot throw pointer to object of sizeless type 'svint8_t'}} + 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(); Index: clang/lib/Sema/SemaExprCXX.cpp =================================================================== --- clang/lib/Sema/SemaExprCXX.cpp +++ clang/lib/Sema/SemaExprCXX.cpp @@ -950,10 +950,9 @@ isPointer = true; } if (!isPointer || !Ty->isVoidType()) { - if (RequireCompleteType(ThrowLoc, Ty, - isPointer ? diag::err_throw_incomplete_ptr - : diag::err_throw_incomplete, - E->getSourceRange())) + if (RequireCompleteSizedType(ThrowLoc, Ty, + diag::err_throw_incomplete_or_sizeless, + isPointer, E->getSourceRange())) return true; if (RequireNonAbstractType(ThrowLoc, ExceptionObjectTy, Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -7113,10 +7113,9 @@ : Error<"vector operands to the vector conditional must be the same type " "%diff{($ and $)|}0,1}">; -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_incomplete_or_sizeless : Error< + "cannot throw %select{|pointer to }0object of %select{incomplete|sizeless}1 " + "type %2">; def warn_throw_underaligned_obj : Warning< "underaligned exception object thrown">, InGroup<UnderalignedExceptionObject>;
Index: clang/test/SemaCXX/sizeless-1.cpp =================================================================== --- clang/test/SemaCXX/sizeless-1.cpp +++ clang/test/SemaCXX/sizeless-1.cpp @@ -395,6 +395,10 @@ 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 1; + throw local_int8; // expected-error {{cannot throw object of sizeless type 'svint8_t'}} + throw global_int8_ptr; // expected-error {{cannot throw pointer to object of sizeless type 'svint8_t'}} + 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(); Index: clang/lib/Sema/SemaExprCXX.cpp =================================================================== --- clang/lib/Sema/SemaExprCXX.cpp +++ clang/lib/Sema/SemaExprCXX.cpp @@ -950,10 +950,9 @@ isPointer = true; } if (!isPointer || !Ty->isVoidType()) { - if (RequireCompleteType(ThrowLoc, Ty, - isPointer ? diag::err_throw_incomplete_ptr - : diag::err_throw_incomplete, - E->getSourceRange())) + if (RequireCompleteSizedType(ThrowLoc, Ty, + diag::err_throw_incomplete_or_sizeless, + isPointer, E->getSourceRange())) return true; if (RequireNonAbstractType(ThrowLoc, ExceptionObjectTy, Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -7113,10 +7113,9 @@ : Error<"vector operands to the vector conditional must be the same type " "%diff{($ and $)|}0,1}">; -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_incomplete_or_sizeless : Error< + "cannot throw %select{|pointer to }0object of %select{incomplete|sizeless}1 " + "type %2">; def warn_throw_underaligned_obj : Warning< "underaligned exception object thrown">, InGroup<UnderalignedExceptionObject>;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits