diff options
| author | Artem Dergachev <artem.dergachev@gmail.com> | 2018-01-24 20:32:26 +0000 |
|---|---|---|
| committer | Artem Dergachev <artem.dergachev@gmail.com> | 2018-01-24 20:32:26 +0000 |
| commit | 50e0372f82a7c7471e68051659a4043e1d30a9da (patch) | |
| tree | f13c1ed99641eeed2fc9f8b51d61ef90d6e01bfa /clang/test/Analysis/new-ctor-conservative.cpp | |
| parent | f386e2b081b728b031b197e6028980b1bbcf52a3 (diff) | |
| download | bcm5719-llvm-50e0372f82a7c7471e68051659a4043e1d30a9da.tar.gz bcm5719-llvm-50e0372f82a7c7471e68051659a4043e1d30a9da.zip | |
[analyzer] Assume that the allocated value is non-null before construction.
I.e. not after. In the c++-allocator-inlining=true mode, we need to make the
assumption that the conservatively evaluated operator new() has returned a
non-null value. Previously we did this on CXXNewExpr, but now we have to do that
before calling the constructor, because some clever constructors are sometimes
assuming that their "this" is null and doing weird stuff. We would also crash
upon evaluating CXXNewExpr when the allocator was inlined and returned null and
had a throw specification; this is UB even for custom allocators, but we still
need not to crash.
Added more FIXME tests to ensure that eventually we fix calling the constructor
for null return values.
Differential Revision: https://reviews.llvm.org/D42192
llvm-svn: 323370
Diffstat (limited to 'clang/test/Analysis/new-ctor-conservative.cpp')
| -rw-r--r-- | clang/test/Analysis/new-ctor-conservative.cpp | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/clang/test/Analysis/new-ctor-conservative.cpp b/clang/test/Analysis/new-ctor-conservative.cpp index 4500e3a253d..b82df9abf1e 100644 --- a/clang/test/Analysis/new-ctor-conservative.cpp +++ b/clang/test/Analysis/new-ctor-conservative.cpp @@ -1,6 +1,7 @@ -// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -analyzer-config c++-allocator-inlining=true -std=c++11 -verify %s +// RUN: %clang_analyze_cc1 -w -analyzer-checker=core,debug.ExprInspection -analyzer-config c++-allocator-inlining=true -std=c++11 -verify %s void clang_analyzer_eval(bool); +void clang_analyzer_warnIfReached(); struct S { int x; @@ -27,3 +28,19 @@ void checkNewArray() { // FIXME: Should be true once we inline array constructors. clang_analyzer_eval(s[0].x == 1); // expected-warning{{UNKNOWN}} } + +struct NullS { + NullS() { + if (this) {} + } + NullS(int x) { + if (!this) { + clang_analyzer_warnIfReached(); // no-warning + } + } +}; + +void checkNullThis() { + NullS *nulls = new NullS(); // no-crash + NullS *nulls2 = new NullS(0); +} |

