Skip to content

Commit 5ee232d

Browse files
committed
Declarations6: add RULE-8-10
1 parent 6672156 commit 5ee232d

File tree

7 files changed

+70
-1
lines changed

7 files changed

+70
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id c/misra/inline-function-not-declared-static-storage
3+
* @name RULE-8-10: An inline function shall be declared with the static storage class
4+
* @description Declaring an inline function with external linkage can lead to undefined or
5+
* incorrect program behaviour.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-10
10+
* correctness
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.Identifiers
17+
18+
from FunctionDeclarationEntry f
19+
where
20+
not isExcluded(f, Declarations6Package::inlineFunctionNotDeclaredStaticStorageQuery()) and
21+
f.getFunction() instanceof InterestingIdentifiers and
22+
f.getFunction().isInline() and
23+
not f.hasSpecifier("static")
24+
select f, "Inline function not explicitly declared static."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:2:20:2:21 | declaration of f1 | Inline function not explicitly declared static. |
2+
| test.c:3:13:3:14 | declaration of f2 | Inline function not explicitly declared static. |
3+
| test.c:4:20:4:20 | declaration of f | Inline function not explicitly declared static. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-10/InlineFunctionNotDeclaredStaticStorage.ql

c/misra/test/rules/RULE-8-10/test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
static inline void f(); // COMPLIANT
2+
extern inline void f1(); // NON_COMPLIANT
3+
inline void f2(); // NON_COMPLIANT
4+
extern inline void f(); // NON_COMPLIANT -while this will be internal linkage it
5+
// is less clear than explicitly specifying static

cpp/common/src/codingstandards/cpp/exclusions/c/Declarations6.qll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ newtype Declarations6Query =
77
TFunctionDeclaredImplicitlyQuery() or
88
TIdentifiersWithExternalLinkageNotUniqueQuery() or
99
TIdentifiersWithInternalLinkageNotUniqueQuery() or
10+
TInlineFunctionNotDeclaredStaticStorageQuery() or
1011
TArrayExternalLinkageSizeExplicitlySpecifiedQuery() or
1112
TShouldNotBeDefinedWithExternalLinkageQuery()
1213

@@ -38,6 +39,15 @@ predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleI
3839
ruleId = "RULE-5-9" and
3940
category = "advisory"
4041
or
42+
query =
43+
// `Query` instance for the `inlineFunctionNotDeclaredStaticStorage` query
44+
Declarations6Package::inlineFunctionNotDeclaredStaticStorageQuery() and
45+
queryId =
46+
// `@id` for the `inlineFunctionNotDeclaredStaticStorage` query
47+
"c/misra/inline-function-not-declared-static-storage" and
48+
ruleId = "RULE-8-10" and
49+
category = "required"
50+
or
4151
query =
4252
// `Query` instance for the `arrayExternalLinkageSizeExplicitlySpecified` query
4353
Declarations6Package::arrayExternalLinkageSizeExplicitlySpecifiedQuery() and
@@ -79,6 +89,13 @@ module Declarations6Package {
7989
TQueryC(TDeclarations6PackageQuery(TIdentifiersWithInternalLinkageNotUniqueQuery()))
8090
}
8191

92+
Query inlineFunctionNotDeclaredStaticStorageQuery() {
93+
//autogenerate `Query` type
94+
result =
95+
// `Query` type for `inlineFunctionNotDeclaredStaticStorage` query
96+
TQueryC(TDeclarations6PackageQuery(TInlineFunctionNotDeclaredStaticStorageQuery()))
97+
}
98+
8299
Query arrayExternalLinkageSizeExplicitlySpecifiedQuery() {
83100
//autogenerate `Query` type
84101
result =

rule_packages/c/Declarations6.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@
6060
],
6161
"title": "Identifiers that define objects or functions with internal linkage should be unique"
6262
},
63+
"RULE-8-10": {
64+
"properties": {
65+
"obligation": "required"
66+
},
67+
"queries": [
68+
{
69+
"description": "Declaring an inline function with external linkage can lead to undefined or incorrect program behaviour.",
70+
"kind": "problem",
71+
"name": "An inline function shall be declared with the static storage class",
72+
"precision": "very-high",
73+
"severity": "error",
74+
"short_name": "InlineFunctionNotDeclaredStaticStorage",
75+
"tags": [
76+
"correctness"
77+
]
78+
}
79+
],
80+
"title": "An inline function shall be declared with the static storage class"
81+
},
6382
"RULE-8-11": {
6483
"properties": {
6584
"obligation": "advisory"

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ c,MISRA-C-2012,RULE-8-6,Yes,Required,,,An identifier with external linkage shall
655655
c,MISRA-C-2012,RULE-8-7,Yes,Advisory,,,Functions and objects should not be defined with external linkage if they are referenced in only one translation unit,,Declarations6,Medium,
656656
c,MISRA-C-2012,RULE-8-8,Yes,Required,,,The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage,M3-3-2,Declarations,Medium,
657657
c,MISRA-C-2012,RULE-8-9,Yes,Advisory,,,An object should be defined at block scope if its identifier only appears in a single function,M3-4-1,Declarations,Medium,
658-
c,MISRA-C-2012,RULE-8-10,Yes,Required,,,An inline function shall be declared with the static storage class,,Declarations,Medium,
658+
c,MISRA-C-2012,RULE-8-10,Yes,Required,,,An inline function shall be declared with the static storage class,,Declarations6,Medium,
659659
c,MISRA-C-2012,RULE-8-11,Yes,Advisory,,,"When an array with external linkage is declared, its size should be explicitly specified",,Declarations6,Medium,
660660
c,MISRA-C-2012,RULE-8-12,Yes,Required,,,"Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique",,Declarations,Medium,
661661
c,MISRA-C-2012,RULE-8-13,Yes,Advisory,,,A pointer should point to a const-qualified type whenever possible,,Pointers1,Medium,

0 commit comments

Comments
 (0)