[cfe-commits] r161934 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaType.cpp test/Sema/static-array.c

Hans Wennborg hans at hanshq.net
Wed Aug 15 00:42:30 PDT 2012


Author: hans
Date: Wed Aug 15 02:42:30 2012
New Revision: 161934

URL: http://llvm.org/viewvc/llvm-project?rev=161934&view=rev
Log:
Check for improper use of 'static' and type qualifiers in array
declarators.

They are only allowed for function parameters, and then only on the
outermost array type derivation.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/Sema/static-array.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=161934&r1=161933&r2=161934&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Aug 15 02:42:30 2012
@@ -3677,6 +3677,10 @@
   "'%0' declared as array of references of type %1">;
 def err_decl_negative_array_size : Error<
   "'%0' declared as an array with a negative size">;
+def err_array_static_outside_prototype : Error<
+  "%0 used in array declarator outside of function prototype">;
+def err_array_static_not_outermost : Error<
+  "%0 used in non-outermost array type derivation">;
 def err_array_star_outside_prototype : Error<
   "star modifier used outside of function prototype">;
 def err_illegal_decl_pointer_to_reference : Error<

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=161934&r1=161933&r2=161934&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Wed Aug 15 02:42:30 2012
@@ -2258,6 +2258,50 @@
         ASM = ArrayType::Normal;
         D.setInvalidType(true);
       }
+
+      // C99 6.7.5.2p1: The optional type qualifiers and the keyword static
+      // shall appear only in a declaration of a function parameter with an
+      // array type, ...
+      if (ASM == ArrayType::Static || ATI.TypeQuals) {
+        if (!D.isPrototypeContext()) {
+          S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) <<
+              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
+          // Remove the 'static' and the type qualifiers.
+          if (ASM == ArrayType::Static)
+            ASM = ArrayType::Normal;
+          ATI.TypeQuals = 0;
+          D.setInvalidType(true);
+        }
+
+        // C99 6.7.5.2p1: ... and then only in the outermost array type
+        // derivation.
+        unsigned x = chunkIndex;
+        while (x != 0) {
+          // Walk outwards along the declarator chunks.
+          x--;
+          const DeclaratorChunk &DC = D.getTypeObject(x);
+          switch (DC.Kind) {
+          case DeclaratorChunk::Paren:
+            continue;
+          case DeclaratorChunk::Array:
+          case DeclaratorChunk::Pointer:
+          case DeclaratorChunk::Reference:
+          case DeclaratorChunk::MemberPointer:
+            S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) <<
+              (ASM == ArrayType::Static ? "'static'" : "type qualifier");
+            if (ASM == ArrayType::Static)
+              ASM = ArrayType::Normal;
+            ATI.TypeQuals = 0;
+            D.setInvalidType(true);
+            break;
+          case DeclaratorChunk::Function:
+          case DeclaratorChunk::BlockPointer:
+            // These are invalid anyway, so just ignore.
+            break;
+          }
+        }
+      }
+
       T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
                            SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
       break;

Modified: cfe/trunk/test/Sema/static-array.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/static-array.c?rev=161934&r1=161933&r2=161934&view=diff
==============================================================================
--- cfe/trunk/test/Sema/static-array.c (original)
+++ cfe/trunk/test/Sema/static-array.c Wed Aug 15 02:42:30 2012
@@ -1,12 +1,9 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -fblocks -verify %s
 
 void cat0(int a[static 0]) {} // expected-warning {{'static' has no effect on zero-length arrays}}
 
 void cat(int a[static 3]) {} // expected-note 2 {{callee declares array parameter as static here}}
 
-typedef int i3[static 3];
-void tcat(i3 a) {}
-
 void vat(int i, int a[static i]) {} // expected-note {{callee declares array parameter as static here}}
 
 void f(int *p) {
@@ -20,12 +17,37 @@
   cat(c);
   cat(p);
 
-  tcat(0); // expected-warning {{null passed to a callee which requires a non-null argument}}
-  tcat(a); // expected-warning {{array argument is too small; contains 2 elements, callee requires at least 3}}
-  tcat(b);
-  tcat(c);
-  tcat(p);
-
   vat(1, 0); // expected-warning {{null passed to a callee which requires a non-null argument}}
   vat(3, b);
 }
+
+
+typedef int td[static 3]; // expected-error {{'static' used in array declarator outside of function prototype}}
+typedef void(*fp)(int[static 42]); // no-warning
+
+void g(void) {
+  int a[static 42]; // expected-error {{'static' used in array declarator outside of function prototype}}
+
+  int b[const 10]; // expected-error {{type qualifier used in array declarator outside of function prototype}}
+  int c[volatile 10]; // expected-error {{type qualifier used in array declarator outside of function prototype}}
+  int d[restrict 10]; // expected-error {{type qualifier used in array declarator outside of function prototype}}
+
+  int e[static restrict 1]; // expected-error {{'static' used in array declarator outside of function prototype}}
+}
+
+void h(int [static const 10][42]); // no-warning
+
+void i(int [10]
+       [static 42]); // expected-error {{'static' used in non-outermost array type derivation}}
+
+void j(int [10]
+       [const 42]); // expected-error {{type qualifier used in non-outermost array type derivation}}
+
+void k(int (*x)[static 10]); // expected-error {{'static' used in non-outermost array type derivation}}
+void l(int (x)[static 10]); // no-warning
+void m(int *x[static 10]); // no-warning
+void n(int *(x)[static 10]); // no-warning
+
+void o(int (x[static 10])(void)); // expected-error{{'x' declared as array of functions of type 'int (void)'}}
+void p(int (^x)[static 10]); // expected-error{{block pointer to non-function type is invalid}}
+void q(int (^x[static 10])()); // no-warning





More information about the cfe-commits mailing list