forked from include-what-you-use/include-what-you-use
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more complete testing for operator new
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
Showing
3 changed files
with
87 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |