Refactored if statement rules and added documentation.

This commit is contained in:
Jeremiah C Leary 2017-12-21 20:00:18 -06:00
parent 40ca454f2f
commit 2c0aa6567d
11 changed files with 476 additions and 109 deletions

View File

@ -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';

View File

@ -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

View File

@ -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)

View File

@ -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):

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -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)