@@ -40,6 +40,29 @@ class MethodBase extends Callable, BodyStmt, Scope, TMethodBase {
40
40
/** A call to `private`. */
41
41
private class Private extends MethodCall {
42
42
Private ( ) { this .getMethodName ( ) = "private" }
43
+
44
+ /** Gets the method that this `private` call applies to, if any */
45
+ Expr getMethod ( ) { result = this .getArgument ( 0 ) }
46
+
47
+ /**
48
+ * Holds if this `private` call happens inside `c`, and refers to a
49
+ * method named `name`.
50
+ */
51
+ pragma [ noinline]
52
+ predicate isRef ( ClassDeclaration c , string name ) {
53
+ this = c .getAStmt ( ) and
54
+ name = this .getMethod ( ) .( SymbolLiteral ) .getValueText ( )
55
+ }
56
+
57
+ /**
58
+ * Holds if this `private` call happens at position `i` inside `c`,
59
+ * and the call has no arguments.
60
+ */
61
+ pragma [ noinline]
62
+ predicate hasNoArg ( ClassDeclaration c , int i ) {
63
+ this = c .getStmt ( i ) and
64
+ not exists ( this .getMethod ( ) )
65
+ }
43
66
}
44
67
45
68
/** A normal method. */
@@ -67,6 +90,12 @@ class Method extends MethodBase, TMethod {
67
90
*/
68
91
final predicate isSetter ( ) { g .getName ( ) instanceof Ruby:: Setter }
69
92
93
+ pragma [ noinline]
94
+ private predicate isDeclaredIn ( ClassDeclaration c , string name ) {
95
+ this = c .getAStmt ( ) and
96
+ name = this .getName ( )
97
+ }
98
+
70
99
/**
71
100
* Holds if this method is private. All methods with the name prefix
72
101
* `private` are private below:
@@ -94,17 +123,15 @@ class Method extends MethodBase, TMethod {
94
123
* ```
95
124
*/
96
125
predicate isPrivate ( ) {
97
- this = any ( Private p ) .getArgument ( 0 )
126
+ this = any ( Private p ) .getMethod ( )
98
127
or
99
- exists ( ClassDeclaration c , Private p , SymbolLiteral s |
100
- p .getArgument ( 0 ) = s and
101
- p = c .getAStmt ( ) and
102
- this .getName ( ) = s .getValueText ( ) and
103
- this = c .getAStmt ( )
128
+ exists ( ClassDeclaration c , Private p , string name |
129
+ this .isDeclaredIn ( c , name ) and
130
+ p .isRef ( c , name )
104
131
)
105
132
or
106
- exists ( ClassDeclaration c , int i , int j |
107
- c . getStmt ( i ) . ( Private ) . getNumberOfArguments ( ) = 0 and
133
+ exists ( ClassDeclaration c , Private p , int i , int j |
134
+ p . hasNoArg ( c , i ) and
108
135
this = c .getStmt ( j ) and
109
136
j > i
110
137
)
0 commit comments