Home | History | Annotate | Download | only in Analysis
      1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode -Wno-null-dereference -verify %s
      2 
      3 // Trigger a warning if the analyzer reaches this point in the control flow.
      4 #define WARN ((void)*(char*)0)
      5 
      6 // There should be no warnings unless otherwise indicated.
      7 
      8 void testComparisons (int a) {
      9   // Sema can already catch the simple comparison a==a,
     10   // since that's usually a logic error (and not path-dependent).
     11   int b = a;
     12   if (!(b==a)) WARN; // expected-warning{{never executed}}
     13   if (!(b>=a)) WARN; // expected-warning{{never executed}}
     14   if (!(b<=a)) WARN; // expected-warning{{never executed}}
     15   if (b!=a) WARN;    // expected-warning{{never executed}}
     16   if (b>a) WARN;     // expected-warning{{never executed}}
     17   if (b<a) WARN;     // expected-warning{{never executed}}
     18 }
     19 
     20 void testSelfOperations (int a) {
     21   if ((a|a) != a) WARN; // expected-warning{{never executed}}
     22   if ((a&a) != a) WARN; // expected-warning{{never executed}}
     23   if ((a^a) != 0) WARN; // expected-warning{{never executed}}
     24   if ((a-a) != 0) WARN; // expected-warning{{never executed}}
     25 }
     26 
     27 void testIdempotent (int a) {
     28   if ((a*1) != a) WARN;    // expected-warning{{never executed}}
     29   if ((a/1) != a) WARN;    // expected-warning{{never executed}}
     30   if ((a+0) != a) WARN;    // expected-warning{{never executed}}
     31   if ((a-0) != a) WARN;    // expected-warning{{never executed}}
     32   if ((a<<0) != a) WARN;   // expected-warning{{never executed}}
     33   if ((a>>0) != a) WARN;   // expected-warning{{never executed}}
     34   if ((a^0) != a) WARN;    // expected-warning{{never executed}}
     35   if ((a&(~0)) != a) WARN; // expected-warning{{never executed}}
     36   if ((a|0) != a) WARN;    // expected-warning{{never executed}}
     37 }
     38 
     39 void testReductionToConstant (int a) {
     40   if ((a*0) != 0) WARN; // expected-warning{{never executed}}
     41   if ((a&0) != 0) WARN; // expected-warning{{never executed}}
     42   if ((a|(~0)) != (~0)) WARN; // expected-warning{{never executed}}
     43 }
     44 
     45 void testSymmetricIntSymOperations (int a) {
     46   if ((2+a) != (a+2)) WARN; // expected-warning{{never executed}}
     47   if ((2*a) != (a*2)) WARN; // expected-warning{{never executed}}
     48   if ((2&a) != (a&2)) WARN; // expected-warning{{never executed}}
     49   if ((2^a) != (a^2)) WARN; // expected-warning{{never executed}}
     50   if ((2|a) != (a|2)) WARN; // expected-warning{{never executed}}
     51 }
     52 
     53 void testAsymmetricIntSymOperations (int a) {
     54   if (((~0) >> a) != (~0)) WARN; // expected-warning{{never executed}}
     55   if ((0 >> a) != 0) WARN; // expected-warning{{never executed}}
     56   if ((0 << a) != 0) WARN; // expected-warning{{never executed}}
     57 
     58   // Unsigned right shift shifts in zeroes.
     59   if ((((unsigned)(~0)) >> ((unsigned) a)) != ((unsigned)(~0)))
     60     WARN; // expected-warning{{}}
     61 }
     62 
     63 void testLocations (char *a) {
     64   char *b = a;
     65   if (!(b==a)) WARN; // expected-warning{{never executed}}
     66   if (!(b>=a)) WARN; // expected-warning{{never executed}}
     67   if (!(b<=a)) WARN; // expected-warning{{never executed}}
     68   if (b!=a) WARN; // expected-warning{{never executed}}
     69   if (b>a) WARN; // expected-warning{{never executed}}
     70   if (b<a) WARN; // expected-warning{{never executed}}
     71   if (b-a) WARN; // expected-warning{{never executed}}
     72 }
     73