Updates during jcl style validation.

1)  Added rule function_016 to handle return statements in functions
      a)  Updated documentation
      b)  Created tests
      c)  Added rule
  2)  Updated rules to match existing jcl style output
This commit is contained in:
Jeremiah Leary 2020-11-14 17:07:48 -06:00
parent 24e104dd61
commit cccf9b7d7c
19 changed files with 198 additions and 54 deletions

View File

@ -351,3 +351,28 @@ Refer to the section `Configuring Identifier Alignment Rules <configuring_declar
variable var1 : natural;
signal sig1 : natural;
constant c_period : time;
function_016
############
This rule checks the indent of return statements in function bodies.
**Violation**
.. code-block:: vhdl
function func1 return integer is
begin
return 99;
return 99;
end func1;
**Fix**
.. code-block:: vhdl
function func1 return integer is
begin
return 99;
return 99;
end func1;

View File

@ -15,3 +15,5 @@ class rule_003(multiline_alignment_between_tokens):
def __init__(self):
multiline_alignment_between_tokens.__init__(self, 'concurrent', '003', lTokenPairs)
self.phase = 5
self.subphase = 2

View File

@ -14,3 +14,4 @@ from .rule_012 import rule_012
from .rule_013 import rule_013
from .rule_014 import rule_014
from .rule_015 import rule_015
from .rule_016 import rule_016

View File

@ -0,0 +1,16 @@
from vsg.rules import token_indent
from vsg import token
lTokens = []
lTokens.append(token.return_statement.return_keyword)
class rule_016(token_indent):
'''
Checks for the proper indentation of return statements.
'''
def __init__(self):
token_indent.__init__(self, 'function', '016', lTokens)

View File

@ -32,6 +32,7 @@ class move_token_next_to_another_token(rule_item.Rule):
rule_item.Rule.__init__(self, name, identifier)
self.solution = None
self.phase = 1
self.subphase = 2
self.anchor_token = anchor_token
self.token_to_move = token_to_move

View File

@ -46,7 +46,12 @@ class rule_029(rule_item.Rule):
self.oEnd = oEnd
def analyze(self, oFile):
lToi = oFile.get_tokens_bounded_by_token_when_between_tokens(lElsifBoundingTokens[0], lElsifBoundingTokens[1], oStart, oEnd)
lToi = []
aToi = oFile.get_tokens_bounded_by_token_when_between_tokens(lElsifBoundingTokens[0], lElsifBoundingTokens[1], oStart, oEnd)
lToi = utils.combine_two_token_class_lists(lToi, aToi)
aToi = oFile.get_tokens_bounded_by_token_when_between_tokens(lIfBoundingTokens[0], lIfBoundingTokens[1], oStart, oEnd)
lToi = utils.combine_two_token_class_lists(lToi, aToi)
for oToi in lToi:
lTokens = oToi.get_tokens()
bEventFound = False

View File

@ -75,8 +75,11 @@ class remove_excessive_blank_lines_above_line_starting_with_token(rule_item.Rule
def check_if_override_exists(oFile, iLine, lOverrides):
oMyToi = oFile.get_line_preceeding_line(iLine)
lTokens = oMyToi.get_tokens()
for oOverride in lOverrides:
if utils.does_token_type_exist_in_list_of_tokens(oOverride, lTokens):
return True
return False
try:
lTokens = oMyToi.get_tokens()
for oOverride in lOverrides:
if utils.does_token_type_exist_in_list_of_tokens(oOverride, lTokens):
return True
return False
except AttributeError:
return False

View File

@ -72,7 +72,6 @@ class separate_multiple_signal_identifiers_into_individual_statements(rule_item.
for oViolation in self.violations:
lTokens = oViolation.get_tokens()
dAction = oViolation.get_action()
lPreTokens = lTokens[:dAction['start']]
lPostTokens = []
lTemp = lTokens[dAction['end'] + 1:]
@ -85,10 +84,7 @@ class separate_multiple_signal_identifiers_into_individual_statements(rule_item.
for iToken, oToken in enumerate(lTemp):
if iToken == 0:
if isinstance(oToken, parser.whitespace):
oToken.set_value(' ')
lPostTokens.append(oToken)
else:
lPostTokens.append(parser.whitespace(' '))
continue
else:
if isinstance(oToken, parser.whitespace) and isinstance(lTemp[iToken - 1], parser.whitespace):
continue
@ -104,10 +100,9 @@ class separate_multiple_signal_identifiers_into_individual_statements(rule_item.
if isinstance(oToken, token.signal_declaration.identifier):
iIdentifiers += 1
if iIdentifiers == dAction['number']:
lNewTokens.extend(lPreTokens + [oToken] + lPostTokens)
lNewTokens.extend(lPreTokens + [oToken, parser.whitespace(' ')] + lPostTokens)
else:
lNewTokens.extend(lPreTokens + [oToken] + lPostTokens + [parser.carriage_return()])
lNewTokens.extend(lPreTokens + [oToken, parser.whitespace(' ')] + lPostTokens + [parser.carriage_return()])
oViolation.set_tokens(lNewTokens)
oFile.update(self.violations)

View File

@ -0,0 +1,24 @@
architecture RTL of FIFO is
function func1 return integer is
begin
return 99;
end func1;
-- Violations follow
function func1 return integer is
begin
return 99;
end func1;
function func1 return integer is
begin
return 99;
end func1;
begin
end architecture RTL;

View File

@ -0,0 +1,24 @@
architecture RTL of FIFO is
function func1 return integer is
begin
return 99;
end func1;
-- Violations follow
function func1 return integer is
begin
return 99;
end func1;
function func1 return integer is
begin
return 99;
end func1;
begin
end architecture RTL;

View File

@ -0,0 +1,44 @@
import os
import unittest
from vsg.rules import function
from vsg import vhdlFile
from vsg.tests import utils
sTestDir = os.path.dirname(__file__)
lFile = utils.read_vhdlfile(os.path.join(sTestDir,'rule_016_test_input.vhd'))
lExpected = []
lExpected.append('')
utils.read_file(os.path.join(sTestDir, 'rule_016_test_input.fixed.vhd'), lExpected)
class test_function_rule(unittest.TestCase):
def setUp(self):
self.oFile = vhdlFile.vhdlFile(lFile)
def test_rule_016(self):
oRule = function.rule_016()
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'function')
self.assertEqual(oRule.identifier, '016')
lExpected = [13, 18]
oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))
def test_fix_rule_016(self):
oRule = function.rule_016()
oRule.fix(self.oFile)
lActual = self.oFile.get_lines()
self.assertEqual(lExpected, lActual)
oRule.analyze(self.oFile)
self.assertEqual(oRule.violations, [])

View File

@ -1,4 +1,3 @@
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.all;

View File

@ -1,4 +1,3 @@
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.all;

View File

@ -1,4 +1,3 @@
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
@ -22,7 +21,7 @@ architecture BEHAVIORAL of FRAMEBUFFER is
type fbuffer is array (0 to 524288 / 16 - 1) of std_logic_vector(2 downto 0);
impure function initFB return FBuffer is
impure function initFB return fbuffer is
variable temp : fbuffer;
variable i : integer;
begin

View File

@ -1,4 +1,3 @@
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.all;

View File

@ -1,4 +1,3 @@
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.all;

View File

@ -1,4 +1,3 @@
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

View File

@ -31,72 +31,79 @@ oSeverityList = severity.create_list({})
class testCodeExample(unittest.TestCase):
@unittest.skip('Need to wait until all rules have been refactored.')
def test_bresenhamer(self):
oRuleList = rule_list.rule_list(oBresenhamer, oSeverityList)
oRuleList.configure(dConfig)
oRuleList.fix()
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'Bresenhamer.fixed.vhd'), lExpected)
for iLineNumber, sLine in enumerate(lExpected):
self.assertEqual(oBresenhamer.lines[iLineNumber].line, sLine)
@unittest.skip('Need to wait until all rules have been refactored.')
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'Bresenhamer.fixed.vhd'), lExpected)
self.assertEqual(lExpected, oBresenhamer.get_lines())
def test_debouncer(self):
oRuleList = rule_list.rule_list(oDebouncer, oSeverityList)
oRuleList.configure(dConfig)
oRuleList.fix()
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'Debouncer.fixed.vhd'), lExpected)
for iLineNumber, sLine in enumerate(lExpected):
self.assertEqual(oDebouncer.lines[iLineNumber].line, sLine)
@unittest.skip('Need to wait until all rules have been refactored.')
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'Debouncer.fixed.vhd'), lExpected)
self.assertEqual(lExpected, oDebouncer.get_lines())
def test_vga_top(self):
oRuleList = rule_list.rule_list(oVgatop, oSeverityList)
oRuleList.configure(dConfig)
oRuleList.fix()
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'VGA_Top.fixed.vhd'), lExpected)
for iLineNumber, sLine in enumerate(lExpected):
self.assertEqual(oVgatop.lines[iLineNumber].line, sLine)
@unittest.skip('Need to wait until all rules have been refactored.')
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'VGA_Top.fixed.vhd'), lExpected)
self.assertEqual(lExpected, oVgatop.get_lines())
def test_pointer(self):
oRuleList = rule_list.rule_list(oPointer, oSeverityList)
oRuleList.configure(dConfig)
oRuleList.fix()
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'Pointer.fixed.vhd'), lExpected)
for iLineNumber, sLine in enumerate(lExpected):
self.assertEqual(oPointer.lines[iLineNumber].line, sLine)
@unittest.skip('Need to wait until all rules have been refactored.')
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'Pointer.fixed.vhd'), lExpected)
self.assertEqual(lExpected, oPointer.get_lines())
def test_freqdiv(self):
oRuleList = rule_list.rule_list(oFreqDiv, oSeverityList)
oRuleList.configure(dConfig)
oRuleList.fix()
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'FreqDiv.fixed.vhd'), lExpected)
for iLineNumber, sLine in enumerate(lExpected):
self.assertEqual(oFreqDiv.lines[iLineNumber].line, sLine)
@unittest.skip('Need to wait until all rules have been refactored.')
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'FreqDiv.fixed.vhd'), lExpected)
self.assertEqual(lExpected, oFreqDiv.get_lines())
def test_synchronizer(self):
oRuleList = rule_list.rule_list(oSynchronizer, oSeverityList)
oRuleList.configure(dConfig)
oRuleList.fix()
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'Synchronizer.fixed.vhd'), lExpected)
for iLineNumber, sLine in enumerate(lExpected):
self.assertEqual(oSynchronizer.lines[iLineNumber].line, sLine)
@unittest.skip('Need to wait until all rules have been refactored.')
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'Synchronizer.fixed.vhd'), lExpected)
self.assertEqual(lExpected, oSynchronizer.get_lines())
def test_framebuffer(self):
oRuleList = rule_list.rule_list(oFrameBuffer, oSeverityList)
oRuleList.configure(dConfig)
oRuleList.fix()
lExpected = ['']
utils.read_file(os.path.join(os.path.dirname(__file__),'FrameBuffer2.fixed.vhd'), lExpected)
for iLineNumber, sLine in enumerate(lExpected):
self.assertEqual(oFrameBuffer.lines[iLineNumber].line, sLine)
self.assertEqual(lExpected, oFrameBuffer.get_lines())

View File

@ -28,6 +28,9 @@ def set_indent(iCurrentIndent, bLabelFound, oToken):
if isinstance(oToken, token.subprogram_body.begin_keyword):
oToken.set_indent(iIndent - 1)
if isinstance(oToken, token.return_statement.return_keyword):
oToken.set_indent(iIndent)
if isinstance(oToken, token.subprogram_body.end_keyword):
oToken.set_indent(iIndent - 1)