Skip to content

Commit a75a79a

Browse files
committed
Declarations1: add testcases and improve type logic for DCL31-C
1 parent 8d8db4f commit a75a79a

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* @id c/cert/declare-identifiers-before-using-them
33
* @name DCL31-C: Declare identifiers before using them
4-
* @description Omission of type specifiers may not be supported by some compilers.
4+
* @description Omission of type specifiers may not be supported by some
5+
* compilers.
56
* @kind problem
67
* @precision very-high
78
* @problem.severity error
@@ -17,5 +18,12 @@ import codingstandards.c.cert
1718
from Declaration d
1819
where
1920
not isExcluded(d, Declarations1Package::declareIdentifiersBeforeUsingThemQuery()) and
20-
d.hasSpecifier("implicit_int")
21+
d.hasSpecifier("implicit_int") and
22+
exists(Type t |
23+
(d.(Variable).getType() = t or d.(Function).getType() = t) and
24+
// Exclude "short" or "long", as opposed to "short int" or "long int".
25+
t instanceof IntType and
26+
// Exclude "signed" or "unsigned", as opposed to "signed int" or "unsigned int".
27+
not exists(IntegralType it | it = t | it.isExplicitlySigned() or it.isExplicitlyUnsigned())
28+
)
2129
select d, "Declaration is missing a type specifier."
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +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. |
1+
| test.c:1:8:1:8 | g | Declaration is missing a type specifier. |
2+
| test.c:5:1:5:1 | f | Declaration is missing a type specifier. |

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
extern g; // NON_COMPLIANT
32

43
extern int g1; // COMPLIANT
@@ -9,4 +8,8 @@ f(void) { // NON_COMPLIANT
98

109
int f1(void) { // COMPLIANT
1110
return 1;
12-
}
11+
}
12+
13+
short g2; // COMPLIANT
14+
long g3; // COMPLIANT
15+
signed g4() { return 1; } // COMPLIANT

0 commit comments

Comments
 (0)