Skip to content

Conversation

voloved
Copy link
Contributor

@voloved voloved commented Aug 27, 2025

Removed duplicate includes found with a Python script.

@voloved
Copy link
Contributor Author

voloved commented Aug 27, 2025

Sharing the Python script. It was a ChatGPT special.

import os
import re
from collections import defaultdict

# Pattern to match #include "..." or #include <...>
include_pattern = re.compile(r'^\s*#include\s+[<"](.+)[>"]')

def find_duplicate_includes_in_file(filepath):
    seen = set()
    duplicates = set()
    with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
        for line in f:
            match = include_pattern.match(line)
            if match:
                header = match.group(1)
                if header in seen:
                    duplicates.add(header)
                else:
                    seen.add(header)
    return duplicates

def scan_directory_for_duplicates(root_dir):
    results = defaultdict(list)
    for dirpath, _, filenames in os.walk(root_dir):
        for filename in filenames:
            if filename.endswith(('.c', '.h', '.cpp', '.hpp')):
                full_path = os.path.join(dirpath, filename)
                duplicates = find_duplicate_includes_in_file(full_path)
                if duplicates:
                    results[full_path] = sorted(duplicates)
    return results

if __name__ == '__main__':
    directory = '.'  # Change to your source directory if needed
    duplicates_by_file = scan_directory_for_duplicates(directory)
    if not duplicates_by_file:
        print("No duplicate includes found.")
    else:
        print("Duplicate includes found:")
        for file, duplicates in duplicates_by_file.items():
            print(f"\n{file}:")
            for header in duplicates:
                print(f"  {header}")

@jimdigriz
Copy link

jimdigriz commented Aug 27, 2025

...when the world loses faith in its own abilities:

alex@hanzawa:~/src/sensorwatch/second-movement$ find . -type f \( -name '*.c' -o -name '*.h' \) | xargs -I{} sed -ne 's~#include~{}\t&~ p' '{}' | sort | uniq -d
./legacy/watch_faces/clock/world_clock2_face.c	#include "watch_utility.h"
      ./lib/chirpy_tx/test/unity_internals.h	#include <time.h>
./movement.c	#include <stdio.h>
./movement.c	#include <string.h>
./tinyusb/hw/bsp/espressif/boards/family.c	#include "esp_private/usb_phy.h"
  ./tinyusb/src/portable/st/stm32_fsdev/fsdev_stm32.h	#include "stm32f3xx.h"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants