summaryrefslogtreecommitdiffstats
path: root/tests/cxx/operator_new.cc
blob: fb90556fb45fa99cc6163a03e10c8b8317126762 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//===--- operator_new.cc - test input file for iwyu -----------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// IWYU_ARGS: -std=c++17 -I .

// Test that iwyu suggests the include for <new> be removed if only
// built-in functions are used.

#include <new>
#include "tests/cxx/direct.h"

// The most primitive ::operator new/delete are builtins, and are basically
// wrappers around malloc.
void ExplicitOperators() {
  // IWYU: IndirectClass needs a declaration
  // IWYU: IndirectClass is...*indirect.h
  IndirectClass* elem = (IndirectClass*)::operator new(sizeof(IndirectClass));
  ::operator delete(elem);

  // IWYU: IndirectClass needs a declaration
  IndirectClass* arr =
      // IWYU: IndirectClass needs a declaration
      // IWYU: IndirectClass is...*indirect.h
      (IndirectClass*)::operator new[](4 * sizeof(IndirectClass));
  ::operator delete[](arr);
}

// New- and delete-expressions, unless using placement syntax, only use builtin
// operators. They're equivalent with the above, but also run ctors/dtors.
// For placement syntax, see tests/cxx/placement_new.cc
void ExpressionsBuiltinTypes() {
  char* elem = new char;
  delete elem;

  int* arr = new int[4];
  delete[] arr;
}

// New- and delete-expressions with user-defined types.
void ExpressionsUserTypes() {
  // IWYU: IndirectClass needs a declaration
  // IWYU: IndirectClass is...*indirect.h
  IndirectClass* elem = new IndirectClass;
  // IWYU: IndirectClass is...*indirect.h
  delete elem;

  // IWYU: IndirectClass needs a declaration
  // IWYU: IndirectClass is...*indirect.h
  IndirectClass* arr = new IndirectClass[4];
  // IWYU: IndirectClass is...*indirect.h
  delete[] arr;
}

// Aligned allocation uses operator new(size_t, std::align_val_t) under the
// hood in C++17, but does not require <new> to be included for it. Pre-C++17,
// the alignment is silently ignored (or unsupported if the standard library
// does not support aligned allocation).
void ImplicitAlignedAllocation() {
  struct alignas(32) Aligned {
    float value[8];
  };

  Aligned* elem = new Aligned;
  delete elem;

  Aligned* arr = new Aligned[10];
  delete[] arr;
}

/**** IWYU_SUMMARY

tests/cxx/operator_new.cc should add these lines:
#include "tests/cxx/indirect.h"

tests/cxx/operator_new.cc should remove these lines:
- #include <new>  // lines XX-XX
- #include "tests/cxx/direct.h"  // lines XX-XX

The full include-list for tests/cxx/operator_new.cc:
#include "tests/cxx/indirect.h"  // for IndirectClass

***** IWYU_SUMMARY */