Skip to content

Commit 998237e

Browse files
authored
Merge pull request #13488 from erik-krogh/finalAlias
Shared: use final class aliases to use `extends` instead of `instanceof` in the shared libraries
2 parents 4a37c2f + 9c06828 commit 998237e

File tree

6 files changed

+33
-94
lines changed

6 files changed

+33
-94
lines changed

shared/regex/codeql/regex/OverlyLargeRangeQuery.qll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ module Make<RegexTreeViewSig TreeImpl> {
8282
bindingset[char]
8383
int toCodePoint(string char) { result.toUnicode() = char }
8484

85+
final private class FinalRegExpCharacterRange = RegExpCharacterRange;
86+
8587
/** A character range that appears to be overly wide. */
86-
class OverlyWideRange instanceof RegExpCharacterRange {
88+
class OverlyWideRange extends FinalRegExpCharacterRange {
8789
OverlyWideRange() {
8890
exists(int low, int high, int numChars |
8991
isRange(this, low, high) and
@@ -111,12 +113,6 @@ module Make<RegexTreeViewSig TreeImpl> {
111113

112114
/** Gets a string representation of a character class that matches the same chars as this range. */
113115
string printEquivalent() { result = RangePrinter::printEquivalentCharClass(this) }
114-
115-
/** Gets a string representation of this range. */
116-
string toString() { result = super.toString() }
117-
118-
/** Holds if `lo` is the lower bound of this character range and `hi` the upper bound. */
119-
predicate isRange(string lo, string hi) { super.isRange(lo, hi) }
120116
}
121117

122118
/** Gets a range that should not be reported as an overly wide range. */

shared/regex/codeql/regex/nfa/BadTagFilterQuery.qll

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module Make<RegexTreeViewSig TreeImpl> {
5555
/**
5656
* A regexp that matches some string from the `isBadTagFilterCandidate` predicate.
5757
*/
58-
class HtmlMatchingRegExp instanceof RootTerm {
58+
class HtmlMatchingRegExp extends RootTerm {
5959
HtmlMatchingRegExp() { RegexpMatching<isBadTagFilterCandidate/4>::matches(this, _) }
6060

6161
/** Holds if this regexp matched `str`, where `str` is one of the string from `isBadTagFilterCandidate`. */
@@ -65,16 +65,6 @@ module Make<RegexTreeViewSig TreeImpl> {
6565
predicate fillsCaptureGroup(string str, int g) {
6666
RegexpMatching<isBadTagFilterCandidate/4>::fillsCaptureGroup(this, str, g)
6767
}
68-
69-
/** Gets a string representation of this term. */
70-
string toString() { result = super.toString() }
71-
72-
/** Holds if this term has the specified location. */
73-
predicate hasLocationInfo(
74-
string filepath, int startline, int startcolumn, int endline, int endcolumn
75-
) {
76-
super.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
77-
}
7868
}
7969

8070
/**

shared/regex/codeql/regex/nfa/NfaUtils.qll

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ module Make<RegexTreeViewSig TreeImpl> {
7474
forex(RegExpTerm child | child = t.(RegExpSequence).getAChild() | matchesEpsilon(child))
7575
}
7676

77+
final private class FinalRegExpSubPattern = RegExpSubPattern;
78+
7779
/**
7880
* A lookahead/lookbehind that matches the empty string.
7981
*/
80-
class EmptyPositiveSubPattern instanceof RegExpSubPattern {
82+
class EmptyPositiveSubPattern extends FinalRegExpSubPattern {
8183
EmptyPositiveSubPattern() {
8284
(
8385
this instanceof RegExpPositiveLookahead
@@ -86,19 +88,18 @@ module Make<RegexTreeViewSig TreeImpl> {
8688
) and
8789
matchesEpsilon(this.getOperand())
8890
}
89-
90-
/** Gets a string representation of this sub-pattern. */
91-
string toString() { result = super.toString() }
9291
}
9392

9493
/** DEPRECATED: Use `EmptyPositiveSubPattern` instead. */
9594
deprecated class EmptyPositiveSubPatttern = EmptyPositiveSubPattern;
9695

96+
final private class FinalRegExpTerm = RegExpTerm;
97+
9798
/**
9899
* A branch in a disjunction that is the root node in a literal, or a literal
99100
* whose root node is not a disjunction.
100101
*/
101-
class RegExpRoot instanceof RegExpTerm {
102+
class RegExpRoot extends FinalRegExpTerm {
102103
RegExpRoot() {
103104
exists(RegExpParent parent |
104105
exists(RegExpAlt alt |
@@ -122,12 +123,6 @@ module Make<RegexTreeViewSig TreeImpl> {
122123
// not excluded for library specific reasons
123124
not isExcluded(super.getRootTerm().getParent())
124125
}
125-
126-
/** Gets a string representation of this root term. */
127-
string toString() { result = this.(RegExpTerm).toString() }
128-
129-
/** Gets the outermost term of this regular expression. */
130-
RegExpTerm getRootTerm() { result = super.getRootTerm() }
131126
}
132127

133128
/**
@@ -146,17 +141,8 @@ module Make<RegexTreeViewSig TreeImpl> {
146141
/**
147142
* A regexp term that is relevant for this ReDoS analysis.
148143
*/
149-
class RelevantRegExpTerm instanceof RegExpTerm {
144+
class RelevantRegExpTerm extends FinalRegExpTerm {
150145
RelevantRegExpTerm() { getRoot(this).isRelevant() }
151-
152-
/** Gets a string representation of this term. */
153-
string toString() { result = super.toString() }
154-
155-
/** Gets the raw source text of this term. */
156-
string getRawValue() { result = super.getRawValue() }
157-
158-
/** Gets the outermost term of this regular expression. */
159-
RegExpTerm getRootTerm() { result = super.getRootTerm() }
160146
}
161147

162148
/**
@@ -864,7 +850,7 @@ module Make<RegexTreeViewSig TreeImpl> {
864850
* which represents the state of the NFA before starting to
865851
* match `t`, or the `i`th character in `t` if `t` is a constant.
866852
*/
867-
class State extends TState {
853+
final class State extends TState {
868854
RegExpTerm repr;
869855

870856
State() {
@@ -1056,16 +1042,10 @@ module Make<RegexTreeViewSig TreeImpl> {
10561042
}
10571043

10581044
/** A state within a regular expression that contains a candidate state. */
1059-
class RelevantState instanceof State {
1045+
class RelevantState extends State {
10601046
RelevantState() {
10611047
exists(State s | isCandidate(s) | getRoot(s.getRepr()) = getRoot(this.getRepr()))
10621048
}
1063-
1064-
/** Gets a string representation for this state in a regular expression. */
1065-
string toString() { result = State.super.toString() }
1066-
1067-
/** Gets the term represented by this state. */
1068-
RegExpTerm getRepr() { result = State.super.getRepr() }
10691049
}
10701050
}
10711051

shared/regex/codeql/regex/nfa/RegexpMatching.qll

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,11 @@ module Make<RegexTreeViewSig TreeImpl> {
1313
private import TreeImpl
1414
import NfaUtils::Make<TreeImpl>
1515

16+
final private class FinalRegExpTerm = RegExpTerm;
17+
1618
/** A root term */
17-
class RootTerm instanceof RegExpTerm {
19+
final class RootTerm extends FinalRegExpTerm {
1820
RootTerm() { this.isRootTerm() }
19-
20-
/** Gets a string representation of this term. */
21-
string toString() { result = super.toString() }
22-
23-
/** Holds if this term has the specified location. */
24-
predicate hasLocationInfo(
25-
string filepath, int startline, int startcolumn, int endline, int endcolumn
26-
) {
27-
super.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
28-
}
2921
}
3022

3123
/**

shared/regex/codeql/regex/nfa/SuperlinearBackTracking.qll

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,13 @@ module Make<RegexTreeViewSig TreeImpl> {
418418
"' can start matching anywhere after the start of the preceeding " + prev
419419
}
420420

421+
final private class FinalInfiniteRepetitionQuantifier = InfiniteRepetitionQuantifier;
422+
421423
/**
422424
* A term that may cause a regular expression engine to perform a
423425
* polynomial number of match attempts, relative to the input length.
424426
*/
425-
class PolynomialBackTrackingTerm instanceof InfiniteRepetitionQuantifier {
427+
class PolynomialBackTrackingTerm extends FinalInfiniteRepetitionQuantifier {
426428
string reason;
427429
string pump;
428430
string prefixMsg;
@@ -463,18 +465,5 @@ module Make<RegexTreeViewSig TreeImpl> {
463465
* Gets the reason for the number of match attempts.
464466
*/
465467
string getReason() { result = reason }
466-
467-
/** Gets a string representation of this term. */
468-
string toString() { result = super.toString() }
469-
470-
/** Gets the outermost term of this regular expression. */
471-
RegExpTerm getRootTerm() { result = super.getRootTerm() }
472-
473-
/** Holds if this term has the specific location. */
474-
predicate hasLocationInfo(
475-
string filepath, int startline, int startcolumn, int endline, int endcolumn
476-
) {
477-
super.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
478-
}
479468
}
480469
}

shared/util/codeql/util/suppression/AlertSuppression.qll

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,36 @@ signature class AstNode {
55
}
66

77
signature class SingleLineComment {
8+
/** Gets a textual representation of this element. */
89
string toString();
910

11+
/**
12+
* Holds if this element is at the specified location.
13+
* The location spans column `startcolumn` of line `startline` to
14+
* column `endcolumn` of line `endline` in file `filepath`.
15+
* For more information, see
16+
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
17+
*/
1018
predicate hasLocationInfo(
1119
string filepath, int startline, int startcolumn, int endline, int endcolumn
1220
);
1321

22+
/**
23+
* Gets the text of this suppression comment.
24+
*/
1425
string getText();
1526
}
1627

1728
/**
1829
* Constructs an alert suppression query.
1930
*/
2031
module Make<AstNode Node, SingleLineComment Comment> {
32+
final private class FinalCommnent = Comment;
33+
2134
/**
2235
* An alert suppression comment.
2336
*/
24-
abstract class SuppressionComment instanceof Comment {
25-
/**
26-
* Gets the text of this suppression comment.
27-
*/
28-
string getText() { result = super.getText() }
29-
30-
/**
31-
* Holds if this element is at the specified location.
32-
* The location spans column `startcolumn` of line `startline` to
33-
* column `endcolumn` of line `endline` in file `filepath`.
34-
* For more information, see
35-
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
36-
*/
37-
predicate hasLocationInfo(
38-
string filepath, int startline, int startcolumn, int endline, int endcolumn
39-
) {
40-
super.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
41-
}
42-
37+
abstract class SuppressionComment extends FinalCommnent {
4338
/** Gets the suppression annotation in this comment. */
4439
abstract string getAnnotation();
4540

@@ -53,9 +48,6 @@ module Make<AstNode Node, SingleLineComment Comment> {
5348

5449
/** Gets the scope of this suppression. */
5550
SuppressionScope getScope() { this = result.getSuppressionComment() }
56-
57-
/** Gets a textual representation of this element. */
58-
string toString() { result = super.toString() }
5951
}
6052

6153
private class LgtmSuppressionComment extends SuppressionComment {

0 commit comments

Comments
 (0)