Skip to content

Commit 5b810c3

Browse files
committed
Rule 7.0.1: Address review issues
- Simplify query implementation - Format test code
1 parent 1d2aa2e commit 5b810c3

File tree

4 files changed

+139
-120
lines changed

4 files changed

+139
-120
lines changed

cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Comments
1212
import Concurrency
1313
import Conditionals
1414
import Const
15+
import Conversions
1516
import DeadCode
1617
import Declarations
1718
import ExceptionSafety
@@ -67,6 +68,7 @@ newtype TCPPQuery =
6768
TConcurrencyPackageQuery(ConcurrencyQuery q) or
6869
TConditionalsPackageQuery(ConditionalsQuery q) or
6970
TConstPackageQuery(ConstQuery q) or
71+
TConversionsPackageQuery(ConversionsQuery q) or
7072
TDeadCodePackageQuery(DeadCodeQuery q) or
7173
TDeclarationsPackageQuery(DeclarationsQuery q) or
7274
TExceptionSafetyPackageQuery(ExceptionSafetyQuery q) or
@@ -122,6 +124,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
122124
isConcurrencyQueryMetadata(query, queryId, ruleId, category) or
123125
isConditionalsQueryMetadata(query, queryId, ruleId, category) or
124126
isConstQueryMetadata(query, queryId, ruleId, category) or
127+
isConversionsQueryMetadata(query, queryId, ruleId, category) or
125128
isDeadCodeQueryMetadata(query, queryId, ruleId, category) or
126129
isDeclarationsQueryMetadata(query, queryId, ruleId, category) or
127130
isExceptionSafetyQueryMetadata(query, queryId, ruleId, category) or

