1 // RUN: %clang_analyze_cc1 -std=c++14 \
2 // RUN: -analyzer-checker=core.CallAndMessage \
3 // RUN: -analyzer-config suppress-null-return-paths=false \
5 // RUN: %clang_analyze_cc1 -std=c++14 \
6 // RUN: -analyzer-checker=core.CallAndMessage \
11 // expected-no-diagnostics
15 #include "../Inputs/system-header-simulator-cxx.h"
21 // From llvm/include/llvm/Support/MathExtras.h
22 inline uintptr_t alignAddr(const void *Addr
, size_t Alignment
) {
23 return (((uintptr_t)Addr
+ Alignment
- 1) & ~(uintptr_t)(Alignment
- 1));
26 inline size_t alignmentAdjustment(const void *Ptr
, size_t Alignment
) {
27 return alignAddr(Ptr
, Alignment
) - (uintptr_t)Ptr
;
31 // From llvm/include/llvm/Support/MemAlloc.h
32 inline void *safe_malloc(size_t Sz
) {
33 void *Result
= malloc(Sz
);
34 if (Result
== nullptr)
41 // From llvm/include/llvm/Support/Allocator.h
42 class MallocAllocator
{
44 void *Allocate(size_t Size
, size_t /*Alignment*/) {
45 return safe_malloc(Size
);
49 class BumpPtrAllocator
{
51 void *Allocate(size_t Size
, size_t Alignment
) {
52 BytesAllocated
+= Size
;
53 size_t Adjustment
= alignmentAdjustment(CurPtr
, Alignment
);
54 size_t SizeToAllocate
= Size
;
56 size_t PaddedSize
= SizeToAllocate
+ Alignment
- 1;
57 uintptr_t AlignedAddr
= alignAddr(Allocator
.Allocate(PaddedSize
, 0),
59 char *AlignedPtr
= (char*)AlignedAddr
;
65 char *CurPtr
= nullptr;
66 size_t BytesAllocated
= 0;
67 MallocAllocator Allocator
;
71 // From clang/include/clang/AST/ASTContextAllocate.h
74 void *operator new(size_t Bytes
, const ASTContext
&C
, size_t Alignment
= 8);
75 void *operator new[](size_t Bytes
, const ASTContext
&C
, size_t Alignment
= 8);
78 // From clang/include/clang/AST/ASTContext.h
81 void *Allocate(size_t Size
, unsigned Align
= 8) const {
82 return BumpAlloc
.Allocate(Size
, Align
);
86 T
*Allocate(size_t Num
= 1) const {
87 return static_cast<T
*>(Allocate(Num
* sizeof(T
), alignof(T
)));
91 mutable BumpPtrAllocator BumpAlloc
;
95 // From clang/include/clang/AST/ASTContext.h
96 inline void *operator new(size_t Bytes
, const ASTContext
&C
,
97 size_t Alignment
/* = 8 */) {
98 return C
.Allocate(Bytes
, Alignment
);
101 inline void *operator new[](size_t Bytes
, const ASTContext
&C
,
102 size_t Alignment
/* = 8 */) {
103 return C
.Allocate(Bytes
, Alignment
);
107 // From clang/include/clang/AST/Attr.h
108 void *operator new(size_t Bytes
, ASTContext
&C
,
109 size_t Alignment
= 8) noexcept
{
110 return ::operator new(Bytes
, C
, Alignment
);
116 void setValue(int value
) { Value
= value
; }
121 void f(const ASTContext
&C
) {
125 // expected-warning@-2 {{Called C++ object pointer is null}}
129 void g(const ASTContext
&C
) {
133 // expected-warning@-2 {{Called C++ object pointer is null}}