Skip to content

Commit ca66a29

Browse files
committed
Go: Rewrite ::Range patterns to use instanceof
1 parent 6f5701f commit ca66a29

File tree

11 files changed

+53
-141
lines changed

11 files changed

+53
-141
lines changed

go/ql/lib/semmle/go/StringOps.qll

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,24 @@ module StringOps {
1212
* Extend this class to refine existing API models. If you want to model new APIs,
1313
* extend `StringOps::HasPrefix::Range` instead.
1414
*/
15-
class HasPrefix extends DataFlow::Node {
16-
HasPrefix::Range range;
17-
18-
HasPrefix() { range = this }
19-
15+
class HasPrefix extends DataFlow::Node instanceof HasPrefix::Range {
2016
/**
2117
* Gets the `A` in `strings.HasPrefix(A, B)`.
2218
*/
23-
DataFlow::Node getBaseString() { result = range.getBaseString() }
19+
DataFlow::Node getBaseString() { result = super.getBaseString() }
2420

2521
/**
2622
* Gets the `B` in `strings.HasPrefix(A, B)`.
2723
*/
28-
DataFlow::Node getSubstring() { result = range.getSubstring() }
24+
DataFlow::Node getSubstring() { result = super.getSubstring() }
2925

3026
/**
3127
* Gets the polarity of the check.
3228
*
3329
* If the polarity is `false` the check returns `true` if the string does not start
3430
* with the given substring.
3531
*/
36-
boolean getPolarity() { result = range.getPolarity() }
32+
boolean getPolarity() { result = super.getPolarity() }
3733
}
3834

3935
class StartsWith = HasPrefix;
@@ -241,25 +237,21 @@ module StringOps {
241237
* Extend this class to refine existing API models. If you want to model new APIs,
242238
* extend `StringOps::Concatenation::Range` instead.
243239
*/
244-
class Concatenation extends DataFlow::Node {
245-
Concatenation::Range self;
246-
247-
Concatenation() { this = self }
248-
240+
class Concatenation extends DataFlow::Node instanceof Concatenation::Range {
249241
/**
250242
* Gets the `n`th operand of this string concatenation, if there is a data-flow node for it.
251243
*/
252-
DataFlow::Node getOperand(int n) { result = self.getOperand(n) }
244+
DataFlow::Node getOperand(int n) { result = super.getOperand(n) }
253245

254246
/**
255247
* Gets the string value of the `n`th operand of this string concatenation, if it is a constant.
256248
*/
257-
string getOperandStringValue(int n) { result = self.getOperandStringValue(n) }
249+
string getOperandStringValue(int n) { result = super.getOperandStringValue(n) }
258250

259251
/**
260252
* Gets the number of operands of this string concatenation.
261253
*/
262-
int getNumOperand() { result = self.getNumOperand() }
254+
int getNumOperand() { result = super.getNumOperand() }
263255
}
264256

265257
/** Provides predicates and classes for working with string concatenations. */

go/ql/lib/semmle/go/concepts/GeneratedFile.qll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,4 @@ module GeneratedFile {
4343
* Extend this class to refine existing API models. If you want to model new APIs,
4444
* extend `GeneratedFile::Range` instead.
4545
*/
46-
class GeneratedFile extends File {
47-
GeneratedFile::Range self;
48-
49-
GeneratedFile() { this = self }
50-
}
46+
class GeneratedFile extends File instanceof GeneratedFile::Range { }

go/ql/lib/semmle/go/concepts/HTTP.qll

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ module HTTP {
3131
* Extend this class to refine existing API models. If you want to model new APIs,
3232
* extend `HTTP::ResponseWriter::Range` instead.
3333
*/
34-
class ResponseWriter extends Variable {
35-
ResponseWriter::Range self;
36-
37-
ResponseWriter() { this = self }
38-
34+
class ResponseWriter extends Variable instanceof ResponseWriter::Range {
3935
/** Gets the body that is written in this HTTP response. */
4036
ResponseBody getBody() { result.getResponseWriter() = this }
4137

@@ -47,8 +43,8 @@ module HTTP {
4743

4844
/** Gets a data-flow node that is a use of this response writer. */
4945
DataFlow::Node getANode() {
50-
result = self.getANode() or
51-
result.(DataFlow::PostUpdateNode).getPreUpdateNode() = self.getANode()
46+
result = super.getANode() or
47+
result.(DataFlow::PostUpdateNode).getPreUpdateNode() = super.getANode()
5248
}
5349
}
5450

@@ -100,19 +96,15 @@ module HTTP {
10096
* Extend this class to refine existing API models. If you want to model new APIs,
10197
* extend `HTTP::HeaderWrite::Range` instead.
10298
*/
103-
class HeaderWrite extends DataFlow::ExprNode {
104-
HeaderWrite::Range self;
105-
106-
HeaderWrite() { this = self }
107-
99+
class HeaderWrite extends DataFlow::ExprNode instanceof HeaderWrite::Range {
108100
/** Gets the (lower-case) name of a header set by this definition. */
109-
string getHeaderName() { result = self.getHeaderName() }
101+
string getHeaderName() { result = super.getHeaderName() }
110102

111103
/** Gets the value of the header set by this definition. */
112-
string getHeaderValue() { result = self.getHeaderValue() }
104+
string getHeaderValue() { result = super.getHeaderValue() }
113105

114106
/** Holds if this header write defines the header `header`. */
115-
predicate definesHeader(string header, string value) { self.definesHeader(header, value) }
107+
predicate definesHeader(string header, string value) { super.definesHeader(header, value) }
116108

117109
/**
118110
* Gets the node representing the name of the header defined by this write.
@@ -121,13 +113,13 @@ module HTTP {
121113
* sets the `Content-Type` header) may not have such a node, so callers should use
122114
* `getHeaderName` in preference to this method).
123115
*/
124-
DataFlow::Node getName() { result = self.getName() }
116+
DataFlow::Node getName() { result = super.getName() }
125117

126118
/** Gets the node representing the value of the header defined by this write. */
127-
DataFlow::Node getValue() { result = self.getValue() }
119+
DataFlow::Node getValue() { result = super.getValue() }
128120

129121
/** Gets the response writer associated with this header write, if any. */
130-
ResponseWriter getResponseWriter() { result = self.getResponseWriter() }
122+
ResponseWriter getResponseWriter() { result = super.getResponseWriter() }
131123
}
132124

133125
/** A data-flow node whose value is written to an HTTP header. */
@@ -171,11 +163,7 @@ module HTTP {
171163
* Extend this class to refine existing API models. If you want to model new APIs,
172164
* extend `HTTP::RequestBody::Range` instead.
173165
*/
174-
class RequestBody extends DataFlow::Node {
175-
RequestBody::Range self;
176-
177-
RequestBody() { this = self }
178-
}
166+
class RequestBody extends DataFlow::Node instanceof RequestBody::Range { }
179167

180168
/** Provides a class for modeling new HTTP response-body APIs. */
181169
module ResponseBody {
@@ -215,19 +203,15 @@ module HTTP {
215203
* Extend this class to refine existing API models. If you want to model new APIs,
216204
* extend `HTTP::ResponseBody::Range` instead.
217205
*/
218-
class ResponseBody extends DataFlow::Node {
219-
ResponseBody::Range self;
220-
221-
ResponseBody() { this = self }
222-
206+
class ResponseBody extends DataFlow::Node instanceof ResponseBody::Range {
223207
/** Gets the response writer associated with this header write, if any. */
224-
ResponseWriter getResponseWriter() { result = self.getResponseWriter() }
208+
ResponseWriter getResponseWriter() { result = super.getResponseWriter() }
225209

226210
/** Gets a content-type associated with this body. */
227-
string getAContentType() { result = self.getAContentType() }
211+
string getAContentType() { result = super.getAContentType() }
228212

229213
/** Gets a dataflow node for a content-type associated with this body. */
230-
DataFlow::Node getAContentTypeNode() { result = self.getAContentTypeNode() }
214+
DataFlow::Node getAContentTypeNode() { result = super.getAContentTypeNode() }
231215
}
232216

233217
/** Provides a class for modeling new HTTP template response-body APIs. */
@@ -250,13 +234,9 @@ module HTTP {
250234
* Extend this class to refine existing API models. If you want to model new APIs,
251235
* extend `HTTP::TemplateResponseBody::Range` instead.
252236
*/
253-
class TemplateResponseBody extends ResponseBody {
254-
override TemplateResponseBody::Range self;
255-
256-
TemplateResponseBody() { this = self }
257-
237+
class TemplateResponseBody extends ResponseBody instanceof TemplateResponseBody::Range {
258238
/** Gets the read of the variable inside the template where this value is read. */
259-
HtmlTemplate::TemplateRead getRead() { result = self.getRead() }
239+
HtmlTemplate::TemplateRead getRead() { result = super.getRead() }
260240
}
261241

262242
/** Provides a class for modeling new HTTP client request APIs. */
@@ -285,15 +265,11 @@ module HTTP {
285265
* Extend this class to refine existing API models. If you want to model new APIs,
286266
* extend `HTTP::ClientRequest::Range` instead.
287267
*/
288-
class ClientRequest extends DataFlow::Node {
289-
ClientRequest::Range self;
290-
291-
ClientRequest() { this = self }
292-
268+
class ClientRequest extends DataFlow::Node instanceof ClientRequest::Range {
293269
/**
294270
* Gets the URL of the request.
295271
*/
296-
DataFlow::Node getUrl() { result = self.getUrl() }
272+
DataFlow::Node getUrl() { result = super.getUrl() }
297273
}
298274

299275
/** Provides a class for modeling new HTTP redirect APIs. */
@@ -337,16 +313,12 @@ module HTTP {
337313
* Extend this class to refine existing API models. If you want to model new APIs,
338314
* extend `HTTP::Redirect::Range` instead.
339315
*/
340-
class Redirect extends DataFlow::Node {
341-
Redirect::Range self;
342-
343-
Redirect() { this = self }
344-
316+
class Redirect extends DataFlow::Node instanceof Redirect::Range {
345317
/** Gets the data-flow node representing the URL being redirected to. */
346-
DataFlow::Node getUrl() { result = self.getUrl() }
318+
DataFlow::Node getUrl() { result = super.getUrl() }
347319

348320
/** Gets the response writer that this redirect is sent on, if any. */
349-
ResponseWriter getResponseWriter() { result = self.getResponseWriter() }
321+
ResponseWriter getResponseWriter() { result = super.getResponseWriter() }
350322
}
351323

352324
/** Provides a class for modeling new HTTP handler APIs. */
@@ -369,12 +341,8 @@ module HTTP {
369341
* Extend this class to refine existing API models. If you want to model new APIs,
370342
* extend `HTTP::RequestHandler::Range` instead.
371343
*/
372-
class RequestHandler extends DataFlow::Node {
373-
RequestHandler::Range self;
374-
375-
RequestHandler() { this = self }
376-
344+
class RequestHandler extends DataFlow::Node instanceof RequestHandler::Range {
377345
/** Gets a node that is used in a check that is tested before this handler is run. */
378-
predicate guardedBy(DataFlow::Node check) { self.guardedBy(check) }
346+
predicate guardedBy(DataFlow::Node check) { super.guardedBy(check) }
379347
}
380348
}

go/ql/lib/semmle/go/dataflow/internal/DataFlowNodes.qll

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,9 @@ module Public {
341341
}
342342

343343
/** A function, viewed as a node in a data flow graph. */
344-
class FunctionNode extends Node {
345-
FunctionNode::Range self;
346-
347-
FunctionNode() { this = self }
348-
344+
class FunctionNode extends Node instanceof FunctionNode::Range {
349345
/** Gets the `i`th parameter of this function. */
350-
ParameterNode getParameter(int i) { result = self.getParameter(i) }
346+
ParameterNode getParameter(int i) { result = super.getParameter(i) }
351347

352348
/** Gets a parameter of this function. */
353349
ParameterNode getAParameter() { result = this.getParameter(_) }
@@ -356,18 +352,18 @@ module Public {
356352
int getNumParameter() { result = count(this.getAParameter()) }
357353

358354
/** Gets the name of this function, if it has one. */
359-
string getName() { result = self.getName() }
355+
string getName() { result = super.getName() }
360356

361357
/**
362358
* Gets the dataflow node holding the value of the receiver, if any.
363359
*/
364-
ReceiverNode getReceiver() { result = self.getReceiver() }
360+
ReceiverNode getReceiver() { result = super.getReceiver() }
365361

366362
/**
367363
* Gets a value returned by the given function via a return statement or an assignment to a
368364
* result variable.
369365
*/
370-
ResultNode getAResult() { result = self.getAResult() }
366+
ResultNode getAResult() { result = super.getAResult() }
371367

372368
/**
373369
* Gets the data-flow node corresponding to the `i`th result of this function.
@@ -379,7 +375,7 @@ module Public {
379375
*
380376
* Note that this predicate has no result for function literals.
381377
*/
382-
Function getFunction() { result = self.getFunction() }
378+
Function getFunction() { result = super.getFunction() }
383379
}
384380

385381
/** A representation of a function that is declared in the module scope. */

go/ql/lib/semmle/go/frameworks/Email.qll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import go
99
* Extend this class to refine existing API models. If you want to model new APIs,
1010
* extend `EmailData::Range` instead.
1111
*/
12-
class EmailData extends DataFlow::Node {
13-
EmailData::Range self;
14-
15-
EmailData() { this = self }
16-
}
12+
class EmailData extends DataFlow::Node instanceof EmailData::Range { }
1713

1814
/** Provides classes for working with data that is incorporated into an email. */
1915
module EmailData {

go/ql/lib/semmle/go/frameworks/NoSQL.qll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ module NoSql {
1212
* Extend this class to refine existing API models. If you want to model new APIs,
1313
* extend `NoSQL::Query::Range` instead.
1414
*/
15-
class Query extends DataFlow::Node {
16-
Query::Range self;
17-
18-
Query() { this = self }
19-
}
15+
class Query extends DataFlow::Node instanceof Query::Range { }
2016

2117
/** Provides classes for working with NoSql queries. */
2218
module Query {

go/ql/lib/semmle/go/frameworks/SQL.qll

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,17 @@ module SQL {
1212
* Extend this class to refine existing API models. If you want to model new APIs,
1313
* extend `SQL::Query::Range` instead.
1414
*/
15-
class Query extends DataFlow::Node {
16-
Query::Range self;
17-
18-
Query() { this = self }
19-
15+
class Query extends DataFlow::Node instanceof Query::Range {
2016
/** Gets a result of this query execution. */
21-
DataFlow::Node getAResult() { result = self.getAResult() }
17+
DataFlow::Node getAResult() { result = super.getAResult() }
2218

2319
/**
2420
* Gets a query string that is used as (part of) this SQL query.
2521
*
2622
* Note that this may not resolve all `QueryString`s that should be associated with this
2723
* query due to data flow.
2824
*/
29-
QueryString getAQueryString() { result = self.getAQueryString() }
25+
QueryString getAQueryString() { result = super.getAQueryString() }
3026
}
3127

3228
/**
@@ -59,11 +55,7 @@ module SQL {
5955
* Extend this class to refine existing API models. If you want to model new APIs,
6056
* extend `SQL::QueryString::Range` instead.
6157
*/
62-
class QueryString extends DataFlow::Node {
63-
QueryString::Range self;
64-
65-
QueryString() { this = self }
66-
}
58+
class QueryString extends DataFlow::Node instanceof QueryString::Range { }
6759

6860
/** Provides classes for working with SQL query strings. */
6961
module QueryString {

go/ql/lib/semmle/go/frameworks/Testing.qll

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import go
88
* Extend this class to refine existing models of testing frameworks. If you want to model new
99
* frameworks, extend `TestCase::Range` instead.
1010
*/
11-
class TestCase extends AstNode {
12-
TestCase::Range self;
13-
14-
TestCase() { this = self }
15-
}
11+
class TestCase extends AstNode instanceof TestCase::Range { }
1612

1713
/** Provides classes for working with test cases. */
1814
module TestCase {
@@ -47,11 +43,7 @@ module TestCase {
4743
* Extend this class to refine existing models of testing frameworks. If you want to model new
4844
* frameworks, extend `TestFile::Range` instead.
4945
*/
50-
class TestFile extends File {
51-
TestFile::Range self;
52-
53-
TestFile() { this = self }
54-
}
46+
class TestFile extends File instanceof TestFile::Range { }
5547

5648
/** Provides classes for working with test files. */
5749
module TestFile {

0 commit comments

Comments
 (0)