Skip to content

Commit

Permalink
Add tests for various operator expression bits that were uncovered
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Jan 25, 2025
1 parent 0830d57 commit 8208c9a
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 13 deletions.
2 changes: 2 additions & 0 deletions source/ast/expressions/Operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ bool OpInfo::isShortCircuit(BinaryOperator op) {
}
}

// LCOV_EXCL_START
std::string_view OpInfo::getText(BinaryOperator op) {
switch (op) {
case BinaryOperator::Add:
Expand Down Expand Up @@ -591,5 +592,6 @@ int OpInfo::getPrecedence(BinaryOperator op) {
}
SLANG_UNREACHABLE;
}
// LCOV_EXCL_STOP

} // namespace slang::ast
13 changes: 4 additions & 9 deletions source/ast/expressions/OperatorExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ bool Expression::bindMembershipExpressions(const ASTContext& context, TokenKind
else
selfDetermined(context, expr);

if (expr->bad())
return false;

SLANG_ASSERT(!expr->bad());
results[index++] = expr;
}

Expand Down Expand Up @@ -384,9 +382,6 @@ ConstantValue UnaryExpression::evalImpl(EvalContext& context) const {
// Handle operations that require an lvalue up front.
if (OpInfo::isLValue(op)) {
LValue lvalue = operand().evalLValue(context);
if (!lvalue)
return nullptr;

ConstantValue cv = lvalue.load();
if (!cv)
return nullptr;
Expand All @@ -404,7 +399,7 @@ ConstantValue UnaryExpression::evalImpl(EvalContext& context) const {
OP(Postincrement, v + 1);
OP(Postdecrement, v - 1);
default:
break;
SLANG_UNREACHABLE;
}
#undef OP
}
Expand All @@ -421,7 +416,7 @@ ConstantValue UnaryExpression::evalImpl(EvalContext& context) const {
OP(Postincrement, v + 1);
OP(Postdecrement, v - 1);
default:
break;
SLANG_UNREACHABLE;
}
#undef OP
}
Expand All @@ -438,7 +433,7 @@ ConstantValue UnaryExpression::evalImpl(EvalContext& context) const {
OP(Postincrement, v + 1);
OP(Postdecrement, v - 1);
default:
break;
SLANG_UNREACHABLE;
}
#undef OP
}
Expand Down
25 changes: 25 additions & 0 deletions tests/regression/all.sv
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ macromodule m3;
if (instr matches (tagged Jmp .j) &&&
j matches (tagged JmpC '{cc:.c,addr:.a})) begin
pc = c[0] & a[0];
pc = instr matches (tagged Jmp .j) &&&
j matches (tagged JmpC '{cc:.c,addr:.a}) ? c[0] & a[0] : 0;
end
else begin
end
Expand Down Expand Up @@ -457,3 +459,26 @@ class C3;
b: cross y, x;
endgroup
endclass

module m9;
logic [3:0] a = {4 {1'b1}};
endmodule

module m10;
byte stream[$];
class Packet;
rand int header;
rand int len;
rand byte payload[];
int crc;
constraint G { len > 1; payload.size == len ; }
function void post_randomize; crc = payload.sum; endfunction
endclass

initial begin
byte q[$];
automatic Packet p = new;
{<< byte{ p.header, p.len, p.payload with [0 +: p.len], p.crc }} = stream;
stream = stream[ $bits(p) / 8 : $ ];
end
endmodule
91 changes: 91 additions & 0 deletions tests/unittests/ast/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3823,3 +3823,94 @@ endmodule
ast::EvalContext eval_ctx(ast::ASTContext(compilation.getRoot(), ast::LookupLocation::max));
CHECK(var.getInitializer()->eval(eval_ctx).getBitstreamWidth() == 4);
}

TEST_CASE("Value range where implicit strings not allowed") {
auto tree = SyntaxTree::fromText(R"(
int i = "a" inside { [1:2] };
)");

Compilation compilation;
compilation.addSyntaxTree(tree);
NO_COMPILATION_ERRORS;
}

TEST_CASE("LValue required for prefix unary operators") {
auto tree = SyntaxTree::fromText(R"(
module m;
initial begin
++(1 + 1);
(1 + 1)++;
end
endmodule
)");

Compilation compilation;
compilation.addSyntaxTree(tree);

auto& diags = compilation.getAllDiagnostics();
REQUIRE(diags.size() == 2);
CHECK(diags[0].code == diag::ExpressionNotAssignable);
CHECK(diags[1].code == diag::ExpressionNotAssignable);
}

TEST_CASE("More operator eval tests") {
auto tree = SyntaxTree::fromText(R"(
parameter p = foo();
function automatic bit foo;
shortreal a = 1.0;
int b = 3;
string s;
event ev;
union { int i; shortreal r; } u, v;
assert(++a == shortreal'(2.0));
assert(a++ == shortreal'(2.0));
--a;
assert(a-- == shortreal'(2.0));
assert(+b == 3);
assert(^4'b1000 == 1);
assert(~&4'b1111 == 0);
assert(~|4'b0000 == 1);
assert(~^4'b1000 == 0);
assert(+a == shortreal'(1.0));
assert(-a == shortreal'(-1.0));
assert((!a) == 0);
assert((!s) == 1);
assert(a === shortreal'(1.0));
assert(!(a !== shortreal'(1.0)));
assert(a && 1);
assert(a || 0);
assert(0 || b);
assert(0 || 1.0);
assert(1 -> a);
assert(1.0 <-> a);
assert(a <-> 1.0);
assert(1.0 && 1.0);
assert(0.0 || 1.0);
assert(3 % 2 == 1);
assert(3 !== 2);
assert(3'b1x0 !=? 3'b111);
assert(1 <-> 1);
assert(s === "");
assert(s !== "asdf");
assert(ev === null);
assert(!(ev != null));
assert(!(ev !== null));
assert(u === v);
u.i = 1;
v.r = 1.0;
assert(u != v);
assert(u !== v);
endfunction
)");

Compilation compilation;
compilation.addSyntaxTree(tree);

auto diags = compilation.getAllDiagnostics().filter({diag::FloatBoolConv, diag::IntBoolConv});
if (!diags.empty()) {
FAIL_CHECK(report(diags));
}
}
8 changes: 5 additions & 3 deletions tests/unittests/ast/WarningTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,17 +743,19 @@ module m;
logic signed [1:0] l = (2'd3:2'd1:-3);
logic signed m = (2'd1 == 2'd1);
logic signed [1:0] n = 2'd1 << 1;
logic signed [1:0] o = +2'd3;
endmodule
)");

Compilation compilation;
compilation.addSyntaxTree(tree);

auto& diags = compilation.getAllDiagnostics();
REQUIRE(diags.size() == 3);
CHECK(diags[0].code == diag::SignConversion);
CHECK(diags[0].code == diag::SignConversion);
REQUIRE(diags.size() == 4);
CHECK(diags[0].code == diag::SignConversion);
CHECK(diags[1].code == diag::SignConversion);
CHECK(diags[2].code == diag::SignConversion);
CHECK(diags[3].code == diag::SignConversion);
}

TEST_CASE("Edge of multibit type") {
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/parsing/VisitorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,5 +747,5 @@ TEST_CASE("Visit all file") {
v.visitDefault(elem);
}));

CHECK(count == 1488);
CHECK(count == 1606);
}

0 comments on commit 8208c9a

Please sign in to comment.