blob: fb67e323f1d128e1e04bfaa734ae3f0aa5448556 [file] [log] [blame]
Reid Klecknerb0a17ed2018-02-12 17:37:06 +00001// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -verify -Wsign-compare %s
2// RUN: %clang_cc1 -triple=x86_64-pc-win32 -fsyntax-only -verify -Wsign-compare %s
3// RUN: %clang_cc1 -x c++ -triple=x86_64-pc-linux-gnu -fsyntax-only -verify -Wsign-compare %s
4// RUN: %clang_cc1 -x c++ -triple=x86_64-pc-win32 -fsyntax-only -verify -Wsign-compare %s
Roman Lebedev7ade0172017-10-24 21:05:43 +00005
Reid Klecknerb0a17ed2018-02-12 17:37:06 +00006// Check that -Wsign-compare is off by default.
7// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -verify -DSILENCE %s
Roman Lebedev7ade0172017-10-24 21:05:43 +00008
Reid Klecknerb0a17ed2018-02-12 17:37:06 +00009#ifdef SILENCE
10// expected-no-diagnostics
11#endif
12
13enum PosEnum {
14 A_a = 0,
15 A_b = 1,
16 A_c = 10
17};
18
19static const int message[] = {0, 1};
20
21int test_pos(enum PosEnum a) {
Roman Lebedev7ade0172017-10-24 21:05:43 +000022 if (a < 2)
23 return 0;
24
Reid Klecknerb0a17ed2018-02-12 17:37:06 +000025 // No warning, except in Windows C mode, where PosEnum is 'int' and it can
26 // take on any value according to the C standard.
27#if !defined(SILENCE) && defined(_WIN32) && !defined(__cplusplus)
28 // expected-warning@+2 {{comparison of integers of different signs}}
29#endif
Roman Lebedev7ade0172017-10-24 21:05:43 +000030 if (a < 2U)
31 return 0;
Reid Klecknerb0a17ed2018-02-12 17:37:06 +000032
33 unsigned uv = 2;
34#if !defined(SILENCE) && defined(_WIN32) && !defined(__cplusplus)
35 // expected-warning@+2 {{comparison of integers of different signs}}
36#endif
37 if (a < uv)
38 return 1;
39
40#if !defined(SILENCE) && defined(_WIN32) && !defined(__cplusplus)
41 // expected-warning@+2 {{comparison of integers of different signs}}
42#endif
Roman Lebedev7ade0172017-10-24 21:05:43 +000043 if (a < sizeof(message)/sizeof(message[0]))
44 return 0;
Reid Klecknerb0a17ed2018-02-12 17:37:06 +000045 return 4;
46}
47
48enum NegEnum {
49 NE_a = -1,
50 NE_b = 0,
51 NE_c = 10
52};
53
54int test_neg(enum NegEnum a) {
55 if (a < 2)
56 return 0;
57
58#ifndef SILENCE
59 // expected-warning@+2 {{comparison of integers of different signs}}
Roman Lebedev7ade0172017-10-24 21:05:43 +000060#endif
Reid Klecknerb0a17ed2018-02-12 17:37:06 +000061 if (a < 2U)
62 return 0;
63
64 unsigned uv = 2;
65#ifndef SILENCE
66 // expected-warning@+2 {{comparison of integers of different signs}}
67#endif
68 if (a < uv)
69 return 1;
70
71#ifndef SILENCE
72 // expected-warning@+2 {{comparison of integers of different signs}}
73#endif
74 if (a < sizeof(message)/sizeof(message[0]))
75 return 0;
76 return 4;
Roman Lebedev7ade0172017-10-24 21:05:43 +000077}