[Lldb-commits] [lldb] [lldb] Add comparison operators to DIL (PR #208832)
Ilia Kuklin via lldb-commits
lldb-commits at lists.llvm.org
Mon Aug 17 11:13:14 PDT 2026
https://github.com/kuilpd updated https://github.com/llvm/llvm-project/pull/208832
>From be452a3097523e05d98357184b25be8a81e4a92a Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Thu, 9 Jul 2026 16:46:53 +0500
Subject: [PATCH 1/4] [lldb] Add comparison operators to DIL
---
lldb/docs/dil-expr-lang.ebnf | 10 +-
lldb/include/lldb/ValueObject/DILAST.h | 19 +-
lldb/include/lldb/ValueObject/DILEval.h | 7 +
lldb/include/lldb/ValueObject/DILLexer.h | 6 +
lldb/include/lldb/ValueObject/DILParser.h | 2 +
lldb/source/ValueObject/DILAST.cpp | 12 ++
lldb/source/ValueObject/DILEval.cpp | 202 ++++++++++++++++++
lldb/source/ValueObject/DILLexer.cpp | 54 +++--
lldb/source/ValueObject/DILParser.cpp | 55 ++++-
.../frame/var-dil/expr/Comparison/Makefile | 3 +
.../TestFrameVarDILExprComparison.py | 173 +++++++++++++++
.../frame/var-dil/expr/Comparison/main.cpp | 34 +++
12 files changed, 557 insertions(+), 20 deletions(-)
create mode 100644 lldb/test/API/commands/frame/var-dil/expr/Comparison/Makefile
create mode 100644 lldb/test/API/commands/frame/var-dil/expr/Comparison/TestFrameVarDILExprComparison.py
create mode 100644 lldb/test/API/commands/frame/var-dil/expr/Comparison/main.cpp
diff --git a/lldb/docs/dil-expr-lang.ebnf b/lldb/docs/dil-expr-lang.ebnf
index 23d4f1894172b..9d7d2a9b09072 100644
--- a/lldb/docs/dil-expr-lang.ebnf
+++ b/lldb/docs/dil-expr-lang.ebnf
@@ -20,7 +20,15 @@ inclusive_or_expression = exclusive_or_expression {"|" exclusive_or_expression}
exclusive_or_expression = and_expression {"^" and_expression} ;
-and_expression = shift_expression {"&" shift_expression} ;
+and_expression = equality_expression {"&" equality_expression} ;
+
+equality_expression = relational_expression {"==" relational_expression}
+ | relational_expression {"!=" relational_expression} ;
+
+relational_expression = shift_expression {"<" shift_expression}
+ | shift_expression {">" shift_expression}
+ | shift_expression {"<=" shift_expression}
+ | shift_expression {">=" shift_expression} ;
shift_expression = additive_expression {"<<" additive_expression}
| additive_expression {">>" additive_expression} ;
diff --git a/lldb/include/lldb/ValueObject/DILAST.h b/lldb/include/lldb/ValueObject/DILAST.h
index 874c93b761242..90bdea13273a4 100644
--- a/lldb/include/lldb/ValueObject/DILAST.h
+++ b/lldb/include/lldb/ValueObject/DILAST.h
@@ -45,21 +45,27 @@ enum class UnaryOpKind {
/// The binary operators recognized by DIL.
enum class BinaryOpKind {
- Add, ///< "+"
- AddAssign, ///< "+="
Assign, ///< "="
- Div, ///< "/"
+ Add, ///< "+"
+ Sub, ///< "-"
Mul, ///< "*"
+ Div, ///< "/"
Rem, ///< "%"
And, ///< "&"
Xor, ///< "^"
Or, ///< "|"
Shl, ///< "<<"
Shr, ///< ">>"
- Sub, ///< "-"
+ AddAssign, ///< "+="
SubAssign, ///< "-="
LAnd, ///< "&&"
LOr, ///< "||"
+ LT, ///< "<"
+ GT, ///< ">"
+ LE, ///< "<="
+ GE, ///< ">="
+ EQ, ///< "=="
+ NE, ///< "!="
};
/// Translates DIL tokens to BinaryOpKind.
@@ -95,6 +101,8 @@ class ASTNode {
virtual llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const = 0;
+ virtual bool IsConstLiteral() const { return false; }
+
uint32_t GetLocation() const { return m_location; }
NodeKind GetKind() const { return m_kind; }
@@ -254,6 +262,7 @@ class IntegerLiteralNode : public ASTNode {
llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
+ bool IsConstLiteral() const override { return true; }
const llvm::APInt &GetValue() const { return m_value; }
uint32_t GetRadix() const { return m_radix; }
bool IsUnsigned() const { return m_is_unsigned; }
@@ -278,6 +287,7 @@ class FloatLiteralNode : public ASTNode {
llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
+ bool IsConstLiteral() const override { return true; }
const llvm::APFloat &GetValue() const { return m_value; }
static bool classof(const ASTNode &node) {
@@ -295,6 +305,7 @@ class BooleanLiteralNode : public ASTNode {
llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
+ bool IsConstLiteral() const override { return true; }
bool GetValue() const & { return m_value; }
static bool classof(const ASTNode &node) {
diff --git a/lldb/include/lldb/ValueObject/DILEval.h b/lldb/include/lldb/ValueObject/DILEval.h
index 97fe72e448d65..ce08961cc7db9 100644
--- a/lldb/include/lldb/ValueObject/DILEval.h
+++ b/lldb/include/lldb/ValueObject/DILEval.h
@@ -107,6 +107,13 @@ class Interpreter : Visitor {
lldb::ValueObjectSP rhs,
CompilerType result_type,
uint32_t location);
+ llvm::Error ValidateComparison(BinaryOpKind kind, lldb::ValueObjectSP &lhs,
+ lldb::ValueObjectSP &rhs, bool lhs_is_literal,
+ bool rhs_is_literal, uint32_t location);
+ llvm::Expected<lldb::ValueObjectSP>
+ EvaluateComparison(BinaryOpKind kind, lldb::ValueObjectSP lhs,
+ lldb::ValueObjectSP rhs, bool lhs_is_literal,
+ bool rhs_is_literal, uint32_t location);
llvm::Expected<lldb::ValueObjectSP>
EvaluateBinaryShift(BinaryOpKind kind, lldb::ValueObjectSP lhs,
lldb::ValueObjectSP rhs, uint32_t location);
diff --git a/lldb/include/lldb/ValueObject/DILLexer.h b/lldb/include/lldb/ValueObject/DILLexer.h
index aef26908d2d0e..21953c60e5198 100644
--- a/lldb/include/lldb/ValueObject/DILLexer.h
+++ b/lldb/include/lldb/ValueObject/DILLexer.h
@@ -33,8 +33,12 @@ class Token {
coloncolon,
eof,
equal,
+ equalequal,
exclaim,
+ exclaimequal,
float_constant,
+ greater,
+ greaterequal,
greatergreater,
identifier,
integer_constant,
@@ -42,6 +46,8 @@ class Token {
kw_true,
l_paren,
l_square,
+ less,
+ lessequal,
lessless,
minus,
minusequal,
diff --git a/lldb/include/lldb/ValueObject/DILParser.h b/lldb/include/lldb/ValueObject/DILParser.h
index 6c5d5ee8fc081..fbfe35314ff71 100644
--- a/lldb/include/lldb/ValueObject/DILParser.h
+++ b/lldb/include/lldb/ValueObject/DILParser.h
@@ -89,6 +89,8 @@ class DILParser {
ASTNodeUP ParseInclusiveOrExpression();
ASTNodeUP ParseExclusiveOrExpression();
ASTNodeUP ParseAndExpression();
+ ASTNodeUP ParseEqualityExpression();
+ ASTNodeUP ParseRelationalExpression();
ASTNodeUP ParseShiftExpression();
ASTNodeUP ParseAdditiveExpression();
ASTNodeUP ParseMultiplicativeExpression();
diff --git a/lldb/source/ValueObject/DILAST.cpp b/lldb/source/ValueObject/DILAST.cpp
index d0f89601385f2..666e8708812fa 100644
--- a/lldb/source/ValueObject/DILAST.cpp
+++ b/lldb/source/ValueObject/DILAST.cpp
@@ -43,6 +43,18 @@ BinaryOpKind GetBinaryOpKindFromToken(Token::Kind token_kind) {
return BinaryOpKind::LAnd;
case Token::pipepipe:
return BinaryOpKind::LOr;
+ case Token::less:
+ return BinaryOpKind::LT;
+ case Token::greater:
+ return BinaryOpKind::GT;
+ case Token::lessequal:
+ return BinaryOpKind::LE;
+ case Token::greaterequal:
+ return BinaryOpKind::GE;
+ case Token::equalequal:
+ return BinaryOpKind::EQ;
+ case Token::exclaimequal:
+ return BinaryOpKind::NE;
default:
break;
}
diff --git a/lldb/source/ValueObject/DILEval.cpp b/lldb/source/ValueObject/DILEval.cpp
index a06a68272b047..83a9a19c84488 100644
--- a/lldb/source/ValueObject/DILEval.cpp
+++ b/lldb/source/ValueObject/DILEval.cpp
@@ -717,6 +717,18 @@ Interpreter::EvaluateScalarOp(BinaryOpKind kind, lldb::ValueObjectSP lhs,
return value_object(l << r);
case BinaryOpKind::Shr:
return value_object(l >> r);
+ case BinaryOpKind::LT:
+ return value_object(l < r);
+ case BinaryOpKind::GT:
+ return value_object(l > r);
+ case BinaryOpKind::LE:
+ return value_object(l <= r);
+ case BinaryOpKind::GE:
+ return value_object(l >= r);
+ case BinaryOpKind::EQ:
+ return value_object(l == r);
+ case BinaryOpKind::NE:
+ return value_object(l != r);
default:
break;
}
@@ -970,6 +982,186 @@ Interpreter::EvaluateAssignment(lldb::ValueObjectSP lhs,
return lhs;
}
+static bool IsLiteralZero(lldb::ValueObjectSP &val, bool is_literal) {
+ bool is_zero = val->GetValueAsUnsigned(-1) == 0;
+ bool is_boolean = val->GetCompilerType().IsBoolean();
+ return is_zero && !is_boolean && is_literal;
+}
+
+llvm::Error
+Interpreter::ValidateComparison(BinaryOpKind kind, lldb::ValueObjectSP &lhs,
+ lldb::ValueObjectSP &rhs, bool lhs_is_literal,
+ bool rhs_is_literal, uint32_t location) {
+ auto orig_lhs_type = lhs->GetCompilerType();
+ auto orig_rhs_type = rhs->GetCompilerType();
+
+ if (orig_lhs_type == orig_rhs_type)
+ return llvm::Error::success();
+
+ bool is_ordered = (kind == BinaryOpKind::LT || kind == BinaryOpKind::LE ||
+ kind == BinaryOpKind::GT || kind == BinaryOpKind::GE);
+ bool lhs_nullptr_or_zero =
+ orig_lhs_type.IsNullPtrType() || IsLiteralZero(lhs, lhs_is_literal);
+ bool rhs_nullptr_or_zero =
+ orig_rhs_type.IsNullPtrType() || IsLiteralZero(rhs, rhs_is_literal);
+
+ if (orig_lhs_type.IsArrayType())
+ lhs = ArrayToPointerConversion(*lhs, m_stack_frame, "result");
+ if (orig_rhs_type.IsArrayType())
+ rhs = ArrayToPointerConversion(*rhs, m_stack_frame, "result");
+
+ CompilerType lhs_type = lhs->GetCompilerType();
+ CompilerType rhs_type = rhs->GetCompilerType();
+ lldb::ValueObjectSP lhs_child;
+ lldb::ValueObjectSP rhs_child;
+ bool is_signed;
+
+ if (!lhs_nullptr_or_zero && !lhs_type.IsPointerType() &&
+ !lhs_type.IsIntegerOrEnumerationType(is_signed)) {
+ // lhs is not a nullptr, pointer, enum or integer. Check to see if its
+ // first child could be a pointer. If so, update lhs_type accordingly.
+ lhs_child = lhs->GetChildAtIndex(0);
+ if (lhs_child && (lhs_child->IsPointerType() ||
+ lhs_child->GetCompilerType().IsNullPtrType()))
+ lhs_type = lhs_child->GetCompilerType();
+ }
+ if (!rhs_nullptr_or_zero && !rhs_type.IsPointerType() &&
+ !rhs_type.IsIntegerOrEnumerationType(is_signed)) {
+ // rhs is not a nullptr, pointer, enum or integer. Check to see if its
+ // first child could be a pointer. If so, update rhs_type accordingly.
+ rhs_child = rhs->GetChildAtIndex(0);
+ if (rhs_child && (rhs_child->IsPointerType() ||
+ rhs_child->GetCompilerType().IsNullPtrType()))
+ rhs_type = rhs_child->GetCompilerType();
+ }
+
+ if ((lhs_type != orig_lhs_type) || (rhs_type != orig_rhs_type)) {
+ if (lhs_type.IsNullPtrType() || rhs_type.IsNullPtrType())
+ return llvm::Error::success();
+
+ // May be an integer or enum.
+ if (!lhs_type.IsPointerType() || !rhs_type.IsPointerType())
+ return llvm::Error::success();
+
+ CompilerType lhs_unqualified =
+ lhs_type.GetCanonicalType().GetFullyUnqualifiedType();
+ CompilerType rhs_unqualified =
+ rhs_type.GetCanonicalType().GetFullyUnqualifiedType();
+
+ if (lhs_unqualified.IsPointerToVoid() || rhs_unqualified.IsPointerToVoid())
+ return llvm::Error::success();
+
+ // We have two pointers, neither of which is nullptr or void *. Make
+ // sure their types are compatible.
+ bool comparable = lhs_unqualified.CompareTypes(rhs_unqualified);
+ if (comparable)
+ return llvm::Error::success();
+
+ std::string errMsg = llvm::formatv(
+ "comparison of distinct pointer types ({0} and {1})",
+ orig_lhs_type.TypeDescription(), orig_rhs_type.TypeDescription());
+ return llvm::make_error<DILDiagnosticError>(m_expr, errMsg, location);
+ }
+
+ if (!is_ordered && ((orig_lhs_type.IsNullPtrType() && rhs_nullptr_or_zero) ||
+ (lhs_nullptr_or_zero && orig_rhs_type.IsNullPtrType())))
+ return llvm::Error::success();
+
+ // If the operands has arithmetic or enumeration type (scoped or unscoped),
+ // usual arithmetic conversions are performed on both operands following the
+ // rules for arithmetic operators.
+ auto type_or_err = ArithmeticConversion(lhs, rhs, location);
+ if (!type_or_err)
+ return type_or_err.takeError();
+
+ lhs_type = lhs->GetCompilerType();
+ rhs_type = rhs->GetCompilerType();
+ if (lhs_type.IsScalarOrUnscopedEnumerationType() &&
+ rhs_type.IsScalarOrUnscopedEnumerationType())
+ return llvm::Error::success();
+
+ // Scoped enums can be compared only to the instances of the same type.
+ if (lhs_type.IsScopedEnumerationType() ||
+ rhs_type.IsScopedEnumerationType()) {
+ if (lhs_type.CompareTypes(rhs_type))
+ return llvm::Error::success();
+ std::string errMsg = llvm::formatv(
+ "invalid operands to binary expression ({0} and {1})",
+ orig_lhs_type.TypeDescription(), orig_rhs_type.TypeDescription());
+ return llvm::make_error<DILDiagnosticError>(m_expr, errMsg, location);
+ }
+
+ // Check if the value can be compared to a pointer. We allow all pointers,
+ // integers, unscoped enumerations and a nullptr literal if it's an
+ // equality/inequality comparison. For "pointer <-> integer" C++ allows only
+ // equality/inequality comparison against literal zero and nullptr. However in
+ // the debugger context it's often useful to compare a pointer with an integer
+ // representing an address. That said, this also allows comparing nullptr and
+ // any integer, not just literal zero, e.g. "nullptr == 1 -> false". C++
+ // doesn't allow it, but we implement this for convenience.
+ auto comparable_to_pointer = [&](CompilerType t) {
+ return t.IsPointerType() || t.IsInteger() ||
+ t.IsUnscopedEnumerationType() || (!is_ordered && t.IsNullPtrType());
+ };
+
+ if ((lhs_type.IsPointerType() && comparable_to_pointer(rhs_type)) ||
+ (comparable_to_pointer(lhs_type) && rhs_type.IsPointerType())) {
+ // If both are pointers, check if they have comparable types.
+ if ((lhs_type.IsPointerType() && !lhs_type.IsPointerToVoid()) &&
+ (rhs_type.IsPointerType() && !rhs_type.IsPointerToVoid())) {
+ // 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(
+ "comparison of distinct pointer types ({0} and {1})",
+ orig_lhs_type.TypeDescription(), orig_rhs_type.TypeDescription());
+ return llvm::make_error<DILDiagnosticError>(m_expr, errMsg, location);
+ }
+ }
+ // Comparing pointers to void is always allowed.
+ return llvm::Error::success();
+ }
+
+ std::string errMsg = llvm::formatv(
+ "invalid operands to binary expression ({0} and {1})",
+ orig_lhs_type.TypeDescription(), orig_rhs_type.TypeDescription());
+ return llvm::make_error<DILDiagnosticError>(m_expr, errMsg, location);
+}
+
+llvm::Expected<lldb::ValueObjectSP>
+Interpreter::EvaluateComparison(BinaryOpKind kind, lldb::ValueObjectSP lhs,
+ lldb::ValueObjectSP rhs, bool lhs_is_literal,
+ bool rhs_is_literal, uint32_t location) {
+ // Comparison works for:
+ // nullptr_t <-> {nullptr_t,integer} (if integer is literal zero)
+ // {nullptr_t,integer} <-> nullptr_t (if integer is literal zero)
+ // {scalar,unscoped_enum} <-> {scalar,unscoped_enum}
+ // scoped_enum <-> scoped_enum (if the same type)
+ // pointer <-> pointer (if pointee types are compatible)
+ // pointer <-> {integer,unscoped_enum,nullptr_t}
+ // {integer,unscoped_enum,nullptr_t} <-> pointer
+ if (auto error = ValidateComparison(kind, lhs, rhs, lhs_is_literal,
+ rhs_is_literal, location))
+ return error;
+
+ CompilerType lhs_type = lhs->GetCompilerType();
+ CompilerType rhs_type = rhs->GetCompilerType();
+
+ llvm::Expected<lldb::TypeSystemSP> type_system =
+ GetTypeSystemFromCU(m_stack_frame);
+ if (!type_system)
+ return type_system.takeError();
+ CompilerType boolean_type = GetBasicType(*type_system, lldb::eBasicTypeBool);
+
+ return EvaluateScalarOp(kind, lhs, rhs, boolean_type, location);
+}
+
llvm::Expected<lldb::ValueObjectSP>
Interpreter::EvaluateBinaryBitwise(BinaryOpKind kind, lldb::ValueObjectSP lhs,
lldb::ValueObjectSP rhs, uint32_t location) {
@@ -1103,6 +1295,8 @@ Interpreter::Visit(const BinaryOpNode &node) {
return rhs_or_err;
lldb::ValueObjectSP rhs = *rhs_or_err;
+ bool lhs_is_literal = node.GetLHS().IsConstLiteral();
+ bool rhs_is_literal = node.GetRHS().IsConstLiteral();
lldb::TypeSystemSP lhs_system =
lhs->GetCompilerType().GetTypeSystem().GetSharedPointer();
lldb::TypeSystemSP rhs_system =
@@ -1145,6 +1339,14 @@ Interpreter::Visit(const BinaryOpNode &node) {
case BinaryOpKind::Shl:
case BinaryOpKind::Shr:
return EvaluateBinaryShift(node.GetKind(), lhs, rhs, node.GetLocation());
+ case BinaryOpKind::EQ:
+ case BinaryOpKind::NE:
+ case BinaryOpKind::LT:
+ case BinaryOpKind::LE:
+ case BinaryOpKind::GT:
+ case BinaryOpKind::GE:
+ return EvaluateComparison(node.GetKind(), lhs, rhs, lhs_is_literal,
+ rhs_is_literal, node.GetLocation());
default:
break;
}
diff --git a/lldb/source/ValueObject/DILLexer.cpp b/lldb/source/ValueObject/DILLexer.cpp
index 8e9809e4ef66f..87ededc8e6b8a 100644
--- a/lldb/source/ValueObject/DILLexer.cpp
+++ b/lldb/source/ValueObject/DILLexer.cpp
@@ -38,8 +38,16 @@ llvm::StringRef Token::GetTokenName(Kind kind) {
return "exclaim";
case Kind::eof:
return "eof";
+ case Kind::equalequal:
+ return "equalequal";
+ case Kind::exclaimequal:
+ return "exclaimequal";
case Kind::float_constant:
return "float_constant";
+ case Kind::greater:
+ return "greater";
+ case Kind::greaterequal:
+ return "greaterequal";
case Kind::greatergreater:
return "greatergreater";
case Kind::identifier:
@@ -54,6 +62,10 @@ llvm::StringRef Token::GetTokenName(Kind kind) {
return "l_paren";
case Kind::l_square:
return "l_square";
+ case Kind::less:
+ return "less";
+ case Kind::lessequal:
+ return "lessequal";
case Kind::lessless:
return "lessless";
case Kind::minus:
@@ -210,18 +222,36 @@ llvm::Expected<Token> DILLexer::Lex(llvm::StringRef expr,
// be ordered longest-to-shortest in the list below. E.g. '::' must come
// before ':', and '+=' must come before '+'.
constexpr std::pair<Token::Kind, const char *> operators[] = {
- {Token::ampamp, "&&"}, {Token::arrow, "->"},
- {Token::coloncolon, "::"}, {Token::greatergreater, ">>"},
- {Token::lessless, "<<"}, {Token::minusequal, "-="},
- {Token::pipepipe, "||"}, {Token::plusequal, "+="},
- {Token::amp, "&"}, {Token::caret, "^"},
- {Token::colon, ":"}, {Token::equal, "="},
- {Token::exclaim, "!"}, {Token::l_paren, "("},
- {Token::l_square, "["}, {Token::minus, "-"},
- {Token::percent, "%"}, {Token::period, "."},
- {Token::pipe, "|"}, {Token::plus, "+"},
- {Token::r_paren, ")"}, {Token::r_square, "]"},
- {Token::slash, "/"}, {Token::star, "*"},
+ {Token::ampamp, "&&"},
+ {Token::arrow, "->"},
+ {Token::coloncolon, "::"},
+ {Token::equalequal, "=="},
+ {Token::exclaimequal, "!="},
+ {Token::greaterequal, ">="},
+ {Token::greatergreater, ">>"},
+ {Token::lessequal, "<="},
+ {Token::lessless, "<<"},
+ {Token::minusequal, "-="},
+ {Token::pipepipe, "||"},
+ {Token::plusequal, "+="},
+ {Token::amp, "&"},
+ {Token::caret, "^"},
+ {Token::colon, ":"},
+ {Token::equal, "="},
+ {Token::exclaim, "!"},
+ {Token::greater, ">"},
+ {Token::l_paren, "("},
+ {Token::l_square, "["},
+ {Token::less, "<"},
+ {Token::minus, "-"},
+ {Token::percent, "%"},
+ {Token::period, "."},
+ {Token::pipe, "|"},
+ {Token::plus, "+"},
+ {Token::r_paren, ")"},
+ {Token::r_square, "]"},
+ {Token::slash, "/"},
+ {Token::star, "*"},
{Token::tilde, "~"},
};
for (auto [kind, str] : operators) {
diff --git a/lldb/source/ValueObject/DILParser.cpp b/lldb/source/ValueObject/DILParser.cpp
index f819ae11d599e..c9ad57b08a235 100644
--- a/lldb/source/ValueObject/DILParser.cpp
+++ b/lldb/source/ValueObject/DILParser.cpp
@@ -251,10 +251,10 @@ ASTNodeUP DILParser::ParseExclusiveOrExpression() {
// Parse an and_expression.
//
// and_expression:
-// shift_expression {"&" shift_expression}
+// equality_expression {"&" equality_expression}
//
ASTNodeUP DILParser::ParseAndExpression() {
- auto lhs = ParseShiftExpression();
+ auto lhs = ParseEqualityExpression();
assert(lhs && "ASTNodeUP must not contain a nullptr");
while (CurToken().Is(Token::amp)) {
@@ -265,7 +265,7 @@ ASTNodeUP DILParser::ParseAndExpression() {
return std::make_unique<ErrorNode>();
}
m_dil_lexer.Advance();
- auto rhs = ParseShiftExpression();
+ auto rhs = ParseEqualityExpression();
assert(rhs && "ASTNodeUP must not contain a nullptr");
lhs = std::make_unique<BinaryOpNode>(
token.GetLocation(), GetBinaryOpKindFromToken(token.GetKind()),
@@ -275,6 +275,55 @@ ASTNodeUP DILParser::ParseAndExpression() {
return lhs;
}
+// Parse an equality_expression.
+//
+// equality_expression:
+// relational_expression {"==" relational_expression}
+// relational_expression {"!=" relational_expression}
+//
+ASTNodeUP DILParser::ParseEqualityExpression() {
+ auto lhs = ParseRelationalExpression();
+ assert(lhs && "ASTNodeUP must not contain a nullptr");
+
+ while (CurToken().IsOneOf({Token::equalequal, Token::exclaimequal})) {
+ Token token = CurToken();
+ m_dil_lexer.Advance();
+ auto rhs = ParseRelationalExpression();
+ assert(lhs && "ASTNodeUP must not contain a nullptr");
+ lhs = std::make_unique<BinaryOpNode>(
+ token.GetLocation(), GetBinaryOpKindFromToken(token.GetKind()),
+ std::move(lhs), std::move(rhs));
+ }
+
+ return lhs;
+}
+
+// Parse a relational_expression.
+//
+// relational_expression:
+// shift_expression {"<" shift_expression}
+// shift_expression {">" shift_expression}
+// shift_expression {"<=" shift_expression}
+// shift_expression {">=" shift_expression}
+//
+ASTNodeUP DILParser::ParseRelationalExpression() {
+ auto lhs = ParseShiftExpression();
+ assert(lhs && "ASTNodeUP must not contain a nullptr");
+
+ while (CurToken().IsOneOf(
+ {Token::less, Token::greater, Token::lessequal, Token::greaterequal})) {
+ Token token = CurToken();
+ m_dil_lexer.Advance();
+ auto rhs = ParseShiftExpression();
+ assert(lhs && "ASTNodeUP must not contain a nullptr");
+ lhs = std::make_unique<BinaryOpNode>(
+ token.GetLocation(), GetBinaryOpKindFromToken(token.GetKind()),
+ std::move(lhs), std::move(rhs));
+ }
+
+ return lhs;
+}
+
// Parse a shift_expression.
//
// shift_expression:
diff --git a/lldb/test/API/commands/frame/var-dil/expr/Comparison/Makefile b/lldb/test/API/commands/frame/var-dil/expr/Comparison/Makefile
new file mode 100644
index 0000000000000..99998b20bcb05
--- /dev/null
+++ b/lldb/test/API/commands/frame/var-dil/expr/Comparison/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules
diff --git a/lldb/test/API/commands/frame/var-dil/expr/Comparison/TestFrameVarDILExprComparison.py b/lldb/test/API/commands/frame/var-dil/expr/Comparison/TestFrameVarDILExprComparison.py
new file mode 100644
index 0000000000000..f5aa61e15ecbf
--- /dev/null
+++ b/lldb/test/API/commands/frame/var-dil/expr/Comparison/TestFrameVarDILExprComparison.py
@@ -0,0 +1,173 @@
+"""
+Test DIL comparison operators.
+"""
+
+import lldb
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test.decorators import *
+from lldbsuite.test import lldbutil
+
+
+class TestFrameVarComparison(TestBase):
+ NO_DEBUG_INFO_TESTCASE = True
+
+ def test_comparison(self):
+ self.build()
+ lldbutil.run_to_source_breakpoint(
+ self, "Set a breakpoint here", lldb.SBFileSpec("main.cpp")
+ )
+
+ self.runCmd("settings set target.experimental.use-DIL true")
+
+ # Check arithmetic comparison
+ self.expect_var_path("1 == 1", value="true")
+ self.expect_var_path("1 == 1.0", value="true")
+ self.expect_var_path("i == 1", value="true")
+ self.expect_var_path("iref == i", value="true")
+ self.expect_var_path("array[0] == i", value="true")
+ self.expect_var_path("trueVar == true", value="true")
+ self.expect_var_path("1 == true", value="true")
+ self.expect_var_path("array[0] != array[1]", value="true")
+ self.expect_var_path("1 != 2 == true", value="true")
+ self.expect_var_path("true != 2 < 3", value="false")
+ self.expect_var_path("ScopedEnum::kZeroS < ScopedEnum::kOneS", value="true")
+ self.expect_var_path("1 > 2", value="false")
+ self.expect_var_path("1 > 0.1", value="true")
+ self.expect_var_path("1 >= 2", value="false")
+ self.expect_var_path("2 >= 2", value="true")
+ self.expect_var_path("1.0 <= 1.25", value="true")
+ self.expect_var_path("1.25f <= 1.0", value="false")
+ self.expect_var_path("1.0 <= 1.0", value="true")
+
+ self.expect(
+ "frame var -- 'ScopedEnum::kZeroS < ScopedEnumInt8::kOneS8'",
+ error=True,
+ substrs=[
+ "invalid operands to binary expression "
+ "('ScopedEnum' and 'ScopedEnumInt8')"
+ ],
+ )
+
+ self.expect(
+ "frame var -- 's < 4",
+ error=True,
+ substrs=["invalid operands to binary expression ('S' and 'int')"],
+ )
+
+ # Check pointer comparison
+ self.expect_var_path("p_void == p_void", value="true")
+ self.expect_var_path("p_void == p_char1", value="true")
+ self.expect_var_path("p_void != p_char1", value="false")
+ self.expect_var_path("p_void > p_char1", value="false")
+ self.expect_var_path("p_void >= p_char1", value="true")
+ self.expect_var_path("p_void < (p_char1 + 1)", value="true")
+ self.expect_var_path("pp_void0 + 1 == pp_void1", value="true")
+
+ self.expect_var_path("(void*)1 == (void*)1", value="true")
+ self.expect_var_path("(void*)1 != (void*)1", value="false")
+ self.expect_var_path("(void*)2 > (void*)1", value="true")
+ self.expect_var_path("(void*)2 < (void*)1", value="false")
+
+ self.expect_var_path("(void*)1 == (char*)1", value="true")
+ self.expect_var_path("(char*)1 != (void*)1", value="false")
+ self.expect_var_path("(void*)2 > (char*)1", value="true")
+ self.expect_var_path("(char*)2 < (void*)1", value="false")
+
+ self.expect_var_path("(void*)0 == 0", value="true")
+ self.expect_var_path("0 != (void*)0", value="false")
+
+ self.expect_var_path("(void*)0 == nullptr", value="true")
+ self.expect_var_path("(void*)0 != nullptr", value="false")
+ self.expect_var_path("nullptr == (void*)1", value="false")
+ self.expect_var_path("nullptr != (void*)1", value="true")
+
+ self.expect_var_path("nullptr == nullptr", value="true")
+ self.expect_var_path("nullptr != nullptr", value="false")
+
+ self.expect_var_path("nullptr == 0", value="true")
+ self.expect_var_path("0 != nullptr", value="false")
+ self.expect_var_path("nullptr == 0U", value="true")
+ self.expect_var_path("0L != nullptr", value="false")
+ self.expect_var_path("nullptr == 0UL", value="true")
+ self.expect_var_path("0ULL != nullptr", value="false")
+ self.expect_var_path("nullptr == 0x0", value="true")
+ self.expect_var_path("0b0 != nullptr", value="false")
+ self.expect_var_path("nullptr == 00", value="true")
+ self.expect_var_path("0x0LLU != nullptr", value="false")
+
+ self.expect_var_path("0 == std_nullptr_t", value="true")
+ self.expect_var_path("std_nullptr_t != 0", value="false")
+
+ self.expect_var_path("array == p_int0", value="true")
+ self.expect_var_path("p_int0 == array", value="true")
+ self.expect_var_path("array < p_int1", value="true")
+ self.expect_var_path("array == nullptr", value="false")
+
+ # These are not allowed by C++, but DIL supports these for convenience.
+ self.expect_var_path("(void*)1 == 1", value="true")
+ self.expect_var_path("(void*)1 == 0", value="false")
+ self.expect_var_path("(void*)1 > 0", value="true")
+ self.expect_var_path("(void*)1 < 0", value="false")
+ self.expect_var_path("1 > (void*)0", value="true")
+ self.expect_var_path("2 < (void*)3", value="true")
+
+ # Integer is converted to uintptr_t, so negative numbers because large
+ # positive numbers.
+ self.expect_var_path("(void*)-1 == -1", value="true")
+ self.expect_var_path("(void*)1 > -1", value="false")
+
+ self.expect(
+ "frame var -- '(void*)0 > nullptr'",
+ error=True,
+ substrs=[
+ "invalid operands to binary expression ('void *' and 'std::nullptr_t')"
+ ],
+ )
+
+ self.expect(
+ "frame var -- 'nullptr > 0'",
+ error=True,
+ substrs=[
+ "invalid operands to binary expression ('std::nullptr_t' and 'int')"
+ ],
+ )
+
+ self.expect(
+ "frame var -- '1 == nullptr'",
+ error=True,
+ substrs=[
+ "invalid operands to binary expression ('int' and 'std::nullptr_t')"
+ ],
+ )
+
+ self.expect(
+ "frame var -- 'nullptr == (int)0'",
+ error=True,
+ substrs=[
+ "invalid operands to binary expression ('std::nullptr_t' and 'int')"
+ ],
+ )
+
+ self.expect(
+ "frame var -- 'false == nullptr'",
+ error=True,
+ substrs=[
+ "invalid operands to binary expression ('bool' and 'std::nullptr_t')"
+ ],
+ )
+
+ self.expect(
+ "frame var -- 'p_int0 > p_char1'",
+ error=True,
+ substrs=[
+ "comparison of distinct pointer types ('int *' and 'const char *')"
+ ],
+ )
+
+ self.expect(
+ "frame var -- 'pp_void0 == p_char1'",
+ error=True,
+ substrs=[
+ "comparison of distinct pointer types ('void **' and 'const char *')"
+ ],
+ )
diff --git a/lldb/test/API/commands/frame/var-dil/expr/Comparison/main.cpp b/lldb/test/API/commands/frame/var-dil/expr/Comparison/main.cpp
new file mode 100644
index 0000000000000..4ff21bb8f982f
--- /dev/null
+++ b/lldb/test/API/commands/frame/var-dil/expr/Comparison/main.cpp
@@ -0,0 +1,34 @@
+#include <cstddef>
+#include <cstdint>
+
+enum class ScopedEnum { kZeroS, kOneS };
+enum class ScopedEnumInt8 : int8_t { kZeroS8, kOneS8 };
+
+void stop() {}
+
+int main(int argc, char **argv) {
+ auto enum_one = ScopedEnum::kOneS;
+ auto enum_one_8 = ScopedEnumInt8::kOneS8;
+
+ bool trueVar = true;
+
+ int i = 1;
+ int j = 2;
+ int &iref = i;
+ int array[2] = {1, 2};
+
+ struct S {
+ } s;
+
+ int *p_int0 = &array[0];
+ int *p_int1 = &array[1];
+ const char *p_char1 = "hello";
+ void *p_void = (void *)p_char1;
+ void **pp_void0 = &p_void;
+ void **pp_void1 = pp_void0 + 1;
+
+ std::nullptr_t std_nullptr_t = nullptr;
+
+ stop(); // Set a breakpoint here
+ return 0;
+}
>From 57b607adcde9de1614d7d9a7949971ca91ccb61d Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Wed, 15 Jul 2026 19:25:33 +0500
Subject: [PATCH 2/4] Fix asserts
---
lldb/source/ValueObject/DILParser.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lldb/source/ValueObject/DILParser.cpp b/lldb/source/ValueObject/DILParser.cpp
index c9ad57b08a235..64f1f8b47c965 100644
--- a/lldb/source/ValueObject/DILParser.cpp
+++ b/lldb/source/ValueObject/DILParser.cpp
@@ -289,7 +289,7 @@ ASTNodeUP DILParser::ParseEqualityExpression() {
Token token = CurToken();
m_dil_lexer.Advance();
auto rhs = ParseRelationalExpression();
- assert(lhs && "ASTNodeUP must not contain a nullptr");
+ assert(rhs && "ASTNodeUP must not contain a nullptr");
lhs = std::make_unique<BinaryOpNode>(
token.GetLocation(), GetBinaryOpKindFromToken(token.GetKind()),
std::move(lhs), std::move(rhs));
@@ -315,7 +315,7 @@ ASTNodeUP DILParser::ParseRelationalExpression() {
Token token = CurToken();
m_dil_lexer.Advance();
auto rhs = ParseShiftExpression();
- assert(lhs && "ASTNodeUP must not contain a nullptr");
+ assert(rhs && "ASTNodeUP must not contain a nullptr");
lhs = std::make_unique<BinaryOpNode>(
token.GetLocation(), GetBinaryOpKindFromToken(token.GetKind()),
std::move(lhs), std::move(rhs));
>From 2b7527f4ab38bb7d47c861e66aeba98435c7765a Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Tue, 4 Aug 2026 19:59:55 +0500
Subject: [PATCH 3/4] Remove C++ mentions and rephrase the comment
---
lldb/source/ValueObject/DILEval.cpp | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)
diff --git a/lldb/source/ValueObject/DILEval.cpp b/lldb/source/ValueObject/DILEval.cpp
index 83a9a19c84488..9270a77078e68 100644
--- a/lldb/source/ValueObject/DILEval.cpp
+++ b/lldb/source/ValueObject/DILEval.cpp
@@ -1092,13 +1092,10 @@ Interpreter::ValidateComparison(BinaryOpKind kind, lldb::ValueObjectSP &lhs,
}
// Check if the value can be compared to a pointer. We allow all pointers,
- // integers, unscoped enumerations and a nullptr literal if it's an
- // equality/inequality comparison. For "pointer <-> integer" C++ allows only
- // equality/inequality comparison against literal zero and nullptr. However in
- // the debugger context it's often useful to compare a pointer with an integer
- // representing an address. That said, this also allows comparing nullptr and
- // any integer, not just literal zero, e.g. "nullptr == 1 -> false". C++
- // doesn't allow it, but we implement this for convenience.
+ // integers, unscoped enumerations and a `nullptr` literal if it's an
+ // equality/inequality comparison, including comparing a pointer with an
+ // integer representing an address. This also allows comparing `nullptr` and
+ // any integer, not just literal zero, e.g. `nullptr == 1` is false.
auto comparable_to_pointer = [&](CompilerType t) {
return t.IsPointerType() || t.IsInteger() ||
t.IsUnscopedEnumerationType() || (!is_ordered && t.IsNullPtrType());
>From e63bc2f6e29c394a9ec0154dc475ea80cc0f00f5 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Thu, 6 Aug 2026 22:25:53 +0500
Subject: [PATCH 4/4] Fix array to array comparison
---
lldb/source/ValueObject/DILEval.cpp | 10 ++++------
.../Comparison/TestFrameVarDILExprComparison.py | 13 +++++++------
.../commands/frame/var-dil/expr/Comparison/main.cpp | 5 ++++-
3 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/lldb/source/ValueObject/DILEval.cpp b/lldb/source/ValueObject/DILEval.cpp
index 9270a77078e68..67fb7bd993ba1 100644
--- a/lldb/source/ValueObject/DILEval.cpp
+++ b/lldb/source/ValueObject/DILEval.cpp
@@ -995,9 +995,6 @@ Interpreter::ValidateComparison(BinaryOpKind kind, lldb::ValueObjectSP &lhs,
auto orig_lhs_type = lhs->GetCompilerType();
auto orig_rhs_type = rhs->GetCompilerType();
- if (orig_lhs_type == orig_rhs_type)
- return llvm::Error::success();
-
bool is_ordered = (kind == BinaryOpKind::LT || kind == BinaryOpKind::LE ||
kind == BinaryOpKind::GT || kind == BinaryOpKind::GE);
bool lhs_nullptr_or_zero =
@@ -1012,6 +1009,10 @@ Interpreter::ValidateComparison(BinaryOpKind kind, lldb::ValueObjectSP &lhs,
CompilerType lhs_type = lhs->GetCompilerType();
CompilerType rhs_type = rhs->GetCompilerType();
+
+ if (lhs_type == rhs_type)
+ return llvm::Error::success();
+
lldb::ValueObjectSP lhs_child;
lldb::ValueObjectSP rhs_child;
bool is_signed;
@@ -1147,9 +1148,6 @@ Interpreter::EvaluateComparison(BinaryOpKind kind, lldb::ValueObjectSP lhs,
rhs_is_literal, location))
return error;
- CompilerType lhs_type = lhs->GetCompilerType();
- CompilerType rhs_type = rhs->GetCompilerType();
-
llvm::Expected<lldb::TypeSystemSP> type_system =
GetTypeSystemFromCU(m_stack_frame);
if (!type_system)
diff --git a/lldb/test/API/commands/frame/var-dil/expr/Comparison/TestFrameVarDILExprComparison.py b/lldb/test/API/commands/frame/var-dil/expr/Comparison/TestFrameVarDILExprComparison.py
index f5aa61e15ecbf..7c8b6f0909b24 100644
--- a/lldb/test/API/commands/frame/var-dil/expr/Comparison/TestFrameVarDILExprComparison.py
+++ b/lldb/test/API/commands/frame/var-dil/expr/Comparison/TestFrameVarDILExprComparison.py
@@ -102,6 +102,8 @@ def test_comparison(self):
self.expect_var_path("p_int0 == array", value="true")
self.expect_var_path("array < p_int1", value="true")
self.expect_var_path("array == nullptr", value="false")
+ self.expect_var_path("array == array1", value="false")
+ self.expect_var_path("array < array1", value="true")
# These are not allowed by C++, but DIL supports these for convenience.
self.expect_var_path("(void*)1 == 1", value="true")
@@ -123,7 +125,6 @@ def test_comparison(self):
"invalid operands to binary expression ('void *' and 'std::nullptr_t')"
],
)
-
self.expect(
"frame var -- 'nullptr > 0'",
error=True,
@@ -131,7 +132,6 @@ def test_comparison(self):
"invalid operands to binary expression ('std::nullptr_t' and 'int')"
],
)
-
self.expect(
"frame var -- '1 == nullptr'",
error=True,
@@ -139,7 +139,6 @@ def test_comparison(self):
"invalid operands to binary expression ('int' and 'std::nullptr_t')"
],
)
-
self.expect(
"frame var -- 'nullptr == (int)0'",
error=True,
@@ -147,7 +146,6 @@ def test_comparison(self):
"invalid operands to binary expression ('std::nullptr_t' and 'int')"
],
)
-
self.expect(
"frame var -- 'false == nullptr'",
error=True,
@@ -155,7 +153,6 @@ def test_comparison(self):
"invalid operands to binary expression ('bool' and 'std::nullptr_t')"
],
)
-
self.expect(
"frame var -- 'p_int0 > p_char1'",
error=True,
@@ -163,7 +160,6 @@ def test_comparison(self):
"comparison of distinct pointer types ('int *' and 'const char *')"
],
)
-
self.expect(
"frame var -- 'pp_void0 == p_char1'",
error=True,
@@ -171,3 +167,8 @@ def test_comparison(self):
"comparison of distinct pointer types ('void **' and 'const char *')"
],
)
+ self.expect(
+ "frame var -- 'array1 == array2'",
+ error=True,
+ substrs=["comparison of distinct pointer types ('int[1]' and 'float[1]')"],
+ )
diff --git a/lldb/test/API/commands/frame/var-dil/expr/Comparison/main.cpp b/lldb/test/API/commands/frame/var-dil/expr/Comparison/main.cpp
index 4ff21bb8f982f..ecc5d2064c658 100644
--- a/lldb/test/API/commands/frame/var-dil/expr/Comparison/main.cpp
+++ b/lldb/test/API/commands/frame/var-dil/expr/Comparison/main.cpp
@@ -4,6 +4,10 @@
enum class ScopedEnum { kZeroS, kOneS };
enum class ScopedEnumInt8 : int8_t { kZeroS8, kOneS8 };
+int array[2] = {1, 2};
+int array1[1] = {3};
+float array2[1] = {1.0};
+
void stop() {}
int main(int argc, char **argv) {
@@ -15,7 +19,6 @@ int main(int argc, char **argv) {
int i = 1;
int j = 2;
int &iref = i;
- int array[2] = {1, 2};
struct S {
} s;
More information about the lldb-commits
mailing list