Skip to content

Commit 9aaae0a

Browse files
committed
[Parser] Check canonical types during type comparison
1 parent 2398045 commit 9aaae0a

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

lldb/source/ValueObject/DILParser.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ static CompilerType UsualArithmeticConversions(
547547
}
548548

549549
if (!is_comp_assign) {
550-
assert(lhs->GetDereferencedResultType().CompareTypes(rhs->GetDereferencedResultType()) &&
550+
assert(lhs->GetDereferencedResultType().GetCanonicalType().CompareTypes(
551+
rhs->GetDereferencedResultType().GetCanonicalType()) &&
551552
"integral promotion error: operands result types must be the same");
552553
}
553554

@@ -3364,6 +3365,12 @@ DILASTNodeUP DILParser::BuildTernaryOp(DILASTNodeUP cond, DILASTNodeUP lhs,
33643365
return std::make_unique<TernaryOpNode>(location, lhs_type, std::move(cond),
33653366
std::move(lhs), std::move(rhs));
33663367
}
3368+
// If operands have the same canonical type, use the canonical type.
3369+
if (lhs_type.GetCanonicalType().CompareTypes(rhs_type.GetCanonicalType())) {
3370+
return std::make_unique<TernaryOpNode>(
3371+
location, lhs_type.GetCanonicalType(), std::move(cond), std::move(lhs),
3372+
std::move(rhs));
3373+
}
33673374

33683375
// If both operands have arithmetic type, apply the usual arithmetic
33693376
// conversions to bring them to a common type.
@@ -3389,6 +3396,12 @@ DILASTNodeUP DILParser::BuildTernaryOp(DILASTNodeUP cond, DILASTNodeUP lhs,
33893396
return std::make_unique<TernaryOpNode>(location, lhs_type, std::move(cond),
33903397
std::move(lhs), std::move(rhs));
33913398
}
3399+
// Check if operands have the same canonical pointer type.
3400+
if (lhs_type.GetCanonicalType().CompareTypes(rhs_type.GetCanonicalType())) {
3401+
return std::make_unique<TernaryOpNode>(
3402+
location, lhs_type.GetCanonicalType(), std::move(cond), std::move(lhs),
3403+
std::move(rhs));
3404+
}
33923405

33933406
// If one operand is a pointer and the other is a nullptr or literal zero,
33943407
// convert the nullptr operand to pointer type.

lldb/unittests/DIL/DILTests.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3382,25 +3382,23 @@ TEST_F(EvalTest, TestTypeComparison) {
33823382
// This test is for border-case situations in the CompareTypes function.
33833383

33843384
// Taking an address of ternary expression require operands of the same type.
3385+
EXPECT_THAT(Eval("&(true ? i : mi)"), IsOk());
33853386
EXPECT_THAT(Eval("&(true ? ip : icpc)"), IsOk());
3386-
EXPECT_THAT(Eval("&(true ? mipp : ipp)"),
3387-
XFail(IsOk(/*compare_types*/ false)));
3387+
EXPECT_THAT(Eval("&(true ? mipp : ipp)"), IsOk(/*compare_types*/ false));
33883388
EXPECT_THAT(Eval("&(true ? ipp : icpcpc)"), IsOk());
3389-
EXPECT_THAT(Eval("&(true ? ipp : mipp)"), XFail(IsOk()));
3389+
EXPECT_THAT(Eval("&(true ? ipp : mipp)"), IsOk());
33903390
EXPECT_THAT(Eval("&(true ? ipp : micpcpc)"), IsOk());
33913391
// TODO: Enable type comparison once the type mismatch is fixed.
33923392
// LLDB results in "int ***", while lldb-eval results in "MyInt ***".
3393-
EXPECT_THAT(Eval("&(true ? mipp : icpcpc)"),
3394-
XFail(IsOk(/*compare_types*/ false)));
3395-
EXPECT_THAT(Eval("&(true ? mipp : micpcpc)"),
3396-
XFail(IsOk(/*compare_types*/ false)));
3393+
EXPECT_THAT(Eval("&(true ? mipp : icpcpc)"), IsOk(/*compare_types*/ false));
3394+
EXPECT_THAT(Eval("&(true ? mipp : micpcpc)"), IsOk(/*compare_types*/ false));
33973395
EXPECT_THAT(Eval("&(true ? icpcpc : micpcpc)"), IsOk());
33983396

33993397
// Ensure that "signed char" and "char" are different types.
34003398
EXPECT_THAT(Eval("true ? c : sc"), IsEqual("2")); // int
34013399
EXPECT_THAT(Eval("true ? sc : (signed char)67"), IsEqual("'A'"));
34023400
EXPECT_THAT(Eval("true ? (char)66 : (signed char)65"), IsEqual("66"));
3403-
EXPECT_THAT(Eval("true ? cc : mc"), XFail(IsEqual("'B'")));
3401+
EXPECT_THAT(Eval("true ? cc : mc"), IsEqual("'B'"));
34043402
EXPECT_THAT(Eval("true ? cc : sc"), IsEqual("66"));
34053403
EXPECT_THAT(Eval("true ? sc : mc"), IsEqual("65"));
34063404
EXPECT_THAT(Eval("&(true ? c : c)"), IsOk());

lldb/unittests/DIL/Inputs/test_binary.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,7 @@ void TestTypeComparison() {
10981098

10991099
using MyInt = int;
11001100
using MyPtr = MyInt *;
1101+
MyInt mi = 2;
11011102
MyPtr *mipp = ipp;
11021103

11031104
using MyConstInt = const int;

0 commit comments

Comments
 (0)