Skip to content

Commit d39bdf4

Browse files
authored
Merge pull request #94 from knewbury01/knewbury01/Declarations3
Package Declarations3
2 parents 5053722 + a6f54c4 commit d39bdf4

Some content is hidden

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

43 files changed

+554
-28
lines changed

c/cert/src/rules/DCL31-C/DeclareIdentifiersBeforeUsingThem.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Search for [vulnerabilities](https://wiki.sei.cmu.edu/confluence/display/c/BB.+D
153153

154154
## Implementation notes
155155

156-
This query does not check for implicit function declarations as this is partially compiler checked.
156+
This query does not check for implicitly typed parameters, typedefs or member declarations as this is partially compiler checked.
157157

158158
## References
159159

c/cert/src/rules/DCL31-C/DeclareIdentifiersBeforeUsingThem.ql

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@
1313

1414
import cpp
1515
import codingstandards.c.cert
16+
import codingstandards.cpp.rules.typeomitted.TypeOmitted
1617

17-
from Declaration d
18-
where
19-
not isExcluded(d, Declarations1Package::declareIdentifiersBeforeUsingThemQuery()) and
20-
d.hasSpecifier("implicit_int") and
21-
exists(Type t |
22-
(d.(Variable).getType() = t or d.(Function).getType() = t) and
23-
// Exclude "short" or "long", as opposed to "short int" or "long int".
24-
t instanceof IntType and
25-
// Exclude "signed" or "unsigned", as opposed to "signed int" or "unsigned int".
26-
not exists(IntegralType it | it = t | it.isExplicitlySigned() or it.isExplicitlyUnsigned())
27-
)
28-
select d, "Declaration " + d.getName() + " is missing a type specifier."
18+
class DeclareIdentifiersBeforeUsingThem extends TypeOmittedSharedQuery {
19+
DeclareIdentifiersBeforeUsingThem() {
20+
this = Declarations1Package::declareIdentifiersBeforeUsingThemQuery()
21+
}
22+
}

c/cert/test/rules/DCL31-C/DeclareIdentifiersBeforeUsingThem.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/typeomitted/TypeOmitted.ql
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import cpp
2+
3+
//Identifiers that are candidates for checking uniqueness
4+
class InterestingIdentifiers extends Declaration {
5+
InterestingIdentifiers() {
6+
not this.isFromTemplateInstantiation(_) and
7+
not this.isFromUninstantiatedTemplate(_) and
8+
not this instanceof TemplateParameter and
9+
not this.hasDeclaringType() and
10+
not this instanceof Operator and
11+
not this.hasName("main") and
12+
exists(this.getADeclarationLocation())
13+
}
14+
15+
string getSignificantName() { result = this.getName().prefix(31) }
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| test.c:4:7:4:9 | id1 | Variable is hiding variable $@. | test.c:1:5:1:7 | id1 | id1 |
2+
| test.c:7:13:7:15 | id1 | Variable is hiding variable $@. | test.c:1:5:1:7 | id1 | id1 |
3+
| test.c:10:12:10:14 | id1 | Variable is hiding variable $@. | test.c:1:5:1:7 | id1 | id1 |
4+
| test.c:11:14:11:16 | id1 | Variable is hiding variable $@. | test.c:10:12:10:14 | id1 | id1 |
5+
| test.c:24:24:24:26 | id2 | Variable is hiding variable $@. | test.c:22:5:22:7 | id2 | id2 |
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.identifierhidden.IdentifierHidden
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
int id1;
2+
3+
void f1() {
4+
int id1; // NON_COMPLIANT
5+
}
6+
7+
void f2(int id1) {} // NON_COMPLIANT
8+
9+
void f3() {
10+
for (int id1; id1 < 1; id1++) { // NON_COMPLIANT
11+
for (int id1; id1 < 1; id1++) {
12+
} // NON_COMPLIANT
13+
}
14+
}
15+
16+
struct astruct {
17+
int id1;
18+
};
19+
20+
extern void g(struct astruct *p);
21+
22+
int id2 = 0;
23+
24+
void f4(struct astruct id2) { // NON_COMPLIANT
25+
g(&id2);
26+
}
27+
28+
void f5(struct astruct id3) { // COMPLIANT
29+
g(&id2);
30+
}
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.typeomitted.TypeOmitted

c/cert/test/rules/DCL31-C/test.c renamed to c/common/test/rules/typeomitted/test.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ int f1(void) { // COMPLIANT
1313
short g2; // COMPLIANT
1414
long g3; // COMPLIANT
1515
signed g4() { return 1; } // COMPLIANT
16+
17+
typedef *newtype3; // NON_COMPLIANT[FALSE_NEGATIVE]
18+
19+
int f2(const x) { // NON_COMPLIANT[FALSE_NEGATIVE]
20+
return 1;
21+
}
22+
23+
struct str {
24+
const y; // NON_COMPLIANT[FALSE_NEGATIVE]
25+
} s;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id c/misra/identifier-hiding-c
3+
* @name RULE-5-3: An identifier declared in an inner scope shall not hide an identifier declared in an outer scope
4+
* @description Use of an identifier declared in an inner scope with an identical name to an
5+
* identifier in an outer scope can lead to inadvertent errors if the incorrect
6+
* identifier is modified.
7+
* @kind problem
8+
* @precision very-high
9+
* @problem.severity warning
10+
* @tags external/misra/id/rule-5-3
11+
* readability
12+
* maintainability
13+
* external/misra/obligation/required
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
import codingstandards.cpp.rules.identifierhidden.IdentifierHidden
19+
20+
class IdentifierHidingCQuery extends IdentifierHiddenSharedQuery {
21+
IdentifierHidingCQuery() {
22+
this = Declarations3Package::identifierHidingCQuery()
23+
}
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @id c/misra/identifiers-not-distinct-from-macro-names
3+
* @name RULE-5-5: Identifiers shall be distinct from macro names
4+
* @description Reusing a macro name compared to the name of any other identifier can cause
5+
* confusion and make code harder to read.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-5-5
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.c.Identifiers
18+
19+
from Macro m, InterestingIdentifiers i, string mName, string iName
20+
where
21+
not isExcluded(m, Declarations3Package::identifiersNotDistinctFromMacroNamesQuery()) and
22+
not isExcluded(i, Declarations3Package::identifiersNotDistinctFromMacroNamesQuery()) and
23+
mName = iName and
24+
(
25+
//C99 states the first 31 characters of external identifiers are significant
26+
//C90 states the first 6 characters of external identifiers are significant and case is not required to be significant
27+
//C90 is not currently considered by this rule
28+
if m.getName().length() > 31 then mName = m.getName().prefix(31) else mName = m.getName()
29+
) and
30+
if i.getName().length() > 31 then iName = i.getSignificantName() else iName = i.getName()
31+
select m, "Macro name is nonunique compared to $@.", i, i.getName()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id c/misra/typedef-name-not-unique
3+
* @name RULE-5-6: A typedef name shall be a unique identifier
4+
* @description Reusing a typedef name compared to the name of any other identifier can cause
5+
* confusion and make code harder to read.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-5-6
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.c.Identifiers
18+
19+
from TypedefType t, InterestingIdentifiers d
20+
where
21+
not isExcluded(t, Declarations3Package::typedefNameNotUniqueQuery()) and
22+
not isExcluded(d, Declarations3Package::typedefNameNotUniqueQuery()) and
23+
not t.getADeclarationLocation() = d.getADeclarationLocation() and
24+
t.getName() = d.getName() and
25+
//exception cases
26+
not d.(Struct).getName() = t.getBaseType().toString() and
27+
not d.(Enum).getName() = t.getBaseType().toString()
28+
select t, "Typedef name is nonunique compared to $@.", d, d.getName()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id c/misra/tag-name-not-unique
3+
* @name RULE-5-7: A tag name shall be a unique identifier
4+
* @description Reusing a tag name compared to the name of any tag can cause confusion and make code
5+
* harder to read.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-5-7
10+
* readability
11+
* maintainability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.c.Identifiers
18+
19+
from Struct s, InterestingIdentifiers s2
20+
where
21+
not isExcluded(s, Declarations3Package::tagNameNotUniqueQuery()) and
22+
not isExcluded(s2, Declarations3Package::tagNameNotUniqueQuery()) and
23+
not s = s2 and
24+
s.getName() = s2.getName() and
25+
not s.getName() = "struct <unnamed>" and
26+
not s.getName() = "union <unnamed>" and
27+
not s.getName() = s2.(TypedefType).getBaseType().toString()
28+
select s, "Tag name is nonunique compared to $@.", s2, s2.getName()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @id c/misra/explicitly-declare-types
3+
* @name RULE-8-1: Declare identifiers before using them
4+
* @description Omission of type specifiers may not be supported by some compilers.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-8-1
9+
* correctness
10+
* readability
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.rules.typeomitted.TypeOmitted
17+
18+
class ExplicitlyDeclareTypesQuery extends TypeOmittedSharedQuery {
19+
ExplicitlyDeclareTypesQuery() { this = Declarations3Package::explicitlyDeclareTypesQuery() }
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/identifierhidden/IdentifierHidden.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:1:1:1:23 | #define Sum(x,y) x + y | Macro name is nonunique compared to $@. | test.c:4:5:4:7 | Sum | Sum |
2+
| test.c:6:1:6:42 | #define iltiqzxgfqsgigwfuyntzghvzltueeaZ ; | Macro name is nonunique compared to $@. | test.c:7:12:7:43 | iltiqzxgfqsgigwfuyntzghvzltueeaQ | iltiqzxgfqsgigwfuyntzghvzltueeaQ |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-5/IdentifiersNotDistinctFromMacroNames.ql

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define Sum(x, y) x + y // NON_COMPLIANT
2+
#undef Sum
3+
4+
int Sum;
5+
6+
#define iltiqzxgfqsgigwfuyntzghvzltueeaZ ; // NON_COMPLIANT - length 32
7+
static int iltiqzxgfqsgigwfuyntzghvzltueeaQ; // NON_COMPLIANT - length 32
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:11:15:11:19 | test1 | Typedef name is nonunique compared to $@. | test.c:13:17:13:21 | test1 | test1 |
2+
| test.c:30:3:30:7 | chain | Typedef name is nonunique compared to $@. | test.c:26:10:26:14 | chain | chain |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-6/TypedefNameNotUnique.ql

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "test.h"
2+
void f() {
3+
{
4+
typedef unsigned char test; // NON_COMPLIANT
5+
}
6+
{
7+
typedef unsigned char test; // NON_COMPLIANT
8+
}
9+
}
10+
11+
typedef float test1; // NON_COMPLIANT
12+
13+
void f2() { int test1 = 0; }
14+
15+
typedef struct list {
16+
int i;
17+
} list; // COMPLIANT
18+
19+
typedef struct BIGList1 {
20+
int i;
21+
} list1; // COMPLIANT
22+
23+
typedef enum enum1 { testenum } enum1; // COMPLIANT
24+
25+
typedef struct {
26+
struct chain {
27+
int ii;
28+
} s1;
29+
int i;
30+
} chain; // NON_COMPLIANT

c/misra/test/rules/RULE-5-6/test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
typedef int headertest; // COMPLIANT

c/misra/test/rules/RULE-5-6/test1.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "test.h"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.c:5:8:5:9 | s1 | Tag name is nonunique compared to $@. | test.c:12:10:12:11 | s1 | s1 |
2+
| test.c:5:8:5:9 | s1 | Tag name is nonunique compared to $@. | test.c:17:17:17:18 | s1 | s1 |
3+
| test.c:12:10:12:11 | s1 | Tag name is nonunique compared to $@. | test.c:5:8:5:9 | s1 | s1 |
4+
| test.c:12:10:12:11 | s1 | Tag name is nonunique compared to $@. | test.c:17:17:17:18 | s1 | s1 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-5-7/TagNameNotUnique.ql

c/misra/test/rules/RULE-5-7/test.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
typedef struct s {
2+
int i;
3+
} s; // COMPLIANT
4+
5+
struct s1 { // NON_COMPLIANT
6+
int i;
7+
};
8+
9+
struct s1 a1 = {0}; // COMPLIANT
10+
11+
void f() {
12+
struct s1 { // NON_COMPLIANT
13+
int i;
14+
};
15+
}
16+
17+
void f1() { int s1 = 0; }
18+
19+
typedef struct {
20+
int i;
21+
} sunnamed; // COMPLIANT
22+
23+
typedef struct {
24+
int i;
25+
} sunnamed2; // COMPLIANT
26+
27+
typedef union {
28+
int i;
29+
} U; // COMPLIANT
30+
31+
typedef union {
32+
int i;
33+
}; // COMPLIANT
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/typeomitted/TypeOmitted.ql

cpp/autosar/src/rules/A2-10-1/IdentifierHiding.ql

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919

2020
import cpp
2121
import codingstandards.cpp.autosar
22-
import codingstandards.cpp.Scope
22+
import codingstandards.cpp.rules.identifierhidden.IdentifierHidden
2323

24-
from UserVariable v1, UserVariable v2
25-
where
26-
not isExcluded(v1, NamingPackage::identifierHidingQuery()) and
27-
not isExcluded(v2, NamingPackage::identifierHidingQuery()) and
28-
hides(v1, v2)
29-
select v2, "Variable is hiding variable $@.", v1, v1.getName()
24+
class IdentifierHidingCQuery extends IdentifierHiddenSharedQuery {
25+
IdentifierHidingCQuery() { this = NamingPackage::identifierHidingQuery() }
26+
}

cpp/autosar/test/rules/A2-10-1/IdentifierHiding.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/identifierhidden/IdentifierHidden.ql

0 commit comments

Comments
 (0)