etc.git


etc

~ scripts and random things
git clone https://github.com/djnnvx/etc.git
log | files | refs | archive

analyze-cppcheck (2422B)

      1 #!/usr/bin/env python3
      2 
      3 import xml.etree.ElementTree as ET
      4 import sys
      5 from collections import defaultdict
      6 
      7 def analyze_cppcheck_results(xml_file_path):
      8     """
      9     Parses a Cppcheck XML report, ranks findings by severity, and prints a summary.
     10 
     11     To get XML file:
     12         cppcheck --enable=all --xml <directory-with-source-code> 2> cpp-check.xml
     13 
     14 
     15     Args:
     16         xml_file_path (str): The path to the Cppcheck XML output file.
     17     """
     18     try:
     19         tree = ET.parse(xml_file_path)
     20         root = tree.getroot()
     21     except FileNotFoundError:
     22         print(f"Error: The file '{xml_file_path}' was not found.")
     23         return
     24     except ET.ParseError:
     25         print(f"Error: Failed to parse the XML file '{xml_file_path}'. Please ensure it is a valid Cppcheck XML report.")
     26         return
     27 
     28     # Severity levels, ordered from highest to lowest criticity
     29     severity_order = ['error', 'warning', 'style', 'performance', 'portability', 'information']
     30     findings_by_severity = defaultdict(list)
     31 
     32     for error_element in root.findall('.//error'):
     33         severity = error_element.get('severity')
     34         message = error_element.get('msg')
     35         file = ""
     36         line = ""
     37 
     38         # Find the first 'location' element to get the file and line number
     39         location_element = error_element.find('location')
     40         if location_element is not None:
     41             file = location_element.get('file', 'N/A')
     42             line = location_element.get('line', 'N/A')
     43 
     44         if severity:
     45             findings_by_severity[severity].append({
     46                 'message': message,
     47                 'file': file,
     48                 'line': line,
     49             })
     50 
     51     print("--- Cppcheck Analysis Report ---")
     52     print(f"Found {sum(len(v) for v in findings_by_severity.values())} total issues.\n")
     53 
     54     for severity in severity_order:
     55         if severity in findings_by_severity:
     56             findings = findings_by_severity[severity]
     57             print(f"[{severity.upper()}] - Total: {len(findings)}")
     58             print("-" * 25)
     59             for finding in findings:
     60                 print(f"  - {finding['message']}")
     61                 print(f"    - File: {finding['file']}:{finding['line']}")
     62             print("\n")
     63 
     64 if __name__ == '__main__':
     65     if len(sys.argv) < 2:
     66         print("Usage: python analyze_cppcheck_results.py <path_to_xml_file>")
     67         sys.exit(1)
     68 
     69     xml_file = sys.argv[1]
     70     analyze_cppcheck_results(xml_file)