[cfe-commits] r149695 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDecl.cpp test/Sema/anonymous-struct-union-c11.c test/Sema/array-init.c

Hans Wennborg hans at hanshq.net
Fri Feb 3 07:47:05 PST 2012


Author: hans
Date: Fri Feb  3 09:47:04 2012
New Revision: 149695

URL: http://llvm.org/viewvc/llvm-project?rev=149695&view=rev
Log:
Don't warn about anonymous struct/union in C11.

Also, in C, call this a C11 extension rather than a GNU extension.

Added:
    cfe/trunk/test/Sema/anonymous-struct-union-c11.c
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/array-init.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=149695&r1=149694&r2=149695&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Feb  3 09:47:04 2012
@@ -4570,9 +4570,11 @@
 
 // C++ anonymous unions and GNU anonymous structs/unions
 def ext_anonymous_union : Extension<
-  "anonymous unions are a GNU extension in C">, InGroup<GNU>;
-def ext_anonymous_struct : Extension<
+  "anonymous unions are a C11 extension">, InGroup<C11>;
+def ext_gnu_anonymous_struct : Extension<
   "anonymous structs are a GNU extension">, InGroup<GNU>;
+def ext_c11_anonymous_struct : Extension<
+  "anonymous structs are a C11 extension">, InGroup<C11>;
 def err_anonymous_union_not_static : Error<
   "anonymous unions at namespace or global scope must be declared 'static'">;
 def err_anonymous_union_with_storage_spec : Error<

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=149695&r1=149694&r2=149695&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Feb  3 09:47:04 2012
@@ -2679,18 +2679,20 @@
 
 /// BuildAnonymousStructOrUnion - Handle the declaration of an
 /// anonymous structure or union. Anonymous unions are a C++ feature
-/// (C++ [class.union]) and a GNU C extension; anonymous structures
-/// are a GNU C and GNU C++ extension.
+/// (C++ [class.union]) and a C11 feature; anonymous structures
+/// are a C11 feature and GNU C++ extension.
 Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS,
                                              AccessSpecifier AS,
                                              RecordDecl *Record) {
   DeclContext *Owner = Record->getDeclContext();
 
   // Diagnose whether this anonymous struct/union is an extension.
-  if (Record->isUnion() && !getLangOptions().CPlusPlus)
+  if (Record->isUnion() && !getLangOptions().CPlusPlus && !getLangOptions().C11)
     Diag(Record->getLocation(), diag::ext_anonymous_union);
-  else if (!Record->isUnion())
-    Diag(Record->getLocation(), diag::ext_anonymous_struct);
+  else if (!Record->isUnion() && getLangOptions().CPlusPlus)
+    Diag(Record->getLocation(), diag::ext_gnu_anonymous_struct);
+  else if (!Record->isUnion() && !getLangOptions().C11)
+    Diag(Record->getLocation(), diag::ext_c11_anonymous_struct);
 
   // C and C++ require different kinds of checks for anonymous
   // structs/unions.

Added: cfe/trunk/test/Sema/anonymous-struct-union-c11.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/anonymous-struct-union-c11.c?rev=149695&view=auto
==============================================================================
--- cfe/trunk/test/Sema/anonymous-struct-union-c11.c (added)
+++ cfe/trunk/test/Sema/anonymous-struct-union-c11.c Fri Feb  3 09:47:04 2012
@@ -0,0 +1,19 @@
+// Check for warnings in non-C11 mode:
+// RUN: %clang_cc1 -fsyntax-only -verify -Wc11-extensions %s
+
+// Expect no warnings in C11 mode:
+// RUN: %clang_cc1 -fsyntax-only -pedantic -Werror -std=c11 %s
+
+struct s {
+  int a;
+  struct { // expected-warning{{anonymous structs are a C11 extension}}
+    int b;
+  };
+};
+
+struct t {
+  int a;
+  union { // expected-warning{{anonymous unions are a C11 extension}}
+    int b;
+  };
+};

Modified: cfe/trunk/test/Sema/array-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/array-init.c?rev=149695&r1=149694&r2=149695&view=diff
==============================================================================
--- cfe/trunk/test/Sema/array-init.c (original)
+++ cfe/trunk/test/Sema/array-init.c Fri Feb  3 09:47:04 2012
@@ -245,7 +245,7 @@
 const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \
 // expected-warning{{excess elements in struct initializer}}
 
-struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a GNU extension in C}}
+struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}
 typedef struct _Matrix Matrix;
 void test_matrix() {
   const Matrix mat1 = {





More information about the cfe-commits mailing list