Created
August 18, 2025 06:48
-
-
Save eiskalteschatten/492f49de70f96fd334e039c369392659 to your computer and use it in GitHub Desktop.
A Python script for parsing C++ files and making fun of older code generated by Claude Sonnet 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
C++ Code Roast Master | |
A script that recursively goes through C++ files and adds sarcastic comments | |
about outdated coding practices. | |
""" | |
import os | |
import re | |
import argparse | |
from pathlib import Path | |
from typing import List, Tuple | |
class CppRoastMaster: | |
def __init__(self): | |
# Dictionary of outdated patterns and their roasts | |
self.roast_patterns = [ | |
# Memory management | |
(r'\bnew\s+\w+(?:\[\])?(?!\s*\()', | |
"// π¦ Found raw 'new'! Are you coding like it's 1998? Ever heard of smart pointers, grandpa?"), | |
(r'\bdelete\s+\w+', | |
"// πͺ¦ Manual delete detected! What's next, managing memory with an abacus?"), | |
(r'\bmalloc\s*\(', | |
"// ποΈ malloc() in C++? Did you time travel from the Stone Age of programming?"), | |
(r'\bfree\s*\(', | |
"// β°οΈ free() - because who needs RAII when you can live dangerously like a caveman?"), | |
# Include guards | |
(r'#ifndef\s+\w+_H\s*\n#define\s+\w+_H', | |
"// π Include guards? How charmingly retro! #pragma once called, it wants its job back."), | |
# NULL instead of nullptr | |
(r'\bNULL\b', | |
"// π΄ NULL? Really? It's 2025, not 1972! nullptr is crying somewhere."), | |
# Old-style casts | |
(r'\(\s*\w+\s*\*\s*\)', | |
"// π C-style cast spotted! Type safety is for the weak, right old-timer?"), | |
# register keyword | |
(r'\bregister\s+', | |
"// π°οΈ 'register' keyword! Did you dig this code up from a archaeological site?"), | |
# auto_ptr | |
(r'\bauto_ptr\s*<', | |
"// πΊ auto_ptr! This is so deprecated even dinosaurs wouldn't use it!"), | |
# Old-style for loops | |
(r'for\s*\(\s*int\s+\w+\s*=\s*0\s*;\s*\w+\s*<\s*.+?\.size\(\)\s*;\s*\+\+\w+\s*\)', | |
"// π C++98 called, it wants its verbose for-loop back. Range-based for exists, fossil!"), | |
# Function pointers instead of std::function | |
(r'\w+\s*\(\s*\*\s*\w+\s*\)\s*\(', | |
"// π§ββοΈ Function pointer syntax! Casting spells like it's the dark ages of C++!"), | |
# using namespace std in headers | |
(r'using\s+namespace\s+std\s*;.*\.h[pp]*', | |
"// π₯ 'using namespace std' in a header? You're a walking code smell, ancient one!"), | |
# char* for strings | |
(r'char\s*\*\s*\w+(?!\s*=\s*nullptr)', | |
"// πͺ¨ char*? std::string has been around since the Clinton administration!"), | |
# printf instead of iostream | |
(r'\bprintf\s*\(', | |
"// πΊ printf? What's next, debugging with punch cards? std::cout is your friend!"), | |
# gets() function | |
(r'\bgets\s*\(', | |
"// β’οΈ gets()! This is so dangerous it's been removed from C++! You living fossil!"), | |
# Macros for constants | |
(r'#define\s+[A-Z_]+\s+\d+', | |
"// πΏ #define for constants? const/constexpr evolved while you were sleeping!"), | |
# void main | |
(r'void\s+main\s*\(', | |
"// π€‘ void main()? Even Turbo C++ from 1990 is embarrassed for you!"), | |
# Old container methods | |
(r'\.push_back\s*\(\s*std::make_pair', | |
"// 𦣠make_pair with push_back? emplace_back with {} wants a word!"), | |
] | |
def find_cpp_files(self, directory: str) -> List[Path]: | |
"""Find all C++ files recursively in the given directory.""" | |
cpp_extensions = {'.cpp', '.cxx', '.cc', '.c++', '.hpp', '.hxx', '.h++', '.h'} | |
cpp_files = [] | |
for root, dirs, files in os.walk(directory): | |
for file in files: | |
if Path(file).suffix.lower() in cpp_extensions: | |
cpp_files.append(Path(root) / file) | |
return cpp_files | |
def analyze_file(self, file_path: Path) -> List[Tuple[int, str, str]]: | |
"""Analyze a single C++ file and return found issues.""" | |
issues = [] | |
try: | |
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: | |
lines = f.readlines() | |
for line_num, line in enumerate(lines, 1): | |
for pattern, roast in self.roast_patterns: | |
if re.search(pattern, line, re.IGNORECASE): | |
issues.append((line_num, line.strip(), roast)) | |
break # Only one roast per line to avoid spam | |
except Exception as e: | |
print(f"Error reading {file_path}: {e}") | |
return issues | |
def add_roast_comments(self, file_path: Path, dry_run: bool = True) -> None: | |
"""Add roast comments to the file.""" | |
issues = self.analyze_file(file_path) | |
if not issues: | |
return | |
try: | |
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: | |
lines = f.readlines() | |
# Add comments in reverse order to maintain line numbers | |
for line_num, original_line, roast in reversed(issues): | |
lines.insert(line_num, f"{roast}\n") | |
if not dry_run: | |
with open(file_path, 'w', encoding='utf-8') as f: | |
f.writelines(lines) | |
print(f"β Roasted {file_path}") | |
else: | |
print(f"π Would roast {len(issues)} lines in {file_path}") | |
except Exception as e: | |
print(f"Error processing {file_path}: {e}") | |
def generate_report(self, directory: str) -> None: | |
"""Generate a report of all outdated practices found.""" | |
cpp_files = self.find_cpp_files(directory) | |
total_issues = 0 | |
print(f"\nπ₯ C++ CODE ROAST REPORT π₯") | |
print(f"Scanning {len(cpp_files)} C++ files in '{directory}'\n") | |
print("=" * 80) | |
for file_path in cpp_files: | |
issues = self.analyze_file(file_path) | |
if issues: | |
total_issues += len(issues) | |
print(f"\nπ {file_path}") | |
print("-" * 60) | |
for line_num, original_line, roast in issues: | |
print(f"Line {line_num:4d}: {original_line}") | |
print(f" {roast}") | |
print("\n" + "=" * 80) | |
print(f"π― ROAST SUMMARY: Found {total_issues} instances of ancient code!") | |
print("π‘ Consider upgrading to modern C++ practices!") | |
if total_issues == 0: | |
print("π Congratulations! Your code is surprisingly modern!") | |
def roast_directory(self, directory: str, dry_run: bool = True) -> None: | |
"""Roast all C++ files in the directory.""" | |
cpp_files = self.find_cpp_files(directory) | |
print(f"π― {'DRY RUN: ' if dry_run else ''}Roasting {len(cpp_files)} C++ files...") | |
roasted_count = 0 | |
for file_path in cpp_files: | |
issues = self.analyze_file(file_path) | |
if issues: | |
self.add_roast_comments(file_path, dry_run) | |
roasted_count += 1 | |
print(f"\nπ₯ {'Would roast' if dry_run else 'Roasted'} {roasted_count} files!") | |
if dry_run: | |
print("π‘ Use --no-dry-run to actually modify files") | |
def main(): | |
parser = argparse.ArgumentParser( | |
description="Recursively roast outdated C++ code with sarcastic comments" | |
) | |
parser.add_argument( | |
"directory", | |
help="Directory to scan for C++ files" | |
) | |
parser.add_argument( | |
"--report-only", "-r", | |
action="store_true", | |
help="Only generate a report, don't modify files" | |
) | |
parser.add_argument( | |
"--no-dry-run", | |
action="store_true", | |
help="Actually modify files (default is dry run)" | |
) | |
args = parser.parse_args() | |
if not os.path.exists(args.directory): | |
print(f"β Directory '{args.directory}' does not exist!") | |
return 1 | |
roaster = CppRoastMaster() | |
if args.report_only: | |
roaster.generate_report(args.directory) | |
else: | |
roaster.roast_directory(args.directory, dry_run=not args.no_dry_run) | |
return 0 | |
if __name__ == "__main__": | |
exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: