Skip to content

Commit 9f48c75

Browse files
committed
Add shared rule A3-1-1 for RULE-6-2-4 linkage2 and improve A3-1-1
1 parent 77a17c2 commit 9f48c75

File tree

18 files changed

+157
-102
lines changed

18 files changed

+157
-102
lines changed

.vscode/tasks.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@
259259
"Language1",
260260
"Language2",
261261
"Language3",
262+
"Linkage1",
263+
"Linkage2",
262264
"Literals",
263265
"Loops",
264266
"Macros",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A3-1-1` - `ViolationsOfOneDefinitionRule.ql`:
2+
- The query previously would incorrectly allow cases where something was defined with `extern` and did not use the defined external linkage library to find external linkage. This change may result in the query finding more results.

cpp/autosar/src/rules/A3-1-1/ViolationsOfOneDefinitionRule.ql

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,10 @@
2020

2121
import cpp
2222
import codingstandards.cpp.autosar
23-
import codingstandards.cpp.AcceptableHeader
23+
import codingstandards.cpp.rules.violationsofonedefinitionrule.ViolationsOfOneDefinitionRule
2424

25-
predicate isInline(Function decl) {
26-
exists(Specifier spec |
27-
spec = decl.getASpecifier() and
28-
(
29-
spec.hasName("inline") or
30-
spec.hasName("constexpr")
31-
)
32-
)
25+
class ViolationsOfOneDefinitionRuleQuery extends ViolationsOfOneDefinitionRuleSharedQuery {
26+
ViolationsOfOneDefinitionRuleQuery() {
27+
this = IncludesPackage::violationsOfOneDefinitionRuleQuery()
28+
}
3329
}
34-
35-
predicate isExtern(FunctionDeclarationEntry decl) {
36-
exists(string spec |
37-
spec = decl.getASpecifier() and
38-
spec = "extern"
39-
)
40-
}
41-
42-
from DeclarationEntry decl, string case, string name
43-
where
44-
(
45-
//a non-inline/non-extern function defined in a header
46-
exists(FunctionDeclarationEntry fn |
47-
fn.isDefinition() and
48-
not (
49-
isInline(fn.getDeclaration())
50-
or
51-
isExtern(fn)
52-
or
53-
//any (defined) templates do not violate the ODR
54-
fn.isFromUninstantiatedTemplate(_)
55-
or
56-
fn.isFromTemplateInstantiation(_) and
57-
//except for specializations, those do violate ODR
58-
not fn.isSpecialization()
59-
or
60-
fn.getDeclaration().isStatic()
61-
) and
62-
decl = fn and
63-
case = "function"
64-
)
65-
or
66-
//an non-const object defined in a header
67-
exists(GlobalOrNamespaceVariable object |
68-
object.hasDefinition() and
69-
not (
70-
object.isConstexpr()
71-
or
72-
object.isConst()
73-
or
74-
object.isStatic()
75-
) and
76-
decl = object.getDefinition() and
77-
case = "object"
78-
)
79-
) and
80-
not decl.getDeclaration().getNamespace().isAnonymous() and
81-
decl.getFile() instanceof AcceptableHeader and
82-
not isExcluded(decl, IncludesPackage::violationsOfOneDefinitionRuleQuery()) and
83-
name = decl.getName()
84-
select decl,
85-
"Header file $@ contains " + case + " " + name + " that lead to One Defintion Rule violation.",
86-
decl.getFile(), decl.getFile().getBaseName()

cpp/autosar/test/rules/A3-1-1/ViolationsOfOneDefinitionRule.expected

Lines changed: 0 additions & 3 deletions
This file was deleted.

cpp/autosar/test/rules/A3-1-1/ViolationsOfOneDefinitionRule.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/violationsofonedefinitionrule/ViolationsOfOneDefinitionRule.ql

cpp/autosar/test/rules/A3-1-1/test.hpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import RuleMetadata
44
import codingstandards.cpp.exclusions.RuleMetadata
55

66
newtype Linkage2Query =
7-
THeaderDefinitionsExternalLinkageQuery() or
7+
TViolationsOfOneDefinitionRuleMisraQuery() or
88
TInternalLinkageSpecifiedAppropriatelyQuery()
99

1010
predicate isLinkage2QueryMetadata(Query query, string queryId, string ruleId, string category) {
1111
query =
12-
// `Query` instance for the `headerDefinitionsExternalLinkage` query
13-
Linkage2Package::headerDefinitionsExternalLinkageQuery() and
12+
// `Query` instance for the `violationsOfOneDefinitionRuleMisra` query
13+
Linkage2Package::violationsOfOneDefinitionRuleMisraQuery() and
1414
queryId =
15-
// `@id` for the `headerDefinitionsExternalLinkage` query
16-
"cpp/misra/header-definitions-external-linkage" and
15+
// `@id` for the `violationsOfOneDefinitionRuleMisra` query
16+
"cpp/misra/violations-of-one-definition-rule-misra" and
1717
ruleId = "RULE-6-2-4" and
1818
category = "required"
1919
or
@@ -28,11 +28,11 @@ predicate isLinkage2QueryMetadata(Query query, string queryId, string ruleId, st
2828
}
2929

3030
module Linkage2Package {
31-
Query headerDefinitionsExternalLinkageQuery() {
31+
Query violationsOfOneDefinitionRuleMisraQuery() {
3232
//autogenerate `Query` type
3333
result =
34-
// `Query` type for `headerDefinitionsExternalLinkage` query
35-
TQueryCPP(TLinkage2PackageQuery(THeaderDefinitionsExternalLinkageQuery()))
34+
// `Query` type for `violationsOfOneDefinitionRuleMisra` query
35+
TQueryCPP(TLinkage2PackageQuery(TViolationsOfOneDefinitionRuleMisraQuery()))
3636
}
3737

3838
Query internalLinkageSpecifiedAppropriatelyQuery() {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Provides a library with a `problems` predicate for the following issue:
3+
* Placing the definitions of functions or objects that are non-inline and have
4+
* external linkage can lead to violations of the ODR and can lead to undefined
5+
* behaviour.
6+
*/
7+
8+
import cpp
9+
import codingstandards.cpp.Customizations
10+
import codingstandards.cpp.Exclusions
11+
import codingstandards.cpp.AcceptableHeader
12+
import codingstandards.cpp.Linkage
13+
14+
predicate isInline(Function decl) {
15+
exists(Specifier spec |
16+
spec = decl.getASpecifier() and
17+
(
18+
spec.hasName("inline") or
19+
spec.hasName("constexpr")
20+
)
21+
)
22+
}
23+
24+
abstract class ViolationsOfOneDefinitionRuleSharedQuery extends Query { }
25+
26+
Query getQuery() { result instanceof ViolationsOfOneDefinitionRuleSharedQuery }
27+
28+
query predicate problems(DeclarationEntry decl, string message, File declFile, string secondmessage) {
29+
exists(string case |
30+
not isExcluded(decl, getQuery()) and
31+
declFile = decl.getFile() and
32+
secondmessage = decl.getFile().getBaseName() and
33+
message =
34+
"Header file $@ contains " + case + " " + decl.getName() +
35+
" that lead to One Defintion Rule violation." and
36+
hasExternalLinkage(decl.getDeclaration()) and
37+
(
38+
//a non-inline/non-extern function defined in a header
39+
exists(FunctionDeclarationEntry fn |
40+
fn.isDefinition() and
41+
not (
42+
isInline(fn.getDeclaration())
43+
or
44+
//any (defined) templates do not violate the ODR
45+
fn.isFromUninstantiatedTemplate(_)
46+
or
47+
fn.isFromTemplateInstantiation(_) and
48+
//except for specializations, those do violate ODR
49+
not fn.isSpecialization()
50+
or
51+
//static/nonstatic member functions should still not be defined (so do not exclude here)
52+
fn.getDeclaration().isStatic() and not fn.getFunction() instanceof MemberFunction
53+
) and
54+
decl = fn and
55+
case = "function"
56+
)
57+
or
58+
//an non-const object defined in a header
59+
exists(Variable object |
60+
not (
61+
object.isConstexpr()
62+
or
63+
object.isConst()
64+
or
65+
object.isStatic()
66+
) and
67+
decl = object.getDefinition() and
68+
case = "object"
69+
)
70+
) and
71+
not decl.getDeclaration().getNamespace().isAnonymous() and
72+
decl.getFile() instanceof AcceptableHeader
73+
)
74+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.hpp:3:6:3:7 | definition of f1 | Header file $@ contains function f1 that lead to One Defintion Rule violation. | test.hpp:0:0:0:0 | test.hpp | test.hpp |
2+
| test.hpp:8:5:8:5 | definition of i | Header file $@ contains object i that lead to One Defintion Rule violation. | test.hpp:0:0:0:0 | test.hpp | test.hpp |
3+
| test.hpp:9:12:9:13 | definition of i1 | Header file $@ contains object i1 that lead to One Defintion Rule violation. | test.hpp:0:0:0:0 | test.hpp | test.hpp |

0 commit comments

Comments
 (0)