-
Notifications
You must be signed in to change notification settings - Fork 66
Initiate Types Rule Package and Implement MISRA RULE-6-1 and RULE-6-2 #174
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
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
696111b
Add rule_packages/c/Types.json and add description for RULE-6-1 and R…
jeongsoolee09 d511157
Better polish description for RULE-6-1
jeongsoolee09 41e755f
make rule_packages/c/Types.json valid
jeongsoolee09 82dadd9
generate rule package files for c/Types.json
jeongsoolee09 dcda9b6
implement MISRA RULE-6-1
jeongsoolee09 26c8f62
Update the alert message in the select statement
jeongsoolee09 17b1ef9
Inline isInappropriateType and fix .expected
jeongsoolee09 50049bf
Add some inline comments
jeongsoolee09 326534f
add test.c and sketch query
jeongsoolee09 abb35fc
update
jeongsoolee09 77a4c55
really minor formatting
jeongsoolee09 7796fdd
separate out BitfieldTypes
jeongsoolee09 0e3bd23
Remove previous files for rules in Types.json
jeongsoolee09 1610215
update RuleMetadata.qll
jeongsoolee09 5f30b77
remove remaining occurrences/reference to Types
jeongsoolee09 e49bcd6
update test.c for RULE-6-1 to differ from given examples
jeongsoolee09 cceac32
Merge branch 'main' into jeongsoolee09/misra6-1_and_misra6-2
jeongsoolee09 6291dca
Merge branch 'jeongsoolee09/misra6-1_and_misra6-2' of github.com:gith…
jeongsoolee09 c0ba262
delete remaining tests and delete Types.json
jeongsoolee09 60f2824
update test.c for 6-1 and 6-2
jeongsoolee09 294e49e
update .expected files to reflect clang-format
jeongsoolee09 a6e86eb
incorporate requested changes for query
jeongsoolee09 8fddaf4
Merge branch 'main' into jeongsoolee09/misra6-1_and_misra6-2
jeongsoolee09 49674de
minor format
jeongsoolee09 c079a14
Merge branch 'jeongsoolee09/misra6-1_and_misra6-2' of github.com:gith…
jeongsoolee09 831cf03
format qldoc comment (indentation)
jeongsoolee09 3e96c5f
refine RULE-6-1
jeongsoolee09 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
33 changes: 33 additions & 0 deletions
33
c/misra/src/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.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,33 @@ | ||
/** | ||
* @id c/misra/bit-fields-shall-only-be-declared-with-an-appropriate-type | ||
* @name RULE-6-1: Bit-fields shall only be declared with an appropriate type | ||
* @description Declaring bit-fields on types other than appropriate ones causes | ||
* implementation-specific or undefined behavior. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity error | ||
* @tags external/misra/id/rule-6-1 | ||
* external/misra/obligation/required | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
|
||
predicate isAppropriatePrimitive(Type type) { | ||
/* An appropriate primitive types to which a bit-field can be declared. */ | ||
type instanceof IntType and | ||
( | ||
type.(IntegralType).isExplicitlySigned() or | ||
type.(IntegralType).isExplicitlyUnsigned() | ||
) | ||
or | ||
type instanceof BoolType | ||
} | ||
|
||
from BitField bitField | ||
where | ||
not isExcluded(bitField, | ||
BitfieldTypesPackage::bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery()) and | ||
/* A violation would neither be an appropriate primitive type nor an appropriate typedef. */ | ||
not isAppropriatePrimitive(bitField.getType().resolveTypedefs()) | ||
select bitField, "Bit-field " + bitField + " is declared on type " + bitField.getType() + "." |
34 changes: 34 additions & 0 deletions
34
c/misra/src/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.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,34 @@ | ||
/** | ||
* @id c/misra/single-bit-named-bit-fields-of-a-signed-type | ||
* @name RULE-6-2: Single-bit named bit fields shall not be of a signed type | ||
* @description Single-bit named bit fields carry no useful information and therefore should not be | ||
* declared or used. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity error | ||
* @tags external/misra/id/rule-6-2 | ||
* external/misra/obligation/required | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
|
||
/* | ||
* Check if the DECLARED bit-fields is a single bit, because Rule 6.2 also intends to catch confusion on the programmers' part. Consider: | ||
* | ||
* struct S { | ||
* int32_t x: 1; | ||
* } | ||
* | ||
* In this case, field x is essentially of 32 bits, but is declared as 1 bit and its type int32_t is signed. Therefore, it indicates confusion by the programmer, which is exactly what this rule intends to find. | ||
*/ | ||
|
||
from BitField bitField | ||
where | ||
not isExcluded(bitField, BitfieldTypesPackage::singleBitNamedBitFieldsOfASignedTypeQuery()) and | ||
bitField.getDeclaredNumBits() = 1 and // Single-bit, | ||
not bitField.isAnonymous() and // named, | ||
bitField.getType().(IntegralType).isSigned() // but its type is signed. | ||
select bitField, | ||
"Single-bit bit-field named " + bitField.toString() + " has a signed type " + bitField.getType() + | ||
"." |
4 changes: 4 additions & 0 deletions
4
c/misra/test/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.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,4 @@ | ||
| test.c:6:7:6:8 | x1 | Bit-field x1 is declared on type int. | | ||
| test.c:10:15:10:16 | x5 | Bit-field x5 is declared on type signed long. | | ||
| test.c:12:15:12:16 | x6 | Bit-field x6 is declared on type signed char. | | ||
| test.c:14:14:14:15 | x7 | Bit-field x7 is declared on type Color. | |
1 change: 1 addition & 0 deletions
1
c/misra/test/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.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-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.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,15 @@ | ||
typedef unsigned int UINT16; | ||
|
||
enum Color { R, G, B }; | ||
|
||
struct SampleStruct { | ||
int x1 : 2; // NON_COMPLIANT - not explicitly signed or unsigned | ||
unsigned int x2 : 2; // COMPILANT - explicitly unsigned | ||
signed int x3 : 2; // COMPILANT - explicitly signed | ||
UINT16 x4 : 2; // COMPLIANT - type alias resolves to a compliant type | ||
signed long x5 : 2; // NON_COMPLIANT - cannot declare bit field for long, even | ||
// if it's signed | ||
signed char x6 : 2; // NON_COMPILANT - cannot declare bit field for char, even | ||
// if it's signed | ||
enum Color x7 : 3; // NON_COMPILANT - cannot declare bit field for enum | ||
} sample_struct; |
4 changes: 4 additions & 0 deletions
4
c/misra/test/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.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,4 @@ | ||
| test.c:4:7:4:8 | x1 | Single-bit bit-field named x1 has a signed type int. | | ||
| test.c:7:14:7:15 | x2 | Single-bit bit-field named x2 has a signed type signed int. | | ||
| test.c:9:7:9:8 | x3 | Single-bit bit-field named x3 has a signed type signed char. | | ||
| test.c:11:7:11:8 | x4 | Single-bit bit-field named x4 has a signed type signed short. | |
1 change: 1 addition & 0 deletions
1
c/misra/test/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.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-6-2/SingleBitNamedBitFieldsOfASignedType.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,17 @@ | ||
#include <stdint.h> | ||
|
||
struct SampleStruct { | ||
int x1 : 1; // NON_COMPILANT: very likely be signed, but if it's not, the | ||
// query will automatically handle it since we use signed(), not | ||
// isExplicitlySigned(). | ||
signed int x2 : 1; // NON_COMPILANT: single-bit named field with a signed type | ||
signed char | ||
x3 : 1; // NON_COMPILANT: single-bit named field with a signed type | ||
signed short | ||
x4 : 1; // NON_COMPILANT: single-bit named field with a signed type | ||
unsigned int | ||
x5 : 1; // COMPILANT: single-bit named field but with an unsigned type | ||
signed int x6 : 2; // COMPILANT: named field with a signed type but declared | ||
// to carry more than 1 bit | ||
signed char : 1; // COMPILANT: single-bit bit-field but unnamed | ||
} sample_struct; |
44 changes: 44 additions & 0 deletions
44
cpp/common/src/codingstandards/cpp/exclusions/c/BitfieldTypes.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,44 @@ | ||
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
import cpp | ||
import RuleMetadata | ||
import codingstandards.cpp.exclusions.RuleMetadata | ||
|
||
newtype BitfieldTypesQuery = | ||
TBitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery() or | ||
TSingleBitNamedBitFieldsOfASignedTypeQuery() | ||
|
||
predicate isBitfieldTypesQueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
query = | ||
// `Query` instance for the `bitFieldsShallOnlyBeDeclaredWithAnAppropriateType` query | ||
BitfieldTypesPackage::bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery() and | ||
queryId = | ||
// `@id` for the `bitFieldsShallOnlyBeDeclaredWithAnAppropriateType` query | ||
"c/misra/bit-fields-shall-only-be-declared-with-an-appropriate-type" and | ||
ruleId = "RULE-6-1" and | ||
category = "required" | ||
or | ||
query = | ||
// `Query` instance for the `singleBitNamedBitFieldsOfASignedType` query | ||
BitfieldTypesPackage::singleBitNamedBitFieldsOfASignedTypeQuery() and | ||
queryId = | ||
// `@id` for the `singleBitNamedBitFieldsOfASignedType` query | ||
"c/misra/single-bit-named-bit-fields-of-a-signed-type" and | ||
ruleId = "RULE-6-2" and | ||
category = "required" | ||
} | ||
|
||
module BitfieldTypesPackage { | ||
Query bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery() { | ||
//autogenerate `Query` type | ||
result = | ||
// `Query` type for `bitFieldsShallOnlyBeDeclaredWithAnAppropriateType` query | ||
TQueryC(TBitfieldTypesPackageQuery(TBitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery())) | ||
} | ||
|
||
Query singleBitNamedBitFieldsOfASignedTypeQuery() { | ||
//autogenerate `Query` type | ||
result = | ||
// `Query` type for `singleBitNamedBitFieldsOfASignedType` query | ||
TQueryC(TBitfieldTypesPackageQuery(TSingleBitNamedBitFieldsOfASignedTypeQuery())) | ||
} | ||
} |
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.