[clang] [Clang] Implement P2718R0 "Lifetime extension in range-based for loops" (PR #76361)

via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 27 21:41:28 PST 2024


yronglin wrote:

Should we also dump the sub-expr which in `CXXDefaultArgExpr`:
for example:
Consider:
```
struct A {
  A();
  ~A();
};

typedef int vec3[3];
vec3 &f(const A &a = A{});

void foo() {
  for (auto e : f()) {}
}
```
Current AST Dump:
```
FunctionDecl 0x126813170 <line:85:1, line:87:1> line:85:6 foo 'void ()'
  `-CompoundStmt 0x126814720 <col:12, line:87:1>
    `-CXXForRangeStmt 0x1268146b0 <line:86:3, col:23>
      |-<<<NULL>>>
      |-DeclStmt 0x1268137b8 <col:17>
      | `-VarDecl 0x126813548 <col:17, col:19> col:17 implicit used __range1 'vec3 &' cinit
      |   `-ExprWithCleanups 0x126813730 <col:17, col:19> 'vec3':'int[3]' lvalue
      |     `-CallExpr 0x126813318 <col:17, col:19> 'vec3':'int[3]' lvalue
      |       |-ImplicitCastExpr 0x126813300 <col:17> 'vec3 &(*)(const A &)' <FunctionToPointerDecay>
      |       | `-DeclRefExpr 0x126813278 <col:17> 'vec3 &(const A &)' lvalue Function 0x126813030 'f' 'vec3 &(const A &)'
      |       `-CXXDefaultArgExpr 0x1268133f0 <<invalid sloc>> 'const A' lvalue
      |-DeclStmt 0x126814448 <col:15>
      | `-VarDecl 0x126813880 <col:15> col:15 implicit used __begin1 'int *' cinit
 .......
```
We can't see directly on the AST whether the lifetime is extended or not. The sub-expr which in `CXXDefaultArgExpr` is:
```
ExprWithCleanups 0x1398c55d8 'const A':'const struct A' lvalue
`-MaterializeTemporaryExpr 0x1398c55c0 'const A':'const struct A' lvalue extended by Var 0x1398c5748 '__range1' 'vec3 &'
  `-ImplicitCastExpr 0x1398c55a8 'const A':'const struct A' <NoOp>
    `-CXXBindTemporaryExpr 0x1398c5588 'A':'struct A' (CXXTemporary 0x1398c5588)
      `-CXXTemporaryObjectExpr 0x1398c5550 'A':'struct A' 'void (void)'
```


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


More information about the cfe-commits mailing list