File: fixheader.pl

package info (click to toggle)
cxref 1.6e-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,592 kB
  • ctags: 2,123
  • sloc: ansic: 16,579; yacc: 2,091; sh: 917; lex: 470; perl: 452; makefile: 425; lisp: 256; cpp: 188; python: 80
file content (400 lines) | stat: -rwxr-xr-x 8,298 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/bin/sh
#
# C Cross Referencing & Documentation tool. Version 1.5.
#
# A Perl script to determine the required headers for source files.
#
# Written by Andrew M. Bishop
#
# This file Copyright 1999 Andrew M. Bishop
# It may be distributed under the GNU Public License, version 2, or
# any higher version.  See section COPYING of the GNU Public license
# for conditions under which this file may be redistributed.
#

exec perl -x $0 $*

exit 1

#!perl

# The verbose flag
$verbose=0;

# The output flag
$output=0;

# The compiler program.
$cc="gcc";
$cflags="-Wall -c";

# The cxref options.
$cxref_root=".";
$cxref_name="cxref";
$cxref_output=".";

# The C pre-processor arguments (-D, -I).
$cpp_args="";

# The files to check.
@files=();

#
# Parse the command line arguments
#

if( $#ARGV==-1 )
  {
   print "Usage: fixheader filename [filename ...] [-v] [-o]\n";
   print "                 [-Odirname] [-Nbasename] [-Rdirname]\n";
   print "                 [-Ddefine] [-Udefine] [-Iinclude]\n";
   print "\n";
   print "-v    Output verbose information during the processing.\n";
   print "-o    Output a modified source file after testing.\n";
   exit 0;
  }

while ( $#ARGV >= 0 )
  {
   $_=$ARGV[0];
  switch:
     {
      $_ eq '-v'    && do { $verbose=1; last switch;};
      $_ eq '-o'    && do { $output=1; last switch;};
      m/^-R/        && die "The -R option is not implemented\n";
      m/^-N/        && do { if($_ eq "-N") {shift; $cxref_name=$ARGV[0];} else {$cxref_name=substr($_,2);} last switch;};
      m/^-O/        && do { if($_ eq "-O") {shift; $cxref_output=$ARGV[0];} else {$cxref_output=substr($_,2);} last switch;};
      -f $_         && do { push(@files,$_); last switch;};

      $cpp_args.=" '".$_."'";
     }
   shift;
  }

#
# The main program
#

foreach $file (@files)
  {
   # Initialise the headers

   @cur_headers=&GetFileHeaders($file);

   @all_headers=@cur_headers;

   %use_headers=();

   if($file =~ m/\.h$/)
       {
        @cxref_headers=&GetCxrefHeaders();

        foreach $h (@cxref_headers)
            {
             next if($file eq substr($h,1,length($h)-2));
             foreach $hh (@all_headers)
                 {
                  $h="" if($hh eq $h);
                 }

             @all_headers=(@all_headers,$h) if($h =~ m/^\"/);
             @all_headers=($h,@all_headers) if($h =~ m/^\</);
            }
       }

   foreach $header (@all_headers)
       {
        $use_headers{$header}=3;
       }

   foreach $header (@cur_headers)
       {
        $use_headers{$header}=1;
       }

   # Test the file as it is now

   print "\nTesting file $file\n" if($verbose);

   print "\nWe need to know how well it works without changing it\n" if($verbose);
   $current=&TryTheseHeaders($file,"unchanged file",());

   if($current>=0)
       {
        @all_headers=@cur_headers;
       }

   # Try all of the headers

   if($file =~ m/\.h$/)
       {
        print "\nThis is a header file so we should try all possible headers\n" if($verbose);
        $all=&TryTheseHeaders($file,"all headers",@all_headers);
       }
   else
       {
        $all=$current;
       }

   # Try removing headers

   print "\nNow we try to remove unneeded headers\n" if($verbose);

  tryagain:

   ($remove,@remain_headers)=&TryRemovingHeaders($file,$all,@all_headers);

   if(($all<0 && $all!=$remove) || $remove<$all)
       {
        $all=$remove;
        @all_headers=@remain_headers;
        print "\nWe need to try again now we have removed some problem headers\n" if($verbose);
        goto tryagain;
       }

   # Print out a summary

   print "\nSummary for $file\n\n";

   $added="";
   foreach $header (@all_headers)
       {
        print "    Removed: \#include $header\n" if($use_headers{$header}==0);
        print "    Kept   : \#include $header\n" if($use_headers{$header}==1);
        print "    Added  : \#include $header\n" if($use_headers{$header}==3);
        $added.=" $header" if($use_headers{$header}==3);
       }

   &FixupFile($file,$added) if($output);

   print "\n";
  }


#
# Get the included headers from an existing file.
#

sub GetFileHeaders
{
 local($file)=@_;
 local(@headers)=();

 # Parse the file

 open(IN,"<$file") || die "Cannot open the source file '$file'\n";

 while(<IN>)
     {
      push(@headers,$1) if(m/^[ \t]*\#include ([<\"][^>\"]+[>\"])/);
     }

 close(IN);

 return(@headers);
}


#
# Get the headers from the cxref database
#

sub GetCxrefHeaders
{
 local(@headers)=();
 local(%headers)=();

 open(INC,"<$cxref_output/$cxref_name.include") || die "Cannot open the cxref database file '$cxref_output/$cxref_name.include'\n";

 while(<INC>)
     {
      chop;
      local($file,@heads)=split(" +");

      foreach $h (@heads)
          {
           next if($headers{$h});
           $headers{$h}=1;

           if($h =~ m/^%/)
               {
                push(@headers,"\"".substr($h,1)."\"");
               }
           else
               {
                push(@headers,"<".$h.">");
               }
          }
     }

 close(INC);

 return(@headers);
}


#
# Try removing them one at a time.
#

sub TryRemovingHeaders
{
 local($file,$curr,@headers)=@_;
 local($best)=$curr;
 local(@best_headers)=();

 foreach $header (reverse(@headers))
     {
      next if($use_headers{$header}==0 || $use_headers{$header}==2);

      @try_headers=();

      foreach $h (@headers)
          {
           next if($use_headers{$h}==0 || $use_headers{$h}==2);
           push(@try_headers,$h) if($h ne $header);
          }

      $use_head=$use_headers{$header};
      $use_headers{$header}=-1;
      $try=&TryTheseHeaders($file,"to remove $header",@try_headers);
      $use_headers{$header}=$use_head;

      if(($curr>=0 && $try>=0 && $try<=$curr) || ($curr<0 && $try>=0))
          {
           $use_headers{$header}--;
          }

      if(($best>=0 && $try>=0 && $try<=$best) || ($best<0 && $try>$best))
          {
           $best=$try;
           @best_headers=@try_headers;
          }
     }

 return($best,@best_headers);
}


#
# Try the specified headers
#

sub TryTheseHeaders
{
 local($file,$what,@these)=@_;

 print "  Trying $what .." if($verbose);
 $result=&TryCompile($file,@these);
 print " ($result)" if($verbose);

 if($result>=0)
     {
      print " OK\n" if($verbose);
     }
 else
     {
      print " Failed\n" if($verbose);
     }

 return $result;
}


#
# Try compiling the file and see if it works.
#

sub TryCompile
{
 local($file,@headers)=@_;
 $length=0;

# Modify the file

 $tmpfile=$file;
 $tmpfile =~ s/\.([ch])/.cxref.$1.c/;

 die "Cannot create temporary filename from name '$file'\n" if($file eq $tmpfile);

 open(IN,"<$file") || die "Cannot open the source file '$file'\n";
 open(OUT,">$tmpfile") || die "Cannot create the temporary file '$tmpfile'\n";

 foreach $header (@headers)
     {
      print OUT "#include $header\n" if($use_headers{$header}==3);
     }

 while(<IN>)
     {
      next if(m/^[ \t]*\#include ([<\"][^>\"]+[>\"])/ && $use_headers{$1}!=1);
      print OUT;
     }

 close(IN);
 close(OUT);

# Test the compilation

 $result=system "$cc $cflags $tmpfile -o /dev/null $cpp_args > $file.cxref-result 2>&1";

 chop($length=`wc -l $file.cxref-result | awk '{print \$1}'`);

 unlink "$tmpfile";
 unlink "$file.cxref-result";

 $result=($result & 0xffff);

 if($result)
     {$length=-1-$length;}

 return($length);
}


#
# Fixup the headers in the file.
#

sub FixupFile
{
 local($file,$added)=@_;

# Modify the file

 $newfile=$file;
 $newfile =~ s/\.([ch])/.cxref.$1/;

 die "Cannot create temporary filename from name '$file'\n" if($file eq $newfile);

 open(IN,"<$file") || die "Cannot open the source file '$file'\n";
 open(OUT,">$newfile") || die "Cannot create the output file '$newfile'\n";

 if($added)
     {
      (@added)=split(" ",$added);

      foreach $h (@added)
          {
           printf OUT "#include %-30s /* Added by cxref */\n",$h;
          }

      print OUT "\n";
     }

 while(<IN>)
     {
      if(m/^[ \t]*\#include ([<\"][^>\"]+[>\"])(.*)$/)
          {
           printf OUT "/* #include %-30s* Removed by cxref */ %s\n",$1,$2 if($use_headers{$1}==0);
           print OUT $_ if($use_headers{$1}==1);
          }
      else
          {
           print OUT;
          }
     }

 close(IN);
 close(OUT);
}