Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions verible/verilog/analysis/checkers/port-name-suffix-rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ bool PortNameSuffixRule::IsSuffixCorrect(std::string_view suffix,
{"output", {"o", "no", "po"}},
{"inout", {"io", "nio", "pio"}}};

// At this point it is guaranteed that the direction will be set to
// one of the expected values (used as keys in the map above).
// Therefore checking the suffix like this is safe
return suffixes.at(direction).count(suffix) == 1;
// `direction` is usually one of the map keys, but the grammar also permits a
// `ref` port direction, which has no suffix convention. Look the direction up
// safely and treat an unknown direction as "correct" (no violation),
// consistent with Violation() which also ignores non-input/output/inout
// directions. Using std::map::at() here would throw on e.g. "ref".
const auto it = suffixes.find(direction);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the FindWithDefault() in verible/common/util/container-util.h

Maybe use that instead, not only for a more compact writing but also a more visible 'we're looking for something that might not be there but we have a fallback' intent.

return FindWithDefault(suffixes, direction, 1) == 1;

if (it == suffixes.end()) return true;
return it->second.count(suffix) == 1;
}

void PortNameSuffixRule::HandleSymbol(const Symbol &symbol,
Expand All @@ -113,8 +117,11 @@ void PortNameSuffixRule::HandleSymbol(const Symbol &symbol,
absl::StrSplit(name, '_', absl::SkipEmpty());

if (name_parts.size() < 2) {
// No suffix at all
// No suffix at all. This also covers an all-underscore name (e.g. "_"),
// for which SkipEmpty leaves name_parts empty; return here so the
// name_parts.back() access below is not reached on an empty vector.
Violation(direction, token, context);
return;
}

if (!IsSuffixCorrect(name_parts.back(), direction)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ TEST(PortNameSuffixRuleTest, AcceptTests) {
{"module t (input bit name_i); endmodule;"},
{"module t (output bit abc_o); endmodule;"},
{"module t (inout bit xyz_io); endmodule;"},
// A `ref` port has no suffix convention and must not be flagged (and must
// not crash the rule via std::map::at).
{"module t (ref logic data_x); endmodule;"},
{"module t (input logic name_i,\n"
"output logic abc_o,\n"
"inout logic xyz_io,\n"
Expand Down Expand Up @@ -90,6 +93,10 @@ TEST(PortNameSuffixRuleTest, RejectTests) {
{"module t (output logic ", {kToken, "_o"}, "); endmodule;"},
{"module t (inout logic ", {kToken, "_io"}, "); endmodule;"},

// An all-underscore name splits (SkipEmpty) to an empty parts list; it
// must report a suffix violation, not dereference an empty vector.
{"module t (input logic ", {kToken, "_"}, "); endmodule;"},

{"module t (input logic ", {kToken, "namei"}, "); endmodule;"},
{"module t (input logic ", {kToken, "nam_ei"}, "); endmodule;"},
{"module t (input logic ", {kToken, "name_o"}, "); endmodule;"},
Expand Down
Loading