@@ -10,6 +10,19 @@ private predicate hasParameterBlockImpl(Internal::Parameter p, ParamBlock block,
10
10
param_block_parameter ( block , i , p )
11
11
}
12
12
13
+ private predicate hasParameterBlockExcludingPipelineImpl (
14
+ Internal:: Parameter p , ParamBlock block , int i
15
+ ) {
16
+ p =
17
+ rank [ i + 1 ] ( Internal:: Parameter cand , int j |
18
+ hasParameterBlockImpl ( cand , block , j ) and
19
+ not cand .getAnAttribute ( ) .( Attribute ) .getANamedArgument ( ) instanceof
20
+ ValueFromPipelineAttribute
21
+ |
22
+ cand order by j
23
+ )
24
+ }
25
+
13
26
/**
14
27
* Gets the enclosing scope of `p`.
15
28
*
@@ -41,7 +54,7 @@ private predicate isParameterImpl(string name, Scope scope) {
41
54
private newtype TParameterImpl =
42
55
TInternalParameter ( Internal:: Parameter p ) or
43
56
TUnderscore ( Scope scope ) {
44
- exists ( VarAccess va | va .getUserPath ( ) = "_" and scope = va .getEnclosingScope ( ) )
57
+ exists ( VarAccess va | va .getUserPath ( ) = [ "_" , "PSItem" ] and scope = va .getEnclosingScope ( ) )
45
58
} or
46
59
TThisParameter ( Scope scope ) { exists ( scope .getEnclosingFunction ( ) .getDeclaringType ( ) ) }
47
60
@@ -56,15 +69,21 @@ private class ParameterImpl extends TParameterImpl {
56
69
57
70
predicate hasParameterBlock ( ParamBlock block , int i ) { none ( ) }
58
71
72
+ predicate hasParameterBlockExcludingPipeline ( ParamBlock block , int i ) { none ( ) }
73
+
59
74
predicate isFunctionParameter ( Function f , int i ) { none ( ) }
60
75
61
76
Expr getDefaultValue ( ) { none ( ) }
62
77
78
+ abstract Attribute getAnAttribute ( ) ;
79
+
63
80
VarAccess getAnAccess ( ) {
64
81
// TODO: This won't join order nicely.
65
82
result .getUserPath ( ) = this .getName ( ) and
66
83
result .getEnclosingScope ( ) = this .getEnclosingScope ( )
67
84
}
85
+
86
+ abstract predicate isPipeline ( ) ;
68
87
}
69
88
70
89
private class InternalParameter extends ParameterImpl , TInternalParameter {
@@ -82,11 +101,26 @@ private class InternalParameter extends ParameterImpl, TInternalParameter {
82
101
hasParameterBlockImpl ( p , block , i )
83
102
}
84
103
104
+ override predicate hasParameterBlockExcludingPipeline ( ParamBlock block , int i ) {
105
+ hasParameterBlockExcludingPipelineImpl ( p , block , i )
106
+ }
107
+
85
108
override predicate isFunctionParameter ( Function f , int i ) { isFunctionParameterImpl ( p , f , i ) }
86
109
87
110
override Expr getDefaultValue ( ) { result = p .getDefaultValue ( ) }
111
+
112
+ override Attribute getAnAttribute ( ) { result = p .getAnAttribute ( ) }
113
+
114
+ override predicate isPipeline ( ) {
115
+ this .getAnAttribute ( ) .getANamedArgument ( ) instanceof ValueFromPipelineAttribute
116
+ }
88
117
}
89
118
119
+ /**
120
+ * The variable that represents an element in the pipeline.
121
+ *
122
+ * This is either the variable `$_` or the variable `$PSItem`.
123
+ */
90
124
private class Underscore extends ParameterImpl , TUnderscore {
91
125
Scope scope ;
92
126
@@ -108,6 +142,12 @@ private class Underscore extends ParameterImpl, TUnderscore {
108
142
override string getName ( ) { result = "_" }
109
143
110
144
final override Scope getEnclosingScope ( ) { result = scope }
145
+
146
+ final override Attribute getAnAttribute ( ) { none ( ) }
147
+
148
+ final override predicate isPipeline ( ) { any ( ) }
149
+
150
+ final override predicate isFunctionParameter ( Function f , int i ) { f .getBody ( ) = scope and i = - 1 }
111
151
}
112
152
113
153
private class ThisParameter extends ParameterImpl , TThisParameter {
@@ -120,6 +160,10 @@ private class ThisParameter extends ParameterImpl, TThisParameter {
120
160
override string getName ( ) { result = "this" }
121
161
122
162
final override Scope getEnclosingScope ( ) { result = scope }
163
+
164
+ final override Attribute getAnAttribute ( ) { none ( ) }
165
+
166
+ final override predicate isPipeline ( ) { none ( ) }
123
167
}
124
168
125
169
private newtype TVariable =
@@ -128,7 +172,8 @@ private newtype TVariable =
128
172
not name = "this" and // This is modeled as a parameter
129
173
exists ( VarAccess va | va .getUserPath ( ) = name and scope = va .getEnclosingScope ( ) )
130
174
} or
131
- TParameter ( ParameterImpl p )
175
+ TParameter ( ParameterImpl p ) or
176
+ TPipelineIteratorVariable ( ProcessBlock pb )
132
177
133
178
private class AbstractVariable extends TVariable {
134
179
abstract Location getLocation ( ) ;
@@ -137,6 +182,8 @@ private class AbstractVariable extends TVariable {
137
182
138
183
abstract string getName ( ) ;
139
184
185
+ final predicate hasName ( string s ) { this .getName ( ) = s }
186
+
140
187
abstract Scope getDeclaringScope ( ) ;
141
188
142
189
VarAccess getAnAccess ( ) {
@@ -194,6 +241,10 @@ class Parameter extends AbstractLocalScopeVariable, TParameter {
194
241
195
242
predicate hasParameterBlock ( ParamBlock block , int i ) { p .hasParameterBlock ( block , i ) }
196
243
244
+ predicate hasParameterBlockExcludingPipeline ( ParamBlock block , int i ) {
245
+ p .hasParameterBlockExcludingPipeline ( block , i )
246
+ }
247
+
197
248
predicate isFunctionParameter ( Function f , int i ) { p .isFunctionParameter ( f , i ) }
198
249
199
250
Expr getDefaultValue ( ) { result = p .getDefaultValue ( ) }
@@ -210,11 +261,45 @@ class Parameter extends AbstractLocalScopeVariable, TParameter {
210
261
*/
211
262
int getIndex ( ) { result = this .getFunctionIndex ( ) or result = this .getBlockIndex ( ) }
212
263
264
+ int getIndexExcludingPipeline ( ) {
265
+ result = this .getFunctionIndex ( ) or result = this .getBlockIndexExcludingPipeline ( )
266
+ }
267
+
213
268
/** Gets the index of this parameter in the parameter block, if any. */
214
269
int getBlockIndex ( ) { this .hasParameterBlock ( _, result ) }
215
270
271
+ int getBlockIndexExcludingPipeline ( ) { this .hasParameterBlockExcludingPipeline ( _, result ) }
272
+
216
273
/** Gets the index of this parameter in the function, if any. */
217
274
int getFunctionIndex ( ) { this .isFunctionParameter ( _, result ) }
218
275
219
276
Function getFunction ( ) { result .getBody ( ) = this .getDeclaringScope ( ) }
277
+
278
+ Attribute getAnAttribute ( ) { result = p .getAnAttribute ( ) }
279
+
280
+ predicate isPipeline ( ) { p .isPipeline ( ) }
281
+ }
282
+
283
+ class PipelineParameter extends Parameter {
284
+ PipelineParameter ( ) { this .isPipeline ( ) }
285
+ }
286
+
287
+ /**
288
+ * The variable that represents the value of a pipeline during a process block.
289
+ *
290
+ * That is, it is _not_ the pipeline variable, but the value that is obtained by reading
291
+ * from the pipeline.
292
+ */
293
+ class PipelineIteratorVariable extends AbstractLocalScopeVariable , TPipelineIteratorVariable {
294
+ private ProcessBlock pb ;
295
+
296
+ PipelineIteratorVariable ( ) { this = TPipelineIteratorVariable ( pb ) }
297
+
298
+ override Location getLocation ( ) { result = pb .getLocation ( ) }
299
+
300
+ override string getName ( ) { result = "pipeline iterator for " + pb .toString ( ) }
301
+
302
+ final override Scope getDeclaringScope ( ) { result = pb .getEnclosingScope ( ) }
303
+
304
+ ProcessBlock getProcessBlock ( ) { result = pb }
220
305
}
0 commit comments