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 14
14
15
15
import cpp
16
16
import codingstandards.c.misra
17
+ import codingstandards.cpp.alertreporting.HoldsForAllInstances
17
18
import codingstandards.cpp.deadcode.UselessAssignments
18
19
19
20
/**
@@ -39,6 +40,9 @@ class DeadOperation extends Expr {
39
40
string description ;
40
41
41
42
DeadOperation ( ) {
43
+ // Exclude cases nested within macro expansions, because the code may be "live" in other
44
+ // expansions
45
+ isNotWithinMacroExpansion ( this ) and
42
46
exists ( ExprStmtExpr e |
43
47
if exists ( getExplicitCast ( e ) )
44
48
then
Original file line number Diff line number Diff line change 5
5
| test.c:23:3:23:30 | (int)... | Cast operation is unused. |
6
6
| test.c:24:3:24:25 | (int)... | Cast operation is unused. |
7
7
| 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) {
26
26
(may_have_side_effects ()); // COMPLIANT
27
27
(no_side_effects (x )); // NON_COMPLIANT
28
28
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
+
29
41
return live5 + live6 ; // COMPLIANT
30
42
}
Original file line number Diff line number Diff line change 20
20
import cpp
21
21
22
22
/**
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.
25
25
*/
26
- predicate isNotWithinMacroExpansion ( Stmt s ) {
27
- not s .isInMacroExpansion ( )
26
+ predicate isNotWithinMacroExpansion ( Element e ) {
27
+ not e .isInMacroExpansion ( )
28
28
or
29
29
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
+ |
31
36
not exists ( mi .getParentInvocation ( ) )
32
37
)
33
38
}
You can’t perform that action at this time.
0 commit comments