Skip to content

Instantly share code, notes, and snippets.

@jviotti
Created May 23, 2025 23:02
Show Gist options
  • Save jviotti/7a9ca8311ce5141cbb7025e5d302e29f to your computer and use it in GitHub Desktop.
Save jviotti/7a9ca8311ce5141cbb7025e5d302e29f to your computer and use it in GitHub Desktop.
Parse Clang Tidy output
import re
import sys
from collections import defaultdict
def split_and_group_errors(input_path):
# Regex to detect the start of an error block and capture the diagnostic name
start_pattern = re.compile(
r'^(?P<file>.+?):(?P<line>\d+):(?P<col>\d+): error: .* \[(?P<diag>[^\]]+)\]'
)
groups = defaultdict(list)
current_diag = None
current_block = []
with open(input_path, 'r') as f:
for line in f:
m = start_pattern.match(line)
if m:
# Flush previous block
if current_diag and current_block:
groups[current_diag].append(''.join(current_block).rstrip() + '\n')
# Start new block
raw_diag = m.group('diag')
# Clean diagnostic name: take up to first comma
current_diag = raw_diag.split(',')[0]
current_block = [line]
else:
if current_diag:
if line.strip() == '' and current_block[-1].strip() == '':
groups[current_diag].append(''.join(current_block).rstrip() + '\n')
current_diag = None
current_block = []
else:
current_block.append(line)
# Flush last block
if current_diag and current_block:
groups[current_diag].append(''.join(current_block).rstrip() + '\n')
# Write out files with markdown formatting
for diag, blocks in groups.items():
filename = f"{diag}.txt"
with open(filename, 'w') as out:
# Write markdown code block
out.write(f"ClangTidy warnings for `{diag}`\n\n")
out.write(f"```\n")
for block in blocks:
out.write(block + '\n')
out.write("```\n")
print(f"Written {len(blocks)} errors to {filename}")
if __name__ == '__main__':
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <clang_tidy_output.txt>")
sys.exit(1)
split_and_group_errors(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment