Skip to content

Commit 2f23fe2

Browse files
committed
Declarations4: improve RULE-8-2
1 parent be0eca9 commit 2f23fe2

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

c/misra/src/rules/RULE-8-2/FunctionTypesNotInPrototypeForm.ql

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ class UnnamedParameter extends Parameter {
2222
UnnamedParameter() { not this.isNamed() }
2323
}
2424

25+
/*
26+
* This is a copy of the private `hasZeroParamDecl` predicate from the standard set of
27+
* queries as of the `codeql-cli/2.11.2` tag in `github/codeql`.
28+
*/
29+
30+
predicate hasZeroParamDecl(Function f) {
31+
exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() |
32+
not fde.isImplicit() and
33+
not fde.hasVoidParamList() and
34+
fde.getNumberOfParameters() = 0 and
35+
not fde.isDefinition()
36+
)
37+
}
38+
2539
from Function f, string msg
2640
where
2741
not isExcluded(f, Declarations4Package::functionTypesNotInPrototypeFormQuery()) and
@@ -30,11 +44,8 @@ where
3044
f.getAParameter() instanceof UnnamedParameter and
3145
msg = "Function " + f + " declares parameter that is unnamed."
3246
or
33-
//void keyword not present in function signature, no way to tell which
34-
not exists(f.getAParameter()) and
35-
msg =
36-
"Function " + f +
37-
" may not specify all parameter types or may not specifiy void for no parameters present."
47+
hasZeroParamDecl(f) and
48+
msg = "Function " + f + " does not specifiy void for no parameters present."
3849
or
3950
exists(Parameter p |
4051
p.getFunction() = f and
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
| test.c:2:6:2:7 | f0 | Function f0 may not specify all parameter types or may not specifiy void for no parameters present. |
21
| test.c:3:6:3:7 | f1 | Function f1 declares parameter that is unnamed. |
3-
| test.c:4:6:4:7 | f2 | Function f2 may not specify all parameter types or may not specifiy void for no parameters present. |
4-
| test.c:5:6:5:7 | f3 | Function f3 may not specify all parameter types or may not specifiy void for no parameters present. |
2+
| test.c:4:6:4:7 | f2 | Function f2 does not specifiy void for no parameters present. |
3+
| test.c:5:6:5:7 | f3 | Function f3 does not specifiy void for no parameters present. |
54
| test.c:7:5:7:6 | f5 | Function f5 declares parameter in unsupported declaration list. |

c/misra/test/rules/RULE-8-2/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
void f(int x); // COMPLIANT
2-
void f0(void); // COMPLIANT[FALSE_POSITIVE]
2+
void f0(void); // COMPLIANT
33
void f1(int); // NON_COMPLIANT
44
void f2(); // NON_COMPLIANT
55
void f3(x); // NON_COMPLIANT

0 commit comments

Comments
 (0)