Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions c/common/test/rules/deadcode/DeadCode.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
| test.c:18:3:18:27 | declaration | This statement is dead code. |
| test.c:19:3:19:12 | ExprStmt | This statement is dead code. |
| test.c:20:3:20:12 | ExprStmt | This statement is dead code. |
| test.c:22:3:24:3 | if (...) ... | This statement is dead code. |
| test.c:34:3:35:3 | if (...) ... | This statement is dead code. |
| test.c:37:3:37:4 | { ... } | This statement is dead code. |
| test.c:38:3:40:3 | { ... } | This statement is dead code. |
| test.c:54:6:55:3 | { ... } | This statement is dead code. |
| test.c:65:46:66:3 | { ... } | This statement is dead code. |
| test.c:69:3:69:8 | ExprStmt | This statement is dead code. |
| test.c:71:3:71:21 | ExprStmt | This statement is dead code. |
2 changes: 2 additions & 0 deletions c/common/test/rules/deadcode/DeadCode.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// GENERATED FILE - DO NOT MODIFY
import codingstandards.cpp.rules.deadcode.DeadCode
74 changes: 74 additions & 0 deletions c/common/test/rules/deadcode/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C++ TEST CASE AND
// CHANGES SHOULD BE REFLECTED THERE AS WELL.
#include <stdbool.h>

int may_have_side_effects();
int no_side_effects(int x) { return 1 + 2; }
int no_side_effects_nondeterministic();

int test_dead_code(int x) {
int live1 = may_have_side_effects(),
live2 = may_have_side_effects(); // COMPLIANT
int live3 = 0,
live4 = may_have_side_effects(); // COMPLIANT
int live5 = 0, live6 = 0; // COMPLIANT
live5 = 1; // COMPLIANT
live6 = 2; // COMPLIANT

int dead1 = 0, dead2 = 0; // NON_COMPLIANT
dead1 = 1; // NON_COMPLIANT - useless assignment
dead2 = 1; // NON_COMPLIANT - useless assignment

if (false) { // NON_COMPLIANT
dead2 = 10; // Only used in dead or unreachable code
}

if (true) { // COMPLIANT
may_have_side_effects();
}

if (may_have_side_effects()) { // COMPLIANT
may_have_side_effects();
}

if (true) { // NON_COMPLIANT
}

{} // NON_COMPLIANT
{ // NON_COMPLIANT
1 + 2;
}

{ // COMPLIANT
may_have_side_effects();
}

do { // COMPLIANT
may_have_side_effects();
} while (may_have_side_effects());

do { // COMPLIANT
may_have_side_effects();
} while (may_have_side_effects());

do { // NON_COMPLIANT
} while (no_side_effects_nondeterministic());

while (may_have_side_effects()) { // COMPLIANT
may_have_side_effects();
}

while (may_have_side_effects()) { // COMPLIANT
may_have_side_effects();
}

while (no_side_effects_nondeterministic()) { // NON_COMPLIANT
}

may_have_side_effects(); // COMPLIANT
1 + 2; // NON_COMPLIANT

no_side_effects(x); // NON_COMPLIANT

return live5 + live6; // COMPLIANT
}
2 changes: 2 additions & 0 deletions c/common/test/rules/unreachablecode/UnreachableCode.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| 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 |
| test.c:21:10:22:12 | { ... } | This statement in function $@ is unreachable. | test.c:20:5:20:27 | test_constant_condition | test_constant_condition |
2 changes: 2 additions & 0 deletions c/common/test/rules/unreachablecode/UnreachableCode.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// GENERATED FILE - DO NOT MODIFY
import codingstandards.cpp.rules.unreachablecode.UnreachableCode
26 changes: 26 additions & 0 deletions c/common/test/rules/unreachablecode/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C++ TEST CASE AND
// CHANGES SHOULD BE REFLECTED THERE AS WELL.

void test_switch(int p1) {
int l1 = 0;
switch (p1) {
l1 = p1; // NON_COMPLIANT[FALSE_NEGATIVE]
case 1:
break;
default:
break;
}
}

int test_after_return() {
return 0;
int l1 = 0; // NON_COMPLIANT - function has returned by this point
}

