diff options
author | Artem Dergachev <artem.dergachev@gmail.com> | 2018-05-04 20:52:39 +0000 |
---|---|---|
committer | Artem Dergachev <artem.dergachev@gmail.com> | 2018-05-04 20:52:39 +0000 |
commit | a2e053638bbfea50b29a29ee687f1bdd6abd427f (patch) | |
tree | dfeae83b24cdb39419329099fd0f93862296aae2 /clang/test | |
parent | b6211d9084e13d892faa6c7020561d9193b5513f (diff) | |
download | bcm5719-llvm-a2e053638bbfea50b29a29ee687f1bdd6abd427f.tar.gz bcm5719-llvm-a2e053638bbfea50b29a29ee687f1bdd6abd427f.zip |
[analyzer] Treat more const variables and fields as known contants.
When loading from a variable or a field that is declared as constant,
the analyzer will try to inspect its initializer and constant-fold it.
Upon success, the analyzer would skip normal load and return the respective
constant.
The new behavior also applies to fields/elements of brace-initialized structures
and arrays.
Patch by Rafael Stahl!
Differential Revision: https://reviews.llvm.org/D45774
llvm-svn: 331556
Diffstat (limited to 'clang/test')
-rw-r--r-- | clang/test/Analysis/globals.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/clang/test/Analysis/globals.cpp b/clang/test/Analysis/globals.cpp new file mode 100644 index 00000000000..5bbb241bdcf --- /dev/null +++ b/clang/test/Analysis/globals.cpp @@ -0,0 +1,111 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s + + +static const unsigned long long scull = 0; +void static_int() +{ + *(int*)scull = 0; // expected-warning{{Dereference of null pointer}} +} + +const unsigned long long cull = 0; +void const_int() +{ + *(int*)cull = 0; // expected-warning{{Dereference of null pointer}} +} + +static int * const spc = 0; +void static_ptr() +{ + *spc = 0; // expected-warning{{Dereference of null pointer}} +} + +int * const pc = 0; +void const_ptr() +{ + *pc = 0; // expected-warning{{Dereference of null pointer}} +} + +const unsigned long long cull_nonnull = 4; +void nonnull_int() +{ + *(int*)(cull_nonnull - 4) = 0; // expected-warning{{Dereference of null pointer}} +} + +int * const pc_nonnull = (int*)sizeof(int); +void nonnull_ptr() +{ + *(pc_nonnull - 1) = 0; // expected-warning{{Dereference of null pointer}} +} + +int * const constcast = const_cast<int * const>((int*)sizeof(int)); +void cast1() +{ + *(constcast - 1) = 0; // expected-warning{{Dereference of null pointer}} +} + +int * const recast = reinterpret_cast<int*>(sizeof(int)); +void cast2() +{ + *(recast - 1) = 0; // expected-warning{{Dereference of null pointer}} +} + +int * const staticcast = static_cast<int * const>((int*)sizeof(int)); +void cast3() +{ + *(staticcast - 1) = 0; // expected-warning{{Dereference of null pointer}} +} + +struct Foo { int a; }; +Foo * const dyncast = dynamic_cast<Foo * const>((Foo*)sizeof(Foo)); +void cast4() +{ + // Do not handle dynamic_cast for now, because it may change the pointer value. + (dyncast - 1)->a = 0; // no-warning +} + +typedef int * const intptrconst; +int * const funccast = intptrconst(sizeof(int)); +void cast5() +{ + *(funccast - 1) = 0; // expected-warning{{Dereference of null pointer}} +} + +struct S1 +{ + int * p; +}; +const S1 s1 = { + .p = (int*)sizeof(int) +}; +void conststruct() +{ + *(s1.p - 1) = 0; // expected-warning{{Dereference of null pointer}} +} + +struct S2 +{ + int * const p; +}; +S2 s2 = { + .p = (int*)sizeof(int) +}; +void constfield() +{ + *(s2.p - 1) = 0; // expected-warning{{Dereference of null pointer}} +} + +int * const parr[1] = { (int*)sizeof(int) }; +void constarr() +{ + *(parr[0] - 1) = 0; // expected-warning{{Dereference of null pointer}} +} + +struct S3 +{ + int * p = (int*)sizeof(int); +}; +void recordinit() +{ + S3 s3; + *(s3.p - 1) = 0; // expected-warning{{Dereference of null pointer}} +} |