Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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 27 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
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 Jan 25, 2023
d511157
Better polish description for RULE-6-1
jeongsoolee09 Jan 25, 2023
41e755f
make rule_packages/c/Types.json valid
jeongsoolee09 Jan 25, 2023
82dadd9
generate rule package files for c/Types.json
jeongsoolee09 Jan 25, 2023
dcda9b6
implement MISRA RULE-6-1
jeongsoolee09 Jan 26, 2023
26c8f62
Update the alert message in the select statement
jeongsoolee09 Jan 26, 2023
17b1ef9
Inline isInappropriateType and fix .expected
jeongsoolee09 Jan 26, 2023
50049bf
Add some inline comments
jeongsoolee09 Jan 26, 2023
326534f
add test.c and sketch query
jeongsoolee09 Jan 27, 2023
abb35fc
update
jeongsoolee09 Jan 27, 2023
77a4c55
really minor formatting
jeongsoolee09 Jan 27, 2023
7796fdd
separate out BitfieldTypes
jeongsoolee09 Jan 27, 2023
0e3bd23
Remove previous files for rules in Types.json
jeongsoolee09 Jan 27, 2023
1610215
update RuleMetadata.qll
jeongsoolee09 Jan 27, 2023
5f30b77
remove remaining occurrences/reference to Types
jeongsoolee09 Jan 27, 2023
e49bcd6
update test.c for RULE-6-1 to differ from given examples
jeongsoolee09 Jan 31, 2023
cceac32
Merge branch 'main' into jeongsoolee09/misra6-1_and_misra6-2
jeongsoolee09 Jan 31, 2023
6291dca
Merge branch 'jeongsoolee09/misra6-1_and_misra6-2' of github.com:gith…
jeongsoolee09 Jan 31, 2023
c0ba262
delete remaining tests and delete Types.json
jeongsoolee09 Jan 31, 2023
60f2824
update test.c for 6-1 and 6-2
jeongsoolee09 Jan 31, 2023
294e49e
update .expected files to reflect clang-format
jeongsoolee09 Jan 31, 2023
a6e86eb
incorporate requested changes for query
jeongsoolee09 Feb 1, 2023
8fddaf4
Merge branch 'main' into jeongsoolee09/misra6-1_and_misra6-2
jeongsoolee09 Feb 1, 2023
49674de
minor format
jeongsoolee09 Feb 1, 2023
c079a14
Merge branch 'jeongsoolee09/misra6-1_and_misra6-2' of github.com:gith…
jeongsoolee09 Feb 1, 2023
831cf03
format qldoc comment (indentation)
jeongsoolee09 Feb 1, 2023
3e96c5f
refine RULE-6-1
jeongsoolee09 Feb 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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() +
"."
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. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.ql
15 changes: 15 additions & 0 deletions 15 c/misra/test/rules/RULE-6-1/test.c
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;
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. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.ql
17 changes: 17 additions & 0 deletions 17 c/misra/test/rules/RULE-6-2/test.c
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
jeongsoolee09 marked this conversation as resolved.
Show resolved Hide resolved
// 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
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()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cpp
import codingstandards.cpp.exclusions.RuleMetadata
//** Import packages for this language **/
import Banned
import BitfieldTypes
import Concurrency1
import Concurrency2
import Concurrency3
Expand Down Expand Up @@ -47,6 +48,7 @@ import Syntax
/** The TQuery type representing this language * */
newtype TCQuery =
TBannedPackageQuery(BannedQuery q) or
TBitfieldTypesPackageQuery(BitfieldTypesQuery q) or
TConcurrency1PackageQuery(Concurrency1Query q) or
TConcurrency2PackageQuery(Concurrency2Query q) or
TConcurrency3PackageQuery(Concurrency3Query q) or
Expand Down Expand Up @@ -91,6 +93,7 @@ newtype TCQuery =
/** The metadata predicate * */
predicate isQueryMetadata(Query query, string queryId, string ruleId, string category) {
isBannedQueryMetadata(query, queryId, ruleId, category) or
isBitfieldTypesQueryMetadata(query, queryId, ruleId, category) or
isConcurrency1QueryMetadata(query, queryId, ruleId, category) or
isConcurrency2QueryMetadata(query, queryId, ruleId, category) or
isConcurrency3QueryMetadata(query, queryId, ruleId, category) or
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.