diff --git a/c/misra/src/rules/RULE-8-18/DefinitionsInHeader.ql b/c/misra/src/rules/RULE-8-18/DefinitionsInHeader.ql new file mode 100644 index 000000000..54459d202 --- /dev/null +++ b/c/misra/src/rules/RULE-8-18/DefinitionsInHeader.ql @@ -0,0 +1,104 @@ +/** + * @id c/misra/definitions-in-header + * @name RULE-8-18: There shall be no tentative definitions in a header file + * @description Tentative definitions in headers can introduce unintended definitions in multiple + * translation units. + * @kind problem + * @precision high + * @problem.severity warning + * @tags external/misra/id/rule-8-18 + * correctness + * external/misra/c/2025 + * external/misra/obligation/required + */ + +import cpp +import codingstandards.c.misra + +/** + * Gets the outermost macro invocation that expands `element`, if any, such as + * `DEFINE_OBJECT(0, x)` for `x` in `#define DEFINE_OBJECT(value, name) int name = value`. + */ +MacroInvocation getOutermostMacroInvocation(Locatable element) { + element = result.getAnExpandedElement() and + not exists(result.getParentInvocation()) +} + +/** + * Holds if `invocation` is an outermost macro invocation that expands `entry` together with at + * least part of `initializer`, such as in `#define DEFINE_OBJECT(value, name) int name = value`. + */ +predicate hasMacroInitializer( + VariableDeclarationEntry entry, MacroInvocation invocation, Initializer initializer +) { + entry.isDefinition() and + initializer = entry.getVariable().getInitializer() and + invocation = getOutermostMacroInvocation(entry) and + invocation = getOutermostMacroInvocation(initializer.getExpr().getAChild*()) +} + +/** + * Holds if `initializer` follows `entry` in source order in every macro expansion at + * `initializer`'s location without an intervening definition of the same variable, such as in + * `int global_object = 1;`. + */ +predicate hasSourceOrderInitializer(VariableDeclarationEntry entry, Initializer initializer) { + entry.isDefinition() and + initializer = entry.getVariable().getInitializer() and + not hasMacroInitializer(_, _, initializer) and + entry.getLocation().isBefore(initializer.getLocation(), _) and + not exists(VariableDeclarationEntry later | + later.getVariable() = entry.getVariable() and + later.isDefinition() and + entry.getLocation().isBefore(later.getLocation(), _) and + later.getLocation().isBefore(initializer.getLocation(), _) + ) and + not exists(MacroInvocation initMacro, MacroInvocation otherMacro | + initMacro = getOutermostMacroInvocation(initializer.getExpr()) and + otherMacro.getLocation() = initMacro.getLocation() and + not exists(otherMacro.getParentInvocation()) and + not exists(Initializer otherInitializer | + otherInitializer = entry.getVariable().getInitializer() and + otherMacro = getOutermostMacroInvocation(otherInitializer.getExpr()) + ) + ) +} + +/** + * A variable declaration entry that defines and initializes a variable in every expansion, for + * example: + * ```c + * int global_object = 1; + * ``` + */ +class InitializedDefinition extends VariableDeclarationEntry { + InitializedDefinition() { + hasSourceOrderInitializer(this, _) + or + forex(MacroInvocation invocation | invocation = getOutermostMacroInvocation(this) | + hasMacroInitializer(this, invocation, _) + ) + } +} + +/** + * A tentative definition of a file-scope variable, for example: + * ```c + * int global_object; + * static int internal_object; + * ``` + */ +class TentativeDefinition extends VariableDeclarationEntry { + TentativeDefinition() { + this.getVariable() instanceof GlobalVariable and + not this.hasSpecifier("extern") and + not this instanceof InitializedDefinition + } +} + +from TentativeDefinition definition +where + not isExcluded(definition, Declarations5Package::definitionsInHeaderQuery()) and + exists(Include include | include.getIncludedFile() = definition.getFile()) +select definition, + "Tentative definition of '" + definition.getName() + "' appears in a header file." diff --git a/c/misra/test/rules/RULE-8-18/DefinitionsInHeader.expected b/c/misra/test/rules/RULE-8-18/DefinitionsInHeader.expected new file mode 100644 index 000000000..ec2cfd20d --- /dev/null +++ b/c/misra/test/rules/RULE-8-18/DefinitionsInHeader.expected @@ -0,0 +1,14 @@ +| conditional.h:4:5:4:21 | definition of conditional_after | Tentative definition of 'conditional_after' appears in a header file. | +| conditional.h:8:5:8:22 | definition of conditional_before | Tentative definition of 'conditional_before' appears in a header file. | +| conditional.h:23:20:23:43 | definition of conditional_macro_object | Tentative definition of 'conditional_macro_object' appears in a header file. | +| conditional.h:24:5:24:29 | definition of conditional_optional_init | Tentative definition of 'conditional_optional_init' appears in a header file. | +| sample.h:4:5:4:20 | definition of header_tentative | Tentative definition of 'header_tentative' appears in a header file. | +| sample.h:5:12:5:24 | definition of static_object | Tentative definition of 'static_object' appears in a header file. | +| sample.h:6:11:6:22 | definition of const_object | Tentative definition of 'const_object' appears in a header file. | +| sample.h:9:22:9:34 | definition of uninitialized | Tentative definition of 'uninitialized' appears in a header file. | +| sample.h:12:5:12:25 | declaration of initialized_elsewhere | Tentative definition of 'initialized_elsewhere' appears in a header file. | +| sample.h:13:5:13:22 | declaration of same_header_object | Tentative definition of 'same_header_object' appears in a header file. | +| sample.h:18:5:18:21 | definition of redeclared_object | Tentative definition of 'redeclared_object' appears in a header file. | +| sample.h:23:16:23:34 | definition of macro_header_object | Tentative definition of 'macro_header_object' appears in a header file. | +| sample.h:25:5:25:19 | definition of cross_tu_object | Tentative definition of 'cross_tu_object' appears in a header file. | +| sample.h:36:21:36:40 | definition of macro_pair_tentative | Tentative definition of 'macro_pair_tentative' appears in a header file. | diff --git a/c/misra/test/rules/RULE-8-18/DefinitionsInHeader.qlref b/c/misra/test/rules/RULE-8-18/DefinitionsInHeader.qlref new file mode 100644 index 000000000..d43d319b9 --- /dev/null +++ b/c/misra/test/rules/RULE-8-18/DefinitionsInHeader.qlref @@ -0,0 +1 @@ +rules/RULE-8-18/DefinitionsInHeader.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-8-18/conditional.h b/c/misra/test/rules/RULE-8-18/conditional.h new file mode 100644 index 000000000..e3768a444 --- /dev/null +++ b/c/misra/test/rules/RULE-8-18/conditional.h @@ -0,0 +1,29 @@ +#ifdef INITIALIZE_CONDITIONAL +int conditional_after = 1; // COMPLIANT +#else +int conditional_after; // NON_COMPLIANT +#endif + +#ifndef INITIALIZE_CONDITIONAL +int conditional_before; // NON_COMPLIANT +#else +int conditional_before = 1; // COMPLIANT +#endif + +#ifdef INITIALIZE_CONDITIONAL +#define CONDITIONAL_OBJECT(name) int name = 1 +#define OPTIONAL_INITIALIZER = 1 +#define DECLARE_OR_DEFINE(name, value) int name = value +#else +#define CONDITIONAL_OBJECT(name) int name +#define OPTIONAL_INITIALIZER +#define DECLARE_OR_DEFINE(name, value) extern int name +#endif + +CONDITIONAL_OBJECT(conditional_macro_object); // NON_COMPLIANT +int conditional_optional_init OPTIONAL_INITIALIZER; // NON_COMPLIANT +DECLARE_OR_DEFINE(conditional_extern_or_defined, 1); // COMPLIANT + +#ifdef INITIALIZE_CONDITIONAL +CONDITIONAL_OBJECT(conditional_macro_when_initialized); // COMPLIANT +#endif diff --git a/c/misra/test/rules/RULE-8-18/other.c b/c/misra/test/rules/RULE-8-18/other.c new file mode 100644 index 000000000..8645d9f3a --- /dev/null +++ b/c/misra/test/rules/RULE-8-18/other.c @@ -0,0 +1,5 @@ +// semmle-extractor-options:--clang -std=c11 -nostdinc +int cross_tu_object = 0; // COMPLIANT + +#define INITIALIZE_CONDITIONAL +#include "conditional.h" diff --git a/c/misra/test/rules/RULE-8-18/sample.h b/c/misra/test/rules/RULE-8-18/sample.h new file mode 100644 index 000000000..1f5109269 --- /dev/null +++ b/c/misra/test/rules/RULE-8-18/sample.h @@ -0,0 +1,36 @@ +// Only uninitialized file-scope objects without extern are tentative. +extern int external_declaration; // COMPLIANT +int initialized_in_header = 0; // COMPLIANT +int header_tentative; // NON_COMPLIANT +static int static_object; // NON_COMPLIANT +const int const_object; // NON_COMPLIANT + +// An initializer applies only to its own declarator. +int initialized = 1, uninitialized; // NON_COMPLIANT - uninitialized + +// Later definitions do not excuse earlier tentative declarations. +int initialized_elsewhere; // NON_COMPLIANT - initialized in test.c +int same_header_object; // NON_COMPLIANT +int same_header_object = 1; // COMPLIANT + +// Extern declarations around a tentative declaration remain compliant. +extern int redeclared_object; // COMPLIANT +int redeclared_object; // NON_COMPLIANT +extern int redeclared_object; // COMPLIANT + +// Macro-generated declarations follow their expansion file. +#define DECLARE_OBJECT(name) int name +DECLARE_OBJECT(macro_header_object); // NON_COMPLIANT + +int cross_tu_object; // NON_COMPLIANT + +#define DEFINE_INITIALIZED_OBJECT(name) int name = 0 +DEFINE_INITIALIZED_OBJECT(macro_initialized_object); // COMPLIANT + +#define DEFINE_REVERSED_OBJECT(value, name) int name = value +DEFINE_REVERSED_OBJECT(0, macro_reversed_object); // COMPLIANT + +#define DEFINE_PARTIAL_PAIR(value, initialized_name, tentative_name) \ + int initialized_name = value, tentative_name +DEFINE_PARTIAL_PAIR(0, macro_pair_initialized, // COMPLIANT + macro_pair_tentative); // NON_COMPLIANT diff --git a/c/misra/test/rules/RULE-8-18/test.c b/c/misra/test/rules/RULE-8-18/test.c new file mode 100644 index 000000000..7baff1df4 --- /dev/null +++ b/c/misra/test/rules/RULE-8-18/test.c @@ -0,0 +1,13 @@ +// semmle-extractor-options:--clang -std=c11 -nostdinc +#include "conditional.h" +#include "sample.h" + +// Source definitions are allowed, including completion of header declarations. +int initialized_elsewhere = 0; // COMPLIANT +int source_tentative; // COMPLIANT +DECLARE_OBJECT(macro_source_object); // COMPLIANT + +// Static locals are not file-scope tentative definitions. +void local_objects(void) { + static int static_local; // COMPLIANT +} \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations5.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations5.qll index b1fe8eacb..63c5a5c34 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations5.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations5.qll @@ -8,7 +8,8 @@ newtype Declarations5Query = TExternalObjectOrFunctionNotDeclaredInOneFileQuery() or TMissingStaticSpecifierFunctionRedeclarationCQuery() or TMissingStaticSpecifierObjectRedeclarationCQuery() or - TUnnecessaryExposedIdentifierDeclarationCQuery() + TUnnecessaryExposedIdentifierDeclarationCQuery() or + TDefinitionsInHeaderQuery() predicate isDeclarations5QueryMetadata(Query query, string queryId, string ruleId, string category) { query = @@ -55,6 +56,15 @@ predicate isDeclarations5QueryMetadata(Query query, string queryId, string ruleI "c/misra/unnecessary-exposed-identifier-declaration-c" and ruleId = "RULE-8-9" and category = "advisory" + or + query = + // `Query` instance for the `definitionsInHeader` query + Declarations5Package::definitionsInHeaderQuery() and + queryId = + // `@id` for the `definitionsInHeader` query + "c/misra/definitions-in-header" and + ruleId = "RULE-8-18" and + category = "required" } module Declarations5Package { @@ -92,4 +102,11 @@ module Declarations5Package { // `Query` type for `unnecessaryExposedIdentifierDeclarationC` query TQueryC(TDeclarations5PackageQuery(TUnnecessaryExposedIdentifierDeclarationCQuery())) } + + Query definitionsInHeaderQuery() { + //autogenerate `Query` type + result = + // `Query` type for `definitionsInHeader` query + TQueryC(TDeclarations5PackageQuery(TDefinitionsInHeaderQuery())) + } } diff --git a/rule_packages/c/Declarations5.json b/rule_packages/c/Declarations5.json index 0c86cad0c..275f9a693 100644 --- a/rule_packages/c/Declarations5.json +++ b/rule_packages/c/Declarations5.json @@ -107,7 +107,23 @@ "properties": { "obligation": "required" }, - "queries": [], + "queries": [ + { + "description": "Tentative definitions in headers can introduce unintended definitions in multiple translation units.", + "kind": "problem", + "name": "There shall be no tentative definitions in a header file", + "precision": "high", + "severity": "warning", + "short_name": "DefinitionsInHeader", + "tags": [ + "correctness", + "external/misra/c/2025" + ], + "implementation_scope": { + "description": "Reports tentative file-scope declarations in included files of any extension. Excludes extern declarations and initialized definitions." + } + } + ], "title": "There shall be no tentative definitions in a header file" }, "RULE-8-19": { diff --git a/rules-misra-c-2025.csv b/rules-misra-c-2025.csv index 31aa6113d..7436c24c3 100644 --- a/rules-misra-c-2025.csv +++ b/rules-misra-c-2025.csv @@ -4,7 +4,7 @@ c,MISRA-C-2025,Edition2025,DIR-4-6,Yes,Refine,No,Medium c,MISRA-C-2025,Edition2025,DIR-4-10,Yes,Expand,No,Medium c,MISRA-C-2025,Edition2025,RULE-1-2,Yes,Renumber,Yes,Easy c,MISRA-C-2025,Edition2025,RULE-5-10,Yes,Renumber,Yes,Easy -c,MISRA-C-2025,Edition2025,RULE-8-18,Yes,New,No,Medium +c,MISRA-C-2025,Edition2025,RULE-8-18,Yes,New,Yes,Medium c,MISRA-C-2025,Edition2025,RULE-8-19,Yes,New,No,Medium c,MISRA-C-2025,Edition2025,RULE-10-1,Yes,Refine,No,Hard c,MISRA-C-2025,Edition2025,RULE-11-3,Yes,Refine,No,Medium diff --git a/scripts/release/create_supported_rules_list_test.py b/scripts/release/create_supported_rules_list_test.py index 765b43e0c..124f1b387 100644 --- a/scripts/release/create_supported_rules_list_test.py +++ b/scripts/release/create_supported_rules_list_test.py @@ -85,7 +85,7 @@ def test_applicability_is_filtered_per_query(tmp_path): assert rows["MISRA-C-2025", "RULE-1-1"] == "Current.ql" -def test_renumbered_rules_do_not_leak_into_older_editions(tmp_path): +def test_renumbered_and_new_rules_do_not_leak_into_older_editions(tmp_path): rows = supported_rules(tmp_path, { "MISRA-C-2012": { "RULE-21-1": {"queries": [{ @@ -94,14 +94,16 @@ def test_renumbered_rules_do_not_leak_into_older_editions(tmp_path): }]}, }, "MISRA-C-2025": { - "RULE-20-15": {"queries": [{ - "short_name": "ReservedMacroName", - "tags": [CURRENT_EDITION], - }]}, + rule: {"queries": [{"short_name": name, "tags": [CURRENT_EDITION]}]} + for rule, name in ( + ("RULE-20-15", "ReservedMacroName"), + ("RULE-8-18", "DefinitionsInHeader"), + ) }, }) assert ("MISRA-C-2025", "RULE-21-1") not in rows assert ("MISRA-C-2012", "RULE-20-15") not in rows + assert ("MISRA-C-2012", "RULE-8-18") not in rows assert rows["MISRA-C-2025", "RULE-20-15"] == "ReservedMacroName.ql"