2018-08-30 04:29:17 +08:00
// RUN: %clang_analyze_cc1 %s -analyzer-checker=core.NullDereference,core.DivideZero -fblocks -analyzer-output=text -analyzer-config suppress-null-return-paths=false -verify -analyzer-config eagerly-assume=false %s
// RUN: %clang_analyze_cc1 -analyzer-config eagerly-assume=false %s -analyzer-checker=core.NullDereference,core.DivideZero -fblocks -analyzer-output=plist -analyzer-config suppress-null-return-paths=false -o %t
2019-06-11 22:21:32 +08:00
// RUN: %normalize_plist <%t | diff -ub %S/Inputs/expected-plists/inline-plist.c.plist -
2012-03-03 05:16:22 +08:00
// <rdar://problem/10967815>
void mmm ( int y ) {
if ( y ! = 0 )
2012-09-22 09:24:46 +08:00
y + + ;
2012-03-03 05:16:22 +08:00
}
int foo ( int x , int y ) {
2012-09-22 09:24:46 +08:00
mmm ( y ) ;
if ( x ! = 0 ) {
// expected-note@-1 {{Assuming 'x' is equal to 0}}
// expected-note@-2 {{Taking false branch}}
x + + ;
}
return 5 / x ; // expected-warning{{Division by zero}} expected-note{{Division by zero}}
2012-03-03 05:16:22 +08:00
}
2012-03-13 06:10:57 +08:00
// Test a bug triggering only when inlined.
void has_bug ( int * p ) {
2012-09-22 09:24:46 +08:00
* p = 0xDEADBEEF ; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}} expected-note{{Dereference of null pointer (loaded from variable 'p')}}
2012-03-13 06:10:57 +08:00
}
2022-02-13 21:02:46 +08:00
void test_has_bug ( void ) {
2012-03-13 06:10:57 +08:00
has_bug ( 0 ) ;
2012-09-22 09:24:46 +08:00
// expected-note@-1 {{Passing null pointer value via 1st parameter 'p'}}
// expected-note@-2 {{Calling 'has_bug'}}
2012-03-13 06:10:57 +08:00
}
2012-05-02 08:31:29 +08:00
void triggers_bug ( int * p ) {
2012-09-22 09:24:46 +08:00
* p = 0xDEADBEEF ; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}} expected-note{{Dereference of null pointer (loaded from variable 'p')}}
2012-05-02 08:31:29 +08:00
}
// This function triggers a bug by calling triggers_bug(). The diagnostics
// should show when p is assumed to be null.
void bar ( int * p ) {
2012-09-22 09:24:46 +08:00
if ( ! ! p ) {
// expected-note@-1 {{Assuming 'p' is null}}
2012-10-26 06:07:10 +08:00
// expected-note@-2 {{Taking false branch}}
2012-05-02 08:31:29 +08:00
return ;
2012-09-22 09:24:46 +08:00
}
2017-03-04 02:02:02 +08:00
2012-09-22 09:24:46 +08:00
if ( p = = 0 ) {
[analyzer] ConditionBRVisitor: Enhance to write out more information
Summary:
Add extra messages to the bug report to inform the user why the analyzer
`Taking true/false branch`.
Reviewers: NoQ, george.karpenkov
Reviewed By: NoQ
Subscribers: gerazo, gsd, dkrupp, whisperity, baloghadamsoftware, xazax.hun,
eraman, szepet, a.sidorin, mikhail.ramalho, Szelethus,
donat.nagy, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D53076
llvm-svn: 362020
2019-05-30 04:06:09 +08:00
// expected-note@-1 {{'p' is equal to null}}
// expected-note@-2 {{Taking true branch}}
2012-05-02 08:31:29 +08:00
triggers_bug ( p ) ;
2012-09-22 09:24:46 +08:00
// expected-note@-1 {{Passing null pointer value via 1st parameter 'p'}}
// expected-note@-2 {{Calling 'triggers_bug'}}
}
2012-05-02 08:31:29 +08:00
}
2012-04-07 06:10:18 +08:00
2012-06-02 04:04:04 +08:00
// ========================================================================== //
// Test inlining of blocks.
// ========================================================================== //
2022-02-13 21:02:46 +08:00
void test_block__capture_null ( void ) {
2013-02-27 03:44:38 +08:00
int * p = 0 ; // expected-note{{'p' initialized to a null pointer value}}
2022-02-13 21:02:46 +08:00
^ ( void ) { // expected-note {{Calling anonymous block}}
2012-09-22 09:24:46 +08:00
* p = 1 ; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}} expected-note{{Dereference of null pointer (loaded from variable 'p')}}
} ( ) ;
2017-03-04 02:02:02 +08:00
2012-06-02 04:04:04 +08:00
}
2022-02-13 21:02:46 +08:00
void test_block_ret ( void ) {
int * p = ^ int * ( void ) { // expected-note {{Calling anonymous block}} expected-note{{Returning to caller}} expected-note {{'p' initialized to a null pointer value}}
2013-02-27 03:44:38 +08:00
int * q = 0 ; // expected-note {{'q' initialized to a null pointer value}}
2012-09-22 09:24:46 +08:00
return q ; // expected-note {{Returning null pointer (loaded from 'q')}}
} ( ) ;
* p = 1 ; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}} expected-note{{Dereference of null pointer (loaded from variable 'p')}}
2012-06-02 04:04:04 +08:00
}
2022-02-13 21:02:46 +08:00
void test_block_blockvar ( void ) {
2012-06-02 04:04:04 +08:00
__block int * p ;
2022-02-13 21:02:46 +08:00
^ ( void ) { // expected-note{{Calling anonymous block}} expected-note{{Returning to caller}}
2012-09-22 09:24:46 +08:00
p = 0 ; // expected-note{{Null pointer value stored to 'p'}}
} ( ) ;
* p = 1 ; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}} expected-note{{Dereference of null pointer (loaded from variable 'p')}}
2012-06-02 04:04:04 +08:00
}
2022-02-13 21:02:46 +08:00
void test_block_arg ( void ) {
2012-06-02 04:04:04 +08:00
int * p ;
2012-09-22 09:24:46 +08:00
^ ( int * * q ) { // expected-note{{Calling anonymous block}} expected-note{{Returning to caller}}
* q = 0 ; // expected-note{{Null pointer value stored to 'p'}}
} ( & p ) ;
* p = 1 ; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}} expected-note{{Dereference of null pointer (loaded from variable 'p')}}
2012-06-02 04:04:04 +08:00
}