Skip to content

Commit

Permalink
Merge pull request #1885 from antmicro/add-macro-in-struct-body-test
Browse files Browse the repository at this point in the history
Added macro call in struct test without a semicolon.
  • Loading branch information
hzeller committed Apr 27, 2023
2 parents ddd81e4 + 784f205 commit d600c4a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 4 deletions.
14 changes: 13 additions & 1 deletion verilog/analysis/verilog_equivalence.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ DiffStatus LexicallyEquivalent(
DiffStatus diff_status = DiffStatus::kEquivalent;
auto recursive_comparator = [&](const TokenSequence::const_iterator l,
const TokenSequence::const_iterator r) {
if (l->token_enum() != r->token_enum()) {
if (l->token_enum() != r->token_enum() &&
!((l->token_enum() == verilog_tokentype::MacroCallCloseToEndLine &&
r->text() == ")") ||
(r->token_enum() == verilog_tokentype::MacroCallCloseToEndLine &&
l->text() == ")"))) {
if (errstream != nullptr) {
*errstream << "Mismatched token enums. got: ";
token_printer(*l, *errstream);
Expand Down Expand Up @@ -246,6 +250,14 @@ DiffStatus FormatEquivalent(absl::string_view left, absl::string_view right,
return IsWhitespace(verilog_tokentype(t.token_enum()));
},
[=](const TokenInfo& l, const TokenInfo& r) {
// MacroCallCloseToEndLine should be considered equivalent to ')', as
// they are whitespace dependant
if (((r.token_enum() == verilog_tokentype::MacroCallCloseToEndLine) &&
(l.text() == ")")) ||
((l.token_enum() == verilog_tokentype::MacroCallCloseToEndLine) &&
(r.text() == ")"))) {
return true;
}
return l.EquivalentWithoutLocation(r);
},
errstream);
Expand Down
19 changes: 19 additions & 0 deletions verilog/analysis/verilog_equivalence_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,25 @@ TEST(FormatEquivalentTest, MismatchTokenType) {
<< errs.str();
}

TEST(FormatEquivalentTest, EquivalenceOfRightParen) {
const char* kTestCases[] = {
"{`FOO()\n}\n",
"{`FOO()}\n",
"{`FOO()()}\n",
"{`FOO()()\n}\n",
};
{
std::ostringstream errs;
ExpectCompareWithErrstream(FormatEquivalent, DiffStatus::kEquivalent,
kTestCases[0], kTestCases[1], &errs);
// Test the other way around too.
ExpectCompareWithErrstream(FormatEquivalent, DiffStatus::kEquivalent,
kTestCases[1], kTestCases[0], &errs);
ExpectCompareWithErrstream(FormatEquivalent, DiffStatus::kEquivalent,
kTestCases[2], kTestCases[3], &errs);
}
}

TEST(FormatEquivalentTest, DiagnosticMismatch) {
const char* kTestCases[] = {
"module foo;\n",
Expand Down
15 changes: 15 additions & 0 deletions verilog/formatting/formatter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8271,6 +8271,21 @@ static constexpr FormatterTestCase kFormatterTestCases[] = {
" }\n"
") ();\n"
"endmodule\n"},
// The same as the previous test case, but without a semicolon at the macro
// call
{"module foo\n"
"#(\n"
" parameter type baz_t = struct packed { `BAZ() }\n"
")\n"
"();\n"
"endmodule\n",
"module foo #(\n"
" parameter type\n"
" baz_t = struct packed {\n"
" `BAZ()\n"
" }\n"
") ();\n"
"endmodule\n"},
// Check that comments with too large starting column difference are not
// aligned as continuation comments.
// Check that starting comments are not linked with a comment in
Expand Down
5 changes: 4 additions & 1 deletion verilog/formatting/token_annotator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <iterator>
#include <vector>

#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "common/formatting/format_token.h"
#include "common/formatting/tree_annotator.h"
Expand Down Expand Up @@ -437,7 +438,9 @@ static WithReason<int> SpacesRequiredBetween(
if (InRangeLikeContext(right_context)) {
int spaces = right.OriginalLeadingSpaces().length();
if (spaces > 1) {
spaces = 1;
// If ExcessSpaces returns 0 if there was a newline - prevents
// counting indentation as spaces
spaces = right.ExcessSpaces() ? 1 : 0;
}
return {spaces, "Limit spaces before ':' in bit slice to 0 or 1"};
}
Expand Down
12 changes: 12 additions & 0 deletions verilog/formatting/token_annotator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5317,6 +5317,18 @@ TEST(TokenAnnotatorTest, OriginalSpacingSensitiveTests) {
{NodeEnum::kDimensionRange},
{0, SpacingOptions::kUndecided},
},
{
DefaultStyle,
verilog_tokentype::SymbolIdentifier,
"a",
"\n ",
':',
":",
{NodeEnum::kDimensionRange},
{NodeEnum::kDimensionRange},
// 0 spaces as this is an indentation, not spacing
{0, SpacingOptions::kUndecided},
},
{
DefaultStyle,
'*',
Expand Down
6 changes: 4 additions & 2 deletions verilog/parser/verilog.y
Original file line number Diff line number Diff line change
Expand Up @@ -3685,11 +3685,13 @@ struct_union_member
{ $$ = MakeTaggedNode(N::kStructUnionMember, $1, $2, $3, $4, $5, $6); }
| preprocessor_directive
{ $$ = std::move($1); }
| MacroCall
{ $$ = std::move($1);}
// since the semicolon is optional, it is better to have both cases covered
| MacroCall ';'
{ $$ = ExtendNode($1, $2);}
| MacroCall
{ $$ = std::move($1);}
| MacroCallId '(' macro_args_opt MacroCallCloseToEndLine
{ $$ = MakeTaggedNode(N::kMacroCall, $1, MakeParenGroup($2, $3, $4)); }
;

case_item
Expand Down
3 changes: 3 additions & 0 deletions verilog/parser/verilog_parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3771,6 +3771,9 @@ static constexpr ParserTestCaseArray kStructTests = {
"struct packed { int i; bit b; } foo;",
"struct packed signed { int i; bit b; } foo;",
"struct packed unsigned { int i; bit b; } foo;",
"struct { `BAZ() } foo;",
"struct { `BAZ(); } foo;",
"struct { `BAZ()\n } foo;",
};

static constexpr ParserTestCaseArray kEnumTests = {
Expand Down

0 comments on commit d600c4a

Please sign in to comment.