-
Notifications
You must be signed in to change notification settings - Fork 2k
Shared CFG: add defaulted getWhileElse/getForeachElse to AstSig #21931
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -211,6 +211,31 @@ signature module AstSig<LocationSig Location> { | |||||
| */ | ||||||
| default AstNode getTryElse(TryStmt try) { none() } | ||||||
|
|
||||||
| /** | ||||||
| * Gets the `else` block of this `while` loop statement, if any. | ||||||
| * | ||||||
| * Only some languages (e.g. Python) support `while-else` constructs. | ||||||
| */ | ||||||
| default AstNode getWhileElse(WhileStmt loop) { none() } | ||||||
|
Contributor
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. Let's stick to just one new predicate
Suggested change
The semantics of a loop-else block is mainly related to whether the loop exits via its condition or a break, so it's not really tied to the specifics of while vs. foreach. |
||||||
|
|
||||||
| /** | ||||||
| * Gets the `else` block of this `foreach` loop statement, if any. | ||||||
|
Contributor
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.
Suggested change
|
||||||
| * | ||||||
| * Only some languages (e.g. Python) support `for-else` constructs. | ||||||
| */ | ||||||
| default AstNode getForeachElse(ForeachStmt loop) { none() } | ||||||
|
|
||||||
| /** | ||||||
| * Gets the type expression of this catch clause, if any. | ||||||
|
Contributor
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.
Suggested change
|
||||||
| * | ||||||
| * In Python, the catch type is a runtime-evaluated expression | ||||||
| * (e.g. `except SomeException:` where `SomeException` is an | ||||||
| * arbitrary expression). For languages where the catch type is | ||||||
| * statically resolved, this defaults to `none()` and no CFG node | ||||||
| * is created. | ||||||
| */ | ||||||
| default Expr getCatchType(CatchClause catch) { none() } | ||||||
|
Contributor
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. This is unrelated to the PR description - was this meant to go in a different PR?
Contributor
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. Also, unless absolutely necessary, I don't think this should be a point of parameterisation - rather, I think we ought to be able to come up with a CFG for catch that solves the needs of all languages. We should probably discuss this offline and put a fix in a different PR when we agree on the solution. |
||||||
|
|
||||||
| /** A catch clause in a try statement. */ | ||||||
| class CatchClause extends AstNode { | ||||||
| /** Gets the variable declared by this catch clause. */ | ||||||
|
|
@@ -1549,19 +1574,32 @@ module Make0<LocationSig Location, AstSig<Location> Ast> { | |||||
| n2.isBefore(loopstmt.getBody()) | ||||||
| or | ||||||
| n1.isAfterFalse(cond) and | ||||||
| n2.isAfter(loopstmt) | ||||||
| ( | ||||||
| n2.isBefore(getWhileElse(loopstmt)) | ||||||
| or | ||||||
| not exists(getWhileElse(loopstmt)) and n2.isAfter(loopstmt) | ||||||
| ) | ||||||
| or | ||||||
| n1.isAfter(loopstmt.getBody()) and | ||||||
| n2.isAdditional(loopstmt, loopHeaderTag()) | ||||||
| ) | ||||||
| or | ||||||
| exists(WhileStmt whilestmt | | ||||||
| n1.isAfter(getWhileElse(whilestmt)) and | ||||||
| n2.isAfter(whilestmt) | ||||||
| ) | ||||||
| or | ||||||
| exists(ForeachStmt foreachstmt | | ||||||
| n1.isBefore(foreachstmt) and | ||||||
| n2.isBefore(foreachstmt.getCollection()) | ||||||
| or | ||||||
| n1.isAfterValue(foreachstmt.getCollection(), | ||||||
| any(EmptinessSuccessor t | t.getValue() = true)) and | ||||||
| n2.isAfter(foreachstmt) | ||||||
| ( | ||||||
| n2.isBefore(getForeachElse(foreachstmt)) | ||||||
| or | ||||||
| not exists(getForeachElse(foreachstmt)) and n2.isAfter(foreachstmt) | ||||||
| ) | ||||||
| or | ||||||
| n1.isAfterValue(foreachstmt.getCollection(), | ||||||
| any(EmptinessSuccessor t | t.getValue() = false)) and | ||||||
|
|
@@ -1574,10 +1612,17 @@ module Make0<LocationSig Location, AstSig<Location> Ast> { | |||||
| n2.isAdditional(foreachstmt, loopHeaderTag()) | ||||||
| or | ||||||
| n1.isAdditional(foreachstmt, loopHeaderTag()) and | ||||||
| n2.isAfter(foreachstmt) | ||||||
| ( | ||||||
| n2.isBefore(getForeachElse(foreachstmt)) | ||||||
| or | ||||||
| not exists(getForeachElse(foreachstmt)) and n2.isAfter(foreachstmt) | ||||||
| ) | ||||||
| or | ||||||
| n1.isAdditional(foreachstmt, loopHeaderTag()) and | ||||||
| n2.isBefore(foreachstmt.getVariable()) | ||||||
| or | ||||||
| n1.isAfter(getForeachElse(foreachstmt)) and | ||||||
| n2.isAfter(foreachstmt) | ||||||
| ) | ||||||
| or | ||||||
| exists(ForStmt forstmt, PreControlFlowNode condentry | | ||||||
|
|
@@ -1671,6 +1716,16 @@ module Make0<LocationSig Location, AstSig<Location> Ast> { | |||||
| exists(CatchClause catchclause | | ||||||
| exists(MatchingSuccessor t | | ||||||
| n1.isBefore(catchclause) and | ||||||
| ( | ||||||
| n2.isBefore(getCatchType(catchclause)) | ||||||
|
Contributor
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.
|
||||||
| or | ||||||
| not exists(getCatchType(catchclause)) and n2.isAfterValue(catchclause, t) | ||||||
| ) and | ||||||
| if Input1::catchAll(catchclause) then t.getValue() = true else any() | ||||||
| ) | ||||||
| or | ||||||
| exists(MatchingSuccessor t | | ||||||
| n1.isAfter(getCatchType(catchclause)) and | ||||||
| n2.isAfterValue(catchclause, t) and | ||||||
| if Input1::catchAll(catchclause) then t.getValue() = true else any() | ||||||
| ) | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.