[PATCH] Fix recognition of empty structures/unions
Richard Smith
richard at metafoo.co.uk
Thu Mar 28 13:03:28 PDT 2013
================
Comment at: lib/Sema/SemaDecl.cpp:11038-11039
@@ +11037,4 @@
+ if (!getLangOpts().CPlusPlus) {
+ bool is_empty = true;
+ bool no_named = true;
+ for (RecordDecl::field_iterator i = Record->field_begin(),
----------------
Variable names should be CamelCase with a leading capital letter.
================
Comment at: lib/Sema/SemaDecl.cpp:11045-11046
@@ +11044,4 @@
+ is_empty = false;
+ }
+ else
+ no_named = is_empty = false;
----------------
} and else should be on the same line.
================
Comment at: lib/Sema/SemaDecl.cpp:11049-11059
@@ +11048,13 @@
+ }
+ 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();
+ }
+ }
----------------
Either use the "without named members" diagnostic in both cases where no_named is true, or actually track whether the struct is empty (that is, has no members, not even empty bitfields). We shouldn't claim that "struct S { unsigned : 0; };" is empty.
http://llvm-reviews.chandlerc.com/D578
More information about the cfe-commits
mailing list