Skip to content

Commit 77a17c2

Browse files
committed
Add RULE-6-5-2 linkage2
1 parent 33c5a6d commit 77a17c2

File tree

8 files changed

+164
-2
lines changed

8 files changed

+164
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Linkage2Query =
7+
THeaderDefinitionsExternalLinkageQuery() or
8+
TInternalLinkageSpecifiedAppropriatelyQuery()
9+
10+
predicate isLinkage2QueryMetadata(Query query, string queryId, string ruleId, string category) {
11+
query =
12+
// `Query` instance for the `headerDefinitionsExternalLinkage` query
13+
Linkage2Package::headerDefinitionsExternalLinkageQuery() and
14+
queryId =
15+
// `@id` for the `headerDefinitionsExternalLinkage` query
16+
"cpp/misra/header-definitions-external-linkage" and
17+
ruleId = "RULE-6-2-4" and
18+
category = "required"
19+
or
20+
query =
21+
// `Query` instance for the `internalLinkageSpecifiedAppropriately` query
22+
Linkage2Package::internalLinkageSpecifiedAppropriatelyQuery() and
23+
queryId =
24+
// `@id` for the `internalLinkageSpecifiedAppropriately` query
25+
"cpp/misra/internal-linkage-specified-appropriately" and
26+
ruleId = "RULE-6-5-2" and
27+
category = "advisory"
28+
}
29+
30+
module Linkage2Package {
31+
Query headerDefinitionsExternalLinkageQuery() {
32+
//autogenerate `Query` type
33+
result =
34+
// `Query` type for `headerDefinitionsExternalLinkage` query
35+
TQueryCPP(TLinkage2PackageQuery(THeaderDefinitionsExternalLinkageQuery()))
36+
}
37+
38+
Query internalLinkageSpecifiedAppropriatelyQuery() {
39+
//autogenerate `Query` type
40+
result =
41+
// `Query` type for `internalLinkageSpecifiedAppropriately` query
42+
TQueryCPP(TLinkage2PackageQuery(TInternalLinkageSpecifiedAppropriatelyQuery()))
43+
}
44+
}

cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import IntegerConversion
3434
import Invariants
3535
import Iterators
3636
import Lambdas
37+
import Linkage2
3738
import Literals
3839
import Loops
3940
import Macros
@@ -96,6 +97,7 @@ newtype TCPPQuery =
9697
TInvariantsPackageQuery(InvariantsQuery q) or
9798
TIteratorsPackageQuery(IteratorsQuery q) or
9899
TLambdasPackageQuery(LambdasQuery q) or
100+
TLinkage2PackageQuery(Linkage2Query q) or
99101
TLiteralsPackageQuery(LiteralsQuery q) or
100102
TLoopsPackageQuery(LoopsQuery q) or
101103
TMacrosPackageQuery(MacrosQuery q) or
@@ -158,6 +160,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
158160
isInvariantsQueryMetadata(query, queryId, ruleId, category) or
159161
isIteratorsQueryMetadata(query, queryId, ruleId, category) or
160162
isLambdasQueryMetadata(query, queryId, ruleId, category) or
163+
isLinkage2QueryMetadata(query, queryId, ruleId, category) or
161164
isLiteralsQueryMetadata(query, queryId, ruleId, category) or
162165
isLoopsQueryMetadata(query, queryId, ruleId, category) or
163166
isMacrosQueryMetadata(query, queryId, ruleId, category) or
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @id cpp/misra/internal-linkage-specified-appropriately
3+
* @name RULE-6-5-2: Internal linkage should be specified appropriately
4+
* @description Using certain specifiers or declaring entities with internal linkage in certain
5+
* namespaces can lead to confusion as to the linkage of the entity and can cause code
6+
* to be more difficult to read.
7+
* @kind problem
8+
* @precision very-high
9+
* @problem.severity error
10+
* @tags external/misra/id/rule-6-5-2
11+
* correctness
12+
* maintainability
13+
* readability
14+
* scope/single-translation-unit
15+
* external/misra/enforcement/decidable
16+
* external/misra/obligation/advisory
17+
*/
18+
19+
import cpp
20+
import codingstandards.cpp.misra
21+
import codingstandards.cpp.Linkage
22+
import codingstandards.cpp.types.Pointers
23+
24+
from DeclarationEntry decl, string message
25+
where
26+
not isExcluded(decl, Linkage2Package::internalLinkageSpecifiedAppropriatelyQuery()) and
27+
hasInternalLinkage(decl.getDeclaration()) and
28+
//exclusions as per rule for const and constexpr Variables
29+
not decl.getDeclaration().(Variable).getUnderlyingType().isConst() and
30+
not decl.getDeclaration().(Variable).getType().(PointerOrArrayType).isDeeplyConstBelow() and
31+
not decl.getDeclaration().(Variable).isConstexpr() and
32+
(
33+
decl.hasSpecifier("static") and
34+
(
35+
decl.getDeclaration().getNamespace().isAnonymous() and
36+
message = "Static specifier used in anonymous namespace."
37+
or
38+
not decl.getDeclaration().getNamespace().isAnonymous() and
39+
message = "Static specifier used in non-anonymous namespace."
40+
)
41+
or
42+
decl.hasSpecifier("extern") and
43+
decl.getDeclaration().getNamespace().isAnonymous() and
44+
message = "Extern specifier used in anonymous namespace."
45+
)
46+
select decl, message
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.cpp:1:13:1:13 | declaration of f | Static specifier used in non-anonymous namespace. |
2+
| test.cpp:5:13:5:14 | declaration of f2 | Extern specifier used in anonymous namespace. |
3+
| test.cpp:8:12:8:13 | declaration of i1 | Extern specifier used in anonymous namespace. |
4+
| test.cpp:9:12:9:13 | definition of i2 | Static specifier used in anonymous namespace. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-6-5-2/InternalLinkageSpecifiedAppropriately.ql
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
static void f(); // NON_COMPILANT - prefer to use an anonymous namespace
2+
3+
namespace {
4+
void f1(); // COMPILANT
5+
extern void f2(); // NON_COMPILANT
6+
7+
int i; // COMPILANT
8+
extern int i1; // NON_COMPILANT
9+
static int i2; // NON_COMPILANT - prefer to not use static as it is redundant
10+
} // namespace
11+
12+
namespace named {
13+
static const int i[] = {1}; // COMPILANT - exception
14+
}

