Skip to content

Commit 3ef95e6

Browse files
mbaludaCopilot
andcommitted
Implement MISRA C:2025 Rule 8.18
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 26a444d commit 3ef95e6

11 files changed

Lines changed: 245 additions & 8 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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."
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
| conditional.h:4:5:4:21 | definition of conditional_after | Tentative definition of 'conditional_after' appears in a header file. |
2+
| conditional.h:8:5:8:22 | definition of conditional_before | Tentative definition of 'conditional_before' appears in a header file. |
3+
| conditional.h:23:20:23:43 | definition of conditional_macro_object | Tentative definition of 'conditional_macro_object' appears in a header file. |
4+
| conditional.h:24:5:24:29 | definition of conditional_optional_init | Tentative definition of 'conditional_optional_init' appears in a header file. |
5+
| sample.h:4:5:4:20 | definition of header_tentative | Tentative definition of 'header_tentative' appears in a header file. |
6+
| sample.h:5:12:5:24 | definition of static_object | Tentative definition of 'static_object' appears in a header file. |
7+
| sample.h:6:11:6:22 | definition of const_object | Tentative definition of 'const_object' appears in a header file. |
8+
| sample.h:9:22:9:34 | definition of uninitialized | Tentative definition of 'uninitialized' appears in a header file. |
9+
| sample.h:12:5:12:25 | declaration of initialized_elsewhere | Tentative definition of 'initialized_elsewhere' appears in a header file. |
10+
| sample.h:13:5:13:22 | declaration of same_header_object | Tentative definition of 'same_header_object' appears in a header file. |
11+
| sample.h:18:5:18:21 | definition of redeclared_object | Tentative definition of 'redeclared_object' appears in a header file. |
12+
| sample.h:23:16:23:34 | definition of macro_header_object | Tentative definition of 'macro_header_object' appears in a header file. |
13+
| sample.h:25:5:25:19 | definition of cross_tu_object | Tentative definition of 'cross_tu_object' appears in a header file. |
14+
| sample.h:36:21:36:40 | definition of macro_pair_tentative | Tentative definition of 'macro_pair_tentative' appears in a header file. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-18/DefinitionsInHeader.ql
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifdef INITIALIZE_CONDITIONAL
2+
int conditional_after = 1; // COMPLIANT
3+
#else
4+
int conditional_after; // NON_COMPLIANT
5+
#endif
6+
7+
#ifndef INITIALIZE_CONDITIONAL
8+
int conditional_before; // NON_COMPLIANT
9+
#else
10+
int conditional_before = 1; // COMPLIANT
11+
#endif
12+
13+
#ifdef INITIALIZE_CONDITIONAL
14+
#define CONDITIONAL_OBJECT(name) int name = 1
15+
#define OPTIONAL_INITIALIZER = 1
16+
#define DECLARE_OR_DEFINE(name, value) int name = value
17+
#else
18+
#define CONDITIONAL_OBJECT(name) int name
19+
#define OPTIONAL_INITIALIZER
20+
#define DECLARE_OR_DEFINE(name, value) extern int name
21+
#endif
22+
23+
CONDITIONAL_OBJECT(conditional_macro_object); // NON_COMPLIANT
24+
int conditional_optional_init OPTIONAL_INITIALIZER; // NON_COMPLIANT
25+
DECLARE_OR_DEFINE(conditional_extern_or_defined, 1); // COMPLIANT
26+
27+
#ifdef INITIALIZE_CONDITIONAL
28+
CONDITIONAL_OBJECT(conditional_macro_when_initialized); // COMPLIANT
29+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// semmle-extractor-options:--clang -std=c11 -nostdinc
2+
int cross_tu_object = 0; // COMPLIANT
3+
4+
#define INITIALIZE_CONDITIONAL
5+
#include "conditional.h"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Only uninitialized file-scope objects without extern are tentative.
2+
extern int external_declaration; // COMPLIANT
3+
int initialized_in_header = 0; // COMPLIANT
4+
int header_tentative; // NON_COMPLIANT
5+
static int static_object; // NON_COMPLIANT
6+
const int const_object; // NON_COMPLIANT
7+
8+
// An initializer applies only to its own declarator.
9+
int initialized = 1, uninitialized; // NON_COMPLIANT - uninitialized
10+
11+
// Later definitions do not excuse earlier tentative declarations.
12+
int initialized_elsewhere; // NON_COMPLIANT - initialized in test.c
13+
int same_header_object; // NON_COMPLIANT
14+
int same_header_object = 1; // COMPLIANT
15+
16+
// Extern declarations around a tentative declaration remain compliant.
17+
extern int redeclared_object; // COMPLIANT
18+
int redeclared_object; // NON_COMPLIANT
19+
extern int redeclared_object; // COMPLIANT
20+
21+
// Macro-generated declarations follow their expansion file.
22+
#define DECLARE_OBJECT(name) int name
23+
DECLARE_OBJECT(macro_header_object); // NON_COMPLIANT
24+
25+
int cross_tu_object; // NON_COMPLIANT
26+
27+
#define DEFINE_INITIALIZED_OBJECT(name) int name = 0
28+
DEFINE_INITIALIZED_OBJECT(macro_initialized_object); // COMPLIANT
29+
30+
#define DEFINE_REVERSED_OBJECT(value, name) int name = value
31+
DEFINE_REVERSED_OBJECT(0, macro_reversed_object); // COMPLIANT
32+
33+
#define DEFINE_PARTIAL_PAIR(value, initialized_name, tentative_name) \
34+
int initialized_name = value, tentative_name
35+
DEFINE_PARTIAL_PAIR(0, macro_pair_initialized, // COMPLIANT
36+
macro_pair_tentative); // NON_COMPLIANT
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// semmle-extractor-options:--clang -std=c11 -nostdinc
2+
#include "conditional.h"
3+
#include "sample.h"
4+
5+
// Source definitions are allowed, including completion of header declarations.
6+
int initialized_elsewhere = 0; // COMPLIANT
7+
int source_tentative; // COMPLIANT
8+
DECLARE_OBJECT(macro_source_object); // COMPLIANT
9+
10+
// Static locals are not file-scope tentative definitions.
11+
void local_objects(void) {
12+
static int static_local; // COMPLIANT
13+
}

‎cpp/common/src/codingstandards/cpp/exclusions/c/Declarations5.qll‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ newtype Declarations5Query =
88
TExternalObjectOrFunctionNotDeclaredInOneFileQuery() or
99
TMissingStaticSpecifierFunctionRedeclarationCQuery() or
1010
TMissingStaticSpecifierObjectRedeclarationCQuery() or
11-
TUnnecessaryExposedIdentifierDeclarationCQuery()
11+
TUnnecessaryExposedIdentifierDeclarationCQuery() or
12+
TDefinitionsInHeaderQuery()
1213

1314
predicate isDeclarations5QueryMetadata(Query query, string queryId, string ruleId, string category) {
1415
query =
@@ -55,6 +56,15 @@ predicate isDeclarations5QueryMetadata(Query query, string queryId, string ruleI
5556
"c/misra/unnecessary-exposed-identifier-declaration-c" and
5657
ruleId = "RULE-8-9" and
5758
category = "advisory"
59+
or
60+
query =
61+
// `Query` instance for the `definitionsInHeader` query
62+
Declarations5Package::definitionsInHeaderQuery() and
63+
queryId =
64+
// `@id` for the `definitionsInHeader` query
65+
"c/misra/definitions-in-header" and
66+
ruleId = "RULE-8-18" and
67+
category = "required"
5868
}
5969

6070
module Declarations5Package {
@@ -92,4 +102,11 @@ module Declarations5Package {
92102
// `Query` type for `unnecessaryExposedIdentifierDeclarationC` query
93103
TQueryC(TDeclarations5PackageQuery(TUnnecessaryExposedIdentifierDeclarationCQuery()))
94104
}
105+
106+
Query definitionsInHeaderQuery() {
107+
//autogenerate `Query` type
108+
result =
109+
// `Query` type for `definitionsInHeader` query
110+
TQueryC(TDeclarations5PackageQuery(TDefinitionsInHeaderQuery()))
111+
}
95112
}

‎rule_packages/c/Declarations5.json‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,23 @@
107107
"properties": {
108108
"obligation": "required"
109109
},
110-
"queries": [],
110+
"queries": [
111+
{
112+
"description": "Tentative definitions in headers can introduce unintended definitions in multiple translation units.",
113+
"kind": "problem",
114+
"name": "There shall be no tentative definitions in a header file",
115+
"precision": "high",
116+
"severity": "warning",
117+
"short_name": "DefinitionsInHeader",
118+
"tags": [
119+
"correctness",
120+
"external/misra/c/2025"
121+
],
122+
"implementation_scope": {
123+
"description": "Reports tentative file-scope declarations in included files of any extension. Excludes extern declarations and initialized definitions."
124+
}
125+
}
126+
],
111127
"title": "There shall be no tentative definitions in a header file"
112128
},
113129
"RULE-8-19": {

‎rules-misra-c-2025.csv‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ c,MISRA-C-2025,Edition2025,DIR-4-6,Yes,Refine,No,Medium
44
c,MISRA-C-2025,Edition2025,DIR-4-10,Yes,Expand,No,Medium
55
c,MISRA-C-2025,Edition2025,RULE-1-2,Yes,Renumber,Yes,Easy
66
c,MISRA-C-2025,Edition2025,RULE-5-10,Yes,Renumber,Yes,Easy
7-
c,MISRA-C-2025,Edition2025,RULE-8-18,Yes,New,No,Medium
7+
c,MISRA-C-2025,Edition2025,RULE-8-18,Yes,New,Yes,Medium
88
c,MISRA-C-2025,Edition2025,RULE-8-19,Yes,New,No,Medium
99
c,MISRA-C-2025,Edition2025,RULE-10-1,Yes,Refine,No,Hard
1010
c,MISRA-C-2025,Edition2025,RULE-11-3,Yes,Refine,No,Medium

0 commit comments

Comments
 (0)