[clang] [clang] Add value_type attr, use it to add noalias when pass-by-value. (PR #95004)

James Y Knight via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 14 15:05:01 PDT 2025


================
@@ -9242,3 +9242,15 @@ Declares that a function potentially allocates heap memory, and prevents any pot
 of ``nonallocating`` by the compiler.
   }];
 }
+
+def ValueTypeDocs : Documentation {
+  let Category = DocCatDecl;
+  let Content = [{
+The ``value_type`` attribute can be used to mark user-defined types as 'value
+types'. When objects of value types are passed value to functions, the objects
+are always considered to be formally copied into a new object. This means the
+argument itself must be the only value referencing the passed object. This
----------------
jyknight wrote:

NRVO copy-elision can enable a function which returns by value to stash the address of the returned value. Or, C++ constructors can do the same.

Then, copy-elision on the call can result in the callee receiving a object whose address is escaped, despite it receiving it "passed by value". E.g. in the following example, `&a == global` in the body of `foo`.

```
struct NonTrivial { NonTrivial(); };
NonTrivial *global;
NonTrivial frob() {
    NonTrivial result;
    global = &result;
    return result;
}

void foo(NonTrivial a);
int main() {
  foo(frob());
}
```

But, I don't think this new attribute is viable; the intent expressed by the PR message is to use it in libc++ to mark stdlib types. But, this cannot be a type property, because any code could do the above with any type.

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


More information about the cfe-commits mailing list