[clang] [clang][bytecode] Compute pointer differences as 64bit integers (PR #137128)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 24 00:38:26 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
And only convert to the target type after that.
---
Full diff: https://github.com/llvm/llvm-project/pull/137128.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.h (+16-10)
- (added) clang/test/AST/ByteCode/i686.cpp (+14)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 588e0502fa88c..99b032bee9e3d 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2161,16 +2161,22 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC) {
}
}
- T A = LHS.isBlockPointer()
- ? (LHS.isElementPastEnd() ? T::from(LHS.getNumElems())
- : T::from(LHS.getIndex()))
- : T::from(LHS.getIntegerRepresentation());
- T B = RHS.isBlockPointer()
- ? (RHS.isElementPastEnd() ? T::from(RHS.getNumElems())
- : T::from(RHS.getIndex()))
- : T::from(RHS.getIntegerRepresentation());
-
- return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, A.bitWidth(), A, B);
+ int64_t A64 =
+ LHS.isBlockPointer()
+ ? (LHS.isElementPastEnd() ? LHS.getNumElems() : LHS.getIndex())
+ : LHS.getIntegerRepresentation();
+
+ int64_t B64 =
+ RHS.isBlockPointer()
+ ? (RHS.isElementPastEnd() ? RHS.getNumElems() : RHS.getIndex())
+ : RHS.getIntegerRepresentation();
+
+ int64_t R64 = A64 - B64;
+ if (static_cast<int64_t>(T::from(R64)) != R64)
+ return handleOverflow(S, OpPC, R64);
+
+ S.Stk.push<T>(T::from(R64));
+ return true;
}
//===----------------------------------------------------------------------===//
diff --git a/clang/test/AST/ByteCode/i686.cpp b/clang/test/AST/ByteCode/i686.cpp
new file mode 100644
index 0000000000000..ad914203d3c4b
--- /dev/null
+++ b/clang/test/AST/ByteCode/i686.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple i686-pc-linux-gnu -fexperimental-new-constant-interpreter -verify=expected,both %s
+// RUN: %clang_cc1 -triple i686-pc-linux-gnu -verify=ref,both %s
+
+
+char melchizedek[2200000000];
+typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
+constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0];
+constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // both-error {{constant expression}} \
+ // both-note {{ 2147483648 }}
+constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u];
+constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // both-error {{constant expression}} \
+ // both-note {{ -2147483649 }}
+
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/137128
More information about the cfe-commits
mailing list