| 1 |
// RUN: %clang_analyze_cc1 %s \ |
| 2 |
// RUN: -analyzer-checker=core \ |
| 3 |
// RUN: -analyzer-checker=unix.StdCLibraryFunctions \ |
| 4 |
// RUN: -analyzer-checker=debug.StdCLibraryFunctionsTester \ |
| 5 |
// RUN: -analyzer-config unix.StdCLibraryFunctions:DisplayLoadedSummaries=true \ |
| 6 |
// RUN: -analyzer-checker=debug.ExprInspection \ |
| 7 |
// RUN: -analyzer-config eagerly-assume=false \ |
| 8 |
// RUN: -triple i686-unknown-linux \ |
| 9 |
// RUN: -analyzer-output=text \ |
| 10 |
// RUN: -verify |
| 11 |
|
| 12 |
template <typename T> |
| 13 |
void clang_analyzer_express(T x); |
| 14 |
void clang_analyzer_eval(bool); |
| 15 |
int clang_analyzer_getExtent(void *); |
| 16 |
|
| 17 |
|
| 18 |
// Check NotNullConstraint assumption notes. |
| 19 |
int __not_null(int *); |
| 20 |
int test_not_null_note(int *x, int y) { |
| 21 |
__not_null(x); // expected-note{{Assuming that the 1st argument to '__not_null' is not NULL}} |
| 22 |
if (x) // expected-note{{'x' is non-null}} \ |
| 23 |
// expected-note{{Taking true branch}} |
| 24 |
if (!y) // expected-note{{Assuming 'y' is 0}} \ |
| 25 |
// expected-note{{Taking true branch}} |
| 26 |
return 1 / y; // expected-warning{{Division by zero}} \ |
| 27 |
// expected-note{{Division by zero}} |
| 28 |
|
| 29 |
return 0; |
| 30 |
} |
| 31 |
|
| 32 |
// Check the RangeConstraint assumption notes. |
| 33 |
int __single_val_0(int); // [0, 0] |
| 34 |
int test_range_constraint_note(int x, int y) { |
| 35 |
__single_val_0(x); // expected-note{{Assuming that the 1st argument to '__single_val_0' is zero}} |
| 36 |
return y / x; // expected-warning{{Division by zero}} \ |
| 37 |
// expected-note{{Division by zero}} |
| 38 |
} |
| 39 |
|
| 40 |
// Check the BufferSizeConstraint assumption notes. |
| 41 |
int __buf_size_arg_constraint_concrete(const void *buf); // size of buf must be >= 10 |
| 42 |
void test_buffer_size_note(char *buf, int y) { |
| 43 |
__buf_size_arg_constraint_concrete(buf); // expected-note {{Assuming that the 1st argument to '__buf_size_arg_constraint_concrete' is a buffer with size equal to or greater than 10}} |
| 44 |
clang_analyzer_eval(clang_analyzer_getExtent(buf) >= 10); // expected-warning{{TRUE}} \ |
| 45 |
// expected-note{{TRUE}} |
| 46 |
|
| 47 |
// clang_analyzer_express marks the argument as interesting. |
| 48 |
clang_analyzer_express(buf); // expected-warning {{}} // the message does not really matter \ |
| 49 |
// expected-note {{}} |
| 50 |
} |
| 51 |
|
| 52 |
int __test_case_note(); |
| 53 |
|
| 54 |
int test_case_note_1(int y) { |
| 55 |
int x0 = __test_case_note(); |
| 56 |
int x = __test_case_note(); // expected-note{{Function returns 0}} \ |
| 57 |
// expected-note{{'x' initialized here}} |
| 58 |
return y / x; // expected-warning{{Division by zero}} \ |
| 59 |
// expected-note{{Division by zero}} |
| 60 |
} |
| 61 |
|
| 62 |
int test_case_note_2(int y) { |
| 63 |
int x = __test_case_note(); |
| 64 |
return y / (x - 1); // expected-warning{{Division by zero}} \ |
| 65 |
// expected-note{{Division by zero}} |
| 66 |
} |