Updates for issue #450:

Adding first attempt at detecting first_paren_new_line option.
Fixing still needs to be implemented.
This commit is contained in:
Jeremiah Leary 2021-01-31 12:27:41 -06:00
parent 0a50b02d0c
commit 5e4000dc83
7 changed files with 403 additions and 159 deletions

View File

@ -5,22 +5,25 @@ There are rules which will check indent and formatting of multiline expressions
There are several options to these rules:
+----------------------+---------+---------+--------------------------------------+
| Method | Type | Default | Description |
+======================+=========+=========+======================================+
| first_paren_new_line | boolean | False | First opening parenthesis on it's own line.
+----------------------+---------+---------+--------------------------------------+
| last_paren_new_line | boolean | False | Last closing parenthesis on it's own line.
+----------------------+---------+---------+--------------------------------------+
| open_paren_new_line | boolean | False | Insert new line after open parenthesis.
+----------------------+----------------------------------------------------------+
| close_paren_new_line | boolean | False | Insert new line before close parenthesis.
+----------------------+----------------------------------------------------------+
| new_line_after_comma | boolean | False | Insert new line after the commas.
+----------------------+----------------------------------------------------------+
| align_left | boolean | True | True = New lines will be aligned left.
| | | | False = Align to current column
+----------------------+----------------------------------------------------------+
+----------------------+---------+---------+---------------------------------------------------------+
| Method | Type | Default | Description |
+======================+=========+=========+=========================================================+
| first_paren_new_line | boolean | False | First opening parenthesis on it's own line. |
+----------------------+---------+---------+---------------------------------------------------------+
| last_paren_new_line | boolean | False | Last closing parenthesis on it's own line. |
+----------------------+---------+---------+---------------------------------------------------------+
| open_paren_new_line | boolean | False | Insert new line after open parenthesis. |
+----------------------+-----------------------------------------------------------------------------+
| close_paren_new_line | boolean | False | Insert new line before close parenthesis. |
+----------------------+-----------------------------------------------------------------------------+
| new_line_after_comma | boolean | False | Insert new line after the commas. |
+----------------------+-----------------------------------------------------------------------------+
| align_left | boolean | True | True = New lines will be aligned left. |
| | | | False = Align to current column |
+----------------------+-----------------------------------------------------------------------------+
| ignore_single_line | boolean | True | Do not apply rules if expression/condition is contained |
| | | | on a single line. |
+----------------------+-----------------------------------------------------------------------------+
The options can be combined to format the output.
@ -38,8 +41,8 @@ This is an example of how to configure these options.
.. NOTE:: All examples below are using the rule **constant_012**.
Example: first_paren_new_line and align_left
############################################
Example: first_paren_new_line and align_left and not ignore_single_line
#######################################################################
The following code would fail with this option:
@ -54,8 +57,8 @@ The following code would pass with this option:
constant c_const : t_type :=
(a => 0, b => 1);
Example: first_paren_new_line and not align_left
################################################
Example: first_paren_new_line and not align_left and not ignore_single_line
###########################################################################
The following code would fail with this option:
@ -70,8 +73,8 @@ The following code would pass with this option:
constant c_const : t_type :=
(a => 0, b => 1);
Example: last_paren_new_line and align_left
###########################################
Example: last_paren_new_line and align_left and not ignore_single_line
######################################################################
The following code would fail with this option:
@ -86,8 +89,8 @@ The following code would pass with this option:
constant c_const : t_type := (a => 0, b => 1
);
Example: last_paren_new_line and not align_left
###############################################
Example: last_paren_new_line and not align_left and not ignore_single_line
##########################################################################
The following code would fail with this option:
@ -102,8 +105,8 @@ The following code would pass with this option:
constant c_const : t_type := (a => 0, b => 1
);
Example: first_paren_new_line and last_paren_new_line and align_left
####################################################################
Example: first_paren_new_line and last_paren_new_line and align_left and not ignore_single_line
###############################################################################################
The following code would fail with this option:
@ -120,8 +123,8 @@ The following code would pass with this option:
a => 0, b => 1
);
Example: first_paren_new_line and last_paren_new_line and not align_left
########################################################################
Example: first_paren_new_line and last_paren_new_line and not align_left and not ignore_single_line
###################################################################################################
The following code would fail with this option:
@ -138,8 +141,8 @@ The following code would pass with this option:
a => 0, b => 1
);
Example: new_line_after_comma and align_left
############################################
Example: new_line_after_comma and align_left and not ignore_single_line
#######################################################################
The following code would fail with this option:
@ -154,8 +157,8 @@ The following code would pass with this option:
constant c_const : t_type := (a => 0,
b => 1);
Example: new_line_after_comma and not align_left
################################################
Example: new_line_after_comma and not align_left and not ignore_single_line
###########################################################################
The following code would fail with this option:
@ -170,8 +173,8 @@ The following code would pass with this option:
constant c_const : t_type := (a => 0,
b => 1);
Example: new_line_after_comma and align_left and first_paren_new_line and last_paren_new_line
#################################################################################################
Example: new_line_after_comma and align_left and first_paren_new_line and last_paren_new_line and not ignore_single_line
########################################################################################################################
The following code would fail with this option:
@ -187,8 +190,8 @@ The following code would pass with this option:
(a => 0,
b => 1);
Example: new_line_after_comma and not align_left and first_paren_new_line and last_paren_new_line
#################################################################################################
Example: new_line_after_comma and not align_left and first_paren_new_line and last_paren_new_line and not ignore_single_line
############################################################################################################################
The following code would fail with this option:
@ -204,8 +207,8 @@ The following code would pass with this option:
(a => 0,
b => 1);
Example: open_paren_new_line and align_left
###########################################
Example: open_paren_new_line and align_left and not ignore_single_line
######################################################################
The following code would fail with this option:
@ -222,8 +225,8 @@ The following code would pass with this option:
a => 0, b => 1), (
c => 0, d => 1));
Example: open_paren_new_line and not align_left
###############################################
Example: open_paren_new_line and not align_left and not ignore_single_line
##########################################################################
The following code would fail with this option:
@ -240,8 +243,8 @@ The following code would pass with this option:
a => 0, b => 1), (
c => 0, d => 1));
Example: close_paren_new_line and align_left
############################################
Example: close_paren_new_line and align_left and not ignore_single_line
#######################################################################
The following code would fail with this option:
@ -257,8 +260,8 @@ The following code would pass with this option:
), (c => 0, d => 1
));
Example: close_paren_new_line and not align_left
################################################
Example: close_paren_new_line and not align_left and not ignore_single_line
###########################################################################
The following code would fail with this option:
@ -275,8 +278,8 @@ The following code would pass with this option:
a => 0, b => 1), (
c => 0, d => 1));
Example: open_paren_new_line and close_paren_new_line and align_left
####################################################################
Example: open_paren_new_line and close_paren_new_line and align_left and not ignore_single_line
###############################################################################################
The following code would fail with this option:
@ -315,8 +318,8 @@ The following code would pass with this option:
c => 0, d => 1
));
Example: all options enabled
############################
Example: all options enabled except ignore_single_line
######################################################
The following code would fail with this option:
@ -340,8 +343,8 @@ The following code would pass with this option:
)
);
Example: all options enabled except align_left
##############################################
Example: all options enabled except align_left and ignore_single_line
#####################################################################
The following code would fail with this option:
@ -364,4 +367,3 @@ The following code would pass with this option:
d => 1
)
);

