Richard Smith | c70f1d6 | 2017-12-14 15:16:18 +0000 | [diff] [blame^] | 1 | // Force x86-64 because some of our heuristics are actually based |
| 2 | // on integer sizes. |
| 3 | |
| 4 | // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -std=c++2a %s |
| 5 | |
| 6 | void test0(long a, unsigned long b) { |
| 7 | enum EnumA {A}; |
| 8 | enum EnumB {B}; |
| 9 | enum EnumC {C = 0x10000}; |
| 10 | // (a,b) |
| 11 | |
| 12 | // FIXME: <=> should never produce -Wsign-compare warnings. All the possible error |
| 13 | // cases involve narrowing conversions and so are ill-formed. |
| 14 | (void)(a <=> (unsigned long) b); // expected-warning {{comparison of integers of different signs}} |
| 15 | (void)(a <=> (unsigned int) b); |
| 16 | (void)(a <=> (unsigned short) b); |
| 17 | (void)(a <=> (unsigned char) b); |
| 18 | (void)((long) a <=> b); // expected-warning {{comparison of integers of different signs}} |
| 19 | (void)((int) a <=> b); // expected-warning {{comparison of integers of different signs}} |
| 20 | (void)((short) a <=> b); // expected-warning {{comparison of integers of different signs}} |
| 21 | (void)((signed char) a <=> b); // expected-warning {{comparison of integers of different signs}} |
| 22 | (void)((long) a <=> (unsigned long) b); // expected-warning {{comparison of integers of different signs}} |
| 23 | (void)((int) a <=> (unsigned int) b); // expected-warning {{comparison of integers of different signs}} |
| 24 | (void)((short) a <=> (unsigned short) b); |
| 25 | (void)((signed char) a <=> (unsigned char) b); |
| 26 | |
| 27 | #if 0 |
| 28 | // (A,b) |
| 29 | (void)(A <=> (unsigned long) b); |
| 30 | (void)(A <=> (unsigned int) b); |
| 31 | (void)(A <=> (unsigned short) b); |
| 32 | (void)(A <=> (unsigned char) b); |
| 33 | (void)((long) A <=> b); |
| 34 | (void)((int) A <=> b); |
| 35 | (void)((short) A <=> b); |
| 36 | (void)((signed char) A <=> b); |
| 37 | (void)((long) A <=> (unsigned long) b); |
| 38 | (void)((int) A <=> (unsigned int) b); |
| 39 | (void)((short) A <=> (unsigned short) b); |
| 40 | (void)((signed char) A <=> (unsigned char) b); |
| 41 | |
| 42 | // (a,B) |
| 43 | (void)(a <=> (unsigned long) B); |
| 44 | (void)(a <=> (unsigned int) B); |
| 45 | (void)(a <=> (unsigned short) B); |
| 46 | (void)(a <=> (unsigned char) B); |
| 47 | (void)((long) a <=> B); |
| 48 | (void)((int) a <=> B); |
| 49 | (void)((short) a <=> B); |
| 50 | (void)((signed char) a <=> B); |
| 51 | (void)((long) a <=> (unsigned long) B); |
| 52 | (void)((int) a <=> (unsigned int) B); |
| 53 | (void)((short) a <=> (unsigned short) B); |
| 54 | (void)((signed char) a <=> (unsigned char) B); |
| 55 | |
| 56 | // (C,b) |
| 57 | (void)(C <=> (unsigned long) b); |
| 58 | (void)(C <=> (unsigned int) b); |
| 59 | (void)(C <=> (unsigned short) b); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always 'std::strong_ordering::greater'}} |
| 60 | (void)(C <=> (unsigned char) b); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always 'std::strong_ordering::greater'}} |
| 61 | (void)((long) C <=> b); |
| 62 | (void)((int) C <=> b); |
| 63 | (void)((short) C <=> b); |
| 64 | (void)((signed char) C <=> b); |
| 65 | (void)((long) C <=> (unsigned long) b); |
| 66 | (void)((int) C <=> (unsigned int) b); |
| 67 | (void)((short) C <=> (unsigned short) b); |
| 68 | (void)((signed char) C <=> (unsigned char) b); |
| 69 | |
| 70 | // (a,C) |
| 71 | (void)(a <=> (unsigned long) C); |
| 72 | (void)(a <=> (unsigned int) C); |
| 73 | (void)(a <=> (unsigned short) C); |
| 74 | (void)(a <=> (unsigned char) C); |
| 75 | (void)((long) a <=> C); |
| 76 | (void)((int) a <=> C); |
| 77 | (void)((short) a <=> C); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always 'std::strong_ordering::less'}} |
| 78 | (void)((signed char) a <=> C); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always 'std::strong_ordering::less'}} |
| 79 | (void)((long) a <=> (unsigned long) C); |
| 80 | (void)((int) a <=> (unsigned int) C); |
| 81 | (void)((short) a <=> (unsigned short) C); |
| 82 | (void)((signed char) a <=> (unsigned char) C); |
| 83 | #endif |
| 84 | |
| 85 | // (0x80000,b) |
| 86 | (void)(0x80000 <=> (unsigned long) b); |
| 87 | (void)(0x80000 <=> (unsigned int) b); |
| 88 | (void)(0x80000 <=> (unsigned short) b); // expected-warning {{result of comparison of constant 524288 with expression of type 'unsigned short' is always 'std::strong_ordering::greater'}} |
| 89 | (void)(0x80000 <=> (unsigned char) b); // expected-warning {{result of comparison of constant 524288 with expression of type 'unsigned char' is always 'std::strong_ordering::greater'}} |
| 90 | (void)((long) 0x80000 <=> b); |
| 91 | (void)((int) 0x80000 <=> b); |
| 92 | (void)((short) 0x80000 <=> b); |
| 93 | (void)((signed char) 0x80000 <=> b); |
| 94 | (void)((long) 0x80000 <=> (unsigned long) b); |
| 95 | (void)((int) 0x80000 <=> (unsigned int) b); |
| 96 | (void)((short) 0x80000 <=> (unsigned short) b); |
| 97 | (void)((signed char) 0x80000 <=> (unsigned char) b); |
| 98 | |
| 99 | // (a,0x80000) |
| 100 | (void)(a <=> (unsigned long) 0x80000); // expected-warning {{comparison of integers of different signs}} |
| 101 | (void)(a <=> (unsigned int) 0x80000); |
| 102 | (void)(a <=> (unsigned short) 0x80000); |
| 103 | (void)(a <=> (unsigned char) 0x80000); |
| 104 | (void)((long) a <=> 0x80000); |
| 105 | (void)((int) a <=> 0x80000); |
| 106 | (void)((short) a <=> 0x80000); // expected-warning {{comparison of constant 524288 with expression of type 'short' is always 'std::strong_ordering::less'}} |
| 107 | (void)((signed char) a <=> 0x80000); // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always 'std::strong_ordering::less'}} |
| 108 | (void)((long) a <=> (unsigned long) 0x80000); // expected-warning {{comparison of integers of different signs}} |
| 109 | (void)((int) a <=> (unsigned int) 0x80000); // expected-warning {{comparison of integers of different signs}} |
| 110 | (void)((short) a <=> (unsigned short) 0x80000); |
| 111 | (void)((signed char) a <=> (unsigned char) 0x80000); |
| 112 | } |
| 113 | |
| 114 | void test5(bool b) { |
| 115 | (void) (b <=> -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always 'std::strong_ordering::greater'}} |
| 116 | (void) (b <=> -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always 'std::strong_ordering::greater'}} |
| 117 | (void) (b <=> 0); |
| 118 | (void) (b <=> 1); |
| 119 | (void) (b <=> 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always 'std::strong_ordering::less'}} |
| 120 | (void) (b <=> 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always 'std::strong_ordering::less'}} |
| 121 | } |
| 122 | |
| 123 | void test6(signed char sc) { |
| 124 | (void)(sc <=> 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always 'std::strong_ordering::less'}} |
| 125 | (void)(200 <=> sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always 'std::strong_ordering::greater'}} |
| 126 | } |
| 127 | |
| 128 | // Test many signedness combinations. |
| 129 | void test7(unsigned long other) { |
| 130 | // Common unsigned, other unsigned, constant unsigned |
| 131 | (void)((unsigned)other <=> (unsigned long)(0x1'ffff'ffff)); // expected-warning{{less}} |
| 132 | (void)((unsigned)other <=> (unsigned long)(0xffff'ffff)); |
| 133 | (void)((unsigned long)other <=> (unsigned)(0x1'ffff'ffff)); |
| 134 | (void)((unsigned long)other <=> (unsigned)(0xffff'ffff)); |
| 135 | |
| 136 | // Common unsigned, other signed, constant unsigned |
| 137 | (void)((int)other <=> (unsigned long)(0xffff'ffff'ffff'ffff)); // expected-warning{{different signs}} |
| 138 | (void)((int)other <=> (unsigned long)(0x0000'0000'ffff'ffff)); // expected-warning{{different signs}} |
| 139 | (void)((int)other <=> (unsigned long)(0x0000'0000'0fff'ffff)); // expected-warning{{different signs}} |
| 140 | (void)((int)other <=> (unsigned)(0x8000'0000)); // expected-warning{{different signs}} |
| 141 | |
| 142 | // Common unsigned, other unsigned, constant signed |
| 143 | (void)((unsigned long)other <=> (int)(0xffff'ffff)); // expected-warning{{different signs}} |
| 144 | |
| 145 | // Common unsigned, other signed, constant signed |
| 146 | // Should not be possible as the common type should also be signed. |
| 147 | |
| 148 | // Common signed, other signed, constant signed |
| 149 | (void)((int)other <=> (long)(0xffff'ffff)); // expected-warning{{less}} |
| 150 | (void)((int)other <=> (long)(0xffff'ffff'0000'0000)); // expected-warning{{greater}} |
| 151 | (void)((int)other <=> (long)(0x0fff'ffff)); |
| 152 | (void)((int)other <=> (long)(0xffff'ffff'f000'0000)); |
| 153 | |
| 154 | // Common signed, other signed, constant unsigned |
| 155 | (void)((int)other <=> (unsigned char)(0xffff)); |
| 156 | (void)((int)other <=> (unsigned char)(0xff)); |
| 157 | |
| 158 | // Common signed, other unsigned, constant signed |
| 159 | (void)((unsigned char)other <=> (int)(0xff)); |
| 160 | (void)((unsigned char)other <=> (int)(0xffff)); // expected-warning{{less}} |
| 161 | |
| 162 | // Common signed, other unsigned, constant unsigned |
| 163 | (void)((unsigned char)other <=> (unsigned short)(0xff)); |
| 164 | (void)((unsigned char)other <=> (unsigned short)(0x100)); // expected-warning{{less}} |
| 165 | (void)((unsigned short)other <=> (unsigned char)(0xff)); |
| 166 | } |