This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Fix ignoring TypeTraitExprs
ClosedPublic

Authored by tbaeder on May 4 2023, 2:57 AM.

Diff Detail

Event Timeline

tbaeder created this revision.May 4 2023, 2:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2023, 2:57 AM
tbaeder requested review of this revision.May 4 2023, 2:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2023, 2:57 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman added inline comments.May 4 2023, 10:39 AM
clang/test/AST/Interp/literals.cpp
898

Let's make sure we still reject this:

constexpr int oh_my() {
  int x = 0;
  sizeof(int[x++]); // This would usually be evaluated because VLAs are terrible
  return x;
}

https://godbolt.org/z/E3jx6co46

tbaeder added inline comments.May 7 2023, 1:09 AM
clang/test/AST/Interp/literals.cpp
898

Hm, no, doesn't get rejected:

array.cpp:1240:3: warning: expression result unused [-Wunused-value]
 1240 |   sizeof(int[x++]); // This would usually be evaluated because VLAs are terrible
      |   ^~~~~~~~~~~~~~~~
array.cpp:1243:15: error: static assertion failed due to requirement 'oh_my() == 1'
 1243 | static_assert(oh_my() == 1);
      |               ^~~~~~~~~~~~
array.cpp:1243:23: note: expression evaluates to '0 == 1'
 1243 | static_assert(oh_my() == 1);
      |               ~~~~~~~~^~~~
aaron.ballman added inline comments.May 8 2023, 5:10 AM
clang/test/AST/Interp/literals.cpp
898

It looks to me like we're not modelling the side effects properly (we would return 1 if we were, so the static_assert would have passed), which might explain why we're failing to reject the code.

tbaeder added inline comments.May 8 2023, 5:24 AM
clang/test/AST/Interp/literals.cpp
898

Right, I understand, I was just showing the current state. From looking at it though, this is not something to be fixed here, the x++ is passed stand-alone to evaluateAsRValue(). Not sure yet what the exact problem is, I guess this is called before we even registered a local variable for x. I'll look into it.

tbaeder added inline comments.May 8 2023, 5:59 AM
clang/test/AST/Interp/literals.cpp
898

Fun, the ArgumentExpr is just x++:

|-UnaryExprOrTypeTraitExpr 0x621000072430 <line:1280:3, col:18> 'unsigned long' sizeof 'int[x++]'
| `-UnaryOperator 0x621000072378 <col:14, col:15> 'int' postfix '++'
|   `-DeclRefExpr 0x621000072350 <col:14> 'int' lvalue Var 0x6210000721a0 'x' 'int'

it's simple to implement just rejecting it by just doing discard(ArgumentExpr), but that doesn't emit any diagnostics.

tbaeder added inline comments.May 8 2023, 6:06 AM
clang/test/AST/Interp/literals.cpp
898

... and now I just realized that the tests for this patch don't even fit with the changes themselves. TypeTraitExpr is not for sizeof and alignof...

tbaeder updated this revision to Diff 520340.May 8 2023, 6:10 AM
This revision is now accepted and ready to land.May 8 2023, 10:11 AM
This revision was landed with ongoing or failed builds.Aug 1 2023, 1:58 AM
This revision was automatically updated to reflect the committed changes.