Skip to content

Commit 52b590b

Browse files
committed
Fill in shared query file for BannedSyntax and Banned6
1 parent cfaccb3 commit 52b590b

File tree

12 files changed

+122
-62
lines changed

12 files changed

+122
-62
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @id cpp/autosar/union-keyword-used-autosar-cpp
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+
import codingstandards.cpp.rules.unionkeywordused.UnionKeywordUsed
20+
21+
module UnionKeywordUsedAutosarCppConfig implements UnionKeywordUsedConfigSig {
22+
Query getQuery() { result = BannedSyntaxPackage::unionKeywordUsedAutosarCppQuery() }
23+
}
24+
25+
import UnionKeywordUsed<UnionKeywordUsedAutosarCppConfig>

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

Lines changed: 0 additions & 41 deletions
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/unionkeywordused/UnionKeywordUsed.ql

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 = TUnionKeywordUsedQuery()
6+
newtype Banned6Query = TUnionKeywordUsedMisraCppQuery()
77

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

1919
module Banned6Package {
20-
Query unionKeywordUsedQuery() {
20+
Query unionKeywordUsedMisraCppQuery() {
2121
//autogenerate `Query` type
2222
result =
23-
// `Query` type for `unionKeywordUsed` query
24-
TQueryCPP(TBanned6PackageQuery(TUnionKeywordUsedQuery()))
23+
// `Query` type for `unionKeywordUsedMisraCpp` query
24+
TQueryCPP(TBanned6PackageQuery(TUnionKeywordUsedMisraCppQuery()))
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-
TUnionsUsedQuery() or
19+
TUnionKeywordUsedAutosarCppQuery() 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 `unionsUsed` query
136-
BannedSyntaxPackage::unionsUsedQuery() and
135+
// `Query` instance for the `unionKeywordUsedAutosarCpp` query
136+
BannedSyntaxPackage::unionKeywordUsedAutosarCppQuery() and
137137
queryId =
138-
// `@id` for the `unionsUsed` query
139-
"cpp/autosar/unions-used" and
138+
// `@id` for the `unionKeywordUsedAutosarCpp` query
139+
"cpp/autosar/union-keyword-used-autosar-cpp" 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 unionsUsedQuery() {
265+
Query unionKeywordUsedAutosarCppQuery() {
266266
//autogenerate `Query` type
267267
result =
268-
// `Query` type for `unionsUsed` query
269-
TQueryCPP(TBannedSyntaxPackageQuery(TUnionsUsedQuery()))
268+
// `Query` type for `unionKeywordUsedAutosarCpp` query
269+
TQueryCPP(TBannedSyntaxPackageQuery(TUnionKeywordUsedAutosarCppQuery()))
270270
}
271271

272272
Query commaOperatorUsedQuery() {

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,27 @@ signature module UnionKeywordUsedConfigSig {
1313
Query getQuery();
1414
}
1515

16+
// A tagged union is a class or a struct
17+
// that has exactly one union and exactly one enum with
18+
// corresponding member variable that represents
19+
// the data type in the union
20+
class TaggedUnion extends UserType {
21+
TaggedUnion() {
22+
(this instanceof Class or this instanceof Struct) and
23+
count(Enum e | e.getParentScope() = this) = 1 and
24+
count(Union u | u.getParentScope() = this) = 1 and
25+
count(MemberVariable m, Enum e |
26+
m.getDeclaringType() = this and
27+
e.getDeclaringType() = this and
28+
m.getType().getName() = e.getName()
29+
) = 1
30+
}
31+
}
32+
1633
module UnionKeywordUsed<UnionKeywordUsedConfigSig Config> {
17-
query predicate problems(Element e, string message) {
18-
not isExcluded(e, Config::getQuery()) and message = "<replace with problem alert message for >"
34+
query predicate problems(Union u, string message) {
35+
not isExcluded(u, Config::getQuery()) and
36+
not u.getParentScope() instanceof TaggedUnion and
37+
message = u.getName() + " is not a tagged union."
1938
}
2039
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
No expected results have yet been specified
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
struct union_with_tag {
3+
enum class TYPE { integer, floating };
4+
5+
union u { // COMPLIANT - union with tag
6+
int i;
7+
float f;
8+
};
9+
10+
TYPE tag;
11+
};
12+
13+
struct union_sans_tag {
14+
union u { // NON_COMPLIANT
15+
int i;
16+
float f;
17+
};
18+
int x;
19+
};
20+
21+
union u { // NON_COMPLIANT
22+
int i;
23+
float f;
24+
};
25+
26+
void test_unions() {
27+
union_with_tag u1;
28+
union_sans_tag u2;
29+
u u3;
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id cpp/misra/union-keyword-used-misra-cpp
3+
* @name RULE-12-3-1: The union keyword shall not be used
4+
* @description Using unions should be avoided and 'std::variant' should be used as a type-safe
5+
* alternative.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-12-3-1
10+
* scope/single-translation-unit
11+
* correctness
12+
* external/misra/enforcement/decidable
13+
* external/misra/obligation/required
14+
*/
15+
16+
import cpp
17+
import codingstandards.cpp.misra
18+
import codingstandards.cpp.rules.unionkeywordused.UnionKeywordUsed
19+
20+
module UnionKeywordUsedMisraCppConfig implements UnionKeywordUsedConfigSig {
21+
Query getQuery() { result = Banned6Package::unionKeywordUsedMisraCppQuery() }
22+
}
23+
24+
import UnionKeywordUsed<UnionKeywordUsedMisraCppConfig>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/unionkeywordused/UnionKeywordUsed.ql

0 commit comments

Comments
 (0)