Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94347ae

Browse files
committed
C++: AV Rule 85: Check templates rather than instantiations
1 parent eef8719 commit 94347ae

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

cpp/ql/src/jsf/4.10 Classes/AV Rule 85.ql

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,39 @@ predicate oppositeOperators(string op1, string op2) {
1919
/* this match is very syntactic: we simply check that op1 is defined as
2020
!op2(_, _) */
2121
predicate implementedAsNegationOf(Operator op1, Operator op2) {
22-
exists(Block b, ReturnStmt r, NotExpr n, FunctionCall c |
22+
exists(Block b, ReturnStmt r, NotExpr n, Expr o |
2323
b = op1.getBlock() and
2424
b.getNumStmt() = 1 and
2525
r = b.getStmt(0) and
2626
n = r.getExpr() and
27-
c = n.getOperand() and
28-
c.getTarget() = op2)
27+
o = n.getOperand() and
28+
(
29+
o instanceof LTExpr and op2.hasName("operator<") or
30+
o instanceof LEExpr and op2.hasName("operator<=") or
31+
o instanceof GTExpr and op2.hasName("operator>") or
32+
o instanceof GEExpr and op2.hasName("operator>=") or
33+
o instanceof EQExpr and op2.hasName("operator==") or
34+
o instanceof NEExpr and op2.hasName("operator!=") or
35+
o.(FunctionCall).getTarget() = op2
36+
)
37+
)
2938
}
3039

3140
predicate classIsCheckableFor(Class c, string op) {
3241
oppositeOperators(op, _) and
33-
if exists(Class templ | c.isConstructedFrom(templ))
34-
then // If we are an instantiation, then we can't check `op` if the
35-
// template has it but we don't (because that member wasn't
36-
// instantiated)
37-
exists(Class templ | c.isConstructedFrom(templ) and
38-
(templ.getAMember().hasName(op) implies
39-
exists(Function f | f = c.getAMember() and
40-
f.hasName(op) and
41-
f.isDefined())))
42-
else any()
42+
// We check the template, not its instantiations
43+
not c instanceof ClassTemplateInstantiation and
44+
// Member functions of templates are not necessarily instantiated, so
45+
// if the function we want to check exists, then make sure that its
46+
// body also exists
47+
((c instanceof TemplateClass)
48+
implies
49+
forall(Function f | f = c.getAMember() and f.hasName(op)
50+
| exists(f.getEntryPoint())))
4351
}
4452

4553
from Class c, string op, string opp, Operator rator
4654
where c.fromSource() and
47-
not c instanceof TemplateClass and
4855
oppositeOperators(op, opp) and
4956
classIsCheckableFor(c, op) and
5057
classIsCheckableFor(c, opp) and

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 85/AV Rule 85.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
| AV Rule 85.cpp:9:7:9:14 | MyClass2 | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 13, but it is not defined in terms of its opposite operator operator<. |
44
| AV Rule 85.cpp:25:7:25:14 | MyClass4 | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 32, but it is not defined in terms of its opposite operator operator>. |
55
| AV Rule 85.cpp:25:7:25:14 | MyClass4 | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 31, but it is not defined in terms of its opposite operator operator<=. |
6-
| AV Rule 85.cpp:79:7:79:14 | MyClass8<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 90, but it is not defined in terms of its opposite operator operator>. |
7-
| AV Rule 85.cpp:79:7:79:14 | MyClass8<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 88, but it is not defined in terms of its opposite operator operator<=. |
8-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 114, but it is not defined in terms of its opposite operator operator>. |
9-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 112, but it is not defined in terms of its opposite operator operator<=. |
6+
| AV Rule 85.cpp:79:7:79:14 | MyClass8<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 90, but it is not defined in terms of its opposite operator operator>. |
7+
| AV Rule 85.cpp:79:7:79:14 | MyClass8<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 88, but it is not defined in terms of its opposite operator operator<=. |
8+
| AV Rule 85.cpp:103:7:103:14 | MyClass9<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 114, but it is not defined in terms of its opposite operator operator>. |
9+
| AV Rule 85.cpp:103:7:103:14 | MyClass9<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 112, but it is not defined in terms of its opposite operator operator<=. |

0 commit comments

Comments
 (0)