kerneldoc2doxygen.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/perl -w
  2. #
  3. ##########################################################################
  4. # Convert kernel-doc style comments to Doxygen comments.
  5. ##########################################################################
  6. #
  7. # This script reads a C source file from stdin, and writes
  8. # to stdout. Normal usage:
  9. #
  10. # $ mv file.c file.c.gtkdoc
  11. # $ kerneldoc2doxygen.pl <file.c.gtkdoc >file.c
  12. #
  13. # Or to do the same thing with multiple files:
  14. # $ perl -i.gtkdoc kerneldoc2doxygen.pl *.c *.h
  15. #
  16. # This script may also be suitable for use as a Doxygen input filter,
  17. # but that has not been tested.
  18. #
  19. # Back up your source files before using this script!!
  20. #
  21. ##########################################################################
  22. # Copyright (C) 2003 Jonathan Foster <jon@jon-foster.co.uk>
  23. # Copyright (C) 2005 Jouni Malinen <j@w1.fi>
  24. # (modified for kerneldoc format used in wpa_supplicant)
  25. #
  26. # This program is free software; you can redistribute it and/or modify
  27. # it under the terms of the GNU General Public License version 2 as
  28. # published by the Free Software Foundation.
  29. #
  30. # This program is distributed in the hope that it will be useful,
  31. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. # GNU General Public License for more details.
  34. #
  35. # You should have received a copy of the GNU General Public License
  36. # along with this program; if not, write to the Free Software
  37. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  38. # or look at http://www.gnu.org/licenses/gpl.html
  39. ##########################################################################
  40. ##########################################################################
  41. #
  42. # This function converts a single comment from gtk-doc to Doxygen format.
  43. # The parameter does not include the opening or closing lines
  44. # (i.e. given a comment like this:
  45. # "/**\n"
  46. # " * FunctionName:\n"
  47. # " * @foo: This describes the foo parameter\n"
  48. # " * @bar: This describes the bar parameter\n"
  49. # " * @Returns: This describes the return value\n"
  50. # " *\n"
  51. # " * This describes the function.\n"
  52. # " */\n"
  53. # This function gets:
  54. # " * FunctionName:\n"
  55. # " * @foo: This describes the foo parameter\n"
  56. # " * @bar: This describes the bar parameter\n"
  57. # " * @Returns: This describes the return value\n"
  58. # " *\n"
  59. # " * This describes the function.\n"
  60. # And it returns:
  61. # " * This describes the function.\n"
  62. # " *\n"
  63. # " * @param foo This describes the foo parameter\n"
  64. # " * @param bar This describes the bar parameter\n"
  65. # " * @return This describes the return value\n"
  66. # )
  67. #
  68. sub fixcomment {
  69. $t = $_[0];
  70. # " * func: foo" --> "\brief foo\n"
  71. # " * struct bar: foo" --> "\brief foo\n"
  72. # If this fails, not a kernel-doc comment ==> return unmodified.
  73. ($t =~ s/^[\t ]*\*[\t ]*(struct )?([^ \t\n]*) - ([^\n]*)/\\brief $3\n/s)
  74. or return $t;
  75. # " * Returns: foo" --> "\return foo"
  76. $t =~ s/\n[\t ]*\*[\t ]*Returns:/\n\\return/sig;
  77. # " * @foo: bar" --> "\param foo bar"
  78. # Handle two common typos: No ":", or "," instead of ":".
  79. $t =~ s/\n[\t ]*\*[\t ]*\@([^ :,]*)[:,]?[\t ]*/\n\\param $1 /sg;
  80. return $t;
  81. }
  82. ##########################################################################
  83. # Start of main code
  84. # Read entire stdin into memory - one multi-line string.
  85. $_ = do { local $/; <> };
  86. s{^/\*\n \*}{/\*\* \\file\n\\brief};
  87. s{ \* Copyright}{\\par Copyright\nCopyright};
  88. # Fix any comments like "/*************" so they don't match.
  89. # "/***" ===> "/* *"
  90. s{/\*\*\*}{/\* \*}gs;
  91. # The main comment-detection code.
  92. s{
  93. ( # $1 = Open comment
  94. /\*\* # Open comment
  95. (?!\*) # Do not match /*** (redundant due to fixup above).
  96. [\t ]*\n? # If 1st line is whitespace, match the lot (including the newline).
  97. )
  98. (.*?) # $2 = Body of comment (multi-line)
  99. ( # $3 = Close comment
  100. ( # If possible, match the whitespace before the close-comment
  101. (?<=\n) # This part only matches after a newline
  102. [\t ]* # Eat whitespace
  103. )?
  104. \*/ # Close comment
  105. )
  106. }
  107. {
  108. $1 . fixcomment($2) . $3
  109. }gesx;
  110. # ^^^^ Modes: g - Global, match all occurances.
  111. # e - Evaluate the replacement as an expression.
  112. # s - Single-line - allows the pattern to match across newlines.
  113. # x - eXtended pattern, ignore embedded whitespace
  114. # and allow comments.
  115. # Write results to stdout
  116. print $_;