This is an archive of the discontinued LLVM Phabricator instance.

[clang][AST] TextNodeDumper learned to output refers_to_enclosing_variable_or_capture flag for DeclRefExpr
ClosedPublic

Authored by strimo378 on Aug 18 2023, 2:56 AM.

Diff Detail

Event Timeline

strimo378 created this revision.Aug 18 2023, 2:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 18 2023, 2:56 AM
strimo378 requested review of this revision.Aug 18 2023, 2:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 18 2023, 2:56 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

This adds quite a bit of output to the dump; given that the DeclRefExpr already links back to the Decl it refers to, do we actually need to expose this directly instead of letting users infer the relationship through pointer matching?

strimo378 added a comment.EditedAug 18 2023, 5:21 AM

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 used. 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
aaron.ballman accepted this revision.Aug 18 2023, 6:37 AM

Ah, I see where the complexity is now, thank you. In that case, this is reasonably helpful, LGTM!

This revision is now accepted and ready to land.Aug 18 2023, 6:37 AM
This revision was landed with ongoing or failed builds.Aug 18 2023, 7:15 AM
This revision was automatically updated to reflect the committed changes.