mantemp.el 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ;;; mantemp.el --- create manual template instantiations from g++ 2.7.2 output
  2. ;; Copyright (C) 1996, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Tom Houlder <thoulder@icor.fr>
  4. ;; Created: 10 Dec 1996
  5. ;; Keywords: g++, templates
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; The following is a typical error message from g++ using STL (here
  19. ;; with split lines):
  20. ;;
  21. ;; AFile.o(.text+0x2d5): undefined reference to
  22. ;; `vector<double>::begin(void)'
  23. ;; AFile.o(.text+0x2e7): undefined reference to
  24. ;; `vector<double>::insert(double *, unsigned int, double const &)'
  25. ;; AnotherFile.o(.text+0x226b8): undefined reference to
  26. ;; `operator!=(rb_tree<basic_string<char>, pair<basic_string<char>
  27. ;; const, AClass *>, select1st<pair<basic_string<char> const, AClass
  28. ;; *>, basic_string<char> >, less<basic_string<char> > >::iterator
  29. ;; const &, rb_tree<basic_string<char>, pair<basic_string<char>
  30. ;; const, AClass *>, select1st<pair<basic_string<char> const, AClass
  31. ;; *>, basic_string<char> >, less<basic_string<char> > >::iterator
  32. ;; const &)'
  33. ;;
  34. ;; The message means that in the object file AFile.o there is one
  35. ;; uninstantiated template class, vector<double>, and in AnotherFile.o
  36. ;; there is one uninstantiated template function, operator!=(...). To
  37. ;; turn this output into manual template instantiations, copy from the
  38. ;; first name of an objective file (here this is AFile.o) to right
  39. ;; after the very last `'' of the output. Put this in a buffer and
  40. ;; call `mantemp-make-mantemps-buffer' with the point in the buffer.
  41. ;; You can also use `mantemp-make-mantemps-region' directly on the
  42. ;; region if the output is already in Emacs.
  43. ;;
  44. ;; The resulting buffer yields (connect the three output lines above
  45. ;; if you want to try):
  46. ;;
  47. ;; template operator!=(rb_tree<basic_string<char>,
  48. ;; pair<basic_string<char> const, AClass *>,
  49. ;; select1st<pair<basic_string<char> const, AClass *>,
  50. ;; basic_string<char> >, less<basic_string<char> > >::iterator const
  51. ;; &, rb_tree<basic_string<char>, pair<basic_string<char> const,
  52. ;; AClass *>, select1st<pair<basic_string<char> const, AClass *>,
  53. ;; basic_string<char> >, less<basic_string<char> > >::iterator const
  54. ;; &);
  55. ;; template class vector<double>;
  56. ;;
  57. ;; which can be included in your C++ program. However, its probably
  58. ;; better to include the necessary header files in the buffer and
  59. ;; compile it as a stand alone implementation file.
  60. ;;
  61. ;; Sometimes, an uninstantiated template may cause a message like the
  62. ;; following
  63. ;;
  64. ;; main.cc:66: invalid use of undefined type
  65. ;; `struct valarray<double,arrayminusopclass<double,c_array<double> > >'
  66. ;;
  67. ;; Follow the same procedure as above and the line is changed to
  68. ;;
  69. ;; template struct valarray<double,
  70. ;; arrayminusopclass<double,c_array<double> > >;
  71. ;; g++ does not output the templates that are needed by the
  72. ;; uninstantiated templates. Therefore you will often get new error
  73. ;; messages after the first instantiations have been included and you
  74. ;; must repeat the operation.
  75. ;;; Code:
  76. (defun mantemp-remove-comments ()
  77. "Remove g++ comments surrounding each function and member function."
  78. (save-excursion
  79. (goto-char (point-min))
  80. (message "Removing comments")
  81. (while (re-search-forward "^[A-z\.()+0-9: ]*`\\|'.*$" nil t)
  82. (replace-match ""))))
  83. (defun mantemp-remove-memfuncs ()
  84. "Remove member function extensions so that only class names remain."
  85. (save-excursion
  86. ;; Remove conversion operator extensions.
  87. (goto-char (point-min))
  88. (message "Removing member function extensions")
  89. (while (re-search-forward
  90. "^[A-z :&*<>~=,0-9+]*>::operator " nil t nil)
  91. (progn
  92. (backward-char 11)
  93. (delete-region (point) (line-end-position))))
  94. ;; Remove other member function extensions.
  95. (goto-char (point-min))
  96. (message "Removing member function extensions")
  97. (while (re-search-forward "^[A-z :&*<>~=,0-9+]*>::" nil t nil)
  98. (progn
  99. (backward-char 2)
  100. (delete-region (point) (line-end-position))))))
  101. (defun mantemp-sort-and-unique-lines ()
  102. "Eliminate all consecutive duplicate lines in the buffer."
  103. (save-excursion
  104. (message "Sorting")
  105. (sort-regexp-fields nil "^.*$" "\\&"
  106. (point-min)
  107. (point-max))
  108. (goto-char (point-min))
  109. (message "Removing consecutive duplicate lines")
  110. (while (re-search-forward "\\(^.+\\)\n\\1" nil t nil)
  111. (progn
  112. (forward-line -1)
  113. (beginning-of-line)
  114. (delete-region (point) (progn (forward-line 1) (point)))))))
  115. (defun mantemp-insert-cxx-syntax ()
  116. "Insert C++ syntax around each template class and function.
  117. Insert 'template class' for classes, 'template' for
  118. functions and add the statement delimiter `;' at the end of
  119. the lines."
  120. (save-excursion
  121. ;; Insert ';' at the end of each nonempty line.
  122. (goto-char (point-min))
  123. (message "Inserting `;' at the ends")
  124. (while (re-search-forward ".+$" nil t)
  125. (progn
  126. (insert ";")
  127. (if (not (equal (point) (point-max)))
  128. (forward-char))))
  129. ;; We first insert 'template class' at each nonempty line and
  130. ;; subsequently remove 'class' for functions so we don't need to
  131. ;; both scan for classes and functions.
  132. (goto-char (point-min))
  133. (message "Inserting 'template class' for classes")
  134. (while (re-search-forward "^.+" nil t)
  135. (progn
  136. (beginning-of-line)
  137. (if (looking-at "struct[\\t ]+\\|class[\\t ]+")
  138. (insert "template ")
  139. (insert "template class "))))
  140. (goto-char (point-min))
  141. (message "Inserting 'template' for functions")
  142. (while (re-search-forward
  143. "^template class [A-z :&*<>~=,0-9+!]*(" nil t nil)
  144. (progn
  145. (beginning-of-line)
  146. (forward-word 1)
  147. (delete-region (point) (progn (forward-word 1) (point)))))))
  148. (defun mantemp-make-mantemps ()
  149. "Gathering interface to the functions modifying the buffer."
  150. (mantemp-remove-comments)
  151. (mantemp-remove-memfuncs)
  152. (mantemp-sort-and-unique-lines)
  153. (mantemp-insert-cxx-syntax))
  154. (defun mantemp-make-mantemps-buffer ()
  155. "Make manual template instantiations from g++ error messages in the buffer.
  156. Scan the output of g++ describing uninstantiated template
  157. classes and functions and generate the corresponding C++
  158. manual template instantiations. The output is supposed to
  159. have been placed in the current buffer and the current buffer
  160. should otherwise be empty.
  161. See the commentary in file mantemp.el for an example of use."
  162. (interactive)
  163. (mantemp-make-mantemps)
  164. (message "Done"))
  165. (defun mantemp-make-mantemps-region ()
  166. "Make manual template instantiations from g++ error messages in the region.
  167. This function does the same thing as `mantemp-make-mantemps-buffer',
  168. but operates on the region."
  169. (interactive)
  170. (let ((cur-buf (current-buffer))
  171. (mantemp-buffer (generate-new-buffer "*mantemp*"))
  172. (str (buffer-substring (mark) (point))))
  173. ;; Copy the region to a temporary buffer, make the C++ code there
  174. ;; and copy the result back to the current buffer.
  175. (set-buffer mantemp-buffer)
  176. (insert str)
  177. (mantemp-make-mantemps)
  178. (setq str (buffer-string))
  179. (set-buffer cur-buf)
  180. (insert str)
  181. (kill-buffer mantemp-buffer))
  182. (message "Done"))
  183. (provide 'mantemp)
  184. ;;; mantemp.el ends here