[clang] f8700db - [Sema][SVE] Don't allow static or thread-local variables to have sizeless type
Richard Sandiford via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 12 10:39:57 PDT 2020
Author: Richard Sandiford
Date: 2020-03-12T17:39:29Z
New Revision: f8700db7f150287b71453e4ae10dea79a2e4fb03
URL: https://github.com/llvm/llvm-project/commit/f8700db7f150287b71453e4ae10dea79a2e4fb03
DIFF: https://github.com/llvm/llvm-project/commit/f8700db7f150287b71453e4ae10dea79a2e4fb03.diff
LOG: [Sema][SVE] Don't allow static or thread-local variables to have sizeless type
clang accepts a TU containing just:
__SVInt8_t x;
However, sizeless types are not allowed to have static or thread-local
storage duration and trying to code-generate the TU triggers an LLVM
fatal error:
Globals cannot contain scalable vectors
<vscale x 16 x i8>* @x
fatal error: error in backend: Broken module found, compilation aborted!
This patch adds an associated clang diagnostic.
Differential Revision: https://reviews.llvm.org/D75736
Added:
Modified:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaDecl.cpp
clang/test/Sema/sizeless-1.c
clang/test/SemaCXX/sizeless-1.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 830787e2663c..5312c1987f56 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -9145,6 +9145,8 @@ def err_block_on_nonlocal : Error<
"__block attribute not allowed, only allowed on local variables">;
def err_block_on_vm : Error<
"__block attribute not allowed on declaration with a variably modified type">;
+def err_sizeless_nonlocal : Error<
+ "non-local variable with sizeless type %0">;
def err_vec_builtin_non_vector : Error<
"first two arguments to %0 must be vectors">;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index da40ac188e54..7d56109b7338 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7939,6 +7939,12 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
return;
}
+ if (!NewVD->hasLocalStorage() && T->isSizelessType()) {
+ Diag(NewVD->getLocation(), diag::err_sizeless_nonlocal) << T;
+ NewVD->setInvalidDecl();
+ return;
+ }
+
if (isVM && NewVD->hasAttr<BlocksAttr>()) {
Diag(NewVD->getLocation(), diag::err_block_on_vm);
NewVD->setInvalidDecl();
diff --git a/clang/test/Sema/sizeless-1.c b/clang/test/Sema/sizeless-1.c
index c33aba8e2e55..f1f562938dbd 100644
--- a/clang/test/Sema/sizeless-1.c
+++ b/clang/test/Sema/sizeless-1.c
@@ -5,6 +5,10 @@
typedef __SVInt8_t svint8_t;
typedef __SVInt16_t svint16_t;
+svint8_t global_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
+extern svint8_t extern_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
+static svint8_t static_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
+__thread svint8_t thread_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
svint8_t *global_int8_ptr;
extern svint8_t *extern_int8_ptr;
static svint8_t *static_int8_ptr;
@@ -48,6 +52,8 @@ void unused() {
struct incomplete_struct *incomplete_ptr;
void func(int sel) {
+ static svint8_t static_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
+
svint8_t local_int8;
svint16_t local_int16;
diff --git a/clang/test/SemaCXX/sizeless-1.cpp b/clang/test/SemaCXX/sizeless-1.cpp
index dda36fdbd0d4..11cc629620d6 100644
--- a/clang/test/SemaCXX/sizeless-1.cpp
+++ b/clang/test/SemaCXX/sizeless-1.cpp
@@ -10,6 +10,10 @@ struct type_info;
typedef __SVInt8_t svint8_t;
typedef __SVInt16_t svint16_t;
+svint8_t global_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
+extern svint8_t extern_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
+static svint8_t static_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
+__thread svint8_t thread_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
svint8_t *global_int8_ptr;
extern svint8_t *extern_int8_ptr;
static svint8_t *static_int8_ptr;
@@ -58,6 +62,8 @@ void unused() {
struct incomplete_struct *incomplete_ptr;
void func(int sel) {
+ static svint8_t static_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
+
svint8_t local_int8;
svint16_t local_int16;
More information about the cfe-commits
mailing list