Skip to content

[Clang] Improve diagnostics for 'placement new' with const storage argument #144270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,9 @@ Improvements to Clang's diagnostics
- Clang now tries to avoid printing file paths that contain ``..``, instead preferring
the canonical file path if it ends up being shorter.

- Improve the diagnostics for placement new expression when const-qualified
object was passed as the storage argument. (#GH143708)

Improvements to Clang's time-trace
----------------------------------

Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -8308,6 +8308,9 @@ def err_need_header_before_typeid : Error<
def err_need_header_before_placement_new : Error<
"no matching %0 function for non-allocating placement new expression; "
"include <new>">;
def err_placement_new_into_const_qualified_storage : Error<
"placement new expression with a const-qualified argument of type %0 "
"is not allowed">;
def err_ms___leave_not_in___try : Error<
"'__leave' statement not in __try block">;
def err_uuidof_without_guid : Error<
Expand Down
12 changes: 11 additions & 1 deletion clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2752,10 +2752,20 @@ static bool resolveAllocationOverloadInterior(
if (Diagnose) {
// If this is an allocation of the form 'new (p) X' for some object
// pointer p (or an expression that will decay to such a pointer),
// diagnose the missing inclusion of <new>.
// diagnose the reason for the error.
if (!R.isClassLookup() && Args.size() == 2 &&
(Args[1]->getType()->isObjectPointerType() ||
Args[1]->getType()->isArrayType())) {
const QualType Arg1Type = Args[1]->getType();
QualType UnderlyingType = S.Context.getBaseElementType(Arg1Type);
if (UnderlyingType->isPointerType())
UnderlyingType = UnderlyingType->getPointeeType();
if (UnderlyingType.isConstQualified()) {
S.Diag(Args[1]->getExprLoc(),
diag::err_placement_new_into_const_qualified_storage)
<< Arg1Type << Args[1]->getSourceRange();
return true;
}
S.Diag(R.getNameLoc(), diag::err_need_header_before_placement_new)
<< R.getLookupName() << Range;
// Listing the candidates is unlikely to be useful; skip it.
Expand Down
41 changes: 41 additions & 0 deletions clang/test/SemaCXX/new-delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,47 @@ void no_matching_placement_new() {
(void)new(&buffer) X; // expected-error {{no matching 'operator new' function for non-allocating placement new expression; include <new>}}
}

void const_placement_new() {
const int value = 42;
(void)new(&value) int; // expected-error {{placement new expression with a const-qualified argument of type 'const int *' is not allowed}}
struct X { int n; };
const X cx = {5};
(void)new(&cx) X{10}; // expected-error {{placement new expression with a const-qualified argument of type 'const X *' is not allowed}}
const X* const cx2 = 0;
(void)new(cx2) X{10}; // expected-error {{placement new expression with a const-qualified argument of type 'const X *const' is not allowed}}
const int arr[1] = {1};
(void)new(&arr[0]) int(10); // expected-error {{placement new expression with a const-qualified argument of type 'const int *' is not allowed}}
const void* ptr = 0;
(void)new(ptr) int; // expected-error {{placement new expression with a const-qualified argument of type 'const void *' is not allowed}}
const int complex_arr[5][3] = {};
(void)new(&complex_arr[0][0]) int; // expected-error {{placement new expression with a const-qualified argument of type 'const int *' is not allowed}}
(void)new(complex_arr[0]) int; // expected-error {{placement new expression with a const-qualified argument of type 'const int[3]' is not allowed}}
const char str[] = "test";
(void)new(str) int; // expected-error {{placement new expression with a const-qualified argument of type 'const char[5]' is not allowed}}
const int* const* ptr_to_const_ptr_to_const = 0;
(void)new(ptr_to_const_ptr_to_const) int; // expected-error {{placement new expression with a const-qualified argument of type 'const int *const *' is not allowed}}
int* const* ptr_to_const_ptr = 0;
(void)new(ptr_to_const_ptr) int; // expected-error {{placement new expression with a const-qualified argument of type 'int *const *' is not allowed}}
typedef const int* ConstIntPtr;
ConstIntPtr cip = 0;
(void)new(cip) int; // expected-error {{placement new expression with a const-qualified argument of type 'ConstIntPtr' (aka 'const int *') is not allowed}}
typedef const void* ConstVoidPtr;
}

void const_placement_new_param(const void* ptr) {
new (ptr) int; // expected-error {{placement new expression with a const-qualified argument of type 'const void *' is not allowed}}
}

template<typename T>
void const_template_placement_new(const T* storage) {
(void)new(storage) int; // expected-error {{placement new expression with a const-qualified argument of type 'const int *' is not allowed}}
}

void const_template_placement_new_instantiation() {
int x = 5;
const_template_placement_new(&x); // expected-note {{in instantiation of function template specialization 'const_template_placement_new<int>' requested here}}
}

void good_deletes()
{
delete (int*)0;
Expand Down