[PATCH] D156453: [clang][Interp] Create only globals when initializing a global variable

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 27 09:31:40 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, shafik, cor3ntin.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

For this code:

  struct O {
    int &&j;
  };
  
  O o1(0);

The generated AST for the initializer of `o1` is:

  VarDecl 0x62100006ab08 <array.cpp:119:3, col:9> col:5 o1 'O':'O' parenlistinit
  `-ExprWithCleanups 0x62100006b250 <col:7, col:9> 'O':'O'
    `-CXXParenListInitExpr 0x62100006b210 <col:7, col:9> 'O':'O'
      `-MaterializeTemporaryExpr 0x62100006b1f0 <col:8> 'int' xvalue
        `-IntegerLiteral 0x62100006abd0 <col:8> 'int' 0

Before this patch, we create a local temporary variable for the `MaterializeTemporaryExpr` and destroy it again when destroying the `EvalEmitter` we create to interpret the initializer. However, since `O::j` is a reference, this reference now points to a local variable that doesn't exist anymore.

I've run into this issue a few times before but always postponed working on it. Does this make sense? The only downside I see is that we might be creating more global variables.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156453

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/lib/AST/Interp/ByteCodeExprGen.h
  clang/test/AST/Interp/records.cpp
  clang/test/SemaCXX/paren-list-agg-init.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156453.544815.patch
Type: text/x-patch
Size: 3748 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230727/fe040016/attachment.bin>


More information about the cfe-commits mailing list