[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 09:26:21 PDT 2026
================
@@ -602,6 +653,60 @@ 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 PointerOffset(lhs, rhs, false, 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();
+ CompilerType rhs_unqualified_type = rhs_type.GetCanonicalType();
+ if (!lhs_unqualified_type.CompareTypes(rhs_unqualified_type)) {
+ 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));
+ assert(item_size > 0 && "Pointer size cannot be 0");
----------------
Michael137 wrote:
```suggestion
assert(item_size > 0 && "Pointee size cannot be 0");
```
https://github.com/llvm/llvm-project/pull/184652
More information about the lldb-commits
mailing list