[clang] [Clang] Improve diagnostics for 'placement new' with const storage argument (PR #144270)

Oliver Hunt via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 15 13:00:35 PDT 2025


================
@@ -2753,10 +2753,18 @@ 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 potential error.
       if (!R.isClassLookup() && Args.size() == 2 &&
           (Args[1]->getType()->isObjectPointerType() ||
            Args[1]->getType()->isArrayType())) {
+        if (Args[1]->getType()->isPointerType()) {
----------------
ojhunt wrote:

I think this would be improved by the addition of something to the effect of

```cpp
QualType Arg1Type = Args[1]->getType();
```

And then just reusing that.

But I think this check is in the wrong place, and should probably be earlier - this is inside an array of object check so I suspect the existing bad error message would likely occur with something like

```cpp
void f(const void* ptr) {
  new (ptr) int;
}
```

you probably want something to the effect of 
```cpp
if (Context.getBaseElementType(Arg1Type).isConstQualified()) {
   emit the error
   return true;
}
```



https://github.com/llvm/llvm-project/pull/144270


More information about the cfe-commits mailing list