Skip to content

Commit 42c2b88

Browse files
authored
Merge pull request #781 from github/lcartey/2-37-0-performance-improvements
Performance improvements since upgrade to 2.16.6
2 parents 18a3f35 + 10b84ec commit 42c2b88

File tree

6 files changed

+63
-40
lines changed

6 files changed

+63
-40
lines changed

c/cert/src/rules/DCL40-C/IncompatibleFunctionDeclarations.ql

+19-17
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,32 @@
1616

1717
import cpp
1818
import codingstandards.c.cert
19+
import codingstandards.cpp.Compatible
1920
import ExternalIdentifiers
2021

21-
//checks if they are incompatible based on return type, number of parameters and parameter types
22-
predicate checkMatchingFunction(FunctionDeclarationEntry d, FunctionDeclarationEntry d2) {
23-
not d.getType() = d2.getType()
24-
or
25-
not d.getNumberOfParameters() = d2.getNumberOfParameters()
26-
or
27-
exists(ParameterDeclarationEntry p, ParameterDeclarationEntry p2, int i |
28-
d.getParameterDeclarationEntry(i) = p and
29-
d2.getParameterDeclarationEntry(i) = p2 and
30-
not p.getType() = p2.getType()
31-
)
32-
}
33-
3422
from ExternalIdentifiers d, FunctionDeclarationEntry f1, FunctionDeclarationEntry f2
3523
where
3624
not isExcluded(f1, Declarations2Package::incompatibleFunctionDeclarationsQuery()) and
3725
not isExcluded(f2, Declarations2Package::incompatibleFunctionDeclarationsQuery()) and
38-
f1 = d.getADeclarationEntry() and
39-
f2 = d.getADeclarationEntry() and
4026
not f1 = f2 and
41-
f1.getLocation().getStartLine() >= f2.getLocation().getStartLine() and
27+
f1.getDeclaration() = d and
28+
f2.getDeclaration() = d and
4229
f1.getName() = f2.getName() and
43-
checkMatchingFunction(f1, f2)
30+
(
31+
//return type check
32+
not typesCompatible(f1.getType(), f2.getType())
33+
or
34+
//parameter type check
35+
parameterTypesIncompatible(f1, f2)
36+
or
37+
not f1.getNumberOfParameters() = f2.getNumberOfParameters()
38+
) and
39+
// Apply ordering on start line, trying to avoid the optimiser applying this join too early
40+
// in the pipeline
41+
exists(int f1Line, int f2Line |
42+
f1.getLocation().hasLocationInfo(_, f1Line, _, _, _) and
43+
f2.getLocation().hasLocationInfo(_, f2Line, _, _, _) and
44+
f1Line >= f2Line
45+
)
4446
select f1, "The object $@ is not compatible with re-declaration $@", f1, f1.getName(), f2,
4547
f2.getName()

c/cert/src/rules/MSC39-C/DoNotCallVaArgOnAVaListThatHasAnIndeterminateValue.ql

+10-3
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,19 @@ predicate sameSource(VaAccess e1, VaAccess e2) {
7171
)
7272
}
7373

74+
/**
75+
* Extracted to avoid poor magic join ordering on the `isExcluded` predicate.
76+
*/
77+
predicate query(VaAccess va_acc, VaArgArg va_arg, FunctionCall fc) {
78+
sameSource(va_acc, va_arg) and
79+
fc = preceedsFC(va_acc) and
80+
fc.getTarget().calls*(va_arg.getEnclosingFunction())
81+
}
82+
7483
from VaAccess va_acc, VaArgArg va_arg, FunctionCall fc
7584
where
7685
not isExcluded(va_acc,
7786
Contracts7Package::doNotCallVaArgOnAVaListThatHasAnIndeterminateValueQuery()) and
78-
sameSource(va_acc, va_arg) and
79-
fc = preceedsFC(va_acc) and
80-
fc.getTarget().calls*(va_arg.getEnclosingFunction())
87+
query(va_acc, va_arg, fc)
8188
select va_acc, "The value of " + va_acc.toString() + " is indeterminate after the $@.", fc,
8289
fc.toString()

c/misra/src/rules/RULE-9-4/RepeatedInitializationOfAggregateObjectElement.ql

