|
| 1 | +/** |
| 2 | + * @id c/misra/function-types-not-in-prototype-form |
| 3 | + * @name RULE-8-2: Function types shall be in prototype form with named parameters |
| 4 | + * @description Omission of parameter types or names prevents the compiler from doing type checking |
| 5 | + * when those functions are used and therefore may result in undefined behaviour. |
| 6 | + * @kind problem |
| 7 | + * @precision medium |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/misra/id/rule-8-2 |
| 10 | + * correctness |
| 11 | + * external/misra/obligation/required |
| 12 | + */ |
| 13 | + |
| 14 | +import cpp |
| 15 | +import codingstandards.c.misra |
| 16 | +import codingstandards.cpp.Identifiers |
| 17 | + |
| 18 | +/** |
| 19 | + * `Parameter`s without names |
| 20 | + */ |
| 21 | +class UnnamedParameter extends Parameter { |
| 22 | + UnnamedParameter() { not this.isNamed() } |
| 23 | +} |
| 24 | + |
| 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 | + |
| 39 | +from Function f, string msg |
| 40 | +where |
| 41 | + not isExcluded(f, Declarations4Package::functionTypesNotInPrototypeFormQuery()) and |
| 42 | + f instanceof InterestingIdentifiers and |
| 43 | + ( |
| 44 | + f.getAParameter() instanceof UnnamedParameter and |
| 45 | + msg = "Function " + f + " declares parameter that is unnamed." |
| 46 | + or |
| 47 | + hasZeroParamDecl(f) and |
| 48 | + msg = "Function " + f + " does not specifiy void for no parameters present." |
| 49 | + or |
| 50 | + //parameters declared in declaration list (not in function signature) |
| 51 | + //have placeholder file location associated only |
| 52 | + exists(Parameter p | |
| 53 | + p.getFunction() = f and |
| 54 | + not p.getFile() = f.getFile() and |
| 55 | + msg = "Function " + f + " declares parameter in unsupported declaration list." |
| 56 | + ) |
| 57 | + ) |
| 58 | +select f, msg |
0 commit comments