Skip to content

Commit b05dbd3

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 b05dbd3

11 files changed

Lines changed: 180 additions & 8 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
* An initialized definition of a variable, for example:
20+
* ```c
21+
* int global_object = 1;
22+
* ```
23+
*/
24+
class InitializedDefinition extends VariableDeclarationEntry {
25+
InitializedDefinition() {
26+
exists(Initializer initializer |
27+
this.isDefinition() and
28+
initializer = this.getVariable().getInitializer() and
29+
(
30+
initializer.getLocation().subsumes(this.getLocation())
31+
or
32+
// Initializers follow their own declarators, not definitions in other translation units.
33+
this.getLocation().isBefore(initializer.getLocation(), _) and
34+
not exists(VariableDeclarationEntry later |
35+
later.getVariable() = this.getVariable() and
36+
later.isDefinition() and
37+
this.getLocation().isBefore(later.getLocation(), _) and
38+
(
39+
later.getLocation().isBefore(initializer.getLocation(), _) or
40+
initializer.getLocation().subsumes(later.getLocation())
41+
)
42+
)
43+
)
44+
)
45+
}
46+
}
47+
48+
/**
49+
* A tentative definition of a file-scope variable, for example:
50+
* ```c
51+
* int global_object;
52+
* static int internal_object;
53+
* ```
54+
*/
55+
class TentativeDefinition extends VariableDeclarationEntry {
56+
TentativeDefinition() {
57+
this.getVariable() instanceof GlobalVariable and
58+
not this.hasSpecifier("extern") and
59+
not this instanceof InitializedDefinition
60+
}
61+
}
62+
63+
from TentativeDefinition definition
64+
where
65+
not isExcluded(definition, Declarations5Package::definitionsInHeaderQuery()) and
66+
exists(Include include | include.getIncludedFile() = definition.getFile())
67+
select definition,
68+
"Tentative definition of '" + definition.getName() + "' appears in a header file."
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
| sample.h:4:5:4:20 | definition of header_tentative | Tentative definition of 'header_tentative' appears in a header file. |
4+
| sample.h:5:12:5:24 | definition of static_object | Tentative definition of 'static_object' appears in a header file. |
5+
| sample.h:6:11:6:22 | definition of const_object | Tentative definition of 'const_object' appears in a header file. |
6+
| sample.h:9:22:9:34 | definition of uninitialized | Tentative definition of 'uninitialized' appears in a header file. |
7+
| sample.h:12:5:12:25 | declaration of initialized_elsewhere | Tentative definition of 'initialized_elsewhere' appears in a header file. |
8+
| sample.h:13:5:13:22 | declaration of same_header_object | Tentative definition of 'same_header_object' appears in a header file. |
9+
| sample.h:18:5:18:21 | definition of redeclared_object | Tentative definition of 'redeclared_object' appears in a header file. |
10+
| sample.h:23:16:23:34 | definition of macro_header_object | Tentative definition of 'macro_header_object' appears in a header file. |
11+
| sample.h:25:5:25:19 | definition of cross_tu_object | Tentative definition of 'cross_tu_object' 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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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
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)