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
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Provides a library which includes a `problems` predicate for reporting....
3+
*/
4+
5+
import cpp
6+
import codingstandards.cpp.Customizations
7+
import codingstandards.cpp.Exclusions
8+
9+
abstract class TypeOmittedSharedQuery extends Query { }
10+
11+
Query getQuery() { result instanceof TypeOmittedSharedQuery }
12+
13+
query predicate problems(Declaration d, string message) {
14+
not isExcluded(d, getQuery()) and
15+
d.hasSpecifier("implicit_int") and
16+
exists(Type t |
17+
(d.(Variable).getType() = t or d.(Function).getType() = t) and
18+
// Exclude "short" or "long", as opposed to "short int" or "long int".
19+
t instanceof IntType and
20+
// Exclude "signed" or "unsigned", as opposed to "signed int" or "unsigned int".
21+
not exists(IntegralType it | it = t | it.isExplicitlySigned() or it.isExplicitlyUnsigned())
22+
) and
23+
message = "Declaration " + d.getName() + " is missing a type specifier."
24+
}

rule_packages/c/Declarations1.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
"precision": "very-high",
1313
"severity": "error",
1414
"short_name": "DeclareIdentifiersBeforeUsingThem",
15+
"shared_implementation_short_name": "TypeOmitted",
1516
"tags": [
1617
"correctness",
1718
"readability"
1819
],
1920
"implementation_scope": {
20-
"description": "This query does not check for implicit function declarations as this is partially compiler checked.",
21+
"description": "This query does not check for implicitly typed parameters, typedefs or member declarations as this is partially compiler checked.",
2122
"items": []
2223
}
2324
}

rule_packages/c/Declarations3.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,31 @@
8383
}
8484
],
8585
"title": "A tag name shall be a unique identifier"
86+
},
87+
"RULE-8-1": {
88+
"properties": {
89+
"obligation": "required"
90+
},
91+
"queries": [
92+
{
93+
"description": "Omission of type specifiers may not be supported by some compilers.",
94+
"kind": "problem",
95+
"name": "Declare identifiers before using them",
96+
"precision": "very-high",
97+
"severity": "error",
98+
"short_name": "ExplicitlyDeclareTypes",
99+
"shared_implementation_short_name": "TypeOmitted",
100+
"tags": [
101+
"correctness",
102+
"readability"
103+
],
104+
"implementation_scope": {
105+
"description": "This query does not check for implicitly typed parameters, typedefs or member declarations as this is partially compiler checked.",
106+
"items": []
107+
}
108+
}
109+
],
110+
"title": "Types shall be explicitly specified"
86111
}
87112
}
88113
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ c,MISRA-C-2012,RULE-7-1,Yes,Required,,,Octal constants shall not be used,M2-13-2
646646
c,MISRA-C-2012,RULE-7-2,Yes,Required,,,A �u� or �U� suffix shall be applied to all integer constants that are represented in an unsigned type,M2-13-3,Syntax,Easy,
647647
c,MISRA-C-2012,RULE-7-3,Yes,Required,,,The lowercase character �l� shall not be used in a literal suffix,M2-13-4,Syntax,Easy,
648648
c,MISRA-C-2012,RULE-7-4,Yes,Required,,,A string literal shall not be assigned to an object unless the object�s type is �pointer to const-qualified char�,A2-13-4,Types,Easy,
649-
c,MISRA-C-2012,RULE-8-1,Yes,Required,,,Types shall be explicitly specified,,Declarations,Medium,
649+
c,MISRA-C-2012,RULE-8-1,Yes,Required,,,Types shall be explicitly specified,,Declarations3,Medium,
650650
c,MISRA-C-2012,RULE-8-2,Yes,Required,,,Function types shall be in prototype form with named parameters,,Declarations,Medium,
651651
c,MISRA-C-2012,RULE-8-3,Yes,Required,,,All declarations of an object or function shall use the same names and type qualifiers,M3-2-1,Declarations,Medium,
652652
c,MISRA-C-2012,RULE-8-4,Yes,Required,,,A compatible declaration shall be visible when an object or function with external linkage is defined,,Declarations,Medium,

0 commit comments

Comments
 (0)