#!/usr/bin/env python3 """ 解析 cppcheck XML 报告并生成摘要 """ import xml.etree.ElementTree as ET from pathlib import Path from collections import Counter import sys def analyze_cppcheck_report(xml_path: Path): """分析 cppcheck XML 报告""" if not xml_path.exists(): print(f"错误: 报告文件不存在: {xml_path}") return try: tree = ET.parse(xml_path) root = tree.getroot() except Exception as e: print(f"错误: 无法解析 XML 文件: {e}") return errors = root.findall(".//error") total = len(errors) if total == 0: print("✓ 未检测到任何问题!") return # 统计信息 severity_count = Counter() issue_type_count = Counter() error_issues = [] for error in errors: issue_id = error.get("id", "unknown") severity = error.get("severity", "unknown") severity_count[severity] += 1 issue_type_count[issue_id] += 1 if severity == "error": msg = error.get("msg", "") locations = error.findall("location") if locations: file_path = locations[0].get("file", "") line = locations[0].get("line", "") error_issues.append({ "id": issue_id, "file": Path(file_path).name if file_path else "unknown", "line": line, "msg": msg[:60] + "..." if len(msg) > 60 else msg }) # 打印摘要 print("=" * 60) print("Cppcheck 测试结果摘要") print("=" * 60) print(f"\n总问题数: {total}") print("\n按严重级别分类:") for sev, count in sorted(severity_count.items(), key=lambda x: {"error": 0, "warning": 1, "style": 2, "information": 3, "note": 4}.get(x[0], 5)): print(f" {sev:12s}: {count:3d} 个") print("\n按问题类型分类(前15):") for issue_id, count in issue_type_count.most_common(15): print(f" {issue_id:30s}: {count:3d} 个") if error_issues: print(f"\n严重错误详情(Error级别,共 {len(error_issues)} 个):") for i, err in enumerate(error_issues[:10], 1): print(f" {i}. [{err['id']}] {err['file']}:{err['line']}") print(f" {err['msg']}") if len(error_issues) > 10: print(f" ... 还有 {len(error_issues) - 10} 个错误未显示") print("\n" + "=" * 60) print(f"报告文件: {xml_path}") print("=" * 60) if __name__ == "__main__": if len(sys.argv) > 1: report_path = Path(sys.argv[1]) else: report_path = Path("/home/feng/test/cppcheck_report.xml") analyze_cppcheck_report(report_path)