-
Notifications
You must be signed in to change notification settings - Fork 2k
Swift: Add EnumDecl.getEnumElement(_) #13213
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
Changes from all commits
66b13e2
95caaec
6c35bbf
3539e55
f0ce5b0
d26a861
5ffde7a
881134a
8306862
3bcaff6
6059d8c
67d4788
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,36 @@ | ||
| // generated by codegen/codegen.py, remove this comment if you wish to edit this file | ||
| private import codeql.swift.generated.decl.EnumDecl | ||
| private import codeql.swift.elements.decl.EnumCaseDecl | ||
| private import codeql.swift.elements.decl.EnumElementDecl | ||
| private import codeql.swift.elements.decl.Decl | ||
|
|
||
| class EnumDecl extends Generated::EnumDecl { } | ||
| /** | ||
| * An enumeration declaration, for example: | ||
| * ``` | ||
| * enum MyColours { | ||
| * case red | ||
| * case green | ||
| * case blue | ||
| * } | ||
| * ``` | ||
| */ | ||
| class EnumDecl extends Generated::EnumDecl { | ||
| /** | ||
| * Gets the `index`th enumeration element of this enumeration (0-based). | ||
| */ | ||
| final EnumElementDecl getEnumElement(int index) { | ||
| result = | ||
| rank[index + 1](int memberIndex, Decl d | | ||
| d = this.getMember(memberIndex) and | ||
| d instanceof EnumElementDecl | ||
|
d10c marked this conversation as resolved.
|
||
| | | ||
| d order by memberIndex | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Gets an enumeration element of this enumeration. | ||
| */ | ||
| final EnumElementDecl getAnEnumElement() { | ||
| result = this.getMember(_).(EnumCaseDecl).getElement(_) | ||
| } | ||
|
Comment on lines
+33
to
+35
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. Two questions:
Contributor
Author
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.
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. Good point about 2. Let's leave in those
Contributor
Author
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. Back on 1., unfortunately I can't override the generated
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. Ah, I wasn't aware that they're marked as
Sounds good to me. cc @redsun82 maybe we should remove the
Contributor
Author
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. I've added the warning to
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. FYI, Another alternative is to make the
Contributor
Author
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. It's
Yeah, you're right, thanks. Though I'm really just looking to update the QLDoc / help text on
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 would only work if each Btw, with #13249 the customization point would be |
||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| | enumdecl.swift:2:1:6:1 | MyColours | (EnumDecl), .getEnumElement(0) = red, .getEnumElement(1) = green, .getEnumElement(2) = yellow, .getEnumElement(3) = blue, .getType = MyColours | | ||
| | enumdecl.swift:3:2:3:7 | case ... | (EnumCaseDecl), .getDeclaringDecl = MyColours, .getElement(0) = red | | ||
| | enumdecl.swift:3:7:3:7 | red | (EnumElementDecl), .getDeclaringDecl = MyColours | | ||
| | enumdecl.swift:4:2:4:14 | case ... | (EnumCaseDecl), .getDeclaringDecl = MyColours, .getElement(0) = green, .getElement(1) = yellow | | ||
| | enumdecl.swift:4:7:4:7 | green | (EnumElementDecl), .getDeclaringDecl = MyColours | | ||
| | enumdecl.swift:4:14:4:14 | yellow | (EnumElementDecl), .getDeclaringDecl = MyColours | | ||
| | enumdecl.swift:5:2:5:7 | case ... | (EnumCaseDecl), .getDeclaringDecl = MyColours, .getElement(0) = blue | | ||
| | enumdecl.swift:5:7:5:7 | blue | (EnumElementDecl), .getDeclaringDecl = MyColours | | ||
| | enumdecl.swift:8:1:11:1 | MyContainer | (EnumDecl), .getEnumElement(0) = str, .getEnumElement(1) = pair, .getType = MyContainer | | ||
| | enumdecl.swift:9:2:9:17 | case ... | (EnumCaseDecl), .getDeclaringDecl = MyContainer, .getElement(0) = str | | ||
| | enumdecl.swift:9:7:9:17 | str | (EnumElementDecl), .getDeclaringDecl = MyContainer, .getParam(0) = _ | | ||
| | enumdecl.swift:10:2:10:26 | case ... | (EnumCaseDecl), .getDeclaringDecl = MyContainer, .getElement(0) = pair | | ||
| | enumdecl.swift:10:7:10:26 | pair | (EnumElementDecl), .getDeclaringDecl = MyContainer, .getParam(0) = x, .getParam(1) = y | | ||
| | enumdecl.swift:13:1:16:1 | MyNumbers | (EnumDecl), .getEnumElement(0) = one, .getEnumElement(1) = two, .getEnumElement(2) = three, .getEnumElement(3) = four, .getType = MyNumbers | | ||
| | enumdecl.swift:14:2:14:16 | case ... | (EnumCaseDecl), .getDeclaringDecl = MyNumbers, .getElement(0) = one, .getElement(1) = two | | ||
| | enumdecl.swift:14:7:14:13 | one | (EnumElementDecl), .getDeclaringDecl = MyNumbers | | ||
| | enumdecl.swift:14:16:14:16 | two | (EnumElementDecl), .getDeclaringDecl = MyNumbers | | ||
| | enumdecl.swift:15:2:15:14 | case ... | (EnumCaseDecl), .getDeclaringDecl = MyNumbers, .getElement(0) = three, .getElement(1) = four | | ||
| | enumdecl.swift:15:7:15:7 | three | (EnumElementDecl), .getDeclaringDecl = MyNumbers | | ||
| | enumdecl.swift:15:14:15:14 | four | (EnumElementDecl), .getDeclaringDecl = MyNumbers | | ||
| | enumdecl.swift:18:1:20:1 | MyGreek | (EnumDecl), .getEnumElement(0) = alpha, .getEnumElement(1) = beta, .getEnumElement(2) = gamma, .getEnumElement(3) = delta, .getEnumElement(4) = epsilon, .getType = MyGreek | | ||
| | enumdecl.swift:19:2:19:34 | case ... | (EnumCaseDecl), .getDeclaringDecl = MyGreek, .getElement(0) = alpha, .getElement(1) = beta, .getElement(2) = gamma, .getElement(3) = delta, .getElement(4) = epsilon | | ||
| | enumdecl.swift:19:7:19:7 | alpha | (EnumElementDecl), .getDeclaringDecl = MyGreek | | ||
| | enumdecl.swift:19:14:19:14 | beta | (EnumElementDecl), .getDeclaringDecl = MyGreek | | ||
| | enumdecl.swift:19:20:19:20 | gamma | (EnumElementDecl), .getDeclaringDecl = MyGreek | | ||
| | enumdecl.swift:19:27:19:27 | delta | (EnumElementDecl), .getDeclaringDecl = MyGreek | | ||
| | enumdecl.swift:19:34:19:34 | epsilon | (EnumElementDecl), .getDeclaringDecl = MyGreek | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import swift | ||
|
|
||
| string describe(Decl d) { | ||
| d instanceof EnumDecl and result = "(EnumDecl)" | ||
| or | ||
| d instanceof EnumCaseDecl and result = "(EnumCaseDecl)" | ||
| or | ||
| d instanceof EnumElementDecl and result = "(EnumElementDecl)" | ||
| or | ||
| result = ".getType = " + d.(EnumDecl).getType().toString() | ||
| or | ||
| result = ".getDeclaringDecl = " + d.getDeclaringDecl().toString() | ||
| or | ||
| exists(int i | | ||
| result = ".getElement(" + i.toString() + ") = " + d.(EnumCaseDecl).getElement(i).toString() | ||
| or | ||
| result = ".getParam(" + i.toString() + ") = " + d.(EnumElementDecl).getParam(i).toString() | ||
| or | ||
| result = ".getEnumElement(" + i.toString() + ") = " + d.(EnumDecl).getEnumElement(i).toString() | ||
| ) | ||
| } | ||
|
|
||
| from Decl d | ||
| where d.getLocation().getFile().getName() != "" | ||
| select d, strictconcat(describe(d), ", ") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
|
|
||
| enum MyColours { | ||
| case red | ||
| case green, yellow | ||
| case blue | ||
| } | ||
|
|
||
| enum MyContainer { | ||
| case str(String) | ||
| case pair(x: Int, y: Int) | ||
| } | ||
|
|
||
| enum MyNumbers: Int { | ||
| case one = 1, two | ||
| case three, four | ||
| } | ||
|
|
||
| enum MyGreek { | ||
| case alpha, beta, gamma, delta, epsilon | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.