-
Notifications
You must be signed in to change notification settings - Fork 66
Implement RULE-11-10 banning atomic void. #833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MichaelRFairhurst
merged 5 commits into
main
from
michaelrfairhurst/implement-declarations-9-rule-11-10
Mar 14, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
99af17c
Implement RULE-11-10 banning atomic void.
MichaelRFairhurst d70ea29
Address feedback
MichaelRFairhurst 438895d
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/imp…
MichaelRFairhurst 7e8ba84
Fix merge
MichaelRFairhurst f1944db
Format cast test cases
MichaelRFairhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
c/misra/src/rules/RULE-11-10/AtomicQualifierAppliedToVoid.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @id c/misra/atomic-qualifier-applied-to-void | ||
* @name RULE-11-10: The _Atomic qualifier shall not be applied to the incomplete type void | ||
* @description Conversions between types by using an _Atomic void type may result in undefined | ||
* behavior. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity error | ||
* @tags external/misra/id/rule-11-10 | ||
* correctness | ||
* external/misra/c/2012/third-edition-first-revision | ||
* external/misra/c/2012/amendment4 | ||
* external/misra/obligation/required | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
|
||
class AtomicVoidType extends Type { | ||
AtomicVoidType() { | ||
hasSpecifier("atomic") and | ||
getUnspecifiedType() instanceof VoidType | ||
} | ||
} | ||
|
||
predicate usesAtomicVoid(Type root) { | ||
root instanceof AtomicVoidType | ||
or | ||
usesAtomicVoid(root.(DerivedType).getBaseType()) | ||
or | ||
usesAtomicVoid(root.(RoutineType).getReturnType()) | ||
or | ||
usesAtomicVoid(root.(RoutineType).getAParameterType()) | ||
or | ||
usesAtomicVoid(root.(FunctionPointerType).getReturnType()) | ||
or | ||
usesAtomicVoid(root.(FunctionPointerType).getAParameterType()) | ||
or | ||
usesAtomicVoid(root.(TypedefType).getBaseType()) | ||
} | ||
|
||
class ExplicitType extends Type { | ||
Element getDeclaration(string description) { | ||
result.(DeclarationEntry).getType() = this and description = result.(DeclarationEntry).getName() | ||
or | ||
result.(CStyleCast).getType() = this and description = "Cast" | ||
} | ||
} | ||
|
||
from Element decl, ExplicitType explicitType, string elementDescription | ||
where | ||
not isExcluded(decl, Declarations9Package::atomicQualifierAppliedToVoidQuery()) and | ||
decl = explicitType.getDeclaration(elementDescription) and | ||
usesAtomicVoid(explicitType) | ||
select decl, elementDescription + " declared with an atomic void type." |
8 changes: 8 additions & 0 deletions
8
c/misra/test/rules/RULE-11-10/AtomicQualifierAppliedToVoid.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
| test.c:3:15:3:16 | definition of g3 | g3 declared with an atomic void type. | | ||
| test.c:10:17:10:18 | definition of m3 | m3 declared with an atomic void type. | | ||
| test.c:15:22:15:23 | definition of p2 | p2 declared with an atomic void type. | | ||
| test.c:20:23:20:24 | declaration of f2 | f2 declared with an atomic void type. | | ||
| test.c:21:25:21:26 | declaration of f3 | f3 declared with an atomic void type. | | ||
| test.c:22:14:22:15 | declaration of f4 | f4 declared with an atomic void type. | | ||
| test.c:23:16:23:17 | declaration of f5 | f5 declared with an atomic void type. | | ||
| test.c:27:3:27:19 | (_Atomic(void) *)... | Cast declared with an atomic void type. | |
1 change: 1 addition & 0 deletions
1
c/misra/test/rules/RULE-11-10/AtomicQualifierAppliedToVoid.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rules/RULE-11-10/AtomicQualifierAppliedToVoid.ql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// _Atomic void g1; // doesn't compile | ||
_Atomic int g2; // COMPLIANT | ||
_Atomic void *g3; // NON_COMPLIANT | ||
// _Atomic void g4[]; // doesn't compile | ||
void *_Atomic g5; // COMPLIANT | ||
|
||
struct { | ||
_Atomic int m1; // COMPLIANT | ||
// _Atomic void m2; // doesn't compile | ||
_Atomic void *m3; // NON_COMPLIANT | ||
void *_Atomic m4; // COMPLIANT | ||
} s1; | ||
|
||
void f(_Atomic int p1, // COMPLIANT | ||
_Atomic void *p2 // NON_COMPLIANT | ||
// _Atomic void p3[] // doesn't compile, even though it perhaps should as | ||
// it is adjusted to void*. | ||
) {} | ||
|
||
typedef _Atomic void *f2(void); // NON_COMPLIANT | ||
typedef _Atomic void *(*f3)(void); // NON_COMPLIANT | ||
typedef void f4(_Atomic void *); // NON_COMPLIANT | ||
typedef void (*f5)(_Atomic void *); // NON_COMPLIANT | ||
|
||
void f6() { | ||
(void *)0; // COMPLIANT | ||
(_Atomic void *)0; // NON_COMPLIANT | ||
} |
26 changes: 26 additions & 0 deletions
26
cpp/common/src/codingstandards/cpp/exclusions/c/Declarations9.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
import cpp | ||
import RuleMetadata | ||
import codingstandards.cpp.exclusions.RuleMetadata | ||
|
||
newtype Declarations9Query = TAtomicQualifierAppliedToVoidQuery() | ||
|
||
predicate isDeclarations9QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
query = | ||
// `Query` instance for the `atomicQualifierAppliedToVoid` query | ||
Declarations9Package::atomicQualifierAppliedToVoidQuery() and | ||
queryId = | ||
// `@id` for the `atomicQualifierAppliedToVoid` query | ||
"c/misra/atomic-qualifier-applied-to-void" and | ||
ruleId = "RULE-11-10" and | ||
category = "required" | ||
} | ||
|
||
module Declarations9Package { | ||
Query atomicQualifierAppliedToVoidQuery() { | ||
//autogenerate `Query` type | ||
result = | ||
// `Query` type for `atomicQualifierAppliedToVoid` query | ||
TQueryC(TDeclarations9PackageQuery(TAtomicQualifierAppliedToVoidQuery())) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"MISRA-C-2012": { | ||
"RULE-11-10": { | ||
"properties": { | ||
"obligation": "required" | ||
}, | ||
"queries": [ | ||
{ | ||
"description": "Conversions between types by using an _Atomic void type may result in undefined behavior.", | ||
"kind": "problem", | ||
"name": "The _Atomic qualifier shall not be applied to the incomplete type void", | ||
"precision": "very-high", | ||
"severity": "error", | ||
"short_name": "AtomicQualifierAppliedToVoid", | ||
"tags": [ | ||
"correctness", | ||
"external/misra/c/2012/third-edition-first-revision", | ||
"external/misra/c/2012/amendment4" | ||
] | ||
} | ||
], | ||
"title": "The _Atomic qualifier shall not be applied to the incomplete type void" | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.