C++: Fix AV Rule 85

We have to be careful to avoid giving alerts to functions that might be
correctly defined, but we can't see the definition as it wasn't
instantiated.
This commit is contained in:
Ian Lynagh
2018-10-25 14:19:58 +01:00
parent ef1552339e
commit eef8719a40
3 changed files with 46 additions and 36 deletions

View File

@@ -28,14 +28,34 @@ predicate implementedAsNegationOf(Operator op1, Operator op2) {
c.getTarget() = op2)
}
predicate classIsCheckableFor(Class c, string op) {
oppositeOperators(op, _) and
if exists(Class templ | c.isConstructedFrom(templ))
then // If we are an instantiation, then we can't check `op` if the
// template has it but we don't (because that member wasn't
// instantiated)
exists(Class templ | c.isConstructedFrom(templ) and
(templ.getAMember().hasName(op) implies
exists(Function f | f = c.getAMember() and
f.hasName(op) and
f.isDefined())))
else any()
}
from Class c, string op, string opp, Operator rator
where c.fromSource() and
not c instanceof TemplateClass and
oppositeOperators(op, opp) and
classIsCheckableFor(c, op) and
classIsCheckableFor(c, opp) and
rator = c.getAMember() and
rator.hasName(op) and
not exists(Operator oprator | oprator = c.getAMember() and
oprator.hasName(opp) and
( implementedAsNegationOf(rator, oprator)
or implementedAsNegationOf(oprator, rator)))
forex(Operator aRator |
aRator = c.getAMember() and aRator.hasName(op) |
not exists(Operator oprator |
oprator = c.getAMember() and
oprator.hasName(opp) and
( implementedAsNegationOf(aRator, oprator)
or implementedAsNegationOf(oprator, aRator))))
select c, "When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator " + op +
" is declared on line " + rator.getLocation().getStartLine().toString() + ", but it is not defined in terms of its opposite operator " + opp + "."

View File

@@ -124,3 +124,25 @@ void f9(void) {
b = myClass9 >= myClass9;
}
template <typename T>
class MyClass10 {
public:
int i;
template <typename U>
bool operator< (const MyClass10<U> &rhs){ return i < rhs.i; }
template <typename U>
bool operator>= (const MyClass10<U> &rhs){ return !(*this < rhs); }
// GOOD
template <typename U>
bool operator> (const MyClass10<U> &rhs){ return i < rhs.i; }
template <typename U>
bool operator<= (const MyClass10<U> &rhs){ return i >= rhs.i; }
// BAD: neither operator defined in terms of the other
};
void f10(void) {
bool b;
MyClass10<int> myClass10;
b = myClass10 < myClass10;
b = myClass10 > myClass10;
}

View File

@@ -3,39 +3,7 @@
| 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<. |
| 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>. |
| 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<=. |
| AV Rule 85.cpp:37:7:37:14 | MyClass5<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 40, but it is not defined in terms of its opposite operator operator>=. |
| AV Rule 85.cpp:37:7:37:14 | MyClass5<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 44, but it is not defined in terms of its opposite operator operator>. |
| AV Rule 85.cpp:37:7:37:14 | MyClass5<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 43, but it is not defined in terms of its opposite operator operator<=. |
| AV Rule 85.cpp:37:7:37:14 | MyClass5<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 41, but it is not defined in terms of its opposite operator operator<. |
| AV Rule 85.cpp:49:7:49:14 | MyClass6<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 52, but it is not defined in terms of its opposite operator operator>=. |
| AV Rule 85.cpp:49:7:49:14 | MyClass6<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 56, but it is not defined in terms of its opposite operator operator>. |
| AV Rule 85.cpp:49:7:49:14 | MyClass6<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 55, but it is not defined in terms of its opposite operator operator<=. |
| AV Rule 85.cpp:49:7:49:14 | MyClass6<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 53, but it is not defined in terms of its opposite operator operator<. |
| AV Rule 85.cpp:62:7:62:14 | MyClass7<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 66, but it is not defined in terms of its opposite operator operator>=. |
| AV Rule 85.cpp:62:7:62:14 | MyClass7<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 73, but it is not defined in terms of its opposite operator operator>. |
| AV Rule 85.cpp:62:7:62:14 | MyClass7<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 71, but it is not defined in terms of its opposite operator operator<=. |
| AV Rule 85.cpp:62:7:62:14 | MyClass7<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 68, but it is not defined in terms of its opposite operator operator<. |
| AV Rule 85.cpp:62:7:62:14 | MyClass7<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 66, but it is not defined in terms of its opposite operator operator>=. |
| AV Rule 85.cpp:62:7:62:14 | MyClass7<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 73, but it is not defined in terms of its opposite operator operator>. |
| AV Rule 85.cpp:62:7:62:14 | MyClass7<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 71, but it is not defined in terms of its opposite operator operator<=. |
| AV Rule 85.cpp:62:7:62:14 | MyClass7<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 68, but it is not defined in terms of its opposite operator operator<. |
| 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 83, but it is not defined in terms of its opposite operator operator>=. |
| 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>. |
| 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<=. |
| 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 85, but it is not defined in terms of its opposite operator operator<. |
| 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 83, but it is not defined in terms of its opposite operator operator>=. |
| 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>. |
| 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<=. |
| 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 85, but it is not defined in terms of its opposite operator operator<. |
| 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 107, but it is not defined in terms of its opposite operator operator>=. |
| 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>. |
| 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<=. |
| 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 109, but it is not defined in terms of its opposite operator operator<. |
| AV Rule 85.cpp:103:7:103:14 | MyClass9<double> | 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 107, but it is not defined in terms of its opposite operator operator>=. |
| AV Rule 85.cpp:103:7:103:14 | MyClass9<double> | 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>. |
| AV Rule 85.cpp:103:7:103:14 | MyClass9<double> | 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<=. |
| AV Rule 85.cpp:103:7:103:14 | MyClass9<double> | 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 109, but it is not defined in terms of its opposite operator operator<. |
| 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 107, but it is not defined in terms of its opposite operator operator>=. |
| 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>. |
| 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<=. |
| 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 109, but it is not defined in terms of its opposite operator operator<. |