Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions c/misra/src/rules/RULE-8-18/DefinitionsInHeader.ql
Original file line number Diff line number Diff line change
@@ -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."
11 changes: 11 additions & 0 deletions c/misra/test/rules/RULE-8-18/DefinitionsInHeader.expected
Original file line number Diff line number Diff line change
@@ -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. |
1 change: 1 addition & 0 deletions c/misra/test/rules/RULE-8-18/DefinitionsInHeader.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-8-18/DefinitionsInHeader.ql
11 changes: 11 additions & 0 deletions c/misra/test/rules/RULE-8-18/conditional.h
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions c/misra/test/rules/RULE-8-18/other.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// semmle-extractor-options:--clang -std=c11 -nostdinc
int cross_tu_object = 0; // COMPLIANT

#define INITIALIZE_CONDITIONAL
#include "conditional.h"
28 changes: 28 additions & 0 deletions c/misra/test/rules/RULE-8-18/sample.h
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions c/misra/test/rules/RULE-8-18/test.c
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()))
}
}
18 changes: 17 additions & 1 deletion rule_packages/c/Declarations5.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion rules-misra-c-2025.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions scripts/release/create_supported_rules_list_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [{
Expand All @@ -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"


Expand Down
Loading