-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Identifying subclasses of a given free variable #15675
-
Suppose you have a variable in CodeQL of a given type. You want to identify all of the subtypes it is a part of. For instance a variable of type string and you want all of the subtypes that it is a part of.
Im asking this because I have a variable of MethodBase in the ruby QL library and I want a clean and canonical way of identifying the subclasses (Method or SingletonMethod) it is a part of so that I can represent this as a string in query output.
Example: from a given MethodBase, output if its a Method or SingletonMethod in the query output.
I know I can achieve this using an if else then statement but that seems ugly. Im assuming theres a better way to achieve this since imagine a scenario where we have 3 subtypes. Subtype1, Subtype2, and Subtype3.
How would you retrieve all of the subtypes a given variable belongs to? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Thank you for the question!
Perhaps the getAQlClass method is what you are looking for? It returns the most specific QL types of the entity that it is called on. You can use it in combination with any as follows:
any(MethodBase b).getAQlClass()
For example, consider this query:
abstract class Base extends int { Base() { this in [1..9] } } class A extends Base { A() { this = 1 } } class B extends Base { B() { this = 2 } } class C extends Base { C() { this = 3 } } select any(Base b).getAQlClass()
The result is:
| A |
| B |
| C |
Note that getAQlClass() is for use as a debugging tool, but should not be included in the final version of your query, as it slows down performance.
Beta Was this translation helpful? Give feedback.