Skip to content

Commit fe01ab8

Browse files
committed
EssentialTypes: Implement Rule 10.3
Adds a query that finds "assignments", as defined by MISRA C 2012, to incompatible essential types.
1 parent 2147282 commit fe01ab8

File tree

4 files changed

+543
-0
lines changed

4 files changed

+543
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @id c/misra/assignment-of-incompatible-essential-type
3+
* @name RULE-10-3: The value of an expression shall not be assigned to an object with a narrower essential type or of a
4+
* @description The value of an expression shall not be assigned to an object with a narrower
5+
* essential type or of a different essential type category
6+
* @kind problem
7+
* @precision high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-10-3
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
import codingstandards.c.misra.EssentialTypes
16+
import codingstandards.c.misra.MisraExpressions
17+
18+
from
19+
Type lValueType, Expr rValue, Type lValueEssentialType, EssentialTypeCategory lValueTypeCategory,
20+
Type rValueEssentialType, EssentialTypeCategory rValueTypeCategory, string message
21+
where
22+
not isExcluded(rValue, EssentialTypesPackage::assignmentOfIncompatibleEssentialTypeQuery()) and
23+
isAssignmentToEssentialType(lValueType, rValue) and
24+
lValueEssentialType = lValueType and
25+
lValueTypeCategory = getEssentialTypeCategory(lValueEssentialType) and
26+
rValueEssentialType = getEssentialType(rValue) and
27+
rValueTypeCategory = getEssentialTypeCategory(rValueEssentialType) and
28+
(
29+
not lValueTypeCategory = rValueTypeCategory and
30+
message =
31+
"Assignment of " + rValueTypeCategory + " value to an object of " + lValueTypeCategory + "."
32+
or
33+
lValueTypeCategory = rValueTypeCategory and
34+
lValueEssentialType.getSize() < rValueEssentialType.getSize() and
35+
message =
36+
"Assignment of value of " + lValueTypeCategory + " of size " + rValueEssentialType.getSize() +
37+
" bytes to an object narrower essential type of size " + lValueEssentialType.getSize() +
38+
" bytes."
39+
) and
40+
// Exception 1: Constant signed integers can be assigned to unsigned integers in certain cases
41+
not exists(int const |
42+
const = rValue.getValue().toInt() and
43+
rValueTypeCategory = EssentiallySignedType() and
44+
rValueEssentialType.getSize() <= any(IntType t | t.isSigned()).getSize() and
45+
lValueTypeCategory = EssentiallyUnsignedType() and
46+
const >= 0 and
47+
const <= 2.pow(lValueEssentialType.getSize() * 8)
48+
)
49+
select rValue, message
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
| test.c:11:7:11:8 | e1 | Assignment of essentially Enum Type value to an object of essentially Boolean type. |
2+
| test.c:12:7:12:7 | s | Assignment of essentially Signed type value to an object of essentially Boolean type. |
3+
| test.c:13:7:13:7 | u | Assignment of essentially Unsigned type value to an object of essentially Boolean type. |
4+
| test.c:14:7:14:7 | f | Assignment of essentially Floating type value to an object of essentially Boolean type. |
5+
| test.c:16:8:16:8 | b | Assignment of essentially Boolean type value to an object of essentially Enum Type. |
6+
| test.c:18:8:18:8 | s | Assignment of essentially Signed type value to an object of essentially Enum Type. |
7+
| test.c:19:8:19:8 | u | Assignment of essentially Unsigned type value to an object of essentially Enum Type. |
8+
| test.c:20:8:20:8 | f | Assignment of essentially Floating type value to an object of essentially Enum Type. |
9+
| test.c:22:7:22:7 | b | Assignment of essentially Boolean type value to an object of essentially Signed type. |
10+
| test.c:23:7:23:8 | e1 | Assignment of essentially Enum Type value to an object of essentially Signed type. |
11+
| test.c:25:7:25:7 | u | Assignment of essentially Unsigned type value to an object of essentially Signed type. |
12+
| test.c:26:7:26:7 | f | Assignment of essentially Floating type value to an object of essentially Signed type. |
13+
| test.c:28:7:28:7 | b | Assignment of essentially Boolean type value to an object of essentially Unsigned type. |
14+
| test.c:29:7:29:8 | e1 | Assignment of essentially Enum Type value to an object of essentially Unsigned type. |
15+
| test.c:30:7:30:7 | s | Assignment of essentially Signed type value to an object of essentially Unsigned type. |
16+
| test.c:32:7:32:7 | f | Assignment of essentially Floating type value to an object of essentially Unsigned type. |
17+
| test.c:34:7:34:7 | b | Assignment of essentially Boolean type value to an object of essentially Floating type. |
18+
| test.c:35:7:35:8 | e1 | Assignment of essentially Enum Type value to an object of essentially Floating type. |
19+
| test.c:36:7:36:7 | s | Assignment of essentially Signed type value to an object of essentially Floating type. |
20+
| test.c:37:7:37:7 | u | Assignment of essentially Unsigned type value to an object of essentially Floating type. |
21+
| test.c:49:14:49:15 | e1 | Assignment of essentially Enum Type value to an object of essentially Boolean type. |
22+
| test.c:50:14:50:14 | s | Assignment of essentially Signed type value to an object of essentially Boolean type. |
23+
| test.c:51:14:51:14 | u | Assignment of essentially Unsigned type value to an object of essentially Boolean type. |
24+
| test.c:52:14:52:14 | f | Assignment of essentially Floating type value to an object of essentially Boolean type. |
25+
| test.c:54:17:54:17 | b | Assignment of essentially Boolean type value to an object of essentially Enum Type. |
26+
| test.c:56:17:56:17 | s | Assignment of essentially Signed type value to an object of essentially Enum Type. |
27+
| test.c:57:17:57:17 | u | Assignment of essentially Unsigned type value to an object of essentially Enum Type. |
28+
| test.c:58:17:58:17 | f | Assignment of essentially Floating type value to an object of essentially Enum Type. |
29+
| test.c:60:19:60:19 | b | Assignment of essentially Boolean type value to an object of essentially Signed type. |
30+
| test.c:61:19:61:20 | e1 | Assignment of essentially Enum Type value to an object of essentially Signed type. |
31+
| test.c:63:19:63:19 | u | Assignment of essentially Unsigned type value to an object of essentially Signed type. |
32+
| test.c:64:19:64:19 | f | Assignment of essentially Floating type value to an object of essentially Signed type. |
33+
| test.c:66:21:66:21 | b | Assignment of essentially Boolean type value to an object of essentially Unsigned type. |
34+
| test.c:67:21:67:22 | e1 | Assignment of essentially Enum Type value to an object of essentially Unsigned type. |
35+
| test.c:68:21:68:21 | s | Assignment of essentially Signed type value to an object of essentially Unsigned type. |
36+
| test.c:70:21:70:21 | f | Assignment of essentially Floating type value to an object of essentially Unsigned type. |
37+
| test.c:72:14:72:14 | b | Assignment of essentially Boolean type value to an object of essentially Floating type. |
38+
| test.c:73:14:73:15 | e1 | Assignment of essentially Enum Type value to an object of essentially Floating type. |
39+
| test.c:74:14:74:14 | s | Assignment of essentially Signed type value to an object of essentially Floating type. |
40+
| test.c:75:14:75:14 | u | Assignment of essentially Unsigned type value to an object of essentially Floating type. |
41+
| test.c:80:7:80:8 | e1 | Assignment of essentially Enum Type value to an object of essentially Boolean type. |
42+
| test.c:81:7:81:7 | s | Assignment of essentially Signed type value to an object of essentially Boolean type. |
43+
| test.c:82:7:82:7 | u | Assignment of essentially Unsigned type value to an object of essentially Boolean type. |
44+
| test.c:83:7:83:7 | f | Assignment of essentially Floating type value to an object of essentially Boolean type. |
45+
| test.c:86:7:86:7 | b | Assignment of essentially Boolean type value to an object of essentially Enum Type. |
46+
| test.c:88:7:88:7 | s | Assignment of essentially Signed type value to an object of essentially Enum Type. |
47+
| test.c:89:7:89:7 | u | Assignment of essentially Unsigned type value to an object of essentially Enum Type. |
48+
| test.c:90:7:90:7 | f | Assignment of essentially Floating type value to an object of essentially Enum Type. |
49+
| test.c:93:7:93:7 | b | Assignment of essentially Boolean type value to an object of essentially Signed type. |
50+
| test.c:94:7:94:8 | e1 | Assignment of essentially Enum Type value to an object of essentially Signed type. |
51+
| test.c:96:7:96:7 | u | Assignment of essentially Unsigned type value to an object of essentially Signed type. |
52+
| test.c:97:7:97:7 | f | Assignment of essentially Floating type value to an object of essentially Signed type. |
53+
| test.c:100:7:100:7 | b | Assignment of essentially Boolean type value to an object of essentially Unsigned type. |
54+
| test.c:101:7:101:8 | e1 | Assignment of essentially Enum Type value to an object of essentially Unsigned type. |
55+
| test.c:102:7:102:7 | s | Assignment of essentially Signed type value to an object of essentially Unsigned type. |
56+
| test.c:104:7:104:7 | f | Assignment of essentially Floating type value to an object of essentially Unsigned type. |
57+
| test.c:107:7:107:7 | b | Assignment of essentially Boolean type value to an object of essentially Floating type. |
58+
| test.c:108:7:108:8 | e1 | Assignment of essentially Enum Type value to an object of essentially Floating type. |
59+
| test.c:109:7:109:7 | s | Assignment of essentially Signed type value to an object of essentially Floating type. |
60+
| test.c:110:7:110:7 | u | Assignment of essentially Unsigned type value to an object of essentially Floating type. |
61+
| test.c:118:7:118:8 | - ... | Assignment of essentially Signed type value to an object of essentially Unsigned type. |
62+
| test.c:119:7:119:16 | 4294967296 | Assignment of essentially Signed type value to an object of essentially Unsigned type. |
63+
| test.c:131:8:131:8 | A | Assignment of essentially Enum Type value to an object of essentially Boolean type. |
64+
| test.c:132:8:132:10 | 100 | Assignment of essentially Signed type value to an object of essentially Boolean type. |
65+
| test.c:133:23:133:25 | 200 | Assignment of essentially Unsigned type value to an object of essentially Boolean type. |
66+
| test.c:138:8:138:11 | 1 | Assignment of essentially Boolean type value to an object of essentially Enum Type. |
67+
| test.c:140:8:140:10 | 100 | Assignment of essentially Signed type value to an object of essentially Enum Type. |
68+
| test.c:141:23:141:25 | 200 | Assignment of essentially Unsigned type value to an object of essentially Enum Type. |
69+
| test.c:146:8:146:11 | 1 | Assignment of essentially Boolean type value to an object of essentially Signed type. |
70+
| test.c:147:8:147:8 | A | Assignment of essentially Enum Type value to an object of essentially Signed type. |
71+
| test.c:149:23:149:25 | 200 | Assignment of essentially Unsigned type value to an object of essentially Signed type. |
72+
| test.c:154:8:154:11 | 1 | Assignment of essentially Boolean type value to an object of essentially Unsigned type. |
73+
| test.c:155:8:155:8 | A | Assignment of essentially Enum Type value to an object of essentially Unsigned type. |
74+
| test.c:174:8:174:8 | b | Assignment of essentially Boolean type value to an object of essentially Enum Type. |
75+
| test.c:175:8:175:8 | b | Assignment of essentially Boolean type value to an object of essentially Signed type. |
76+
| test.c:176:8:176:8 | b | Assignment of essentially Boolean type value to an object of essentially Unsigned type. |
77+
| test.c:177:8:177:8 | b | Assignment of essentially Boolean type value to an object of essentially Floating type. |
78+
| test.c:180:8:180:9 | e1 | Assignment of essentially Enum Type value to an object of essentially Boolean type. |
79+
| test.c:182:8:182:9 | e1 | Assignment of essentially Enum Type value to an object of essentially Signed type. |
80+
| test.c:183:8:183:9 | e1 | Assignment of essentially Enum Type value to an object of essentially Unsigned type. |
81+
| test.c:184:8:184:9 | e1 | Assignment of essentially Enum Type value to an object of essentially Floating type. |
82+
| test.c:187:8:187:8 | s | Assignment of essentially Signed type value to an object of essentially Boolean type. |
83+
| test.c:188:8:188:8 | s | Assignment of essentially Signed type value to an object of essentially Enum Type. |
84+
| test.c:190:8:190:8 | s | Assignment of essentially Signed type value to an object of essentially Unsigned type. |
85+
| test.c:191:8:191:8 | s | Assignment of essentially Signed type value to an object of essentially Floating type. |
86+
| test.c:194:8:194:8 | u | Assignment of essentially Unsigned type value to an object of essentially Boolean type. |
87+
| test.c:195:8:195:8 | u | Assignment of essentially Unsigned type value to an object of essentially Enum Type. |
88+
| test.c:196:8:196:8 | u | Assignment of essentially Unsigned type value to an object of essentially Signed type. |
89+
| test.c:198:8:198:8 | u | Assignment of essentially Unsigned type value to an object of essentially Floating type. |
90+
| test.c:201:8:201:8 | f | Assignment of essentially Floating type value to an object of essentially Boolean type. |
91+
| test.c:202:8:202:8 | f | Assignment of essentially Floating type value to an object of essentially Enum Type. |
92+
| test.c:203:8:203:8 | f | Assignment of essentially Floating type value to an object of essentially Signed type. |
93+
| test.c:204:8:204:8 | f | Assignment of essentially Floating type value to an object of essentially Unsigned type. |
94+
| test.c:220:12:220:13 | e1 | Assignment of essentially Enum Type value to an object of essentially Boolean type. |
95+
| test.c:222:12:222:12 | s | Assignment of essentially Signed type value to an object of essentially Boolean type. |
96+
| test.c:224:12:224:12 | u | Assignment of essentially Unsigned type value to an object of essentially Boolean type. |
97+
| test.c:226:12:226:12 | f | Assignment of essentially Floating type value to an object of essentially Boolean type. |
98+
| test.c:239:12:239:12 | b | Assignment of essentially Boolean type value to an object of essentially Enum Type. |
99+
| test.c:243:12:243:12 | s | Assignment of essentially Signed type value to an object of essentially Enum Type. |
100+
| test.c:245:12:245:12 | u | Assignment of essentially Unsigned type value to an object of essentially Enum Type. |
101+
| test.c:247:12:247:12 | f | Assignment of essentially Floating type value to an object of essentially Enum Type. |
102+
| test.c:260:12:260:12 | b | Assignment of essentially Boolean type value to an object of essentially Signed type. |
103+
| test.c:262:12:262:13 | e1 | Assignment of essentially Enum Type value to an object of essentially Signed type. |
104+
| test.c:266:12:266:12 | u | Assignment of essentially Unsigned type value to an object of essentially Signed type. |
105+
| test.c:268:12:268:12 | f | Assignment of essentially Floating type value to an object of essentially Signed type. |
106+
| test.c:281:12:281:12 | b | Assignment of essentially Boolean type value to an object of essentially Unsigned type. |
107+
| test.c:283:12:283:13 | e1 | Assignment of essentially Enum Type value to an object of essentially Unsigned type. |
108+
| test.c:285:12:285:12 | s | Assignment of essentially Signed type value to an object of essentially Unsigned type. |
109+
| test.c:289:12:289:12 | f | Assignment of essentially Floating type value to an object of essentially Unsigned type. |
110+
| test.c:302:12:302:12 | b | Assignment of essentially Boolean type value to an object of essentially Floating type. |
111+
| test.c:304:12:304:13 | e1 | Assignment of essentially Enum Type value to an object of essentially Floating type. |
112+
| test.c:306:12:306:12 | s | Assignment of essentially Signed type value to an object of essentially Floating type. |
113+
| test.c:308:12:308:12 | u | Assignment of essentially Unsigned type value to an object of essentially Floating type. |
114+
| test.c:332:10:332:11 | e1 | Assignment of essentially Enum Type value to an object of essentially Boolean type. |
115+
| test.c:333:10:333:10 | s | Assignment of essentially Signed type value to an object of essentially Boolean type. |
116+
| test.c:334:10:334:10 | u | Assignment of essentially Unsigned type value to an object of essentially Boolean type. |
117+
| test.c:335:10:335:10 | f | Assignment of essentially Floating type value to an object of essentially Boolean type. |
118+
| test.c:337:11:337:11 | b | Assignment of essentially Boolean type value to an object of essentially Enum Type. |
119+
| test.c:339:11:339:11 | s | Assignment of essentially Signed type value to an object of essentially Enum Type. |
120+
| test.c:340:11:340:11 | u | Assignment of essentially Unsigned type value to an object of essentially Enum Type. |
121+
| test.c:341:11:341:11 | f | Assignment of essentially Floating type value to an object of essentially Enum Type. |
122+
| test.c:343:10:343:10 | b | Assignment of essentially Boolean type value to an object of essentially Signed type. |
123+
| test.c:344:10:344:11 | e1 | Assignment of essentially Enum Type value to an object of essentially Signed type. |
124+
| test.c:346:10:346:10 | u | Assignment of essentially Unsigned type value to an object of essentially Signed type. |
125+
| test.c:347:10:347:10 | f | Assignment of essentially Floating type value to an object of essentially Signed type. |
126+
| test.c:349:10:349:10 | b | Assignment of essentially Boolean type value to an object of essentially Unsigned type. |
127+
| test.c:350:10:350:11 | e1 | Assignment of essentially Enum Type value to an object of essentially Unsigned type. |
128+
| test.c:351:10:351:10 | s | Assignment of essentially Signed type value to an object of essentially Unsigned type. |
129+
| test.c:353:10:353:10 | f | Assignment of essentially Floating type value to an object of essentially Unsigned type. |
130+
| test.c:355:10:355:10 | b | Assignment of essentially Boolean type value to an object of essentially Floating type. |
131+
| test.c:356:10:356:11 | e1 | Assignment of essentially Enum Type value to an object of essentially Floating type. |
132+
| test.c:357:10:357:10 | s | Assignment of essentially Signed type value to an object of essentially Floating type. |
133+
| test.c:358:10:358:10 | u | Assignment of essentially Unsigned type value to an object of essentially Floating type. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-10-3/AssignmentOfIncompatibleEssentialType.ql

0 commit comments

Comments
 (0)