View File

@ -5,7 +5,7 @@ from vsg import token
from vsg import violation
from vsg.vhdlFile import utils
from vsg.rules import utils as rule_utils
lTokenPairs = []
lTokenPairs.append([token.constant_declaration.constant_keyword, token.constant_declaration.semicolon])
@ -35,123 +35,186 @@ class rule_012(rule.Rule):
self.subphase = 4
self.lTokenPairs = lTokenPairs
self.bExcludeLastToken = False
self.first_paren_new_line = False
self.configuration.append('first_paren_new_line')
self.last_paren_new_line = False
self.configuration.append('last_paren_new_line')
self.open_paren_new_line = False
self.configuration.append('open_paren_new_line')
self.close_paren_new_line = False
self.configuration.append('close_paren_new_line')
self.new_line_after_comma = False
self.configuration.append('new_line_after_comma')
self.align_left = True
self.configuration.append('align_left')
self.ignore_single_line = True
self.configuration.append('ignore_single_line')
def analyze(self, oFile):
def _get_tokens_of_interest(self, oFile):
lToi = []
for lTokenPair in self.lTokenPairs:
aToi = oFile.get_tokens_bounded_by(lTokenPair[0], lTokenPair[1], bExcludeLastToken=self.bExcludeLastToken)
lToi = utils.combine_two_token_class_lists(lToi, aToi)
return lToi
for oNewToi in lToi:
iLine, lTokens = utils.get_toi_parameters(oNewToi)
def analyze(self, oFile):
lToi = self._get_tokens_of_interest(oFile)
bSearchOpen = False
bSearchClose = False
for iToken, oToken in enumerate(lTokens):
if isinstance(oToken, token.constant_declaration.assignment_operator):
if utils.are_next_consecutive_token_types_ignoring_whitespace([parser.open_parenthesis], iToken + 1, lTokens):
bSearchOpen = True
if bSearchOpen:
if isinstance(oToken, parser.open_parenthesis):
iStartIndex = iToken
bSearchOpen = False
bSearchClose = True
if bSearchClose:
if isinstance(oToken, parser.close_parenthesis):
iEndIndex = iToken
if bSearchClose and isinstance(oToken, token.constant_declaration.semicolon):
lNewTokens = lTokens.copy()
lNewTokens = utils.remove_trailing_whitespace_and_comments(lNewTokens[:iEndIndex])
iEndIndex = iEndIndex - ( len(lTokens) - len(lNewTokens) )
oToi = oNewToi.extract_tokens(iStartIndex, iEndIndex)
break
else:
for oToi in lToi:
if rule_utils.is_single_line(oToi) and self.ignore_single_line:
continue
iLine, lTokens = utils.get_toi_parameters(oToi)
if not _is_open_paren_after_assignment(oToi):
continue
# print('-'*80)
# print(iLine)
# print(lTokens)
iStartColumn = calculate_start_column(oFile, oToi)
lColumn = []
lColumn.append(iStartColumn)
bCheckAlignment = False
iFirstColumn = oFile.get_column_of_token_index(oToi.get_start_index())
iColumn = iFirstColumn
iPreviousColumn = 0
iIndent = 0
# print('-'*80)
for iToken, oToken in enumerate(lTokens):
iLine = utils.increment_line_number(iLine, oToken)
if isinstance(oToken, parser.carriage_return):
bCheckAlignment = True
iPreviousColumn = lColumn[-1]
iColumn = 0
if isinstance(lTokens[iToken + 1], parser.whitespace):
iIndent = len(lTokens[iToken + 1].get_value())
else:
iIndent = 0
continue
if isinstance(oToken, parser.blank_line):
bCheckAlignment = False
continue
iColumn += len(oToken.get_value())
if isinstance(oToken, parser.open_parenthesis):
lColumn.append(iColumn + iPreviousColumn - iIndent)
if isinstance(oToken, parser.close_parenthesis):
lColumn.pop()
if bCheckAlignment:
if isinstance(oToken, parser.whitespace):
if len(oToken.get_value()) != lColumn[-1]:
dAction = {}
dAction['line'] = iLine
dAction['column'] = lColumn[-1]
dAction['action'] = 'adjust'
dAction['indent'] = iIndent
dAction['previous'] = iPreviousColumn
oViolation = violation.New(iLine, oToi.extract_tokens(iToken, iToken), self.solution)
oViolation.set_action(dAction)
self.add_violation(oViolation)
# print(dAction)
else:
if lColumn != 0:
dAction = {}
dAction['line'] = iLine
if isinstance(oToken, parser.open_parenthesis):
dAction['column'] = lColumn[-2]
else:
dAction['column'] = lColumn[-1]
dAction['action'] = 'insert'
dAction['indent'] = iIndent
dAction['previous'] = iPreviousColumn
oViolation = violation.New(iLine, oToi.extract_tokens(iToken, iToken), self.solution)
oViolation.set_action(dAction)
self.add_violation(oViolation)
bCheckAlignment = False
def _fix_violation(self, oViolation):
lTokens = oViolation.get_tokens()
dAction = oViolation.get_action()
if dAction['action'] == 'adjust':
lTokens[0].set_value(' '*dAction['column'])
else:
lTokens.insert(0, parser.whitespace(' '*dAction['column']))
oViolation.set_tokens(lTokens)
_check_first_paren_new_line(self, oToi)
def calculate_start_column(oFile, oToi):
iReturn = oFile.get_column_of_token_index(oToi.get_start_index())
iReturn += len(oToi.get_tokens()[0].get_value())
iReturn += 1
return iReturn
# for oNewToi in lToi:
#
# iLine, lTokens = utils.get_toi_parameters(oNewToi)
#
# bSearchOpen = False
# bSearchClose = False
# for iToken, oToken in enumerate(lTokens):
# if isinstance(oToken, token.constant_declaration.assignment_operator):
# if utils.are_next_consecutive_token_types_ignoring_whitespace([parser.open_parenthesis], iToken + 1, lTokens):
# bSearchOpen = True
# if bSearchOpen:
# if isinstance(oToken, parser.open_parenthesis):
# iStartIndex = iToken
# bSearchOpen = False
# bSearchClose = True
# if bSearchClose:
# if isinstance(oToken, parser.close_parenthesis):
# iEndIndex = iToken
# if bSearchClose and isinstance(oToken, token.constant_declaration.semicolon):
# lNewTokens = lTokens.copy()
# lNewTokens = utils.remove_trailing_whitespace_and_comments(lNewTokens[:iEndIndex])
# iEndIndex = iEndIndex - ( len(lTokens) - len(lNewTokens) )
# oToi = oNewToi.extract_tokens(iStartIndex, iEndIndex)
# break
# else:
# continue
#
# iLine, lTokens = utils.get_toi_parameters(oToi)
#
## print('-'*80)
## print(iLine)
## print(lTokens)
# iStartColumn = calculate_start_column(oFile, oToi)
# lColumn = []
# lColumn.append(iStartColumn)
# bCheckAlignment = False
# iFirstColumn = oFile.get_column_of_token_index(oToi.get_start_index())
# iColumn = iFirstColumn
# iPreviousColumn = 0
# iIndent = 0
## print('-'*80)
# for iToken, oToken in enumerate(lTokens):
#
# iLine = utils.increment_line_number(iLine, oToken)
#
# if isinstance(oToken, parser.carriage_return):
# bCheckAlignment = True
# iPreviousColumn = lColumn[-1]
# iColumn = 0
# if isinstance(lTokens[iToken + 1], parser.whitespace):
# iIndent = len(lTokens[iToken + 1].get_value())
# else:
# iIndent = 0
# continue
#
# if isinstance(oToken, parser.blank_line):
# bCheckAlignment = False
# continue
#
# iColumn += len(oToken.get_value())
#
# if isinstance(oToken, parser.open_parenthesis):
# lColumn.append(iColumn + iPreviousColumn - iIndent)
#
# if isinstance(oToken, parser.close_parenthesis):
# lColumn.pop()
#
# if bCheckAlignment:
# if isinstance(oToken, parser.whitespace):
# if len(oToken.get_value()) != lColumn[-1]:
# dAction = {}
# dAction['line'] = iLine
# dAction['column'] = lColumn[-1]
# dAction['action'] = 'adjust'
# dAction['indent'] = iIndent
# dAction['previous'] = iPreviousColumn
# oViolation = violation.New(iLine, oToi.extract_tokens(iToken, iToken), self.solution)
# oViolation.set_action(dAction)
# self.add_violation(oViolation)
## print(dAction)
# else:
# if lColumn != 0:
# dAction = {}
# dAction['line'] = iLine
# if isinstance(oToken, parser.open_parenthesis):
# dAction['column'] = lColumn[-2]
# else:
# dAction['column'] = lColumn[-1]
# dAction['action'] = 'insert'
# dAction['indent'] = iIndent
# dAction['previous'] = iPreviousColumn
# oViolation = violation.New(iLine, oToi.extract_tokens(iToken, iToken), self.solution)
# oViolation.set_action(dAction)
# self.add_violation(oViolation)
# bCheckAlignment = False
#
# def _fix_violation(self, oViolation):
# lTokens = oViolation.get_tokens()
# dAction = oViolation.get_action()
#
# if dAction['action'] == 'adjust':
# lTokens[0].set_value(' '*dAction['column'])
# else:
# lTokens.insert(0, parser.whitespace(' '*dAction['column']))
#
# oViolation.set_tokens(lTokens)
#def calculate_start_column(oFile, oToi):
# iReturn = oFile.get_column_of_token_index(oToi.get_start_index())
# iReturn += len(oToi.get_tokens()[0].get_value())
# iReturn += 1
# return iReturn
def _check_first_paren_new_line(self, oToi):
iLine, lTokens = utils.get_toi_parameters(oToi)
bSearch = False
for iToken, oToken in enumerate(lTokens):
iLine = utils.increment_line_number(iLine, oToken)
if isinstance(oToken, token.constant_declaration.assignment_operator):
iStart = iToken
bSearch = True
if isinstance(oToken, parser.open_parenthesis) and bSearch:
if utils.find_carriage_return(lTokens[iStart:iToken]) is None:
if self.first_paren_new_line:
sSolution = 'Move parenthesis after assignment to the next line.'
oViolation = violation.New(iLine, oToi.extract_tokens(iStart, iToken), sSolution)
self.add_violation(oViolation)
else:
if not self.first_paren_new_line:
sSolution = 'Move parenthesis to same line as assignment operator.'
oViolation = violation.New(iLine, oToi.extract_tokens(iStart, iToken), sSolution)
self.add_violation(oViolation)
break
def _is_open_paren_after_assignment(oToi):
iLine, lTokens = utils.get_toi_parameters(oToi)
for iToken, oToken in enumerate(lTokens):
if isinstance(oToken, token.constant_declaration.assignment_operator):
if utils.are_next_consecutive_token_types_ignoring_whitespace([token.constant_declaration.assignment_operator, parser.open_parenthesis], iToken, lTokens):
return True
return False

