Skip to content

Commit ab3b7c3

Browse files
committed
Don't try to diagnose the mismatch unless the warning is enabled
No longer perform the field analysis unless the warning is enabled. For safety i've updated a few of the existing bitfield tests to also run with this diagnostic enabled just to get a bit more coverage given we're no longer running this diagnostic code for every test.
1 parent bd2b006 commit ab3b7c3

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19406,11 +19406,13 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl,
1940619406

1940719407
if (Record && FD->getType().isVolatileQualified())
1940819408
Record->setHasVolatileMember(true);
19409+
bool ReportMSBitfieldStoragePacking = Record && PreviousField &&
19410+
!Diags.isIgnored(diag::warn_ms_bitfield_mismatched_storage_packing, Record->getLocation());
1940919411
auto IsNonDependentBitField = [](const FieldDecl *FD) {
1941019412
return FD->isBitField() && !FD->getType()->isDependentType();
1941119413
};
1941219414

19413-
if (Record && PreviousField && IsNonDependentBitField(FD) &&
19415+
if (ReportMSBitfieldStoragePacking && IsNonDependentBitField(FD) &&
1941419416
IsNonDependentBitField(PreviousField)) {
1941519417
CharUnits FDStorageSize = Context.getTypeSizeInChars(FD->getType());
1941619418
CharUnits PreviousFieldStorageSize =

clang/test/Sema/bitfield-layout.c

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=aarch64-linux-gnu
44
// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=x86_64-pc-linux-gnu
55
// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=x86_64-scei-ps4
6+
// RUN: %clang_cc1 %s -fsyntax-only -verify=checkms -triple=i686-apple-darwin9 -Wms-bitfield-compatibility
7+
68
// expected-no-diagnostics
79
#include <stddef.h>
810

@@ -24,12 +26,27 @@ CHECK_ALIGN(struct, a, 1)
2426
#endif
2527

2628
// Zero-width bit-fields with packed
27-
struct __attribute__((packed)) a2 { short x : 9; char : 0; int y : 17; };
29+
struct __attribute__((packed)) a2 {
30+
short x : 9; // #a2x
31+
char : 0; // #a2anon
32+
// checkms-warning@-1 {{bit-field '' of type 'char' has a different storage size than the preceding bit-field (1 vs 2 bytes) and will not be packed under the MS Windows platform ABI}}
33+
// checkms-note@#a2x {{preceding bit-field 'x' declared here with type 'short'}}
34+
int y : 17;
35+
// checkms-warning@-1 {{bit-field 'y' of type 'int' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the MS Windows platform ABI}}
36+
// checkms-note@#a2anon {{preceding bit-field '' declared here with type 'char'}}
37+
};
38+
2839
CHECK_SIZE(struct, a2, 5)
2940
CHECK_ALIGN(struct, a2, 1)
3041

3142
// Zero-width bit-fields at the end of packed struct
32-
struct __attribute__((packed)) a3 { short x : 9; int : 0; };
43+
struct __attribute__((packed)) a3 {
44+
short x : 9; // #a3x
45+
int : 0;
46+
// checkms-warning@-1 {{bit-field '' of type 'int' has a different storage size than the preceding bit-field (4 vs 2 bytes) and will not be packed under the MS Windows platform ABI}}
47+
// checkms-note@#a3x {{preceding bit-field 'x' declared here with type 'short'}}
48+
};
49+
3350
#if defined(__arm__) || defined(__aarch64__)
3451
CHECK_SIZE(struct, a3, 4)
3552
CHECK_ALIGN(struct, a3, 4)
@@ -39,7 +56,12 @@ CHECK_ALIGN(struct, a3, 1)
3956
#endif
4057

4158
// For comparison, non-zero-width bit-fields at the end of packed struct
42-
struct __attribute__((packed)) a4 { short x : 9; int : 1; };
59+
struct __attribute__((packed)) a4 {
60+
short x : 9; // #a4x
61+
int : 1;
62+
// checkms-warning@-1 {{bit-field '' of type 'int' has a different storage size than the preceding bit-field (4 vs 2 bytes) and will not be packed under the MS Windows platform ABI}}
63+
// checkms-note@#a4x {{preceding bit-field 'x' declared here with type 'short'}}
64+
};
4365
CHECK_SIZE(struct, a4, 2)
4466
CHECK_ALIGN(struct, a4, 1)
4567

@@ -165,22 +187,28 @@ CHECK_OFFSET(struct, g4, c, 3);
165187
#endif
166188

167189
struct g5 {
168-
char : 1;
190+
char : 1; // #g5
169191
__attribute__((aligned(1))) int n : 24;
192+
// checkms-warning@-1 {{bit-field 'n' of type 'int' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the MS Windows platform ABI}}
193+
// checkms-note@#g5 {{preceding bit-field '' declared here with type 'char'}}
170194
};
171195
CHECK_SIZE(struct, g5, 4);
172196
CHECK_ALIGN(struct, g5, 4);
173197

174198
struct __attribute__((packed)) g6 {
175-
char : 1;
199+
char : 1; // #g6
176200
__attribute__((aligned(1))) int n : 24;
201+
// checkms-warning@-1 {{bit-field 'n' of type 'int' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the MS Windows platform ABI}}
202+
// checkms-note@#g6 {{preceding bit-field '' declared here with type 'char'}}
177203
};
178204
CHECK_SIZE(struct, g6, 4);
179205
CHECK_ALIGN(struct, g6, 1);
180206

181207
struct g7 {
182-
char : 1;
208+
char : 1; // #g7
183209
__attribute__((aligned(1))) int n : 25;
210+
// checkms-warning@-1 {{bit-field 'n' of type 'int' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the MS Windows platform ABI}}
211+
// checkms-note@#g7 {{preceding bit-field '' declared here with type 'char'}}
184212
};
185213
#if defined(__ORBIS__)
186214
CHECK_SIZE(struct, g7, 4);
@@ -190,8 +218,10 @@ CHECK_SIZE(struct, g7, 8);
190218
CHECK_ALIGN(struct, g7, 4);
191219

192220
struct __attribute__((packed)) g8 {
193-
char : 1;
221+
char : 1; // #g8
194222
__attribute__((aligned(1))) int n : 25;
223+
// checkms-warning@-1 {{bit-field 'n' of type 'int' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the MS Windows platform ABI}}
224+
// checkms-note@#g8 {{preceding bit-field '' declared here with type 'char'}}
195225
};
196226
#if defined(__ORBIS__)
197227
CHECK_SIZE(struct, g8, 4);

clang/test/Sema/bitfield-layout_1.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=arm-linux-gnueabihf
33
// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=aarch64-linux-gnu
44
// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=x86_64-pc-linux-gnu
5+
// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=i686-apple-darwin9 -Wms-bitfield-compatibility
56
// expected-no-diagnostics
67

78
#define CHECK_SIZE(name, size) \

clang/test/Sema/mms-bitfields.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// RUN: %clang_cc1 -mms-bitfields -fsyntax-only -verify -triple x86_64-apple-darwin9 %s
2+
// RUN: %clang_cc1 -mms-bitfields -fsyntax-only -Wms-bitfield-compatibility -verify=checkms -triple x86_64-apple-darwin9 %s
3+
24
// expected-no-diagnostics
35

46
// The -mms-bitfields commandline parameter should behave the same
57
// as the ms_struct attribute.
68
struct
79
{
8-
int a : 1;
10+
int a : 1; // #a
911
short b : 1;
12+
// checkms-warning@-1 {{bit-field 'b' of type 'short' has a different storage size than the preceding bit-field (2 vs 4 bytes) and will not be packed under the MS Windows platform ABI}}
13+
// checkms-note@#a {{preceding bit-field 'a' declared here with type 'int'}}
1014
} t;
1115

1216
// MS pads out bitfields between different types.

clang/test/SemaCXX/bitfield.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 %s -verify
2+
// RUN: %clang_cc1 %s -verify -Wms-bitfield-compatibility
23

34
// expected-no-diagnostics
45

0 commit comments

Comments
 (0)