Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
"Declarations",
"Declarations1",
"Declarations2",
"Declarations5",
"Exceptions1",
"Exceptions2",
"Expressions",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| test.c:4:12:4:13 | g2 | The declaration g2 should be moved from the global namespace scope$@ into the $@ too minimize its visibility. | file://:0:0:0:0 | (global namespace) | scope | test.c:59:11:59:25 | { ... } | scope |
| test.c:7:7:7:7 | j | The declaration j should be moved from $@ into the $@ too minimize its visibility. | test.c:6:11:13:1 | { ... } | scope | test.c:8:13:12:3 | { ... } | scope |
| test.c:62:7:62:7 | i | The declaration i should be moved from $@ into the $@ too minimize its visibility. | test.c:61:11:71:1 | { ... } | scope | test.c:64:13:70:3 | { ... } | scope |
| test.c:73:8:73:9 | S1 | The declaration S1 should be moved from the global namespace scope$@ into the $@ too minimize its visibility. | file://:0:0:0:0 | (global namespace) | scope | test.c:77:12:77:28 | { ... } | scope |
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// GENERATED FILE - DO NOT MODIFY
import codingstandards.cpp.rules.unnecessaryexposedidentifierdeclarationshared.UnnecessaryExposedIdentifierDeclarationShared
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <stdbool.h>
extern void f1(int i);
extern int g1; // COMPLIANT
extern int g2; // NON_COMPLIANT; single use of a global variable
bool f2() { return g1 == 1; }
void f3() {
int j = g1; // NON_COMPLIANT
if (f2()) {
int k; // COMPLIANT
f1(j);
f1(k);
}
}

void f4() {
int j = g1; // COMPLIANT; value of g1 changed between
// definition and use
g1 = 1;
if (f2()) {
f1(j);
}
}

void f5() {
int j = g1; // COMPLIANT; shouldn't be moved inside loop
while (true) {
int i = g1++;
while (f2()) {
i += j;
}

if (i % 2)
break;
}
}

void f6() {
int j = g1; // COMPLIANT; can't moved into smaller scope
#ifdef FOO
if (g1) {
g1 = j + 1;
}
#else
if (g1) {
g1 = j + 2;
}
#endif
}

void f7() {
int j = g1; // COMPLIANT; potentially stores previous value of
// g1 so moving this would be incorrect.
f1(1); // f1 may change the value of g1
if (f2()) {
f1(j);
}
}

void f8() { int i = g2; }

void f9() {
int i; // NON_COMPLIANT

if (f2()) {
if (f2()) {
i++;
} else {
i--;
}
}
}

struct S1 { // NON_COMPLIANT
int i;
};

void f10() { struct S1 l1; }

void f11() {
struct S2 { // COMPLIANT
int i;
} l1;
}

struct S3 {
int i;
};

struct S4 { // NON_COMPLIANT; single use in function f13
int i;
};

void f15() {
int i; // COMPLIANT

if (i == 0) {
i++;
}
}

