File tree Expand file tree Collapse file tree 4 files changed +28
-5
lines changed
cpp/common/src/codingstandards/cpp/alertreporting Expand file tree Collapse file tree 4 files changed +28
-5
lines changed Original file line number Diff line number Diff line change 1414
1515import cpp
1616import codingstandards.c.misra
17+ import codingstandards.cpp.alertreporting.HoldsForAllInstances
1718import codingstandards.cpp.deadcode.UselessAssignments
1819
1920/**
@@ -39,6 +40,9 @@ class DeadOperation extends Expr {
3940 string description ;
4041
4142 DeadOperation ( ) {
43+ // Exclude cases nested within macro expansions, because the code may be "live" in other
44+ // expansions
45+ isNotWithinMacroExpansion ( this ) and
4246 exists ( ExprStmtExpr e |
4347 if exists ( getExplicitCast ( e ) )
4448 then
Original file line number Diff line number Diff line change 55| test.c:23:3:23:30 | (int)... | Cast operation is unused. |
66| test.c:24:3:24:25 | (int)... | Cast operation is unused. |
77| test.c:27:4:27:18 | call to no_side_effects | Result of operation is unused and has no side effects. |
8+ | test.c:37:3:37:27 | call to no_side_effects | Result of operation is unused and has no side effects. |
9+ | test.c:38:7:38:31 | call to no_side_effects | Result of operation is unused and has no side effects. |
Original file line number Diff line number Diff line change @@ -26,5 +26,17 @@ int test_dead_code(int x) {
2626 (may_have_side_effects ()); // COMPLIANT
2727 (no_side_effects (x )); // NON_COMPLIANT
2828
29+ #define FULL_STMT_NO_SIDE_EFFECTS no_side_effects(1);
30+ #define PART_STMT_NO_SIDE_EFFECTS no_side_effects(1)
31+ #define BLOCK_SOME_SIDE_EFFECTS \
32+ { \
33+ may_have_side_effects(); \
34+ no_side_effects(1); \
35+ }
36+
37+ FULL_STMT_NO_SIDE_EFFECTS // NON_COMPLIANT
38+ PART_STMT_NO_SIDE_EFFECTS ; // NON_COMPLIANT
39+ BLOCK_SOME_SIDE_EFFECTS ; // COMPLIANT
40+
2941 return live5 + live6 ; // COMPLIANT
3042}
Original file line number Diff line number Diff line change 2020import cpp
2121
2222/**
23- * Holds if the `Stmt ` `s ` is not within a macro expansion, i.e. generated by a macro, but not the
24- * outermost `Stmt` generated by the macro.
23+ * Holds if the `Element ` `e ` is not within a macro expansion, i.e. generated by a macro, but not
24+ * the outermost `Stmt` or `Expr ` generated by the macro.
2525 */
26- predicate isNotWithinMacroExpansion ( Stmt s ) {
27- not s .isInMacroExpansion ( )
26+ predicate isNotWithinMacroExpansion ( Element e ) {
27+ not e .isInMacroExpansion ( )
2828 or
2929 exists ( MacroInvocation mi |
30- mi .getStmt ( ) = s and
30+ mi .getStmt ( ) = e
31+ or
32+ mi .getExpr ( ) = e
33+ or
34+ mi .getStmt ( ) .( ExprStmt ) .getExpr ( ) = e
35+ |
3136 not exists ( mi .getParentInvocation ( ) )
3237 )
3338}
You can’t perform that action at this time.
0 commit comments