|
| 1 | +CLASS y_check_scope_of_variable DEFINITION PUBLIC INHERITING FROM y_check_base CREATE PUBLIC . |
| 2 | + PUBLIC SECTION. |
| 3 | + METHODS constructor . |
| 4 | + |
| 5 | + PROTECTED SECTION. |
| 6 | + METHODS inspect_tokens REDEFINITION. |
| 7 | + |
| 8 | + PRIVATE SECTION. |
| 9 | + METHODS is_isolated IMPORTING strucutre_row TYPE stmnt_stru |
| 10 | + RETURNING VALUE(result) TYPE abap_bool. |
| 11 | + |
| 12 | + METHODS extract_variable_name IMPORTING token TYPE string |
| 13 | + RETURNING VALUE(result) TYPE string. |
| 14 | + |
| 15 | + METHODS get_scope_structure IMPORTING strucutre_row TYPE stmnt_stru |
| 16 | + RETURNING VALUE(result) TYPE sstruc. |
| 17 | + |
| 18 | +ENDCLASS. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +CLASS y_check_scope_of_variable IMPLEMENTATION. |
| 23 | + |
| 24 | + |
| 25 | + METHOD constructor. |
| 26 | + super->constructor( ). |
| 27 | + |
| 28 | + settings-pseudo_comment = '"#EC SCOPE_OF_VAR' ##NO_TEXT. |
| 29 | + settings-disable_threshold_selection = abap_true. |
| 30 | + settings-threshold = 0. |
| 31 | + settings-prio = c_warning. |
| 32 | + settings-documentation = |{ c_docs_path-checks }scope-of-variable.md|. |
| 33 | + |
| 34 | + set_check_message( 'Variable in use out of its scope!' ). |
| 35 | + ENDMETHOD. |
| 36 | + |
| 37 | + |
| 38 | + METHOD inspect_tokens. |
| 39 | + |
| 40 | + DATA(token) = get_token_abs( statement-from ). |
| 41 | + |
| 42 | + IF token NP 'DATA(*)' |
| 43 | + AND token NP 'FIELD-SYMBOLS(*)'. |
| 44 | + RETURN. |
| 45 | + ENDIF. |
| 46 | + |
| 47 | + IF is_isolated( statement-struc ) = abap_false. |
| 48 | + RETURN. |
| 49 | + ENDIF. |
| 50 | + |
| 51 | + DATA(variable) = extract_variable_name( token ). |
| 52 | + DATA(scope) = get_scope_structure( statement-struc ). |
| 53 | + DATA(statement_index) = index. |
| 54 | + |
| 55 | + LOOP AT ref_scan_manager->get_statements( ) ASSIGNING FIELD-SYMBOL(<statement>) |
| 56 | + FROM scope-stmnt_from TO scope-stmnt_to |
| 57 | + WHERE number > statement-number. |
| 58 | + |
| 59 | + statement_index = statement_index + 1. |
| 60 | + |
| 61 | + IF <statement>-struc = statement-struc. |
| 62 | + CONTINUE. |
| 63 | + ENDIF. |
| 64 | + |
| 65 | + LOOP AT ref_scan_manager->get_tokens( ) ASSIGNING FIELD-SYMBOL(<tokens>) |
| 66 | + FROM <statement>-from TO <statement>-to |
| 67 | + WHERE str = variable. |
| 68 | + |
| 69 | + DATA(check_configuration) = detect_check_configuration( <statement> ). |
| 70 | + |
| 71 | + IF check_configuration IS INITIAL. |
| 72 | + RETURN. |
| 73 | + ENDIF. |
| 74 | + |
| 75 | + raise_error( statement_level = <statement>-level |
| 76 | + statement_index = statement_index |
| 77 | + statement_from = <statement>-from |
| 78 | + error_priority = check_configuration-prio ). |
| 79 | + ENDLOOP. |
| 80 | + |
| 81 | + ENDLOOP. |
| 82 | + ENDMETHOD. |
| 83 | + |
| 84 | + |
| 85 | + METHOD is_isolated. |
| 86 | + DATA(structures) = ref_scan_manager->get_structures( ). |
| 87 | + DATA(structure) = structures[ strucutre_row ]. |
| 88 | + |
| 89 | + result = xsdbool( structure-stmnt_type = scan_struc_stmnt_type-if |
| 90 | + OR structure-stmnt_type = scan_struc_stmnt_type-then |
| 91 | + OR structure-stmnt_type = scan_struc_stmnt_type-elseif |
| 92 | + OR structure-stmnt_type = scan_struc_stmnt_type-else |
| 93 | + OR structure-stmnt_type = scan_struc_stmnt_type-do |
| 94 | + OR structure-stmnt_type = scan_struc_stmnt_type-case |
| 95 | + OR structure-stmnt_type = scan_struc_stmnt_type-when |
| 96 | + OR structure-stmnt_type = scan_struc_stmnt_type-loop |
| 97 | + OR structure-stmnt_type = scan_struc_stmnt_type-while ). |
| 98 | + ENDMETHOD. |
| 99 | + |
| 100 | + |
| 101 | + METHOD extract_variable_name. |
| 102 | + result = token. |
| 103 | + REPLACE ALL OCCURRENCES OF 'DATA(' IN result WITH ''. |
| 104 | + REPLACE ALL OCCURRENCES OF 'FIELD-SYMBOLS(' IN result WITH ''. |
| 105 | + REPLACE ALL OCCURRENCES OF ')' IN result WITH ''. |
| 106 | + ENDMETHOD. |
| 107 | + |
| 108 | + |
| 109 | + METHOD get_scope_structure. |
| 110 | + DATA(structures) = ref_scan_manager->get_structures( ). |
| 111 | + DATA(structure) = structures[ strucutre_row ]. |
| 112 | + |
| 113 | + DATA(is_root) = xsdbool( structure-stmnt_type = scan_struc_stmnt_type-form |
| 114 | + OR structure-stmnt_type = scan_struc_stmnt_type-method |
| 115 | + OR structure-stmnt_type = scan_struc_stmnt_type-function |
| 116 | + OR structure-stmnt_type = scan_struc_stmnt_type-module |
| 117 | + OR structure-type = scan_struc_type-event ). |
| 118 | + |
| 119 | + result = COND #( WHEN is_root = abap_true THEN structure |
| 120 | + ELSE get_scope_structure( structure-back ) ). |
| 121 | + ENDMETHOD. |
| 122 | + |
| 123 | +ENDCLASS. |
0 commit comments