Skip to content

Commit 21a8532

Browse files
committed
[clang][Sema] Don't issue -Wcast-function-type-mismatch for enums with a matching underlying type
Enums are passed as their underlying integral type so they're ABI compatible if the size matches. Useful with C APIs that pass user-controlled values to callbacks that can be made type safe by using enumerations (e.g. GStreamer).
1 parent 9708d09 commit 21a8532

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

clang/lib/Sema/SemaCast.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,9 +1083,10 @@ static bool argTypeIsABIEquivalent(QualType SrcType, QualType DestType,
10831083
return true;
10841084

10851085
// Allow integral type mismatch if their size are equal.
1086-
if (SrcType->isIntegralType(Context) && DestType->isIntegralType(Context))
1087-
if (Context.getTypeInfoInChars(SrcType).Width ==
1088-
Context.getTypeInfoInChars(DestType).Width)
1086+
if ((SrcType->isIntegralType(Context) || SrcType->isEnumeralType()) &&
1087+
(DestType->isIntegralType(Context) || DestType->isEnumeralType()))
1088+
if (Context.getTypeSizeInChars(SrcType) ==
1089+
Context.getTypeSizeInChars(DestType))
10891090
return true;
10901091

10911092
return Context.hasSameUnqualifiedType(SrcType, DestType);

clang/test/Sema/warn-cast-function-type-strict.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ f8 *h;
2929
f9 *i;
3030
f10 *j;
3131

32+
enum E : long;
33+
int efunc(enum E);
34+
3235
void foo(void) {
3336
a = (f1 *)x;
37+
a = (f1 *)efunc; // strict-warning {{cast from 'int (*)(enum E)' to 'f1 *' (aka 'int (*)(long)') converts to incompatible function type}}
3438
b = (f2 *)x; /* expected-warning {{cast from 'int (*)(long)' to 'f2 *' (aka 'int (*)(void *)') converts to incompatible function type}} */
3539
c = (f3 *)x; /* strict-warning {{cast from 'int (*)(long)' to 'f3 *' (aka 'int (*)()') converts to incompatible function type}} */
3640
d = (f4 *)x; /* expected-warning {{cast from 'int (*)(long)' to 'f4 *' (aka 'void (*)()') converts to incompatible function type}} */

clang/test/Sema/warn-cast-function-type.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ f5 *e;
1919
f6 *f;
2020
f7 *g;
2121

22+
enum E : long;
23+
int efunc(enum E);
24+
2225
void foo(void) {
2326
a = (f1 *)x;
27+
a = (f1 *)efunc; // enum is just type system sugar, still passed as a long.
2428
b = (f2 *)x; /* expected-warning {{cast from 'int (*)(long)' to 'f2 *' (aka 'int (*)(void *)') converts to incompatible function type}} */
2529
c = (f3 *)x;
2630
d = (f4 *)x; /* expected-warning {{cast from 'int (*)(long)' to 'f4 *' (aka 'void (*)()') converts to incompatible function type}} */

clang/test/SemaCXX/warn-cast-function-type-strict.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ struct S
2929

3030
typedef void (S::*mf)(int);
3131

32+
enum E : long;
33+
int efunc(E);
34+
3235
void foo() {
3336
a = (f1 *)x;
37+
a = (f1 *)efunc; // strict-warning {{cast from 'int (*)(E)' to 'f1 *' (aka 'int (*)(long)') converts to incompatible function type}}
3438
b = (f2 *)x; // expected-warning {{cast from 'int (*)(long)' to 'f2 *' (aka 'int (*)(void *)') converts to incompatible function type}}
3539
b = reinterpret_cast<f2 *>(x); // expected-warning {{cast from 'int (*)(long)' to 'f2 *' (aka 'int (*)(void *)') converts to incompatible function type}}
3640
c = (f3 *)x; // strict-warning {{cast from 'int (*)(long)' to 'f3 *' (aka 'int (*)(...)') converts to incompatible function type}}

clang/test/SemaCXX/warn-cast-function-type.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ struct S
2828

2929
typedef void (S::*mf)(int);
3030

31+
enum E : long;
32+
int efunc(E);
33+
3134
void foo() {
3235
a = (f1 *)x;
36+
a = (f1 *)efunc; // enum is just type system sugar, still passed as a long.
3337
b = (f2 *)x; // expected-warning {{cast from 'int (*)(long)' to 'f2 *' (aka 'int (*)(void *)') converts to incompatible function type}}
3438
b = reinterpret_cast<f2 *>(x); // expected-warning {{cast from 'int (*)(long)' to 'f2 *' (aka 'int (*)(void *)') converts to incompatible function type}}
3539
c = (f3 *)x;

0 commit comments

Comments
 (0)