-
Notifications
You must be signed in to change notification settings - Fork 74
Move M5-14-1 implementation into shared query with Rule 8.14.1, increase test coverage. #1050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4b0b74e
df1a5f2
c3af72c
0822b06
2f1e920
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - `M5-14-1` - `RightHandOperandOfALogicalAndOperatorsContainSideEffects.ql`: | ||
| - Implementation has been refactor to share logic with Rule 8.14.1. No observable changes to results expected. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| cpp/common/test/rules/shortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.ql | ||
|
||
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
| import cpp | ||
| import RuleMetadata | ||
| import codingstandards.cpp.exclusions.RuleMetadata | ||
|
|
||
| newtype SideEffects5Query = TShortCircuitedPersistentSideEffectQuery() | ||
|
|
||
| predicate isSideEffects5QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `shortCircuitedPersistentSideEffect` query | ||
| SideEffects5Package::shortCircuitedPersistentSideEffectQuery() and | ||
| queryId = | ||
| // `@id` for the `shortCircuitedPersistentSideEffect` query | ||
| "cpp/misra/short-circuited-persistent-side-effect" and | ||
| ruleId = "RULE-8-14-1" and | ||
| category = "advisory" | ||
| } | ||
|
|
||
| module SideEffects5Package { | ||
| Query shortCircuitedPersistentSideEffectQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `shortCircuitedPersistentSideEffect` query | ||
| TQueryCPP(TSideEffects5PackageQuery(TShortCircuitedPersistentSideEffectQuery())) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** | ||
| * Provides a configurable module ShortCircuitedPersistentSideEffectShared with a `problems` predicate | ||
| * for the following issue: | ||
| * The right-hand operand of a logical && or || operator should not contain persistent | ||
| * side effects, as this may violate developer intent and expectations. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.Customizations | ||
| import codingstandards.cpp.Exclusions | ||
| import codingstandards.cpp.SideEffect | ||
| import codingstandards.cpp.sideeffect.DefaultEffects | ||
| import codingstandards.cpp.Expr | ||
|
|
||
| signature module ShortCircuitedPersistentSideEffectSharedConfigSig { | ||
| Query getQuery(); | ||
| } | ||
|
|
||
| module ShortCircuitedPersistentSideEffectShared< | ||
| ShortCircuitedPersistentSideEffectSharedConfigSig Config> | ||
| { | ||
| query predicate problems( | ||
| BinaryLogicalOperation op, string message, Expr rhs, string rhsDescription | ||
| ) { | ||
| not isExcluded(op, Config::getQuery()) and | ||
| rhs = op.getRightOperand() and | ||
| hasSideEffect(rhs) and | ||
| not rhs instanceof UnevaluatedExprExtension and | ||
| message = "The $@ may have a side effect that is not always evaluated." and | ||
| rhsDescription = "right-hand operand" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| | 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 | | ||
| | 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 | | ||
| | 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 | | ||
| | 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 | | ||
| | 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 | | ||
| | test.cpp:56:7:56:15 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:56:12:56:13 | call to f8 | right-hand operand | | ||
| | test.cpp:66:7:66:16 | ... \|\| ... | The $@ may have a side effect that is not always evaluated. | test.cpp:66:12:66:14 | call to f10 | right-hand operand | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // GENERATED FILE - DO NOT MODIFY | ||
| import codingstandards.cpp.rules.shortcircuitedpersistentsideeffectshared.ShortCircuitedPersistentSideEffectShared | ||
|
|
||
| module TestFileConfig implements ShortCircuitedPersistentSideEffectSharedConfigSig { | ||
| Query getQuery() { result instanceof TestQuery } | ||
| } | ||
|
|
||
| import ShortCircuitedPersistentSideEffectShared<TestFileConfig> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||
| int i = 0; | ||||||
|
|
||||||
| bool f1() { | ||||||
| i++; | ||||||
| return i == 1; | ||||||
| } | ||||||
|
|
||||||
| bool f2() { | ||||||
| int i = 0; | ||||||
| return i++ == 1; | ||||||
| } | ||||||
|
|
||||||
| bool f3(int &i) { | ||||||
| i++; | ||||||
| return i == 1; | ||||||
| } | ||||||
|
|
||||||
| void f4(bool b) { | ||||||
| int j = 0; | ||||||
| if (b || i++) { // NON_COMPLIANT | ||||||
| } | ||||||
|
|
||||||
| if (b || (j == i++)) { // NON_COMPLIANT | ||||||
| } | ||||||
|
|
||||||
| if (b || f1()) { // NON_COMPLIANT, f1 has global side-effects | ||||||
| } | ||||||
|
|
||||||
| if (b || f3(j)) { // NON_COMPLIANT, f3 has local side-effects | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| int g1 = 0; | ||||||
| int f5() { return g1++; } | ||||||
| int f6() { return 1; } | ||||||
|
|
||||||
| #include <typeinfo> | ||||||
|
|
||||||
| void f7() { | ||||||
| if (1 && sizeof(f5())) { | ||||||
| } // COMPLIANT - sizeof operands not evaluated | ||||||
| if (1 &&noexcept(f5()) &&noexcept(f5())) { | ||||||
| } // COMPLIANT - noexcept operands not evaluated | ||||||
|
|
||||||
| if (1 || (typeid(f6()) == typeid(f5()))) { | ||||||
| } // NON_COMPLIANT - typeid operands not evaluated, but the ==operator is | ||||||
|
||||||
| } // NON_COMPLIANT - typeid operands not evaluated, but the ==operator is | |
| } // NON_COMPLIANT - typeid operands are not evaluated, but the == operator is, so its operands are evaluated |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * @id cpp/misra/short-circuited-persistent-side-effect | ||
| * @name RULE-8-14-1: The right-hand operand of a logical && or || operator should not contain persistent side effects | ||
| * @description The right-hand operand of a logical && or || operator should not contain persistent | ||
| * side effects, as such side effects will only conditionally occur, which may violate | ||
| * developer intent and/or expectations. | ||
| * @kind problem | ||
| * @precision high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-8-14-1 | ||
| * scope/system | ||
| * correctness | ||
| * readability | ||
| * external/misra/enforcement/undecidable | ||
| * external/misra/obligation/advisory | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.rules.shortcircuitedpersistentsideeffectshared.ShortCircuitedPersistentSideEffectShared | ||
|
|
||
| module ShortCircuitedPersistentSideEffectConfig implements | ||
| ShortCircuitedPersistentSideEffectSharedConfigSig | ||
| { | ||
| Query getQuery() { result = SideEffects5Package::shortCircuitedPersistentSideEffectQuery() } | ||
| } | ||
|
|
||
| import ShortCircuitedPersistentSideEffectShared<ShortCircuitedPersistentSideEffectConfig> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| cpp/common/test/rules/shortcircuitedpersistentsideeffectshared/ShortCircuitedPersistentSideEffectShared.ql |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "MISRA-C++-2023": { | ||
| "RULE-8-14-1": { | ||
| "properties": { | ||
| "enforcement": "undecidable", | ||
|
Comment on lines
+1
to
+5
|
||
| "obligation": "advisory" | ||
| }, | ||
| "queries": [ | ||
| { | ||
| "description": "The right-hand operand of a logical && or || operator should not contain persistent side effects, as such side effects will only conditionally occur, which may violate developer intent and/or expectations.", | ||
| "kind": "problem", | ||
| "name": "The right-hand operand of a logical && or || operator should not contain persistent side effects", | ||
| "precision": "high", | ||
| "severity": "error", | ||
| "short_name": "ShortCircuitedPersistentSideEffect", | ||
| "shared_implementation_short_name": "ShortCircuitedPersistentSideEffectShared", | ||
| "tags": [ | ||
| "scope/system", | ||
| "correctness", | ||
| "readability" | ||
| ] | ||
| } | ||
| ], | ||
| "title": "The right-hand operand of a logical && or || operator should not contain persistent side effects" | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo/grammar: "has been refactor" should be "has been refactored".