Skip to content

Commit 99af17c

Browse files
Implement RULE-11-10 banning atomic void.
1 parent ce5b364 commit 99af17c

File tree

8 files changed

+113
-1
lines changed

8 files changed

+113
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @id c/misra/atomic-qualifier-applied-to-void
3+
* @name RULE-11-10: The _Atomic qualifier shall not be applied to the incomplete type void
4+
* @description Conversions between types by using an _Atomic void type may result in undefined
5+
* behavior.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-11-10
10+
* correctness
11+
* external/misra/c/2012/third-edition-first-revision
12+
* external/misra/c/2012/amendment4
13+
* external/misra/obligation/required
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
19+
class AtomicVoidType extends Type {
20+
AtomicVoidType() {
21+
hasSpecifier("atomic") and
22+
getUnspecifiedType() instanceof VoidType
23+
}
24+
}
25+
26+
Type getNestedType(Type root) {
27+
result = root
28+
or
29+
exists(DerivedType derived | derived = root | result = getNestedType(derived.getBaseType()))
30+
}
31+
32+
from DeclarationEntry decl, AtomicVoidType atomicVoid
33+
where
34+
not isExcluded(decl, Declarations9Package::atomicQualifierAppliedToVoidQuery()) and
35+
atomicVoid = getNestedType(decl.getType())
36+
select decl, decl.getName() + " declared with an atomic void type."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:3:15:3:16 | definition of g3 | g3 declared with an atomic void type. |
2+
| test.c:10:17:10:18 | definition of m3 | m3 declared with an atomic void type. |
3+
| test.c:15:22:15:23 | definition of p2 | p2 declared with an atomic void type. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-11-10/AtomicQualifierAppliedToVoid.ql

c/misra/test/rules/RULE-11-10/test.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// _Atomic void g1; // doesn't compile
2+
_Atomic int g2; // COMPLIANT
3+
_Atomic void *g3; // NON_COMPLIANT
4+
// _Atomic void g4[]; // doesn't compile
5+
void *_Atomic g5; // COMPLIANT
6+
7+
struct {
8+
_Atomic int m1; // COMPLIANT
9+
// _Atomic void m2; // doesn't compile
10+
_Atomic void *m3; // NON_COMPLIANT
11+
void *_Atomic m4; // COMPLIANT
12+
} s1;
13+
14+
void f(_Atomic int p1, // COMPLIANT
15+
_Atomic void *p2 // NON_COMPLIANT
16+
// _Atomic void p3[] // doesn't compile, even though it perhaps should as
17+
// it is adjusted to void*.
18+
) {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Declarations9Query = TAtomicQualifierAppliedToVoidQuery()
7+
8+
predicate isDeclarations9QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `atomicQualifierAppliedToVoid` query
11+
Declarations9Package::atomicQualifierAppliedToVoidQuery() and
12+
queryId =
13+
// `@id` for the `atomicQualifierAppliedToVoid` query
14+
"c/misra/atomic-qualifier-applied-to-void" and
15+
ruleId = "RULE-11-10" and
16+
category = "required"
17+
}
18+
19+
module Declarations9Package {
20+
Query atomicQualifierAppliedToVoidQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `atomicQualifierAppliedToVoid` query
24+
TQueryC(TDeclarations9PackageQuery(TAtomicQualifierAppliedToVoidQuery()))
25+
}
26+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import Declarations5
2828
import Declarations6
2929
import Declarations7
3030
import Declarations8
31+
import Declarations9
3132
import EssentialTypes
3233
import Expressions
3334
import FloatingTypes
@@ -107,6 +108,7 @@ newtype TCQuery =
107108
TDeclarations6PackageQuery(Declarations6Query q) or
108109
TDeclarations7PackageQuery(Declarations7Query q) or
109110
TDeclarations8PackageQuery(Declarations8Query q) or
111+
TDeclarations9PackageQuery(Declarations9Query q) or
110112
TEssentialTypesPackageQuery(EssentialTypesQuery q) or
111113
TExpressionsPackageQuery(ExpressionsQuery q) or
112114
TFloatingTypesPackageQuery(FloatingTypesQuery q) or
@@ -186,6 +188,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
186188
isDeclarations6QueryMetadata(query, queryId, ruleId, category) or
187189
isDeclarations7QueryMetadata(query, queryId, ruleId, category) or
188190
isDeclarations8QueryMetadata(query, queryId, ruleId, category) or
191+
isDeclarations9QueryMetadata(query, queryId, ruleId, category) or
189192
isEssentialTypesQueryMetadata(query, queryId, ruleId, category) or
190193
isExpressionsQueryMetadata(query, queryId, ruleId, category) or
191194
isFloatingTypesQueryMetadata(query, queryId, ruleId, category) or

rule_packages/c/Declarations9.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"MISRA-C-2012": {
3+
"RULE-11-10": {
4+
"properties": {
5+
"obligation": "required"
6+
},
7+
"queries": [
8+
{
9+
"description": "Conversions between types by using an _Atomic void type may result in undefined behavior.",
10+
"kind": "problem",
11+
"name": "The _Atomic qualifier shall not be applied to the incomplete type void",
12+
"precision": "very-high",
13+
"severity": "error",
14+
"short_name": "AtomicQualifierAppliedToVoid",
15+
"tags": [
16+
"correctness",
17+
"external/misra/c/2012/third-edition-first-revision",
18+
"external/misra/c/2012/amendment4"
19+
]
20+
}
21+
],
22+
"title": "The _Atomic qualifier shall not be applied to the incomplete type void"
23+
}
24+
}
25+
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ c,MISRA-C-2012,RULE-9-2,Yes,Required,,,The initializer for an aggregate or union
677677
c,MISRA-C-2012,RULE-9-3,Yes,Required,,,Arrays shall not be partially initialized,,Memory1,Medium,
678678
c,MISRA-C-2012,RULE-9-4,Yes,Required,,,An element of an object shall not be initialized more than once,,Memory1,Medium,
679679
c,MISRA-C-2012,RULE-9-5,No,Required,,,Where designated initializers are used to initialize an array object the size of the array shall be specified explicitly,,,Medium,
680-
c,MISRA-C-2012,RULE-9-6,Yes,Required,,,An initializer using chained designators shall not contain initializers without designators,,Declarations9,Hard,
680+
c,MISRA-C-2012,RULE-9-6,Yes,Required,,,An initializer using chained designators shall not contain initializers without designators,,Declarations10,Hard,
681681
c,MISRA-C-2012,RULE-9-7,Yes,Mandatory,,,Atomic objects shall be appropriately initialized before being accessed,,Concurrency6,Hard,
682682
c,MISRA-C-2012,RULE-10-1,Yes,Required,,,Operands shall not be of an inappropriate essential type,,EssentialTypes,Hard,
683683
c,MISRA-C-2012,RULE-10-2,Yes,Required,,,Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations,,EssentialTypes,Medium,

0 commit comments

Comments
 (0)