movedocs.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. if ($ARGV[0] eq '--help') {
  5. print << "EOF";
  6. Usage:
  7. $0 file.h file.c
  8. Removes documentation attached to function declarations in file.h and adds them
  9. to function definitions found in file.c.
  10. $0 file.c
  11. Moves documentation attached to function declaration present in the same file as
  12. the definition.
  13. EOF
  14. exit 0;
  15. }
  16. my $hfile = shift @ARGV;
  17. my @cfiles = @ARGV;
  18. my %docs = ();
  19. my $F;
  20. sub write_lines {
  21. my $file = shift;
  22. my @lines = @_;
  23. my $F;
  24. open $F, '>', $file;
  25. print $F (join "", @lines);
  26. close $F;
  27. }
  28. if (@cfiles) {
  29. open $F, '<', $hfile
  30. or die "Failed to open $hfile.";
  31. my @hlines = ();
  32. my $lastdoc = '';
  33. while (<$F>) {
  34. if (/^\/\/\/?/) {
  35. $lastdoc .= $_;
  36. } elsif (/^\S.*?(\w+)\(.*(?:,|\);?|FUNC_ATTR_\w+;?)$/) {
  37. die "Documentation for $1 was already defined" if (defined $docs{$1});
  38. if ($lastdoc ne '') {
  39. $docs{$1} = $lastdoc;
  40. $lastdoc = '';
  41. }
  42. push @hlines, $_;
  43. } elsif ($lastdoc ne '') {
  44. push @hlines, $lastdoc;
  45. $lastdoc = '';
  46. push @hlines, $_;
  47. } else {
  48. push @hlines, $_;
  49. }
  50. }
  51. close $F;
  52. my %clines_hash = ();
  53. for my $cfile (@cfiles) {
  54. open $F, '<', $cfile
  55. or die "Failed to open $cfile.";
  56. my @clines = ();
  57. while (<$F>) {
  58. if (/^\S.*?(\w+)\(.*[,)]$/ and defined $docs{$1}) {
  59. push @clines, $docs{$1};
  60. delete $docs{$1};
  61. } elsif (/^(?!static\s)\S.*?(\w+)\(.*[,)]$/ and not defined $docs{$1}) {
  62. print STDERR "Documentation not defined for $1\n";
  63. }
  64. push @clines, $_;
  65. }
  66. close $F;
  67. $clines_hash{$cfile} = \@clines;
  68. }
  69. while (my ($func, $value) = each %docs) {
  70. die "Function not found: $func\n";
  71. }
  72. write_lines($hfile, @hlines);
  73. while (my ($cfile, $clines) = each %clines_hash) {
  74. write_lines($cfile, @$clines);
  75. }
  76. } else {
  77. open $F, '<', $hfile;
  78. my @lines;
  79. my $lastdoc = '';
  80. my $defstart = '';
  81. my $funcname;
  82. sub clear_lastdoc {
  83. if ($lastdoc ne '') {
  84. push @lines, $lastdoc;
  85. $lastdoc = '';
  86. }
  87. }
  88. sub record_lastdoc {
  89. my $funcname = shift;
  90. if ($lastdoc ne '') {
  91. $docs{$funcname} = $lastdoc;
  92. $lastdoc = '';
  93. }
  94. }
  95. sub add_doc {
  96. my $funcname = shift;
  97. if (defined $docs{$funcname}) {
  98. push @lines, $docs{$funcname};
  99. delete $docs{$funcname};
  100. }
  101. }
  102. sub clear_defstart {
  103. push @lines, $defstart;
  104. $defstart = '';
  105. }
  106. while (<$F>) {
  107. if (/\/\*/ .. /\*\// and not /\/\*.*?\*\//) {
  108. push @lines, $_;
  109. } elsif (/^\/\/\/?/) {
  110. $lastdoc .= $_;
  111. } elsif (/^\S.*?(\w+)\(.*(?:,|(\);?))$/) {
  112. if (not $2) {
  113. $defstart .= $_;
  114. $funcname = $1;
  115. } elsif ($2 eq ');') {
  116. record_lastdoc $1;
  117. push @lines, $_;
  118. } elsif ($2 eq ')') {
  119. clear_lastdoc;
  120. add_doc $1;
  121. push @lines, $_;
  122. }
  123. } elsif ($defstart ne '') {
  124. $defstart .= $_;
  125. if (/[{}]/) {
  126. clear_lastdoc;
  127. clear_defstart;
  128. } elsif (/\);$/) {
  129. record_lastdoc $funcname;
  130. clear_defstart;
  131. } elsif (/\)$/) {
  132. clear_lastdoc;
  133. add_doc $funcname;
  134. clear_defstart;
  135. }
  136. } else {
  137. clear_lastdoc;
  138. push @lines, $_;
  139. }
  140. }
  141. close $F;
  142. while (my ($func, $value) = each %docs) {
  143. die "Function not found: $func\n";
  144. }
  145. write_lines($hfile, @lines);
  146. }