blob: 81d11939265838ab81a24f9f46113f020b7ade0e [file] [log] [blame]
Jordy Rose43d9f0d2012-05-16 16:01:10 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -verify %s
Jordy Rose43fdb7f2010-06-20 04:56:29 +00002
Jordy Rose43d9f0d2012-05-16 16:01:10 +00003void clang_analyzer_eval(int);
Jordy Rose43fdb7f2010-06-20 04:56:29 +00004
5// There should be no warnings unless otherwise indicated.
6
7void 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;
Jordy Rose43d9f0d2012-05-16 16:01:10 +000011 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}}
Jordy Rose43fdb7f2010-06-20 04:56:29 +000017}
18
19void testSelfOperations (int a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +000020 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}}
Jordy Rose43fdb7f2010-06-20 04:56:29 +000024}
25
26void testIdempotent (int a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +000027 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}}
Jordy Rose43fdb7f2010-06-20 04:56:29 +000036}
37
38void testReductionToConstant (int a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +000039 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}}
Jordy Rose43fdb7f2010-06-20 04:56:29 +000042}
43
44void testSymmetricIntSymOperations (int a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +000045 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}}
Jordy Rose43fdb7f2010-06-20 04:56:29 +000050}
51
52void testAsymmetricIntSymOperations (int a) {
Jordy Rose43d9f0d2012-05-16 16:01:10 +000053 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}}
Jordy Rose43fdb7f2010-06-20 04:56:29 +000056
57 // Unsigned right shift shifts in zeroes.
Jordy Rose43d9f0d2012-05-16 16:01:10 +000058 clang_analyzer_eval(((~0U) >> a) != (~0U)); // expected-warning{{UNKNOWN}}
Jordy Rose43fdb7f2010-06-20 04:56:29 +000059}
Jordy Roseeac4a002010-06-28 08:26:15 +000060
61void testLocations (char *a) {
62 char *b = a;
Jordy Rose43d9f0d2012-05-16 16:01:10 +000063 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}}
Jordy Roseeac4a002010-06-28 08:26:15 +000069}
Jordy Rosec838fd22012-05-06 23:40:02 +000070
71void testMixedTypeComparisons (char a, unsigned long b) {
72 if (a != 0) return;
73 if (b != 0x100) return;
74
Jordy Rose43d9f0d2012-05-16 16:01:10 +000075 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}}
Jordy Rosec838fd22012-05-06 23:40:02 +000078}