teak-llvm/clang/test/Analysis/new-with-exceptions.cpp
Pavel Labath fce1b03ee7 [analyzer] Assume new returns non-null even under -fno-exceptions
Summary:
-fno-exceptions does not implicitly attach a nothrow specifier to every operator
new. Even in this mode, non-nothrow new must not return a null pointer. Failure
to allocate memory can be signalled by other means, or just by killing the
program. This behaviour is consistent with the compiler - even with
-fno-exceptions, the generated code never tests for null (and would segfault if
the opeator actually happened to return null).

Reviewers: jordan_rose

CC: cfe-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D1528

llvm-svn: 189452
2013-08-28 08:04:08 +00:00

54 lines
1.7 KiB
C++

// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -analyzer-store region -std=c++11 -fexceptions -fcxx-exceptions -verify %s
// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -analyzer-store region -std=c++11 -verify %s
void clang_analyzer_eval(bool);
typedef __typeof__(sizeof(int)) size_t;
extern "C" void *malloc(size_t);
// This is the standard placement new.
inline void* operator new(size_t, void* __p) throw()
{
return __p;
}
struct NoThrow {
void *operator new(size_t) throw();
};
struct NoExcept {
void *operator new(size_t) noexcept;
};
struct DefaultThrow {
void *operator new(size_t);
};
struct ExplicitThrow {
void *operator new(size_t) throw(int);
};
void testNew() {
clang_analyzer_eval(new NoThrow); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(new NoExcept); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(new DefaultThrow); // expected-warning{{TRUE}}
clang_analyzer_eval(new ExplicitThrow); // expected-warning{{TRUE}}
}
void testNewArray() {
clang_analyzer_eval(new NoThrow[2]); // expected-warning{{TRUE}}
clang_analyzer_eval(new NoExcept[2]); // expected-warning{{TRUE}}
clang_analyzer_eval(new DefaultThrow[2]); // expected-warning{{TRUE}}
clang_analyzer_eval(new ExplicitThrow[2]); // expected-warning{{TRUE}}
}
extern void *operator new[](size_t, int) noexcept;
void testNewArrayNoThrow() {
clang_analyzer_eval(new (1) NoThrow[2]); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(new (1) NoExcept[2]); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(new (1) DefaultThrow[2]); // expected-warning{{UNKNOWN}}
clang_analyzer_eval(new (1) ExplicitThrow[2]); // expected-warning{{UNKNOWN}}
}