From e7db0a75bc414b5acab5e37b68dcb2bf76c62a83 Mon Sep 17 00:00:00 2001
From: Grigoris Pavlakis <grigpavl@ece.auth.gr>
Date: Wed, 20 Mar 2019 20:55:35 +0200
Subject: [PATCH] Start work on report parsing and prettyprinting

Finish report pre-processing

Commit before full final test of pipeline
---
 ci/cppcheck-misra.sh | 14 ++++++------
 ci/summarizer.py     | 51 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 8 deletions(-)
 create mode 100755 ci/summarizer.py

diff --git a/ci/cppcheck-misra.sh b/ci/cppcheck-misra.sh
index 5e033050..2df95340 100755
--- a/ci/cppcheck-misra.sh
+++ b/ci/cppcheck-misra.sh
@@ -14,10 +14,10 @@ curl https://raw.githubusercontent.com/danmar/cppcheck/f4b5b156d720c712f6ce99f6e
 curl https://raw.githubusercontent.com/danmar/cppcheck/f4b5b156d720c712f6ce99f6e01d8c1b3f800d52/addons/cppcheckdata.py > cppcheckdata.py
 
 # generate dump files (XML representations of AST etc.) for all headers, source files etc.
-#for file in $(find inc/ src/ -type f)
-#do 
-#    cppcheck --dump $file
-#done
+for file in $(find inc/ src/ -type f \( -iname "*.cpp" -or -iname "*.hpp" \))
+do 
+    cppcheck --dump $file
+done
 
 # run the MISRA checks against the dumps and send the results to a file
 for file in $(find inc/ src/ -type f -name "*.dump")
@@ -25,7 +25,5 @@ do
     python misra.py $file >> ci/report.msr 2>&1
 done
 
-# clean up the report file from any useless info
-sed -i -r 's/(.*Script.*)|(.*Checking.*)|(.*MISRA.*)//gm' ci/report.msr
-sed -i -r '/(^$)/d' ci/report.msr
-sed -i -r 's/(\s\(.*\)\s)//gm' ci/report.msr
+# pre-process the generated report to remove all useless strings
+sed -i -r 's/(.*Script.*)|(.*Checking.*)|(.*MISRA.*)//gm; /(^$)/d; s/(\s\(.*\)\s)//gm; s/(\]|\[)//gm; s/(misra-c2012-)//gm' ci/report.msr
diff --git a/ci/summarizer.py b/ci/summarizer.py
new file mode 100755
index 00000000..68e62dc5
--- /dev/null
+++ b/ci/summarizer.py
@@ -0,0 +1,51 @@
+#!/bin/env python3
+
+from sys import argv
+from collections import Counter
+
+script, reportfile = argv
+
+def analyze():
+    errorsMap = {}
+    file = open(reportfile, 'r')
+    fileLines = file.readlines()
+    cppcheckNumOfErrors = len(fileLines)
+    linesSeen = set()
+    
+    for line in fileLines:  # remove duplicate lines
+        if line not in linesSeen:
+            linesSeen.add(line)
+            
+    for line in linesSeen:
+        lineContents = line.split(':')
+        fileName = lineContents[0]
+        error = (lineContents[1], lineContents[2].strip('\n'))
+
+        if fileName not in errorsMap.keys():
+            errorsMap[fileName] = list()
+            errorsMap[fileName].append(error)
+        else:
+            errorsMap[fileName].append(error)
+            
+    return errorsMap
+
+def prettyprint(errors):
+    print("\033[1m\033[91m=================================================\n")
+    print("\033[1m       Static analysis results: Error Summary        \n")
+    for key in errors:
+        for error in errors[key]:
+            print("\033[1mFile \033[93m{0}\033[91m violates rule \033[93m#{1}\033[91m of the MISRA C 2012 standard at line \033[93m{2}\033[91m".format(key, error[1], error[0]))
+    print()
+    print("\033[1m=================================================\033[0m\n")
+
+    
+
+if __name__ == "__main__":
+    errors = analyze()
+    if len(errors) == 0:
+        print("\033[1m\033[92mStatic analysis for MISRA compliance complete. No errors found.")
+        exit(0)
+    else:
+        prettyprint(errors)
+        exit(1)
+    
-- 
GitLab