Skip to content

Commit 7977bbf

Browse files
committed
Revert "misra.py: Fix R12.3 false negative in variables declaration (#2431)"
This reverts commit 7ce8ae4.
1 parent ec4c979 commit 7977bbf

2 files changed

Lines changed: 12 additions & 137 deletions

File tree

addons/misra.py

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,82 +1349,15 @@ def misra_12_2(self, data):
13491349
if maxval >= sz:
13501350
self.reportError(token, 12, 2)
13511351

1352-
def misra_12_3(self, data, rawTokens, filename):
1353-
# We need an additional check for closing braces using in
1354-
# initialization lists and function calls, e.g.:
1355-
# struct S a = {1, 2, 3}, b, c = foo(1, 2), d;
1356-
# ^ ^
1357-
end_tokens_map = {}
1358-
1352+
def misra_12_3(self, data):
13591353
for token in data.tokenlist:
1360-
if token.scope.type in ('Enum', 'Class', 'Struct', 'Global'):
1354+
if token.str != ',' or token.scope.type == 'Enum' or \
1355+
token.scope.type == 'Class' or token.scope.type == 'Global':
1356+
continue
1357+
if token.astParent and token.astParent.str in ['(', ',', '{']:
13611358
continue
1362-
# Save end tokens from function calls in initialization
1363-
if simpleMatch(token, ') ;'):
1364-
if (token.isExpandedMacro):
1365-
end_tokens_map.setdefault(token.next.linenr, set())
1366-
end_tokens_map[token.linenr].add(token.next.column)
1367-
else:
1368-
end_tokens_map.setdefault(token.linenr, set())
1369-
end_tokens_map[token.linenr].add(token.column)
1370-
if token.str != ',':
1371-
continue
1372-
if token.astParent:
1373-
if token.astParent.str in ('(', '{'):
1374-
end_token = token.astParent.link
1375-
if end_token:
1376-
end_tokens_map.setdefault(end_token.linenr, set())
1377-
end_tokens_map[end_token.linenr].add(end_token.column)
1378-
continue
1379-
elif token.astParent.str == ',':
1380-
continue
13811359
self.reportError(token, 12, 3)
13821360

1383-
# Cppcheck performs some simplifications in variables declaration code:
1384-
# int a, b, c;
1385-
# Will be reresented in dump file as:
1386-
# int a; int b; int c;
1387-
name_tokens_map = {}
1388-
for v in data.variables:
1389-
if v.isArgument:
1390-
continue
1391-
nt = v.nameToken
1392-
if nt and nt.scope and nt.scope.type not in ('Enum', 'Class', 'Struct'):
1393-
name_tokens_map.setdefault(nt.linenr, set())
1394-
name_tokens_map[nt.linenr].add(nt.column)
1395-
if not name_tokens_map:
1396-
return
1397-
1398-
# Select tokens to check
1399-
maybe_map = {}
1400-
for linenr in set(name_tokens_map.keys()) | set(end_tokens_map.keys()):
1401-
maybe_map[linenr] = name_tokens_map.get(linenr, set())
1402-
maybe_map[linenr] |= end_tokens_map.get(linenr, set())
1403-
1404-
# Check variables declaration code in raw tokens to distinguish ';' and
1405-
# ',' symbols.
1406-
STATE_SKIP = 0
1407-
STATE_CHECK = 1
1408-
state = STATE_SKIP
1409-
cur_line = min(maybe_map)
1410-
for tok in rawTokens:
1411-
if tok.linenr in maybe_map and tok.column in maybe_map[tok.linenr]:
1412-
if tok.linenr in end_tokens_map and tok.column in end_tokens_map[tok.linenr]:
1413-
if tok.str == ',' or (tok.next and tok.next.str == ','):
1414-
self.reportError(tok, 12, 3)
1415-
end_tokens_map[tok.linenr].remove(tok.column)
1416-
state = STATE_CHECK
1417-
cur_line = tok.linenr
1418-
if tok.str in ('(', ';', '{'):
1419-
state = STATE_SKIP
1420-
if tok.linenr > cur_line:
1421-
maybe_map.pop(cur_line)
1422-
if not maybe_map:
1423-
break
1424-
cur_line = min(maybe_map)
1425-
if state == STATE_CHECK and tok.str == ',':
1426-
self.reportError(tok, 12, 3)
1427-
14281361
def misra_12_4(self, data):
14291362
if typeBits['INT'] == 16:
14301363
max_uint = 0xffff
@@ -2521,18 +2454,18 @@ def printStatus(self, *args, **kwargs):
25212454
if not self.settings.quiet:
25222455
print(*args, **kwargs)
25232456

2524-
def executeCheck(self, rule_num, check_function, *args):
2457+
def executeCheck(self, rule_num, check_function, arg):
25252458
"""Execute check function for a single MISRA rule.
25262459
25272460
:param rule_num: Number of rule in hundreds format
25282461
:param check_function: Check function to execute
2529-
:param args: Check function arguments
2462+
:param arg: Check function argument
25302463
"""
25312464
if not self.isRuleGloballySuppressed(rule_num):
2532-
check_function(*args)
2465+
check_function(arg)
25332466

