Skip to content

Commit b73fab8

Browse files
committed
Un-share UnionKeywordUsed
1 parent 67d06e9 commit b73fab8

File tree

18 files changed

+100
-103
lines changed

18 files changed

+100
-103
lines changed

cpp/autosar/src/rules/A9-5-1/UnionKeywordUsedAutosarCpp.ql

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @id cpp/autosar/unions-used
3+
* @name A9-5-1: Unions shall not be used
4+
* @description Unions shall not be used. Tagged unions can be used if 'std::variant' is not
5+
* available.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/autosar/id/a9-5-1
10+
* correctness
11+
* scope/single-translation-unit
12+
* external/autosar/allocated-target/implementation
13+
* external/autosar/enforcement/automated
14+
* external/autosar/obligation/required
15+
*/
16+
17+
import cpp
18+
import codingstandards.cpp.autosar
19+
20+
// A tagged union is a class or a struct
21+
// that has exactly one union and exactly one enum with
22+
// corresponding member variable that represents
23+
// the data type in the union
24+
class TaggedUnion extends UserType {
25+
TaggedUnion() {
26+
(this instanceof Class or this instanceof Struct) and
27+
count(Enum e | e.getParentScope() = this) = 1 and
28+
count(Union u | u.getParentScope() = this) = 1 and
29+
count(MemberVariable m, Enum e |
30+
m.getDeclaringType() = this and
31+
e.getDeclaringType() = this and
32+
m.getType().getName() = e.getName()
33+
) = 1
34+
}
35+
}
36+
37+
from Union u, string message
38+
where
39+
not isExcluded(u, BannedSyntaxPackage::unionsUsedQuery()) and
40+
not u.getParentScope() instanceof TaggedUnion and
41+
message = "'" + u.getName() + "' is not a tagged union."
42+
select u, message

cpp/autosar/test/rules/A9-5-1/UnionKeywordUsedAutosarCpp.testref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:14:9:14:9 | u | 'u' is not a tagged union. |
2+
| test.cpp:21:7:21:7 | u | 'u' is not a tagged union. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/A9-5-1/UnionsUsed.ql
File renamed without changes.

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

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

6-
newtype Banned6Query = TUnionKeywordUsedMisraCppQuery()
6+
newtype Banned6Query = TUnionKeywordUsedQuery()
77

88
predicate isBanned6QueryMetadata(Query query, string queryId, string ruleId, string category) {
99
query =
10-
// `Query` instance for the `unionKeywordUsedMisraCpp` query
11-
Banned6Package::unionKeywordUsedMisraCppQuery() and
10+
// `Query` instance for the `unionKeywordUsed` query
11+
Banned6Package::unionKeywordUsedQuery() and
1212
queryId =
13-
// `@id` for the `unionKeywordUsedMisraCpp` query
14-
"cpp/misra/union-keyword-used-misra-cpp" and
13+
// `@id` for the `unionKeywordUsed` query
14+
"cpp/misra/union-keyword-used" and
1515
ruleId = "RULE-12-3-1" and
1616
category = "required"
1717
}
1818

1919
module Banned6Package {
20-
Query unionKeywordUsedMisraCppQuery() {
20+
Query unionKeywordUsedQuery() {
2121
//autogenerate `Query` type
2222
result =
23-
// `Query` type for `unionKeywordUsedMisraCpp` query
24-
TQueryCPP(TBanned6PackageQuery(TUnionKeywordUsedMisraCppQuery()))
23+
// `Query` type for `unionKeywordUsed` query
24+
TQueryCPP(TBanned6PackageQuery(TUnionKeywordUsedQuery()))
2525
}
2626
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ newtype BannedSyntaxQuery =
1616
TTypedefSpecifierUsedQuery() or
1717
TAsmDeclarationUsedQuery() or
1818
TFunctionsDefinedUsingTheEllipsisNotationQuery() or
19-
TUnionKeywordUsedAutosarCppQuery() or
19+
TUnionsUsedQuery() or
2020
TCommaOperatorUsedQuery() or
2121
TUsingDirectivesUsedQuery() or
2222
TUsingDeclarationsUsedInHeaderFilesQuery() or
@@ -132,11 +132,11 @@ predicate isBannedSyntaxQueryMetadata(Query query, string queryId, string ruleId
132132
category = "required"
133133
or
134134
query =
135-
// `Query` instance for the `unionKeywordUsedAutosarCpp` query
136-
BannedSyntaxPackage::unionKeywordUsedAutosarCppQuery() and
135+
// `Query` instance for the `unionsUsed` query
136+
BannedSyntaxPackage::unionsUsedQuery() and
137137
queryId =
138-
// `@id` for the `unionKeywordUsedAutosarCpp` query
139-
"cpp/autosar/union-keyword-used-autosar-cpp" and
138+
// `@id` for the `unionsUsed` query
139+
"cpp/autosar/unions-used" and
140140
ruleId = "A9-5-1" and
141141
category = "required"
142142
or
@@ -262,11 +262,11 @@ module BannedSyntaxPackage {
262262
TQueryCPP(TBannedSyntaxPackageQuery(TFunctionsDefinedUsingTheEllipsisNotationQuery()))
263263
}
264264

265-
Query unionKeywordUsedAutosarCppQuery() {
265+
Query unionsUsedQuery() {
266266
//autogenerate `Query` type
267267
result =
268-
// `Query` type for `unionKeywordUsedAutosarCpp` query
269-
TQueryCPP(TBannedSyntaxPackageQuery(TUnionKeywordUsedAutosarCppQuery()))
268+
// `Query` type for `unionsUsed` query
269+
TQueryCPP(TBannedSyntaxPackageQuery(TUnionsUsedQuery()))
270270
}
271271

272272
Query commaOperatorUsedQuery() {

cpp/common/src/codingstandards/cpp/rules/unionkeywordused/UnionKeywordUsed.qll

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

cpp/common/test/rules/unionkeywordused/UnionKeywordUsed.expected

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

0 commit comments

Comments
 (0)