Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / bin / show_unused_macros.pl
blobe5f04ec13522beb255e05d12f7778cf89d892a92
1 eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
2 & eval 'exec perl -w -S $0 $argv:q'
3 if 0;
5 # ************************************************************
6 # Description : Find macros in specified config files that
7 # are not referenced in other config files,
8 # but are referenced in the rest of the source
9 # files.
10 # Author : Chad Elliott
11 # Create Date : 12/22/2004
12 # ************************************************************
14 # ************************************************************
15 # Pragmas
16 # ************************************************************
18 use strict;
19 use Cwd;
20 use FileHandle;
21 use File::Basename;
23 # ************************************************************
24 # Data Section
25 # ************************************************************
27 my($in_comment) = undef;
29 # ************************************************************
30 # Subroutine Section
31 # ************************************************************
33 sub getline {
34 my($fh) = shift;
35 my($line) = $fh->getline();
37 if (defined $line) {
38 ## Remove the line feed
39 $line =~ s/\n//;
41 ## Remove one line c comments
42 $line =~ s/\/\*.*\*\///;
44 ## Check for multi lined c comments
45 if ($line =~ s/\/\*.*//) {
46 $in_comment = 1;
48 elsif ($line =~ s/.*\*\///) {
49 $in_comment = 0;
51 elsif ($in_comment) {
52 $line = '';
54 else {
55 ## Remove c++ comments
56 $line =~ s/\/\/.*//;
58 ## Concatenate lines
59 if ($line =~ s/\\\s*$//) {
60 $line .= getline($fh);
65 return $line;
69 sub findMacros {
70 my($defines) = shift;
71 my($macros) = shift;
72 my(@files) = @_;
73 foreach my $file (@files) {
74 my($fh) = new FileHandle();
76 if (open($fh, $file)) {
77 $in_comment = undef;
78 while(defined($_ = getline($fh))) {
79 if (($defines & 1) == 1 && /^\s*#\s*define\s*([^\s]+)/) {
80 my($word) = $1;
81 $word =~ s/\(.*//;
82 if (!defined $$macros{$word}) {
83 $$macros{$word} = $file;
86 elsif (($defines & 2) == 2 && /^\s*#\s*if/) {
87 foreach my $word (split(/[^\w]/, $_)) {
88 if ($word =~ /^[^\d]\w+$/ &&
89 $word !~ /^if([n]?def)?$/ &&
90 $word !~ /^define[d]?/ &&
91 $word !~ /^els(e|if)$/ && !defined $$macros{$word}) {
92 $$macros{$word} = $file;
98 close($fh);
100 else {
101 print STDERR "Unable to open $file\n";
102 exit(2);
108 sub usageAndExit {
109 my($msg) = shift;
111 if (defined $msg) {
112 print STDERR "ERROR: $msg\n";
115 print STDERR 'Usage: ', basename($0), " [-I <directory>] <config headers>\n\n",
116 "This script will provide a set of macros that may possibly\n",
117 "be removed from ACE.\n\n",
118 "It should be run under ACE_wrappers/ace and the input should\n",
119 "be the config header file or files planned for removal.\n";
120 exit(1);
124 # ************************************************************
125 # Main Section
126 # ************************************************************
128 my(@files) = ();
129 my(@dirs) = ('.', 'os_include', 'os_include/sys',
130 'os_include/netinet', 'os_include/net',
131 'os_include/arpa',
134 for(my $i = 0; $i <= $#ARGV; ++$i) {
135 my($arg) = $ARGV[$i];
136 if ($arg =~ /^-/) {
137 if ($arg eq '-h') {
138 usageAndExit();
140 elsif ($arg eq '-I') {
141 ++$i;
142 if (defined $ARGV[$i]) {
143 push(@dirs, $ARGV[$i]);
145 else {
146 usageAndExit('-I requires a directory parameter');
149 else {
150 usageAndExit("$arg is an unknown option");
153 else {
154 push(@files, $arg);
158 if (!defined $files[0]) {
159 usageAndExit();
162 ## First find all of the control macros
163 my(%control) = ();
164 findMacros(3, \%control, @files);
166 ## Now find all of the macros from the other config files
167 my(@other) = grep(!/config-all\.h|config-lite\.h/, <config-*.h>);
169 for(my $i = 0; $i <= $#other; ++$i) {
170 foreach my $file (@files) {
171 if ($other[$i] eq $file) {
172 splice(@other, $i, 1);
173 --$i;
174 last;
178 my(%other) = ();
179 findMacros(3, \%other, @other);
182 my(%notreferenced) = ();
183 foreach my $macro (keys %control) {
184 if (!defined $other{$macro}) {
185 $notreferenced{$macro} = $control{$macro};
190 ## Find all other macros
191 my(@all) = ();
192 foreach my $dir (@dirs) {
193 my($orig) = getcwd();
194 if (chdir($dir)) {
195 my(@more) = <*.h *.i* *.cpp>;
196 if ($dir ne '.') {
197 foreach my $file (@more) {
198 $file = "$dir/$file";
201 push(@all, @more);
202 chdir($orig);
206 for(my $i = 0; $i <= $#all; ++$i) {
207 foreach my $file (@files, @other) {
208 if ($all[$i] eq $file) {
209 splice(@all, $i, 1);
210 --$i;
211 last;
216 my(%all) = ();
217 findMacros(2, \%all, @all);
219 foreach my $macro (sort keys %notreferenced) {
220 if (defined $all{$macro}) {
221 print "$macro\n";