Skip to content

Commit

Permalink
Add more complete testing for operator new
Browse files Browse the repository at this point in the history
This replaces builtins_new_included.cc with operator_new.cc, and covers
all variants of operator new that do _not_ require <new> to be
included:

- Raw explicit ::operator new/::operator delete calls
- New expressions, both of builtin and user-defined types
- Aligned allocation (currently disabled, as we misdiagnose it,
  see PR include-what-you-use#777).
  • Loading branch information
kimgr committed Apr 12, 2020
1 parent de51501 commit 34f5c8c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 31 deletions.
2 changes: 2 additions & 0 deletions run_iwyu_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def setUp(self):
self.Include('macro_defined_by_includer-prefix.h')],
'macro_location.cc': ['-Wno-sizeof-pointer-div'],
'ms_inline_asm.cc': ['-fms-extensions'],
'operator_new.cc': ['-std=c++17'],
'placement_new.cc': ['-std=c++17'],
'prefix_header_attribution.cc': [self.Include('prefix_header_attribution-d1.h')],
'prefix_header_includes_add.cc': prefix_headers,
Expand Down Expand Up @@ -181,6 +182,7 @@ def setUp(self):
'no_fwd_decls.cc': ['.'],
'no_h_includes_cc.cc': ['.'],
'non_transitive_include.cc': ['.'],
'operator_new.cc': ['.'],
'overloaded_class.cc': ['.'],
'pch_in_code.cc': ['.'],
'pointer_arith.cc': ['.'],
Expand Down
31 changes: 0 additions & 31 deletions tests/cxx/builtins_new_included.cc

This file was deleted.

85 changes: 85 additions & 0 deletions tests/cxx/operator_new.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//===--- 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.
//
//===----------------------------------------------------------------------===//

// 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
// IWYU: IndirectClass is...*indirect.h
IndirectClass* arr = (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;
}

#if 0
// 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 just ignored.
void ImplicitAlignedAllocation() {
struct alignas(32) Aligned {
float value[8];
};

Aligned* elem = new Aligned;
delete elem;

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

/**** 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 */

0 comments on commit 34f5c8c

Please sign in to comment.