+17-19
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,31 @@ int getMaxDepth(ArrayAggregateLiteral al) {
6262

6363
// internal recursive predicate for `hasMultipleInitializerExprsForSameIndex`
6464
predicate hasMultipleInitializerExprsForSameIndexInternal(
65-
ArrayAggregateLiteral al1, ArrayAggregateLiteral al2, Expr out_al1_expr, Expr out_al2_expr
65+
ArrayAggregateLiteral root, Expr e1, Expr e2
6666
) {
67-
exists(int shared_index, Expr al1_expr, Expr al2_expr |
68-
// an `Expr` initializing an element of the same index in both `al1` and `al2`
69-
shared_index = [0 .. al1.getArraySize() - 1] and
70-
al1_expr = al1.getAnElementExpr(shared_index) and
71-
al2_expr = al2.getAnElementExpr(shared_index) and
72-
// but not the same `Expr`
73-
not al1_expr = al2_expr and
74-
(
75-
// case A - the children are not aggregate literals
76-
// holds if `al1` and `al2` both hold for .getElement[sharedIndex]
77-
not al1_expr instanceof ArrayAggregateLiteral and
78-
out_al1_expr = al1_expr and
79-
out_al2_expr = al2_expr
80-
or
81-
// case B - `al1` and `al2` both have an aggregate literal child at the same index, so recurse
82-
hasMultipleInitializerExprsForSameIndexInternal(al1_expr, al2_expr, out_al1_expr, out_al2_expr)
83-
)
67+
root = e1 and root = e2
68+
or
69+
exists(ArrayAggregateLiteral parent1, ArrayAggregateLiteral parent2, int shared_index |
70+
hasMultipleInitializerExprsForSameIndexInternal(root, parent1, parent2) and
71+
shared_index = [0 .. parent1.getArraySize() - 1] and
72+
e1 = parent1.getAnElementExpr(shared_index) and
73+
e2 = parent2.getAnElementExpr(shared_index)
8474
)
8575
}
8676

8777
/**
8878
* Holds if `expr1` and `expr2` both initialize the same array element of `root`.
8979
*/
9080
predicate hasMultipleInitializerExprsForSameIndex(ArrayAggregateLiteral root, Expr expr1, Expr expr2) {
91-
hasMultipleInitializerExprsForSameIndexInternal(root, root, expr1, expr2)
81+
hasMultipleInitializerExprsForSameIndexInternal(root, expr1, expr2) and
82+
not root = expr1 and
83+
not root = expr2 and
84+
not expr1 = expr2 and
85+
(
86+
not expr1 instanceof ArrayAggregateLiteral
87+
or
88+
not expr2 instanceof ArrayAggregateLiteral
89+
)
9290
}
9391

9492
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `DCL40-C` - `IncompatibleFunctionDeclarations.ql`:
2+
- Reduce false positives by identifying compatible integer arithmetic types (e.g. "signed int" and "int").

cpp/common/src/codingstandards/cpp/Compatible.qll

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import cpp
22

3+
pragma[noinline]
4+
pragma[nomagic]
35
predicate typesCompatible(Type t1, Type t2) {
46
t1 = t2
57
or
@@ -8,6 +10,7 @@ predicate typesCompatible(Type t1, Type t2) {
810
}
911

1012
predicate parameterTypesIncompatible(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
13+
f1.getDeclaration() = f2.getDeclaration() and
1114
exists(ParameterDeclarationEntry p1, ParameterDeclarationEntry p2, int i |
1215
p1 = f1.getParameterDeclarationEntry(i) and
1316
p2 = f2.getParameterDeclarationEntry(i)
@@ -17,6 +20,7 @@ predicate parameterTypesIncompatible(FunctionDeclarationEntry f1, FunctionDeclar
1720
}
1821

1922
predicate parameterNamesIncompatible(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
23+
f1.getDeclaration() = f2.getDeclaration() and
2024
exists(ParameterDeclarationEntry p1, ParameterDeclarationEntry p2, int i |
2125
p1 = f1.getParameterDeclarationEntry(i) and
2226
p2 = f2.getParameterDeclarationEntry(i)

cpp/common/src/codingstandards/cpp/rules/notdistinctidentifier/NotDistinctIdentifier.qll

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ abstract class NotDistinctIdentifierSharedQuery extends Query { }
1212

1313
Query getQuery() { result instanceof NotDistinctIdentifierSharedQuery }
1414

15+
bindingset[d, d2]
16+
pragma[inline_late]
17+
private predicate after(ExternalIdentifiers d, ExternalIdentifiers d2) {
18+
exists(int dStartLine, int d2StartLine |
19+
d.getLocation().hasLocationInfo(_, dStartLine, _, _, _) and
20+
d2.getLocation().hasLocationInfo(_, d2StartLine, _, _, _) and
21+
dStartLine >= d2StartLine
22+
)
23+
}
24+
1525
query predicate problems(
1626
ExternalIdentifiers d, string message, ExternalIdentifiers d2, string nameplaceholder
1727
) {
@@ -20,10 +30,10 @@ query predicate problems(
2030
d.getName().length() >= 31 and
2131
d2.getName().length() >= 31 and
2232
not d = d2 and
23-
d.getLocation().getStartLine() >= d2.getLocation().getStartLine() and
2433
d.getSignificantName() = d2.getSignificantName() and
2534
not d.getName() = d2.getName() and
2635
nameplaceholder = d2.getName() and
36+
after(d, d2) and
2737
message =
2838
"External identifer " + d.getName() +
2939
" is nondistinct in characters at or over 31 limit, compared to $@"

0 commit comments

Comments
 (0)