-
I have the following python code: @s_module.req("aaa")
@deco2
def fct()
pass The issue is that my ql code catches only the predicate is_deco2(FunctionExpr fe) {
exists(Expr decorator |
decorator = fe.getADecorator() and decorator.(Name).getId() = "deco2"
)
} I am kind of unable to catch the other decorator with the same way by modifying "deco2" to |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Can you be a bit more specific about what you're trying to achieve here? E.g. is it "I want to find all functions decorated by a known decorator Also, it's unclear to me how Either way, you're probably better off working at the level of API graphs, rather than using the AST interface as you do above. For that, it may be helpful to know that decorators are desugared by the Python extractor so that @s_module.req("aaa")
@deco2
def fct():
pass behaves as if the actual code was fct = (s_module.req("aaa"))(deco2(def fct(): pass)) if we imagine for the moment that Python allowed this kind of "function expression". |
Beta Was this translation helpful? Give feedback.
-
Well, I wanted to identify all functions that have (or have not) the
Yes, a little mistake on my side,
I am trying something like \ /**
* @name Decorated functions
* @kind problem
* @problem.severity warning
* @id python/example/empty-scope
*/
import python
class SecurityDecoratedFunctionExpr extends FunctionExpr {
SecurityDecoratedFunctionExpr() { this.getADecorator().(Name).getId().matches("security%") }
}
from SecurityDecoratedFunctionExpr sdfe, Expr decorator
where
decorator = sdfe.getADecorator()
select decorator, "This is the one." |
Beta Was this translation helpful? Give feedback.
I think the reason your query as written isn't working is that in
the decorator is not a
Name
, but aCall
(specificallysecurity_module.req("aaa")
), of which the function part (security_module.req
, accessible throughgetFunc
) is anAttribute
, of which the object (security_module
, accessible throughgetObject
) is aName
, whose id matchessecurity%
. Thus, you're missing a few levels of syntax in order to match the name.You could of course extend this to account for all of those additional bits of syntax, but I still think this is going about it the wrong way. Instead, I would again urge you to use API graphs. First identify all references to
s…