This is an archive of the discontinued LLVM Phabricator instance.

Warn on zero-sized structures inside extern "C" block (PR5065)
ClosedPublic

Authored by sepavloff on Nov 12 2013, 8:29 AM.

Details

Summary

This patch adds warning on structures/unions that are empty or contain
only bit fields of zero size. Warnings are generated in C++ mode and
if only such type is defined inside extern "C" block.
The patch fixed PR5065.

Diff Detail

Event Timeline

rsmith added inline comments.Nov 12 2013, 12:04 PM
include/clang/Basic/DiagnosticSemaKinds.td
5721 ↗(On Diff #5468)

Maybe name this warn_..._union_in_extern_c or similar?

lib/Sema/SemaDecl.cpp
12074–12075 ↗(On Diff #5468)

You could also check that Record->getDeclContext()->getRedeclContext()->isFileContext() here (that is, it's not a local class nor a member class).

12076 ↗(On Diff #5468)

This is redundant. In CPlusPlus mode, every RecordDecl is a CXXRecordDecl.

12105 ↗(On Diff #5468)

Non-static data members can't have deduced types, so this is impossible.

12111–12132 ↗(On Diff #5468)

It would be simpler to organize this as:

if (ZeroSize)
  Diag(..., getLangOpts().CPlusPlus ? diag::..._extern_c : diag::..._compat) ...

if (NonBitFields == 0 && !getLangOpts().CPlusPlus) {
  ...
}

Thank you for review.

include/clang/Basic/DiagnosticSemaKinds.td
5721 ↗(On Diff #5468)

Renamed.

lib/Sema/SemaDecl.cpp
12074–12075 ↗(On Diff #5468)

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++.

12076 ↗(On Diff #5468)

The check is removed.

12105 ↗(On Diff #5468)

Removed.

sepavloff updated this revision to Unknown Object (????).Nov 13 2013, 10:29 AM

Updated patch according to reviewer's notes. Added new tests.

rsmith accepted this revision.Nov 13 2013, 12:26 PM

LGTM

lib/Sema/SemaDecl.cpp
12074–12075 ↗(On Diff #5468)

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?

sepavloff closed this revision.Nov 13 2013, 6:17 PM

Closed by commit rL194653 (authored by @sepavloff).