Skip to content

Commit

Permalink
fix UnusedVariablesRule and FinalVariableRule for macro usage (SAP#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgrassau committed Jun 5, 2023
1 parent ccf7945 commit dca6e5a
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ else if (!command.isAbap())
else
executeOnOtherCommand(command, localVariables);

// note down if macros are used in this method; in such a case, rules like the UnusedVariablesRule
// must skip this method
if (command.usesMacro()) {
localVariables.setMethodUsesMacros();
}

} catch (UnexpectedSyntaxBeforeChanges ex) {
ex.addToLog();
skipMethod = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static String getObjectName(String identifier) {

private Rule rule;
private MethodInfo methodInfo;
private boolean methodUsesMacros;

public HashMap<String, VariableInfo> locals = new HashMap<String, VariableInfo>();

Expand Down Expand Up @@ -154,4 +155,12 @@ public VariableInfo addUsage(Token identifier, String name, boolean isAssignment
varInfo.addUsage(identifier, isAssignment, isUsageInSelfAssignment, isCommentedOut, writesToReferencedMemory);
return varInfo;
}

public void setMethodUsesMacros() {
methodUsesMacros = true;
}

public boolean getMethodUsesMacros() {
return methodUsesMacros;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ protected void executeOn(Code code, ClassInfo classInfo, int releaseRestriction)

@Override
protected void executeOn(Code code, Command methodStart, LocalVariables localVariables, int releaseRestriction) throws UnexpectedSyntaxAfterChanges {
// skip this method if macros are used inside the method to avoid making variables FINAL that are used inside
// the macros (note that macro code may be local or 'out of sight')
if (localVariables.getMethodUsesMacros())
return;

for (VariableInfo varInfo : localVariables.getLocalsInDeclarationOrder()) {
if (!varInfo.isDeclaredInline || varInfo.isAssignedAfterDeclaration())
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ protected void executeOn(Code code, ClassInfo classInfo, int releaseRestriction)

@Override
protected void executeOn(Code code, Command methodStart, LocalVariables localVariables, int releaseRestriction) throws UnexpectedSyntaxAfterChanges, IntegrityBrokenException {
// skip this method if macros are used inside the method to avoid deletion of variables that are used inside
// the macros (note that macro code may be local or 'out of sight')
if (localVariables.getMethodUsesMacros())
return;

for (VariableInfo varInfo : localVariables.getLocalsInDeclarationOrder()) {
Command command = varInfo.declarationToken.getParentCommand();
// .isBlocked must be considered only at this point, NOT earlier when usage information is gathered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,40 @@ void testDataUnchangedForPerformUsing() {

testRule();
}

@Test
void testDataUnchangedDueToMacroUsage() {
// expect seemingly unchanged variables to be kept as DATA, because they might be used in the macro

buildSrc(" DATA(lv_any) = 1.");
buildSrc(" DATA(lv_other) = 2.");
buildSrc("");
buildSrc(" any_macro.");
buildSrc("");
buildSrc(" rv_result = lv_any + lv_other.");

copyExpFromSrc();

putAnyMethodAroundSrcAndExp();

testRule();
}

@Test
void testDataUnchangedDueToChainedMacroUsage() {
// expect seemingly unchanged variables to be kept as DATA, because they might be used in the macro

buildSrc(" DATA(lv_any) = 1.");
buildSrc(" DATA(lv_other) = 2.");
buildSrc("");
buildSrc(" other_macro: iv_any_param, iv_other_param.");
buildSrc("");
buildSrc(" rv_result = lv_any + lv_other.");

copyExpFromSrc();

putAnyMethodAroundSrcAndExp();

testRule();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1717,4 +1717,42 @@ void testDefineSectionSkipped() {

testRule();
}

@Test
void testUnchangedDueToMacroUsage() {
// expect seemingly unused variables to be kept, because they might be used in the macro

buildSrc(" DATA: lv_unused TYPE string,");
buildSrc(" lv_another_unused TYPE i.");
buildSrc("");
buildSrc(" FIELD-SYMBOLS: <ls_unused> TYPE ty_s_any_struc.");
buildSrc("");
buildSrc(" any_macro.");

copyExpFromSrc();

putAnyMethodAroundSrcAndExp();

testRule();
}

@Test
void testUnchangedDueToChainedMacroUsage() {
// expect seemingly unused variables to be kept, because they might be used in the macro

buildSrc(" DATA: lv_unused TYPE string,");
buildSrc(" lv_another_unused TYPE i.");
buildSrc("");
buildSrc(" FIELD-SYMBOLS: <ls_unused> TYPE ty_s_any_struc.");
buildSrc("");
buildSrc(" other_macro: 'any_macro_parameter',");
buildSrc(" 'other_macro_parameter'.");

copyExpFromSrc();

putAnyMethodAroundSrcAndExp();

testRule();
}

}

0 comments on commit dca6e5a

Please sign in to comment.