-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Ruby: use dual graph for common module ancestor check #21463
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
Draft
asgerf
wants to merge
5
commits into
github:main
Choose a base branch
from
asgerf:rb/dual-graph
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−9
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /** | ||
| * Provides an efficient mechanism for checking if two nodes have | ||
| * a common ancestor in a graph. | ||
| */ | ||
| overlay[local?] | ||
| module; | ||
|
|
||
| private import Location | ||
|
|
||
| signature module DualGraphInputSig<LocationSig Location> { | ||
| class Node { | ||
| string toString(); | ||
|
|
||
| Location getLocation(); | ||
| } | ||
|
|
||
| predicate edge(Node node1, Node node2); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a "dual graph" in which each node in the given graph has a "forward" and "backward" | ||
| * copy. | ||
| * | ||
| * All original edges are present in both copies, but reversed in the backward copy. | ||
| * | ||
| * In addition, all nodes have an edge from their backward node to their forward node. | ||
| * | ||
| * This can be used to check if two nodes have a common ancestor in the graph, by checking | ||
| * if a path exists from the reverse node of one node, to the forward node of another. | ||
| */ | ||
| module MakeDualGraph<LocationSig Location, DualGraphInputSig<Location> Input> { | ||
| private import Input | ||
|
|
||
| private newtype TDualNode = | ||
| TForward(Node n) or | ||
| TBackward(Node n) | ||
|
|
||
| cached | ||
| private module Cached { | ||
| /** Gets the node representing the backward node wrapping `n`. */ | ||
| cached | ||
| DualNode getBackwardNode(Node n) { result = TBackward(n) } | ||
|
|
||
| /** Gets the node representing the forward node wrapping `n`. */ | ||
| cached | ||
| DualNode getForwardNode(Node n) { result = TForward(n) } | ||
|
|
||
| /** | ||
| * Holds if the dual graph contains the edge `node1 -> node2`. See `MakeDualGraph`. | ||
| */ | ||
| private predicate dualEdge(DualNode node1, DualNode node2) { | ||
| edge(node1.asForward(), node2.asForward()) | ||
| or | ||
| edge(node2.asBackward(), node1.asBackward()) | ||
| or | ||
| node1.asBackward() = node2.asForward() | ||
| } | ||
|
|
||
| /** | ||
| * Holds if there is a non-empty path from `node1 -> node2` in the dual graph. | ||
| */ | ||
| cached | ||
| predicate dualPath(DualNode node1, DualNode node2) = fastTC(dualEdge/2)(node1, node2) | ||
| } | ||
|
|
||
| import Cached | ||
|
|
||
| /** A forward or backward copy of a node from the original graph. */ | ||
| class DualNode extends TDualNode { | ||
| /** Gets the underlying node if this is a forward node. */ | ||
| Node asForward() { this = getForwardNode(result) } | ||
|
|
||
| /** Gets the underlying node if this is a backward node. */ | ||
| Node asBackward() { this = getBackwardNode(result) } | ||
|
|
||
| /** Gets a string representation of this node. */ | ||
| string toString() { | ||
| result = this.asForward().toString() | ||
| or | ||
| result = "[rev] " + this.asBackward().toString() | ||
| } | ||
|
|
||
| /** Gets the location of this node. */ | ||
| Location getLocation() { | ||
| result = this.asForward().getLocation() | ||
| or | ||
| result = this.asBackward().getLocation() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `node1` and `node2` have a common ancestor in the original graph, that is, | ||
| * there exists a node from which both nodes are reachable. | ||
| */ | ||
| overlay[caller?] | ||
| pragma[inline] | ||
| predicate hasCommonAncestor(Node node1, Node node2) { | ||
| // Note: `fastTC` only checks for non-empty paths, but there is no need to special-case | ||
| // `node1 = node2` because the path `Backward(n) -> Forward(n)` is non-empty. | ||
| dualPath(getBackwardNode(node1), getForwardNode(node2)) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.