Skip to content

Commit 110f211

Browse files
committed
Declarations1: add rule DCL31-C
1 parent 516b086 commit 110f211

File tree

8 files changed

+97
-2
lines changed

8 files changed

+97
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# DCL31-C: Declare identifiers before using them
2+
3+
This query implements the CERT-C rule DCL31-C:
4+
5+
> Declare identifiers before using them
6+
7+
8+
## CERT
9+
10+
** REPLACE THIS BY RUNNING THE SCRIPT `scripts/help/cert-help-extraction.py` **
11+
12+
## Implementation notes
13+
14+
This query does not check for implicit function declarations as this is partially compiler checked.
15+
16+
## References
17+
18+
* CERT-C: [DCL31-C: Declare identifiers before using them](https://wiki.sei.cmu.edu/confluence/display/c)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id c/cert/declare-identifiers-before-using-them
3+
* @name DCL31-C: 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/cert/id/dcl31-c
9+
* correctness
10+
* readability
11+
* external/cert/obligation/rule
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.cert
16+
17+
from Declaration d
18+
where
19+
not isExcluded(d, Declarations1Package::declareIdentifiersBeforeUsingThemQuery()) and
20+
exists(Specifier s | s = d.getASpecifier() and s.toString().regexpMatch("implicit_.*"))
21+
select d, "Declaration is missing a type specifier."
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:2:8:2:8 | g | Declaration is missing a type specifier. |
2+
| test.c:6:1:6:1 | f | Declaration is missing a type specifier. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/DCL31-C/DeclareIdentifiersBeforeUsingThem.ql

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
extern g; //NON_COMPLIANT
3+
4+
extern int g1; //COMPLIANT
5+
6+
f(void) { //NON_COMPLIANT
7+
return 1;
8+
}
9+
10+
int f1(void) { //COMPLIANT
11+
return 1;
12+
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ import cpp
33
import RuleMetadata
44
import codingstandards.cpp.exclusions.RuleMetadata
55

6-
newtype Declarations1Query = TDoNotDeclareOrDefineAReservedIdentifierQuery()
6+
newtype Declarations1Query =
7+
TDeclareIdentifiersBeforeUsingThemQuery() or
8+
TDoNotDeclareOrDefineAReservedIdentifierQuery()
79

810
predicate isDeclarations1QueryMetadata(Query query, string queryId, string ruleId) {
11+
query =
12+
// `Query` instance for the `declareIdentifiersBeforeUsingThem` query
13+
Declarations1Package::declareIdentifiersBeforeUsingThemQuery() and
14+
queryId =
15+
// `@id` for the `declareIdentifiersBeforeUsingThem` query
16+
"c/cert/declare-identifiers-before-using-them" and
17+
ruleId = "DCL31-C"
18+
or
919
query =
1020
// `Query` instance for the `doNotDeclareOrDefineAReservedIdentifier` query
1121
Declarations1Package::doNotDeclareOrDefineAReservedIdentifierQuery() and
@@ -16,6 +26,13 @@ predicate isDeclarations1QueryMetadata(Query query, string queryId, string ruleI
1626
}
1727

1828
module Declarations1Package {
29+
Query declareIdentifiersBeforeUsingThemQuery() {
30+
//autogenerate `Query` type
31+
result =
32+
// `Query` type for `declareIdentifiersBeforeUsingThem` query
33+
TQueryC(TDeclarations1PackageQuery(TDeclareIdentifiersBeforeUsingThemQuery()))
34+
}
35+
1936
Query doNotDeclareOrDefineAReservedIdentifierQuery() {
2037
//autogenerate `Query` type
2138
result =

rule_packages/c/Declarations1.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
{
22
"CERT-C": {
3+
"DCL31-C": {
4+
"properties": {
5+
"obligation": "rule"
6+
},
7+
"queries": [
8+
{
9+
"description": "Omission of type specifiers may not be supported by some compilers.",
10+
"kind": "problem",
11+
"name": "Declare identifiers before using them",
12+
"precision": "very-high",
13+
"severity": "error",
14+
"short_name": "DeclareIdentifiersBeforeUsingThem",
15+
"tags": [
16+
"correctness",
17+
"readability"
18+
],
19+
"implementation_scope": {
20+
"description": "This query does not check for implicit function declarations as this is partially compiler checked.",
21+
"items": []
22+
}
23+
}
24+
],
25+
"title": "Declare identifiers before using them"
26+
},
327
"DCL37-C": {
428
"properties": {
529
"obligation": "rule"

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ c,CERT-C,CON40-C,Yes,Rule,,,Do not refer to an atomic variable twice in an expre
499499
c,CERT-C,CON41-C,Yes,Rule,,,Wrap functions that can fail spuriously in a loop,CON53-CPP,Concurrency3,Medium,
500500
c,CERT-C,CON43-C,OutOfScope,Rule,,,Do not allow data races in multithreaded code,,,,
501501
c,CERT-C,DCL30-C,Yes,Rule,,,Declare objects with appropriate storage durations,,Declarations,Hard,
502-
c,CERT-C,DCL31-C,Yes,Rule,,,Declare identifiers before using them,,Declarations,Medium,
502+
c,CERT-C,DCL31-C,Yes,Rule,,,Declare identifiers before using them,,Declarations1,Medium,
503503
c,CERT-C,DCL36-C,Yes,Rule,,,Do not declare an identifier with conflicting linkage classifications,,Declarations,Medium,
504504
c,CERT-C,DCL37-C,Yes,Rule,,,Do not declare or define a reserved identifier,,Declarations1,Easy,
505505
c,CERT-C,DCL38-C,Yes,Rule,,,Use the correct syntax when declaring a flexible array member,,Declarations,Easy,

0 commit comments

Comments
 (0)