Skip to content

Commit

Permalink
[clang-format] Add spaces around the Verilog implication operator (#7…
Browse files Browse the repository at this point in the history
…1352)

The Verilog implication operator `->` is a binary operator meaning
either the left hand side is false or the right hand side is true.
Previously it was treated as the C++ struct member operator.

I didn't even know it existed when I added the operator formatting part.
And I didn't check all the tests for all the operators I added. That is
how the bad test got in.
  • Loading branch information
sstwcw committed Nov 29, 2023
1 parent 0468867 commit 3af82b3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
6 changes: 4 additions & 2 deletions clang/lib/Format/FormatTokenLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ void FormatTokenLexer::tryMergePreviousTokens() {
TT_BinaryOperator)) {
return;
}
// Module paths in specify blocks and implications in properties.
// Module paths in specify blocks and the implication and boolean equality
// operators.
if (tryMergeTokensAny({{tok::plusequal, tok::greater},
{tok::plus, tok::star, tok::greater},
{tok::minusequal, tok::greater},
Expand All @@ -265,7 +266,8 @@ void FormatTokenLexer::tryMergePreviousTokens() {
{tok::pipe, tok::arrow},
{tok::hash, tok::minus, tok::hash},
{tok::hash, tok::equal, tok::hash}},
TT_BinaryOperator)) {
TT_BinaryOperator) ||
Tokens.back()->is(tok::arrow)) {
Tokens.back()->ForcedPrecedence = prec::Comma;
return;
}
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,9 @@ class AnnotatingParser {
} else if (Current.is(tok::arrow) &&
Style.Language == FormatStyle::LK_Java) {
Current.setType(TT_TrailingReturnArrow);
} else if (Current.is(tok::arrow) && Style.isVerilog()) {
// The implication operator.
Current.setType(TT_BinaryOperator);
} else if (Current.is(tok::arrow) && AutoFound &&
Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
!Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
Expand Down Expand Up @@ -4717,6 +4720,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
(Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) {
return false;
}
// Add spaces around the implication operator `->`.
if (Left.is(tok::arrow) || Right.is(tok::arrow))
return true;
// Don't add spaces between two at signs. Like in a coverage event.
// Don't add spaces between at and a sensitivity list like
// `@(posedge clk)`.
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Format/FormatTestVerilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ TEST_F(FormatTestVerilog, Operators) {
verifyFormat("x = x ^~ x;");
verifyFormat("x = x && x;");
verifyFormat("x = x || x;");
verifyFormat("x = x->x;");
verifyFormat("x = x -> x;");
verifyFormat("x = x <-> x;");
verifyFormat("x += x;");
verifyFormat("x -= x;");
Expand Down
23 changes: 12 additions & 11 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1980,17 +1980,18 @@ TEST_F(TokenAnnotatorTest, UnderstandsVerilogOperators) {
// joined operators, we don't have a separate type, so we only test for their
// precedence.
std::pair<prec::Level, std::string> JoinedBinary[] = {
{prec::Comma, "<->"}, {prec::Assignment, "+="},
{prec::Assignment, "-="}, {prec::Assignment, "*="},
{prec::Assignment, "/="}, {prec::Assignment, "%="},
{prec::Assignment, "&="}, {prec::Assignment, "^="},
{prec::Assignment, "<<="}, {prec::Assignment, ">>="},
{prec::Assignment, "<<<="}, {prec::Assignment, ">>>="},
{prec::LogicalOr, "||"}, {prec::LogicalAnd, "&&"},
{prec::Equality, "=="}, {prec::Equality, "!="},
{prec::Equality, "==="}, {prec::Equality, "!=="},
{prec::Equality, "==?"}, {prec::Equality, "!=?"},
{prec::ExclusiveOr, "~^"}, {prec::ExclusiveOr, "^~"},
{prec::Comma, "->"}, {prec::Comma, "<->"},
{prec::Assignment, "+="}, {prec::Assignment, "-="},
{prec::Assignment, "*="}, {prec::Assignment, "/="},
{prec::Assignment, "%="}, {prec::Assignment, "&="},
{prec::Assignment, "^="}, {prec::Assignment, "<<="},
{prec::Assignment, ">>="}, {prec::Assignment, "<<<="},
{prec::Assignment, ">>>="}, {prec::LogicalOr, "||"},
{prec::LogicalAnd, "&&"}, {prec::Equality, "=="},
{prec::Equality, "!="}, {prec::Equality, "==="},
{prec::Equality, "!=="}, {prec::Equality, "==?"},
{prec::Equality, "!=?"}, {prec::ExclusiveOr, "~^"},
{prec::ExclusiveOr, "^~"},
};
for (auto Operator : JoinedBinary) {
auto Tokens = Annotate(std::string("x = x ") + Operator.second + " x;");
Expand Down

0 comments on commit 3af82b3

Please sign in to comment.