View File

@ -13,3 +13,11 @@ def remove_optional_item(lTokens, oViolation, oInsertToken):
oViolation.set_tokens([])
else:
oViolation.set_tokens([lTokens[0]])
def is_single_line(oToi):
lTokens = oToi.get_tokens()
for oToken in lTokens:
if isinstance(oToken, parser.carriage_return):
return False
return True

View File

@ -0,0 +1,22 @@
architecture rtl of fifo is
constant c_zeros : std_logic_vector(7 downto 0) := (others => '0')
constant c_one : std_logic_vector(7 downto 0) :=
(0 => '1',
(others => '0')
)
constant c_two : std_logic_vector(7 downto 0) :=
(1 => '1',
(others => '0')
)
constant c_stimulus : t_stimulus_array :=
(
(name => "Hold in reset", clk_in => "01", rst_in => "11", cnt_en_in => "00", cnt_out => "00"),
(name => "Not enabled", clk_in => "01", rst_in => "00", cnt_en_in => "00", cnt_out => "00")
);
begin
end architecture rtl;

View File

@ -0,0 +1,18 @@
architecture rtl of fifo is
constant c_zeros : std_logic_vector(7 downto 0) := (others => '0');
constant c_stimulus : t_stimulus_array :=
(
(name => "Hold in reset",
clk_in => "01",
rst_in => "11",
cnt_en_in => "00",
cnt_out => "00"),
(name => "Not enabled", clk_in => "01", rst_in => "00", cnt_en_in => "00", cnt_out => "00")
);
begin
end architecture rtl;

