[PATCH] D158265: [clang][AST] TextNodeDumper learned to output refers_to_enclosing_variable_or_capture flag for DeclRefExpr

Timo Stripf via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 18 05:21:39 PDT 2023


strimo378 added a comment.

It is not that easy. It is an important flag for lambdas and it took me quite a while analyzing codegen to find it. In general, it is very seldom useful. OpenMP seems to have to good test coverage of ast-dump test cases. I think it is also used for nested C function that can access the scope of the parent function.

Consider the following example:

  void func(int x) {
          x;
  
          [x]() { return x; };
  }

The outer DeclRefExpr #189 to x does not have the flag, the inner has the flag DeclRefExpr #195 .

  FunctionDecl #187 0x20ddd29c760 lc 0x20ddb844ed8 <D:\emmtrix\git\llvm2\test_clang_ccode\test74_lambda04_capture1_value1.cpp:2:1, line:6:1> line:2:6
  |-Decl: ParmVar 0x20ddd2520f0 #185 'x' 'int'#7 func 'void (int)'#186
  |-Decl: CXXRecord 0x20ddd29c950 #192 ''
  |-ParmVarDecl #185 0x20ddd2520f0 lc 0x20ddd29c760 <col:11, col:15> col:15 used x 'int'#7
  `-CompoundStmt 0x20ddd29cfe0 <col:18, line:6:1>
    |-DeclRefExpr #189 0x20ddd29c860 <line:3:2> 'int'#7 lvalue ParmVar 0x20ddd2520f0 #185 'x' 'int'#7**
    `-LambdaExpr #207 0x20ddd29ce50 <line:5:2, col:20> '(lambda at D:\emmtrix\git\llvm2\test_clang_ccode\test74_lambda04_capture1_value1.cpp:5:2)'#193
      |-CXXRecordDecl #192 0x20ddd29c950 lc 0x20ddd29c760 <col:2> col:2 implicit class definition '(lambda at D:\emmtrix\git\llvm2\test_clang_ccode\test74_lambda04_capture1_value1.cpp:5:2)'#193
      | |-DefinitionData lambda pass_in_registers standard_layout trivially_copyable can_const_default_init
      | | |-DefaultConstructor
      | | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
      | | |-MoveConstructor exists simple trivial needs_implicit
      | | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
      | | |-MoveAssignment
      | | `-Destructor simple irrelevant trivial
      | |-CXXMethodDecl #194 0x20ddd29ca98 lc 0x20ddd29c950 <col:6, col:20> col:2 operator() 'int () const'#202 typeloc 'auto () const'#191 inline
      | | `-CompoundStmt 0x20ddd29cd50 <col:8, col:20>
      | |   `-ReturnStmt 0x20ddd29cd40 <col:10, col:17>
      | |     `-ImplicitCastExpr #203 0x20ddd29cd20 <col:17> 'int'#7 <LValueToRValue>
      | |       `-DeclRefExpr #195 0x20ddd29cb50 <col:17> 'const int'#7 lvalue ParmVar 0x20ddd2520f0 #185 'x' 'int'#7 refers_to_enclosing_variable_or_capture
      | `-FieldDecl #206 0x20ddd29cdc8 lc 0x20ddd29c950 <col:3> col:3 implicit 'int'#7
      |-ImplicitCastExpr #205 0x20ddd29cd90 <col:3> 'int'#7 <LValueToRValue>
      | `-DeclRefExpr #204 0x20ddd29cd68 <col:3> 'int'#7 lvalue ParmVar 0x20ddd2520f0 #185 'x' 'int'#7
      `-CompoundStmt 0x20ddd29cd50 <col:8, col:20>
        `-ReturnStmt 0x20ddd29cd40 <col:10, col:17>
          `-ImplicitCastExpr #203 0x20ddd29cd20 <col:17> 'int'#7 <LValueToRValue>
            `-DeclRefExpr #195 0x20ddd29cb50 <col:17> 'const int'#7 lvalue ParmVar 0x20ddd2520f0 #185 'x' 'int'#7 refers_to_enclosing_variable_or_capture


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D158265



More information about the cfe-commits mailing list