[PATCH] Fix recognition of empty structures/unions

Serge Pavlov sepavloff at gmail.com
Thu Mar 28 07:59:16 PDT 2013


  Fix recognition of empty structs/unions

http://llvm-reviews.chandlerc.com/D578

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D578?vs=1371&id=1426#toc

Files:
  include/clang/Basic/DiagnosticParseKinds.td
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Parse/ParseDecl.cpp
  lib/Sema/SemaDecl.cpp
  test/Parser/declarators.c
  test/Sema/array-init.c
  test/Sema/empty1.c
  test/Sema/empty2.c

Index: include/clang/Basic/DiagnosticParseKinds.td
===================================================================
--- include/clang/Basic/DiagnosticParseKinds.td
+++ include/clang/Basic/DiagnosticParseKinds.td
@@ -50,10 +50,6 @@
   "complex integer types are a GNU extension">, InGroup<GNU>;
 def ext_thread_before : Extension<"'__thread' before '%0'">;
 
-def ext_empty_struct_union : Extension<
-  "empty %select{struct|union}0 is a GNU extension">, InGroup<GNU>;
-def warn_empty_struct_union_compat : Warning<"empty %select{struct|union}0 "
-  "has size 0 in C, size 1 in C++">, InGroup<CXXCompat>, DefaultIgnore;
 def error_empty_enum : Error<"use of empty enum">;
 def err_invalid_sign_spec : Error<"'%0' cannot be signed or unsigned">;
 def err_invalid_short_spec : Error<"'short %0' is invalid">;
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -5286,6 +5286,12 @@
 
 def err_typecheck_cond_incompatible_operands_null : Error<
   "non-pointer operand type %0 incompatible with %select{NULL|nullptr}1">;
+def ext_empty_struct_union : Extension<
+  "empty %select{struct|union}0 is a GNU extension">, InGroup<GNU>;
+def ext_no_named_members_in_struct_union : Extension<
+  "%select{struct|union}0 without named members is a GNU extension">, InGroup<GNU>;
+def warn_empty_struct_union_compat : Warning<"empty %select{struct|union}0 "
+  "has size 0 in C, size 1 in C++">, InGroup<CXXCompat>, DefaultIgnore;
 } // End of general sema category.
 
 // inline asm.
Index: lib/Parse/ParseDecl.cpp
===================================================================
--- lib/Parse/ParseDecl.cpp
+++ lib/Parse/ParseDecl.cpp
@@ -3065,13 +3065,6 @@
   ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
   Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
 
-  // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
-  // C++.
-  if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
-    Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
-    Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
-  }
-
   SmallVector<Decl *, 32> FieldDecls;
 
   // While we still have something to read, read the declarations in the struct.
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -11033,6 +11033,31 @@
 
     if (Record->hasAttrs())
       CheckAlignasUnderalignment(Record);
+
+    if (!getLangOpts().CPlusPlus) {
+      bool is_empty = true;
+      bool no_named = true;
+      for (RecordDecl::field_iterator i = Record->field_begin(),
+                                      e = Record->field_end(); no_named && i != e; ++i) {
+        if (i->isUnnamedBitfield()) {
+          if (i->getBitWidthValue(Context) > 0)
+            is_empty = false;
+        }
+        else
+          no_named = is_empty = false;
+      }
+      if (is_empty) {
+        // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
+        // C++.
+        Diag(RecLoc, diag::ext_empty_struct_union) << Record->isUnion();
+        Diag(RecLoc, diag::warn_empty_struct_union_compat) << Record->isUnion();
+      }
+      else if (no_named) {
+        // Structs without named members are extension in C (C99 6.7.2.1p7), but
+        // are accepted by GCC.
+        Diag(RecLoc, diag::ext_no_named_members_in_struct_union) << Record->isUnion();
+      }
+    }
   } else {
     ObjCIvarDecl **ClsFields =
       reinterpret_cast<ObjCIvarDecl**>(RecFields.data());
Index: test/Parser/declarators.c
===================================================================
--- test/Parser/declarators.c
+++ test/Parser/declarators.c
@@ -108,7 +108,8 @@
 }
 
 enum E1 { e1 }: // expected-error {{expected ';'}}
-struct EnumBitfield {
+struct EnumBitfield { // expected-warning {{struct without named members is a GNU extension}}
   enum E2 { e2 } : 4; // ok
   struct S { int n; }: // expected-error {{expected ';'}}
+
 };
Index: test/Sema/array-init.c
===================================================================
--- test/Sema/array-init.c
+++ test/Sema/array-init.c
@@ -226,7 +226,8 @@
 // expected-error{{initializer for aggregate with no elements}}
 
 void noNamedInit() {
-  struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}}
+  struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}} \
+// expected-warning {{struct without named members is a GNU extension}}
 }
 struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
 int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
Index: test/Sema/empty1.c
===================================================================
--- /dev/null
+++ test/Sema/empty1.c
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify -Wc++-compat
+
+struct emp_1 { // expected-warning {{empty struct has size 0 in C, size 1 in C++}}
+};
+
+union emp_2 { // expected-warning {{empty union has size 0 in C, size 1 in C++}}
+};
+
+struct emp_3 { // expected-warning {{empty struct has size 0 in C, size 1 in C++}}
+  int : 0;
+};
+
+union emp_4 { // expected-warning {{empty union has size 0 in C, size 1 in C++}}
+  int : 0;
+};
+
+struct emp_5 { // expected-warning {{empty struct has size 0 in C, size 1 in C++}}
+  int : 0;
+  int : 0;
+};
+
+union emp_6 { // expected-warning {{empty union has size 0 in C, size 1 in C++}}
+  int : 0;
+  int : 0;
+};
Index: test/Sema/empty2.c
===================================================================
--- /dev/null
+++ test/Sema/empty2.c
@@ -0,0 +1,43 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic
+
+struct emp_1 { // expected-warning {{empty struct is a GNU extension}}
+};
+
+union emp_2 { // expected-warning {{empty union is a GNU extension}}
+};
+
+struct emp_3 { // expected-warning {{empty struct is a GNU extension}}
+  int : 0;
+};
+
+union emp_4 { // expected-warning {{empty union is a GNU extension}}
+  int : 0;
+};
+
+struct emp_5 { // expected-warning {{empty struct is a GNU extension}}
+  int : 0;
+  int : 0;
+};
+
+union emp_6 { // expected-warning {{empty union is a GNU extension}}
+  int : 0;
+  int : 0;
+};
+
+struct nonamed_1 { // expected-warning {{struct without named members is a GNU extension}}
+  int : 4;
+};
+
+union nonamed_2 { // expected-warning {{union without named members is a GNU extension}}
+  int : 4;
+};
+
+struct nonamed_3 { // expected-warning {{struct without named members is a GNU extension}}
+  int : 4;
+  unsigned int : 4;
+};
+
+union nonamed_4 { // expected-warning {{union without named members is a GNU extension}}
+  int : 4;
+  unsigned int : 4;
+};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D578.2.patch
Type: text/x-patch
Size: 6858 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130328/48e4713c/attachment.bin>


More information about the cfe-commits mailing list