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