|
| 1 | +/** |
| 2 | + * @id c/misra/definitions-in-header |
| 3 | + * @name RULE-8-18: There shall be no tentative definitions in a header file |
| 4 | + * @description Tentative definitions in headers can introduce unintended definitions in multiple |
| 5 | + * translation units. |
| 6 | + * @kind problem |
| 7 | + * @precision high |
| 8 | + * @problem.severity warning |
| 9 | + * @tags external/misra/id/rule-8-18 |
| 10 | + * correctness |
| 11 | + * external/misra/c/2025 |
| 12 | + * external/misra/obligation/required |
| 13 | + */ |
| 14 | + |
| 15 | +import cpp |
| 16 | +import codingstandards.c.misra |
| 17 | + |
| 18 | +/** |
| 19 | + * Gets the outermost macro invocation that expands `element`, if any, such as |
| 20 | + * `DEFINE_OBJECT(0, x)` for `x` in `#define DEFINE_OBJECT(value, name) int name = value`. |
| 21 | + */ |
| 22 | +MacroInvocation getOutermostMacroInvocation(Locatable element) { |
| 23 | + element = result.getAnExpandedElement() and |
| 24 | + not exists(result.getParentInvocation()) |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Holds if `invocation` is an outermost macro invocation that expands `entry` together with at |
| 29 | + * least part of `initializer`, such as in `#define DEFINE_OBJECT(value, name) int name = value`. |
| 30 | + */ |
| 31 | +predicate hasMacroInitializer( |
| 32 | + VariableDeclarationEntry entry, MacroInvocation invocation, Initializer initializer |
| 33 | +) { |
| 34 | + entry.isDefinition() and |
| 35 | + initializer = entry.getVariable().getInitializer() and |
| 36 | + invocation = getOutermostMacroInvocation(entry) and |
| 37 | + invocation = getOutermostMacroInvocation(initializer.getExpr().getAChild*()) |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Holds if `initializer` follows `entry` in source order in every macro expansion at |
| 42 | + * `initializer`'s location without an intervening definition of the same variable, such as in |
| 43 | + * `int global_object = 1;`. |
| 44 | + */ |
| 45 | +predicate hasSourceOrderInitializer(VariableDeclarationEntry entry, Initializer initializer) { |
| 46 | + entry.isDefinition() and |
| 47 | + initializer = entry.getVariable().getInitializer() and |
| 48 | + not hasMacroInitializer(_, _, initializer) and |
| 49 | + entry.getLocation().isBefore(initializer.getLocation(), _) and |
| 50 | + not exists(VariableDeclarationEntry later | |
| 51 | + later.getVariable() = entry.getVariable() and |
| 52 | + later.isDefinition() and |
| 53 | + entry.getLocation().isBefore(later.getLocation(), _) and |
| 54 | + later.getLocation().isBefore(initializer.getLocation(), _) |
| 55 | + ) and |
| 56 | + not exists(MacroInvocation initMacro, MacroInvocation otherMacro | |
| 57 | + initMacro = getOutermostMacroInvocation(initializer.getExpr()) and |
| 58 | + otherMacro.getLocation() = initMacro.getLocation() and |
| 59 | + not exists(otherMacro.getParentInvocation()) and |
| 60 | + not exists(Initializer otherInitializer | |
| 61 | + otherInitializer = entry.getVariable().getInitializer() and |
| 62 | + otherMacro = getOutermostMacroInvocation(otherInitializer.getExpr()) |
| 63 | + ) |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * A variable declaration entry that defines and initializes a variable in every expansion, for |
| 69 | + * example: |
| 70 | + * ```c |
| 71 | + * int global_object = 1; |
| 72 | + * ``` |
| 73 | + */ |
| 74 | +class InitializedDefinition extends VariableDeclarationEntry { |
| 75 | + InitializedDefinition() { |
| 76 | + hasSourceOrderInitializer(this, _) |
| 77 | + or |
| 78 | + forex(MacroInvocation invocation | invocation = getOutermostMacroInvocation(this) | |
| 79 | + hasMacroInitializer(this, invocation, _) |
| 80 | + ) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * A tentative definition of a file-scope variable, for example: |
| 86 | + * ```c |
| 87 | + * int global_object; |
| 88 | + * static int internal_object; |
| 89 | + * ``` |
| 90 | + */ |
| 91 | +class TentativeDefinition extends VariableDeclarationEntry { |
| 92 | + TentativeDefinition() { |
| 93 | + this.getVariable() instanceof GlobalVariable and |
| 94 | + not this.hasSpecifier("extern") and |
| 95 | + not this instanceof InitializedDefinition |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +from TentativeDefinition definition |
| 100 | +where |
| 101 | + not isExcluded(definition, Declarations5Package::definitionsInHeaderQuery()) and |
| 102 | + exists(Include include | include.getIncludedFile() = definition.getFile()) |
| 103 | +select definition, |
| 104 | + "Tentative definition of '" + definition.getName() + "' appears in a header file." |
0 commit comments