Merge pull request #1598 from markshannon/python-better-parameter-api

Python: Better API for parameters.
This commit is contained in:
Taus
2019-07-25 11:35:51 +02:00
committed by GitHub
11 changed files with 81 additions and 13 deletions

View File

@@ -81,7 +81,7 @@ class Comprehension extends Comprehension_, AstNode {
}
class BytesOrStr extends BytesOrStr_ {
class BytesOrStr extends BytesOrStr_ {
}

View File

@@ -224,6 +224,12 @@ class Parameter extends Parameter_ {
Parameter() {
/* Parameter_ is just defined as a Name or Tuple, narrow to actual parameters */
exists(ParameterList pl | py_exprs(this, _, pl, _))
or
exists(Function f |
f.getVararg() = this
or
f.getKwarg() = this
)
}
Location getLocation() {
@@ -242,6 +248,7 @@ class Parameter extends Parameter_ {
result = this
}
/** Gets the expression for the default value of this parameter */
Expr getDefault() {
exists(Function f, int n, int c, int d, Arguments args |
args = f.getDefinition().getArgs() |
@@ -252,6 +259,24 @@ class Parameter extends Parameter_ {
)
}
/** Gets the annotation expression of this parameter */
Expr getAnnotation() {
exists(Function f, int n, Arguments args |
args = f.getDefinition().getArgs() |
f.getArg(n) = this and
result = args.getAnnotation(n)
)
or
exists(Function f, Arguments args |
args = f.getDefinition().getArgs() |
f.getKwarg() = this and
result = args.getKwargannotation()
or
f.getVararg() = this and
result = args.getVarargannotation()
)
}
Variable getVariable() {
result.getAnAccess() = this.asName()
}

View File

@@ -275,14 +275,19 @@ class ParameterDefinition extends PyNodeDefinition {
this.getDefiningNode().getNode().(Parameter).isSelf()
}
/** Gets the control flow node for the default value of this parameter */
ControlFlowNode getDefault() {
exists(Function f, int n, int c, int d, Arguments args |
args = f.getDefinition().getArgs() |
f.getArg(n) = this.getDefiningNode().getNode() and
c = count(f.getAnArg()) and
d = count(args.getADefault()) and
result.getNode() = args.getDefault(d-c+n)
)
result.getNode() = this.getParameter().getDefault()
}
/** Gets the annotation control flow node of this parameter */
ControlFlowNode getAnnotation() {
result.getNode() = this.getParameter().getAnnotation()
}
/** Gets the name of this parameter definition */
string getName() {
result = this.getParameter().asName().getId()
}
predicate isVarargs() {