[clang] 72ffb16 - [Sema][SVE] Don't allow sizeless types to be caught

Richard Sandiford via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 17 05:01:04 PDT 2020


Author: Richard Sandiford
Date: 2020-03-17T12:00:16Z
New Revision: 72ffb16b4cde972dd3b0b5db98d1702fdcccbc6f

URL: https://github.com/llvm/llvm-project/commit/72ffb16b4cde972dd3b0b5db98d1702fdcccbc6f
DIFF: https://github.com/llvm/llvm-project/commit/72ffb16b4cde972dd3b0b5db98d1702fdcccbc6f.diff

LOG: [Sema][SVE] Don't allow sizeless types to be caught

In the current SVE ACLE spec, the usual rules for throwing and
catching incomplete types also apply to sizeless types.  However,
throwing pointers to sizeless types should not pose any real difficulty,
so as an extension, the clang implementation allows that.

This patch enforces these rules for catch statements.

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

Added: 
    

Modified: 
    clang/include/clang/Basic/DiagnosticSemaKinds.td
    clang/lib/Sema/SemaDeclCXX.cpp
    clang/test/SemaCXX/sizeless-1.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1b82abadd8a9..3e0591bc2cf0 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7015,6 +7015,8 @@ def err_catch_incomplete_ptr : Error<
 def err_catch_incomplete_ref : Error<
   "cannot catch reference to incomplete type %0">;
 def err_catch_incomplete : Error<"cannot catch incomplete type %0">;
+def err_catch_sizeless : Error<
+  "cannot catch %select{|reference to }0sizeless type %1">;
 def err_catch_rvalue_ref : Error<"cannot catch exceptions by rvalue reference">;
 def err_catch_variably_modified : Error<
   "cannot catch variably modified type %0">;

diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 4ffa84604fbb..2cc770e5f3d1 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -15564,6 +15564,11 @@ VarDecl *Sema::BuildExceptionDeclaration(Scope *S,
       !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK))
     Invalid = true;
 
+  if (!Invalid && Mode != 1 && BaseType->isSizelessType()) {
+    Diag(Loc, diag::err_catch_sizeless) << (Mode == 2 ? 1 : 0) << BaseType;
+    Invalid = true;
+  }
+
   if (!Invalid && !ExDeclType->isDependentType() &&
       RequireNonAbstractType(Loc, ExDeclType,
                              diag::err_abstract_type_in_decl,

diff  --git a/clang/test/SemaCXX/sizeless-1.cpp b/clang/test/SemaCXX/sizeless-1.cpp
index af207550eb37..3b0b73a26eed 100644
--- a/clang/test/SemaCXX/sizeless-1.cpp
+++ b/clang/test/SemaCXX/sizeless-1.cpp
@@ -398,6 +398,19 @@ void cxx_only(int sel) {
   throw local_int8; // expected-error {{cannot throw object of sizeless type 'svint8_t'}}
   throw global_int8_ptr;
 
+  try {
+  } catch (int) {
+  }
+  try {
+  } catch (svint8_t) { // expected-error {{cannot catch sizeless type 'svint8_t'}}
+  }
+  try {
+  } catch (svint8_t *) {
+  }
+  try {
+  } catch (svint8_t &) { // expected-error {{cannot catch reference to 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();


        


More information about the cfe-commits mailing list