xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/NewDelete-intersections.mm (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc// RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.NewDelete -std=c++11 -fblocks -verify %s
2*0a6a1f1dSLionel Sambuc// RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks -std=c++11 -DLEAKS -fblocks -verify %s
3f4a2713aSLionel Sambuc#include "Inputs/system-header-simulator-cxx.h"
4f4a2713aSLionel Sambuc#include "Inputs/system-header-simulator-objc.h"
5f4a2713aSLionel Sambuc
6f4a2713aSLionel Sambuctypedef __typeof__(sizeof(int)) size_t;
7f4a2713aSLionel Sambucextern "C" void *malloc(size_t);
8f4a2713aSLionel Sambucextern "C" void free(void *);
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc//----------------------------------------------------------------------------
11f4a2713aSLionel Sambuc// Check for intersections with unix.Malloc and unix.MallocWithAnnotations
12f4a2713aSLionel Sambuc// checkers bounded with cplusplus.NewDelete.
13f4a2713aSLionel Sambuc//----------------------------------------------------------------------------
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc//----- malloc()/free() are subjects of unix.Malloc and unix.MallocWithAnnotations
16f4a2713aSLionel Sambucvoid testMallocFreeNoWarn() {
17f4a2713aSLionel Sambuc  int i;
18f4a2713aSLionel Sambuc  free(&i); // no warn
19f4a2713aSLionel Sambuc
20f4a2713aSLionel Sambuc  int *p1 = (int *)malloc(sizeof(int));
21f4a2713aSLionel Sambuc  free(++p1); // no warn
22f4a2713aSLionel Sambuc
23f4a2713aSLionel Sambuc  int *p2 = (int *)malloc(sizeof(int));
24f4a2713aSLionel Sambuc  free(p2);
25f4a2713aSLionel Sambuc  free(p2); // no warn
26f4a2713aSLionel Sambuc
27f4a2713aSLionel Sambuc  int *p3 = (int *)malloc(sizeof(int)); // no warn
28f4a2713aSLionel Sambuc
29f4a2713aSLionel Sambuc  int *p4 = (int *)malloc(sizeof(int));
30f4a2713aSLionel Sambuc  free(p4);
31f4a2713aSLionel Sambuc  int j = *p4; // no warn
32f4a2713aSLionel Sambuc}
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambucvoid testDeleteMalloced() {
35f4a2713aSLionel Sambuc  int *p = (int *)malloc(sizeof(int));
36f4a2713aSLionel Sambuc  delete p; // no warn
37f4a2713aSLionel Sambuc}
38f4a2713aSLionel Sambuc
39f4a2713aSLionel Sambuc//----- Test free standard new
40f4a2713aSLionel Sambucvoid testFreeOpNew() {
41f4a2713aSLionel Sambuc  void *p = operator new(0);
42f4a2713aSLionel Sambuc  free(p);
43f4a2713aSLionel Sambuc}
44f4a2713aSLionel Sambuc#ifdef LEAKS
45f4a2713aSLionel Sambuc// expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
46f4a2713aSLionel Sambuc#endif
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambucvoid testFreeNewExpr() {
49f4a2713aSLionel Sambuc  int *p = new int;
50f4a2713aSLionel Sambuc  free(p);
51f4a2713aSLionel Sambuc}
52f4a2713aSLionel Sambuc#ifdef LEAKS
53f4a2713aSLionel Sambuc// expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
54f4a2713aSLionel Sambuc#endif
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambucvoid testObjcFreeNewed() {
57f4a2713aSLionel Sambuc  int *p = new int;
58f4a2713aSLionel Sambuc  NSData *nsdata = [NSData dataWithBytesNoCopy:p length:sizeof(int) freeWhenDone:1];
59f4a2713aSLionel Sambuc#ifdef LEAKS
60f4a2713aSLionel Sambuc  // expected-warning@-2 {{Potential leak of memory pointed to by 'p'}}
61f4a2713aSLionel Sambuc#endif
62f4a2713aSLionel Sambuc}
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambucvoid testFreeAfterDelete() {
65f4a2713aSLionel Sambuc  int *p = new int;
66f4a2713aSLionel Sambuc  delete p;
67f4a2713aSLionel Sambuc  free(p); // expected-warning{{Use of memory after it is freed}}
68f4a2713aSLionel Sambuc}
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambucvoid testStandardPlacementNewAfterDelete() {
71f4a2713aSLionel Sambuc  int *p = new int;
72f4a2713aSLionel Sambuc  delete p;
73f4a2713aSLionel Sambuc  p = new(p) int; // expected-warning{{Use of memory after it is freed}}
74f4a2713aSLionel Sambuc}
75