-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
detect loops that can be do-while loops
- Loading branch information
Showing
7 changed files
with
464 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...der-core/src/main/java/de/firemage/autograder/core/check/general/LoopShouldBeDoWhile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package de.firemage.autograder.core.check.general; | ||
|
||
import de.firemage.autograder.core.LocalizedMessage; | ||
import de.firemage.autograder.core.ProblemType; | ||
import de.firemage.autograder.core.check.ExecutableCheck; | ||
import de.firemage.autograder.core.dynamic.DynamicAnalysis; | ||
import de.firemage.autograder.core.integrated.IntegratedCheck; | ||
import de.firemage.autograder.core.integrated.SpoonUtil; | ||
import de.firemage.autograder.core.integrated.StaticAnalysis; | ||
import spoon.processing.AbstractProcessor; | ||
import spoon.reflect.code.CtStatement; | ||
import spoon.reflect.code.CtWhile; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
import java.util.Map; | ||
|
||
|
||
@ExecutableCheck(reportedProblems = { ProblemType.LOOP_SHOULD_BE_DO_WHILE }) | ||
public class LoopShouldBeDoWhile extends IntegratedCheck { | ||
@Override | ||
protected void check(StaticAnalysis staticAnalysis, DynamicAnalysis dynamicAnalysis) { | ||
staticAnalysis.processWith(new AbstractProcessor<CtWhile>() { | ||
@Override | ||
public void process(CtWhile ctWhile) { | ||
if (ctWhile.isImplicit() || !ctWhile.getPosition().isValidPosition() || ctWhile.getBody() == null) { | ||
return; | ||
} | ||
|
||
// This check detects loops that should be do-while loops, for example: | ||
// | ||
// ``` | ||
// a; | ||
// b; | ||
// c; | ||
// while (condition) { | ||
// a; | ||
// b; | ||
// c; | ||
// } | ||
// ``` | ||
// | ||
// ``` | ||
// a; | ||
// b; | ||
// c; | ||
// while (condition) { | ||
// b; | ||
// c; | ||
// } | ||
// ``` | ||
// | ||
// ``` | ||
// a; | ||
// b; | ||
// c; | ||
// while (condition) { | ||
// a; | ||
// b; | ||
// } | ||
// ``` | ||
|
||
Deque<CtStatement> loopStatements = new ArrayDeque<>(SpoonUtil.getEffectiveStatements(ctWhile.getBody())); | ||
CtStatement currentStatementBeforeLoop = SpoonUtil.getPreviousStatement(ctWhile).orElse(null); | ||
if (currentStatementBeforeLoop == null || loopStatements.isEmpty()) { | ||
return; | ||
} | ||
|
||
while (!loopStatements.isEmpty() && currentStatementBeforeLoop != null) { | ||
CtStatement lastStatement = loopStatements.removeLast(); | ||
|
||
if (!lastStatement.equals(currentStatementBeforeLoop)) { | ||
return; | ||
} | ||
|
||
currentStatementBeforeLoop = SpoonUtil.getPreviousStatement(currentStatementBeforeLoop).orElse(null); | ||
} | ||
|
||
if (loopStatements.isEmpty()) { | ||
addLocalProblem( | ||
ctWhile.getLoopingExpression(), | ||
new LocalizedMessage( | ||
"loop-should-be-do-while", | ||
Map.of("suggestion", """ | ||
%ndo %s while (%s)""".formatted(SpoonUtil.truncatedSuggestion(ctWhile.getBody()), ctWhile.getLoopingExpression())) | ||
), | ||
ProblemType.LOOP_SHOULD_BE_DO_WHILE | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.