[PATCH] Warn on zero-sized structures inside extern "C" block (PR5065)

Serge Pavlov sepavloff at gmail.com
Wed Nov 13 10:28:36 PST 2013


  Thank you for review.


================
Comment at: include/clang/Basic/DiagnosticSemaKinds.td:5721
@@ -5720,1 +5720,3 @@
   InGroup<CXXCompat>, DefaultIgnore;
+def warn_zero_size_struct_union : Warning<"%select{|empty }0"
+  "%select{struct|union}1 has size 0 in C, %select{size 1|non-zero size}2 in C++">,
----------------
Richard Smith wrote:
> Maybe name this `warn_..._union_in_extern_c` or similar?
Renamed.

================
Comment at: lib/Sema/SemaDecl.cpp:12074-12075
@@ +12073,4 @@
+      // For C++ filter out types that cannot be referenced in C code.
+      if (Record->getLexicalDeclContext()->isExternCContext() &&
+          !Record->isDependentType()) {
+        if (CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(Record)) {
----------------
Richard Smith wrote:
> You could also check that `Record->getDeclContext()->getRedeclContext()->isFileContext()` here (that is, it's not a local class nor a member class).
With this check the warning for inner struct would be absent in the following declaration:
```struct pr5065_n2 {
    struct Inner {} x;
} q;```

Expression `sizeof(q.x)` would give different results in C and C++. 

================
Comment at: lib/Sema/SemaDecl.cpp:12076
@@ +12075,3 @@
+          !Record->isDependentType()) {
+        if (CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(Record)) {
+          CheckForZeroSize =
----------------
Richard Smith wrote:
> This is redundant. In `CPlusPlus` mode, every `RecordDecl` is a `CXXRecordDecl`.
The check is removed.

================
Comment at: lib/Sema/SemaDecl.cpp:12105
@@ -12082,1 +12104,3 @@
+              FieldType->isDependentType()  ||
+              FieldType->isUndeducedType()  ||
               !Context.getTypeSizeInChars(FieldType).isZero())
----------------
Richard Smith wrote:
> Non-static data members can't have deduced types, so this is impossible.
Removed.


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



More information about the cfe-commits mailing list