-
Notifications
You must be signed in to change notification settings - Fork 104
SONARPY-4544 Count terminal raises as exits #2296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
erwan-leforestier-sonarsource
wants to merge
6
commits into
master
from
fix/sonarpy-4544-treat-terminal-raise-as-exit
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
758c203
SONARPY-4544 Count terminal raises as exits
erwan-leforestier-sonarsource 68e0eae
SONARPY-4544 Ignore unreachable raise blocks when collecting exits
erwan-leforestier-sonarsource c42a1d1
SONARPY-4544 Treat raises through except/finally branches as exits
erwan-leforestier-sonarsource 7f3af0e
SONARPY-4544 Keep endsWithElementKind next to lastElement
erwan-leforestier-sonarsource 0f2f196
SONARPY-4544 Rename misleading self parameter in test fixture
erwan-leforestier-sonarsource b4fb6ef
SONARPY-4544 Apply reachability filter to except/finally predecessors
erwan-leforestier-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -80,17 +80,37 @@ public void initialize(Context context) { | |
|
|
||
| private static List<LatestExecutedBlock> collectLatestExecutedBlocks(ControlFlowGraph cfg) { | ||
| List<LatestExecutedBlock> collectedBlocks = new ArrayList<>(); | ||
| Set<CfgBlock> reachableBlocks = reachableBlocks(cfg.start()); | ||
| for (CfgBlock predecessor : cfg.end().predecessors()) { | ||
| if (!reachableBlocks.contains(predecessor)) { | ||
| continue; | ||
| } | ||
| if (predecessor instanceof PythonCfgBranchingBlock pythonCfgBranchingBlock) { | ||
| collectBranchingBlock(collectedBlocks, pythonCfgBranchingBlock); | ||
| } else if (!endsWithElementKind(predecessor, Kind.RAISE_STMT)) { | ||
| collectBranchingBlock(collectedBlocks, pythonCfgBranchingBlock, reachableBlocks); | ||
| } else { | ||
| collectedBlocks.add(new LatestExecutedBlock(predecessor)); | ||
| } | ||
| } | ||
| return collectedBlocks; | ||
| } | ||
|
|
||
| private static void collectBranchingBlock(List<LatestExecutedBlock> collectedBlocks, PythonCfgBranchingBlock branchingBlock) { | ||
| private static Set<CfgBlock> reachableBlocks(CfgBlock start) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we don't extract this method to existing CfgUtils (and reuse it in unreachableBlocks)? |
||
| Set<CfgBlock> reachable = new HashSet<>(); | ||
| Deque<CfgBlock> blockToVisit = new ArrayDeque<>(); | ||
| blockToVisit.push(start); | ||
| reachable.add(start); | ||
| while (!blockToVisit.isEmpty()) { | ||
| CfgBlock block = blockToVisit.pop(); | ||
| for (CfgBlock successor : block.successors()) { | ||
| if (reachable.add(successor)) { | ||
| blockToVisit.push(successor); | ||
| } | ||
| } | ||
| } | ||
| return reachable; | ||
| } | ||
|
|
||
| private static void collectBranchingBlock(List<LatestExecutedBlock> collectedBlocks, PythonCfgBranchingBlock branchingBlock, Set<CfgBlock> reachableBlocks) { | ||
| Tree branchingTree = branchingBlock.branchingTree(); | ||
| if (branchingTree.is(Kind.TRY_STMT)) { | ||
| TryStatement tryStatement = (TryStatement) branchingTree; | ||
|
|
@@ -100,16 +120,19 @@ private static void collectBranchingBlock(List<LatestExecutedBlock> collectedBlo | |
| } else if (branchingTree.is(Kind.IF_STMT) || branchingTree instanceof Pattern) { | ||
| collectedBlocks.add(new LatestExecutedBlock(branchingBlock)); | ||
| } else { | ||
| collectBlocksHavingReturnBeforeExceptOrFinallyBlock(collectedBlocks, branchingBlock); | ||
| collectBlocksHavingReturnBeforeExceptOrFinallyBlock(collectedBlocks, branchingBlock, reachableBlocks); | ||
| } | ||
| } | ||
|
|
||
| private static void collectBlocksHavingReturnBeforeExceptOrFinallyBlock(List<LatestExecutedBlock> collectedBlocks, PythonCfgBranchingBlock branchingBlock) { | ||
| private static void collectBlocksHavingReturnBeforeExceptOrFinallyBlock(List<LatestExecutedBlock> collectedBlocks, PythonCfgBranchingBlock branchingBlock, Set<CfgBlock> reachableBlocks) { | ||
| if (branchingBlock.branchingTree().is(Kind.EXCEPT_CLAUSE, Kind.FINALLY_CLAUSE)) { | ||
| for (CfgBlock predecessor : branchingBlock.predecessors()) { | ||
| if (!reachableBlocks.contains(predecessor)) { | ||
| continue; | ||
| } | ||
| if (predecessor instanceof PythonCfgBranchingBlock pythonCfgBranchingBlock) { | ||
| collectBlocksHavingReturnBeforeExceptOrFinallyBlock(collectedBlocks, pythonCfgBranchingBlock); | ||
| } else if (endsWithElementKind(predecessor, Kind.RETURN_STMT)) { | ||
| collectBlocksHavingReturnBeforeExceptOrFinallyBlock(collectedBlocks, pythonCfgBranchingBlock, reachableBlocks); | ||
| } else if (endsWithElementKind(predecessor, Kind.RETURN_STMT) || endsWithElementKind(predecessor, Kind.RAISE_STMT)) { | ||
| collectedBlocks.add(new LatestExecutedBlock(predecessor)); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.