Skip to content

Commit e450b40

Browse files
committed
Merge remote-tracking branch 'origin/jsinglet/automation-check-shared-rules' into jsinglet/automation-check-shared-rules
2 parents 6befae3 + 7f3bbbb commit e450b40

File tree

70 files changed

+1081
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1081
-197
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
| test.c:18:3:18:27 | declaration | This statement is dead code. |
2+
| test.c:19:3:19:12 | ExprStmt | This statement is dead code. |
3+
| test.c:20:3:20:12 | ExprStmt | This statement is dead code. |
4+
| test.c:22:3:24:3 | if (...) ... | This statement is dead code. |
5+
| test.c:34:3:35:3 | if (...) ... | This statement is dead code. |
6+
| test.c:37:3:37:4 | { ... } | This statement is dead code. |
7+
| test.c:38:3:40:3 | { ... } | This statement is dead code. |
8+
| test.c:54:6:55:3 | { ... } | This statement is dead code. |
9+
| test.c:65:46:66:3 | { ... } | This statement is dead code. |
10+
| test.c:69:3:69:8 | ExprStmt | This statement is dead code. |
11+
| test.c:71:3:71:21 | ExprStmt | This statement is dead code. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.deadcode.DeadCode

c/common/test/rules/deadcode/test.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C++ TEST CASE AND
2+
// CHANGES SHOULD BE REFLECTED THERE AS WELL.
3+
#include <stdbool.h>
4+
5+
int may_have_side_effects();
6+
int no_side_effects(int x) { return 1 + 2; }
7+
int no_side_effects_nondeterministic();
8+
9+
int test_dead_code(int x) {
10+
int live1 = may_have_side_effects(),
11+
live2 = may_have_side_effects(); // COMPLIANT
12+
int live3 = 0,
13+
live4 = may_have_side_effects(); // COMPLIANT
14+
int live5 = 0, live6 = 0; // COMPLIANT
15+
live5 = 1; // COMPLIANT
16+
live6 = 2; // COMPLIANT
17+
18+
int dead1 = 0, dead2 = 0; // NON_COMPLIANT
19+
dead1 = 1; // NON_COMPLIANT - useless assignment
20+
dead2 = 1; // NON_COMPLIANT - useless assignment
21+
22+
if (false) { // NON_COMPLIANT
23+
dead2 = 10; // Only used in dead or unreachable code
24+
}
25+
26+
if (true) { // COMPLIANT
27+
may_have_side_effects();
28+
}
29+
30+
if (may_have_side_effects()) { // COMPLIANT
31+
may_have_side_effects();
32+
}
33+
34+
if (true) { // NON_COMPLIANT
35+
}
36+
37+
{} // NON_COMPLIANT
38+
{ // NON_COMPLIANT
39+
1 + 2;
40+
}
41+
42+
{ // COMPLIANT
43+
may_have_side_effects();
44+
}
45+
46+
do { // COMPLIANT
47+
may_have_side_effects();
48+
} while (may_have_side_effects());
49+
50+
do { // COMPLIANT
51+
may_have_side_effects();
52+
} while (may_have_side_effects());
53+
54+
do { // NON_COMPLIANT
55+
} while (no_side_effects_nondeterministic());
56+
57+
while (may_have_side_effects()) { // COMPLIANT
58+
may_have_side_effects();
59+
}
60+
61+
while (may_have_side_effects()) { // COMPLIANT
62+
may_have_side_effects();
63+
}
64+
65+
while (no_side_effects_nondeterministic()) { // NON_COMPLIANT
66+
}
67+
68+
may_have_side_effects(); // COMPLIANT
69+
1 + 2; // NON_COMPLIANT
70+
71+
no_side_effects(x); // NON_COMPLIANT
72+
73+
return live5 + live6; // COMPLIANT
74+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:17:3:17:12 | declaration | This statement in function $@ is unreachable. | test.c:15:5:15:21 | test_after_return | test_after_return |
2+
| test.c:21:10:22:12 | { ... } | This statement in function $@ is unreachable. | test.c:20:5:20:27 | test_constant_condition | test_constant_condition |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.unreachablecode.UnreachableCode
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C++ TEST CASE AND
2+
// CHANGES SHOULD BE REFLECTED THERE AS WELL.
3+
4+
void test_switch(int p1) {
5+
int l1 = 0;
6+
switch (p1) {
7+
l1 = p1; // NON_COMPLIANT[FALSE_NEGATIVE]
8+
case 1:
9+
break;
10+
default:
11+
break;
12+
}
13+
}
14+
15+
int test_after_return() {
16+
return 0;
17+
int l1 = 0; // NON_COMPLIANT - function has returned by this point
18+
}
19+
20+
int test_constant_condition() {
21+
if (0) { // NON_COMPLIANT
22+
return 1;
23+
} else { // COMPLIANT
24+
return 2;
25+
}
26+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:6:22:6:22 | x | Unused parameter 'x' for function $@. | test.c:6:6:6:16 | test_unused | test_unused |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.unusedparameter.UnusedParameter
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C++ TEST CASE AND
2+
// CHANGES SHOULD BE REFLECTED THERE AS WELL.
3+
4+
int test_used(int x) { return x; } // COMPLIANT
5+
6+
void test_unused(int x) {} // NON_COMPLIANT
7+
8+
void test_no_def(int x); // COMPLIANT - no definition, so cannot be "unused"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| file://:0:0:0:0 | __va_list_tag | Type declaration __va_list_tag is not used. |
2+
| test.c:4:8:4:8 | A | Type declaration A is not used. |
3+
| test.c:7:18:7:18 | D | Type declaration D is not used. |
4+
| test.c:28:11:28:11 | R | Type declaration R is not used. |
5+
| test.c:41:12:41:12 | struct <unnamed> | Type declaration struct <unnamed> is not used. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.unusedtypedeclarations.UnusedTypeDeclarations
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C++ TEST CASE AND
2+
// CHANGES SHOULD BE REFLECTED THERE AS WELL.
3+
4+
struct A {}; // NON_COMPLIANT - unused
5+
6+
struct C {}; // COMPLIANT - used in the type def
7+
typedef struct C D; // NON_COMPLIANT - typedef itself not used
8+
9+
struct F {}; // COMPLIANT - used as a global function return type
10+
11+
struct F test_return_value() {
12+
struct F f;
13+
return f;
14+
}
15+
16+
struct G {}; // COMPLIANT - used as a global function parameter type
17+
18+
void test_global_function(struct G g) {}
19+
20+
enum M { C1, C2, C3 }; // COMPLIANT - used in an enum type access below
21+
22+
void test_enum_access() { int i = C1; }
23+
24+
struct O {}; // COMPLIANT - used in typedef below
25+
26+
typedef struct O P; // COMPLIANT - used in typedef below
27+
typedef P Q; // COMPLIANT - used in function below
28+
typedef Q R; // NON_COMPLIANT - never used
29+
30+
Q test_type_def() {}
31+
32+
struct { // COMPLIANT - used in type definition
33+
union { // COMPLIANT - f1 and f3 is accessed
34+
struct { // COMPLIANT - f1 is accessed
35+
int f1;
36+
};
37+
struct { // COMPLIANT - f3 is accessed
38+
float f2;
39+
float f3;
40+
};
41+
struct { // NON_COMPLIANT - f4 is never accessed
42+
long f4;
43+
};
44+
};
45+
int f5;
46+
} s;
47+
48+
void test_nested_struct() {
49+
s.f1;
50+
s.f3;
51+
s.f5;
52+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @id c/misra/unreachable-code
3+
* @name RULE-2-1: A project shall not contain unreachable code
4+
* @description Unreachable code complicates the program and can 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-1
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.rules.unreachablecode.UnreachableCode
18+
19+
class UnreachableCodeQuery extends UnreachableCodeSharedQuery {
20+
UnreachableCodeQuery() {
21+
this = DeadCodePackage::unreachableCodeQuery()
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id c/misra/dead-code
3+
* @name RULE-2-2: There shall be no dead code
4+
* @description Dead code complicates the program and can indicate a possible mistake on the part of
5+
* the programmer.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-2-2
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.rules.deadcode.DeadCode
18+
19+
class MisraCDeadCodeQuery extends DeadCodeSharedQuery {
20+
MisraCDeadCodeQuery() { this = DeadCodePackage::deadCodeQuery() }
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id c/misra/unused-type-declarations
3+
* @name RULE-2-3: A project should not contain unused type declarations
4+
* @description Unused type 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-3
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.rules.unusedtypedeclarations.UnusedTypeDeclarations
18+
19+
class UnusedTypeDeclarationsQuery extends UnusedTypeDeclarationsSharedQuery {
20+
UnusedTypeDeclarationsQuery() { this = DeadCodePackage::unusedTypeDeclarationsQuery() }
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @id c/misra/unused-tag-declaration
3+
* @name RULE-2-4: A project should not contain unused tag declarations
4+
* @description Unused tag 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-4
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.TypeUses
18+
19+
from UserType s
20+
where
21+
not isExcluded(s, DeadCodePackage::unusedTagDeclarationQuery()) and
22+
// ignore structs without a tag name
23+
not s.getName() = "struct <unnamed>" and
24+
// typedefs do not have a "tag" name, so this rule does not apply to them
25+
not s instanceof TypedefType and
26+
// Not mentioned anywhere
27+
not exists(TypeMention tm | tm.getMentionedType() = s) and
28+
// Exclude any struct that is fully generated from a macro expansion, as it may be used in other
29+
// expansions of the same macro.
30+
// Note: due to a bug in the CodeQL CLI version 2.9.4, this will currently have no effect, because
31+
// `isInMacroExpansion` is broken for `UserType`s.
32+
not s.isInMacroExpansion()
33+
select s, "struct " + s.getName() + " has an unused tag."
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id c/misra/unused-macro-declaration
3+
* @name RULE-2-5: A project should not contain unused macro declarations
4+
* @description Unused macro 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-5
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
18+
from Macro m
19+
where
20+
not isExcluded(m, DeadCodePackage::unusedMacroDeclarationQuery()) and
21+
not exists(MacroAccess ma | ma.getMacro() = m) and
22+
// We consider a macro "used" if the name is undef-ed at some point in the same file, or a file
23+
// that includes the file defining the macro. This will over approximate use in the case of a
24+
// macro which is defined, then undefined, then re-defined but not used.
25+
not exists(PreprocessorUndef u |
26+
u.getName() = m.getName() and u.getFile().getAnIncludedFile*() = m.getFile()
27+
)
28+
select m, "Macro " + m.getName() + " is unused."
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/unused-parameter
3+
* @name RULE-2-7: There should be no unused parameters in functions
4+
* @description Unused parameters can indicate a mistake when implementing the function.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-2-7
9+
* readability
10+
* maintainability
11+
* external/misra/obligation/advisory
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.rules.unusedparameter.UnusedParameter
17+
18+
class UnusedParameterQuery extends UnusedParameterSharedQuery {
19+
UnusedParameterQuery() {
20+
this = DeadCodePackage::unusedParameterQuery()
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/unreachablecode/UnreachableCode.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/deadcode/DeadCode.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/unusedtypedeclarations/UnusedTypeDeclarations.ql
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| test.c:4:8:4:9 | S2 | struct S2 has an unused tag. |
2+
| test.c:7:16:7:17 | S3 | struct S3 has an unused tag. |
3+
| test.c:17:6:17:7 | E1 | struct E1 has an unused tag. |
4+
| test.c:31:10:31:11 | S7 | struct S7 has an unused tag. |
5+
| test.c:50:8:50:10 | S10 | struct S10 has an unused tag. |
6+
| test.c:66:3:66:14 | S13 | struct S13 has an unused tag. |
7+
| test.c:79:8:79:10 | s14 | struct s14 has an unused tag. |

0 commit comments

Comments
 (0)