Skip to content

Commit e88c688

Browse files
authored
Merge pull request #15121 from MathiasVP/fix-joins-in-av-rule-145
C++: Fix joins in `AV Rule 145`
2 parents 9446249 + ef916f0 commit e88c688

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

cpp/ql/src/jsf/4.16 Initialization/AV Rule 145.ql

+30-7
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,41 @@ predicate hasReferenceInitializer(EnumConstant c) {
3232
)
3333
}
3434

35+
/**
36+
* Gets the `rnk`'th (1-based) enumeration constant in `e` that does not have a
37+
* reference initializer (i.e., an initializer that refers to an enumeration
38+
* constant from the same enumeration).
39+
*/
40+
EnumConstant getNonReferenceInitializedEnumConstantByRank(Enum e, int rnk) {
41+
result =
42+
rank[rnk](EnumConstant cand, int pos, string filepath, int startline, int startcolumn |
43+
e.getEnumConstant(pos) = cand and
44+
not hasReferenceInitializer(cand) and
45+
cand.getLocation().hasLocationInfo(filepath, startline, startcolumn, _, _)
46+
|
47+
cand order by pos, filepath, startline, startcolumn
48+
)
49+
}
50+
51+
/**
52+
* Holds if `ec` is not the last enumeration constant in `e` that has a non-
53+
* reference initializer.
54+
*/
55+
predicate hasNextWithoutReferenceInitializer(Enum e, EnumConstant ec) {
56+
exists(int rnk |
57+
ec = getNonReferenceInitializedEnumConstantByRank(e, rnk) and
58+
exists(getNonReferenceInitializedEnumConstantByRank(e, rnk + 1))
59+
)
60+
}
61+
3562
// There exists another constant whose value is implicit, but it's
3663
// not the last one: the last value is okay to use to get the highest
3764
// enum value automatically. It can be followed by aliases though.
3865
predicate enumThatHasConstantWithImplicitValue(Enum e) {
39-
exists(EnumConstant ec, int pos |
40-
ec = e.getEnumConstant(pos) and
66+
exists(EnumConstant ec |
67+
ec = e.getAnEnumConstant() and
4168
not hasInitializer(ec) and
42-
exists(EnumConstant ec2, int pos2 |
43-
ec2 = e.getEnumConstant(pos2) and
44-
pos2 > pos and
45-
not hasReferenceInitializer(ec2)
46-
)
69+
hasNextWithoutReferenceInitializer(e, ec)
4770
)
4871
}
4972

0 commit comments

Comments
 (0)