diff --git a/vsg/rules/architecture/rule_006.py b/vsg/rules/architecture/rule_006.py index e208705a..5d4ee91c 100644 --- a/vsg/rules/architecture/rule_006.py +++ b/vsg/rules/architecture/rule_006.py @@ -27,4 +27,4 @@ class rule_006(rule.rule): for iLineNumber in self.violations: oLine = oFile.lines[iLineNumber] oLine.update_line(re.sub(r'^(\s*architecture\s+\w+\s+of\s+\w+)', r'\1 is', oLine.line, re.IGNORECASE)) - utilities.search_for_and_remove_is_keyword(oFile, iLineNumber) + utilities.search_for_and_remove_keyword(oFile, iLineNumber, 'is') diff --git a/vsg/rules/entity/rule_005.py b/vsg/rules/entity/rule_005.py index e1380e9d..a12b028a 100644 --- a/vsg/rules/entity/rule_005.py +++ b/vsg/rules/entity/rule_005.py @@ -27,4 +27,4 @@ class rule_005(rule.rule): for iLineNumber in self.violations: oLine = oFile.lines[iLineNumber] oLine.update_line(re.sub(r'^(\s*entity\s+\w+)', r'\1 is', oLine.line, re.IGNORECASE)) - utilities.search_for_and_remove_is_keyword(oFile, iLineNumber) + utilities.search_for_and_remove_keyword(oFile, iLineNumber, 'is') diff --git a/vsg/rules/port/__init__.py b/vsg/rules/port/__init__.py index d5c1fdab..7a94e72b 100644 --- a/vsg/rules/port/__init__.py +++ b/vsg/rules/port/__init__.py @@ -19,3 +19,4 @@ from rule_017 import rule_017 from rule_018 import rule_018 from rule_019 import rule_019 from rule_020 import rule_020 +from rule_021 import rule_021 diff --git a/vsg/rules/port/rule_003.py b/vsg/rules/port/rule_003.py index 794867fd..0849b524 100644 --- a/vsg/rules/port/rule_003.py +++ b/vsg/rules/port/rule_003.py @@ -1,7 +1,7 @@ from vsg import rule - -import re +from vsg import check +from vsg import fix class rule_003(rule.rule): @@ -18,10 +18,10 @@ class rule_003(rule.rule): def analyze(self, oFile): for iLineNumber, oLine in enumerate(oFile.lines): - if oLine.isPortKeyword and not re.match('^\s*\S+\s\(', oLine.line): - self.add_violation(iLineNumber) + if oLine.isPortKeyword and '(' in oLine.line: + check.is_single_space_after(self, 'port', oLine, iLineNumber) def _fix_violations(self, oFile): for iLineNumber in self.violations: oLine = oFile.lines[iLineNumber] - oLine.update_line(re.sub(r'(port)\s*\(', r'\1 (', oLine.line, flags=re.IGNORECASE)) + fix.enforce_one_space_after_word(self, oLine, 'port') diff --git a/vsg/rules/port/rule_021.py b/vsg/rules/port/rule_021.py new file mode 100644 index 00000000..f00cb375 --- /dev/null +++ b/vsg/rules/port/rule_021.py @@ -0,0 +1,31 @@ + +from vsg import rule +from vsg import check +from vsg import fix +from vsg import utilities + +import re + + +class rule_021(rule.rule): + ''' + Port rule 021 checks the **port** keyword is on the same line as the (. + ''' + + def __init__(self): + rule.rule.__init__(self) + self.name = 'port' + self.identifier = '021' + self.solution = 'Move the ( to the same line as the "port" keyword.' + self.phase = 2 + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isPortKeyword and '(' not in oLine.lineNoComment: + self.add_violation(iLineNumber) + + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + oLine = oFile.lines[iLineNumber] + oLine.update_line(re.sub('port', 'port (', oLine.line, 1, re.IGNORECASE)) + utilities.search_for_and_remove_keyword(oFile, iLineNumber, '\(') diff --git a/vsg/tests/port/port_test_input.vhd b/vsg/tests/port/port_test_input.vhd index 482377d2..13150c75 100644 --- a/vsg/tests/port/port_test_input.vhd +++ b/vsg/tests/port/port_test_input.vhd @@ -143,3 +143,12 @@ entity ENTITY1 is --- debug ports: can be removed or left unconnected for the application circuit --- ); end entity ENTITY1 + +-- Check if port opening parenthesis is on the same line + +entity ENTITY1 is + port + ( + PORT1 : in std_logic + ); +end entity ENTITY1; diff --git a/vsg/tests/port/test_fix_rule_port.py b/vsg/tests/port/test_fix_rule_port.py index 1e1cb08a..7dd921e2 100644 --- a/vsg/tests/port/test_fix_rule_port.py +++ b/vsg/tests/port/test_fix_rule_port.py @@ -155,3 +155,12 @@ class testFixRulePortMethods(unittest.TestCase): oRule.fix(oFile) oRule.analyze(oFile) self.assertEqual(oRule.violations, []) + + def test_fix_rule_021(self): + oRule = port.rule_021() + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, []) + self.assertEqual(oFile.lines[152].line, ' port (') + self.assertEqual(oFile.lines[153].line, '') + self.assertEqual(oFile.lines[153].isBlank, True) diff --git a/vsg/tests/port/test_rule_port.py b/vsg/tests/port/test_rule_port.py index 02e48e31..3a12bdf1 100644 --- a/vsg/tests/port/test_rule_port.py +++ b/vsg/tests/port/test_rule_port.py @@ -119,7 +119,7 @@ class testRulePortMethods(unittest.TestCase): self.assertEqual(oRule.identifier, '011') oRule.port_direction = 'Prefix' - dExpected = [12,13,14,29,30,31,43,44,45,60,61,62,74,75,76,99,100,101] + dExpected = [12,13,14,29,30,31,43,44,45,60,61,62,74,75,76,99,100,101,152] oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) @@ -135,7 +135,7 @@ class testRulePortMethods(unittest.TestCase): oRule = port.rule_011() oRule.port_direction = 'Suffix' - dExpected = [9,10,11,12,13,14,26,27,28,29,30,31,40,41,42,43,44,45,57,58,59,60,61,62,71,72,73,74,75,76,87,88,89,119,120,121,129,130,131,141,142] + dExpected = [9,10,11,12,13,14,26,27,28,29,30,31,40,41,42,43,44,45,57,58,59,60,61,62,71,72,73,74,75,76,87,88,89,119,120,121,129,130,131,141,142,152] oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) @@ -228,3 +228,13 @@ class testRulePortMethods(unittest.TestCase): dExpected = [141] oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) + + def test_rule_021(self): + oRule = port.rule_021() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'port') + self.assertEqual(oRule.identifier, '021') + + dExpected = [150] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) diff --git a/vsg/tests/vhdlFile/test_vhdlFile.py b/vsg/tests/vhdlFile/test_vhdlFile.py index 34e3ae12..4c761767 100644 --- a/vsg/tests/vhdlFile/test_vhdlFile.py +++ b/vsg/tests/vhdlFile/test_vhdlFile.py @@ -134,7 +134,7 @@ class testVhdlFileMethods(unittest.TestCase): self.assertEqual(lActual, lExpected) def test_insidePortMap_assignment(self): - lExpected = [8,9,10,11,12,13,14,15,25,26,27,28,29,30,31,39,40,41,42,43,44,45,46,56,57,58,59,60,61,62,70,71,72,73,74,75,76,77,86,87,88,89,90,98,99,100,101,102,118,119,120,121,122,128,129,130,131,132,133,140,141,142,143,144] + lExpected = [8,9,10,11,12,13,14,15,25,26,27,28,29,30,31,39,40,41,42,43,44,45,46,56,57,58,59,60,61,62,70,71,72,73,74,75,76,77,86,87,88,89,90,98,99,100,101,102,118,119,120,121,122,128,129,130,131,132,133,140,141,142,143,144,150,151,152,153] # Generic actual list lActual = [] for iIndex, oLine in enumerate(oFilePort.lines): @@ -144,7 +144,7 @@ class testVhdlFileMethods(unittest.TestCase): self.assertEqual(lActual, lExpected) def test_isPortKeyword_assignment(self): - lExpected = [8,25,39,56,70,86,98,118,128,140] + lExpected = [8,25,39,56,70,86,98,118,128,140,150] # Generic actual list lActual = [] for iIndex, oLine in enumerate(oFilePort.lines): @@ -154,7 +154,7 @@ class testVhdlFileMethods(unittest.TestCase): self.assertEqual(lActual, lExpected) def test_isEndPortMap_assignment(self): - lExpected = [15,31,46,62,77,90,102,122,133,144] + lExpected = [15,31,46,62,77,90,102,122,133,144,153] # Generic actual list lActual = [] for iIndex, oLine in enumerate(oFilePort.lines): @@ -164,7 +164,7 @@ class testVhdlFileMethods(unittest.TestCase): self.assertEqual(lActual, lExpected) def test_isPortDeclaration_assignment(self): - lExpected = [9,10,11,12,13,14,26,27,28,29,30,31,40,41,42,43,44,45,57,58,59,60,61,62,71,72,73,74,75,76,87,88,89,99,100,101,119,120,121,129,130,131,141,142] + lExpected = [9,10,11,12,13,14,26,27,28,29,30,31,40,41,42,43,44,45,57,58,59,60,61,62,71,72,73,74,75,76,87,88,89,99,100,101,119,120,121,129,130,131,141,142,152] # Generic actual list lActual = [] for iIndex, oLine in enumerate(oFilePort.lines): diff --git a/vsg/utilities.py b/vsg/utilities.py index d1d02167..6334fc42 100644 --- a/vsg/utilities.py +++ b/vsg/utilities.py @@ -64,20 +64,20 @@ def is_vhdl_keyword(sWord): return False -def clear_is_from_line(oLine): - oLine.update_line(re.sub(r'^(\s*)is', r'\1 ', oLine.line)) +def clear_keyword_from_line(oLine, sKeyword): + oLine.update_line(re.sub(r'^(\s*)' + sKeyword, r'\1 ', oLine.line)) if re.match('^\s*$', oLine.line): oLine.update_line('') oLine.isBlank = True -def search_for_and_remove_is_keyword(oFile, iLineNumber): +def search_for_and_remove_keyword(oFile, iLineNumber, sKeyword): iSearchIndex = iLineNumber while True: iSearchIndex += 1 oLine = oFile.lines[iSearchIndex] - if re.match('^\s*is', oLine.line, re.IGNORECASE): - clear_is_from_line(oLine) + if re.match('^\s*' + sKeyword, oLine.line, re.IGNORECASE): + clear_keyword_from_line(oLine, sKeyword) if not oLine.isBlank: break diff --git a/vsg/vhdlFile/classify/port.py b/vsg/vhdlFile/classify/port.py index 52b97125..5838069f 100644 --- a/vsg/vhdlFile/classify/port.py +++ b/vsg/vhdlFile/classify/port.py @@ -11,6 +11,8 @@ def port(dVars, oLine): else: oLine.indentLevel = 2 dVars['iCurrentIndentLevel'] = 3 + if not '(' in oLine.line: + return if oLine.insidePortMap: if re.match('^\s*\w+.*:', oLine.line) and not oLine.isComment and not oLine.isPortKeyword: