Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 1 | // 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 Lebedev | 7ade017 | 2017-10-24 21:05:43 +0000 | [diff] [blame] | 5 | |
Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 6 | // Check that -Wsign-compare is off by default. |
| 7 | // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -verify -DSILENCE %s |
Roman Lebedev | 7ade017 | 2017-10-24 21:05:43 +0000 | [diff] [blame] | 8 | |
Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 9 | #ifdef SILENCE |
| 10 | // expected-no-diagnostics |
| 11 | #endif |
| 12 | |
| 13 | enum PosEnum { |
| 14 | A_a = 0, |
| 15 | A_b = 1, |
| 16 | A_c = 10 |
| 17 | }; |
| 18 | |
| 19 | static const int message[] = {0, 1}; |
| 20 | |
| 21 | int test_pos(enum PosEnum a) { |
Roman Lebedev | 7ade017 | 2017-10-24 21:05:43 +0000 | [diff] [blame] | 22 | if (a < 2) |
| 23 | return 0; |
| 24 | |
Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 25 | // 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 Lebedev | 7ade017 | 2017-10-24 21:05:43 +0000 | [diff] [blame] | 30 | if (a < 2U) |
| 31 | return 0; |
Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 32 | |
| 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 Lebedev | 7ade017 | 2017-10-24 21:05:43 +0000 | [diff] [blame] | 43 | if (a < sizeof(message)/sizeof(message[0])) |
| 44 | return 0; |
Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 45 | return 4; |
| 46 | } |
| 47 | |
| 48 | enum NegEnum { |
| 49 | NE_a = -1, |
| 50 | NE_b = 0, |
| 51 | NE_c = 10 |
| 52 | }; |
| 53 | |
| 54 | int 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 Lebedev | 7ade017 | 2017-10-24 21:05:43 +0000 | [diff] [blame] | 60 | #endif |
Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 61 | 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 Lebedev | 7ade017 | 2017-10-24 21:05:43 +0000 | [diff] [blame] | 77 | } |