Skip to content

Commit

Permalink
Merge pull request #1868 from IEncinas10/suggest_size_for_undersized_…
Browse files Browse the repository at this point in the history
…binary_literal

undersized-binary-literal: suggest inferred size
  • Loading branch information
hzeller authored Apr 17, 2023
2 parents 5a92de1 + 78dc821 commit 682db3e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion verilog/analysis/checkers/undersized_binary_literal_rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ void UndersizedBinaryLiteralRule::HandleSymbol(
LOG(FATAL) << "Unexpected base '" << base_text << "'"; // Lexer issue ?
}

const int missing_bits = width - number.literal.length() * bits_per_digit;
const int inferred_size = number.literal.length() * bits_per_digit;
const int missing_bits = width - inferred_size;
// if !lint_zero, "0" is an exceptions. Also "?" is always an exception
if (missing_bits > 0 && (lint_zero_ || number.literal != "0") &&
number.literal != "?") {
Expand Down Expand Up @@ -156,6 +157,10 @@ void UndersizedBinaryLiteralRule::HandleSymbol(
}
}

// Suggest inferred width.
autofixes.push_back(AutoFix("Adjust width to inferred width",
{{width_text, std::to_string(inferred_size)}}));

violations_.insert(LintViolation(
digits_leaf->get(),
FormatReason(width_text, base_text, number.base, digits_text), context,
Expand Down
21 changes: 21 additions & 0 deletions verilog/analysis/checkers/undersized_binary_literal_rule_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@ TEST(UndersizedBinaryLiteralRule, AutoFixSingleDigitProvideDecimalAlternative) {
"autofix:true");
}

TEST(UndersizedBinaryLiteralRule, AutoFixProvideInferredSize) {
// Alternatives the auto fix offers
constexpr int kFirstFix = 0;
constexpr int kSecondFix = 1;
const std::initializer_list<verible::AutoFixInOut> kTestCases = {
// First choice: zero expand
{"localparam x = 32'h10;", "localparam x = 32'h00000010;", kFirstFix},
{"localparam x = 3'b01;", "localparam x = 3'b001;", kFirstFix},
{"localparam x = 8'o77;", "localparam x = 8'o077;", kFirstFix},

// Second choice: Adjust size to inferred size
{"localparam x = 32'h10;", "localparam x = 8'h10;", kSecondFix},
{"localparam x = 3'b01;", "localparam x = 2'b01;", kSecondFix},
{"localparam x = 8'o77;", "localparam x = 6'o77;", kSecondFix},
};
RunApplyFixCases<VerilogAnalyzer, UndersizedBinaryLiteralRule>(
kTestCases,
"bin:true;hex:true;oct:true;"
"autofix:true");
}

} // namespace
} // namespace analysis
} // namespace verilog

0 comments on commit 682db3e

Please sign in to comment.