Skip to content

Navigation Menu

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

Commit a6e86eb

Browse filesBrowse files
committed
incorporate requested changes for query
1 parent 294e49e commit a6e86eb
Copy full SHA for a6e86eb

5 files changed

+36
-51
lines changed
+26-28Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
11
/**
2-
* @id c/misra/bit-fields-shall-only-be-declared-with-an-appropriate-type
3-
* @name RULE-6-1: Bit-fields shall only be declared with an appropriate type
4-
* @description Declaring bit-fields on types other than appropriate ones causes
5-
* implementation-specific or undefined behavior.
6-
* @kind problem
7-
* @precision very-high
8-
* @problem.severity error
9-
* @tags external/misra/id/rule-6-1
10-
* external/misra/obligation/required
11-
*/
2+
* @id c/misra/bit-fields-shall-only-be-declared-with-an-appropriate-type
3+
* @name RULE-6-1: Bit-fields shall only be declared with an appropriate type
4+
* @description Declaring bit-fields on types other than appropriate ones causes
5+
* implementation-specific or undefined behavior.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-6-1
10+
* external/misra/obligation/required
11+
*/
1212

1313
import cpp
1414
import codingstandards.c.misra
1515

16-
predicate isSignedOrUnsignedInt(Type type) {
17-
type instanceof IntType and
18-
(type.(IntegralType).isExplicitlySigned() or
19-
type.(IntegralType).isExplicitlyUnsigned())
20-
}
21-
2216
predicate isAppropriatePrimitive(Type type) {
23-
/* An appropriate primitive types to which a bit-field can be declared. */
24-
isSignedOrUnsignedInt(type) or type instanceof BoolType
25-
}
26-
27-
predicate isAppropriateTypedef(Type type) {
28-
type instanceof TypedefType and
29-
/* An appropriate typedef should be an alias to an appropriate primitive type. */
30-
isAppropriatePrimitive(type.(TypedefType).resolveTypedefs())
17+
/* An appropriate primitive types to which a bit-field can be declared. */
18+
type instanceof IntType and
19+
(
20+
type.(IntegralType).isExplicitlySigned() or
21+
type.(IntegralType).isExplicitlyUnsigned()
22+
)
23+
or
24+
type instanceof BoolType
3125
}
3226

3327
from BitField bitField
3428
where
35-
not isExcluded(bitField, BitfieldTypesPackage::bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery()) and
36-
/* A violation would neither an appropriate primitive type nor an appropriate typedef. */
37-
not (isAppropriatePrimitive(bitField.getType()) or isAppropriateTypedef(bitField.getType()))
38-
select bitField, "Bit-field " + bitField + " is declared on type " + bitField + "."
29+
not isExcluded(bitField,
30+
BitfieldTypesPackage::bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery()) and
31+
/* A violation would neither an appropriate primitive type nor an appropriate typedef. */
32+
not (
33+
isAppropriatePrimitive(bitField.getType()) or
34+
isAppropriatePrimitive(bitField.getType().resolveTypedefs())
35+
)
36+
select bitField, "Bit-field " + bitField + " is declared on type " + bitField.getType() + "."

‎c/misra/src/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.ql

Copy file name to clipboardExpand all lines: c/misra/src/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.ql
+5-16Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,18 @@
1313
import cpp
1414
import codingstandards.c.misra
1515

16-
predicate isSigned(Type type) {
17-
/* Check if it's a fixed number type, because declaring fixed number types like int8_t as 1 bit is obviously absurd */
18-
type instanceof FixedWidthIntegralType or
19-
/* Check if it's EXPLICITLY signed, because according to Rule 6.1, 'int' may be either signed or unsigned depending on the implementation. In the latter case, the query would lead to false positives. */
20-
type instanceof IntegralType and
21-
type.(IntegralType).isExplicitlySigned()
22-
}
23-
2416
/* Check if the DECLARED bit-fields is a single bit, because Rule 6.2 also intends to catch confusion on the programmers' part. Consider:
2517
2618
struct S {
2719
int32_t x: 1;
2820
}
2921
3022
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. */
31-
predicate isSingleBit(BitField bitField) {
32-
bitField.getDeclaredNumBits() = 1
33-
}
3423

3524
from BitField bitField
3625
where
37-
not isExcluded(bitField, BitfieldTypesPackage::singleBitNamedBitFieldsOfASignedTypeQuery()) and
38-
isSingleBit(bitField) and // Single-bit,
39-
not bitField.isAnonymous() and // named,
40-
isSigned(bitField.getType()) // but its type is signed.
41-
select bitField, "Single-bit bit-field named " + bitField.toString() + " has a signed type " + bitField.getType() + "."
26+
not isExcluded(bitField, BitfieldTypesPackage::singleBitNamedBitFieldsOfASignedTypeQuery()) and
27+
bitField.getDeclaredNumBits() = 1 and // Single-bit,
28+
not bitField.isAnonymous() and // named,
29+
bitField.getType().(IntegralType).isSigned() // but its type is signed.
30+
select bitField, "Single-bit bit-field named " + bitField.toString() + " has a signed type " + bitField.getType() + "."
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| test.c:6:7:6:8 | x1 | Bit-field x1 is declared on type x1. |
2-
| test.c:10:15:10:16 | x5 | Bit-field x5 is declared on type x5. |
3-
| test.c:12:15:12:16 | x6 | Bit-field x6 is declared on type x6. |
4-
| test.c:14:14:14:15 | x7 | Bit-field x7 is declared on type x7. |
1+
| test.c:6:7:6:8 | x1 | Bit-field x1 is declared on type int. |
2+
| test.c:10:15:10:16 | x5 | Bit-field x5 is declared on type signed long. |
3+
| test.c:12:15:12:16 | x6 | Bit-field x6 is declared on type signed char. |
4+
| test.c:14:14:14:15 | x7 | Bit-field x7 is declared on type Color. |
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
| test.c:4:7:4:8 | x1 | Single-bit bit-field named x1 has a signed type int. |
12
| test.c:7:14:7:15 | x2 | Single-bit bit-field named x2 has a signed type signed int. |
23
| test.c:9:7:9:8 | x3 | Single-bit bit-field named x3 has a signed type signed char. |
34
| test.c:11:7:11:8 | x4 | Single-bit bit-field named x4 has a signed type signed short. |
4-
| test.c:16:11:16:12 | x7 | Single-bit bit-field named x7 has a signed type int32_t. |

‎c/misra/test/rules/RULE-6-2/test.c

Copy file name to clipboardExpand all lines: c/misra/test/rules/RULE-6-2/test.c
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,5 @@ struct SampleStruct {
1313
x5 : 1; // COMPILANT: single-bit named field but with an unsigned type
1414
signed int x6 : 2; // COMPILANT: named field with a signed type but declared
1515
// to carry more than 1 bit
16-
int32_t x7 : 1; // NON_COMPILANT: single-bit named field that has single-bit
17-
// bit-field, even though technically it has 32 bits
1816
signed char : 1; // COMPILANT: single-bit bit-field but unnamed
1917
} sample_struct;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.