cpp/misra/src/rules/RULE-7-0-1/NoConversionFromBool.ql

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,19 @@ where
2525
// Exclude cases that are explicitly allowed
2626
not (
2727
// Exception: equality operators with both bool operands
28-
exists(EQExpr eq |
28+
exists(EqualityOperation eq |
2929
eq.getAnOperand() = e and
3030
eq.getLeftOperand().getType().stripTopLevelSpecifiers() instanceof BoolType and
3131
eq.getRightOperand().getType().stripTopLevelSpecifiers() instanceof BoolType
32-
) or
33-
exists(NEExpr ne |
34-
ne.getAnOperand() = e and
35-
ne.getLeftOperand().getType().stripTopLevelSpecifiers() instanceof BoolType and
36-
ne.getRightOperand().getType().stripTopLevelSpecifiers() instanceof BoolType
37-
) or
32+
)
33+
or
3834
// Exception: explicit constructor calls
39-
exists(ConstructorCall cc | cc.getAnArgument() = e) or
35+
exists(ConstructorCall cc | cc.getAnArgument() = e)
36+
or
4037
// Exception: assignment to bit-field of length 1
4138
exists(AssignExpr assign |
4239
assign.getRValue() = e and
43-
assign.getLValue().(ValueFieldAccess).getTarget() instanceof BitField and
4440
assign.getLValue().(ValueFieldAccess).getTarget().(BitField).getNumBits() = 1
4541
)
4642
)
47-
select e, "Conversion from 'bool' to '" + conv.getType().toString() + "'."
43+
select e, "Conversion from 'bool' to '" + conv.getType().toString() + "'."
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
| test.cpp:23:9:23:10 | b1 | Conversion from 'bool' to 'int'. |
2-
| test.cpp:23:14:23:15 | b2 | Conversion from 'bool' to 'int'. |
3-
| test.cpp:24:9:24:10 | b1 | Conversion from 'bool' to 'int'. |
4-
| test.cpp:24:14:24:15 | b2 | Conversion from 'bool' to 'int'. |
5-
| test.cpp:25:9:25:10 | b1 | Conversion from 'bool' to 'int'. |
6-
| test.cpp:25:14:25:15 | b2 | Conversion from 'bool' to 'int'. |
7-
| test.cpp:26:10:26:11 | b1 | Conversion from 'bool' to 'int'. |
8-
| test.cpp:29:9:29:10 | b1 | Conversion from 'bool' to 'int'. |
9-
| test.cpp:29:14:29:15 | b2 | Conversion from 'bool' to 'int'. |
10-
| test.cpp:30:9:30:10 | b1 | Conversion from 'bool' to 'int'. |
11-
| test.cpp:30:14:30:15 | b2 | Conversion from 'bool' to 'int'. |
12-
| test.cpp:31:9:31:10 | b1 | Conversion from 'bool' to 'int'. |
13-
| test.cpp:31:15:31:16 | b2 | Conversion from 'bool' to 'int'. |
14-
| test.cpp:32:9:32:10 | b1 | Conversion from 'bool' to 'int'. |
15-
| test.cpp:32:15:32:16 | b2 | Conversion from 'bool' to 'int'. |
16-
| test.cpp:35:9:35:10 | b1 | Conversion from 'bool' to 'int'. |
17-
| test.cpp:36:9:36:10 | b1 | Conversion from 'bool' to 'int'. |
18-
| test.cpp:37:9:37:10 | b1 | Conversion from 'bool' to 'int'. |
19-
| test.cpp:40:22:40:23 | b1 | Conversion from 'bool' to 'double'. |
20-
| test.cpp:41:22:41:23 | b1 | Conversion from 'bool' to 'double'. |
21-
| test.cpp:42:30:42:31 | b1 | Conversion from 'bool' to 'int'. |
22-
| test.cpp:45:36:45:37 | b1 | Conversion from 'bool' to 'int8_t'. |
23-
| test.cpp:46:38:46:39 | b1 | Conversion from 'bool' to 'int32_t'. |
24-
| test.cpp:49:8:49:9 | b1 | Conversion from 'bool' to 'int32_t'. |
25-
| test.cpp:50:8:50:9 | b1 | Conversion from 'bool' to 'double'. |
26-
| test.cpp:53:13:53:14 | b1 | Conversion from 'bool' to 'int'. |
27-
| test.cpp:59:11:59:12 | b1 | Conversion from 'bool' to 'int8_t'. |
28-
| test.cpp:60:12:60:13 | b1 | Conversion from 'bool' to 'int32_t'. |
29-
| test.cpp:61:10:61:11 | b1 | Conversion from 'bool' to 'double'. |
1+
| test.cpp:23:7:23:8 | b1 | Conversion from 'bool' to 'int'. |
2+
| test.cpp:23:12:23:13 | b2 | Conversion from 'bool' to 'int'. |
3+
| test.cpp:25:7:25:8 | b1 | Conversion from 'bool' to 'int'. |
4+
| test.cpp:25:12:25:13 | b2 | Conversion from 'bool' to 'int'. |
5+
| test.cpp:27:7:27:8 | b1 | Conversion from 'bool' to 'int'. |
6+
| test.cpp:27:12:27:13 | b2 | Conversion from 'bool' to 'int'. |
7+
| test.cpp:29:8:29:9 | b1 | Conversion from 'bool' to 'int'. |
8+
| test.cpp:33:7:33:8 | b1 | Conversion from 'bool' to 'int'. |
9+
| test.cpp:33:12:33:13 | b2 | Conversion from 'bool' to 'int'. |
10+
| test.cpp:35:7:35:8 | b1 | Conversion from 'bool' to 'int'. |
11+
| test.cpp:35:12:35:13 | b2 | Conversion from 'bool' to 'int'. |
12+
| test.cpp:37:7:37:8 | b1 | Conversion from 'bool' to 'int'. |
13+
| test.cpp:37:13:37:14 | b2 | Conversion from 'bool' to 'int'. |
14+
| test.cpp:39:7:39:8 | b1 | Conversion from 'bool' to 'int'. |
15+
| test.cpp:39:13:39:14 | b2 | Conversion from 'bool' to 'int'. |
16+
| test.cpp:43:7:43:8 | b1 | Conversion from 'bool' to 'int'. |
17+
| test.cpp:45:7:45:8 | b1 | Conversion from 'bool' to 'int'. |
18+
| test.cpp:47:7:47:8 | b1 | Conversion from 'bool' to 'int'. |
19+
| test.cpp:51:20:51:21 | b1 | Conversion from 'bool' to 'double'. |
20+
| test.cpp:52:20:52:21 | b1 | Conversion from 'bool' to 'double'. |
21+
| test.cpp:53:28:53:29 | b1 | Conversion from 'bool' to 'int'. |
22+
| test.cpp:56:34:56:35 | b1 | Conversion from 'bool' to 'int8_t'. |
23+
| test.cpp:57:36:57:37 | b1 | Conversion from 'bool' to 'int32_t'. |
24+
| test.cpp:60:6:60:7 | b1 | Conversion from 'bool' to 'int32_t'. |
25+
| test.cpp:61:6:61:7 | b1 | Conversion from 'bool' to 'double'. |
26+
| test.cpp:64:11:64:12 | b1 | Conversion from 'bool' to 'int'. |
27+
| test.cpp:72:9:72:10 | b1 | Conversion from 'bool' to 'int8_t'. |
28+
| test.cpp:73:10:73:11 | b1 | Conversion from 'bool' to 'int32_t'. |
29+
| test.cpp:74:8:74:9 | b1 | Conversion from 'bool' to 'double'. |
Lines changed: 101 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,117 @@
11
#include <cstdint>
22

