From 068f9102efe9d9900a81963e4bcd435c5b7abb05 Mon Sep 17 00:00:00 2001 From: Benjamin Thiemann Date: Wed, 3 Sep 2025 04:18:33 +0200 Subject: [PATCH] Issue#1451: Fixed array detection and parenthesis handling in multiline_alignment_between_tokens (#1468) * Issue#1451: Fixed array detection in multiline_alignment_between_tokens * Move function toi_is_array from multiline_simple_structure to the utils module and use this function in multiline_alignment_between_tokens, because it correctly classifies parenthesis of aggregates. The previous function there falsely triggered array detection on parenthesis around logical operators. * Rename "check_for_array" to "skip_array", because that is what the variable is used to do. * Extend tests for concurrent_003 with logical expressions that start with a parenthesis. * Issue#1451: Fixed parenthesis handling in multiline_alignment_between_tokens Do not add parenthesis pairs to the list of occurring parenthesis if they are on the same line. --- ...put.fixed_align_left_no_align_paren_no.vhd | 16 +++++++------- ...ut.fixed_align_left_no_align_paren_yes.vhd | 16 +++++++------- ...ut.fixed_align_left_yes_align_paren_no.vhd | 16 +++++++------- ...t.fixed_align_left_yes_align_paren_yes.vhd | 16 +++++++------- tests/concurrent/rule_003_test_input.vhd | 16 +++++++------- .../multiline_alignment_between_tokens.py | 21 ++++++++++--------- vsg/rules/multiline_simple_structure.py | 12 ++--------- vsg/rules/selected_assignment/rule_400.py | 2 +- vsg/rules/signal/rule_400.py | 1 + vsg/rules/variable/rule_400.py | 1 + vsg/vhdlFile/utils.py | 9 ++++++++ 11 files changed, 65 insertions(+), 61 deletions(-) diff --git a/tests/concurrent/rule_003_test_input.fixed_align_left_no_align_paren_no.vhd b/tests/concurrent/rule_003_test_input.fixed_align_left_no_align_paren_no.vhd index f7686850..5d76c143 100644 --- a/tests/concurrent/rule_003_test_input.fixed_align_left_no_align_paren_no.vhd +++ b/tests/concurrent/rule_003_test_input.fixed_align_left_no_align_paren_no.vhd @@ -12,12 +12,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -30,12 +30,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -48,12 +48,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -66,12 +66,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; diff --git a/tests/concurrent/rule_003_test_input.fixed_align_left_no_align_paren_yes.vhd b/tests/concurrent/rule_003_test_input.fixed_align_left_no_align_paren_yes.vhd index 247a652b..c5bfbaa3 100644 --- a/tests/concurrent/rule_003_test_input.fixed_align_left_no_align_paren_yes.vhd +++ b/tests/concurrent/rule_003_test_input.fixed_align_left_no_align_paren_yes.vhd @@ -12,12 +12,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -30,12 +30,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -48,12 +48,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -66,12 +66,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; diff --git a/tests/concurrent/rule_003_test_input.fixed_align_left_yes_align_paren_no.vhd b/tests/concurrent/rule_003_test_input.fixed_align_left_yes_align_paren_no.vhd index 278fa3fa..1f85a47f 100644 --- a/tests/concurrent/rule_003_test_input.fixed_align_left_yes_align_paren_no.vhd +++ b/tests/concurrent/rule_003_test_input.fixed_align_left_yes_align_paren_no.vhd @@ -12,12 +12,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -30,12 +30,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -48,12 +48,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -66,12 +66,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; diff --git a/tests/concurrent/rule_003_test_input.fixed_align_left_yes_align_paren_yes.vhd b/tests/concurrent/rule_003_test_input.fixed_align_left_yes_align_paren_yes.vhd index 7157d10e..ed56573f 100644 --- a/tests/concurrent/rule_003_test_input.fixed_align_left_yes_align_paren_yes.vhd +++ b/tests/concurrent/rule_003_test_input.fixed_align_left_yes_align_paren_yes.vhd @@ -12,12 +12,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -30,12 +30,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -48,12 +48,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -66,12 +66,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; diff --git a/tests/concurrent/rule_003_test_input.vhd b/tests/concurrent/rule_003_test_input.vhd index 38d1cf92..2d5d8676 100644 --- a/tests/concurrent/rule_003_test_input.vhd +++ b/tests/concurrent/rule_003_test_input.vhd @@ -12,12 +12,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -30,12 +30,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -48,12 +48,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; @@ -66,12 +66,12 @@ begin resize(unsigned(I_FOO) + unsigned(I_BAR), q_foo'length); - n_bar <= a or b and c + n_bar <= (a or b) and c xor z and x or w and z; n_bar <= - a or b and c + (a or b) and c xor z and x or w and z; diff --git a/vsg/rules/multiline_alignment_between_tokens.py b/vsg/rules/multiline_alignment_between_tokens.py index d381a775..2ff66c9d 100644 --- a/vsg/rules/multiline_alignment_between_tokens.py +++ b/vsg/rules/multiline_alignment_between_tokens.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from vsg import parser, token, violation +from vsg import parser, violation from vsg.rule_group import alignment from vsg.rules import alignment_utils, utils as rules_utils from vsg.vhdlFile import utils @@ -37,7 +37,7 @@ class multiline_alignment_between_tokens(alignment.Rule): self.iIndentAfterParen = 1 self.override = False self.configuration_documentation_link = "configuring_multiline_indent_rules_link" - self.check_for_array = True + self.skip_array = True def _get_tokens_of_interest(self, oFile): lToi = [] @@ -47,7 +47,7 @@ class multiline_alignment_between_tokens(alignment.Rule): lReturn = [] for oToi in lToi: - if self.check_for_array and toi_is_an_array(oToi): + if self.skip_array and utils.toi_is_array(oToi): continue iLine, lTokens = utils.get_toi_parameters(oToi) iFirstLine, iFirstLineIndent = alignment_utils.get_first_line_info(iLine, oFile) @@ -102,6 +102,11 @@ class multiline_alignment_between_tokens(alignment.Rule): iColumn += len(oToken.get_value()) if isinstance(oToken, parser.close_parenthesis): + dLastParen = lParens[-1] + if dLastParen["type"] == "open" and dLastParen["line"] == iLine: + lParens.pop() + continue + dParen = {} dParen["type"] = "close" dParen["line"] = iLine @@ -121,6 +126,9 @@ class multiline_alignment_between_tokens(alignment.Rule): if iFirstLine == iLastLine: continue + if len(lParens) == 0 or lParens[0]["line"] != iFirstLine: + bStartsWithParen = False + iFirstTokenLength = len(lTokens[0].get_value()) if not align_paren(self) and align_left(self): @@ -528,10 +536,3 @@ def _analyze_align_paren_no_align_left_no(iFirstLine, iLastLine, lParens, dActua dReturn[iLine] = dExpectedIndent[iLine] * " " return dReturn - - -def toi_is_an_array(oToi): - for sAssignmentToken in ["<=", ":="]: - if utils.are_next_consecutive_tokens_ignoring_whitespace([sAssignmentToken, "("], 0, oToi.get_tokens()): - return True - return False diff --git a/vsg/rules/multiline_simple_structure.py b/vsg/rules/multiline_simple_structure.py index e96a2959..31ca7203 100644 --- a/vsg/rules/multiline_simple_structure.py +++ b/vsg/rules/multiline_simple_structure.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from vsg import parser, token, violation +from vsg import parser, violation from vsg.rule_group import structure from vsg.rules import utils as rules_utils from vsg.vhdlFile import utils @@ -54,7 +54,7 @@ class multiline_simple_structure(structure.Rule): lReturn = [] for oToi in lToi: - if toi_is_an_array(oToi): + if utils.toi_is_array(oToi): continue lReturn.append(oToi) @@ -128,11 +128,3 @@ def _create_action_dictionary(sType, sAction): dReturn["type"] = sType dReturn["action"] = sAction return dReturn - - -def toi_is_an_array(oToi): - lTokens = oToi.get_tokens() - for oToken in lTokens: - if isinstance(oToken, token.aggregate.open_parenthesis): - return True - return False diff --git a/vsg/rules/selected_assignment/rule_400.py b/vsg/rules/selected_assignment/rule_400.py index 55b5268c..c6adcf1c 100644 --- a/vsg/rules/selected_assignment/rule_400.py +++ b/vsg/rules/selected_assignment/rule_400.py @@ -43,4 +43,4 @@ class rule_400(Rule): self.align_left = "yes" self.align_paren = "no" self.override = True - self.check_for_array = False + self.skip_array = False diff --git a/vsg/rules/signal/rule_400.py b/vsg/rules/signal/rule_400.py index d982a3b4..bf9ea25d 100644 --- a/vsg/rules/signal/rule_400.py +++ b/vsg/rules/signal/rule_400.py @@ -38,3 +38,4 @@ class rule_400(Rule): self.subphase = 3 self.bIgnoreStartParen = True self.bConstraint = True + self.skip_array = False diff --git a/vsg/rules/variable/rule_400.py b/vsg/rules/variable/rule_400.py index f0c95ad0..ab434377 100644 --- a/vsg/rules/variable/rule_400.py +++ b/vsg/rules/variable/rule_400.py @@ -38,3 +38,4 @@ class rule_400(Rule): self.subphase = 3 self.bIgnoreStartParen = True self.bConstraint = True + self.skip_array = False diff --git a/vsg/vhdlFile/utils.py b/vsg/vhdlFile/utils.py index bd4d3a3e..af3c480c 100644 --- a/vsg/vhdlFile/utils.py +++ b/vsg/vhdlFile/utils.py @@ -4,6 +4,7 @@ import sys from vsg import exceptions, parser, token from vsg.token import ( + aggregate, choice, direction, element_association, @@ -751,6 +752,14 @@ def get_toi_parameters(oToi): return oToi.get_line_number(), oToi.get_tokens() +def toi_is_array(oToi): + lTokens = oToi.get_tokens() + for oToken in lTokens: + if isinstance(oToken, aggregate.open_parenthesis): + return True + return False + + def does_token_start_line(iToken, lTokens): if isinstance(lTokens[iToken - 1], parser.whitespace) and isinstance(lTokens[iToken - 2], parser.carriage_return): return True