View File

@ -0,0 +1,76 @@
architecture rtl of fifo is
constant c_zeros : std_logic_vector(7 downto 0) := (others => '0');
constant c_one : std_logic_vector(7 downto 0) := (0 => '1', (others => '0'));
constant c_two : std_logic_vector(7 downto 0) := (1 => '1', (others => '0'));
constant c_stimulus : t_stimulus_array := ((name => "Hold in reset", clk_in => "01", rst_in => "11", cnt_en_in => "00", cnt_out => "00"), (name => "Not enabled", clk_in => "01", rst_in => "00", cnt_en_in => "00", cnt_out => "00"));
constant c_stimulus : t_stimulus_array := (
(name => "Hold in reset", clk_in => "01", rst_in => "11", cnt_en_in => "00", cnt_out => "00"), (name => "Not enabled", clk_in => "01", rst_in => "00", cnt_en_in => "00", cnt_out => "00"));
constant c_stimulus : t_stimulus_array :=
((name => "Hold in reset", clk_in => "01", rst_in => "11", cnt_en_in => "00", cnt_out => "00"), (name => "Not enabled", clk_in => "01", rst_in => "00", cnt_en_in => "00", cnt_out => "00"));
constant c_stimulus : t_stimulus_array :=
((name => "Hold in reset", clk_in => "01", rst_in => "11", cnt_en_in => "00", cnt_out => "00"), (name => "Not enabled", clk_in => "01", rst_in => "00", cnt_en_in => "00", cnt_out => "00")
);
constant c_stimulus : t_stimulus_array :=
(
(name => "Hold in reset", clk_in => "01", rst_in => "11", cnt_en_in => "00", cnt_out => "00"),
(name => "Not enabled", clk_in => "01", rst_in => "00", cnt_en_in => "00", cnt_out => "00")
);
constant c_stimulus : t_stimulus_array :=
(
(name => "Hold in reset",
clk_in => "01",
rst_in => "11",
cnt_en_in => "00",
cnt_out => "00"),
(name => "Not enabled",
clk_in => "01",
rst_in => "00",
cnt_en_in => "00",
cnt_out => "00")
);
constant c_stimulus : t_stimulus_array :=
(
(
name => "Hold in reset",
clk_in => "01",
rst_in => "11",
cnt_en_in => "00",
cnt_out => "00"),
(
name => "Not enabled",
clk_in => "01",
rst_in => "00",
cnt_en_in => "00",
cnt_out => "00")
);
constant c_stimulus : t_stimulus_array :=
(
(
name => "Hold in reset",
clk_in => "01",
rst_in => "11",
cnt_en_in => "00",
cnt_out => "00"
),
(
name => "Not enabled",
clk_in => "01",
rst_in => "00",
cnt_en_in => "00",
cnt_out => "00"
)
);
begin
end architecture rtl;

