1 // RUN: %clang_analyze_cc1 -std=c++11 -fblocks %s \
2 // RUN: -verify=newdelete \
3 // RUN: -analyzer-checker=core \
4 // RUN: -analyzer-checker=cplusplus.NewDelete
6 // RUN: %clang_analyze_cc1 -std=c++11 -DLEAKS -fblocks %s \
8 // RUN: -analyzer-checker=core \
9 // RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
11 // leak-no-diagnostics
13 // RUN: %clang_analyze_cc1 -std=c++11 -DLEAKS -fblocks %s \
14 // RUN: -verify=mismatch \
15 // RUN: -analyzer-checker=core \
16 // RUN: -analyzer-checker=unix.MismatchedDeallocator
18 #include "Inputs/system-header-simulator-cxx.h"
19 #include "Inputs/system-header-simulator-objc.h"
21 typedef __typeof__(sizeof(int)) size_t;
22 extern "C" void *malloc(size_t);
23 extern "C" void *alloca(size_t);
24 extern "C" void free(void *);
26 void testMallocFreeNoWarn() {
27 int *p1 = (int *)malloc(sizeof(int));
28 free(++p1); // no warn
30 int *p2 = (int *)malloc(sizeof(int));
34 int *p3 = (int *)malloc(sizeof(int)); // no warn
36 int *p4 = (int *)malloc(sizeof(int));
38 int j = *p4; // no warn
40 int *p5 = (int *)alloca(sizeof(int));
44 void testDeleteMalloced() {
45 int *p1 = (int *)malloc(sizeof(int));
47 // mismatch-warning@-1{{Memory allocated by malloc() should be deallocated by free(), not 'delete'}}
49 int *p2 = (int *)__builtin_alloca(sizeof(int));
53 void testUseZeroAllocatedMalloced() {
54 int *p1 = (int *)malloc(0);
58 //----- Test free standard new
59 void testFreeOpNew() {
60 void *p = operator new(0);
62 // mismatch-warning@-1{{Memory allocated by operator new should be deallocated by 'delete', not free()}}
65 void testFreeNewExpr() {
68 // mismatch-warning@-1{{Memory allocated by 'new' should be deallocated by 'delete', not free()}}
72 void testObjcFreeNewed() {
74 NSData *nsdata = [NSData dataWithBytesNoCopy:p length:sizeof(int) freeWhenDone:1];
75 // mismatch-warning@-1{{+dataWithBytesNoCopy:length:freeWhenDone: cannot take ownership of memory allocated by 'new'}}
78 void testFreeAfterDelete() {
81 free(p); // newdelete-warning{{Use of memory after it is freed}}
84 void testStandardPlacementNewAfterDelete() {
87 p = new (p) int; // newdelete-warning{{Use of memory after it is freed}}