codeql icon indicating copy to clipboard operation
codeql copied to clipboard

Need help in implementing a predicate in a query

Open aaaayush-n opened this issue 1 year ago • 3 comments

Description of the issue I want to write a predicate similar to getEnclosingFunction() for TypeDecls and ParameterOrResultDecls. The problem I am facing is i want to limit my search of certain entities to a particular TypeDecl but there is no convenient predicate like getEnclosingTypeDecl() that I can use.

Please 1.share the implementation of getEnclosingFunction() predicate
2. help me write some similar predicate for any type in codeql(but particularly TypeDecl and ParameterOrResultDecl).

aaaayush-n avatar Aug 06 '24 07:08 aaaayush-n

ParameterOrResultDecl has the required predicate:

from ParameterOrResultDecl d
select d.getEnclosingFunction()

The implementation can be found in AST.qll -- try ctrl/cmd-clicking in VS Code to navigate to the definition.

  /** Gets the innermost function definition to which this AST node belongs, if any. */
  FuncDef getEnclosingFunction() { result = this.getParent().parentInSameFunction*() }

TypeDecl similarly inherits getEnclosingFunction from ASTNode, so the same query will work with from TypeDecl d.

smowton avatar Aug 06 '24 10:08 smowton

@smowton I think I was not able to explain my problem, What I want to do is

from TypeVariable tv,ParameterOrResultDecl d
where tv.getEnclosingParameterDecl()=d
select tv

I want to make sure that tv is a child of d. I don't want to know or worry about which child it is... it may be nested inside any amount of selector nodes. So for all ast node structures like ParameterDecl>SelectorExpression>SelectorExpression>MapTypeExpression>SelectorExpression>.....>TypeVariable I only want to worry about TypeVariables being enclosed in ParameterDecl/ResultDecl

aaaayush-n avatar Aug 07 '24 05:08 aaaayush-n

We don't have TypeVariable in Golang, and I infer from ParameterOrResultDecl that we must be talking about QL for Golang? Could you give an example of some Go source code and what you want to identify?

smowton avatar Aug 07 '24 11:08 smowton

This issue is stale because it has been open 14 days with no activity. Comment or remove the Stale label in order to avoid having this issue closed in 7 days.

github-actions[bot] avatar Sep 11 '24 01:09 github-actions[bot]

This issue was closed because it has been inactive for 7 days.

github-actions[bot] avatar Sep 18 '24 01:09 github-actions[bot]