llvm-project/clang/test/Sema/enum-sign-conversion.c
Micah Weston 882915df61 Enum conversion warning when one signed and other unsigned.
Ensures an -Wenum-conversion warning happens when one of the enums is
signed and the other is unsigned. Also adds a test file to verify these
warnings.

This warning would not happen since the -Wsign-conversion would make a
diagnostic then return, never allowing the -Wenum-conversion checks.

For example:

C
enum PE { P = -1 };
enum NE { N };
enum NE conv(enum PE E) { return E; }
Before this would only create a diagnostic with -Wsign-conversion and
never on -Wenum-conversion. Now it will create a diagnostic for both
-Wsign-conversion and -Wenum-conversion.

I could change it to just warn on -Wenum-conversion as that was what I
initially did. Seeing PR35200 (or GitHub Issue 316268), I let both
diagnostics check so that the sign conversion could generate a warning.
2022-05-09 10:16:19 -04:00

46 lines
1.5 KiB
C

// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -Wsign-conversion -verify=unsigned,both %s
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -Wconversion -verify=unsigned,both %s
// RUN: %clang_cc1 -triple=x86_64-pc-win32 -fsyntax-only -verify -Wsign-conversion -verify=win32,both %s
// PR35200
enum X { A,B,C};
int f(enum X x) {
return x; // unsigned-warning {{implicit conversion changes signedness: 'enum X' to 'int'}}
}
enum SE1 { N1 = -1 }; // Always a signed underlying type.
enum E1 { P1 }; // Unsigned underlying type except on Windows.
// ensure no regression with enum to sign (related to enum-enum-conversion.c)
int f1(enum E1 E) {
return E; // unsigned-warning {{implicit conversion changes signedness: 'enum E1' to 'int'}}
}
enum E1 f2(int E) {
return E; // unsigned-warning {{implicit conversion changes signedness: 'int' to 'enum E1'}}
}
int f3(enum SE1 E) {
return E; // shouldn't warn
}
enum SE1 f4(int E) {
return E; // shouldn't warn
}
unsigned f5(enum E1 E) {
return E; // win32-warning {{implicit conversion changes signedness: 'enum E1' to 'unsigned int'}}
}
enum E1 f6(unsigned E) {
return E; // win32-warning {{implicit conversion changes signedness: 'unsigned int' to 'enum E1'}}
}
unsigned f7(enum SE1 E) {
return E; // both-warning {{implicit conversion changes signedness: 'enum SE1' to 'unsigned int'}}
}
enum SE1 f8(unsigned E) {
return E; // both-warning {{implicit conversion changes signedness: 'unsigned int' to 'enum SE1'}}
}