-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #548 from rvermeulen/rvermeulen/fix-152
Fix 152: Adding query for depencence on operator precedence
- Loading branch information
Showing
7 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
cpp/autosar/src/rules/M5-0-2/InsufficientUseOfParentheses.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @id cpp/autosar/insufficient-use-of-parentheses | ||
* @name M5-0-2: Limited dependence should be placed on C++ operator precedence rules in expressions | ||
* @description The use of parentheses can be used to emphasize precedence and increase code | ||
* readability. | ||
* @kind problem | ||
* @precision medium | ||
* @problem.severity recommendation | ||
* @tags external/autosar/id/m5-0-2 | ||
* external/autosar/audit | ||
* readability | ||
* external/autosar/allocated-target/implementation | ||
* external/autosar/enforcement/partially-automated | ||
* external/autosar/obligation/advisory | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.cpp.autosar | ||
import semmle.code.cpp.commons.Assertions | ||
|
||
class InsufficientlyParenthesizedExpr extends Expr { | ||
InsufficientlyParenthesizedExpr() { | ||
// Exclude expressions affected by macros, including assertions, because | ||
// it is unclear that the expression must be parenthesized since it seems | ||
// to be the top-level expression instead of an operand of a binary or ternary operation. | ||
not this.isAffectedByMacro() and | ||
( | ||
exists(BinaryOperation root, BinaryOperation child | child = this | | ||
root.getAnOperand() = child and | ||
root.getOperator() != child.getOperator() and | ||
not any(ParenthesisExpr pe).getExpr() = child | ||
) | ||
or | ||
exists(ConditionalExpr root, BinaryOperation child | child = this | | ||
root.getAnOperand() = child and | ||
not any(ParenthesisExpr pe).getExpr() = child | ||
) | ||
) | ||
} | ||
} | ||
|
||
from InsufficientlyParenthesizedExpr e | ||
where not isExcluded(e, OrderOfEvaluationPackage::insufficientUseOfParenthesesQuery()) | ||
select e, "Dependence on operator precedence rules." |
8 changes: 8 additions & 0 deletions
8
cpp/autosar/test/rules/M5-0-2/InsufficientUseOfParentheses.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
| test.cpp:40:8:40:13 | ... * ... | Dependence on operator precedence rules. | | ||
| test.cpp:41:19:41:24 | ... * ... | Dependence on operator precedence rules. | | ||
| test.cpp:42:8:42:13 | ... * ... | Dependence on operator precedence rules. | | ||
| test.cpp:42:17:42:22 | ... * ... | Dependence on operator precedence rules. | | ||
| test.cpp:48:8:48:15 | ... == ... | Dependence on operator precedence rules. | | ||
| test.cpp:49:26:49:32 | ... - ... | Dependence on operator precedence rules. | | ||
| test.cpp:50:8:50:15 | ... == ... | Dependence on operator precedence rules. | | ||
| test.cpp:50:24:50:30 | ... - ... | Dependence on operator precedence rules. | |
1 change: 1 addition & 0 deletions
1
cpp/autosar/test/rules/M5-0-2/InsufficientUseOfParentheses.qlref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rules/M5-0-2/InsufficientUseOfParentheses.ql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters