Skip to content

Commit 4b0b74e

Browse files
Move M5-14-1 implementation into shared query with Rule 8.14.1, increase test coverage.
1 parent b0e343f commit 4b0b74e

16 files changed

+213
-58
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M5-14-1` - `RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql`:
2+
- Implementation has been refactor to share logic with Rule 8.14.1. No observable changes to results expected.

cpp/autosar/src/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616

1717
import cpp
1818
import codingstandards.cpp.autosar
19-
import codingstandards.cpp.SideEffect
20-
import codingstandards.cpp.sideeffect.DefaultEffects
21-
import codingstandards.cpp.Expr
19+
import codingstandards.cpp.rules.shortcircuitedpersistentsideeffectshared.ShortCircuitedPersistentSideEffectShared
2220

23-
from BinaryLogicalOperation op, Expr rhs
24-
where
25-
not isExcluded(op,
26-
SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()) and
27-
rhs = op.getRightOperand() and
28-
hasSideEffect(rhs) and
29-
not rhs instanceof UnevaluatedExprExtension
30-
select op, "The $@ may have a side effect that is not always evaluated.", rhs, "right-hand operand"
21+
module RightHandOperandOfALogicalAndOperatorsContainSideEffectsConfig implements
22+
ShortCircuitedPersistentSideEffectSharedConfigSig
23+
{
24+
Query getQuery() {
25+
result = SideEffects1Package::rightHandOperandOfALogicalAndOperatorsContainSideEffectsQuery()
26+
}
27+
}
28+
29+
import ShortCircuitedPersistentSideEffectShared<RightHandOperandOfALogicalAndOperatorsContainSideEffectsConfig>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/shortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.ql

cpp/autosar/test/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.expected

Lines changed: 0 additions & 4 deletions
This file was deleted.

cpp/autosar/test/rules/M5-14-1/RightHandOperandOfALogicalAndOperatorsContainSideEffects.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.

cpp/autosar/test/rules/M5-14-1/test.cpp

Lines changed: 0 additions & 42 deletions
This file was deleted.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import Representation
6161
import Scope
6262
import SideEffects1
6363
import SideEffects2
64+
import SideEffects5
6465
import SmartPointers1
6566
import SmartPointers2
6667
import Statements
@@ -134,6 +135,7 @@ newtype TCPPQuery =
134135
TScopePackageQuery(ScopeQuery q) or
135136
TSideEffects1PackageQuery(SideEffects1Query q) or
136137
TSideEffects2PackageQuery(SideEffects2Query q) or
138+
TSideEffects5PackageQuery(SideEffects5Query q) or
137139
TSmartPointers1PackageQuery(SmartPointers1Query q) or
138140
TSmartPointers2PackageQuery(SmartPointers2Query q) or
139141
TStatementsPackageQuery(StatementsQuery q) or
@@ -207,6 +209,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
207209
isScopeQueryMetadata(query, queryId, ruleId, category) or
208210
isSideEffects1QueryMetadata(query, queryId, ruleId, category) or
209211
isSideEffects2QueryMetadata(query, queryId, ruleId, category) or
212+
isSideEffects5QueryMetadata(query, queryId, ruleId, category) or
210213
isSmartPointers1QueryMetadata(query, queryId, ruleId, category) or
211214
isSmartPointers2QueryMetadata(query, queryId, ruleId, category) or
212215
isStatementsQueryMetadata(query, queryId, ruleId, category) or
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype SideEffects5Query = TShortCircuitedPersistentSideEffectQuery()
7+
8+
predicate isSideEffects5QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `shortCircuitedPersistentSideEffect` query
11+
SideEffects5Package::shortCircuitedPersistentSideEffectQuery() and
12+
queryId =
13+
// `@id` for the `shortCircuitedPersistentSideEffect` query
14+
"cpp/misra/short-circuited-persistent-side-effect" and
15+
ruleId = "RULE-8-14-1" and
16+
category = "advisory"
17+
}
18+
19+
module SideEffects5Package {
20+
Query shortCircuitedPersistentSideEffectQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `shortCircuitedPersistentSideEffect` query
24+
TQueryCPP(TSideEffects5PackageQuery(TShortCircuitedPersistentSideEffectQuery()))
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Provides a configurable module ShortCircuitedPersistentSideEffectShared with a `problems` predicate
3+
* for the following issue:
4+
* The right-hand operand of a logical && or || operator should not contain persistent
5+
* side effects, as this may violate developer intent and expectations.
6+
*/
7+
8+
import cpp
9+
import codingstandards.cpp.Customizations
10+
import codingstandards.cpp.Exclusions
11+
import codingstandards.cpp.SideEffect
12+
import codingstandards.cpp.sideeffect.DefaultEffects
13+
import codingstandards.cpp.Expr
14+
15+
signature module ShortCircuitedPersistentSideEffectSharedConfigSig {
16+
Query getQuery();
17+
}
18+
19+
module ShortCircuitedPersistentSideEffectShared<
20+
ShortCircuitedPersistentSideEffectSharedConfigSig Config>
21+
{
22+
query predicate problems(
23+
BinaryLogicalOperation op, string message, Expr rhs, string rhsDescription
24+
) {
25+
not isExcluded(op, Config::getQuery()) and
26+
rhs = op.getRightOperand() and
27+
hasSideEffect(rhs) and
28+
not rhs instanceof UnevaluatedExprExtension and
29+
message = "The $@ may have a side effect that is not always evaluated." and
30+
rhsDescription = "right-hand operand"
31+
}
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| test.cpp:20:7:20:14 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:20:12:20:14 | ... ++ | right-hand operand |
2+
| test.cpp:23:7:23:21 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:23:13:23:20 | ... == ... | right-hand operand |
3+
| test.cpp:26:7:26:15 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:26:12:26:13 | call to f1 | right-hand operand |
4+
| test.cpp:29:7:29:16 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:29:12:29:13 | call to f3 | right-hand operand |
5+
| test.cpp:45:7:45:41 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:45:26:45:26 | call to operator== | right-hand operand |
6+
| test.cpp:56:9:56:17 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:56:14:56:15 | call to f8 | right-hand operand |
7+
| test.cpp:66:9:66:18 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:66:14:66:16 | call to f10 | right-hand operand |

0 commit comments

Comments
 (0)