Skip to content

Commit 85b161a

Browse files
committed
RULE-2-2: Exclude cases nested in macro expansions
1 parent 783f8d4 commit 85b161a

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

c/misra/src/rules/RULE-2-2/DeadCode.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import cpp
1616
import codingstandards.c.misra
17+
import codingstandards.cpp.alertreporting.HoldsForAllInstances
1718
import 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

c/misra/test/rules/RULE-2-2/DeadCode.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
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. |

c/misra/test/rules/RULE-2-2/test.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

cpp/common/src/codingstandards/cpp/alertreporting/HoldsForAllInstances.qll

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020
import 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
}

0 commit comments

Comments
 (0)