Swift: Add EnumDecl.getEnumElement(_)#13213
Swift: Add EnumDecl.getEnumElement(_)#13213redsun82 merged 12 commits intogithub:codeql-cli-2.13.3from
Conversation
MathiasVP
left a comment
There was a problem hiding this comment.
A couple of comments and suggestions, but otherwise this LGTM
| final EnumElementDecl getAnEnumElement() { | ||
| result = this.getMember(_).(EnumCaseDecl).getElement(_) | ||
| } |
There was a problem hiding this comment.
Two questions:
-
For an
EnumDecl dwe now have bothd.getAMember()andd.getAnEnumElement(). Should we overridegetAMember()in this class to add QLDoc that mention the difference between this new predicate andgetAMember? -
Now that we're making it more difficult for users to accidentally get hold of an
EnumCaseDecl, how about we completely get rid of them from the AST by replacingany()withnone()in https://github.com/github/codeql/blob/main/swift/ql/lib/codeql/swift/elements/decl/EnumCaseDeclConstructor.qll#L4?
There was a problem hiding this comment.
- Good idea, clearer documentation is always a good thing.
- I'm a little reluctant to get rid of the ability to reason about
EnumCaseDecls entirely. The fashion at the moment is very much for security queries whereEnumCaseDeclis a useless detail. But some users may want to write style queries - for example "only put one enum value in each enum case declaration" would be a valid style query.
There was a problem hiding this comment.
Good point about 2. Let's leave in those EnumCaseDecls for now.
There was a problem hiding this comment.
Back on 1., unfortunately I can't override the generated getMember and getAMember methods as they are marked final. Let me know if you can think of a workaround that will work for documentation purposes. I'm tempted to put a general warning on Decl.getMember itself (the ordered version) to prefer not to rely on the order of elements via this predicate when alternative methods are available?
There was a problem hiding this comment.
Ah, I wasn't aware that they're marked as final. Maybe they should't be for exactly this reason 🤔. I can't think of any good workaround for this either.
I'm tempted to put a general warning on Decl.getMember itself (the ordered version) to prefer not to rely on the order of elements via this predicate when alternative methods are available?
Sounds good to me.
cc @redsun82 maybe we should remove the final annotation on these predicates (so that we can override them with custom QLDoc).
There was a problem hiding this comment.
I've added the warning to Decl.getMember. I'm open to suggestions to improve the wording.
There was a problem hiding this comment.
FYI, Decl.getMember/0 is final because it's a generated accessor; the current extension point would be Decl.getImmediateMember/0.
Another alternative is to make the EnumCaseDecls hidden, i.e. they resolve to their child EnumElementDecls.
There was a problem hiding this comment.
It's Decl.getMember/1 that's problematic, because the indexes aren't what you might expect (if you didn't realize the cases are children interleaves with the actual elements). It works fine, it's just potentially confusing.
the current extension point would be Decl.getImmediateMember/0.
Yeah, you're right, thanks. Though I'm really just looking to update the QLDoc / help text on getMember/1 for EnumDecl, which I've achieved by just rewording the root version.
There was a problem hiding this comment.
Another alternative is to make the
EnumCaseDecls hidden, i.e. they resolve to their childEnumElementDecls.
This would only work if each EnumCaseDecl would have exactly one EnumElementDecl, which is not the case.
Btw, with #13249 the customization point would be Decl.getMember, as Decls would cease to be hideable.
MathiasVP
left a comment
There was a problem hiding this comment.
A couple of comments and suggestions, but otherwise this LGTM
Co-authored-by: Mathias Vorreiter Pedersen <mathiasvp@github.com>
d10c
left a comment
There was a problem hiding this comment.
LGTM! Let's merge this as soon as DCA is positive (also, .generated needs to be rebuilt it seems).
I don't think DCA will be very revealing here, but you're the second person to ask, so I'll start one...
Fixed. |
|
DCA LGTM. |
Add
EnumDecltests and a methodEnumDecl.getEnumElement(_). This is a shortcut from anEnumDeclto theEnumElementDecls without having to deal with theEnumCaseDecls in between (that are rarely what you care about).In the diff below, I highly recommend revealing
EnumDecl.qll. The diff has mistakenly hidden it, presumably because it was (but no longer is) a generated file.