Author: Aaron Ballman Date: 2020-07-10T15:58:11-04:00 New Revision: 006c49d890da633d1ce502117fc2a49863cd65b7
URL: https://github.com/llvm/llvm-project/commit/006c49d890da633d1ce502117fc2a49863cd65b7 DIFF: https://github.com/llvm/llvm-project/commit/006c49d890da633d1ce502117fc2a49863cd65b7.diff LOG: Change behavior with zero-sized static array extents Currently, Clang previously diagnosed this code by default: void f(int a[static 0]); saying that "static has no effect on zero-length arrays", which was accurate. However, static array extents require that the caller of the function pass a nonnull pointer to an array of *at least* that number of elements, but it can pass more (see C17 6.7.6.3p6). Given that we allow zero-sized arrays as a GNU extension and that it's valid to pass more elements than specified by the static array extent, we now support zero-sized static array extents with the usual semantics because it can be useful in cases like: void my_bzero(char p[static 0], int n); my_bzero(&c+1, 0); //ok my_bzero(t+k,n-k); //ok, pattern from actual code Added: Modified: clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/CodeGen/CGCall.cpp clang/lib/Sema/SemaType.cpp clang/test/CodeGen/vla.c clang/test/Sema/static-array.c Removed: ################################################################################ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 29408be6881f..24e942037ecf 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -5395,9 +5395,6 @@ def ext_typecheck_zero_array_size : Extension< "zero size arrays are an extension">, InGroup<ZeroLengthArray>; def err_typecheck_zero_array_size : Error< "zero-length arrays are not permitted in C++">; -def warn_typecheck_zero_static_array_size : Warning< - "'static' has no effect on zero-length arrays">, - InGroup<ArrayBounds>; def err_array_size_non_int : Error<"size of array has non-integer type %0">; def err_init_element_not_constant : Error< "initializer element is not a compile-time constant">; diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index 7af986981e5b..e8235c775d8f 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -2504,9 +2504,11 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, ArrSize) { llvm::AttrBuilder Attrs; Attrs.addDereferenceableAttr( - getContext().getTypeSizeInChars(ETy).getQuantity()*ArrSize); + getContext().getTypeSizeInChars(ETy).getQuantity() * + ArrSize); AI->addAttrs(Attrs); - } else if (getContext().getTargetAddressSpace(ETy) == 0 && + } else if (getContext().getTargetInfo().getNullPointerValue( + ETy.getAddressSpace()) == 0 && !CGM.getCodeGenOpts().NullPointerIsValid) { AI->addAttr(llvm::Attribute::NonNull); } diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index b8a787f010d6..b8f7f1a58159 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -2393,13 +2393,6 @@ QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, ? diag::err_typecheck_zero_array_size : diag::ext_typecheck_zero_array_size) << ArraySize->getSourceRange(); - - if (ASM == ArrayType::Static) { - Diag(ArraySize->getBeginLoc(), - diag::warn_typecheck_zero_static_array_size) - << ArraySize->getSourceRange(); - ASM = ArrayType::Normal; - } } else if (!T->isDependentType() && !T->isVariablyModifiedType() && !T->isIncompleteType() && !T->isUndeducedType()) { // Is the array too large? diff --git a/clang/test/CodeGen/vla.c b/clang/test/CodeGen/vla.c index 37243cd17290..16b82f4acc7d 100644 --- a/clang/test/CodeGen/vla.c +++ b/clang/test/CodeGen/vla.c @@ -206,3 +206,7 @@ void test9(int n, int a[static n]) { } // NULL-INVALID: define void @test9(i32 %n, i32* nonnull %a) // NULL-VALID: define void @test9(i32 %n, i32* %a) +// Make sure a zero-sized static array extent is still required to be nonnull. +void test10(int a[static 0]) {} +// NULL-INVALID: define void @test10(i32* nonnull %a) +// NULL-VALID: define void @test10(i32* %a) diff --git a/clang/test/Sema/static-array.c b/clang/test/Sema/static-array.c index cc1043fe9c47..ef070718dc63 100644 --- a/clang/test/Sema/static-array.c +++ b/clang/test/Sema/static-array.c @@ -1,6 +1,7 @@ -// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 -fsyntax-only -fblocks -verify %s +// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 -fsyntax-only -fblocks -pedantic -verify %s -void cat0(int a[static 0]) {} // expected-warning {{'static' has no effect on zero-length arrays}} +void cat0(int a[static 0]) {} // expected-warning {{zero size arrays are an extension}} \ + // expected-note {{callee declares array parameter as static here}} void cat(int a[static 3]) {} // expected-note 4 {{callee declares array parameter as static here}} expected-note 2 {{passing argument to parameter 'a' here}} @@ -9,7 +10,7 @@ void vat(int i, int a[static i]) {} // expected-note {{callee declares array par void f(int *p) { int a[2], b[3], c[4]; - cat0(0); + cat0(0); // expected-warning {{null passed to a callee that requires a non-null argument}} cat(0); // expected-warning {{null passed to a callee that requires a non-null argument}} cat(a); // expected-warning {{array argument is too small; contains 2 elements, callee requires at least 3}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits