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
104 changes: 104 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,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."
14 changes: 14 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,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. |
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
29 changes: 29 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,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
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"
36 changes: 36 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,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
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,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
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