Home | History | Annotate | Download | only in Sema
      1 // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-overlap-compare %s
      2 
      3 #define mydefine 2
      4 
      5 enum Choices {
      6   CHOICE_0 = 0,
      7   CHOICE_1 = 1
      8 };
      9 
     10 enum Unchoices {
     11   UNCHOICE_0 = 0,
     12   UNCHOICE_1 = 1
     13 };
     14 
     15 void f(int x) {
     16   int y = 0;
     17 
     18   // > || <
     19   if (x > 2 || x < 1) { }
     20   if (x > 2 || x < 2) { }
     21   if (x != 2 || x != 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     22   if (x > 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     23   if (x > 0 || x < 0) { }
     24 
     25   if (x > 2 || x <= 1) { }
     26   if (x > 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     27   if (x > 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     28 
     29   if (x >= 2 || x < 1) { }
     30   if (x >= 2 || x < 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     31   if (x >= 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     32 
     33   if (x >= 2 || x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     34   if (x >= 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     35   if (x >= 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     36   if (x >= 0 || x <= 0) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     37 
     38   // > && <
     39   if (x > 2 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     40   if (x > 2 && x < 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     41   if (x > 2 && x < 3) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     42   if (x > 0 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     43 
     44   if (x > 2 && x <= 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     45   if (x > 2 && x <= 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     46   if (x > 2 && x <= 3) { }
     47 
     48   if (x >= 2 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     49   if (x >= 2 && x < 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     50   if (x >= 2 && x < 3) { }
     51   if (x >= 0 && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     52 
     53   if (x >= 2 && x <= 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     54   if (x >= 2 && x <= 2) { }
     55   if (x >= 2 && x <= 3) { }
     56 
     57   // !=, ==, ..
     58   if (x != 2 || x != 3) { }  // expected-warning {{overlapping comparisons always evaluate to true}}
     59   if (x != 2 || x < 3) { }   // expected-warning {{overlapping comparisons always evaluate to true}}
     60   if (x == 2 && x == 3) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     61   if (x == 2 && x > 3) { }   // expected-warning {{overlapping comparisons always evaluate to false}}
     62   if (x == 3 && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     63   if (3 == x && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
     64 
     65   if (x == mydefine && x > 3) { }
     66   if (x == (mydefine + 1) && x > 3) { }
     67 
     68   if (x != CHOICE_0 || x != CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     69   if (x == CHOICE_0 && x == CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
     70 
     71   // Don't warn if comparing x to different types
     72   if (x == CHOICE_0 && x == 1) { }
     73   if (x != CHOICE_0 || x != 1) { }
     74 
     75   // "Different types" includes different enums
     76   if (x == CHOICE_0 && x == UNCHOICE_1) { }
     77   if (x != CHOICE_0 || x != UNCHOICE_1) { }
     78 }
     79 
     80 void enums(enum Choices c) {
     81   if (c != CHOICE_0 || c != CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
     82   if (c == CHOICE_0 && c == CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
     83 
     84   // Don't warn if comparing x to different types
     85   if (c == CHOICE_0 && c == 1) { }
     86   if (c != CHOICE_0 || c != 1) { }
     87 
     88   // "Different types" includes different enums
     89   if (c == CHOICE_0 && c == UNCHOICE_1) { }
     90   if (c != CHOICE_0 || c != UNCHOICE_1) { }
     91 }
     92 
     93 // Don't generate a warning here.
     94 void array_out_of_bounds() {
     95   int x;
     96   int buffer[4];
     97   x = (-7 > 0) ? (buffer[-7]) : 0;
     98 }
     99