33
struct A {
4-
explicit A(bool) {}
4+
explicit A(bool) {}
55
};
66

77
struct BitField {
8-
std::uint8_t bit : 1;
8+
std::uint8_t bit : 1;
99
};
1010

1111
void f1(std::int32_t n) {}
1212
void f2(double d) {}
1313

1414
void test_bool_conversion_violations() {
15-
bool b1 = true;
16-
bool b2 = false;
17-
double d1 = 1.0;
18-
std::int8_t s8a = 0;
19-
std::int32_t s32a = 0;
20-
BitField bf;
21-
22-
// Bitwise operations - non-compliant
23-
if (b1 & b2) {} // NON_COMPLIANT
24-
if (b1 | b2) {} // NON_COMPLIANT
25-
if (b1 ^ b2) {} // NON_COMPLIANT
26-
if (~b1) {} // NON_COMPLIANT
27-
28-
// Relational operations - non-compliant
29-
if (b1 < b2) {} // NON_COMPLIANT
30-
if (b1 > b2) {} // NON_COMPLIANT
31-
if (b1 <= b2) {} // NON_COMPLIANT
32-
if (b1 >= b2) {} // NON_COMPLIANT
33-
34-
// Comparison with integer literals - non-compliant
35-
if (b1 == 0) {} // NON_COMPLIANT
36-
if (b1 == 1) {} // NON_COMPLIANT
37-
if (b1 != 0) {} // NON_COMPLIANT
38-
39-
// Arithmetic operations - non-compliant
40-
double l1 = d1 * b1; // NON_COMPLIANT
41-
double l2 = d1 + b1; // NON_COMPLIANT
42-
std::int32_t l3 = s32a + b1; // NON_COMPLIANT
43-
44-
// Explicit casts to integral types - non-compliant
45-
s8a = static_cast<std::int8_t>(b1); // NON_COMPLIANT
46-
s32a = static_cast<std::int32_t>(b1); // NON_COMPLIANT
47-
48-
// Function parameter conversion - non-compliant
49-
f1(b1); // NON_COMPLIANT
50-
f2(b1); // NON_COMPLIANT
51-
52-
// Switch statement - non-compliant
53-
switch (b1) { // NON_COMPLIANT
54-
case 0: break;
55-
case 1: break;
56-
}
57-
58-
// Assignment to integral types - non-compliant
59-
s8a = b1; // NON_COMPLIANT
60-
s32a = b1; // NON_COMPLIANT
61-
d1 = b1; // NON_COMPLIANT
15+
bool b1 = true;
16+
bool b2 = false;
17+
double d1 = 1.0;
18+
std::int8_t s8a = 0;
19+
std::int32_t s32a = 0;
20+
BitField bf;
21+
22+
// Bitwise operations - non-compliant
23+
if (b1 & b2) { // NON_COMPLIANT
24+
}
25+
if (b1 | b2) { // NON_COMPLIANT
26+
}
27+
if (b1 ^ b2) { // NON_COMPLIANT
28+
}
29+
if (~b1) { // NON_COMPLIANT
30+
}
31+
32+
// Relational operations - non-compliant
33+
if (b1 < b2) { // NON_COMPLIANT
34+
}
35+
if (b1 > b2) { // NON_COMPLIANT
36+
}
37+
if (b1 <= b2) { // NON_COMPLIANT
38+
}
39+
if (b1 >= b2) { // NON_COMPLIANT
40+
}
41+
42+
// Comparison with integer literals - non-compliant
43+
if (b1 == 0) { // NON_COMPLIANT
44+
}
45+
if (b1 == 1) { // NON_COMPLIANT
46+
}
47+
if (b1 != 0) { // NON_COMPLIANT
48+
}
49+
50+
// Arithmetic operations - non-compliant
51+
double l1 = d1 * b1; // NON_COMPLIANT
52+
double l2 = d1 + b1; // NON_COMPLIANT
53+
std::int32_t l3 = s32a + b1; // NON_COMPLIANT
54+
55+
// Explicit casts to integral types - non-compliant
56+
s8a = static_cast<std::int8_t>(b1); // NON_COMPLIANT
57+
s32a = static_cast<std::int32_t>(b1); // NON_COMPLIANT
58+
59+
// Function parameter conversion - non-compliant
60+
f1(b1); // NON_COMPLIANT
61+
f2(b1); // NON_COMPLIANT
62+
63+
// Switch statement - non-compliant
64+
switch (b1) { // NON_COMPLIANT
65+
case 0:
66+
break;
67+
case 1:
68+
break;
69+
}
70+
71+
// Assignment to integral types - non-compliant
72+
s8a = b1; // NON_COMPLIANT
73+
s32a = b1; // NON_COMPLIANT
74+
d1 = b1; // NON_COMPLIANT
6275
}
6376

