[Lldb-commits] [lldb] [lldb] Add pointer arithmetics for addition and subtraction to DIL (PR #184652)
Michael Buch via lldb-commits
lldb-commits at lists.llvm.org
Fri Mar 13 01:36:44 PDT 2026
================
@@ -602,6 +642,62 @@ llvm::Expected<lldb::ValueObjectSP> Interpreter::EvaluateBinarySubtraction(
if (result_type.IsScalarType())
return EvaluateScalarOp(BinaryOpKind::Sub, lhs, rhs, result_type, location);
+ auto lhs_type = lhs->GetCompilerType();
+ auto rhs_type = rhs->GetCompilerType();
+
+ // "pointer - integer" operation.
+ if (lhs_type.IsPointerType() && rhs_type.IsInteger())
+ return PointerAdd(lhs, -rhs->GetValueAsUnsigned(0), location);
+
+ // "pointer - pointer" operation.
+ if (lhs_type.IsPointerType() && rhs_type.IsPointerType()) {
+ if (lhs_type.IsPointerToVoid() && rhs_type.IsPointerToVoid()) {
+ return llvm::make_error<DILDiagnosticError>(
+ m_expr, "arithmetic on pointers to void", location);
+ }
+ // Compare canonical unqualified pointer types.
+ CompilerType lhs_unqualified_type =
+ lhs_type.GetCanonicalType().GetFullyUnqualifiedType();
+ CompilerType rhs_unqualified_type =
+ rhs_type.GetCanonicalType().GetFullyUnqualifiedType();
+ bool comparable = lhs_unqualified_type.CompareTypes(rhs_unqualified_type);
+ if (!comparable) {
+ std::string errMsg = llvm::formatv(
+ "'{0}' and '{1}' are not pointers to compatible types",
+ orig_lhs_type.GetTypeName(), orig_rhs_type.GetTypeName());
+ return llvm::make_error<DILDiagnosticError>(m_expr, errMsg, location);
+ }
+
+ llvm::Expected<uint64_t> lhs_byte_size =
+ lhs_type.GetPointeeType().GetByteSize(m_exe_ctx_scope.get());
+ if (!lhs_byte_size)
+ return lhs_byte_size.takeError();
+ // Since pointers have compatible types, both have the same pointee size.
+ int64_t item_size = *lhs_byte_size;
+ int64_t diff = static_cast<int64_t>(lhs->GetValueAsUnsigned(0) -
+ rhs->GetValueAsUnsigned(0));
+ if (diff % item_size != 0) {
+ // If address difference isn't divisible by pointee size then performing
+ // the operation is undefined behaviour.
+ return llvm::make_error<DILDiagnosticError>(
+ m_expr, "undefined pointer arithmetic", location);
+ }
+ diff /= item_size;
----------------
Michael137 wrote:
can we add an `assert (item_size > 0)`?
https://github.com/llvm/llvm-project/pull/184652
More information about the lldb-commits
mailing list