[clang] [clang][ExprConst] Diagnose ptr subs with non-zero offset (PR #135938)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 17 07:01:33 PDT 2025
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/135938
>From 4a7fbbc62ee5b90627d053a641182d62bac38d09 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 16 Apr 2025 10:34:33 +0200
Subject: [PATCH] [clang][ExprConst] Diagnose ptr subs with non-zero offset
---
clang/lib/AST/ExprConstant.cpp | 3 ---
clang/test/AST/ByteCode/arrays.cpp | 2 +-
clang/test/SemaCXX/constant-expression-cxx11.cpp | 4 ++++
3 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index d1cc722fb7945..b14ff21a8ebc2 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14764,9 +14764,6 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
// Reject differing bases from the normal codepath; we special-case
// comparisons to null.
if (!HasSameBase(LHSValue, RHSValue)) {
- // Handle &&A - &&B.
- if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero())
- return Error(E);
const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>();
const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>();
diff --git a/clang/test/AST/ByteCode/arrays.cpp b/clang/test/AST/ByteCode/arrays.cpp
index f60cc19b09bd2..e50839e0f0877 100644
--- a/clang/test/AST/ByteCode/arrays.cpp
+++ b/clang/test/AST/ByteCode/arrays.cpp
@@ -107,7 +107,7 @@ static_assert(k1 == 1, "");
static_assert((&arr[0] - &arr[1]) == -1, "");
constexpr int k2 = &arr2[1] - &arr[0]; // both-error {{must be initialized by a constant expression}} \
- // expected-note {{arithmetic involving unrelated objects}}
+ // both-note {{arithmetic involving unrelated objects}}
static_assert((arr + 0) == arr, "");
static_assert(&arr[0] == arr, "");
diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp
index c35f3a5632a05..28016da925ef9 100644
--- a/clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -409,6 +409,10 @@ constexpr int a = 0;
constexpr int b = 1;
constexpr int n = &b - &a; // expected-error {{must be initialized by a constant expression}} \
// expected-note {{arithmetic involving unrelated objects '&b' and '&a' has unspecified value}}
+constexpr static int arrk[2] = {1,2};
+constexpr static int arrk2[2] = {3,4};
+constexpr int k2 = &arrk[1] - &arrk2[0]; // expected-error {{must be initialized by a constant expression}} \
+ // expected-note {{arithmetic involving unrelated objects}}
namespace MaterializeTemporary {
More information about the cfe-commits
mailing list