[PATCH] D139586: [Clang][C++23] Lifetime extension in range-based for loops

PoYao Chang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 7 15:11:24 PST 2022


rZhBoYao added a comment.

Consider:

  struct T {
    const int *begin() const;
    const int *end()   const;
    T &r() [[clang::lifetimebound]];
    T t();
  };
  
  const T &f1(const T &t [[clang::lifetimebound]]) { return t; }
  T g();
  
  void foo() {
    for (auto e : f1(g())) {}
    for (auto e : g().t().t().r()) {}
  }

The first __range1:

  | | `-VarDecl 0x11e901358 <col:17, col:23> col:17 implicit used __range1 'const T &' cinit
  | |   `-ExprWithCleanups 0x11e9015b8 <col:17, col:23> 'const T':'const T' lvalue
  | |     `-CallExpr 0x11e9010b0 <col:17, col:23> 'const T':'const T' lvalue
  | |       |-ImplicitCastExpr 0x11e901098 <col:17> 'const T &(*)(const T &)' <FunctionToPointerDecay>
  | |       | `-DeclRefExpr 0x11e901018 <col:17> 'const T &(const T &)' lvalue Function 0x11e900a48 'f1' 'const T &(const T &)'
  | |       `-MaterializeTemporaryExpr 0x11e9010f0 <col:20, col:22> 'const T':'const T' lvalue extended by Var 0x11e901358 '__range1' 'const T &'
  | |         `-ImplicitCastExpr 0x11e9010d8 <col:20, col:22> 'const T':'const T' <NoOp>
  | |           `-CallExpr 0x11e900ec0 <col:20, col:22> 'T':'T'
  | |             `-ImplicitCastExpr 0x11e900ea8 <col:20> 'T (*)()' <FunctionToPointerDecay>
  | |               `-DeclRefExpr 0x11e900e30 <col:20> 'T ()' lvalue Function 0x11e900bd0 'g' 'T ()'

g() is extended by __range1.

The second __range1:

  | `-VarDecl 0x11e9040a0 <col:17, col:31> col:17 implicit used __range1 'T &' cinit
  |   `-ExprWithCleanups 0x11e904338 <col:17, col:31> 'T':'T' lvalue
  |     `-CXXMemberCallExpr 0x11e903f68 <col:17, col:31> 'T':'T' lvalue
  |       `-MemberExpr 0x11e903f38 <col:17, col:29> '<bound member function type>' .r 0x11e900638
  |         `-MaterializeTemporaryExpr 0x11e903f20 <col:17, col:27> 'T':'T' xvalue extended by Var 0x11e9040a0 '__range1' 'T &'
  |           `-CXXMemberCallExpr 0x11e903f00 <col:17, col:27> 'T':'T'
  |             `-MemberExpr 0x11e903ed0 <col:17, col:25> '<bound member function type>' .t 0x11e900780
  |               `-MaterializeTemporaryExpr 0x11e903eb8 <col:17, col:23> 'T':'T' xvalue extended by Var 0x11e9040a0 '__range1' 'T &'
  |                 `-CXXMemberCallExpr 0x11e903e68 <col:17, col:23> 'T':'T'
  |                   `-MemberExpr 0x11e903e38 <col:17, col:21> '<bound member function type>' .t 0x11e900780
  |                     `-MaterializeTemporaryExpr 0x11e903e20 <col:17, col:19> 'T':'T' xvalue extended by Var 0x11e9040a0 '__range1' 'T &'
  |                       `-CallExpr 0x11e903e00 <col:17, col:19> 'T':'T'
  |                         `-ImplicitCastExpr 0x11e903de8 <col:17> 'T (*)()' <FunctionToPointerDecay>
  |                           `-DeclRefExpr 0x11e903dc8 <col:17> 'T ()' lvalue Function 0x11e900bd0 'g' 'T ()'

g() and 2 t()'s are extended by __range1.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139586/new/

https://reviews.llvm.org/D139586



More information about the cfe-commits mailing list