void f17() {
int i; // COMPLIANT
int *ptr;
{
// Moving the declaration of i into the reduced scope will result in a
// dangling pointer
ptr = &i;
}
*ptr = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @id c/misra/identifiers-declared-in-the-same-scope-not-distinct
* @name RULE-5-2: Identifiers declared in the same scope and name space shall be distinct
* @description Using nondistinct identifiers results in undefined behaviour.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-5-2
* correctness
* maintainability
* readability
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.Identifiers

from InterestingIdentifiers d, InterestingIdentifiers d2
where
not isExcluded(d, Declarations5Package::identifiersDeclaredInTheSameScopeNotDistinctQuery()) and
not isExcluded(d2, Declarations5Package::identifiersDeclaredInTheSameScopeNotDistinctQuery()) and
//this rule does not apply if both are external identifiers
//that is covered by RULE-5-3
not (
d instanceof ExternalIdentifiers and
d2 instanceof ExternalIdentifiers
) and
d.getNamespace() = d2.getNamespace() and
d.getParentScope() = d2.getParentScope() and
not d = d2 and
d.getLocation().getStartLine() >= d2.getLocation().getStartLine() and
//first 63 chars in the name as per C99
d.getSignificantNameComparedToMacro() = d2.getSignificantNameComparedToMacro() and
not d.getName() = d2.getName()
select d,
"Identifer " + d.getName() + " is nondistinct in characters at or over 63 limit, compared to $@",
d2, d2.getName()
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @id c/misra/external-object-or-function-not-declared-in-one-file
* @name RULE-8-5: An external object or function shall be declared once in one and only one file
* @description Declarations in multiple files can lead to unexpected program behaviour.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-8-5
* correctness
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra

from DeclarationEntry de, DeclarationEntry otherDeclaration, string kind
where
not isExcluded(de, Declarations5Package::externalObjectOrFunctionNotDeclaredInOneFileQuery()) and
//this rule applies to non-defining declarations only
not de.isDefinition() and
not otherDeclaration.isDefinition() and
exists(Declaration d |
de.getDeclaration() = d and
otherDeclaration.getDeclaration() = d and
de.getFile() != otherDeclaration.getFile()
) and
(
de.getDeclaration() instanceof Function and kind = "function"
or
de.getDeclaration() instanceof Variable and
not de.getDeclaration() instanceof Parameter and
kind = "variable"
) and
// Apply an ordering based on location to enforce that (de1, de2) = (de2, de1) and we only report (de1, de2).
(
de.getFile().getAbsolutePath() < otherDeclaration.getFile().getAbsolutePath()
or
de.getFile().getAbsolutePath() = otherDeclaration.getFile().getAbsolutePath() and
de.getLocation().getStartLine() < otherDeclaration.getLocation().getStartLine()
)
select de,
"The " + kind + " declaration " + de.getName() +
" is declared in multiple files and has an additional $@.", otherDeclaration, "declaration"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @id c/misra/missing-static-specifier-function-redeclaration-c
* @name RULE-8-8: If a function has internal linkage then all re-declarations shall include the static storage class
* @description If a function has internal linkage then all re-declarations shall include the static
* storage class specifier to make the internal linkage explicit.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-8-8
* readability
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.rules.missingstaticspecifierfunctionredeclarationshared.MissingStaticSpecifierFunctionRedeclarationShared

class MissingStaticSpecifierFunctionRedeclarationCQuery extends MissingStaticSpecifierFunctionRedeclarationSharedSharedQuery {
MissingStaticSpecifierFunctionRedeclarationCQuery() {
this = Declarations5Package::missingStaticSpecifierFunctionRedeclarationCQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @id c/misra/missing-static-specifier-object-redeclaration-c
* @name RULE-8-8: If an object has internal linkage then all re-declarations shall include the static storage class
* @description If an object has internal linkage then all re-declarations shall include the static
* storage class specifier to make the internal linkage explicit.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-8-8
* readability
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra

from VariableDeclarationEntry redeclaration, VariableDeclarationEntry de
where
not isExcluded(redeclaration,
Declarations5Package::missingStaticSpecifierObjectRedeclarationCQuery()) and
de.hasSpecifier("static") and
de.getDeclaration().isTopLevel() and
redeclaration.getDeclaration() = de.getDeclaration() and
not redeclaration.hasSpecifier("static") and
de != redeclaration
select redeclaration, "The redeclaration of $@ with internal linkage misses the static specifier.",
de, de.getName()
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @id c/misra/unnecessary-exposed-identifier-declaration-c
* @name RULE-8-9: An object should be defined at block scope if its identifier only appears in a single function
* @description An identifier declared to be an object or type shall be defined in a block that
* minimizes its visibility to prevent any accidental use of the identifier.
* @kind problem
* @precision high
* @problem.severity warning
* @tags external/misra/id/rule-8-9
* correctness
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.rules.unnecessaryexposedidentifierdeclarationshared.UnnecessaryExposedIdentifierDeclarationShared

class UnnecessaryExposedIdentifierDeclarationCQuery extends UnnecessaryExposedIdentifierDeclarationSharedSharedQuery {
UnnecessaryExposedIdentifierDeclarationCQuery() {
this = Declarations5Package::unnecessaryExposedIdentifierDeclarationCQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| test.c:8:5:8:68 | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB | Identifer iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB is nondistinct in characters at or over 63 limit, compared to $@ | test.c:2:5:2:68 | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-5-2/IdentifiersDeclaredInTheSameScopeNotDistinct.ql
26 changes: 26 additions & 0 deletions c/misra/test/rules/RULE-5-2/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern int
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA; // NON_COMPLIANT
// -
// length
// 64

static int
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB; // NON_COMPLIANT
// -
// length
// 64

void f() {
int iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyC; // COMPLIANT
// -
// length
// 64
// but
// diff
// scope
}

static int
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjy_C; // COMPLIANT length <63
static int
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjy_D; // COMPLIANT length <63
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| test.c:8:12:8:13 | declaration of g3 | The variable declaration g3 is declared in multiple files and has an additional $@. | test1.c:1:12:1:13 | declaration of g3 | declaration |
| test.h:1:12:1:12 | declaration of g | The variable declaration g is declared in multiple files and has an additional $@. | test1.h:1:12:1:12 | declaration of g | declaration |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-8-5/ExternalObjectOrFunctionNotDeclaredInOneFile.ql
8 changes: 8 additions & 0 deletions c/misra/test/rules/RULE-8-5/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "test.h"
#include "test1.h"

int g = 1; // COMPLIANT

extern int g1; // COMPLIANT

extern int g3; // NON_COMPLIANT
3 changes: 3 additions & 0 deletions c/misra/test/rules/RULE-8-5/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern int g; // NON_COMPLIANT

int g2; // COMPLIANT
1 change: 1 addition & 0 deletions c/misra/test/rules/RULE-8-5/test1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extern int g3; // NON_COMPLIANT
3 changes: 3 additions & 0 deletions c/misra/test/rules/RULE-8-5/test1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern int g; // NON_COMPLIANT

int g2; // COMPLIANT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/missingstaticspecifierfunctionredeclarationshared/MissingStaticSpecifierFunctionRedeclarationShared.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| test.c:2:12:2:12 | declaration of g | The redeclaration of $@ with internal linkage misses the static specifier. | test.c:1:12:1:12 | definition of g | g |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-8-8/MissingStaticSpecifierObjectRedeclarationC.ql
8 changes: 8 additions & 0 deletions c/misra/test/rules/RULE-8-8/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
static int g = 0;
extern int g; // NON_COMPLIANT

static int g1;
static int g1 = 0; // COMPLIANT

int g2;
int g2 = 0; // COMPLIANT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c/common/test/rules/unnecessaryexposedidentifierdeclarationshared/UnnecessaryExposedIdentifierDeclarationShared.ql
Loading