From a484f3cb7b828ae621c55ad69c847d8a671e9b8a Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Sat, 7 Dec 2024 14:59:55 -0600 Subject: [PATCH 1/2] Updating release process documentation. --- docs/processes/release_process.rst | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/processes/release_process.rst b/docs/processes/release_process.rst index 57c265d8..0d4f2ced 100644 --- a/docs/processes/release_process.rst +++ b/docs/processes/release_process.rst @@ -19,10 +19,14 @@ Follow these steps: $ git push --tags origin master -4) Download wheel from CI action +4) Build the Wheel + +.. code-block:: bash + + $ tox -e wheel-py38 5) Submit to PyPI .. code-block:: bash - $ twine upload dist/* + $ twine upload dist/wheel-py38/* From 22601912a92843774475838b5e17793573223e3d Mon Sep 17 00:00:00 2001 From: Jukka Hertzog Date: Mon, 9 Dec 2024 23:20:48 +0000 Subject: [PATCH 2/2] Issue#1234: Consistent capitalization of generic names doesn't work inside generic maps (#1235) * Issue#1234: Copied rule architecture_601 into rule architecture_600 to fix instances within generic maps not being corrected. * Issue#1234: Updated/corrected spi_master.fixed.vhd for smart_tab. * Issue#1228: Not sure why this was working before, but updated extract_generic/port_names_from_entities to protect against entities with no generic or port declarations. * Issue#1234: Refactored common code from architecture_600 and architecture_601 out to shared base class. * Issue#1234: Improved violation reporting. * Issue#1234: Sorted setup in architecture_600 and architecture_601. * Issue#1234: Modified imports to appease isort. * Issue#1234: Updated tests to exercise improvement. * Issue#1234: Removed unused function. --- .../rule_600_test_input.fixed.vhd | 58 ++++ tests/architecture/rule_600_test_input.vhd | 58 ++++ tests/architecture/test_rule_600.py | 5 +- tests/smart_tab/spi_master.fixed.vhd | 28 +- vsg/rules/architecture/rule_600.py | 69 +---- vsg/rules/architecture/rule_601.py | 267 +---------------- vsg/rules/consistent_interface_token_case.py | 281 ++++++++++++++++++ 7 files changed, 426 insertions(+), 340 deletions(-) create mode 100644 vsg/rules/consistent_interface_token_case.py diff --git a/tests/architecture/rule_600_test_input.fixed.vhd b/tests/architecture/rule_600_test_input.fixed.vhd index 05f40ed4..b83ea45e 100644 --- a/tests/architecture/rule_600_test_input.fixed.vhd +++ b/tests/architecture/rule_600_test_input.fixed.vhd @@ -60,3 +60,61 @@ begin output <= large_data(G_DEPTH - 1 downto 0); end architecture rtl; + +entity test is + generic ( + G1 : integer := 0; + g2 : integer := 0 + ); + port ( + P1 : in std_logic; + P2 : out std_logic + ); +end entity test; + +architecture rtl of test is + + component test2 is + generic ( + G1 : integer := 0; + g2 : integer := 0 + ); + port ( + P1 : in std_logic; + p2 : out std_logic + ); + end component test2; + +begin + + test2_i : component test2 + generic map ( + G1 => G1, + g2 => g2 + ) + port map ( + P1 => P1, + P2 => P2 + ); + + test3_lower : entity work.test3 + generic map ( + g1 => G1, + g2 => g2 + ) + port map ( + p1 => p1, + p2 => p2 + ); + + test3_upper : entity work.test3 + generic map ( + G1 => G1, + G2 => g2 + ) + port map ( + p1 => p1, + p2 => p2 + ); + +end architecture; diff --git a/tests/architecture/rule_600_test_input.vhd b/tests/architecture/rule_600_test_input.vhd index 0998fb36..1978f816 100644 --- a/tests/architecture/rule_600_test_input.vhd +++ b/tests/architecture/rule_600_test_input.vhd @@ -60,3 +60,61 @@ begin output <= large_data(G_DEPTH - 1 downto 0); end architecture rtl; + +entity test is + generic ( + G1 : integer := 0; + g2 : integer := 0 + ); + port ( + P1 : in std_logic; + P2 : out std_logic + ); +end entity test; + +architecture rtl of test is + + component test2 is + generic ( + G1 : integer := 0; + g2 : integer := 0 + ); + port ( + P1 : in std_logic; + p2 : out std_logic + ); + end component test2; + +begin + + test2_i : component test2 + generic map ( + G1 => G1, + g2 => G2 + ) + port map ( + P1 => P1, + P2 => P2 + ); + + test3_lower : entity work.test3 + generic map ( + g1 => G1, + g2 => G2 + ) + port map ( + p1 => p1, + p2 => p2 + ); + + test3_upper : entity work.test3 + generic map ( + G1 => g1, + G2 => g2 + ) + port map ( + p1 => p1, + p2 => p2 + ); + +end architecture; diff --git a/tests/architecture/test_rule_600.py b/tests/architecture/test_rule_600.py index 1b36a968..e4e95884 100644 --- a/tests/architecture/test_rule_600.py +++ b/tests/architecture/test_rule_600.py @@ -23,6 +23,7 @@ class test_rule(unittest.TestCase): self.oFile = vhdlFile.vhdlFile(lFile) self.assertIsNone(eError) self.oFile.set_indent_map(dIndentMap) + self.maxDiff = None def test_rule(self): oRule = architecture.rule_600() @@ -30,9 +31,7 @@ class test_rule(unittest.TestCase): self.assertEqual(oRule.name, "architecture") self.assertEqual(oRule.identifier, "600") - lExpected = [18, 20, 25, 27] - lExpected.extend([34, 36, 41, 43]) - lExpected.extend([50, 52, 57, 59]) + lExpected = [18, 20, 25, 27, 34, 36, 41, 43, 50, 52, 57, 59, 93, 93, 103, 103, 112, 112] oRule.analyze(self.oFile) self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations)) diff --git a/tests/smart_tab/spi_master.fixed.vhd b/tests/smart_tab/spi_master.fixed.vhd index 36cd5192..48f12feb 100644 --- a/tests/smart_tab/spi_master.fixed.vhd +++ b/tests/smart_tab/spi_master.fixed.vhd @@ -290,19 +290,19 @@ begin -- GENERICS CONSTRAINTS CHECKING --============================================================================================= -- minimum word width is 8 bits - assert n >= 8 + assert N >= 8 report "Generic parameter 'N' (shift register size) needs to be 8 bits minimum" severity FAILURE; -- minimum prefetch lookahead check - assert prefetch >= 1 + assert PREFETCH >= 1 report "Generic parameter 'PREFETCH' (lookahead count) needs to be 1 minimum" severity FAILURE; -- maximum prefetch lookahead check - assert prefetch <= n - 5 + assert PREFETCH <= N - 5 report "Generic parameter 'PREFETCH' (lookahead count) out of range, needs to be N-5 maximum" severity FAILURE; -- SPI_2X_CLK_DIV clock divider value must not be zero - assert spi_2x_clk_div > 0 + assert SPI_2X_CLK_DIV > 0 report "Generic parameter 'SPI_2X_CLK_DIV' must not be zero" severity FAILURE; @@ -368,12 +368,12 @@ begin --============================================================================================= -- spi clk generator: generate spi_clk from core_clk depending on CPOL - spi_sck_cpol_0_proc : if cpol = '0' generate + spi_sck_cpol_0_proc : if CPOL = '0' generate begin spi_clk <= core_clk; -- for CPOL=0, spi clk has idle LOW end generate spi_sck_cpol_0_proc; - spi_sck_cpol_1_proc : if cpol = '1' generate + spi_sck_cpol_1_proc : if CPOL = '1' generate begin spi_clk <= core_n_clk; -- for CPOL=1, spi clk has idle HIGH end generate spi_sck_cpol_1_proc; @@ -382,12 +382,12 @@ begin -- Sampling clock enable generation: generate 'samp_ce' from 'core_ce' or 'core_n_ce' depending on CPHA -- always sample data at the half-cycle of the fsm update cell - samp_ce_cpha_0_proc : if cpha = '0' generate + samp_ce_cpha_0_proc : if CPHA = '0' generate begin samp_ce <= core_ce; end generate samp_ce_cpha_0_proc; - samp_ce_cpha_1_proc : if cpha = '1' generate + samp_ce_cpha_1_proc : if CPHA = '1' generate begin samp_ce <= core_n_ce; end generate samp_ce_cpha_1_proc; @@ -395,12 +395,12 @@ begin ----------------------------------------------------------------------------------------------- -- FSM clock enable generation: generate 'fsm_ce' from core_ce or core_n_ce depending on CPHA - fsm_ce_cpha_0_proc : if cpha = '0' generate + fsm_ce_cpha_0_proc : if CPHA = '0' generate begin fsm_ce <= core_n_ce; -- for CPHA=0, latch registers at rising edge of negative core clock enable end generate fsm_ce_cpha_0_proc; - fsm_ce_cpha_1_proc : if cpha = '1' generate + fsm_ce_cpha_1_proc : if CPHA = '1' generate begin fsm_ce <= core_ce; -- for CPHA=1, latch registers at rising edge of positive core clock enable end generate fsm_ce_cpha_1_proc; @@ -549,7 +549,7 @@ begin spi_mosi_o <= sh_reg(n - 1); -- shift out tx bit from the MSb di_req_next <= '0'; -- prefetch data request: deassert when shifting data - sh_next(N - 1 downto 1) <= sh_reg(n - 2 downto 0); -- shift inner bits + sh_next(n - 1 downto 1) <= sh_reg(n - 2 downto 0); -- shift inner bits sh_next(0) <= rx_bit_reg; -- shift in rx bit into LSb wr_ack_next <= '0'; -- remove write acknowledge for all but the load stages state_next <= state_reg - 1; -- update next state at each sck pulse @@ -559,7 +559,7 @@ begin spi_mosi_o <= sh_reg(n - 1); -- shift out tx bit from the MSb di_req_next <= '0'; -- prefetch data request: deassert when shifting data do_transfer_next <= '0'; -- reset 'do_valid' transfer signal - sh_next(N - 1 downto 1) <= sh_reg(n - 2 downto 0); -- shift inner bits + sh_next(n - 1 downto 1) <= sh_reg(n - 2 downto 0); -- shift inner bits sh_next(0) <= rx_bit_reg; -- shift in rx bit into LSb wr_ack_next <= '0'; -- remove write acknowledge for all but the load stages state_next <= state_reg - 1; -- update next state at each sck pulse @@ -568,7 +568,7 @@ begin spi_mosi_o <= sh_reg(n - 1); -- shift out tx bit from the MSb di_req_next <= '1'; -- request data in advance to allow for pipeline delays - sh_next(N - 1 downto 1) <= sh_reg(n - 2 downto 0); -- shift inner bits + sh_next(n - 1 downto 1) <= sh_reg(n - 2 downto 0); -- shift inner bits sh_next(0) <= rx_bit_reg; -- shift in rx bit into LSb wr_ack_next <= '0'; -- remove write acknowledge for all but the load stages state_next <= state_reg - 1; -- update next state at each sck pulse @@ -577,7 +577,7 @@ begin spi_mosi_o <= sh_reg(n - 1); -- shift out tx bit from the MSb di_req_next <= '1'; -- request data in advance to allow for pipeline delays - do_buffer_next(N - 1 downto 1) <= sh_reg(n - 2 downto 0); -- shift rx data directly into rx buffer + do_buffer_next(n - 1 downto 1) <= sh_reg(n - 2 downto 0); -- shift rx data directly into rx buffer do_buffer_next(0) <= rx_bit_reg; -- shift last rx bit into rx buffer do_transfer_next <= '1'; -- signal transfer to do_buffer diff --git a/vsg/rules/architecture/rule_600.py b/vsg/rules/architecture/rule_600.py index 3244127f..1076a783 100644 --- a/vsg/rules/architecture/rule_600.py +++ b/vsg/rules/architecture/rule_600.py @@ -1,20 +1,10 @@ # -*- coding: utf-8 -*- -from vsg import parser, token -from vsg.rules import consistent_case_utils as cc_utils, consistent_token_case as Rule - -lTokens = [] -lTokens.append(token.interface_unknown_declaration.identifier) -lTokens.append(token.interface_constant_declaration.identifier) -lTokens.append(token.interface_variable_declaration.identifier) -lTokens.append(token.interface_signal_declaration.identifier) - -lNames = [] -lNames.append(parser.todo) -lNames.append(token.todo.name) +from vsg import token +from vsg.rules import consistent_interface_token_case -class rule_600(Rule): +class rule_600(consistent_interface_token_case.consistent_interface_token_case): """ This rule checks for consistent capitalization of generic names in an architecture body. @@ -60,52 +50,7 @@ class rule_600(Rule): """ def __init__(self): - super().__init__(lTokens, lNames) - - def _get_tokens_of_interest(self, oFile): - lNameTokens = cc_utils.get_all_name_tokens(oFile, self.lNames) - # print(f'lNames = {lNameTokens}') - # for iName in lNameTokens: - # print(f'{iName} = {oFile.lAllObjects[iName].get_value()}') - - lIdentifierss = cc_utils.get_all_identifiers(oFile, self.lTokens) - # print(f'lIdentifiers = {lIdentifierss}') - # for iName in lIdentifierss: - # print(f'{iName} = {oFile.lAllObjects[iName].get_value()}') - - lEntityDicts = cc_utils.get_entity_declaration_indexes(oFile) - lGenericDicts = cc_utils.get_generic_clause_indexes(oFile) - lPortDicts = cc_utils.get_port_clause_indexes(oFile) - - lAllDicts = cc_utils.merge_dict_lists(lEntityDicts, lGenericDicts) - lAllDicts = cc_utils.merge_dict_lists(lAllDicts, lPortDicts) - - lAllDicts = cc_utils.populate_identifiers(lAllDicts, lIdentifierss) - lAllDicts = cc_utils.remove_duplicate_identifiers("entity_declaration", "port_clause", lAllDicts) - - lAllDicts = cc_utils.populate_declarative_part_names(lAllDicts, lNameTokens) - lAllDicts = cc_utils.remove_duplicate_names("entity_declaration", "generic_clause", lAllDicts) - lAllDicts = cc_utils.remove_duplicate_names("entity_declaration", "port_clause", lAllDicts) - - lAllDicts = cc_utils.remove_type("generic_clause", lAllDicts) - lAllDicts = cc_utils.remove_type("port_clause", lAllDicts) - - lArchitectureDicts = cc_utils.merge_dict_lists(cc_utils.get_architecture_indexes(oFile), cc_utils.get_subprogram_body_indexes(oFile)) - lArchitectureDicts = cc_utils.merge_dict_lists(lArchitectureDicts, cc_utils.get_component_declaration_indexes(oFile)) - - lArchitectureDicts = cc_utils.populate_declarative_part_names(lArchitectureDicts, lNameTokens) - lArchitectureDicts = cc_utils.populate_statement_part_names(lArchitectureDicts, lNameTokens) - - lArchitectureDicts = cc_utils.remove_duplicate_names("architecture_body", "subprogram_body", lArchitectureDicts) - lArchitectureDicts = cc_utils.remove_duplicate_names("architecture_body", "component_declaration", lArchitectureDicts) - - lArchitectureDicts = cc_utils.remove_type("subprogram_body", lArchitectureDicts) - lArchitectureDicts = cc_utils.remove_type("component_declaration", lArchitectureDicts) - - lAllDicts = cc_utils.merge_dict_lists(lAllDicts, lArchitectureDicts) - - lAllDicts = cc_utils.add_entity_identifiers_to_architecture_body(lAllDicts) - - lAllDicts = cc_utils.remove_type("entity_declaration", lAllDicts) - - return cc_utils.create_tois(lAllDicts, oFile) + super().__init__() + self.clause_token = token.generic_clause + self.interface_string = "Generic" + self.map_aspect_token = token.generic_map_aspect diff --git a/vsg/rules/architecture/rule_601.py b/vsg/rules/architecture/rule_601.py index 4f34ba9d..f5f19518 100644 --- a/vsg/rules/architecture/rule_601.py +++ b/vsg/rules/architecture/rule_601.py @@ -1,28 +1,10 @@ # -*- coding: utf-8 -*- -from vsg import token, violation -from vsg.rule_group import case - -lPortTokens = [] -lPortTokens.append(token.interface_unknown_declaration.identifier) -lPortTokens.append(token.interface_constant_declaration.identifier) -lPortTokens.append(token.interface_variable_declaration.identifier) -lPortTokens.append(token.interface_signal_declaration.identifier) - -lPairs = [] -lPairs.append([token.concurrent_simple_signal_assignment.target, token.concurrent_simple_signal_assignment.semicolon]) -lPairs.append([token.concurrent_conditional_signal_assignment.target, token.concurrent_conditional_signal_assignment.semicolon]) -lPairs.append([token.constant_declaration.colon, token.constant_declaration.semicolon]) -lPairs.append([token.signal_declaration.colon, token.signal_declaration.semicolon]) -lPairs.append([token.variable_declaration.colon, token.variable_declaration.semicolon]) -lPairs.append([token.process_statement.open_parenthesis, token.process_statement.close_parenthesis]) -lPairs.append([token.process_statement.begin_keyword, token.process_statement.end_keyword]) - -oStartToken = token.architecture_body.entity_name -oEndToken = token.architecture_body.end_keyword +from vsg import token +from vsg.rules import consistent_interface_token_case -class rule_601(case.Rule): +class rule_601(consistent_interface_token_case.consistent_interface_token_case): """ This rule checks for consistent capitalization of port names in an architecture body. @@ -65,243 +47,6 @@ class rule_601(case.Rule): def __init__(self): super().__init__() - self.subphase = 2 - self.configuration_documentation_link = None - - def analyze(self, oFile): - lPorts = extract_port_names_from_entities(oFile) - - if no_ports_detected(lPorts): - return None - - lArchitectures = oFile.get_tokens_bounded_by(oStartToken, oEndToken) - for oArchitecture in lArchitectures: - oArchitecture.name = extract_entity_name(oArchitecture) - lArchitecturePorts = lPorts[oArchitecture.name] - lToi = extract_token_pairs(oArchitecture, lPairs) - lToi.extend(extract_component_instantiation_actual_parts(oArchitecture)) - for oToi in lToi: - validate_port_name_in_token_list(self, lArchitecturePorts, oToi) - - def _fix_violation(self, oViolation): - lTokens = oViolation.get_tokens() - dActions = oViolation.get_action() - lTokens[0].set_value(dActions["value"]) - oViolation.set_tokens(lTokens) - - -def extract_component_instantiation_actual_parts(oArchitecture): - lReturn = [] - - lInstantiations = extract_component_instantiations(oArchitecture) - for oInstantiation in lInstantiations: - lGenericMapAspects = extract_generic_map_aspect(oInstantiation) - lReturn.extend(extract_actual_part_tokens_from_aspects(lGenericMapAspects)) - lPortMapAspects = extract_port_map_aspect(oInstantiation) - lReturn.extend(extract_actual_part_tokens_from_aspects(lPortMapAspects)) - return lReturn - - -def extract_component_instantiations(oArchitecture): - return extract_token_pairs(oArchitecture, [[token.component_instantiation_statement.label_colon, token.component_instantiation_statement.semicolon]]) - - -def extract_generic_map_aspect(oInstantiation): - return extract_token_pairs(oInstantiation, [[token.generic_map_aspect.open_parenthesis, token.generic_map_aspect.close_parenthesis]]) - - -def extract_port_map_aspect(oInstantiation): - return extract_token_pairs(oInstantiation, [[token.port_map_aspect.open_parenthesis, token.port_map_aspect.close_parenthesis]]) - - -def extract_actual_part_tokens_from_aspects(lMapAspect): - lReturn = [] - for oMapAspect in lMapAspect: - lReturn.extend(extract_actual_part_tokens_from_aspect(oMapAspect)) - return lReturn - - -def extract_actual_part_tokens_from_aspect(oMapAspect): - lReturn = [] - for iToken, oToken in enumerate(oMapAspect.get_tokens()): - lReturn.extend(extract_actual_part_token(oMapAspect, iToken, oToken)) - return lReturn - - -def extract_actual_part_token(oMapAspect, iToken, oToken): - lReturn = [] - if is_actual_part_token(oToken): - lReturn.append(oMapAspect.extract_tokens(iToken, iToken)) - return lReturn - - -def is_actual_part_token(oToken): - if isinstance(oToken, token.association_element.actual_part): - return True - return False - - -def no_ports_detected(lPorts): - if len(lPorts) == 0: - return True - return False - - -def validate_port_name_in_token_list(self, lMyPorts, oToi): - lMyPortsLower = [] - dPortMap = {} - for sPort in lMyPorts: - lMyPortsLower.append(sPort.lower()) - dPortMap[sPort.lower()] = sPort - - lTokens = oToi.get_tokens() - for iToken, oToken in enumerate(lTokens): - sToken = oToken.get_value() - if port_case_mismatch(sToken, lMyPorts, lMyPortsLower): - oViolation = create_violation(sToken, iToken, dPortMap, oToi) - self.add_violation(oViolation) - - -def create_violation(sToken, iToken, dPortMap, oToi): - sSolution = "Port case mismatch: Change " + sToken + " to " + dPortMap[sToken.lower()] - oNewToi = oToi.extract_tokens(iToken, iToken) - oViolation = violation.New(oNewToi.get_line_number(), oNewToi, sSolution) - dAction = {} - dAction["value"] = dPortMap[sToken.lower()] - oViolation.set_action(dAction) - return oViolation - - -def skip_code(oToken, bSkip): - bReturn = bSkip - - if not bSkip: - return start_of_skip_region_found(oToken, bReturn) - - return end_of_skip_region_found(oToken, bReturn) - - -def end_of_skip_region_found(oToken, bReturn): - if isinstance(oToken, token.subprogram_body.semicolon): - return False - return True - - -def start_of_skip_region_found(oToken, bReturn): - bReturn = function_keyword_found(oToken, bReturn) - bReturn = procedure_keyword_found(oToken, bReturn) - return bReturn - - -def function_keyword_found(oToken, bReturn): - if isinstance(oToken, token.function_specification.function_keyword): - return True - return bReturn - - -def procedure_keyword_found(oToken, bReturn): - if isinstance(oToken, token.procedure_specification.procedure_keyword): - return True - return bReturn - - -def port_case_mismatch(sToken, lPorts, lPortsLower): - if sToken.lower() in lPortsLower and sToken not in lPorts: - return True - return False - - -def extract_matching_value(sFind, lValues): - sLower = sFind.lower() - for sValue in lValues: - if sLower == sValue.lower(): - return sValue - - -def extract_token_pairs(oArchitecture, lPairs): - lReturn = [] - for lPair in lPairs: - lReturn.extend(extract_token_pair(oArchitecture, lPair)) - return lReturn - - -def extract_token_pair(oArchitecture, lPair): - lReturn = [] - bSkip = False - iStart = None - for iToken, oToken in enumerate(oArchitecture.get_tokens()): - bSkip = skip_code(oToken, bSkip) - if bSkip: - continue - iStart = set_start_index(iToken, oToken, lPair, iStart) - lReturn.extend(extract_tokens_if_end_is_found(oArchitecture, iToken, oToken, lPair, iStart)) - return lReturn - - -def extract_tokens_if_end_is_found(oArchitecture, iToken, oToken, lPair, iStart): - lReturn = [] - if isinstance(oToken, lPair[1]): - lReturn.append(oArchitecture.extract_tokens(iStart, iToken)) - return lReturn - - -def set_start_index(iToken, oToken, lPair, iStart): - if isinstance(oToken, lPair[0]): - return iToken - return iStart - - -def extract_port_names_from_entities(oFile): - lEntities = oFile.get_tokens_bounded_by(token.entity_declaration.identifier, token.entity_declaration.end_keyword) - lPorts = {} - for oEntity in lEntities: - oEntity.name = extract_entity_name(oEntity) - lPorts[oEntity.name] = extract_port_names(oEntity) - return lPorts - - -def extract_entity_name(oToi): - lTokens = oToi.get_tokens() - for oToken in lTokens: - if isinstance(oToken, token.entity_declaration.identifier): - return oToken.get_lower_value() - if isinstance(oToken, token.architecture_body.entity_name): - return oToken.get_lower_value() - - -def extract_port_names(oToi): - lReturn = [] - - bSearch = False - for oToken in oToi.get_tokens(): - bSearch = end_search(oToken, bSearch) - lReturn.extend(extract_port_token(bSearch, oToken, lPortTokens)) - bSearch = start_search(oToken, bSearch) - return lReturn - - -def end_search(oToken, bSearch): - if isinstance(oToken, token.port_clause.close_parenthesis): - return False - return bSearch - - -def extract_port_token(bSearch, oToken, lPortTokens): - lReturn = [] - if bSearch: - if is_token_in_token_type_list(oToken, lPortTokens): - lReturn.append(oToken.get_value()) - return lReturn - - -def start_search(oToken, bSearch): - if isinstance(oToken, token.port_clause.open_parenthesis): - return True - return bSearch - - -def is_token_in_token_type_list(oToken, lTokenTypes): - for oTokenType in lTokenTypes: - if isinstance(oToken, oTokenType): - return True - return False + self.clause_token = token.port_clause + self.interface_string = "Port" + self.map_aspect_token = token.port_map_aspect diff --git a/vsg/rules/consistent_interface_token_case.py b/vsg/rules/consistent_interface_token_case.py new file mode 100644 index 00000000..be7ef1c1 --- /dev/null +++ b/vsg/rules/consistent_interface_token_case.py @@ -0,0 +1,281 @@ +# -*- coding: utf-8 -*- + +from vsg import token, violation +from vsg.rule_group import case + +lInterfaceTokens = [] +lInterfaceTokens.append(token.interface_unknown_declaration.identifier) +lInterfaceTokens.append(token.interface_constant_declaration.identifier) +lInterfaceTokens.append(token.interface_variable_declaration.identifier) +lInterfaceTokens.append(token.interface_signal_declaration.identifier) + +lPairs = [] +lPairs.append([token.concurrent_simple_signal_assignment.target, token.concurrent_simple_signal_assignment.semicolon]) +lPairs.append([token.concurrent_conditional_signal_assignment.target, token.concurrent_conditional_signal_assignment.semicolon]) +lPairs.append([token.constant_declaration.colon, token.constant_declaration.semicolon]) +lPairs.append([token.signal_declaration.colon, token.signal_declaration.semicolon]) +lPairs.append([token.variable_declaration.colon, token.variable_declaration.semicolon]) +lPairs.append([token.process_statement.open_parenthesis, token.process_statement.close_parenthesis]) +lPairs.append([token.process_statement.begin_keyword, token.process_statement.end_keyword]) + +oStartToken = token.architecture_body.entity_name +oEndToken = token.architecture_body.end_keyword + + +class consistent_interface_token_case(case.Rule): + """ + Checks the case for generic and port identifiers. + + Parameters + ---------- + + name : string + The group the rule belongs to. + + identifier : string + unique identifier. Usually in the form of 00N. + + lTokens: list of token type objects + token type to apply the case check against + """ + + def __init__(self): + super().__init__() + self.subphase = 2 + self.configuration_documentation_link = None + self.map_aspect_token = None + self.clause_token = None + self.interface_string = None + + def analyze(self, oFile): + lInterfaces = extract_interface_names_from_entities(oFile, self.clause_token) + + if no_interfaces_detected(lInterfaces): + return None + + lArchitectures = oFile.get_tokens_bounded_by(oStartToken, oEndToken) + for oArchitecture in lArchitectures: + oArchitecture.name = extract_entity_name(oArchitecture) + lArchitectureInterfaces = lInterfaces[oArchitecture.name] + lToi = extract_token_pairs(oArchitecture, lPairs) + lToi.extend(extract_component_instantiation_actual_parts(oArchitecture, self.map_aspect_token)) + for oToi in lToi: + validate_interface_name_in_token_list(self, lArchitectureInterfaces, oToi, self.interface_string) + + def _fix_violation(self, oViolation): + lTokens = oViolation.get_tokens() + dActions = oViolation.get_action() + lTokens[0].set_value(dActions["value"]) + oViolation.set_tokens(lTokens) + + +def extract_component_instantiation_actual_parts(oArchitecture, oMapAspectToken): + lReturn = [] + + lInstantiations = extract_component_instantiations(oArchitecture) + for oInstantiation in lInstantiations: + lGenericMapAspects = extract_generic_map_aspect(oInstantiation) + lReturn.extend(extract_actual_part_tokens_from_aspects(lGenericMapAspects)) + lInterfaceMapAspects = extract_interface_map_aspect(oInstantiation, oMapAspectToken) + lReturn.extend(extract_actual_part_tokens_from_aspects(lInterfaceMapAspects)) + return lReturn + + +def extract_component_instantiations(oArchitecture): + return extract_token_pairs(oArchitecture, [[token.component_instantiation_statement.label_colon, token.component_instantiation_statement.semicolon]]) + + +def extract_generic_map_aspect(oInstantiation): + return extract_token_pairs(oInstantiation, [[token.generic_map_aspect.open_parenthesis, token.generic_map_aspect.close_parenthesis]]) + + +def extract_interface_map_aspect(oInstantiation, oMapAspectToken): + return extract_token_pairs(oInstantiation, [[oMapAspectToken.open_parenthesis, oMapAspectToken.close_parenthesis]]) + + +def extract_actual_part_tokens_from_aspects(lMapAspect): + lReturn = [] + for oMapAspect in lMapAspect: + lReturn.extend(extract_actual_part_tokens_from_aspect(oMapAspect)) + return lReturn + + +def extract_actual_part_tokens_from_aspect(oMapAspect): + lReturn = [] + for iToken, oToken in enumerate(oMapAspect.get_tokens()): + lReturn.extend(extract_actual_part_token(oMapAspect, iToken, oToken)) + return lReturn + + +def extract_actual_part_token(oMapAspect, iToken, oToken): + lReturn = [] + if is_actual_part_token(oToken): + lReturn.append(oMapAspect.extract_tokens(iToken, iToken)) + return lReturn + + +def is_actual_part_token(oToken): + if isinstance(oToken, token.association_element.actual_part): + return True + return False + + +def no_interfaces_detected(lInterfaces): + if len(lInterfaces) == 0: + return True + return False + + +def validate_interface_name_in_token_list(self, lMyInterfaces, oToi, sInterface): + lMyInterfacesLower = [] + dInterfaceMap = {} + for sInterfaceName in lMyInterfaces: + lMyInterfacesLower.append(sInterfaceName.lower()) + dInterfaceMap[sInterfaceName.lower()] = sInterfaceName + + lTokens = oToi.get_tokens() + for iToken, oToken in enumerate(lTokens): + sToken = oToken.get_value() + if interface_case_mismatch(sToken, lMyInterfaces, lMyInterfacesLower): + oViolation = create_violation(sInterface, sToken, iToken, dInterfaceMap, oToi) + self.add_violation(oViolation) + + +def create_violation(sInterface, sToken, iToken, dInterfaceMap, oToi): + sSolution = sInterface + " case mismatch: Change " + sToken + " to " + dInterfaceMap[sToken.lower()] + oNewToi = oToi.extract_tokens(iToken, iToken) + oViolation = violation.New(oNewToi.get_line_number(), oNewToi, sSolution) + dAction = {} + dAction["value"] = dInterfaceMap[sToken.lower()] + oViolation.set_action(dAction) + return oViolation + + +def skip_code(oToken, bSkip): + bReturn = bSkip + + if not bSkip: + return start_of_skip_region_found(oToken, bReturn) + + return end_of_skip_region_found(oToken, bReturn) + + +def end_of_skip_region_found(oToken, bReturn): + if isinstance(oToken, token.subprogram_body.semicolon): + return False + return True + + +def start_of_skip_region_found(oToken, bReturn): + bReturn = function_keyword_found(oToken, bReturn) + bReturn = procedure_keyword_found(oToken, bReturn) + return bReturn + + +def function_keyword_found(oToken, bReturn): + if isinstance(oToken, token.function_specification.function_keyword): + return True + return bReturn + + +def procedure_keyword_found(oToken, bReturn): + if isinstance(oToken, token.procedure_specification.procedure_keyword): + return True + return bReturn + + +def interface_case_mismatch(sToken, lInterfaces, lInterfacesLower): + if sToken.lower() in lInterfacesLower and sToken not in lInterfaces: + return True + return False + + +def extract_token_pairs(oArchitecture, lPairs): + lReturn = [] + for lPair in lPairs: + lReturn.extend(extract_token_pair(oArchitecture, lPair)) + return lReturn + + +def extract_token_pair(oArchitecture, lPair): + lReturn = [] + bSkip = False + iStart = None + for iToken, oToken in enumerate(oArchitecture.get_tokens()): + bSkip = skip_code(oToken, bSkip) + if bSkip: + continue + iStart = set_start_index(iToken, oToken, lPair, iStart) + lReturn.extend(extract_tokens_if_end_is_found(oArchitecture, iToken, oToken, lPair, iStart)) + return lReturn + + +def extract_tokens_if_end_is_found(oArchitecture, iToken, oToken, lPair, iStart): + lReturn = [] + if isinstance(oToken, lPair[1]): + lReturn.append(oArchitecture.extract_tokens(iStart, iToken)) + return lReturn + + +def set_start_index(iToken, oToken, lPair, iStart): + if isinstance(oToken, lPair[0]): + return iToken + return iStart + + +def extract_interface_names_from_entities(oFile, oClauseToken): + lEntities = oFile.get_tokens_bounded_by(token.entity_declaration.identifier, token.entity_declaration.end_keyword) + lInterfaces = {} + for oEntity in lEntities: + oEntity.name = extract_entity_name(oEntity) + lInterfaceNames = extract_interface_names(oEntity, oClauseToken) + if lInterfaceNames != []: + lInterfaces[oEntity.name] = lInterfaceNames + return lInterfaces + + +def extract_entity_name(oToi): + lTokens = oToi.get_tokens() + for oToken in lTokens: + if isinstance(oToken, token.entity_declaration.identifier): + return oToken.get_lower_value() + if isinstance(oToken, token.architecture_body.entity_name): + return oToken.get_lower_value() + + +def extract_interface_names(oToi, oClauseToken): + lReturn = [] + + bSearch = False + for oToken in oToi.get_tokens(): + bSearch = end_search(oToken, bSearch, oClauseToken) + lReturn.extend(extract_interface_token(bSearch, oToken, lInterfaceTokens)) + bSearch = start_search(oToken, bSearch, oClauseToken) + return lReturn + + +def end_search(oToken, bSearch, oClauseToken): + if isinstance(oToken, oClauseToken.close_parenthesis): + return False + return bSearch + + +def extract_interface_token(bSearch, oToken, lInterfaceTokens): + lReturn = [] + if bSearch: + if is_token_in_token_type_list(oToken, lInterfaceTokens): + lReturn.append(oToken.get_value()) + return lReturn + + +def start_search(oToken, bSearch, oClauseToken): + if isinstance(oToken, oClauseToken.open_parenthesis): + return True + return bSearch + + +def is_token_in_token_type_list(oToken, lTokenTypes): + for oTokenType in lTokenTypes: + if isinstance(oToken, oTokenType): + return True + return False