Skip to content

Commit 99b2df0

Browse files
committed
Ruby: Make get(Explicit)VisibilityModifier private
1 parent cb8865f commit 99b2df0

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

ruby/ql/lib/codeql/ruby/ast/Method.qll

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,68 @@ class MethodBase extends Callable, BodyStmt, Scope, TMethodBase {
5353
* This is either 'public', 'private' or 'protected'.
5454
*/
5555
string getVisibility() {
56-
result = this.getVisibilityModifier().getVisibility()
56+
result = getVisibilityModifier(this).getVisibility()
5757
or
58-
not exists(this.getVisibilityModifier()) and result = "public"
58+
not exists(getVisibilityModifier(this)) and result = "public"
5959
}
60+
}
6061

61-
/**
62-
* Gets the visibility modifier that defines the visibility of this method, if
63-
* any.
64-
*/
65-
VisibilityModifier getVisibilityModifier() { none() }
62+
/**
63+
* Gets the visibility modifier that explicitly sets the visibility of method
64+
* `m`.
65+
*
66+
* Examples:
67+
* ```rb
68+
* def f
69+
* end
70+
* private :f
71+
*
72+
* private def g
73+
* end
74+
* ```
75+
*/
76+
private VisibilityModifier getExplicitVisibilityModifier(Method m) {
77+
result.getMethodArgument() = m
78+
or
79+
exists(ModuleBase n, string name |
80+
methodIsDeclaredIn(m, n, name) and
81+
modifiesIn(result, n, name)
82+
)
83+
}
84+
85+
/**
86+
* Gets the visibility modifier that defines the visibility of method `m`, if
87+
* any.
88+
*/
89+
private VisibilityModifier getVisibilityModifier(MethodBase mb) {
90+
mb =
91+
any(Method m |
92+
result = getExplicitVisibilityModifier(m)
93+
or
94+
not exists(getExplicitVisibilityModifier(m)) and
95+
exists(ModuleBase n, int methodPos | isDeclaredIn(m, n, methodPos) |
96+
// The relevant visibility modifier is the closest call that occurs before
97+
// the definition of `m` (typically this means higher up the file).
98+
result =
99+
max(int modifierPos, VisibilityModifier modifier |
100+
modifier.modifiesAmbientVisibility() and
101+
isDeclaredIn(modifier, n, modifierPos) and
102+
modifierPos < methodPos
103+
|
104+
modifier order by modifierPos
105+
)
106+
)
107+
)
108+
or
109+
mb =
110+
any(SingletonMethod m |
111+
result.getMethodArgument() = m
112+
or
113+
exists(ModuleBase n, string name |
114+
methodIsDeclaredIn(m, n, name) and
115+
modifiesIn(result, n, name)
116+
)
117+
)
66118
}
67119

68120
/**
@@ -159,57 +211,16 @@ class Method extends MethodBase, TMethod {
159211
final override string toString() { result = this.getName() }
160212

161213
override string getVisibility() {
162-
result = this.getVisibilityModifier().getVisibility()
214+
result = getVisibilityModifier(this).getVisibility()
163215
or
164216
this.getEnclosingModule() instanceof Toplevel and
165-
not exists(this.getVisibilityModifier()) and
217+
not exists(getVisibilityModifier(this)) and
166218
result = "private"
167219
or
168220
not this.getEnclosingModule() instanceof Toplevel and
169-
not exists(this.getVisibilityModifier()) and
221+
not exists(getVisibilityModifier(this)) and
170222
result = "public"
171223
}
172-
173-
override VisibilityModifier getVisibilityModifier() {
174-
result = this.getExplicitVisibilityModifier()
175-
or
176-
not exists(this.getExplicitVisibilityModifier()) and
177-
exists(ModuleBase n, int methodPos | isDeclaredIn(this, n, methodPos) |
178-
// The relevant visibility modifier is the closest call that occurs before
179-
// the definition of `m` (typically this means higher up the file).
180-
result =
181-
max(int modifierPos, VisibilityModifier modifier |
182-
modifier.modifiesAmbientVisibility() and
183-
isDeclaredIn(modifier, n, modifierPos) and
184-
modifierPos < methodPos
185-
|
186-
modifier order by modifierPos
187-
)
188-
)
189-
}
190-
191-
/**
192-
* Gets the visibility modifier that explicitly sets the visibility of this
193-
* method.
194-
*
195-
* Examples:
196-
* ```rb
197-
* def f
198-
* end
199-
* private :f
200-
*
201-
* private def g
202-
* end
203-
* ```
204-
*/
205-
VisibilityModifier getExplicitVisibilityModifier() {
206-
result.getMethodArgument() = this
207-
or
208-
exists(ModuleBase n, string name |
209-
methodIsDeclaredIn(this, n, name) and
210-
modifiesIn(result, n, name)
211-
)
212-
}
213224
}
214225

215226
pragma[nomagic]
@@ -289,15 +300,6 @@ class SingletonMethod extends MethodBase, TSingletonMethod {
289300
* ```
290301
*/
291302
override predicate isPrivate() { super.isPrivate() }
292-
293-
override VisibilityModifier getVisibilityModifier() {
294-
result.getMethodArgument() = this
295-
or
296-
exists(ModuleBase n, string name |
297-
methodIsDeclaredIn(this, n, name) and
298-
modifiesIn(result, n, name)
299-
)
300-
}
301303
}
302304

303305
/**

0 commit comments

Comments
 (0)