rule_packages/cpp/Linkage2.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"MISRA-C++-2023": {
3+
"RULE-6-2-4": {
4+
"properties": {
5+
"enforcement": "decidable",
6+
"obligation": "required"
7+
},
8+
"queries": [
9+
{
10+
"description": "Placing the definitions of functions or objects that are non-inline and have external linkage can lead to violations of the ODR and can lead to undefined behaviour.",
11+
"kind": "problem",
12+
"name": "A header file shall not contain definitions of functions or objects that are non-inline and have external linkage",
13+
"precision": "very-high",
14+
"severity": "error",
15+
"short_name": "HeaderDefinitionsExternalLinkage",
16+
"tags": [
17+
"correctness",
18+
"maintainability",
19+
"readability",
20+
"scope/single-translation-unit"
21+
]
22+
}
23+
],
24+
"title": "A header file shall not contain definitions of functions or objects that are non-inline and have external linkage"
25+
},
26+
"RULE-6-5-2": {
27+
"properties": {
28+
"enforcement": "decidable",
29+
"obligation": "advisory"
30+
},
31+
"queries": [
32+
{
33+
"description": "Using certain specifiers or declaring entities with internal linkage in certain namespaces can lead to confusion as to the linkage of the entity and can cause code to be more difficult to read.",
34+
"kind": "problem",
35+
"name": "Internal linkage should be specified appropriately",
36+
"precision": "very-high",
37+
"severity": "error",
38+
"short_name": "InternalLinkageSpecifiedAppropriately",
39+
"tags": [
40+
"correctness",
41+
"maintainability",
42+
"readability",
43+
"scope/single-translation-unit"
44+
]
45+
}
46+
],
47+
"title": "Internal linkage should be specified appropriately"
48+
}
49+
}
50+
}

rules.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,12 +856,12 @@ cpp,MISRA-C++-2023,RULE-6-0-4,Yes,Required,Decidable,Single Translation Unit,The
856856
cpp,MISRA-C++-2023,RULE-6-2-1,Yes,Required,Decidable,System,The one-definition rule shall not be violated,M3-2-2,ImportMisra23,Import,
857857
cpp,MISRA-C++-2023,RULE-6-2-2,Yes,Required,Decidable,System,All declarations of a variable or function shall have the same type,"M3-9-1,DCL40-C",Declarations2,Easy,
858858
cpp,MISRA-C++-2023,RULE-6-2-3,Yes,Required,Decidable,System,The source code used to implement an entity shall appear only once,,Declarations2,Medium,
859-
cpp,MISRA-C++-2023,RULE-6-2-4,Yes,Required,Decidable,Single Translation Unit,A header file shall not contain definitions of functions or objects that are non-inline and have external linkage,,Linkage,Easy,
859+
cpp,MISRA-C++-2023,RULE-6-2-4,Yes,Required,Decidable,Single Translation Unit,A header file shall not contain definitions of functions or objects that are non-inline and have external linkage,M3-2-4,Linkage2,Easy,
860860
cpp,MISRA-C++-2023,RULE-6-4-1,Yes,Required,Decidable,Single Translation Unit,A variable declared in an inner scope shall not hide a variable declared in an outer scope,A2-10-1,ImportMisra23,Import,
861861
cpp,MISRA-C++-2023,RULE-6-4-2,Yes,Required,Decidable,Single Translation Unit,Derived classes shall not conceal functions that are inherited from their bases,A7-3-1,ImportMisra23,Import,
862862
cpp,MISRA-C++-2023,RULE-6-4-3,Yes,Required,Decidable,Single Translation Unit,A name that is present in a dependent base shall not be resolved by unqualified lookup,M14-6-1,ImportMisra23,Import,
863863
cpp,MISRA-C++-2023,RULE-6-5-1,Yes,Advisory,Decidable,Single Translation Unit,A function or object with external linkage should be introduced in a header file,,Linkage,Medium,
864-
cpp,MISRA-C++-2023,RULE-6-5-2,Yes,Advisory,Decidable,Single Translation Unit,Internal linkage should be specified appropriately,,Linkage,Medium,
864+
cpp,MISRA-C++-2023,RULE-6-5-2,Yes,Advisory,Decidable,Single Translation Unit,Internal linkage should be specified appropriately,,Linkage2,Medium,
865865
cpp,MISRA-C++-2023,RULE-6-7-1,Yes,Required,Decidable,Single Translation Unit,Local variables shall not have static storage duration,,Declarations2,Easy,
866866
cpp,MISRA-C++-2023,RULE-6-7-2,Yes,Required,Decidable,Single Translation Unit,Global variables shall not be used,,Banned,Easy,
867867
cpp,MISRA-C++-2023,RULE-6-8-1,Yes,Required,Undecidable,System,An object shall not be accessed outside of its lifetime,A3-8-1,ImportMisra23,Import,

0 commit comments

Comments
 (0)