-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Rust: AST support for variables #17606
Conversation
da9b7dd
to
6914fb6
Compare
8c2d56d
to
1029ab7
Compare
96c6b17
to
bf3f8dd
Compare
bf3f8dd
to
2ea39da
Compare
2ea39da
to
ee3291e
Compare
ee3291e
to
b0efffd
Compare
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.
Looks good to me.
* Gets the text of this comment, excluding the comment marker. | ||
*/ | ||
string getCommentText() { | ||
exists(string s | s = this.getText() | result = s.regexpCapture("///?\\s*(.*)", 1)) |
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.
Rust also has block comments starting with /*
or /**
.
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.
Yes, but I skipped that for now, as it is not needed for inline test expectations.
|
||
/** Gets the outermost enclosing `|` pattern parent of `p`, if any. */ | ||
private OrPat getOutermostEnclosingOrPat(IdentPat p) { | ||
result = getEnclosingOrPat+(p) and |
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.
What about ParenPat
nodes? Things like a | (b | c) | d
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.
Those should be handled as well; getAPatAncestor
uses the generic getImmediateParent
predicate which also works for ParenPat
.
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.
Ok, could you add a test case, just to be sure?
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.
Sure.
) | ||
} | ||
|
||
private newtype TVariableOrAccessCand = |
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.
This type looked a little strange to me at first sight. Perhaps you could add a comment explaining the idea of algorithm. If I understand correctly you rank all variable definitions and uses, and bind each use to the highest ranking definition that has a lower rank than the variable use.
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.
Fair; I have tried to document the class with the algorithm that is used.
This PR adds (initial) AST support for variables. For now, two classes
Variable
andVariableAccess
are exposed, and aVariableAccess
is linked to theVariable
that it accesses.Commit-by-commit review is suggested.
Care must be taken when taking shadowing into account: A variable can be shadowed both in a nested scope
as well as in the same scope
In order to identify which variable is being accessed, we employ a purely location-based analysis, as we want to avoid an analysis that depends on the CFG.
In almost all cases, a
Variable
is identified by theIdentPat
node that introduces it, however in case ofor
patterns likex
can be introduced by either of the two branches, so we instead identifyx
with the wholeEither::Left(x) | Either::Right(x)
pattern.