forked from mendrix/SonarDelphi
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extended CatchingGeneralExceptionRule to detect more use cases
- except HandleException(); is now detected (#4) - on Exception do is now also detected
- Loading branch information
Artur Lipinski
committed
Jul 30, 2021
1 parent
81e5e91
commit 13be25a
Showing
4 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/org/sonar/plugins/delphi/pmd/rules/CatchingGeneralExceptionRule.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,65 @@ | ||
package org.sonar.plugins.delphi.pmd.rules; | ||
|
||
import net.sourceforge.pmd.RuleContext; | ||
import org.sonar.plugins.delphi.antlr.DelphiLexer; | ||
import org.sonar.plugins.delphi.antlr.ast.DelphiNode; | ||
import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; | ||
|
||
public class CatchingGeneralExceptionRule extends DelphiRule { | ||
|
||
public CatchingGeneralExceptionRule() | ||
{ | ||
} | ||
|
||
@Override | ||
public void visit(DelphiPMDNode node, RuleContext ctx) { | ||
|
||
|
||
int a = node.getLine(); | ||
// Skip if not an except block | ||
if (node.getType() != DelphiLexer.EXCEPT) | ||
return; | ||
|
||
int childIndexDelta = 1; | ||
DelphiPMDNode parent = (DelphiPMDNode) node.getParent(); | ||
DelphiPMDNode nextNode = (DelphiPMDNode) parent.getChild(node.getChildIndex() + childIndexDelta); | ||
|
||
// ... except HandleException ... | ||
if (nextNode.getType() != DelphiLexer.ON) { | ||
addViolation(ctx, node); | ||
return; | ||
} | ||
|
||
DelphiPMDNode currentNode; | ||
do { | ||
currentNode = (DelphiPMDNode) parent.getChild(node.getChildIndex() + childIndexDelta); | ||
if (currentNode.getType() == DelphiLexer.ON) { | ||
// ... on Exception ... | ||
nextNode = (DelphiPMDNode) parent.getChild(currentNode.getChildIndex() + 1); | ||
if (nextNode.getType() == DelphiLexer.TkIdentifier && nextNode.getText().equals("Exception")) { | ||
addViolation(ctx, nextNode); | ||
return; | ||
} | ||
|
||
// ... on E : Exception ... | ||
nextNode = (DelphiPMDNode) parent.getChild(currentNode.getChildIndex() + 3); | ||
if (nextNode.getType() == DelphiLexer.TkIdentifier && nextNode.getText().equals("Exception")) { | ||
addViolation(ctx, nextNode); | ||
return; | ||
} | ||
} | ||
|
||
childIndexDelta++; | ||
} while (currentNode.getType() != DelphiLexer.END); | ||
|
||
// try | ||
// ... | ||
// except | ||
// on EZeroDivide do HandleZeroDivide; | ||
// on EOverflow do HandleOverflow; | ||
// else | ||
// HandleAllOthers; | ||
// end; | ||
|
||
} | ||
} |
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