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

Richard Smith richard at metafoo.co.uk
Wed Nov 13 12:26:18 PST 2013


  LGTM


================
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)) {
----------------
Serge Pavlov wrote:
> 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++. 
OK, that's fair. On the other hand, this code means different things in C and C++ (in C, 'Inner' is a top-level type, not a nested type). Maybe we should have a separate warning for that?


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

BRANCH
  empty

ARCANIST PROJECT
  clang



More information about the cfe-commits mailing list