Skip to content

Commit

Permalink
Address third row of comments:
Browse files Browse the repository at this point in the history
- Perform check before doing SymbolCastToNode
- Call function with broken input (leafs, ...)
- Add comments, style cleanup.
  • Loading branch information
IEncinas10 committed Apr 4, 2023
1 parent 820e8f8 commit ab991cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
8 changes: 6 additions & 2 deletions verilog/CST/expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ const verible::Symbol* GetConditionExpressionFalseCase(

const verible::TokenInfo* GetUnaryPrefixOperator(
const verible::Symbol& symbol) {
const SyntaxTreeNode* node = &verible::SymbolCastToNode(symbol);
const SyntaxTreeNode* node = symbol.Kind() == SymbolKind::kNode
? &verible::SymbolCastToNode(symbol)
: nullptr;
if (!node || !MatchNodeEnumOrNull(*node, NodeEnum::kUnaryPrefixExpression)) {
return nullptr;
}
Expand All @@ -110,7 +112,9 @@ const verible::TokenInfo* GetUnaryPrefixOperator(
}

const verible::Symbol* GetUnaryPrefixOperand(const verible::Symbol& symbol) {
const SyntaxTreeNode* node = &verible::SymbolCastToNode(symbol);
const SyntaxTreeNode* node = symbol.Kind() == SymbolKind::kNode
? &verible::SymbolCastToNode(symbol)
: nullptr;
if (!node || !MatchNodeEnumOrNull(*node, NodeEnum::kUnaryPrefixExpression)) {
return nullptr;
}
Expand Down
2 changes: 2 additions & 0 deletions verilog/CST/expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ TEST(GetUnaryPrefixOperator, Exprs) {
} else {
EXPECT_NE(NodeEnum(last_node->Tag().tag),
NodeEnum::kUnaryPrefixExpression);
EXPECT_FALSE(GetUnaryPrefixOperand(*last_node));
}
}
}
Expand All @@ -472,6 +473,7 @@ TEST(GetUnaryPrefixOperand, Exprs) {
} else {
EXPECT_NE(NodeEnum(last_node->Tag().tag),
NodeEnum::kUnaryPrefixExpression);
EXPECT_FALSE(GetUnaryPrefixOperand(*last_node));
}
}
}
Expand Down
27 changes: 15 additions & 12 deletions verilog/analysis/checkers/forbid_negative_array_dim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,30 @@ static const Matcher& UnaryPrefixExprMatcher() {

void ForbidNegativeArrayDim::HandleSymbol(
const verible::Symbol& symbol, const verible::SyntaxTreeContext& context) {
// This only works for simple unary expressions. They can't be nested inside
// other expressions. This avoids false positives of the form:
// logic l [10+(-5):0], logic l[-(-5):0]
if (!context.IsInsideFirst(
{NodeEnum::kPackedDimensions, NodeEnum::kUnpackedDimensions},
{NodeEnum::kBinaryExpression, NodeEnum::kUnaryPrefixExpression})) {
return;
}

verible::matcher::BoundSymbolManager manager;
if (UnaryPrefixExprMatcher().Matches(symbol, &manager)) {
int value = 0;
if (!UnaryPrefixExprMatcher().Matches(symbol, &manager)) return;

// Extract operand and operator from kUnaryPrefixExpression
const verible::TokenInfo* u_operator =
verilog::GetUnaryPrefixOperator(symbol);
const verible::Symbol* operand = verilog::GetUnaryPrefixOperand(symbol);
// As we've previously ensured that this symbol is a kUnaryPrefixExpression
// both its operator and operand are defined
const verible::TokenInfo* u_operator =
verilog::GetUnaryPrefixOperator(symbol);
const verible::Symbol* operand = verilog::GetUnaryPrefixOperand(symbol);

const bool is_constant = verilog::ConstantIntegerValue(*operand, &value);
if (is_constant && value > 0 && u_operator->text() == "-") {
const verible::TokenInfo token(TK_OTHER,
verible::StringSpanOfSymbol(symbol));
violations_.insert(verible::LintViolation(token, kMessage, context));
}
int value = 0;
const bool is_constant = verilog::ConstantIntegerValue(*operand, &value);
if (is_constant && value > 0 && u_operator->text() == "-") {
const verible::TokenInfo token(TK_OTHER,
verible::StringSpanOfSymbol(symbol));
violations_.insert(verible::LintViolation(token, kMessage, context));
}
}

Expand Down

0 comments on commit ab991cf

Please sign in to comment.