123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- #!/usr/bin/perl
- use strict;
- use warnings;
- if ($ARGV[0] eq '--help') {
- print << "EOF";
- Usage:
- $0 file.h file.c
- Removes documentation attached to function declarations in file.h and adds them
- to function definitions found in file.c.
- $0 file.c
- Moves documentation attached to function declaration present in the same file as
- the definition.
- EOF
- exit 0;
- }
- my $hfile = shift @ARGV;
- my @cfiles = @ARGV;
- my %docs = ();
- my $F;
- sub write_lines {
- my $file = shift;
- my @lines = @_;
- my $F;
- open $F, '>', $file;
- print $F (join "", @lines);
- close $F;
- }
- if (@cfiles) {
- open $F, '<', $hfile
- or die "Failed to open $hfile.";
- my @hlines = ();
- my $lastdoc = '';
- while (<$F>) {
- if (/^\/\/\/?/) {
- $lastdoc .= $_;
- } elsif (/^\S.*?(\w+)\(.*(?:,|\);?|FUNC_ATTR_\w+;?)$/) {
- die "Documentation for $1 was already defined" if (defined $docs{$1});
- if ($lastdoc ne '') {
- $docs{$1} = $lastdoc;
- $lastdoc = '';
- }
- push @hlines, $_;
- } elsif ($lastdoc ne '') {
- push @hlines, $lastdoc;
- $lastdoc = '';
- push @hlines, $_;
- } else {
- push @hlines, $_;
- }
- }
- close $F;
- my %clines_hash = ();
- for my $cfile (@cfiles) {
- open $F, '<', $cfile
- or die "Failed to open $cfile.";
- my @clines = ();
- while (<$F>) {
- if (/^\S.*?(\w+)\(.*[,)]$/ and defined $docs{$1}) {
- push @clines, $docs{$1};
- delete $docs{$1};
- } elsif (/^(?!static\s)\S.*?(\w+)\(.*[,)]$/ and not defined $docs{$1}) {
- print STDERR "Documentation not defined for $1\n";
- }
- push @clines, $_;
- }
- close $F;
- $clines_hash{$cfile} = \@clines;
- }
- while (my ($func, $value) = each %docs) {
- die "Function not found: $func\n";
- }
- write_lines($hfile, @hlines);
- while (my ($cfile, $clines) = each %clines_hash) {
- write_lines($cfile, @$clines);
- }
- } else {
- open $F, '<', $hfile;
- my @lines;
- my $lastdoc = '';
- my $defstart = '';
- my $funcname;
- sub clear_lastdoc {
- if ($lastdoc ne '') {
- push @lines, $lastdoc;
- $lastdoc = '';
- }
- }
- sub record_lastdoc {
- my $funcname = shift;
- if ($lastdoc ne '') {
- $docs{$funcname} = $lastdoc;
- $lastdoc = '';
- }
- }
- sub add_doc {
- my $funcname = shift;
- if (defined $docs{$funcname}) {
- push @lines, $docs{$funcname};
- delete $docs{$funcname};
- }
- }
- sub clear_defstart {
- push @lines, $defstart;
- $defstart = '';
- }
- while (<$F>) {
- if (/\/\*/ .. /\*\// and not /\/\*.*?\*\//) {
- push @lines, $_;
- } elsif (/^\/\/\/?/) {
- $lastdoc .= $_;
- } elsif (/^\S.*?(\w+)\(.*(?:,|(\);?))$/) {
- if (not $2) {
- $defstart .= $_;
- $funcname = $1;
- } elsif ($2 eq ');') {
- record_lastdoc $1;
- push @lines, $_;
- } elsif ($2 eq ')') {
- clear_lastdoc;
- add_doc $1;
- push @lines, $_;
- }
- } elsif ($defstart ne '') {
- $defstart .= $_;
- if (/[{}]/) {
- clear_lastdoc;
- clear_defstart;
- } elsif (/\);$/) {
- record_lastdoc $funcname;
- clear_defstart;
- } elsif (/\)$/) {
- clear_lastdoc;
- add_doc $funcname;
- clear_defstart;
- }
- } else {
- clear_lastdoc;
- push @lines, $_;
- }
- }
- close $F;
- while (my ($func, $value) = each %docs) {
- die "Function not found: $func\n";
- }
- write_lines($hfile, @lines);
- }
|