This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] Fix a crash on copy elided initialized lambda captures
ClosedPublic

Authored by isuckatcs on Aug 12 2022, 8:27 AM.

Details

Summary
struct S {
  S(int) {}
};

void foo(int x) {
  [s = S(x)] {};
}

Before C++17 the AST of s = S(x) is

|-CXXConstructExpr 'S':'S' 'void (S &&) noexcept' elidable
| `-MaterializeTemporaryExpr 'S':'S' xvalue
|   `-CXXFunctionalCastExpr 'S':'S' functional cast to S <ConstructorConversion>
|     `-CXXConstructExpr 'S':'S' 'void (int)'
|       `-ImplicitCastExpr 'int' <LValueToRValue>
|         `-DeclRefExpr 'int' lvalue ParmVar 'x' 'int'

With C++17 copy elision the AST of the same expression is

|-CXXFunctionalCastExpr 0x564d0bbcdfa8 <col:8, col:11> 'S':'S' functional cast to S <ConstructorConversion>
| `-CXXConstructExpr 0x564d0bbcde40 <col:8, col:11> 'S':'S' 'void (int)'
|   `-ImplicitCastExpr 0x564d0bbcdd28 <col:10> 'int' <LValueToRValue>
|     `-DeclRefExpr 0x564d0bbcd990 <col:10> 'int' lvalue ParmVar 0x564d0bbcd7d0 'x' 'int'

The copy elided version wasn't handled, which lead to a crash later.

Diff Detail

Event Timeline

isuckatcs created this revision.Aug 12 2022, 8:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 12 2022, 8:27 AM
isuckatcs requested review of this revision.Aug 12 2022, 8:27 AM
NoQ accepted this revision.Aug 12 2022, 12:59 PM

Oh right, functional casts *sigh* Thanks!!

This revision is now accepted and ready to land.Aug 12 2022, 12:59 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 12 2022, 3:22 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript