[PATCH] D135858: [clang][Interp] Support pointer arithmetic in binary operators

Shafik Yaghmour via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 24 21:39:03 PDT 2022


shafik added inline comments.


================
Comment at: clang/lib/AST/Interp/ByteCodeExprGen.cpp:259
+
+  if (Op != BO_Add && Op != BO_Sub)
+    return false;
----------------
or neither left or right operand is a pointer type


================
Comment at: clang/lib/AST/Interp/ByteCodeExprGen.cpp:271
+
+  if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) {
+    assert(E->getType()->isIntegerType());
----------------
If it is two pointers then it must be a subtraction.


================
Comment at: clang/lib/AST/Interp/ByteCodeExprGen.cpp:280
+  PrimType OffsetType;
+  if (LHS->getType()->isIntegerType() && RHS->getType()->isPointerType()) {
+    if (!visit(RHS) || !visit(LHS))
----------------



================
Comment at: clang/lib/AST/Interp/ByteCodeExprGen.cpp:284
+    OffsetType = *LT;
+  } else {
+    if (!visit(LHS) || !visit(RHS))
----------------



================
Comment at: clang/lib/AST/Interp/ByteCodeExprGen.cpp:288
+    OffsetType = *RT;
+  }
+
----------------



================
Comment at: clang/lib/AST/Interp/ByteCodeExprGen.cpp:241
+  // Pointer arithmethic special case. This is supported for one of
+  // LHS and RHS being a pointer type and the other being an integer type.
+  if (BO->getType()->isPointerType()) {
----------------
tbaeder wrote:
> shafik wrote:
> > I am not sure if this is the right place to handle this but there are a bunch of other cases.
> > 
> > - `nullptr` can have `0` added or subtracted
> > - You can only do addition/subtraction from a pointer if the result in within bounds or one after the end
> > - You can subtract two pointers if they point to the same object.
> > 
> > godbolt: https://godbolt.org/z/5YTY93z8M
> I will probably move this special case out of the function and into its own.
> Thanks for the tests, I think this should all already work except for adding two pointers. I'll update the diff once I checked that out.
You will also want to cover this case as well: https://eel.is/c++draft/expr.add#6


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135858/new/

https://reviews.llvm.org/D135858



More information about the cfe-commits mailing list