Skip to content

Instantly share code, notes, and snippets.

@psalz
Created July 27, 2022 12:53
Show Gist options
  • Save psalz/5f936894a451f25e2b9d1d6f66915307 to your computer and use it in GitHub Desktop.
Save psalz/5f936894a451f25e2b9d1d6f66915307 to your computer and use it in GitHub Desktop.
Print number of unique occurences of clang tidy diagnostics in a given log file
#!/usr/bin/env python3
import sys
import re
from typing import Dict, Set
clang_tidy_log = sys.argv[1]
# A clang-tidy diagnostic is of the form:
# <path>:<line>:<column>: <warning|error>: <message> [<diagnostic-name(s)>]
diagnostic_re = re.compile(
r"^([^:]+):(\d+):(\d+): (warning|error): ([^[]+) \[([^]]+)\]")
# We keep a set of per file, per line, per column (which is probably overkill) diagnostics to remove duplicates
per_file_diagnostics: Dict[str, Dict[int, Dict[int, Set[str]]]] = {}
diagnostic_counts: Dict[str, int] = {}
diagnostics_parsed: int = 0
duplicates: int = 0
with open(clang_tidy_log) as fd:
line = fd.readline()
while line:
match = diagnostic_re.search(line)
if match:
diagnostics_parsed += 1
(file, line, column, type, msg, diagnostic) = match.groups()
per_file_diagnostics.setdefault(file, {}).setdefault(
line, {}).setdefault(column, set())
if diagnostic not in per_file_diagnostics[file][line][column]:
diagnostic_counts.setdefault(diagnostic, 0)
diagnostic_counts[diagnostic] += 1
per_file_diagnostics[file][line][column].add(diagnostic)
else:
duplicates += 1
line = fd.readline()
print(
f"Found {diagnostics_parsed - duplicates} diagnostics (out of "
f"{diagnostics_parsed} parsed; {duplicates} were duplicates).")
sorted_diagnostics = {k: v for k, v in reversed(sorted(
diagnostic_counts.items(), key=lambda item: item[1]))}
for (diagnostic, count) in sorted_diagnostics.items():
print(f"{count:>5}: {diagnostic}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment