Skip to content

Commit 8bfb9c8

Browse files
committed
DeadCode: Add MISRA C 2012 Rule 2.6
Adds a query to find unused labels. We consider a label unused if we never "goto" the label and never take the address of the label.
1 parent cce11f4 commit 8bfb9c8

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @id c/misra/unused-label-declaration
3+
* @name RULE-2-6: A function should not contain unused label declarations
4+
* @description Unused label declarations are either redundant or indicate a possible mistake on the
5+
* part of the programmer.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-2-6
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
18+
from LabelStmt label
19+
where
20+
not isExcluded(label, DeadCodePackage::unusedLabelDeclarationQuery()) and
21+
// No GotoStmt jumps to this label
22+
not exists(GotoStmt gs | gs.hasName() and gs.getTarget() = label) and
23+
// The address of the label is never taken
24+
not exists(LabelLiteral literal | literal.getLabel() = label)
25+
select label, "Label " + label.getName() + " is unused."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:2:1:2:13 | label ...: | Label dead_label_1 is unused. |
2+
| test.c:6:1:6:13 | label ...: | Label dead_label_2 is unused. |
3+
| test.c:8:1:8:13 | label ...: | Label dead_label_3 is unused. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-2-6/UnusedLabelDeclaration.ql

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
void test1(int p1) {
2+
dead_label_1: // NON_COMPLIANT
3+
live_label_1: // COMPLIANT
4+
int x = 0;
5+
live_label_2: // COMPLIANT
6+
dead_label_2: // NON_COMPLIANT
7+
int y = 0;
8+
dead_label_3: // NON_COMPLIANT
9+
int z = 0;
10+
11+
if (p1 > 1) {
12+
goto live_label_1;
13+
}
14+
15+
// Taking the address of a label is sufficient to make it "live"
16+
void *label_ptr = &&live_label_2;
17+
}

0 commit comments

Comments
 (0)