Skip to content

Commit 9cd018a

Browse files
committed
unified: Invert definition of plugin hook
1 parent 2dcc6ea commit 9cd018a

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

‎unified/ql/lib/codeql/unified/internal/LocalNameBinding.qll‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
189189
exists(SiblingShadowingDecl decl |
190190
scope = decl and
191191
pattern = decl.getPattern() and
192-
(not pattern instanceof Identifier or any(NameBindingPlugin p).isNameDeclaration(pattern)) and
192+
not any(NameBindingPlugin p).isNameReferenceInPatternContext(pattern) and
193193
declaration = decl
194194
)
195195
or
196196
exists(VariableDeclaration decl |
197197
not decl instanceof SiblingShadowingDecl and
198198
getChild(scope, _) = decl and
199199
pattern = decl.getPattern() and
200-
(not pattern instanceof Identifier or any(NameBindingPlugin p).isNameDeclaration(pattern)) and
200+
not any(NameBindingPlugin p).isNameReferenceInPatternContext(pattern) and
201201
declaration = decl
202202
)
203203
or
@@ -210,28 +210,28 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
210210
exists(Parameter param |
211211
scope = param.getParent() and // TODO: add SourceCallable and use .getParameter() instead
212212
pattern = param.getPattern() and
213-
(not pattern instanceof Identifier or any(NameBindingPlugin p).isNameDeclaration(pattern)) and
213+
not any(NameBindingPlugin p).isNameReferenceInPatternContext(pattern) and
214214
declaration = param
215215
)
216216
or
217217
exists(CatchClause catch |
218218
scope = catch and // ensure both body and pattern are in scope
219219
pattern = catch.getPattern() and
220-
(not pattern instanceof Identifier or any(NameBindingPlugin p).isNameDeclaration(pattern)) and
220+
not any(NameBindingPlugin p).isNameReferenceInPatternContext(pattern) and
221221
declaration = catch
222222
)
223223
or
224224
exists(SwitchCase case |
225225
scope = case and // ensure both body and pattern are in scope
226226
pattern = case.getPattern() and
227-
(not pattern instanceof Identifier or any(NameBindingPlugin p).isNameDeclaration(pattern)) and
227+
not any(NameBindingPlugin p).isNameReferenceInPatternContext(pattern) and
228228
declaration = case
229229
)
230230
or
231231
exists(ForEachStmt stmt |
232232
scope = stmt and // ensure both 'body' and 'guard' are in scope
233233
pattern = stmt.getPattern() and
234-
(not pattern instanceof Identifier or any(NameBindingPlugin p).isNameDeclaration(pattern)) and
234+
not any(NameBindingPlugin p).isNameReferenceInPatternContext(pattern) and
235235
declaration = stmt
236236
)
237237
or
@@ -278,7 +278,7 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
278278
)
279279
or
280280
bindingContext(getEnclosingPatternExpr(pattern.(Expr)), scope, declaration) and
281-
(not pattern instanceof Identifier or any(NameBindingPlugin p).isNameDeclaration(pattern))
281+
not any(NameBindingPlugin p).isNameReferenceInPatternContext(pattern)
282282
}
283283

284284
private Expr getEnclosingPatternExpr(Expr child) {
@@ -405,7 +405,6 @@ class PotentialLocalNameAccess extends Identifier {
405405
this instanceof NameDeclaration
406406
or
407407
not this instanceof NameDeclaration and
408-
not any(NameBindingPlugin p).isNameDeclaration(this) and
409408
not this = any(NamedPattern p).getIdentifier() and
410409
not this = any(MemberAccessExpr e).getMember() and
411410
not this = any(Argument a).getName() and

‎unified/ql/lib/codeql/unified/internal/NameBindingPlugin.qll‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ private import codeql.unified.internal.NameBindingPluginSwift // ensure override
55

66
/** Extension point for language-specific inputs to name binding. */
77
class NameBindingPlugin extends Unit {
8-
/** Holds if `identifier`, occurring in pattern context, introduces a name. */
9-
predicate isNameDeclaration(Identifier identifier) { none() }
8+
/** Holds if `identifier`, occurring in pattern context, refers to an existing name. */
9+
predicate isNameReferenceInPatternContext(Identifier identifier) { none() }
1010

1111
/**
1212
* Holds if `member` is an instance member.

‎unified/ql/lib/codeql/unified/internal/NameBindingPluginSwift.qll‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ private import codeql.unified.internal.FacadeAst::Unified
77
private import codeql.unified.internal.NameBindingPlugin
88

99
class NameBindingPluginSwift extends NameBindingPlugin {
10-
override predicate isNameDeclaration(Identifier identifier) { isInsideBindingPattern(identifier) }
10+
override predicate isNameReferenceInPatternContext(Identifier identifier) {
11+
not isInsideBindingPattern(identifier)
12+
}
1113

1214
// Note: For now we assume all code is Swift, but in the future we must restrict these rules to Swift-files
1315
bindingset[cls, member]
@@ -46,10 +48,7 @@ private predicate isInsideBindingPattern(AstNode child) {
4648
parent.hasModifier(["let", "var"])
4749
)
4850
or
49-
exists(VariableDeclaration parent |
50-
child = parent.getPattern() and
51-
parent.hasModifier(["let", "var", "enum_case"])
52-
)
51+
child = any(VariableDeclaration parent).getPattern()
5352
or
5453
child = any(Parameter parent).getPattern()
5554
or

0 commit comments

Comments
 (0)