6477
void test_bool_conversion_compliant() {
65-
bool b1 = true;
66-
bool b2 = false;
67-
std::int8_t s8a = 0;
68-
BitField bf;
69-
70-
// Boolean equality operations - compliant
71-
if (b1 == false) {} // COMPLIANT
72-
if (b1 == true) {} // COMPLIANT
73-
if (b1 == b2) {} // COMPLIANT
74-
if (b1 != b2) {} // COMPLIANT
75-
76-
// Logical operations - compliant
77-
if (b1 && b2) {} // COMPLIANT
78-
if (b1 || b2) {} // COMPLIANT
79-
if (!b1) {} // COMPLIANT
80-
81-
// Conditional operator without conversion - compliant
82-
s8a = b1 ? 3 : 7; // COMPLIANT
83-
84-
// Function parameter without conversion - compliant
85-
f1(b1 ? 1 : 0); // COMPLIANT
86-
87-
// Explicit constructor calls - compliant
88-
A l1{true}; // COMPLIANT
89-
A l2(false); // COMPLIANT
90-
A l3 = static_cast<A>(true); // COMPLIANT
91-
92-
// Assignment to constructor - compliant
93-
A l4 = A{false}; // COMPLIANT
94-
95-
// Bit-field assignment exception - compliant
96-
bf.bit = b1; // COMPLIANT
78+
bool b1 = true;
79+
bool b2 = false;
80+
std::int8_t s8a = 0;
81+
BitField bf;
82+
83+
// Boolean equality operations - compliant
84+
if (b1 == false) { // COMPLIANT
85+
}
86+
if (b1 == true) { // COMPLIANT
87+
}
88+
if (b1 == b2) { // COMPLIANT
89+
}
90+
if (b1 != b2) { // COMPLIANT
91+
}
92+
93+
// Logical operations - compliant
94+
if (b1 && b2) { // COMPLIANT
95+
}
96+
if (b1 || b2) { // COMPLIANT
97+
}
98+
if (!b1) { // COMPLIANT
99+
}
100+
101+
// Conditional operator without conversion - compliant
102+
s8a = b1 ? 3 : 7; // COMPLIANT
103+
104+
// Function parameter without conversion - compliant
105+
f1(b1 ? 1 : 0); // COMPLIANT
106+
107+
// Explicit constructor calls - compliant
108+
A l1{true}; // COMPLIANT
109+
A l2(false); // COMPLIANT
110+
A l3 = static_cast<A>(true); // COMPLIANT
111+
112+
// Assignment to constructor - compliant
113+
A l4 = A{false}; // COMPLIANT
114+
115+
// Bit-field assignment exception - compliant
116+
bf.bit = b1; // COMPLIANT
97117
}

0 commit comments

Comments
 (0)