llvm-capstone/clang/test/Analysis/_Bool-increment-decrement.c
Henry Wong e47b89d1f8 [Analyzer] More accurate modeling about the increment operator of the operand with type bool.
Summary:
There is a problem with analyzer that a wrong value is given when modeling the increment operator of the operand with type bool. After `rL307604` is applied, a unsigned overflow may occur.

Example:
```
void func() {
  bool b = true;
  // unsigned overflow occur, 2 -> 0 U1b
  b++;
}
``` 

The use of an operand of type bool with the ++ operators is deprecated but valid untill C++17. And if the operand of the increment operator is of type bool, it is set to true.

This patch includes two parts:

  - If the operand of the increment operator is of type bool or type _Bool, set to true.
  - Modify `BasicValueFactory::getTruthValue()`, use `getIntWidth()` instead `getTypeSize()` and use `unsigned` instead `signed`.

Reviewers: alexshap, NoQ, dcoughlin, george.karpenkov

Reviewed By: NoQ

Subscribers: xazax.hun, szepet, a.sidorin, cfe-commits, MTC

Differential Revision: https://reviews.llvm.org/D43741

llvm-svn: 326776
2018-03-06 12:29:09 +00:00

141 lines
2.5 KiB
C

// RUN: %clang_analyze_cc1 -analyzer-checker=debug.ExprInspection -verify -std=c99 -Dbool=_Bool -Dtrue=1 -Dfalse=0 %s
// RUN: %clang_analyze_cc1 -analyzer-checker=debug.ExprInspection -verify -std=c11 -Dbool=_Bool -Dtrue=1 -Dfalse=0 %s
extern void clang_analyzer_eval(bool);
void test__Bool_value() {
{
bool b = true;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = false;
clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
}
{
bool b = -10;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = 10;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = 10;
b++;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = 0;
b++;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
}
void test__Bool_increment() {
{
bool b = true;
b++;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = false;
b++;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = true;
++b;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = false;
++b;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = 0;
++b;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = 10;
++b;
++b;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = -10;
++b;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = -1;
++b;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
}
void test__Bool_decrement() {
{
bool b = true;
b--;
clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
}
{
bool b = false;
b--;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = true;
--b;
clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
}
{
bool b = false;
--b;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = 0;
--b;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = 10;
--b;
clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
--b;
clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
}
{
bool b = -10;
--b;
clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
}
{
bool b = 1;
--b;
clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
}
}