From 2c0aa6567ded47dd9fc16cc2d5bd91d1b16be809 Mon Sep 17 00:00:00 2001 From: Jeremiah C Leary Date: Thu, 21 Dec 2017 20:00:18 -0600 Subject: [PATCH] Refactored if statement rules and added documentation. --- docs/if_rules.rst | 376 ++++++++++++++++++++++++- vsg/rules/if_statement/__init__.py | 3 + vsg/rules/if_statement/rule_001.py | 28 +- vsg/rules/if_statement/rule_002.py | 10 +- vsg/rules/if_statement/rule_003.py | 27 +- vsg/rules/if_statement/rule_004.py | 27 +- vsg/rules/if_statement/rule_005.py | 27 +- vsg/rules/if_statement/rule_012.py | 11 + vsg/rules/if_statement/rule_013.py | 11 + vsg/rules/if_statement/rule_014.py | 11 + vsg/tests/if_statement/test_rule_if.py | 54 +++- 11 files changed, 476 insertions(+), 109 deletions(-) create mode 100644 vsg/rules/if_statement/rule_012.py create mode 100644 vsg/rules/if_statement/rule_013.py create mode 100644 vsg/rules/if_statement/rule_014.py diff --git a/docs/if_rules.rst b/docs/if_rules.rst index a85f75ce..ad89e75d 100644 --- a/docs/if_rules.rst +++ b/docs/if_rules.rst @@ -1,46 +1,347 @@ If Rules -------- +if_001 +###### + +This rule checks the indent of the **if** keyword. + +**Violation** + +.. code-block:: vhdl + + if (a = '1') then + b <= '0' + elsif (c = '1') then + d <= '1'; + else + e <= '0'; + end if; + +**Fix** + +.. code-block:: vhdl + + if (a = '1') then + b <= '0' + elsif (c = '1') then + d <= '1'; + else + e <= '0'; + end if; + +if_002 +###### + +This rule checks the bolean expression is enclosed in (). + +**Violation** + +.. code-block:: vhdl + + if a = '1' then + +**Fix** + +.. code-block:: vhdl + + if (a = '1') then + +if_003 +###### + +This rule checks for a single space between the **if** keyword and the (. + +**Violation** + +.. code-block:: vhdl + + if(a = '1') then + + if (a = '1') then + +**Fix** + +.. code-block:: vhdl + + if (a = '1') then + + if (a = '1') then + +if_004 +###### + +This rule checks for a single space between the ) and the **then** keyword. + +**Violation** + +.. code-block:: vhdl + + if (a = '1')then + + if (a = '1') then + +**Fix** + +.. code-block:: vhdl + + if (a = '1') then + + if (a = '1') then + +if_005 +###### + +This rule checks for a single space between the **elsif** keyword and the (. + +**Violation** + +.. code-block:: vhdl + + elsif(c = '1') then + + elsif (c = '1') then + +**Fix** + +.. code-block:: vhdl + + elsif (c = '1') then + + elsif (c = '1') then + +if_006 +###### + +This rule checks for empty lines after the **then** keyword. + +**Violation** + +.. code-block:: vhdl + + if (a = '1') then + + + b <= '0' + +**Fix** + +.. code-block:: vhdl + + if (a = '1') then + b <= '0' + +if_007 +###### + +This rule checks for empty lines before the **elsif** keyword. + +**Violation** + +.. code-block:: vhdl + + b <= '0' + + + + elsif (c = '1') then + +**Fix** + +.. code-block:: vhdl + + b <= '0' + elsif (c = '1') then + +if_008 +###### + +This rule checks for empty lines before the **end if** keywords. + +**Violation** + +.. code-block:: vhdl + + e <= '0'; + + + end if; + +**Fix** + +.. code-block:: vhdl + + e <= '0'; + end if; + +if_009 +###### + +This rule checks the alignment of multiline boolean expressions. + +**Violation** + +.. code-block:: vhdl + + if (a = '0' and b = '1' and + c = '0') then + +**Fix** + +.. code-block:: vhdl + + if (a = '0' and b = '1' and + c = '0') then + +if_010 +###### + +This rule checks for empty lines before the **else** keyword. + +**Violation** + +.. code-block:: vhdl + + d <= '1'; + + + else + +**Fix** + +.. code-block:: vhdl + + d <= '1'; + else + +if_011 +###### + +This rule checks for empty lines after the **else** keyword. + +**Violation** + +.. code-block:: vhdl + + else + + + e <= '0'; + +**Fix** + +.. code-block:: vhdl + + else + e <= '0'; + if_012 ###### -This rule checks for code after the **then** keyword. -Moving sequential statements to their own line makes the code easier to comprehend. +This rule checks the indent of the **elsif** keyword. **Violation** .. code-block:: vhdl - if (a = '1') then c <= '1'; + if (a = '1') then + b <= '0' + elsif (c = '1') then + d <= '1'; + else + e <= '0'; + end if; **Fix** .. code-block:: vhdl - if (a = '1') then - c <= '1'; + if (a = '1') then + b <= '0' + elsif (c = '1') then + d <= '1'; + else + e <= '0'; + end if; if_013 ###### -This rule checks the **else** keyword is on it's own line. +This rule checks the indent of the **else** keyword. **Violation** .. code-block:: vhdl - if (a = '1') then c <= '1'; else c <= '0'; end if; + if (a = '1') then + b <= '0' + elsif (c = '1') then + d <= '1'; + else + e <= '0'; + end if; **Fix** .. code-block:: vhdl - if (a = '1') then c <= '0'; - else c <= '1'; end if; + if (a = '1') then + b <= '0' + elsif (c = '1') then + d <= '1'; + else + e <= '0'; + end if; if_014 ###### +This rule checks the indent of the **end if** keyword. + +**Violation** + +.. code-block:: vhdl + + if (a = '1') then + b <= '0' + elsif (c = '1') then + d <= '1'; + else + e <= '0'; + end if; + +**Fix** + +.. code-block:: vhdl + + if (a = '1') then + b <= '0' + elsif (c = '1') then + d <= '1'; + else + e <= '0'; + end if; + +if_015 +###### + +This rule checks for a single space between the **end if** keywords. + +**Violation** + +.. code-block:: vhdl + + end if; + +**Fix** + +.. code-block:: vhdl + + end if; + +if_020 +###### + This rule checks the **end if** keyword is on it's own line. **Violation** @@ -56,7 +357,43 @@ This rule checks the **end if** keyword is on it's own line. if (a = '1') then c <= '1'; else c <= '0'; end if; -if_017 +if_021 +###### + +This rule checks the **else** keyword is on it's own line. + +**Violation** + +.. code-block:: vhdl + + if (a = '1') then c <= '1'; else c <= '0'; end if; + +**Fix** + +.. code-block:: vhdl + + if (a = '1') then c <= '0'; + else c <= '1'; end if; + +if_022 +###### + +This rule checks for code after the **else** keyword. + +**Violation** + +.. code-block:: vhdl + + if (a = '1') then c <= '1'; else c <= '0'; end if; + +**Fix** + +.. code-block:: vhdl + + if (a = '1') then c <= '1'; else + c <= '0'; end if; + +if_023 ###### This rule checks the **elsif** keyword is on it's own line. @@ -74,3 +411,22 @@ This rule checks the **elsif** keyword is on it's own line. if (a = '1') then c <= '1'; else c <= '0'; elsif (b = '0') then d <= '0'; end if; + +if_024 +###### + +This rule checks for code after the **then** keyword. + +**Violation** + +.. code-block:: vhdl + + if (a = '1') then c <= '1'; + +**Fix** + +.. code-block:: vhdl + + if (a = '1') then + c <= '1'; + diff --git a/vsg/rules/if_statement/__init__.py b/vsg/rules/if_statement/__init__.py index 99d0897a..1db5c962 100644 --- a/vsg/rules/if_statement/__init__.py +++ b/vsg/rules/if_statement/__init__.py @@ -10,6 +10,9 @@ from rule_008 import rule_008 from rule_009 import rule_009 from rule_010 import rule_010 from rule_011 import rule_011 +from rule_012 import rule_012 +from rule_013 import rule_013 +from rule_014 import rule_014 from rule_015 import rule_015 # ordering rules to handle compressed if statements diff --git a/vsg/rules/if_statement/rule_001.py b/vsg/rules/if_statement/rule_001.py index e191ee09..3fe2bc8d 100644 --- a/vsg/rules/if_statement/rule_001.py +++ b/vsg/rules/if_statement/rule_001.py @@ -1,24 +1,16 @@ -from vsg import rule -from vsg import check -from vsg import fix +from vsg.rules import indent_rule -class rule_001(rule.rule): - '''If rule 001 checks for the proper indentation at the beginning of the line.''' +class rule_001(indent_rule): + ''' + If rule 001 checks the indent of the "if" keyword. + ''' def __init__(self): - rule.rule.__init__(self) - self.name = 'if' - self.identifier = '001' - self.solution = 'Ensure proper indentation.' - self.phase = 4 + indent_rule.__init__(self, 'if', '001', 'isIfKeyword') - def analyze(self, oFile): - for iLineNumber, oLine in enumerate(oFile.lines): - if oLine.isIfKeyword or oLine.isElseIfKeyword or oLine.isEndIfKeyword or oLine.isElseKeyword: - check.indent(self, oLine, iLineNumber) - - def _fix_violations(self, oFile): - for iLineNumber in self.violations: - fix.indent(self, oFile.lines[iLineNumber]) +# def analyze(self, oFile): +# for iLineNumber, oLine in enumerate(oFile.lines): +# if oLine.isIfKeyword or oLine.isElseIfKeyword or oLine.isEndIfKeyword or oLine.isElseKeyword: +# check.indent(self, oLine, iLineNumber) diff --git a/vsg/rules/if_statement/rule_002.py b/vsg/rules/if_statement/rule_002.py index a3c3664a..d556fd4c 100644 --- a/vsg/rules/if_statement/rule_002.py +++ b/vsg/rules/if_statement/rule_002.py @@ -5,7 +5,9 @@ import re class rule_002(rule.rule): - '''If rule 002 checks the if boolean expression is enclosed in ()'s.''' + ''' + If rule 002 checks the if boolean expression is enclosed in ()'s. + ''' def __init__(self): rule.rule.__init__(self) @@ -16,11 +18,9 @@ class rule_002(rule.rule): def analyze(self, oFile): for iLineNumber, oLine in enumerate(oFile.lines): - if oLine.isIfKeyword: - if not re.match('^\s*if\s*\(', oLine.lineLower): + if oLine.isIfKeyword and not re.match('^\s*if\s*\(', oLine.lineLower): self.add_violation(iLineNumber) - if oLine.isElseIfKeyword: - if re.match('^\s*elsif\s+\w', oLine.lineLower): + if oLine.isElseIfKeyword and re.match('^\s*elsif\s+\w', oLine.lineLower): self.add_violation(iLineNumber) def _fix_violations(self, oFile): diff --git a/vsg/rules/if_statement/rule_003.py b/vsg/rules/if_statement/rule_003.py index fac4967c..ca6a8845 100644 --- a/vsg/rules/if_statement/rule_003.py +++ b/vsg/rules/if_statement/rule_003.py @@ -1,27 +1,12 @@ -from vsg import rule -from vsg import fix - -import re +from vsg.rules import single_space_after_rule -class rule_003(rule.rule): - '''If rule 003 checks there is a single space between the if keyword and the (.''' +class rule_003(single_space_after_rule): + ''' + If rule 003 checks there is a single space between the if keyword and the (. + ''' def __init__(self): - rule.rule.__init__(self) - self.name = 'if' - self.identifier = '003' + single_space_after_rule.__init__(self, 'if', '003', 'isIfKeyword', 'if') self.solution = 'Ensure only a single space exists between the "if" keyword and the (.' - self.phase = 2 - - def analyze(self, oFile): - for iLineNumber, oLine in enumerate(oFile.lines): - if oLine.isIfKeyword: - if re.match('^\s*if\s*\(', oLine.lineLower): - if not re.match('^\s*if\s\(', oLine.lineLower): - self.add_violation(iLineNumber) - - def _fix_violations(self, oFile): - for iLineNumber in self.violations: - fix.enforce_one_space_after_word(self, oFile.lines[iLineNumber], 'if') diff --git a/vsg/rules/if_statement/rule_004.py b/vsg/rules/if_statement/rule_004.py index e1b5753f..e7bb7db2 100644 --- a/vsg/rules/if_statement/rule_004.py +++ b/vsg/rules/if_statement/rule_004.py @@ -1,27 +1,12 @@ -from vsg import rule -from vsg import fix - -import re +from vsg.rules import single_space_before_rule -class rule_004(rule.rule): - '''If rule 004 checks there is a single space between the ) and "then" keyword.''' +class rule_004(single_space_before_rule): + ''' + If rule 004 checks there is a single space between the ) and "then" keyword. + ''' def __init__(self): - rule.rule.__init__(self) - self.name = 'if' - self.identifier = '004' + single_space_before_rule.__init__(self, 'if', '004', 'isThenKeyword', 'then') self.solution = 'Ensure only a single space exists between the ) and "then" keyword.' - self.phase = 2 - - def analyze(self, oFile): - for iLineNumber, oLine in enumerate(oFile.lines): - if oLine.isThenKeyword: - if re.match('^\s*.*\)\s*then', oLine.lineLower): - if not re.match('^\s*.*\)\sthen', oLine.lineLower): - self.add_violation(iLineNumber) - - def _fix_violations(self, oFile): - for iLineNumber in self.violations: - fix.enforce_one_space_before_word(self, oFile.lines[iLineNumber], 'then') diff --git a/vsg/rules/if_statement/rule_005.py b/vsg/rules/if_statement/rule_005.py index f9163bc8..06fae839 100644 --- a/vsg/rules/if_statement/rule_005.py +++ b/vsg/rules/if_statement/rule_005.py @@ -1,27 +1,12 @@ -from vsg import rule -from vsg import fix - -import re +from vsg.rules import single_space_after_rule -class rule_005(rule.rule): - '''If rule 005 checks there is a single space between the "elsif" keyword and the (.''' +class rule_005(single_space_after_rule): + ''' + If rule 005 checks there is a single space between the "elsif" keyword and the (. + ''' def __init__(self): - rule.rule.__init__(self) - self.name = 'if' - self.identifier = '005' + single_space_after_rule.__init__(self, 'if', '005', 'isElseIfKeyword', 'elsif') self.solution = 'Ensure only a single space exists between the "elsif" keyword and the (.' - self.phase = 2 - - def analyze(self, oFile): - for iLineNumber, oLine in enumerate(oFile.lines): - if oLine.isElseIfKeyword: - if re.match('^\s*elsif\s*\(', oLine.lineLower): - if not re.match('^\s*elsif\s\(', oLine.lineLower): - self.add_violation(iLineNumber) - - def _fix_violations(self, oFile): - for iLineNumber in self.violations: - fix.enforce_one_space_after_word(self, oFile.lines[iLineNumber], 'elsif') diff --git a/vsg/rules/if_statement/rule_012.py b/vsg/rules/if_statement/rule_012.py new file mode 100644 index 00000000..ca1a89cf --- /dev/null +++ b/vsg/rules/if_statement/rule_012.py @@ -0,0 +1,11 @@ + +from vsg.rules import indent_rule + + +class rule_012(indent_rule): + ''' + If rule 012 checks the indent of the "if" keyword. + ''' + + def __init__(self): + indent_rule.__init__(self, 'if', '012', 'isElseIfKeyword') diff --git a/vsg/rules/if_statement/rule_013.py b/vsg/rules/if_statement/rule_013.py new file mode 100644 index 00000000..0810108f --- /dev/null +++ b/vsg/rules/if_statement/rule_013.py @@ -0,0 +1,11 @@ + +from vsg.rules import indent_rule + + +class rule_013(indent_rule): + ''' + If rule 013 checks the indent of the "else" keyword. + ''' + + def __init__(self): + indent_rule.__init__(self, 'if', '013', 'isElseKeyword') diff --git a/vsg/rules/if_statement/rule_014.py b/vsg/rules/if_statement/rule_014.py new file mode 100644 index 00000000..4a4f206e --- /dev/null +++ b/vsg/rules/if_statement/rule_014.py @@ -0,0 +1,11 @@ + +from vsg.rules import indent_rule + + +class rule_014(indent_rule): + ''' + If rule 014 checks the indent of the "end if" keyword. + ''' + + def __init__(self): + indent_rule.__init__(self, 'if', '014', 'isEndIfKeyword') diff --git a/vsg/tests/if_statement/test_rule_if.py b/vsg/tests/if_statement/test_rule_if.py index be9e677b..ca8b93c0 100644 --- a/vsg/tests/if_statement/test_rule_if.py +++ b/vsg/tests/if_statement/test_rule_if.py @@ -16,7 +16,7 @@ class testRuleIfMethods(unittest.TestCase): self.assertTrue(oRule) self.assertEqual(oRule.name, 'if') self.assertEqual(oRule.identifier, '001') - dExpected = [11,13,24,27,33,36,39,60,85,108,109,110] + dExpected = [13,33] oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) @@ -146,21 +146,39 @@ class testRuleIfMethods(unittest.TestCase): oRule.analyze(oFileCase) self.assertEqual(oRule.violations, dExpected) - def test_rule_024(self): - oRule = if_statement.rule_024() + def test_rule_012(self): + oRule = if_statement.rule_012() self.assertTrue(oRule) self.assertEqual(oRule.name, 'if') - self.assertEqual(oRule.identifier, '024') - dExpected = [98,99,105,109] + self.assertEqual(oRule.identifier, '012') + dExpected = [24,108] oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) - def test_rule_021(self): - oRule = if_statement.rule_021() + def test_rule_013(self): + oRule = if_statement.rule_013() self.assertTrue(oRule) self.assertEqual(oRule.name, 'if') - self.assertEqual(oRule.identifier, '021') - dExpected = [105,109] + self.assertEqual(oRule.identifier, '013') + dExpected = [85,109] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_rule_014(self): + oRule = if_statement.rule_014() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'if') + self.assertEqual(oRule.identifier, '014') + dExpected = [11,27,36,39,60,110] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) + + def test_rule_015(self): + oRule = if_statement.rule_015() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'if') + self.assertEqual(oRule.identifier, '015') + dExpected = [117] oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) @@ -173,12 +191,12 @@ class testRuleIfMethods(unittest.TestCase): oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) - def test_rule_015(self): - oRule = if_statement.rule_015() + def test_rule_021(self): + oRule = if_statement.rule_021() self.assertTrue(oRule) self.assertEqual(oRule.name, 'if') - self.assertEqual(oRule.identifier, '015') - dExpected = [117] + self.assertEqual(oRule.identifier, '021') + dExpected = [105,109] oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) @@ -199,3 +217,13 @@ class testRuleIfMethods(unittest.TestCase): dExpected = [108] oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) + + def test_rule_024(self): + oRule = if_statement.rule_024() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'if') + self.assertEqual(oRule.identifier, '024') + dExpected = [98,99,105,109] + oRule.analyze(oFile) + self.assertEqual(oRule.violations, dExpected) +