Skip to content

Commit 68304ab

Browse files
committed
Declarations3: add shared RULE-8-1
1 parent 311eb92 commit 68304ab

File tree

14 files changed

+110
-17
lines changed

14 files changed

+110
-17
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: 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: 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/typeomitted/TypeOmitted.ql

cpp/common/src/codingstandards/cpp/exclusions/c/Declarations3.qll

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ newtype Declarations3Query =
77
TIdentifierHidingCQuery() or
88
TIdentifiersNotDistinctFromMacroNamesQuery() or
99
TTypedefNameNotUniqueQuery() or
10-
TTagNameNotUniqueQuery()
10+
TTagNameNotUniqueQuery() or
11+
TExplicitlyDeclareTypesQuery()
1112

1213
predicate isDeclarations3QueryMetadata(Query query, string queryId, string ruleId) {
1314
query =
@@ -41,6 +42,14 @@ predicate isDeclarations3QueryMetadata(Query query, string queryId, string ruleI
4142
// `@id` for the `tagNameNotUnique` query
4243
"c/misra/tag-name-not-unique" and
4344
ruleId = "RULE-5-7"
45+
or
46+
query =
47+
// `Query` instance for the `explicitlyDeclareTypes` query
48+
Declarations3Package::explicitlyDeclareTypesQuery() and
49+
queryId =
50+
// `@id` for the `explicitlyDeclareTypes` query
51+
"c/misra/explicitly-declare-types" and
52+
ruleId = "RULE-8-1"
4453
}
4554

4655
module Declarations3Package {
@@ -71,4 +80,11 @@ module Declarations3Package {
7180
// `Query` type for `tagNameNotUnique` query
7281
TQueryC(TDeclarations3PackageQuery(TTagNameNotUniqueQuery()))
7382
}
83+
84+
Query explicitlyDeclareTypesQuery() {
85+
//autogenerate `Query` type
86+
result =
87+
// `Query` type for `explicitlyDeclareTypes` query
88+
TQueryC(TDeclarations3PackageQuery(TExplicitlyDeclareTypesQuery()))
89+
}
7490
}

0 commit comments

Comments
 (0)