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