Skip to content

Commit 7615668

Browse files
authored
Merge pull request #11662 from erik-krogh/c-useInstanceOf
Swift/C++: Use instanceof in more places
2 parents f61b817 + 92a7e78 commit 7615668

File tree

8 files changed

+33
-70
lines changed

8 files changed

+33
-70
lines changed

cpp/ql/lib/experimental/semmle/code/cpp/semantic/SemanticExprSpecific.qll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,8 @@ module SemanticExprConfig {
292292
final Location getLocation() { result = super.getLocation() }
293293
}
294294

295-
private class ValueNumberBound extends Bound {
296-
IRBound::ValueNumberBound bound;
297-
298-
ValueNumberBound() { bound = this }
299-
300-
override string toString() { result = bound.toString() }
295+
private class ValueNumberBound extends Bound instanceof IRBound::ValueNumberBound {
296+
override string toString() { result = IRBound::ValueNumberBound.super.toString() }
301297
}
302298

303299
predicate zeroBound(Bound bound) { bound instanceof IRBound::ZeroBound }

cpp/ql/lib/experimental/semmle/code/cpp/semantic/analysis/SignAnalysisCommon.qll

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,15 @@ abstract private class FlowSignDef extends SignDef {
3333
}
3434

3535
/** An SSA definition whose sign is determined by the sign of that definitions source expression. */
36-
private class ExplicitSignDef extends FlowSignDef {
37-
SemSsaExplicitUpdate update;
38-
39-
ExplicitSignDef() { update = this }
40-
41-
final override Sign getSign() { result = semExprSign(update.getSourceExpr()) }
36+
private class ExplicitSignDef extends FlowSignDef instanceof SemSsaExplicitUpdate {
37+
final override Sign getSign() { result = semExprSign(super.getSourceExpr()) }
4238
}
4339

4440
/** An SSA Phi definition, whose sign is the union of the signs of its inputs. */
45-
private class PhiSignDef extends FlowSignDef {
46-
SemSsaPhiNode phi;
47-
48-
PhiSignDef() { phi = this }
49-
41+
private class PhiSignDef extends FlowSignDef instanceof SemSsaPhiNode {
5042
final override Sign getSign() {
5143
exists(SemSsaVariable inp, SemSsaReadPositionPhiInputEdge edge |
52-
edge.phiInput(phi, inp) and
44+
edge.phiInput(this, inp) and
5345
result = semSsaSign(inp, edge)
5446
)
5547
}

cpp/ql/lib/semmle/code/cpp/commons/Printf.qll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,8 @@ private int lengthInBase16(float f) {
397397
/**
398398
* A class to represent format strings that occur as arguments to invocations of formatting functions.
399399
*/
400-
class FormatLiteral extends Literal {
401-
FormatLiteral() {
402-
exists(FormattingFunctionCall ffc | ffc.getFormat() = this) and
403-
this instanceof StringLiteral
404-
}
400+
class FormatLiteral extends Literal instanceof StringLiteral {
401+
FormatLiteral() { exists(FormattingFunctionCall ffc | ffc.getFormat() = this) }
405402

406403
/**
407404
* Gets the function call where this format string is used.

cpp/ql/lib/semmle/code/cpp/commons/Scanf.qll

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ abstract class ScanfFunction extends Function {
3030
/**
3131
* The standard function `scanf` (and variations).
3232
*/
33-
class Scanf extends ScanfFunction {
33+
class Scanf extends ScanfFunction instanceof TopLevelFunction {
3434
Scanf() {
35-
this instanceof TopLevelFunction and
36-
(
37-
this.hasGlobalOrStdOrBslName("scanf") or // scanf(format, args...)
38-
this.hasGlobalOrStdOrBslName("wscanf") or // wscanf(format, args...)
39-
this.hasGlobalName("_scanf_l") or // _scanf_l(format, locale, args...)
40-
this.hasGlobalName("_wscanf_l") // _wscanf_l(format, locale, args...)
41-
)
35+
this.hasGlobalOrStdOrBslName("scanf") or // scanf(format, args...)
36+
this.hasGlobalOrStdOrBslName("wscanf") or // wscanf(format, args...)
37+
this.hasGlobalName("_scanf_l") or // _scanf_l(format, locale, args...)
38+
this.hasGlobalName("_wscanf_l")
4239
}
4340

4441
override int getInputParameterIndex() { none() }
@@ -49,15 +46,12 @@ class Scanf extends ScanfFunction {
4946
/**
5047
* The standard function `fscanf` (and variations).
5148
*/
52-
class Fscanf extends ScanfFunction {
49+
class Fscanf extends ScanfFunction instanceof TopLevelFunction {
5350
Fscanf() {
54-
this instanceof TopLevelFunction and
55-
(
56-
this.hasGlobalOrStdOrBslName("fscanf") or // fscanf(src_stream, format, args...)
57-
this.hasGlobalOrStdOrBslName("fwscanf") or // fwscanf(src_stream, format, args...)
58-
this.hasGlobalName("_fscanf_l") or // _fscanf_l(src_stream, format, locale, args...)
59-
this.hasGlobalName("_fwscanf_l") // _fwscanf_l(src_stream, format, locale, args...)
60-
)
51+
this.hasGlobalOrStdOrBslName("fscanf") or // fscanf(src_stream, format, args...)
52+
this.hasGlobalOrStdOrBslName("fwscanf") or // fwscanf(src_stream, format, args...)
53+
this.hasGlobalName("_fscanf_l") or // _fscanf_l(src_stream, format, locale, args...)
54+
this.hasGlobalName("_fwscanf_l")
6155
}
6256

6357
override int getInputParameterIndex() { result = 0 }
@@ -68,15 +62,12 @@ class Fscanf extends ScanfFunction {
6862
/**
6963
* The standard function `sscanf` (and variations).
7064
*/
71-
class Sscanf extends ScanfFunction {
65+
class Sscanf extends ScanfFunction instanceof TopLevelFunction {
7266
Sscanf() {
73-
this instanceof TopLevelFunction and
74-
(
75-
this.hasGlobalOrStdOrBslName("sscanf") or // sscanf(src_stream, format, args...)
76-
this.hasGlobalOrStdOrBslName("swscanf") or // swscanf(src, format, args...)
77-
this.hasGlobalName("_sscanf_l") or // _sscanf_l(src, format, locale, args...)
78-
this.hasGlobalName("_swscanf_l") // _swscanf_l(src, format, locale, args...)
79-
)
67+
this.hasGlobalOrStdOrBslName("sscanf") or // sscanf(src_stream, format, args...)
68+
this.hasGlobalOrStdOrBslName("swscanf") or // swscanf(src, format, args...)
69+
this.hasGlobalName("_sscanf_l") or // _sscanf_l(src, format, locale, args...)
70+
this.hasGlobalName("_swscanf_l")
8071
}
8172

8273
override int getInputParameterIndex() { result = 0 }
@@ -87,17 +78,12 @@ class Sscanf extends ScanfFunction {
8778
/**
8879
* The standard(ish) function `snscanf` (and variations).
8980
*/
90-
class Snscanf extends ScanfFunction {
81+
class Snscanf extends ScanfFunction instanceof TopLevelFunction {
9182
Snscanf() {
92-
this instanceof TopLevelFunction and
93-
(
94-
this.hasGlobalName("_snscanf") or // _snscanf(src, max_amount, format, args...)
95-
this.hasGlobalName("_snwscanf") or // _snwscanf(src, max_amount, format, args...)
96-
this.hasGlobalName("_snscanf_l") or // _snscanf_l(src, max_amount, format, locale, args...)
97-
this.hasGlobalName("_snwscanf_l") // _snwscanf_l(src, max_amount, format, locale, args...)
98-
// note that the max_amount is not a limit on the output length, it's an input length
99-
// limit used with non null-terminated strings.
100-
)
83+
this.hasGlobalName("_snscanf") or // _snscanf(src, max_amount, format, args...)
84+
this.hasGlobalName("_snwscanf") or // _snwscanf(src, max_amount, format, args...)
85+
this.hasGlobalName("_snscanf_l") or // _snscanf_l(src, max_amount, format, locale, args...)
86+
this.hasGlobalName("_snwscanf_l")
10187
}
10288

10389
override int getInputParameterIndex() { result = 0 }

cpp/ql/src/JPL_C/LOC-4/Rule 23/MismatchedIfdefs.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ class ElseDirective extends Directive {
6666
override predicate mismatched() { depth() < 1 }
6767
}
6868

69-
class EndifDirective extends Directive {
70-
EndifDirective() { this instanceof PreprocessorEndif }
71-
69+
class EndifDirective extends Directive instanceof PreprocessorEndif {
7270
override int depthChange() { result = -1 }
7371

7472
override predicate mismatched() { depth() < 0 }

cpp/ql/test/query-tests/Metrics/Dependencies/TestPackage.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import Metrics.Dependencies.ExternalDependencies
33
/**
44
* Count directories as libraries for testing purposes.
55
*/
6-
class TestPackage extends LibraryElement {
7-
TestPackage() { this instanceof Folder }
8-
9-
override string getName() { result = this.(Folder).getBaseName() }
6+
class TestPackage extends LibraryElement instanceof Folder {
7+
override string getName() { result = super.getBaseName() }
108

119
override string getVersion() { result = "1.0" }
1210

swift/ql/lib/codeql/swift/elements/expr/ArithmeticOperation.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ class RemExpr extends BinaryExpr {
9797
* -a
9898
* ```
9999
*/
100-
class UnaryArithmeticOperation extends PrefixUnaryExpr {
101-
UnaryArithmeticOperation() { this instanceof UnaryMinusExpr }
102-
}
100+
class UnaryArithmeticOperation extends PrefixUnaryExpr instanceof UnaryMinusExpr { }
103101

104102
/**
105103
* A unary minus expression.

swift/ql/lib/codeql/swift/elements/expr/LogicalOperation.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ class NotExpr extends PrefixUnaryExpr {
3030
NotExpr() { unaryHasName(this, "!(_:)") }
3131
}
3232

33-
class UnaryLogicalOperation extends PrefixUnaryExpr {
34-
UnaryLogicalOperation() { this instanceof NotExpr }
35-
}
33+
class UnaryLogicalOperation extends PrefixUnaryExpr instanceof NotExpr { }
3634

3735
class LogicalOperation extends Expr {
3836
LogicalOperation() {

0 commit comments

Comments
 (0)