Skip to content

Struct size is 0 even if the compiler emits "warning: struct has size 0 in C, size 1 in C++ [-Wc++-compat]" #25085

Open
@dyung

Description

@dyung
Bugzilla Link 24711
Version trunk
OS Linux
CC @DougGregor

Extended Description

Consider the following code:

/*** test.c ***/
#include <stdio.h>

struct S {
char tmp[0];
};

struct T {
struct S v01;
struct S v02;
};

int main(void) {
printf("%lu\n", sizeof(struct S));
printf("%lu\n", sizeof(struct T));
return 0;
}
/*****/

If you compile this as a .c file on x64 linux targeting x64 linux, using -Wc++-compat, you get the following warnings:

test.c:3:1: warning: struct has size 0 in C, size 1 in C++ [-Wc++-compat]
struct S {
^
test.c:7:1: warning: struct has size 0 in C, non-zero size in C++ [-Wc++-compat]
struct T {
^

But if rename the file to be .cpp and recompile, you find that both of those warnings are not true. The size of both S and T are reported to be 0, which does at least seem to be consistent with what gcc produces.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partyfalse-positiveWarning fires when it should not

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions