xref: /llvm-project/clang/test/Analysis/bitwise-shift-state-update.c (revision 25b9696b61e53a958e217bb3d0eab66350dc187f)
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core.BitwiseShift \
2 // RUN:    -analyzer-config core.BitwiseShift:Pedantic=true \
3 // RUN:    -analyzer-checker=debug.ExprInspection \
4 // RUN:    -analyzer-config eagerly-assume=false \
5 // RUN:    -verify=expected,c \
6 // RUN:    -triple x86_64-pc-linux-gnu -x c %s \
7 // RUN:    -Wno-shift-count-negative -Wno-shift-negative-value \
8 // RUN:    -Wno-shift-count-overflow -Wno-shift-overflow \
9 // RUN:    -Wno-shift-sign-overflow
10 //
11 // RUN: %clang_analyze_cc1 -analyzer-checker=core.BitwiseShift \
12 // RUN:    -analyzer-config core.BitwiseShift:Pedantic=true \
13 // RUN:    -analyzer-checker=debug.ExprInspection \
14 // RUN:    -analyzer-config eagerly-assume=false \
15 // RUN:    -verify=expected,cxx \
16 // RUN:    -triple x86_64-pc-linux-gnu -x c++ -std=c++14 %s \
17 // RUN:    -Wno-shift-count-negative -Wno-shift-negative-value \
18 // RUN:    -Wno-shift-count-overflow -Wno-shift-overflow \
19 // RUN:    -Wno-shift-sign-overflow
20 
21 // Tests for validating the state updates provided by the BitwiseShift checker.
22 // These clang_analyzer_value() tests are in a separate file because
23 // debug.ExprInspection repeats each 'warning' with an superfluous 'note', so
24 // note level output (-analyzer-output=text) is not enabled in this file.
25 
26 void clang_analyzer_value(int);
27 void clang_analyzer_eval(int);
28 
state_update_generic(int left,int right)29 int state_update_generic(int left, int right) {
30   int x = left << right;
31   clang_analyzer_value(left); // expected-warning {{32s:{ [0, 2147483647] } }}
32   clang_analyzer_value(right); // expected-warning {{32s:{ [0, 31] } }}
33   return x;
34 }
35 
state_update_exact_shift(int arg)36 int state_update_exact_shift(int arg) {
37   int x = 65535 << arg;
38   clang_analyzer_value(arg);
39   // c-warning@-1 {{32s:{ [0, 15] } }}
40   // cxx-warning@-2 {{32s:{ [0, 16] } }}
41   return x;
42 }
43