25342467
def parseDump(self, dumpfile):
2535-
filename = '.'.join(dumpfile.split('.')[:-1])
2468+
25362469
data = cppcheckdata.parsedump(dumpfile)
25372470

25382471
self.dumpfileSuppressions = data.suppressions
@@ -2597,7 +2530,7 @@ def parseDump(self, dumpfile):
25972530
self.executeCheck(1201, self.misra_12_1_sizeof, data.rawTokens)
25982531
self.executeCheck(1201, self.misra_12_1, cfg)
25992532
self.executeCheck(1202, self.misra_12_2, cfg)
2600-
self.executeCheck(1203, self.misra_12_3, cfg, data.rawTokens, filename)
2533+
self.executeCheck(1203, self.misra_12_3, cfg)
26012534
self.executeCheck(1204, self.misra_12_4, cfg)
26022535
self.executeCheck(1301, self.misra_13_1, cfg)
26032536
self.executeCheck(1303, self.misra_13_3, cfg)

addons/test/misra/misra-test.c

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -332,67 +332,9 @@ void misra_12_2(u8 x) {
332332
a = x << 8; // 12.2
333333
}
334334

335-
static int misra_12_3_v1 = 0, misra_12_3_v2; // 12.3
336-
static int misra_12_3_v3, misra_12_3_v4; // 12.3
337-
enum misra_12_3_e1 { M123A1, M123B1, M123C1 };
338-
enum misra_12_3_e2 { M123A2 = 3, M123B2 = 4, M123C2 };
339-
typedef enum misra_12_3_e3 { M123A3 , M123B3, M123C3 } misra_12_3_e3_t;
340-
typedef enum { M123A4 , M123B4, M123C4 } misra_12_3_e4_t;
341-
struct misra_12_3_s1 { int a; int b; int c, d; };
342-
static struct misra_12_3_s1 misra_12_3_s1_inst = {
343-
3,
344-
4, 5,
345-
6, // no warning
346-
};
347-
typedef struct misra_12_3_s2 { int a; int b; int c, d; } misra_12_3_s2_t;
348-
typedef struct { int a; int b; int c, d; } misra_12_3_s3_t;
349-
void misra_12_3_fn1(int, int); static int misra_12_3_v5, misra_12_4_v6; // 12.3
350-
void misra_12_3_fn2(int a, int b) { int d, e; } // 12.3 2.7
351-
int misra_12_3_fn3(int a, int b) { return a+b;} static int misra_12_3_v5, misra_12_4_v6; // 12.3
352-
#define MISRA_12_3_FN3_1(A, B) (misra_12_3_fn3(A, B))
353-
#define MISRA_12_3_FN3_2(A, B) (misra_12_3_fn3(A, \
354-
B))
355-
#define MISRA_12_3_FN3_2_MSG(x) x, fflush(stderr) // 20.7
356-
void misra_12_3(int, int, int); // no warning
357-
void misra_12_3(int a, int b, int c) { // no warning
358-
int a1, a2; // 12.3
359-
int a3; int a4; // no warning
360-
int a5 = 9, a6; // 12.3
361-
int a7, a8 = 11; // 12.3
362-
int a9 = foo(), a10; // 12.3
363-
int a11 = a = b = c; // 17.8
364-
365-
struct s1 {int a, b;}; int a12, a13; // 12.3
366-
int a14, a15; misra_12_3_fn3(a14, a15); // 12.3 17.7
367-
; int a16, a17; // 12.3
368-
int a18; int a19, a20; // 12.3
369-
int a21, a22; int a23; // 12.3
370-
int a24, // 12.3
371-
a25;
372-
int a26
373-
, a27; // 12.3
374-
int a28
375-
, // 12.3
376-
a29;
377-
378-
struct misra_12_3_s2 a30 = {1, 2}, a31; // 12.3
379-
struct misra_12_3_s2 a32, a33; // 12.3
380-
struct misra_12_3_s2 a34, a35 = {1, 2}, a36; // 12.3
381-
382-
int a37 = MISRA_12_3_FN3_1(a34, a35), a38; // 12.3
383-
int a39, a40 = MISRA_12_3_FN3_1(a34, a35); // 12.3
384-
int a41 = MISRA_12_3_FN3_2(a34, a35), a42; // 12.3
385-
int a43, a44 = MISRA_12_3_FN3_2(a34, a35); // 12.3
386-
387-
MISRA_12_3_FN3_2_MSG(fprintf(stderr, "test\n")); // 12.3
388-
335+
void misra_12_3() {
389336
f((1,2),3); // TODO
390-
391-
for (i=0; i<10; i++, j++){} // 12.3
392-
for (int i = 0, p = &a1; // 12.3 14.2
393-
i < 42;
394-
++i, ++p ) // 12.3
395-
{}
337+
for (i=0;i<10;i++,j++){} // 12.3
396338
}
397339

398340
#define MISRA12_4a 2000000000u

0 commit comments

Comments
 (0)