xref: /aosp_15_r20/external/clang/test/Analysis/bitwise-ops.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -triple x86_64-apple-darwin13 -Wno-shift-count-overflow -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li void clang_analyzer_eval(int);
4*67e74705SXin Li #define CHECK(expr) if (!(expr)) return; clang_analyzer_eval(expr)
5*67e74705SXin Li 
testPersistentConstraints(int x,int y)6*67e74705SXin Li void testPersistentConstraints(int x, int y) {
7*67e74705SXin Li   // Sanity check
8*67e74705SXin Li   CHECK(x); // expected-warning{{TRUE}}
9*67e74705SXin Li   CHECK(x & 1); // expected-warning{{TRUE}}
10*67e74705SXin Li 
11*67e74705SXin Li   // False positives due to SValBuilder giving up on certain kinds of exprs.
12*67e74705SXin Li   CHECK(1 - x); // expected-warning{{UNKNOWN}}
13*67e74705SXin Li   CHECK(x & y); // expected-warning{{UNKNOWN}}
14*67e74705SXin Li }
15*67e74705SXin Li 
testConstantShifts_PR18073(int which)16*67e74705SXin Li int testConstantShifts_PR18073(int which) {
17*67e74705SXin Li   // FIXME: We should have a checker that actually specifically checks bitwise
18*67e74705SXin Li   // shifts against the width of the LHS's /static/ type, rather than just
19*67e74705SXin Li   // having BasicValueFactory return "undefined" when dealing with two constant
20*67e74705SXin Li   // operands.
21*67e74705SXin Li   switch (which) {
22*67e74705SXin Li   case 1:
23*67e74705SXin Li     return 0ULL << 63; // no-warning
24*67e74705SXin Li   case 2:
25*67e74705SXin Li     return 0ULL << 64; // expected-warning{{The result of the '<<' expression is undefined}}
26*67e74705SXin Li   case 3:
27*67e74705SXin Li     return 0ULL << 65; // expected-warning{{The result of the '<<' expression is undefined}}
28*67e74705SXin Li 
29*67e74705SXin Li   default:
30*67e74705SXin Li     return 0;
31*67e74705SXin Li   }
32*67e74705SXin Li }