diff --git a/docs/component_rules.rst b/docs/component_rules.rst index 549b939f..704e5045 100644 --- a/docs/component_rules.rst +++ b/docs/component_rules.rst @@ -381,3 +381,23 @@ Refer to the entity for port types, port directions and purpose. WR_EN : in std_logic; RD_EN : out std_logic; +component_020 +############# + +This rule checks the comments at the end of the port and generic assignments in component declarations are aligned. +This rule is useful if component_019 is disabled. + +**Violation** + +.. code-block:: vhdl + + WR_EN : in std_logic; -- Enables write to RAM + RD_EN : out std_logic; -- Enable reads from RAM + +**Fix** + +.. code-block:: vhdl + + WR_EN : in std_logic; -- Enables write to RAM + RD_EN : out std_logic; -- Enable reads from RAM + diff --git a/vsg/rules/component/__init__.py b/vsg/rules/component/__init__.py index 41ba64b5..50c545a7 100644 --- a/vsg/rules/component/__init__.py +++ b/vsg/rules/component/__init__.py @@ -18,3 +18,4 @@ from .rule_016 import rule_016 from .rule_017 import rule_017 from .rule_018 import rule_018 from .rule_019 import rule_019 +from .rule_020 import rule_020 diff --git a/vsg/rules/component/rule_020.py b/vsg/rules/component/rule_020.py new file mode 100644 index 00000000..671f1e1d --- /dev/null +++ b/vsg/rules/component/rule_020.py @@ -0,0 +1,49 @@ + +from vsg import rule +from vsg import check +from vsg import fix +from vsg import line + + +class rule_020(rule.rule): + ''' + Entity rule 020 ensures the alignment of comments in a component declaration. + ''' + + def __init__(self): + rule.rule.__init__(self) + self.name = 'component' + self.identifier = '020' + self.solution = 'Inconsistent alignment of comments in component instantiation.' + self.phase = 6 + + def analyze(self, oFile): + lGroup = [] + fGroupFound = False + iStartGroupIndex = None + for iLineNumber, oLine in enumerate(oFile.lines): + fGroupFound, iStartGroupIndex = search_for_group(fGroupFound, oLine, iStartGroupIndex, iLineNumber) + if oLine.isComponentEnd: + lGroup.append(oLine) + fGroupFound = False + check.keyword_alignment(self, iStartGroupIndex, '--', lGroup) + lGroup = [] + iStartGroupIndex = None + store_lines_for_group(fGroupFound, oLine, lGroup) + + def _fix_violations(self, oFile): + fix.keyword_alignment(self, oFile) + + +def search_for_group(fGroupFound, oLine, iStartGroupIndex, iLineNumber): + if not fGroupFound and oLine.insideComponent: + return True, iLineNumber + return fGroupFound, iStartGroupIndex + + +def store_lines_for_group(fGroupFound, oLine, lGroup): + if fGroupFound: + if oLine.hasComment and not oLine.isComment: + lGroup.append(oLine) + else: + lGroup.append(line.line('Removed line')) diff --git a/vsg/tests/component/test_fix_rule_component.py b/vsg/tests/component/test_fix_rule_component.py index 487ecd81..a94f7a93 100644 --- a/vsg/tests/component/test_fix_rule_component.py +++ b/vsg/tests/component/test_fix_rule_component.py @@ -128,3 +128,13 @@ class testFixRuleComponentMethods(unittest.TestCase): self.assertEqual(oFileComment.lines[12].line, ' port_2 : in std_logic;') self.assertEqual(oFileComment.lines[14].line, ' port_4 : out std_logic;') + def test_fix_rule_020(self): + oRule = component.rule_020() + + oFileComment = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'component_comment_test_input.vhd')) + oRule.fix(oFileComment) + oRule.analyze(oFileComment) + self.assertEqual(oRule.violations, []) + self.assertEqual(oFileComment.lines[7].line, ' generic_1 : std_logic := \'0\'; -- This should be removed') + self.assertEqual(oFileComment.lines[12].line, ' port_2 : in std_logic; -- This should be removed') + self.assertEqual(oFileComment.lines[14].line, ' port_4 : out std_logic; -- This should be removed') diff --git a/vsg/tests/component/test_rule_component.py b/vsg/tests/component/test_rule_component.py index 26934756..bc36c676 100644 --- a/vsg/tests/component/test_rule_component.py +++ b/vsg/tests/component/test_rule_component.py @@ -200,3 +200,14 @@ class testRuleComponentMethods(unittest.TestCase): dExpected = [7,12,14] oRule.analyze(oFileComment) self.assertEqual(oRule.violations, dExpected) + + def test_rule_020(self): + oRule = component.rule_020() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'component') + self.assertEqual(oRule.identifier, '020') + + dExpected = ['5-16'] + oFileComment = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'component_comment_test_input.vhd')) + oRule.analyze(oFileComment) + self.assertEqual(oRule.violations, dExpected)