From 2a20fbffcfc7bb9c78e7bf228adcf0019c6efc5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kruszewski?= Date: Thu, 21 Nov 2019 18:24:23 +0100 Subject: [PATCH] Make instantiation rule_011 inherit from case_rule. During this change it turned out that there was a bug in how case was fixed for case rule. It is now fixed, because words that needs fixing are now stored per line, not per file like it was previously. --- vsg/rules/case_rule.py | 18 ++++--- vsg/rules/instantiation/rule_011.py | 28 ++++------- vsg/tests/code_examples/spi_master.fixed.vhd | 4 +- vsg/tests/code_examples/spi_slave.fixed.vhd | 8 ++-- .../instantiation/test_rule_instantiation.py | 2 +- vsg/tests/vsg_utils/test_utils.py | 47 +++++++++++++++++++ vsg/utils.py | 39 +++++++++++++++ 7 files changed, 113 insertions(+), 33 deletions(-) diff --git a/vsg/rules/case_rule.py b/vsg/rules/case_rule.py index 5b8d57da..486425f1 100644 --- a/vsg/rules/case_rule.py +++ b/vsg/rules/case_rule.py @@ -37,7 +37,8 @@ class case_rule(rule.rule): self.solution = None self.sTrigger = sTrigger self.case = 'lower' - self.words_to_fix = set() + # Dictionary with words to be fixed. Key is line number, value is a list of words to fix for given key. + self.words_to_fix = {} @abstractmethod def _extract(self, oLine): @@ -47,19 +48,24 @@ class case_rule(rule.rule): if oLine.__dict__[self.sTrigger]: words = self._extract(oLine) - # ToDo: Raise exception when case differs from lower and upper if self.case == 'lower': check_function = check.is_lowercase - else: + elif self.case == 'upper': check_function = check.is_uppercase + else: + raise Exception(f"case option needs to be 'lower' or 'upper', detected: {self.case}") + words_to_fix = set() for word in words: - if check_function(self, word, iLineNumber) == False: - self.words_to_fix.add(word) + if not check_function(self, word, iLineNumber): + words_to_fix.add(word) + + if words_to_fix: + self.words_to_fix[iLineNumber] = words_to_fix def _fix_violations(self, oFile): for iLineNumber in self.violations: - for word in self.words_to_fix: + for word in self.words_to_fix[iLineNumber]: if self.case == 'lower': fix_function = fix.lower_case else: diff --git a/vsg/rules/instantiation/rule_011.py b/vsg/rules/instantiation/rule_011.py index 4b71c868..925b733d 100644 --- a/vsg/rules/instantiation/rule_011.py +++ b/vsg/rules/instantiation/rule_011.py @@ -1,29 +1,17 @@ -from vsg import rule -from vsg import fix -from vsg import check +from vsg.rules import case_rule from vsg import utils -class rule_011(rule.rule): +class rule_011(case_rule): ''' - Instantiation rule 011 checks the port name is uppercase. + Instantiation rule 011 checks the port name has proper case. ''' def __init__(self): - rule.rule.__init__(self) - self.name = 'instantiation' - self.identifier = '011' - self.solution = 'Uppercase port name.' - self.phase = 6 + case_rule.__init__(self, 'instantiation', '011', 'isInstantiationPortAssignment') + self.case = 'upper' + self.solution = 'Change port name to ' - def _analyze(self, oFile, oLine, iLineNumber): - if oLine.isInstantiationPortAssignment and not oLine.isInstantiationPortKeyword: - sWord = utils.remove_parenthesis_from_word(oLine.line.split()[0]) - check.is_uppercase(self, sWord, iLineNumber) - - def _fix_violations(self, oFile): - for iLineNumber in self.violations: - oLine = oFile.lines[iLineNumber] - sPortName = oLine.line.split('=>')[0].split('(')[0].lstrip().rstrip() - fix.upper_case(oLine, sPortName) + def _extract(self, oLine): + return utils.extract_port_names_from_port_map(oLine) diff --git a/vsg/tests/code_examples/spi_master.fixed.vhd b/vsg/tests/code_examples/spi_master.fixed.vhd index 2e2a11f5..e09c89a4 100644 --- a/vsg/tests/code_examples/spi_master.fixed.vhd +++ b/vsg/tests/code_examples/spi_master.fixed.vhd @@ -193,13 +193,13 @@ entity SPI_MASTER is DI_I : in std_logic_vector(N - 1 downto 0) := (others => 'X'); -- parallel data in (clocked on rising spi_clk after last bit) WREN_I : in std_logic := 'X'; -- user data write enable, starts transmission when interface is idle WR_ACK_O : out std_logic; -- write acknowledge - DO_VALID_O : out std_logic; -- DO_O data valid signal, valid during one spi_clk rising edge. + DO_VALID_O : out std_logic; -- do_o data valid signal, valid during one spi_clk rising edge. DO_O : out std_logic_vector(N - 1 downto 0); -- parallel output (clocked on rising spi_clk after last bit) --- debug ports: can be removed or left unconnected for the application circuit --- SCK_ENA_O : out std_logic; -- debug: internal sck enable signal SCK_ENA_CE_O : out std_logic; -- debug: internal sck clock enable signal DO_TRANSFER_O : out std_logic; -- debug: internal transfer driver - WREN_O : out std_logic; -- debug: internal state of the WREN_I pulse stretcher + WREN_O : out std_logic; -- debug: internal state of the wren_i pulse stretcher RX_BIT_REG_O : out std_logic; -- debug: internal rx bit STATE_DBG_O : out std_logic_vector(3 downto 0); -- debug: internal state register CORE_CLK_O : out std_logic; diff --git a/vsg/tests/code_examples/spi_slave.fixed.vhd b/vsg/tests/code_examples/spi_slave.fixed.vhd index f3b7ad82..a7555c80 100644 --- a/vsg/tests/code_examples/spi_slave.fixed.vhd +++ b/vsg/tests/code_examples/spi_slave.fixed.vhd @@ -155,14 +155,14 @@ entity SPI_SLAVE is SPI_MOSI_I : in std_logic := 'X'; -- spi bus mosi input SPI_MISO_O : out std_logic := 'X'; -- spi bus spi_miso_o output DI_REQ_O : out std_logic; -- preload lookahead data request line - DI_I : in std_logic_vector(N - 1 downto 0) := (others => 'X'); -- parallel load data in (clocked in on rising edge of CLK_I) + DI_I : in std_logic_vector(N - 1 downto 0) := (others => 'X'); -- parallel load data in (clocked in on rising edge of clk_i) WREN_I : in std_logic := 'X'; -- user data write enable WR_ACK_O : out std_logic; -- write acknowledge - DO_VALID_O : out std_logic; -- DO_O data valid strobe, valid during one CLK_I rising edge. - DO_O : out std_logic_vector(N - 1 downto 0); -- parallel output (clocked out on falling CLK_I) + DO_VALID_O : out std_logic; -- do_o data valid strobe, valid during one clk_i rising edge. + DO_O : out std_logic_vector(N - 1 downto 0); -- parallel output (clocked out on falling clk_i) --- debug ports: can be removed for the application circuit --- DO_TRANSFER_O : out std_logic; -- debug: internal transfer driver - WREN_O : out std_logic; -- debug: internal state of the WREN_I pulse stretcher + WREN_O : out std_logic; -- debug: internal state of the wren_i pulse stretcher RX_BIT_NEXT_O : out std_logic; -- debug: internal rx bit STATE_DBG_O : out std_logic_vector(3 downto 0); -- debug: internal state register SH_REG_DBG_O : out std_logic_vector(N - 1 downto 0) -- debug: internal shift register diff --git a/vsg/tests/instantiation/test_rule_instantiation.py b/vsg/tests/instantiation/test_rule_instantiation.py index b6d40e84..a2f452c8 100644 --- a/vsg/tests/instantiation/test_rule_instantiation.py +++ b/vsg/tests/instantiation/test_rule_instantiation.py @@ -126,7 +126,7 @@ class testRuleInstantiationMethods(unittest.TestCase): self.assertEqual(oRule.name, 'instantiation') self.assertEqual(oRule.identifier, '011') - dExpected = [19,20,21,25,26,27,32,33,34,39,41,46,47,48,53,54,55,71,72] + dExpected = [19,20,21,25,26,27,32,33,34,39,41,46,47,48,53,54,55,71,72,95] oRule.analyze(oFilePort) self.assertEqual(oRule.violations, dExpected) diff --git a/vsg/tests/vsg_utils/test_utils.py b/vsg/tests/vsg_utils/test_utils.py index 7b3702ed..55a1d536 100644 --- a/vsg/tests/vsg_utils/test_utils.py +++ b/vsg/tests/vsg_utils/test_utils.py @@ -302,6 +302,53 @@ class testExtractFunctions(unittest.TestCase): sActual = utils.extract_first_keyword(oLine) self.assertEqual(sExpected, sActual) + def test_extract_port_names_from_port_map(self): + oLine = line.blank_line() + oLine.update_line('port map( some_port=>sig,') + sExpected = ['some_port'] + sActual = utils.extract_port_names_from_port_map(oLine) + self.assertEqual(sExpected, sActual) + + oLine = line.blank_line() + oLine.update_line('port map (p => s,') + sExpected = ['p'] + sActual = utils.extract_port_names_from_port_map(oLine) + self.assertEqual(sExpected, sActual) + + oLine = line.blank_line() + oLine.update_line('map(p100=>s100,') + sExpected = ['p100'] + sActual = utils.extract_port_names_from_port_map(oLine) + self.assertEqual(sExpected, sActual) + + oLine = line.blank_line() + oLine.update_line('P1 => S1,') + sExpected = ['P1'] + sActual = utils.extract_port_names_from_port_map(oLine) + self.assertEqual(sExpected, sActual) + + oLine = line.blank_line() + oLine.update_line('port1 => S1,') + sExpected = ['port1'] + sActual = utils.extract_port_names_from_port_map(oLine) + self.assertEqual(sExpected, sActual) + oLine = line.blank_line() + + oLine.update_line('p1 => s1, p2 => s2)') + sExpected = ['p1', 'p2'] + sActual = utils.extract_port_names_from_port_map(oLine) + self.assertEqual(sExpected, sActual) + + oLine.update_line(' port_2 (3 downto 0) => w_port_2') + sExpected = ['port_2'] + sActual = utils.extract_port_names_from_port_map(oLine) + self.assertEqual(sExpected, sActual) + + oLine.update_line("PORT_2 => (others => (a => (others => '0'), b => (others => '1')))") + sExpected = ['PORT_2', 'b'] + sActual = utils.extract_port_names_from_port_map(oLine) + self.assertEqual(sExpected, sActual) + def test_is_number(self): self.assertTrue(utils.is_number('34')) self.assertTrue(utils.is_number('-34')) diff --git a/vsg/utils.py b/vsg/utils.py index 18570328..5995d4b5 100644 --- a/vsg/utils.py +++ b/vsg/utils.py @@ -708,6 +708,45 @@ def extract_port_name(oLine): return [sLine.split()[0]] +def _extract_port_name_from_assignment(sLine): + sLine = sLine.split('=>')[0] + sLine = sLine.strip() + + # Check also if line contains 'map' because some assignments may start with 'port', for example: port1 => sig1. + if sLine.lower().startswith('port') and 'map' in sLine.lower(): + sLine = sLine[5:] + sLine = sLine.strip() + + if sLine.lower().startswith('map'): + sLine = sLine[3:] + sLine = sLine.strip() + + if sLine.lower().startswith('('): + sLine = sLine[1:] + sLine = sLine.strip() + + return sLine.split('(')[0].strip() + + +def extract_port_names_from_port_map(oLine): + ''' + Returns port names from port assignment inside port map. + + Parameters: + + oLine: (line object) + + Returns: (list of strings) + ''' + names = [] + for s in oLine.line.split(','): + name = _extract_port_name_from_assignment(s) + if name != '': + names.append(name) + + return names + + def extract_word(oLine, keyword): ''' Returns is keyword from line.