Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; mantemp.el --- create manual template instantiations from g++ 2.7.2 output |
d20b6dfd | 2 | |
acaf905b | 3 | ;; Copyright (C) 1996, 2001-2012 Free Software Foundation, Inc. |
d20b6dfd RS |
4 | |
5 | ;; Author: Tom Houlder <thoulder@icor.fr> | |
6 | ;; Created: 10 Dec 1996 | |
7 | ;; Keywords: g++, templates | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
b1fc2b50 GM |
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. | |
d20b6dfd | 15 | |
b1fc2b50 GM |
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. | |
d20b6dfd RS |
20 | |
21 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
d20b6dfd RS |
23 | |
24 | ;;; Commentary: | |
25 | ||
26 | ;; The following is a typical error message from g++ using STL (here | |
27 | ;; with split lines): | |
28 | ;; | |
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 &)' | |
41 | ;; | |
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 | |
474a797e RS |
48 | ;; call `mantemp-make-mantemps-buffer' with the point in the buffer. |
49 | ;; You can also use `mantemp-make-mantemps-region' directly on the | |
d20b6dfd RS |
50 | ;; region if the output is already in Emacs. |
51 | ;; | |
52 | ;; The resulting buffer yields (connect the three output lines above | |
53 | ;; if you want to try): | |
54 | ;; | |
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>; | |
64 | ;; | |
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. | |
474a797e RS |
68 | ;; |
69 | ;; Sometimes, an uninstantiated template may cause a message like the | |
70 | ;; following | |
71 | ;; | |
72 | ;; main.cc:66: invalid use of undefined type | |
73 | ;; `struct valarray<double,arrayminusopclass<double,c_array<double> > >' | |
74 | ;; | |
75 | ;; Follow the same procedure as above and the line is changed to | |
76 | ;; | |
77 | ;; template struct valarray<double, | |
78 | ;; arrayminusopclass<double,c_array<double> > >; | |
d20b6dfd RS |
79 | |
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. | |
84 | ||
85 | ;;; Code: | |
86 | ||
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 "")))) | |
94 | ||
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) | |
97f67490 | 105 | (delete-region (point) (line-end-position)))) |
d20b6dfd RS |
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) | |
97f67490 | 112 | (delete-region (point) (line-end-position)))))) |
d20b6dfd RS |
113 | |
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) | |
97f67490 | 127 | (delete-region (point) (progn (forward-line 1) (point))))))) |
d20b6dfd RS |
128 | |
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) | |
474a797e RS |
151 | (if (looking-at "struct[\\t ]+\\|class[\\t ]+") |
152 | (insert "template ") | |
153 | (insert "template class ")))) | |
d20b6dfd RS |
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) | |
97f67490 | 161 | (delete-region (point) (progn (forward-word 1) (point))))))) |
d20b6dfd RS |
162 | |
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)) | |
169 | ||
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. | |
177 | ||
178 | See the commentary in file mantemp.el for an example of use." | |
179 | (interactive) | |
180 | (mantemp-make-mantemps) | |
474a797e | 181 | (message "Done")) |
d20b6dfd RS |
182 | |
183 | (defun mantemp-make-mantemps-region () | |
184 | "Make manual template instantiations from g++ error messages in the region. | |
474a797e RS |
185 | This function does the same thing as `mantemp-make-mantemps-buffer', |
186 | but operates on the region." | |
d20b6dfd RS |
187 | (interactive) |
188 | (let ((cur-buf (current-buffer)) | |
97f67490 CY |
189 | (mantemp-buffer (generate-new-buffer "*mantemp*")) |
190 | (str (buffer-substring (mark) (point)))) | |
d20b6dfd RS |
191 | ;; Copy the region to a temporary buffer, make the C++ code there |
192 | ;; and copy the result back to the current buffer. | |
d20b6dfd | 193 | (set-buffer mantemp-buffer) |
97f67490 | 194 | (insert str) |
d20b6dfd | 195 | (mantemp-make-mantemps) |
97f67490 | 196 | (setq str (buffer-string)) |
d20b6dfd | 197 | (set-buffer cur-buf) |
97f67490 | 198 | (insert str) |
d20b6dfd | 199 | (kill-buffer mantemp-buffer)) |
474a797e | 200 | (message "Done")) |
d20b6dfd RS |
201 | |
202 | (provide 'mantemp) | |
203 | ||
204 | ;;; mantemp.el ends here |