|
| 1 | +/** |
| 2 | + * @id c/misra/external-object-or-function-not-declared-in-one-file |
| 3 | + * @name RULE-8-5: An external object or function shall be declared once in one and only one file |
| 4 | + * @description Declarations in multiple files can lead to unexpected program behaviour. |
| 5 | + * @kind problem |
| 6 | + * @precision very-high |
| 7 | + * @problem.severity warning |
| 8 | + * @tags external/misra/id/rule-8-5 |
| 9 | + * correctness |
| 10 | + * external/misra/obligation/required |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | +import codingstandards.c.misra |
| 15 | + |
| 16 | +from DeclarationEntry de, DeclarationEntry otherDeclaration, string kind |
| 17 | +where |
| 18 | + not isExcluded(de, Declarations5Package::externalObjectOrFunctionNotDeclaredInOneFileQuery()) and |
| 19 | + //this rule applies to non-defining declarations only |
| 20 | + not de.isDefinition() and |
| 21 | + not otherDeclaration.isDefinition() and |
| 22 | + exists(Declaration d | |
| 23 | + de.getDeclaration() = d and |
| 24 | + otherDeclaration.getDeclaration() = d and |
| 25 | + de.getFile() != otherDeclaration.getFile() |
| 26 | + ) and |
| 27 | + ( |
| 28 | + de.getDeclaration() instanceof Function and kind = "function" |
| 29 | + or |
| 30 | + de.getDeclaration() instanceof Variable and |
| 31 | + not de.getDeclaration() instanceof Parameter and |
| 32 | + kind = "variable" |
| 33 | + ) and |
| 34 | + // Apply an ordering based on location to enforce that (de1, de2) = (de2, de1) and we only report (de1, de2). |
| 35 | + ( |
| 36 | + de.getFile().getAbsolutePath() < otherDeclaration.getFile().getAbsolutePath() |
| 37 | + or |
| 38 | + de.getFile().getAbsolutePath() = otherDeclaration.getFile().getAbsolutePath() and |
| 39 | + de.getLocation().getStartLine() < otherDeclaration.getLocation().getStartLine() |
| 40 | + ) |
| 41 | +select de, |
| 42 | + "The " + kind + " declaration " + de.getName() + |
| 43 | + " is declared in multiple files and has an additional $@.", otherDeclaration, "declaration" |
0 commit comments