Skip to content

Commit afc029e

Browse files
committed
Declarations6: add RULE-8-7
1 parent 7b1024e commit afc029e

File tree

9 files changed

+111
-2
lines changed

9 files changed

+111
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @id c/misra/should-not-be-defined-with-external-linkage
3+
* @name RULE-8-7: Functions and objects should not be defined with external linkage if they are referenced in only one
4+
* @description Declarations with external linkage that are referenced in only one translation unit
5+
* can indicate an intention to only have those identifiers accessible in that
6+
* translation unit and accidental future accesses in other translation units can lead
7+
* to confusing program behaviour.
8+
* @kind problem
9+
* @precision very-high
10+
* @problem.severity error
11+
* @tags external/misra/id/rule-8-7
12+
* correctness
13+
* maintainability
14+
* readability
15+
* external/misra/obligation/advisory
16+
*/
17+
18+
import cpp
19+
import codingstandards.c.misra
20+
import codingstandards.cpp.Identifiers
21+
import codingstandards.cpp.Scope
22+
23+
/**
24+
* Re-introduce function calls into access description as
25+
* "any reference"
26+
*/
27+
class Reference extends NameQualifiableElement {
28+
Reference() {
29+
this instanceof Access or
30+
this instanceof FunctionCall
31+
}
32+
}
33+
34+
from ExternalIdentifiers e, Reference a1, TranslationUnit t1
35+
where
36+
not isExcluded(e, Declarations6Package::shouldNotBeDefinedWithExternalLinkageQuery()) and
37+
(a1.(Access).getTarget() = e or a1.(FunctionCall).getTarget() = e) and
38+
a1.getFile() = t1 and
39+
//not accessed in any other translation unit
40+
not exists(TranslationUnit t2, Reference a2 |
41+
not t1 = t2 and
42+
(a2.(Access).getTarget() = e or a2.(FunctionCall).getTarget() = e) and
43+
a2.getFile() = t2
44+
)
45+
select e, "Declaration with external linkage is accessed in only one translation unit $@.", a1,
46+
a1.toString()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.h:2:12:2:13 | i1 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:4:5:4:6 | i1 | i1 |
2+
| test.h:3:5:3:6 | i2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:5:5:5:6 | i2 | i2 |
3+
| test.h:5:13:5:14 | f2 | Declaration with external linkage is accessed in only one translation unit $@. | test.c:7:5:7:6 | call to f2 | call to f2 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-7/ShouldNotBeDefinedWithExternalLinkage.ql

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "test.h"
2+
void f(){
3+
i = 0;
4+
i1 = 0;
5+
i2 = 0;
6+
f1();
7+
f2();
8+
f3();
9+
}

c/misra/test/rules/RULE-8-7/test.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern int i; // COMPLIANT - accessed multiple translation units
2+
extern int i1; // NON_COMPLIANT - accessed one translation unit
3+
int i2; // NON_COMPLIANT - accessed one translation unit
4+
extern void f1(); // COMPLIANT - accessed multiple translation units
5+
extern void f2(); // NON_COMPLIANT - accessed one translation unit
6+
static void f3(); // COMPLIANT - internal linkage
7+
extern void f3(); // COMPLIANT - internal linkage

c/misra/test/rules/RULE-8-7/test1.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "test.h"
2+
void f(){
3+
i = 0;
4+
f1();
5+
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ newtype Declarations6Query =
77
TFunctionDeclaredImplicitlyQuery() or
88
TIdentifiersWithExternalLinkageNotUniqueQuery() or
99
TIdentifiersWithInternalLinkageNotUniqueQuery() or
10-
TArrayExternalLinkageSizeExplicitlySpecifiedQuery()
10+
TArrayExternalLinkageSizeExplicitlySpecifiedQuery() or
11+
TShouldNotBeDefinedWithExternalLinkageQuery()
1112

1213
predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleId, string category) {
1314
query =
@@ -45,6 +46,15 @@ predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleI
4546
"c/misra/array-external-linkage-size-explicitly-specified" and
4647
ruleId = "RULE-8-11" and
4748
category = "advisory"
49+
or
50+
query =
51+
// `Query` instance for the `shouldNotBeDefinedWithExternalLinkage` query
52+
Declarations6Package::shouldNotBeDefinedWithExternalLinkageQuery() and
53+
queryId =
54+
// `@id` for the `shouldNotBeDefinedWithExternalLinkage` query
55+
"c/misra/should-not-be-defined-with-external-linkage" and
56+
ruleId = "RULE-8-7" and
57+
category = "advisory"
4858
}
4959

5060
module Declarations6Package {
@@ -75,4 +85,11 @@ module Declarations6Package {
7585
// `Query` type for `arrayExternalLinkageSizeExplicitlySpecified` query
7686
TQueryC(TDeclarations6PackageQuery(TArrayExternalLinkageSizeExplicitlySpecifiedQuery()))
7787
}
88+
89+
Query shouldNotBeDefinedWithExternalLinkageQuery() {
90+
//autogenerate `Query` type
91+
result =
92+
// `Query` type for `shouldNotBeDefinedWithExternalLinkage` query
93+
TQueryC(TDeclarations6PackageQuery(TShouldNotBeDefinedWithExternalLinkageQuery()))
94+
}
7895
}

rule_packages/c/Declarations6.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@
7979
}
8080
],
8181
"title": "When an array with external linkage is declared, its size should be explicitly specified"
82+
},
83+
"RULE-8-7": {
84+
"properties": {
85+
"obligation": "advisory"
86+
},
87+
"queries": [
88+
{
89+
"description": "Declarations with external linkage that are referenced in only one translation unit can indicate an intention to only have those identifiers accessible in that translation unit and accidental future accesses in other translation units can lead to confusing program behaviour.",
90+
"kind": "problem",
91+
"name": "Functions and objects should not be defined with external linkage if they are referenced in only one",
92+
"precision": "very-high",
93+
"severity": "error",
94+
"short_name": "ShouldNotBeDefinedWithExternalLinkage",
95+
"tags": [
96+
"correctness",
97+
"maintainability",
98+
"readability"
99+
]
100+
}
101+
],
102+
"title": "Functions and objects should not be defined with external linkage if they are referenced in only one translation unit"
82103
}
83104
}
84105
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ c,MISRA-C-2012,RULE-8-3,Yes,Required,,,All declarations of an object or function
652652
c,MISRA-C-2012,RULE-8-4,Yes,Required,,,A compatible declaration shall be visible when an object or function with external linkage is defined,,Declarations4,Medium,
653653
c,MISRA-C-2012,RULE-8-5,Yes,Required,,,An external object or function shall be declared once in one and only one file,,Declarations,Medium,
654654
c,MISRA-C-2012,RULE-8-6,Yes,Required,,,An identifier with external linkage shall have exactly one external definition,M3-2-4,Declarations4,Import,
655-
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,,Declarations,Medium,
655+
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,
658658
c,MISRA-C-2012,RULE-8-10,Yes,Required,,,An inline function shall be declared with the static storage class,,Declarations,Medium,

0 commit comments

Comments
 (0)