#!/usr/bin/perl # Print unused macros in .c files # Copyright (C) 2013 Alexander Cherepanov # # Redistribution and use in source and binary forms, with or without # modification, are permitted. # Usage: # unused-macros *.c use strict; use warnings; # Ignore some macros from https://gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html my %ignore; for (qw(_POSIX_SOURCE _BSD_SOURCE _XOPEN_SOURCE _XOPEN_SOURCE_EXTENDED _LARGEFILE64_SOURCE _GNU_SOURCE)) { $ignore{$_} = 1; } for my $file (@ARGV) { my %def; open IN, '<', $file; my $lineno; while () { $lineno++; # ignore also john specific macros NEED_OS_* if (s/^#\s*define\s+(\w+)// && !$ignore{$1} && (my $def = $1) !~ /^NEED_OS_/) { $def{$def} = $lineno; } if (/^#\s*include/) { # forget everything (in case it's used in included file) undef %def; } while (/\w+/g) { delete $def{$&}; } } close IN; # To sort by line numbers we need to reverse out hash my %rev_def = reverse %def; for my $lineno (sort { $a <=> $b } keys %rev_def) { # numeric sort my $macro = $rev_def{$lineno}; print "$file:$def{$macro}: warning: unused macro '$macro'\n"; } }