From 0d20c9035052f63fbcfd249fd6d2b59b9447c8a8 Mon Sep 17 00:00:00 2001 From: Jeremiah C Leary Date: Mon, 20 Nov 2017 21:27:13 -0600 Subject: [PATCH] Updates for issue #38: 1) Added fixes for process rules. --- vsg/rule.py | 13 ++ vsg/rules/process.py | 139 ++++++++++++++-- vsg/tests/process/test_fix_rule_process.py | 174 +++++++++++++++++++-- 3 files changed, 302 insertions(+), 24 deletions(-) diff --git a/vsg/rule.py b/vsg/rule.py index 0793e42c..a0cb25af 100644 --- a/vsg/rule.py +++ b/vsg/rule.py @@ -63,6 +63,11 @@ class rule(): # print (self.name + '_' + self.identifier + ': ' + self.solution + '...PASSED') def fix(self, oFile): + self.analyze(oFile) + self._fix_violations(oFile) + self._clear_violations() + + def _fix_violations(self, oFile): return def add_violation(self, lineNumber): @@ -121,6 +126,12 @@ class rule(): def _check_multiline_alignment(self, iColumn, oLine, iLineNumber): if not re.match('\s{' + str(iColumn) + '}\S', oLine.line): self.add_violation(iLineNumber) + self.dFix['violations'][iLineNumber] = {} + self.dFix['violations'][iLineNumber]['column'] = iColumn + + def _fix_multiline_alignment(self, oFile, iLineNumber): + oLine = oFile.lines[iLineNumber] + oLine.update_line(' '*self.dFix['violations'][iLineNumber]['column'] + oLine.line.lstrip()) def _is_uppercase(self, sString, iLineNumber): if not sString == sString.upper(): @@ -187,6 +198,7 @@ class rule(): oLine.line = re.sub(' ' + sKeyword + '$', ' ' + sKeyword.lower(), oLine.line, 1, flags=re.IGNORECASE) oLine.line = re.sub('^' + sKeyword + '$', sKeyword.lower(), oLine.line, 1, flags=re.IGNORECASE) oLine.line = re.sub('^' + sKeyword + ' ', sKeyword.lower() + ' ', oLine.line, 1, flags=re.IGNORECASE) + oLine.line = re.sub(' ' + sKeyword + ';', ' ' + sKeyword.lower() + ';', oLine.line, 1, flags=re.IGNORECASE) oLine.line = re.sub(' ' + sKeyword + '\(', ' ' + sKeyword.lower() + '\(', oLine.line, 1, flags=re.IGNORECASE) oLine.lineLower = oLine.line.lower() @@ -195,6 +207,7 @@ class rule(): oLine.line = re.sub(' ' + sKeyword + '$', ' ' + sKeyword.upper(), oLine.line, 1, flags=re.IGNORECASE) oLine.line = re.sub('^' + sKeyword + '$', sKeyword.upper(), oLine.line, 1, flags=re.IGNORECASE) oLine.line = re.sub('^' + sKeyword + ' ', sKeyword.upper() + ' ', oLine.line, 1, flags=re.IGNORECASE) + oLine.line = re.sub(' ' + sKeyword + ';', ' ' + sKeyword.upper() + ';', oLine.line, 1, flags=re.IGNORECASE) oLine.line = re.sub(' ' + sKeyword + '\(', ' ' + sKeyword.upper() + '\(', oLine.line, 1, flags=re.IGNORECASE) oLine.lineLower = oLine.line.lower() diff --git a/vsg/rules/process.py b/vsg/rules/process.py index c8ddc8ff..f7162eb7 100644 --- a/vsg/rules/process.py +++ b/vsg/rules/process.py @@ -1,5 +1,6 @@ from vsg import rule +from vsg import line import re @@ -44,6 +45,10 @@ class rule_002(process_rule): if not re.match('^\s*.*process\s\(', oLine.lineLower): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'process') + class rule_003(process_rule): '''Process rule 003 checks for the proper indentation at the beginning of the line.''' @@ -78,6 +83,10 @@ class rule_004(process_rule): if oLine.isProcessBegin: self._is_lowercase(self._get_first_word(oLine), iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._lower_case(oFile.lines[iLineNumber], 'begin') + class rule_005(process_rule): '''Process rule 004 checks the "process" keyword is lower case.''' @@ -94,6 +103,10 @@ class rule_005(process_rule): if not re.match('^\s*.*process', oLine.line): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._lower_case(oFile.lines[iLineNumber], 'process') + class rule_006(process_rule): '''Process rule 006 checks for the proper indentation at the beginning of the line.''' @@ -112,6 +125,7 @@ class rule_006(process_rule): def fix(self, oFile): self._fix_indent(oFile) + class rule_007(process_rule): '''Process rule 007 checks for a single space between the "end" and "process" keywords.''' @@ -127,6 +141,10 @@ class rule_007(process_rule): if not re.match('^\s*\S+\s\S', oLine.line): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._enforce_one_space_after_word(oFile.lines[iLineNumber], 'end') + class rule_008(process_rule): '''Process rule 008 checks the "end" keyword is lowercase.''' @@ -142,6 +160,10 @@ class rule_008(process_rule): if oLine.isEndProcess: self._is_lowercase(self._get_first_word(oLine), iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._lower_case(oFile.lines[iLineNumber], 'end') + class rule_009(process_rule): '''Process rule 009 checks the "process" keyword is lowercase on the closing of a process.''' @@ -158,6 +180,10 @@ class rule_009(process_rule): if not re.match('^\s*\w+\s+process', oLine.line): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._lower_case(oFile.lines[iLineNumber], 'process') + class rule_010(process_rule): '''Process rule 010 checks the "begin" keyword is on it's own line.''' @@ -174,7 +200,15 @@ class rule_010(process_rule): if not re.match('^\s*begin', oLine.lineLower): self.add_violation(iLineNumber) - + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + oLine = oFile.lines[iLineNumber] + oLine.update_line(re.sub('begin', ' '*len('begin'), oLine.line, 1, flags=re.IGNORECASE)) + oLine.isProcessBegin = False + oFile.lines.insert(iLineNumber + 1, line.line(' begin')) + oFile.lines[iLineNumber + 1].isProcessBegin = True + + class rule_011(process_rule): '''Process rule 010 checks for a blank line after the "end process" keywords.''' @@ -189,6 +223,10 @@ class rule_011(process_rule): if oLine.isEndProcess: self._is_blank_line_after(oFile, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._insert_blank_line_below(oFile, iLineNumber) + class rule_012(process_rule): '''Process rule 012 checks for the existance of the "is" keyword.''' @@ -205,6 +243,12 @@ class rule_012(process_rule): if not re.match('^.*\)\s*is', oLine.lineLower): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + oLine = oFile.lines[iLineNumber] + iInsertIndex = oLine.line.rfind(')') + oLine.update_line(oLine.line[:iInsertIndex + 1] + ' is ' + oLine.line[iInsertIndex + 1:]) + class rule_013(process_rule): '''Process rule 013 checks the "is" keyword is lowercase.''' @@ -222,6 +266,10 @@ class rule_013(process_rule): if not re.match('^.*\)\s*is', oLine.line): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._lower_case(oFile.lines[iLineNumber], 'is') + class rule_014(process_rule): '''Process rule 014 checks for a single space between the ) and "is" keyword.''' @@ -239,6 +287,10 @@ class rule_014(process_rule): if not re.match('^.*\)\sis', oLine.lineLower): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._enforce_one_space_before_word(oFile.lines[iLineNumber], 'is') + class rule_015(process_rule): '''Process rule 015 checks for a blank line or a comment line above the "process" keyword.''' @@ -255,6 +307,10 @@ class rule_015(process_rule): if not oFile.lines[iLineNumber - 1].isBlank and not oFile.lines[iLineNumber - 1].isComment: self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._insert_blank_line_above(oFile, iLineNumber) + class rule_016(process_rule): '''Process rule 016 checks a process has a label.''' @@ -271,6 +327,10 @@ class rule_016(process_rule): if not re.match('^\s*\S+\s*:', oLine.line): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + '''This requires the user to fix.''' + return + class rule_017(process_rule): '''Process rule 017 checks the process label is uppercase.''' @@ -289,6 +349,11 @@ class rule_017(process_rule): if not lLine[0] == lLine[0].upper(): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + lLine = oFile.lines[iLineNumber].line.split(':') + self._upper_case(oFile.lines[iLineNumber], lLine[0].rstrip().lstrip()) + class rule_018(process_rule): '''Process rule 018 checks the "end process" has a label.''' @@ -305,6 +370,10 @@ class rule_018(process_rule): if not re.match('^\s*\S+\s+\S+\s+\S+', oLine.line): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + '''The user must fix this issue.''' + return + class rule_019(process_rule): '''Process rule 019 checks the "end process" label is uppercase.''' @@ -321,6 +390,11 @@ class rule_019(process_rule): if re.match('^\s*\w+\s+\w+\s+\w+', oLine.line): self._is_uppercase(oLine.line.split()[2], iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + lLine = oFile.lines[iLineNumber].line.split() + self._upper_case(oFile.lines[iLineNumber], lLine[2]) + class rule_020(process_rule): '''Process rule 020 checks the indentation on multiline sensitivity lists.''' @@ -343,20 +417,9 @@ class rule_020(process_rule): else: self._check_multiline_alignment(iAlignmentColumn + 1, oLine, iLineNumber) - -class rule_022(process_rule): - '''Process rule 022 checks for a blank line below the "begin" keyword.''' - - def __init__(self): - process_rule.__init__(self) - self.identifier = '022' - self.solution = 'Add blank line below the "begin" keyword.' - self.phase = 3 - - def analyze(self, oFile): - for iLineNumber, oLine in enumerate(oFile.lines): - if oLine.isProcessBegin: - self._is_blank_line_after(oFile, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.dFix['violations']: + self._fix_multiline_alignment(oFile, iLineNumber) class rule_021(process_rule): @@ -398,6 +461,29 @@ class rule_021(process_rule): if oLine.isSensitivityListEnd: fCheckForBlanks = True + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._remove_blank_lines_above(oFile, iLineNumber) + + +class rule_022(process_rule): + '''Process rule 022 checks for a blank line below the "begin" keyword.''' + + def __init__(self): + process_rule.__init__(self) + self.identifier = '022' + self.solution = 'Add blank line below the "begin" keyword.' + self.phase = 3 + + def analyze(self, oFile): + for iLineNumber, oLine in enumerate(oFile.lines): + if oLine.isProcessBegin: + self._is_blank_line_after(oFile, iLineNumber) + + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._insert_blank_line_below(oFile, iLineNumber) + class rule_023(process_rule): '''Process rule 023 checks for a blank line above the "end process" keywords.''' @@ -413,6 +499,10 @@ class rule_023(process_rule): if oLine.isEndProcess: self._is_blank_line_before(oFile, iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._insert_blank_line_above(oFile, iLineNumber) + class rule_024(process_rule): '''Process rule 024 checks for a single space after the process label and the :.''' @@ -430,6 +520,10 @@ class rule_024(process_rule): if not re.match('^\s*\S+\s:', oLine.line): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._enforce_one_space_before_word(oFile.lines[iLineNumber], ':') + class rule_025(process_rule): '''Process rule 025 checks for a single space after the : and before the "process" keyword.''' @@ -447,6 +541,10 @@ class rule_025(process_rule): if not re.match('^\s*\S+\s*:\s\S', oLine.line): self.add_violation(iLineNumber) + def _fix_violations(self, oFile): + for iLineNumber in self.violations: + self._enforce_one_space_after_word(oFile.lines[iLineNumber], ':') + class rule_026(process_rule): '''Process rule 026 checks for blank lines between the end of the sensitivity list and process declarative lines.''' @@ -487,6 +585,11 @@ class rule_026(process_rule): fCheckForBlanks = True iFailingLineNumber = iLineNumber + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._insert_blank_line_below(oFile, iLineNumber) + oFile.lines[iLineNumber + 1].insideProcess = True + class rule_027(process_rule): '''Process rule 027 checks for blank lines between the process declarative lines and the "begin" keyword.''' @@ -527,5 +630,11 @@ class rule_027(process_rule): if oLine.isSensitivityListEnd: fCheckForBlanks = True + def _fix_violations(self, oFile): + for iLineNumber in self.violations[::-1]: + self._insert_blank_line_above(oFile, iLineNumber) + oFile.lines[iLineNumber].insideProcess = True + + #TODOu # Remove spaces after ( and before ) diff --git a/vsg/tests/process/test_fix_rule_process.py b/vsg/tests/process/test_fix_rule_process.py index 709cfb12..d218fb36 100644 --- a/vsg/tests/process/test_fix_rule_process.py +++ b/vsg/tests/process/test_fix_rule_process.py @@ -9,26 +9,182 @@ from vsg import vhdlFile # Read in test file used for all tests oFile = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'..','process','process_test_input.vhd')) -class testRuleProcessMethods(unittest.TestCase): +class testFixRuleProcessMethods(unittest.TestCase): - - def test_rule_001(self): + def test_fix_rule_001(self): oRule = process.rule_001() + dExpected = [] oRule.fix(oFile) oRule.analyze(oFile) - self.assertEqual(oRule.violations, []) + self.assertEqual(oRule.violations, dExpected) - def test_rule_003(self): + def test_fix_rule_002(self): + oRule = process.rule_002() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_003(self): oRule = process.rule_003() + dExpected = [] oRule.fix(oFile) oRule.analyze(oFile) - self.assertEqual(oRule.violations, []) + self.assertEqual(oRule.violations, dExpected) - def test_rule_006(self): - oRule = process.rule_006() + def test_fix_rule_004(self): + oRule = process.rule_004() + dExpected = [] oRule.fix(oFile) oRule.analyze(oFile) - self.assertEqual(oRule.violations, []) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_005(self): + oRule = process.rule_005() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_006(self): + oRule = process.rule_006() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_007(self): + oRule = process.rule_007() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_008(self): + oRule = process.rule_008() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_009(self): + oRule = process.rule_009() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_010(self): + oRule = process.rule_010() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_011(self): + oRule = process.rule_011() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_012(self): + oRule = process.rule_012() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_013(self): + oRule = process.rule_013() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_014(self): + oRule = process.rule_014() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_015(self): + oRule = process.rule_015() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_017(self): + oRule = process.rule_017() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_019(self): + oRule = process.rule_019() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_020(self): + oRule = process.rule_020() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_021(self): + oRule = process.rule_021() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_022(self): + oRule = process.rule_022() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_023(self): + oRule = process.rule_023() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_024(self): + oRule = process.rule_024() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_025(self): + oRule = process.rule_025() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_026(self): + oRule = process.rule_026() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_fix_rule_027(self): + oRule = process.rule_027() + dExpected = [] + oRule.fix(oFile) + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) if __name__ == '__main__': unittest.main()