Skip to content

Commit 7b73896

Browse files
committed
EssentialTypes: Fix integer constant types
Integer constants are only subject to the stlr or utlr if they are type int, signed int or unsigned int, otherwise they have the standard type. Fixes issues with RULE 12.2.
1 parent 7a6b8df commit 7b73896

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

c/misra/src/codingstandards/c/misra/EssentialTypes.qll

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,17 @@ class EssentialLiteral extends EssentialExpr, Literal {
355355
else (
356356
if this.(CharLiteral).getCharacter().length() = 1
357357
then result instanceof PlainCharType
358-
else (
359-
getStandardType().(IntegralType).isSigned() and
360-
result = stlr(this)
361-
or
362-
not getStandardType().(IntegralType).isSigned() and
363-
result = utlr(this)
364-
)
358+
else
359+
exists(Type underlyingStandardType |
360+
underlyingStandardType = getStandardType().getUnderlyingType()
361+
|
362+
if underlyingStandardType instanceof IntType
363+
then
364+
if underlyingStandardType.(IntType).isSigned()
365+
then result = stlr(this)
366+
else result = utlr(this)
367+
else result = underlyingStandardType
368+
)
365369
)
366370
}
367371
}

c/misra/test/c/misra/EssentialTypes.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@
3838
| test.c:26:3:26:3 | f | float | float | essentially Floating type |
3939
| test.c:27:3:27:5 | f32 | float32_t | float32_t | essentially Floating type |
4040
| test.c:28:3:28:6 | cf32 | float | float | essentially Floating type |
41+
| test.c:32:3:32:3 | 1 | signed char | signed char | essentially Signed type |
42+
| test.c:33:3:33:4 | 1 | unsigned char | unsigned char | essentially Unsigned type |
43+
| test.c:34:3:34:5 | 1 | unsigned long | unsigned long | essentially Unsigned type |

c/misra/test/c/misra/test.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ void testCategoriesForComplexTypes() {
2626
f; // Should be essentially Floating type
2727
f32; // Should be essentially Floating type
2828
cf32; // Should be essentially Floating type
29+
}
30+
31+
void testConstants() {
32+
1; // Essentially signed char
33+
1U; // Essentially unsigned char
34+
1UL; // Essentially unsigned long
2935
}

0 commit comments

Comments
 (0)