From 9c8fc6253a2ebe6a341c6506fc83dee299d9074b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 14 Apr 2017 11:20:20 +0200 Subject: [PATCH] Misra: Added verification code in the script to simplify testing --- addons/misra-test.c | 6 ++---- addons/misra.py | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/addons/misra-test.c b/addons/misra-test.c index ddd75fec9..ed6c27896 100644 --- a/addons/misra-test.c +++ b/addons/misra-test.c @@ -1,7 +1,5 @@ -/* -~/cppcheck/cppcheck --dump misra-test.c -python misra.py misra-test.c.dump -*/ +// To test: +// ~/cppcheck/cppcheck --dump misra-test.c && python misra.py -verify misra-test.c.dump typedef unsigned char u8; diff --git a/addons/misra.py b/addons/misra.py index d319ab0e7..54ca6337f 100644 --- a/addons/misra.py +++ b/addons/misra.py @@ -15,9 +15,15 @@ import cppcheckdata import sys import re +VERIFY = False +VERIFY_EXPECTED = [] +VERIFY_ACTUAL = [] + def reportError(token, num1, num2): - sys.stderr.write( - '[' + token.file + ':' + str(token.linenr) + '] misra ' + str(num1) + '.' + str(num2) + ' violation\n') + if VERIFY: + VERIFY_ACTUAL.append(str(token.linenr) + ':' + str(num1) + '.' + str(num2)) + else: + sys.stderr.write('[' + token.file + ':' + str(token.linenr) + '] misra ' + str(num1) + '.' + str(num2) + ' violation\n') # Platform # TODO get this from dump @@ -439,10 +445,26 @@ def misra_16_3(rawTokens): elif token.str == 'case' and state != 2: reportError(token, 16, 3) +if '-verify' in sys.argv[1:]: + VERIFY = True + for arg in sys.argv[1:]: - print('Checking ' + arg + '...') + if not arg.endswith('.dump'): + continue + data = cppcheckdata.parsedump(arg) + if VERIFY: + VERIFY_ACTUAL = [] + VERIFY_EXPECTED = [] + for tok in data.rawTokens: + if tok.str.startswith('//') and tok.str.find('TODO')<0: + for word in tok.str[2:].split(' '): + if re.match(r'[0-9]+\.[0-9]+', word): + VERIFY_EXPECTED.append(str(tok.linenr) + ':' + word) + else: + print('Checking ' + arg + '...') + cfgNumber = 0 for cfg in data.configurations: @@ -478,3 +500,12 @@ for arg in sys.argv[1:]: if cfgNumber == 1: misra_16_3(data.rawTokens) + if VERIFY: + for expected in VERIFY_EXPECTED: + if not expected in VERIFY_ACTUAL: + print('Expected but not seen: ' + expected) + sys.exit(1) + for actual in VERIFY_ACTUAL: + if not actual in VERIFY_EXPECTED: + print('Not expected: ' + actual) + sys.exit(1)