Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 27 additions & 2 deletions verible/verilog/analysis/checkers/port-name-suffix-rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,33 @@ void PortNameSuffixRule::HandleSymbol(const Symbol &symbol,
const auto *identifier_leaf = GetIdentifierFromPortDeclaration(symbol);
const auto *direction_leaf = GetDirectionFromPortDeclaration(symbol);
const auto token = identifier_leaf->get();
const auto direction =
direction_leaf ? direction_leaf->get().text() : implicit_direction;
std::string 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.

Why not std::string_view ? The texts you assign are guaranteed to exist for the lifetime, and you also save std::string conversion.

if (direction_leaf) {
direction = std::string(direction_leaf->get().text());
} else {
// Non-ANSI style: try to find direction inside module body
// Search parent for matching port declaration with direction
const SyntaxTreeContext *parent_ctx = &context;
while (parent_ctx && !direction_leaf) {
// Iterate symbols in parent context to find matching identifier
for (const auto &sym : *parent_ctx) {
if (sym->Kind() == SymbolKind::kNode) {
const auto *decl_dir = GetDirectionFromPortDeclaration(*sym);
const auto *decl_id = GetIdentifierFromPortDeclaration(*sym);
if (decl_id &&
decl_id->get().text() == identifier_leaf->get().text()) {
if (decl_dir) {
direction = std::string(decl_dir->get().text());
break;
}
}
}
}
parent_ctx = parent_ctx->parent();
}
if (direction.empty()) direction = std::string(implicit_direction);
}

const auto name = ABSL_DIE_IF_NULL(identifier_leaf)->get().text();

// Check if there is any suffix
Expand Down
23 changes: 23 additions & 0 deletions verible/verilog/analysis/checkers/port-name-suffix-rule_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ TEST(PortNameSuffixRuleTest, AcceptTests) {
"output bit abcb_o,\n"
"inout bit xyzb_io);\n"
"endmodule;"},
{"module t (name_i,\n"
"abc_o,\n"
"xyz_io,\n"
"namea_i,\n"
"abca_o,\n"
"xyza_io,\n"
"nameb_i,\n"
"abcb_o,\n"
"xyzb_io);\n"
"input logic name_i;\n"
"output logic abc_o;\n"
"inout logic xyz_io;\n"
"input logic [7:0] namea_i;\n"
"output logic [2:0] abca_o;\n"
"inout logic [3:0] xyza_io;\n"
"input bit nameb_i;\n"
"output bit abcb_o;\n"
"inout bit xyzb_io;\n"
"endmodule;"},
};
RunLintTestCases<VerilogAnalyzer, PortNameSuffixRule>(kTestCases);
}
Expand Down Expand Up @@ -108,6 +127,10 @@ TEST(PortNameSuffixRuleTest, RejectTests) {
{"module t (output logic ", {kToken, "name_pi"}, "); endmodule;"},
{"module t (output logic ", {kToken, "name_pio"}, "); endmodule;"},

{"module t (", {kToken, "name"}, "); input logic name; endmodule;"},
{"module t (", {kToken, "abc"}, "); output logic abc; endmodule;"},
{"module t (", {kToken, "xyz"}, "); inout logic [3:0] xyz; endmodule;"},

{"module t (input logic ",
{kToken, "name"},
",\n"
Expand Down
Loading