This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Support inc/dec operators on pointers
ClosedPublic

Authored by tbaeder on Nov 2 2022, 1:12 AM.

Details

Summary

I think this is pretty uncommon so I was trying to implement it without adding new opcodes, but looks like that's not possible.

Diff Detail

Event Timeline

tbaeder created this revision.Nov 2 2022, 1:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 2 2022, 1:12 AM
tbaeder requested review of this revision.Nov 2 2022, 1:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 2 2022, 1:12 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tbaeder updated this revision to Diff 472526.Nov 2 2022, 1:13 AM
aaron.ballman added inline comments.Nov 8 2022, 10:59 AM
clang/lib/AST/Interp/Opcodes.td
402–406

Don't both of these result in a Pointer? And should they have a Types field?

clang/test/AST/Interp/arrays.cpp
245–331

I'd like test cases where the pointer arithmetic has run off the beginning/end of the object (forming the invalid pointer is UB per http://eel.is/c++draft/expr.add#4.3 even if you never dereference the pointer).

tbaeder updated this revision to Diff 474182.Nov 9 2022, 12:55 AM
tbaeder marked an inline comment as done.
tbaeder added inline comments.Nov 9 2022, 8:00 AM
clang/lib/AST/Interp/Opcodes.td
402–406

Ah yes, they do. They don't need a type field, we know we're operating on a pointer.

aaron.ballman added inline comments.Nov 9 2022, 11:07 AM
clang/test/AST/Interp/arrays.cpp
245–331
constexpr int bad1() {
  const int *e =  E + 3;
  e++; // This is fine because it's a one-past-the-end pointer
  return *e; // This is UB
}

constexpr int bad2() {
  const int *e = E + 4;
  e++; // This is UB
  return *e; // This is UB as well
}

constexpr int bad3() {
  const int *e = E;
  --e; // This is UB
  return *e; // This is UB as well
}
tbaeder updated this revision to Diff 474724.Nov 11 2022, 4:20 AM
tbaeder marked 2 inline comments as done.
tbaeder added inline comments.Nov 11 2022, 4:22 AM
clang/test/AST/Interp/arrays.cpp
245–331

Interesting side-effect of removing the Run call in isPotentialConstantExpression(): The new constant interpreter doesn't diagnose the "never produces a constant expression` case. The function must be called for any diagnostics to be printed.

This revision is now accepted and ready to land.Nov 22 2022, 5:21 AM
shafik accepted this revision.Dec 21 2022, 1:05 PM

LGTM

This revision was landed with ongoing or failed builds.Jan 25 2023, 3:17 AM
This revision was automatically updated to reflect the committed changes.