Skip to content

Commit c17560f

Browse files
authored
Merge pull request #7096 from tausbn/python-fix-more-bad-joins
Python: Fix a bunch of performance issues
2 parents c2e057d + 33135e9 commit c17560f

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

python/ql/lib/semmle/python/essa/Definitions.qll

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,17 @@ class NonLocalVariable extends SsaSourceVariable {
152152
}
153153

154154
override ControlFlowNode getAnImplicitUse() {
155-
result.(CallNode).getScope().getScope*() = this.(LocalVariable).getScope()
155+
result.(CallNode).getScope().getScope*() = this.scope_as_local_variable()
156156
}
157157

158158
override ControlFlowNode getScopeEntryDefinition() {
159159
exists(Function f |
160-
f.getScope+() = this.(LocalVariable).getScope() and
160+
f.getScope+() = this.scope_as_local_variable() and
161161
f.getEntryNode() = result
162162
)
163163
or
164164
not this.(LocalVariable).isParameter() and
165-
this.(LocalVariable).getScope().getEntryNode() = result
165+
this.scope_as_local_variable().getEntryNode() = result
166166
}
167167

168168
pragma[noinline]
@@ -215,12 +215,15 @@ class ModuleVariable extends SsaSourceVariable {
215215
)
216216
}
217217

218+
pragma[nomagic]
219+
private Scope scope_as_global_variable() { result = this.(GlobalVariable).getScope() }
220+
218221
pragma[noinline]
219-
CallNode global_variable_callnode() { result.getScope() = this.(GlobalVariable).getScope() }
222+
CallNode global_variable_callnode() { result.getScope() = this.scope_as_global_variable() }
220223

221224
pragma[noinline]
222225
ImportMemberNode global_variable_import() {
223-
result.getScope() = this.(GlobalVariable).getScope() and
226+
result.getScope() = this.scope_as_global_variable() and
224227
import_from_dot_in_init(result.getModule(this.getName()))
225228
}
226229

@@ -250,7 +253,7 @@ class ModuleVariable extends SsaSourceVariable {
250253
override ControlFlowNode getScopeEntryDefinition() {
251254
exists(Scope s | s.getEntryNode() = result |
252255
/* Module entry point */
253-
this.(GlobalVariable).getScope() = s
256+
this.scope_as_global_variable() = s
254257
or
255258
/* For implicit use of __metaclass__ when constructing class */
256259
class_with_global_metaclass(s, this)
@@ -286,13 +289,13 @@ class EscapingGlobalVariable extends ModuleVariable {
286289
override ControlFlowNode getAnImplicitUse() {
287290
result = ModuleVariable.super.getAnImplicitUse()
288291
or
289-
result.(CallNode).getScope().getScope+() = this.(GlobalVariable).getScope()
292+
result.(CallNode).getScope().getScope+() = this.scope_as_global_variable()
290293
or
291294
result = this.innerScope().getANormalExit()
292295
}
293296

294297
private Scope innerScope() {
295-
result.getScope+() = this.(GlobalVariable).getScope() and
298+
result.getScope+() = this.scope_as_global_variable() and
296299
not result instanceof ImportTimeScope
297300
}
298301

python/ql/lib/semmle/python/pointsto/PointsTo.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,16 +1195,22 @@ module InterProceduralPointsTo {
11951195
ControlFlowNode argument, PointsToContext caller, ParameterDefinition param,
11961196
PointsToContext callee
11971197
) {
1198+
PointsToInternal::pointsTo(argument, caller, _, _) and
11981199
exists(CallNode call, Function func, int offset |
11991200
callsite_calls_function(call, caller, func, callee, offset)
12001201
|
12011202
exists(string name |
12021203
argument = call.getArgByName(name) and
1203-
param.getParameter() = func.getArgByName(name)
1204+
function_parameter_name(func, param, name)
12041205
)
12051206
)
12061207
}
12071208

1209+
pragma[nomagic]
1210+
private predicate function_parameter_name(Function func, ParameterDefinition param, string name) {
1211+
param.getParameter() = func.getArgByName(name)
1212+
}
1213+
12081214
/**
12091215
* Holds if the `call` with context `caller` calls the function `scope` in context `callee`
12101216
* and the offset from argument to parameter is `parameter_offset`

python/ql/src/Statements/RedundantAssignment.ql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ predicate same_attribute(Attribute a1, Attribute a2) {
7070
not is_property_access(a1)
7171
}
7272

73+
pragma[nomagic]
74+
Comment pyflakes_comment() { result.getText().toLowerCase().matches("%pyflakes%") }
75+
7376
int pyflakes_commented_line(File file) {
74-
exists(Comment c | c.getText().toLowerCase().matches("%pyflakes%") |
75-
c.getLocation().hasLocationInfo(file.getAbsolutePath(), result, _, _, _)
76-
)
77+
pyflakes_comment().getLocation().hasLocationInfo(file.getAbsolutePath(), result, _, _, _)
7778
}
7879

7980
predicate pyflakes_commented(AssignStmt assignment) {

0 commit comments

Comments
 (0)