int test_constant_condition() {
if (0) { // NON_COMPLIANT
return 1;
} else { // COMPLIANT
return 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| test.c:6:22:6:22 | x | Unused parameter 'x' for function $@. | test.c:6:6:6:16 | test_unused | test_unused |
2 changes: 2 additions & 0 deletions c/common/test/rules/unusedparameter/UnusedParameter.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// GENERATED FILE - DO NOT MODIFY
import codingstandards.cpp.rules.unusedparameter.UnusedParameter
8 changes: 8 additions & 0 deletions c/common/test/rules/unusedparameter/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C++ TEST CASE AND
// CHANGES SHOULD BE REFLECTED THERE AS WELL.

int test_used(int x) { return x; } // COMPLIANT

void test_unused(int x) {} // NON_COMPLIANT

void test_no_def(int x); // COMPLIANT - no definition, so cannot be "unused"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| file://:0:0:0:0 | __va_list_tag | Type declaration __va_list_tag is not used. |
| test.c:4:8:4:8 | A | Type declaration A is not used. |
| test.c:7:18:7:18 | D | Type declaration D is not used. |
| test.c:28:11:28:11 | R | Type declaration R is not used. |
| test.c:41:12:41:12 | struct <unnamed> | Type declaration struct <unnamed> is not used. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// GENERATED FILE - DO NOT MODIFY
import codingstandards.cpp.rules.unusedtypedeclarations.UnusedTypeDeclarations
52 changes: 52 additions & 0 deletions c/common/test/rules/unusedtypedeclarations/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C++ TEST CASE AND
// CHANGES SHOULD BE REFLECTED THERE AS WELL.

struct A {}; // NON_COMPLIANT - unused

struct C {}; // COMPLIANT - used in the type def
typedef struct C D; // NON_COMPLIANT - typedef itself not used

struct F {}; // COMPLIANT - used as a global function return type

struct F test_return_value() {
struct F f;
return f;
}

struct G {}; // COMPLIANT - used as a global function parameter type

void test_global_function(struct G g) {}

enum M { C1, C2, C3 }; // COMPLIANT - used in an enum type access below

void test_enum_access() { int i = C1; }

struct O {}; // COMPLIANT - used in typedef below

typedef struct O P; // COMPLIANT - used in typedef below
typedef P Q; // COMPLIANT - used in function below
typedef Q R; // NON_COMPLIANT - never used

Q test_type_def() {}

struct { // COMPLIANT - used in type definition
union { // COMPLIANT - f1 and f3 is accessed
struct { // COMPLIANT - f1 is accessed
int f1;
};
struct { // COMPLIANT - f3 is accessed
float f2;
float f3;
};
struct { // NON_COMPLIANT - f4 is never accessed
long f4;
};
};
int f5;
} s;

void test_nested_struct() {
s.f1;
s.f3;
s.f5;
}
23 changes: 23 additions & 0 deletions c/misra/src/rules/RULE-2-1/UnreachableCode.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @id c/misra/unreachable-code
* @name RULE-2-1: A project shall not contain unreachable code
* @description Unreachable code complicates the program and can indicate a possible mistake on the
* part of the programmer.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-2-1
* readability
* maintainability
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.rules.unreachablecode.UnreachableCode

class UnreachableCodeQuery extends UnreachableCodeSharedQuery {
UnreachableCodeQuery() {
this = DeadCodePackage::unreachableCodeQuery()
}
}
21 changes: 21 additions & 0 deletions c/misra/src/rules/RULE-2-2/DeadCode.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @id c/misra/dead-code
* @name RULE-2-2: There shall be no dead code
* @description Dead code complicates the program and can indicate a possible mistake on the part of
* the programmer.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-2-2
* readability
* maintainability
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.rules.deadcode.DeadCode

class MisraCDeadCodeQuery extends DeadCodeSharedQuery {
MisraCDeadCodeQuery() { this = DeadCodePackage::deadCodeQuery() }
}
21 changes: 21 additions & 0 deletions c/misra/src/rules/RULE-2-3/UnusedTypeDeclarations.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @id c/misra/unused-type-declarations
* @name RULE-2-3: A project should not contain unused type declarations
* @description Unused type declarations are either redundant or indicate a possible mistake on the
* part of the programmer.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-2-3
* readability
* maintainability
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.rules.unusedtypedeclarations.UnusedTypeDeclarations

class UnusedTypeDeclarationsQuery extends UnusedTypeDeclarationsSharedQuery {
UnusedTypeDeclarationsQuery() { this = DeadCodePackage::unusedTypeDeclarationsQuery() }
}
33 changes: 33 additions & 0 deletions c/misra/src/rules/RULE-2-4/UnusedTagDeclaration.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @id c/misra/unused-tag-declaration
* @name RULE-2-4: A project should not contain unused tag declarations
* @description Unused tag declarations are either redundant or indicate a possible mistake on the
* part of the programmer.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-2-4
* readability
* maintainability
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.TypeUses

from UserType s
where
not isExcluded(s, DeadCodePackage::unusedTagDeclarationQuery()) and
// ignore structs without a tag name
not s.getName() = "struct <unnamed>" and
// typedefs do not have a "tag" name, so this rule does not apply to them
not s instanceof TypedefType and
// Not mentioned anywhere
not exists(TypeMention tm | tm.getMentionedType() = s) and
// Exclude any struct that is fully generated from a macro expansion, as it may be used in other
// expansions of the same macro.
// Note: due to a bug in the CodeQL CLI version 2.9.4, this will currently have no effect, because
// `isInMacroExpansion` is broken for `UserType`s.
not s.isInMacroExpansion()
select s, "struct " + s.getName() + " has an unused tag."
28 changes: 28 additions & 0 deletions c/misra/src/rules/RULE-2-5/UnusedMacroDeclaration.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @id c/misra/unused-macro-declaration
* @name RULE-2-5: A project should not contain unused macro declarations
* @description Unused macro declarations are either redundant or indicate a possible mistake on the
* part of the programmer.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-2-5
* readability
* maintainability
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.c.misra

from Macro m
where
not isExcluded(m, DeadCodePackage::unusedMacroDeclarationQuery()) and
not exists(MacroAccess ma | ma.getMacro() = m) and
// We consider a macro "used" if the name is undef-ed at some point in the same file, or a file
// that includes the file defining the macro. This will over approximate use in the case of a
// macro which is defined, then undefined, then re-defined but not used.
not exists(PreprocessorUndef u |
u.getName() = m.getName() and u.getFile().getAnIncludedFile*() = m.getFile()
)
select m, "Macro " + m.getName() + " is unused."
25 changes: 25 additions & 0 deletions c/misra/src/rules/RULE-2-6/UnusedLabelDeclaration.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @id c/misra/unused-label-declaration
* @name RULE-2-6: A function should not contain unused label declarations
* @description Unused label declarations are either redundant or indicate a possible mistake on the
* part of the programmer.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-2-6
* readability
* maintainability
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.c.misra

from LabelStmt label
where
not isExcluded(label, DeadCodePackage::unusedLabelDeclarationQuery()) and
// No GotoStmt jumps to this label
not exists(GotoStmt gs | gs.hasName() and gs.getTarget() = label) and
// The address of the label is never taken
not exists(LabelLiteral literal | literal.getLabel() = label)
select label, "Label " + label.getName() + " is unused."
22 changes: 22 additions & 0 deletions c/misra/src/rules/RULE-2-7/UnusedParameter.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @id c/misra/unused-parameter
* @name RULE-2-7: There should be no unused parameters in functions
* @description Unused parameters can indicate a mistake when implementing the function.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-2-7
* readability
* maintainability
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.rules.unusedparameter.UnusedParameter

class UnusedParameterQuery extends UnusedParameterSharedQuery {
UnusedParameterQuery() {
this = DeadCodePackage::unusedParameterQuery()
}
}
1 change: 1 addition & 0 deletions c/misra/test/rules/RULE-2-1/UnreachableCode.testref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c/common/test/rules/unreachablecode/UnreachableCode.ql
1 change: 1 addition & 0 deletions c/misra/test/rules/RULE-2-2/DeadCode.testref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c/common/test/rules/deadcode/DeadCode.ql
1 change: 1 addition & 0 deletions c/misra/test/rules/RULE-2-3/UnusedTypeDeclarations.testref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c/common/test/rules/unusedtypedeclarations/UnusedTypeDeclarations.ql
7 changes: 7 additions & 0 deletions c/misra/test/rules/RULE-2-4/UnusedTagDeclaration.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
| test.c:4:8:4:9 | S2 | struct S2 has an unused tag. |
| test.c:7:16:7:17 | S3 | struct S3 has an unused tag. |
| test.c:17:6:17:7 | E1 | struct E1 has an unused tag. |
| test.c:31:10:31:11 | S7 | struct S7 has an unused tag. |
| test.c:50:8:50:10 | S10 | struct S10 has an unused tag. |
| test.c:66:3:66:14 | S13 | struct S13 has an unused tag. |
| test.c:79:8:79:10 | s14 | struct s14 has an unused tag. |
Loading