View File

@ -0,0 +1,55 @@
import os
import unittest
from vsg.rules import constant
from vsg import vhdlFile
from vsg.tests import utils
sTestDir = os.path.dirname(__file__)
lFile, eError =vhdlFile.utils.read_vhdlfile(os.path.join(sTestDir,'rule_012a_test_input.vhd'))
#lExpected = []
#lExpected.append('')
#utils.read_file(os.path.join(sTestDir, 'rule_012a_test_input.fixed.vhd'), lExpected)
class test_constant_rule(unittest.TestCase):
def setUp(self):
self.oFile = vhdlFile.vhdlFile(lFile)
self.assertIsNone(eError)
def test_rule_012_first_paren_new_line_true(self):
oRule = constant.rule_012()
oRule.first_paren_new_line = True
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '012')
lExpected = [10]
oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))
def test_rule_012_first_paren_new_line_false(self):
oRule = constant.rule_012()
oRule.first_paren_new_line = False
lExpected = [14, 17, 21, 27, 41, 57]
oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))
# def test_fix_rule_012(self):
# oRule = constant.rule_012()
#
# oRule.fix(self.oFile)
#
# lActual = self.oFile.get_lines()
#
# self.assertEqual(lExpected, lActual)
#
# oRule.analyze(self.oFile)
# self.assertEqual(oRule.violations, [])