-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rust: Implement
ConditionalCompletionSplitting
- Loading branch information
Showing
5 changed files
with
228 additions
and
126 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
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
111 changes: 106 additions & 5 deletions
111
rust/ql/lib/codeql/rust/controlflow/internal/Splitting.qll
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 |
---|---|---|
@@ -1,17 +1,118 @@ | ||
private import rust | ||
private import Completion | ||
Check warning Code scanning / CodeQL Redundant import Warning
Redundant import, the module is already imported inside
ControlFlowGraphImpl Error loading related location Loading |
||
private import ControlFlowGraphImpl | ||
private import Scope | ||
|
||
cached | ||
private module Cached { | ||
// Not using CFG splitting, so the following are just placeholder types. | ||
cached | ||
newtype TSplitKind = TSplitKindUnit() | ||
newtype TSplitKind = TConditionalCompletionSplitKind() | ||
|
||
cached | ||
newtype TSplit = TSplitUnit() | ||
newtype TSplit = TConditionalCompletionSplit(ConditionalCompletion c) | ||
} | ||
|
||
import Cached | ||
|
||
/** A split for a control flow node. */ | ||
class Split extends TSplit { | ||
abstract private class Split_ extends TSplit { | ||
/** Gets a textual representation of this split. */ | ||
string toString() { none() } | ||
abstract string toString(); | ||
} | ||
|
||
final class Split = Split_; | ||
|
||
private module ConditionalCompletionSplitting { | ||
/** | ||
* A split for conditional completions. For example, in | ||
* | ||
* ```rust | ||
* if x && !y { | ||
* // ... | ||
* } | ||
* ``` | ||
* | ||
* we record whether `x`, `y`, and `!y` evaluate to `true` or `false`, and restrict | ||
* the edges out of `!y` and `x && !y` accordingly. | ||
*/ | ||
class ConditionalCompletionSplit extends Split_, TConditionalCompletionSplit { | ||
ConditionalCompletion completion; | ||
|
||
ConditionalCompletionSplit() { this = TConditionalCompletionSplit(completion) } | ||
|
||
override string toString() { result = completion.toString() } | ||
} | ||
|
||
private class ConditionalCompletionSplitKind extends SplitKind, TConditionalCompletionSplitKind { | ||
override int getListOrder() { result = 0 } | ||
|
||
override predicate isEnabled(AstNode cfe) { this.appliesTo(cfe) } | ||
|
||
override string toString() { result = "ConditionalCompletion" } | ||
} | ||
|
||
int getNextListOrder() { result = 1 } | ||
Check warning Code scanning / CodeQL Dead code Warning
This code is never used, and it's not publicly exported.
|
||
|
||
private class ConditionalCompletionSplitImpl extends SplitImpl instanceof ConditionalCompletionSplit | ||
{ | ||
ConditionalCompletion completion; | ||
|
||
ConditionalCompletionSplitImpl() { this = TConditionalCompletionSplit(completion) } | ||
|
||
override ConditionalCompletionSplitKind getKind() { any() } | ||
|
||
override predicate hasEntry(AstNode pred, AstNode succ, Completion c) { | ||
succ(pred, succ, c) and | ||
last(succ, _, completion) and | ||
( | ||
last(succ.(LogicalNotExpr).getExpr(), pred, c) and | ||
completion.(BooleanCompletion).getDual() = c | ||
or | ||
last(succ.(BinaryLogicalOperation).getAnOperand(), pred, c) and | ||
completion = c | ||
or | ||
succ = | ||
any(IfExpr ie | | ||
last([ie.getThen(), ie.getElse()], pred, c) and | ||
completion = c | ||
) | ||
or | ||
last(succ.(MatchExpr).getAnArm().getExpr(), pred, c) and | ||
completion = c | ||
or | ||
last(succ.(BlockExpr).getStmtList().getTailExpr(), pred, c) and | ||
completion = c | ||
) | ||
or | ||
succ(pred, succ, c) and | ||
last(succ.(BreakExpr).getExpr(), pred, c) and | ||
exists(AstNode target | | ||
succ(succ, target, _) and | ||
last(target, _, completion) | ||
) and | ||
completion = c | ||
} | ||
|
||
override predicate hasEntryScope(CfgScope scope, AstNode first) { none() } | ||
|
||
override predicate hasExit(AstNode pred, AstNode succ, Completion c) { | ||
this.appliesTo(pred) and | ||
succ(pred, succ, c) and | ||
if c instanceof ConditionalCompletion | ||
then completion = c | ||
else not this.hasSuccessor(pred, succ, c) | ||
} | ||
|
||
override predicate hasExitScope(CfgScope scope, AstNode last, Completion c) { | ||
this.appliesTo(last) and | ||
scope.scopeLast(last, c) and | ||
if c instanceof ConditionalCompletion then completion = c else any() | ||
} | ||
|
||
override predicate hasSuccessor(AstNode pred, AstNode succ, Completion c) { | ||
this.appliesTo(pred) and | ||
succ(pred, succ, c) and | ||
not c instanceof ConditionalCompletion | ||
} | ||
} | ||
} |
Oops, something went wrong.