|
| 1 | +// RUN: %clang_cc1 -fsyntax-only -Wenum-compare-typo -verify %s |
| 2 | +// RUN: %clang_cc1 -fsyntax-only -Wenum-compare-typo -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s |
| 3 | + |
| 4 | + |
| 5 | +enum PossibleTypoLeft { |
| 6 | + Val1 = 1 << 0, |
| 7 | + // expected-warning@+3 {{comparison operator '<' is potentially a typo for a shift operator '<<'}} |
| 8 | + // expected-note@+2 {{use '<<' to perform a bitwise shift}} |
| 9 | + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:12-[[@LINE+1]]:13}:"<<" |
| 10 | + Bad1 = 1 < 2, |
| 11 | + // expected-warning@+3 {{comparison operator '>' is potentially a typo for a shift operator '>>'}} |
| 12 | + // expected-note@+2 {{use '>>' to perform a bitwise shift}} |
| 13 | + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:12-[[@LINE+1]]:13}:">>" |
| 14 | + Bad2 = 1 > 3, |
| 15 | + // expected-warning@+3 {{comparison operator '>' is potentially a typo for a shift operator '>>'}} |
| 16 | + // expected-note@+2 {{use '>>' to perform a bitwise shift}} |
| 17 | + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:13-[[@LINE+1]]:14}:">>" |
| 18 | + Bad3 = (1 > 3) |
| 19 | +}; |
| 20 | + |
| 21 | +enum PossibleTypoRight { |
| 22 | + Val2 = 1 >> 0, |
| 23 | + // expected-warning@+3 {{comparison operator '<' is potentially a typo for a shift operator '<<'}} |
| 24 | + // expected-note@+2 {{use '<<' to perform a bitwise shift}} |
| 25 | + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:12-[[@LINE+1]]:13}:"<<" |
| 26 | + Bad4 = 1 < 2, |
| 27 | + // expected-warning@+3 {{comparison operator '>' is potentially a typo for a shift operator '>>'}} |
| 28 | + // expected-note@+2 {{use '>>' to perform a bitwise shift}} |
| 29 | + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:12-[[@LINE+1]]:13}:">>" |
| 30 | + Bad5 = 1 > 3, |
| 31 | + // expected-warning@+3 {{comparison operator '<' is potentially a typo for a shift operator '<<'}} |
| 32 | + // expected-note@+2 {{use '<<' to perform a bitwise shift}} |
| 33 | + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:13-[[@LINE+1]]:14}:"<<" |
| 34 | + Bad6 = (1 < 3) |
| 35 | +}; |
| 36 | + |
| 37 | +// Case 3: Context provided by other bitwise operators (&, |) |
| 38 | +// Even though there are no shifts, the presence of '|' implies flags. |
| 39 | +enum PossibleTypoBitwiseOr { |
| 40 | + FlagA = 0x1, |
| 41 | + FlagB = 0x2, |
| 42 | + FlagCombo = FlagA | FlagB, |
| 43 | + // expected-warning@+3 {{comparison operator '<' is potentially a typo for a shift operator '<<'}} |
| 44 | + // expected-note@+2 {{use '<<' to perform a bitwise shift}} |
| 45 | + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:17-[[@LINE+1]]:18}:"<<" |
| 46 | + FlagTypo1 = 1 < FlagCombo, |
| 47 | + // expected-warning@+3 {{comparison operator '>' is potentially a typo for a shift operator '>>'}} |
| 48 | + // expected-note@+2 {{use '>>' to perform a bitwise shift}} |
| 49 | + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:17-[[@LINE+1]]:18}:">>" |
| 50 | + FlagTypo2 = 1 > FlagCombo |
| 51 | +}; |
| 52 | + |
| 53 | +enum PossibleTypoBitwiseAnd { |
| 54 | + FlagAnd = FlagA & FlagB, |
| 55 | + // expected-warning@+3 {{comparison operator '<' is potentially a typo for a shift operator '<<'}} |
| 56 | + // expected-note@+2 {{use '<<' to perform a bitwise shift}} |
| 57 | + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:17-[[@LINE+1]]:18}:"<<" |
| 58 | + FlagTypo3 = 1 < FlagAnd, |
| 59 | + // expected-warning@+3 {{comparison operator '>' is potentially a typo for a shift operator '>>'}} |
| 60 | + // expected-note@+2 {{use '>>' to perform a bitwise shift}} |
| 61 | + // CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:17-[[@LINE+1]]:18}:">>" |
| 62 | + FlagTypo4 = 1 > FlagAnd |
| 63 | +}; |
| 64 | + |
| 65 | +enum NoWarningOnDirectInit { |
| 66 | + A = 0, |
| 67 | + B = 1, |
| 68 | + Ok1 = 1 < 2, // No warning expected |
| 69 | + Ok2 = 1 > 2 // No warning expected |
| 70 | +}; |
| 71 | + |
| 72 | +enum NoWarningOnArith { |
| 73 | + D = 0 + 1, |
| 74 | + E = D * 10, |
| 75 | + F = E - D, |
| 76 | + G = F / D, |
| 77 | + Ok3 = 1 < E, // No warning expected |
| 78 | + Ok4 = 1 > E // No warning expected |
| 79 | +}; |
| 80 | + |
| 81 | +enum NoWarningOnExplicitCast { |
| 82 | + Bit1 = 1 << 0, |
| 83 | + Ok5 = (int)(1 < 2) // No warning expected |
| 84 | +}; |
| 85 | + |
| 86 | +enum NoWarningOnNoneBitShift { |
| 87 | + Bit2 = 1 << 0, |
| 88 | + Ok6 = (3 < 2) // No warning expected |
| 89 | +}; |
| 90 | + |
| 91 | +// Ensure the diagnostic group works |
| 92 | +#pragma clang diagnostic push |
| 93 | +#pragma clang diagnostic ignored "-Wenum-compare-typo" |
| 94 | +enum IGNORED { |
| 95 | + Ok7 = 1 << 1, |
| 96 | + Ignored3 = 1 < 10 // No warning |
| 97 | +}; |
| 98 | +#pragma clang diagnostic pop |
0 commit comments