137 lines · cpp
1// RUN: %clang_analyze_cc1 -std=c++14 \2// RUN: -analyzer-checker=core.CallAndMessage \3// RUN: -analyzer-config suppress-null-return-paths=false \4// RUN: -verify %s5// RUN: %clang_analyze_cc1 -std=c++14 \6// RUN: -analyzer-checker=core.CallAndMessage \7// RUN: -DSUPPRESSED \8// RUN: -verify %s9 10#ifdef SUPPRESSED11// expected-no-diagnostics12#endif13 14#include <stdint.h>15#include "../Inputs/system-header-simulator-cxx.h"16 17void error();18void *malloc(size_t);19 20 21// From llvm/include/llvm/Support/MathExtras.h22inline uintptr_t alignAddr(const void *Addr, size_t Alignment) {23 return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));24}25 26inline size_t alignmentAdjustment(const void *Ptr, size_t Alignment) {27 return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;28}29 30 31// From llvm/include/llvm/Support/MemAlloc.h32inline void *safe_malloc(size_t Sz) {33 void *Result = malloc(Sz);34 if (Result == nullptr)35 error();36 37 return Result;38}39 40 41// From llvm/include/llvm/Support/Allocator.h42class MallocAllocator {43public:44 void *Allocate(size_t Size, size_t /*Alignment*/) {45 return safe_malloc(Size);46 }47};48 49class BumpPtrAllocator {50public:51 void *Allocate(size_t Size, size_t Alignment) {52 BytesAllocated += Size;53 size_t Adjustment = alignmentAdjustment(CurPtr, Alignment);54 size_t SizeToAllocate = Size;55 56 size_t PaddedSize = SizeToAllocate + Alignment - 1;57 uintptr_t AlignedAddr = alignAddr(Allocator.Allocate(PaddedSize, 0),58 Alignment);59 char *AlignedPtr = (char*)AlignedAddr;60 61 return AlignedPtr;62 }63 64private:65 char *CurPtr = nullptr;66 size_t BytesAllocated = 0;67 MallocAllocator Allocator;68};69 70 71// From clang/include/clang/AST/ASTContextAllocate.h72class ASTContext;73 74void *operator new(size_t Bytes, const ASTContext &C, size_t Alignment = 8);75void *operator new[](size_t Bytes, const ASTContext &C, size_t Alignment = 8);76 77 78// From clang/include/clang/AST/ASTContext.h79class ASTContext {80public:81 void *Allocate(size_t Size, unsigned Align = 8) const {82 return BumpAlloc.Allocate(Size, Align);83 }84 85 template <typename T>86 T *Allocate(size_t Num = 1) const {87 return static_cast<T *>(Allocate(Num * sizeof(T), alignof(T)));88 }89 90private:91 mutable BumpPtrAllocator BumpAlloc;92};93 94 95// From clang/include/clang/AST/ASTContext.h96inline void *operator new(size_t Bytes, const ASTContext &C,97 size_t Alignment /* = 8 */) {98 return C.Allocate(Bytes, Alignment);99}100 101inline void *operator new[](size_t Bytes, const ASTContext &C,102 size_t Alignment /* = 8 */) {103 return C.Allocate(Bytes, Alignment);104}105 106 107// From clang/include/clang/AST/Attr.h108void *operator new(size_t Bytes, ASTContext &C,109 size_t Alignment = 8) noexcept {110 return ::operator new(Bytes, C, Alignment);111}112 113 114class A {115public:116 void setValue(int value) { Value = value; }117private:118 int Value;119};120 121void f(const ASTContext &C) {122 A *a = new (C) A;123 a->setValue(13);124#ifndef SUPPRESSED125 // expected-warning@-2 {{Called C++ object pointer is null}}126#endif127} 128 129void g(const ASTContext &C) {130 A *a = new (C) A[1];131 a[0].setValue(13);132#ifndef SUPPRESSED133 // expected-warning@-2 {{Called C++ object pointer is null}}134#endif135}136 137