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