From d166aa461b6b06c60c140fc340ad98ab7587bf3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kruszewski?= Date: Wed, 11 Dec 2019 21:32:51 +0100 Subject: [PATCH] Fix case rules for port rules. --- docs/port_rules.rst | 8 +++++-- vsg/rules/port/rule_018.py | 36 ++++++---------------------- vsg/rules/port/rule_019.py | 24 ++++++------------- vsg/tests/vsg_utils/test_utils.py | 19 +++++++++++++++ vsg/utils.py | 39 +++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 48 deletions(-) diff --git a/docs/port_rules.rst b/docs/port_rules.rst index 896a9a14..d5c8cd5a 100644 --- a/docs/port_rules.rst +++ b/docs/port_rules.rst @@ -430,7 +430,9 @@ This rule checks the **port** keyword has proper case. port_018 ######## -This rule checks the port type is lowercase if it is a VHDL keyword. +This rule checks the port type has proper case if it is a VHDL keyword. + +.. NOTE:: The default is lowercase. **Violation** @@ -457,7 +459,9 @@ This rule checks the port type is lowercase if it is a VHDL keyword. port_019 ######## -This rule checks the port direction is lowercase. +This rule checks the port direction has proper case. + +.. NOTE:: The default is lowercase. **Violation** diff --git a/vsg/rules/port/rule_018.py b/vsg/rules/port/rule_018.py index 9b8128d7..e30e8b93 100644 --- a/vsg/rules/port/rule_018.py +++ b/vsg/rules/port/rule_018.py @@ -1,38 +1,16 @@ -from vsg import rule -from vsg import check -from vsg import fix +from vsg.rules import case_rule from vsg import utils -class rule_018(rule.rule): +class rule_018(case_rule): ''' - Port rule 018 checks the port type is lowercase. + Port rule 018 checks the port type has proper case. ''' def __init__(self): - rule.rule.__init__(self, 'port', '018') - self.solution = 'Change port type to lowercase.' - self.phase = 6 + case_rule.__init__(self, 'port', '018', 'isPortDeclaration') + self.solution = 'Change port type name to ' - def analyze(self, oFile): - for iLineNumber, oLine in enumerate(oFile.lines): - if self._is_vsg_off(oLine): - continue - if oLine.isPortDeclaration: - sLine = oLine.line.split(':')[1] - if '(' in sLine: - sLine = sLine.split('(')[0] - if not utils.is_port_mode(sLine.split()[0]): - continue - if utils.is_vhdl_keyword(sLine.split()[1]): - check.is_lowercase(self, sLine.split()[1], iLineNumber) - - def _fix_violations(self, oFile): - for iLineNumber in self.violations: - oLine = oFile.lines[iLineNumber] - sLine = oLine.line.split(':')[1] - if '(' in sLine: - sLine = sLine.split('(')[0] - sWord = sLine.split()[1] - fix.lower_case(oLine, sWord) + def _extract(self, oLine): + return utils.extract_type_name_from_port_vhdl_only(oLine) diff --git a/vsg/rules/port/rule_019.py b/vsg/rules/port/rule_019.py index f4a456cb..133a4721 100644 --- a/vsg/rules/port/rule_019.py +++ b/vsg/rules/port/rule_019.py @@ -1,25 +1,15 @@ -from vsg import rule -from vsg import fix -from vsg import check +from vsg.rules import case_rule -class rule_019(rule.rule): +class rule_019(case_rule): ''' - Port rule 019 checks the port direction is lowercase. + Port rule 019 checks the port direction has proper case. ''' def __init__(self): - rule.rule.__init__(self, 'port', '019') - self.solution = 'Change port direction to lowercase.' - self.phase = 6 + case_rule.__init__(self, 'port', '019', 'isPortDeclaration') + self.solution = 'Change port direction to .' - def _analyze(self, oFile, oLine, iLineNumber): - if oLine.isPortDeclaration: - sLine = oLine.line.split(':')[1] - check.is_lowercase(self, sLine.split()[0], iLineNumber) - - def _fix_violations(self, oFile): - for iLineNumber in self.violations: - sLine = oFile.lines[iLineNumber].line.split(':')[1] - fix.lower_case(oFile.lines[iLineNumber], sLine.split()[0]) + def _extract(self, oLine): + return [oLine.line.split(':')[1].split()[0]] diff --git a/vsg/tests/vsg_utils/test_utils.py b/vsg/tests/vsg_utils/test_utils.py index 5ad2d63f..87188cad 100644 --- a/vsg/tests/vsg_utils/test_utils.py +++ b/vsg/tests/vsg_utils/test_utils.py @@ -282,6 +282,25 @@ class testExtractFunctions(unittest.TestCase): sActual = utils.extract_type_name_vhdl_only(oLine) self.assertEqual(sExpected, sActual) + def test_extract_type_name_from_port(self): + oLine = line.blank_line() + oLine.update_line('PORT( CLK_I : in STD_LOGIC;') + sExpected = ['STD_LOGIC'] + sActual = utils.extract_type_name_from_port(oLine) + self.assertEqual(sExpected, sActual) + + oLine = line.blank_line() + oLine.update_line('d : in std_logic_vector(WIDTH-1 downto 0),') + sExpected = ['std_logic_vector'] + sActual = utils.extract_type_name_from_port(oLine) + self.assertEqual(sExpected, sActual) + + oLine = line.blank_line() + oLine.update_line('d : in std_logic;') + sExpected = ['std_logic'] + sActual = utils.extract_type_name_from_port(oLine) + self.assertEqual(sExpected, sActual) + def test_extract_label(self): oLine = line.blank_line() oLine.update_line(' cp: CPU') diff --git a/vsg/utils.py b/vsg/utils.py index 2bb046eb..99a5c28b 100644 --- a/vsg/utils.py +++ b/vsg/utils.py @@ -637,6 +637,45 @@ def extract_type_name_vhdl_only(oLine): return [] +def extract_type_name_from_port(oLine): + ''' + Returns the name of a type in port declaration. + + Parameters: + + oLine: (line object) + + Returns: (one element list of strings) + ''' + sLine = oLine.line.split(':')[1] + sLine = sLine.replace('(', ' ') + sLine = sLine.replace(')', ' ') + sLine = sLine.replace(';', ' ') + + try: + return [sLine.split()[1]] + except: + return [] + + +def extract_type_name_from_port_vhdl_only(oLine): + ''' + Returns the name of a VHDL only types in port declaration. + + Parameters: + + oLine: (line object) + + Returns: (one element list of strings) + ''' + name = extract_type_name_from_port(oLine) + + if name and is_vhdl_keyword(name[0]): + return name + + return [] + + def extract_type_identifier(oLine): ''' Returns the type identifier from type declaration.