Skip to content

Commit 262768a

Browse files
authored
Merge pull request #1018 from knewbury01/knewbury01/Linkage2
Add package linkage 2
2 parents c20a5ef + 29e26a4 commit 262768a

27 files changed

+324
-99
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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. Additionally a typo has been fixed in the alert message which will cause the old alerts for this query to now show up as new ones.
3+
- `RULE-6-0-2`, `A3-1-4` - `ExternalLinkageArrayWithoutExplicitSizeMisra.ql`, `ExternalLinkageArrayWithoutExplicitSizeAutosar.ql`:
4+
- The queries listed now find flexible member arrays in structs, as those do not have an explicit size.

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/Scope.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ predicate hidesStrict(UserVariable v1, UserVariable v2) {
369369
predicate hasNamespaceScope(Declaration decl) {
370370
// getNamespace always returns a namespace (e.g. the global namespace).
371371
exists(Namespace n | namespacembrs(unresolveElement(n), underlyingElement(decl)))
372+
or
373+
decl.isTopLevel() and
374+
not namespacembrs(_, decl)
372375
}
373376

374377
/** Holds if `decl` has class scope. */
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+
TViolationsOfOneDefinitionRuleMisraQuery() or
8+
TInternalLinkageSpecifiedAppropriatelyQuery()
9+
10+
predicate isLinkage2QueryMetadata(Query query, string queryId, string ruleId, string category) {
11+
query =
12+
// `Query` instance for the `violationsOfOneDefinitionRuleMisra` query
13+
Linkage2Package::violationsOfOneDefinitionRuleMisraQuery() and
14+
queryId =
15+
// `@id` for the `violationsOfOneDefinitionRuleMisra` query
16+
"cpp/misra/violations-of-one-definition-rule-misra" 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 violationsOfOneDefinitionRuleMisraQuery() {
32+
//autogenerate `Query` type
33+
result =
34+
// `Query` type for `violationsOfOneDefinitionRuleMisra` query
35+
TQueryCPP(TLinkage2PackageQuery(TViolationsOfOneDefinitionRuleMisraQuery()))
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
@@ -36,6 +36,7 @@ import Invariants
3636
import Iterators
3737
import Lambdas
3838
import Linkage1
39+
import Linkage2
3940
import Literals
4041
import Loops
4142
import Macros
@@ -100,6 +101,7 @@ newtype TCPPQuery =
100101
TIteratorsPackageQuery(IteratorsQuery q) or
101102
TLambdasPackageQuery(LambdasQuery q) or
102103
TLinkage1PackageQuery(Linkage1Query q) or
104+
TLinkage2PackageQuery(Linkage2Query q) or
103105
TLiteralsPackageQuery(LiteralsQuery q) or
104106
TLoopsPackageQuery(LoopsQuery q) or
105107
TMacrosPackageQuery(MacrosQuery q) or
@@ -164,6 +166,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
164166
isIteratorsQueryMetadata(query, queryId, ruleId, category) or
165167
isLambdasQueryMetadata(query, queryId, ruleId, category) or
166168
isLinkage1QueryMetadata(query, queryId, ruleId, category) or
169+
isLinkage2QueryMetadata(query, queryId, ruleId, category) or
167170
isLiteralsQueryMetadata(query, queryId, ruleId, category) or
168171
isLoopsQueryMetadata(query, queryId, ruleId, category) or
169172
isMacrosQueryMetadata(query, queryId, ruleId, category) or

0 commit comments

Comments
 (0)