Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Identifying subclasses of a given free variable #15675

Unanswered
Discussion options

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.

You must be logged in to vote

Replies: 1 comment

Comment options

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.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /