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..cd23a02cd --- /dev/null +++ b/c/misra/src/rules/RULE-8-18/DefinitionsInHeader.ql @@ -0,0 +1,68 @@ +/** + * @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 + +/** + * An initialized definition of a variable, for example: + * ```c + * int global_object = 1; + * ``` + */ +class InitializedDefinition extends VariableDeclarationEntry { + InitializedDefinition() { + exists(Initializer initializer | + this.isDefinition() and + initializer = this.getVariable().getInitializer() and + ( + initializer.getLocation().subsumes(this.getLocation()) + or + // Initializers follow their own declarators, not definitions in other translation units. + this.getLocation().isBefore(initializer.getLocation(), _) and + not exists(VariableDeclarationEntry later | + later.getVariable() = this.getVariable() and + later.isDefinition() and + this.getLocation().isBefore(later.getLocation(), _) and + ( + later.getLocation().isBefore(initializer.getLocation(), _) or + initializer.getLocation().subsumes(later.getLocation()) + ) + ) + ) + ) + } +} + +/** + * 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..31f190469 --- /dev/null +++ b/c/misra/test/rules/RULE-8-18/DefinitionsInHeader.expected @@ -0,0 +1,11 @@ +| 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. | +| 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. | 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..7bfc51456 --- /dev/null +++ b/c/misra/test/rules/RULE-8-18/conditional.h @@ -0,0 +1,11 @@ +#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 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..77d36872b --- /dev/null +++ b/c/misra/test/rules/RULE-8-18/sample.h @@ -0,0 +1,28 @@ +// 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 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 93b4c2b2d..3224d7390 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,Yes,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"