Skip to content

Commit 573494d

Browse files
authored
Merge pull request github#3096 from tausbn/python-autoformat-almost-everything
Python: Autoformat (almost) all `.qll` files.
2 parents fce04f0 + 727cde3 commit 573494d

File tree

133 files changed

+6911
-11488
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+6911
-11488
lines changed

python/ql/src/Classes/ClassAttributes.qll

Lines changed: 81 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ private import semmle.python.pointsto.PointsTo
33

44
/** Helper class for UndefinedClassAttribute.ql and MaybeUndefinedClassAttribute.ql */
55
class CheckClass extends ClassObject {
6-
76
private predicate ofInterest() {
87
not this.unknowableAttributes() and
98
not this.getPyClass().isProbableMixin() and
@@ -19,7 +18,8 @@ class CheckClass extends ClassObject {
1918
forall(ClassObject sup |
2019
sup = this.getAnImproperSuperType() and
2120
sup.declaresAttribute("__init__") and
22-
not sup = theObjectType() |
21+
not sup = theObjectType()
22+
|
2323
sup.declaredAttribute("__init__") instanceof PyFunctionObject
2424
)
2525
}
@@ -32,108 +32,111 @@ class CheckClass extends ClassObject {
3232
}
3333

3434
predicate sometimesDefines(string name) {
35-
this.alwaysDefines(name) or
36-
exists(SelfAttributeStore sa |
37-
sa.getScope().getScope+() = this.getAnImproperSuperType().getPyClass() |
35+
this.alwaysDefines(name)
36+
or
37+
exists(SelfAttributeStore sa |
38+
sa.getScope().getScope+() = this.getAnImproperSuperType().getPyClass()
39+
|
3840
name = sa.getName()
3941
)
4042
}
4143

4244
private predicate selfDictAssigns() {
43-
exists(Assign a, SelfAttributeRead self_dict, Subscript sub |
45+
exists(Assign a, SelfAttributeRead self_dict, Subscript sub |
4446
self_dict.getName() = "__dict__" and
45-
(
46-
self_dict = sub.getObject()
47-
or
48-
/* Indirect assignment via temporary variable */
49-
exists(SsaVariable v |
50-
v.getAUse() = sub.getObject().getAFlowNode() and
51-
v.getDefinition().(DefinitionNode).getValue() = self_dict.getAFlowNode()
52-
)
47+
(
48+
self_dict = sub.getObject()
49+
or
50+
/* Indirect assignment via temporary variable */
51+
exists(SsaVariable v |
52+
v.getAUse() = sub.getObject().getAFlowNode() and
53+
v.getDefinition().(DefinitionNode).getValue() = self_dict.getAFlowNode()
54+
)
5355
) and
5456
a.getATarget() = sub and
55-
exists(FunctionObject meth | meth = this.lookupAttribute(_) and a.getScope() = meth.getFunction())
57+
exists(FunctionObject meth |
58+
meth = this.lookupAttribute(_) and a.getScope() = meth.getFunction()
59+
)
5660
)
5761
}
5862

59-
pragma [nomagic]
63+
pragma[nomagic]
6064
private predicate monkeyPatched(string name) {
6165
exists(Attribute a |
62-
a.getCtx() instanceof Store and
63-
PointsTo::points_to(a.getObject().getAFlowNode(), _, this, _, _) and a.getName() = name
66+
a.getCtx() instanceof Store and
67+
PointsTo::points_to(a.getObject().getAFlowNode(), _, this, _, _) and
68+
a.getName() = name
6469
)
6570
}
6671

6772
private predicate selfSetattr() {
68-
exists(Call c, Name setattr, Name self, Function method |
69-
( method.getScope() = this.getPyClass() or
70-
method.getScope() = this.getASuperType().getPyClass()
71-
) and
72-
c.getScope() = method and
73-
c.getFunc() = setattr and
74-
setattr.getId() = "setattr" and
75-
c.getArg(0) = self and
76-
self.getId() = "self"
77-
)
73+
exists(Call c, Name setattr, Name self, Function method |
74+
(
75+
method.getScope() = this.getPyClass() or
76+
method.getScope() = this.getASuperType().getPyClass()
77+
) and
78+
c.getScope() = method and
79+
c.getFunc() = setattr and
80+
setattr.getId() = "setattr" and
81+
c.getArg(0) = self and
82+
self.getId() = "self"
83+
)
7884
}
7985

80-
predicate interestingUndefined(SelfAttributeRead a) {
81-
exists(string name | name = a.getName() |
82-
interestingContext(a, name) and
83-
not this.definedInBlock(a.getAFlowNode().getBasicBlock(), name)
84-
)
85-
}
86-
87-
private predicate interestingContext(SelfAttributeRead a, string name) {
88-
name = a.getName() and
89-
this.ofInterest() and
90-
this.getPyClass() = a.getScope().getScope() and
91-
not a.locallyDefined() and
92-
not a.guardedByHasattr() and
93-
a.getScope().isPublic() and
94-
not this.monkeyPatched(name) and
95-
not attribute_assigned_in_method(lookupAttribute("setUp"), name)
96-
}
86+
predicate interestingUndefined(SelfAttributeRead a) {
87+
exists(string name | name = a.getName() |
88+
interestingContext(a, name) and
89+
not this.definedInBlock(a.getAFlowNode().getBasicBlock(), name)
90+
)
91+
}
9792

98-
private predicate probablyAbstract() {
99-
this.getName().matches("Abstract%")
100-
or
101-
this.isAbstract()
102-
}
93+
private predicate interestingContext(SelfAttributeRead a, string name) {
94+
name = a.getName() and
95+
this.ofInterest() and
96+
this.getPyClass() = a.getScope().getScope() and
97+
not a.locallyDefined() and
98+
not a.guardedByHasattr() and
99+
a.getScope().isPublic() and
100+
not this.monkeyPatched(name) and
101+
not attribute_assigned_in_method(lookupAttribute("setUp"), name)
102+
}
103103

104-
private pragma[nomagic] predicate definitionInBlock(BasicBlock b, string name) {
105-
exists(SelfAttributeStore sa |
106-
sa.getAFlowNode().getBasicBlock() = b and sa.getName() = name and sa.getClass() = this.getPyClass()
107-
)
108-
or
109-
exists(FunctionObject method | this.lookupAttribute(_) = method |
110-
attribute_assigned_in_method(method, name) and
111-
b = method.getACall().getBasicBlock()
112-
)
113-
}
104+
private predicate probablyAbstract() {
105+
this.getName().matches("Abstract%")
106+
or
107+
this.isAbstract()
108+
}
114109

115-
private pragma[nomagic] predicate definedInBlock(BasicBlock b, string name) {
116-
// manual specialisation: this is only called from interestingUndefined,
117-
// so we can push the context in from there, which must apply to a
118-
// SelfAttributeRead in the same scope
119-
exists(SelfAttributeRead a |
120-
a.getScope() = b.getScope() and name = a.getName() |
121-
interestingContext(a, name)
122-
)
123-
and
124-
this.definitionInBlock(b, name)
125-
or
126-
exists(BasicBlock prev | this.definedInBlock(prev, name) and prev.getASuccessor() = b)
127-
}
110+
pragma[nomagic]
111+
private predicate definitionInBlock(BasicBlock b, string name) {
112+
exists(SelfAttributeStore sa |
113+
sa.getAFlowNode().getBasicBlock() = b and
114+
sa.getName() = name and
115+
sa.getClass() = this.getPyClass()
116+
)
117+
or
118+
exists(FunctionObject method | this.lookupAttribute(_) = method |
119+
attribute_assigned_in_method(method, name) and
120+
b = method.getACall().getBasicBlock()
121+
)
122+
}
128123

124+
pragma[nomagic]
125+
private predicate definedInBlock(BasicBlock b, string name) {
126+
// manual specialisation: this is only called from interestingUndefined,
127+
// so we can push the context in from there, which must apply to a
128+
// SelfAttributeRead in the same scope
129+
exists(SelfAttributeRead a | a.getScope() = b.getScope() and name = a.getName() |
130+
interestingContext(a, name)
131+
) and
132+
this.definitionInBlock(b, name)
133+
or
134+
exists(BasicBlock prev | this.definedInBlock(prev, name) and prev.getASuccessor() = b)
135+
}
129136
}
130137

131-
132138
private Object object_getattribute() {
133139
result.asBuiltin() = theObjectType().asBuiltin().getMember("__getattribute__")
134140
}
135141

136-
private predicate auto_name(string name) {
137-
name = "__class__" or name = "__dict__"
138-
}
139-
142+
private predicate auto_name(string name) { name = "__class__" or name = "__dict__" }

python/ql/src/Classes/Equality.qll

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import python
22

3-
43
private Attribute dictAccess(LocalVariable var) {
54
result.getName() = "__dict__" and
65
result.getObject() = var.getAnAccess()
@@ -12,18 +11,21 @@ private Call getattr(LocalVariable obj, LocalVariable attr) {
1211
result.getArg(1) = attr.getAnAccess()
1312
}
1413

15-
/** A generic equality method that compares all attributes in its dict,
16-
* or compares attributes using `getattr`. */
14+
/**
15+
* A generic equality method that compares all attributes in its dict,
16+
* or compares attributes using `getattr`.
17+
*/
1718
class GenericEqMethod extends Function {
18-
1919
GenericEqMethod() {
2020
this.getName() = "__eq__" and
2121
exists(LocalVariable self, LocalVariable other |
22-
self.getAnAccess() = this.getArg(0) and self.getId() = "self" and
22+
self.getAnAccess() = this.getArg(0) and
23+
self.getId() = "self" and
2324
other.getAnAccess() = this.getArg(1) and
24-
exists(Compare eq |
25+
exists(Compare eq |
2526
eq.getOp(0) instanceof Eq or
26-
eq.getOp(0) instanceof NotEq |
27+
eq.getOp(0) instanceof NotEq
28+
|
2729
// `self.__dict__ == other.__dict__`
2830
eq.getAChildNode() = dictAccess(self) and
2931
eq.getAChildNode() = dictAccess(other)
@@ -40,31 +42,32 @@ class GenericEqMethod extends Function {
4042

4143
/** An `__eq__` method that just does `self is other` */
4244
class IdentityEqMethod extends Function {
43-
4445
IdentityEqMethod() {
4546
this.getName() = "__eq__" and
4647
exists(LocalVariable self, LocalVariable other |
47-
self.getAnAccess() = this.getArg(0) and self.getId() = "self" and
48+
self.getAnAccess() = this.getArg(0) and
49+
self.getId() = "self" and
4850
other.getAnAccess() = this.getArg(1) and
4951
exists(Compare eq | eq.getOp(0) instanceof Is |
5052
eq.getAChildNode() = self.getAnAccess() and
5153
eq.getAChildNode() = other.getAnAccess()
5254
)
5355
)
5456
}
55-
5657
}
5758

5859
/** An (in)equality method that delegates to its complement */
5960
class DelegatingEqualityMethod extends Function {
60-
6161
DelegatingEqualityMethod() {
6262
exists(Return ret, UnaryExpr not_, Compare comp, Cmpop op, Parameter p0, Parameter p1 |
6363
ret.getScope() = this and
6464
ret.getValue() = not_ and
65-
not_.getOp() instanceof Not and not_.getOperand() = comp and
66-
comp.compares(p0.getVariable().getAnAccess(), op, p1.getVariable().getAnAccess()) |
67-
this.getName() = "__eq__" and op instanceof NotEq or
65+
not_.getOp() instanceof Not and
66+
not_.getOperand() = comp and
67+
comp.compares(p0.getVariable().getAnAccess(), op, p1.getVariable().getAnAccess())
68+
|
69+
this.getName() = "__eq__" and op instanceof NotEq
70+
or
6871
this.getName() = "__ne__" and op instanceof Eq
6972
)
7073
}

python/ql/src/Classes/MethodCallOrder.qll

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import python
22

33
// Helper predicates for multiple call to __init__/__del__ queries.
4-
5-
pragma [noinline]
6-
private predicate multiple_invocation_paths_helper(FunctionInvocation top, FunctionInvocation i1, FunctionInvocation i2, FunctionObject multi) {
4+
pragma[noinline]
5+
private predicate multiple_invocation_paths_helper(
6+
FunctionInvocation top, FunctionInvocation i1, FunctionInvocation i2, FunctionObject multi
7+
) {
78
i1 != i2 and
89
i1 = top.getACallee+() and
910
i2 = top.getACallee+() and
1011
i1.getFunction() = multi
1112
}
1213

13-
pragma [noinline]
14-
private predicate multiple_invocation_paths(FunctionInvocation top, FunctionInvocation i1, FunctionInvocation i2, FunctionObject multi) {
14+
pragma[noinline]
15+
private predicate multiple_invocation_paths(
16+
FunctionInvocation top, FunctionInvocation i1, FunctionInvocation i2, FunctionObject multi
17+
) {
1518
multiple_invocation_paths_helper(top, i1, i2, multi) and
1619
i2.getFunction() = multi
1720
}
@@ -21,7 +24,8 @@ predicate multiple_calls_to_superclass_method(ClassObject self, FunctionObject m
2124
exists(FunctionInvocation top, FunctionInvocation i1, FunctionInvocation i2 |
2225
multiple_invocation_paths(top, i1, i2, multi) and
2326
top.runtime(self.declaredAttribute(name)) and
24-
self.getASuperType().declaredAttribute(name) = multi |
27+
self.getASuperType().declaredAttribute(name) = multi
28+
|
2529
// Only called twice if called from different functions,
2630
// or if one call-site can reach the other.
2731
i1.getCall().getScope() != i2.getCall().getScope()
@@ -53,7 +57,9 @@ private predicate missing_call(FunctionObject meth, string name) {
5357
}
5458

5559
/** Holds if `self.name` does not call `missing`, even though it is expected to. */
56-
predicate missing_call_to_superclass_method(ClassObject self, FunctionObject top, FunctionObject missing, string name) {
60+
predicate missing_call_to_superclass_method(
61+
ClassObject self, FunctionObject top, FunctionObject missing, string name
62+
) {
5763
missing = self.getASuperType().declaredAttribute(name) and
5864
top = self.lookupAttribute(name) and
5965
/* There is no call to missing originating from top */
@@ -63,10 +69,7 @@ predicate missing_call_to_superclass_method(ClassObject self, FunctionObject top
6369
sup = self.getAnImproperSuperType() and
6470
named_attributes_not_method(sup, name)
6571
) and
66-
not self.isAbstract()
67-
and
68-
does_something(missing)
69-
and
72+
not self.isAbstract() and
73+
does_something(missing) and
7074
not missing_call(top, name)
7175
}
72-

python/ql/src/Exceptions/NotImplemented.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import python
32

43
/** Holds if `notimpl` refers to `NotImplemented` or `NotImplemented()` in the `raise` statement */

python/ql/src/Exceptions/Raising.qll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import python
22

3-
/** Whether the raise statement 'r' raises 'type' from origin 'orig' */
3+
/** Whether the raise statement 'r' raises 'type' from origin 'orig' */
44
predicate type_or_typeof(Raise r, ClassValue type, AstNode orig) {
5-
exists(Expr exception |
6-
exception = r.getRaised() |
5+
exists(Expr exception | exception = r.getRaised() |
76
exception.pointsTo(type, orig)
87
or
98
not exists(ClassValue exc_type | exception.pointsTo(exc_type)) and
109
not type = ClassValue::type() and // First value is an unknown exception type
11-
exists(Value val | exception.pointsTo(val, orig) |
12-
val.getClass() = type
13-
)
10+
exists(Value val | exception.pointsTo(val, orig) | val.getClass() = type)
1411
)
15-
1612
}

0 commit comments

Comments
 (0)