Mercurial > projects > qtd
annotate generator/parser/rpp/pp-main.cpp @ 414:b2a803c73b89 default tip
Declare tabArray const.
author | David Nadlinger <code@klickverbot.at> |
---|---|
date | Fri, 06 May 2011 13:39:49 +0200 |
parents | 09a0f1d048f2 |
children |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
52
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
3 ** Copyright (C) 1992-2009 Nokia. All rights reserved. |
1 | 4 ** Copyright 2005 Roberto Raggi <roberto@kdevelop.org> |
5 ** | |
6 ** This file is part of Qt Jambi. | |
7 ** | |
52
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
8 ** Commercial Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
9 Licensees holding valid Qt Commercial licenses may use this file in |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
10 accordance with the Qt Commercial License Agreement provided with the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
11 Software or, alternatively, in accordance with the terms contained in |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
12 a written agreement between you and Nokia. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
13 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
14 GNU Lesser General Public License Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
15 Alternatively, this file may be used under the terms of the GNU Lesser |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
16 General Public License version 2.1 as published by the Free Software |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
17 Foundation and appearing in the file LICENSE.LGPL included in the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
18 packaging of this file. Please review the following information to |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
19 ensure the GNU Lesser General Public License version 2.1 requirements |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
20 will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
21 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
22 In addition, as a special exception, Nokia gives you certain |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
23 additional rights. These rights are described in the Nokia Qt LGPL |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
24 Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
25 package. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
26 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
27 GNU General Public License Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
28 Alternatively, this file may be used under the terms of the GNU |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
29 General Public License version 3.0 as published by the Free Software |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
30 Foundation and appearing in the file LICENSE.GPL included in the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
31 packaging of this file. Please review the following information to |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
32 ensure the GNU General Public License version 3.0 requirements will be |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
33 met: http://www.gnu.org/copyleft/gpl.html. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
34 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
35 If you are unsure which license is appropriate for your use, please |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
36 contact the sales department at qt-sales@nokia.com. |
1 | 37 |
38 ** | |
39 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | |
40 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
41 ** | |
42 ****************************************************************************/ | |
43 | |
44 #include <fstream> | |
45 #include "pp.h" | |
46 | |
47 using namespace rpp; | |
48 | |
49 #ifndef GCC_MACHINE | |
50 # define GCC_MACHINE "i386-redhat-linux" | |
51 #endif | |
52 | |
53 #ifndef GCC_VERSION | |
54 # define GCC_VERSION "4.1.1" | |
55 #endif | |
56 | |
57 void usage () | |
58 { | |
59 std::cerr << "usage: rpp file.cpp" << std::endl; | |
60 ::exit (EXIT_FAILURE); | |
61 } | |
62 | |
63 void dump_macros (pp_environment &env, pp &, std::ostream &__out) | |
64 { | |
65 for (pp_environment::const_iterator it = env.first_macro (); it != env.last_macro (); ++it) | |
66 { | |
67 pp_macro const *m = *it; | |
68 | |
69 if (m->hidden) | |
70 continue; | |
71 | |
72 std::string id (m->name->begin (), m->name->end ()); | |
73 __out << "#define " << id; | |
74 | |
75 if (m->function_like) | |
76 { | |
77 __out << "("; | |
78 | |
79 for (std::size_t i = 0; i < m->formals.size (); ++i) | |
80 { | |
81 if (i != 0) | |
82 __out << ", "; | |
83 | |
84 pp_fast_string const *f = m->formals [i]; | |
85 std::string name (f->begin (), f->end ()); | |
86 __out << name; | |
87 } | |
88 | |
89 if (m->variadics) | |
90 __out << "..."; | |
91 | |
92 __out << ")"; | |
93 } | |
94 | |
95 __out << "\t"; | |
96 if (m->definition) | |
97 { | |
98 std::string def (m->definition->begin (), m->definition->end ()); | |
99 __out << def; | |
100 } | |
101 | |
102 __out << std::endl; | |
103 } | |
104 } | |
105 | |
106 int main (int, char *argv []) | |
107 { | |
108 char const *input_file = 0; | |
109 char const *output_file = 0; | |
110 char const *include_pch_file = 0; | |
111 bool opt_help = false; | |
112 bool opt_dump_macros = false; | |
113 bool opt_pch = false; | |
114 | |
115 pp_environment env; | |
116 pp preprocess(env); | |
117 | |
118 std::string result; | |
119 result.reserve (20 * 1024); // 20K | |
120 | |
121 pp_output_iterator<std::string> out (result); | |
122 pp_null_output_iterator null_out; | |
123 | |
124 preprocess.push_include_path ("/usr/include"); | |
125 preprocess.push_include_path ("/usr/lib/gcc/" GCC_MACHINE "/" GCC_VERSION "/include"); | |
126 | |
127 preprocess.push_include_path ("/usr/include/c++/" GCC_VERSION); | |
128 preprocess.push_include_path ("/usr/include/c++/" GCC_VERSION "/" GCC_MACHINE); | |
129 | |
130 std::string extra_args; | |
131 | |
132 while (const char *arg = *++argv) | |
133 { | |
134 if (arg [0] != '-') | |
135 input_file = arg; | |
136 | |
137 else if (! strcmp (arg, "-help")) | |
138 opt_help = true; | |
139 | |
140 else if (! strcmp (arg, "-dM")) | |
141 opt_dump_macros = true; | |
142 | |
143 else if (! strcmp (arg, "-pch")) | |
144 opt_pch = true; | |
145 | |
146 else if (! strcmp (arg, "-msse")) | |
147 { | |
148 pp_macro __macro; | |
149 __macro.name = pp_symbol::get ("__SSE__", 7); | |
150 env.bind (__macro.name, __macro); | |
151 | |
152 __macro.name = pp_symbol::get ("__MMX__", 7); | |
153 env.bind (__macro.name, __macro); | |
154 } | |
155 | |
156 else if (! strcmp (arg, "-include")) | |
157 { | |
158 if (argv [1]) | |
159 include_pch_file = *++argv; | |
160 } | |
161 | |
162 else if (! strncmp (arg, "-o", 2)) | |
163 { | |
164 arg += 2; | |
165 | |
166 if (! arg [0] && argv [1]) | |
167 arg = *++argv; | |
168 | |
169 if (arg) | |
170 output_file = arg; | |
171 } | |
172 | |
173 else if (! strncmp (arg, "-conf", 8)) | |
174 { | |
175 if (argv [1]) | |
176 preprocess.file (*++argv, null_out); | |
177 } | |
178 | |
179 else if (! strncmp (arg, "-I", 2)) | |
180 { | |
181 arg += 2; | |
182 | |
183 if (! arg [0] && argv [1]) | |
184 arg = *++argv; | |
185 | |
186 if (arg) | |
187 preprocess.push_include_path (arg); | |
188 } | |
189 | |
190 else if (! strncmp (arg, "-U", 2)) | |
191 { | |
192 arg += 2; | |
193 | |
194 if (! arg [0] && argv [1]) | |
195 arg = *++argv; | |
196 | |
197 if (arg) | |
198 { | |
199 env.unbind (arg, strlen (arg)); | |
200 } | |
201 } | |
202 | |
203 else if (! strncmp (arg, "-D", 2)) | |
204 { | |
205 arg += 2; | |
206 | |
207 if (! arg [0] && argv [1]) | |
208 arg = *++argv; | |
209 | |
210 if (arg) | |
211 { | |
212 pp_macro __macro; | |
213 | |
214 char const *end = arg; | |
215 char const *eq = 0; | |
216 | |
217 for (; *end; ++end) | |
218 { | |
219 if (*end == '=') | |
220 eq = end; | |
221 } | |
222 | |
223 if (eq != 0) | |
224 { | |
225 __macro.name = pp_symbol::get (arg, eq - arg); | |
226 __macro.definition = pp_symbol::get (eq + 1, end - (eq + 1)); | |
227 } | |
228 | |
229 else | |
230 { | |
231 __macro.name = pp_symbol::get (arg, end - arg); | |
232 __macro.definition = 0; | |
233 } | |
234 | |
235 env.bind (__macro.name, __macro); | |
236 } | |
237 } | |
238 else | |
239 { | |
240 extra_args += " "; | |
241 extra_args += arg; | |
242 } | |
243 } | |
244 | |
245 if (! input_file || opt_help) | |
246 { | |
247 usage (); | |
248 return EXIT_FAILURE; | |
249 } | |
250 | |
251 std::string __ifile (input_file); | |
252 bool is_c_file = false; | |
253 if (__ifile.size () > 2 && __ifile [__ifile.size () - 1] == 'c' && __ifile [__ifile.size () - 2] == '.') | |
254 { | |
255 is_c_file = true; | |
256 env.unbind ("__cplusplus", 11); | |
257 | |
258 pp_macro __macro; | |
259 __macro.name = pp_symbol::get ("__null"); | |
260 __macro.definition = pp_symbol::get ("((void*) 0)"); | |
261 env.bind (__macro.name, __macro); | |
262 | |
263 // turn off the pch | |
264 include_pch_file = 0; | |
265 } | |
266 else if (include_pch_file) | |
267 { | |
268 std::string __pch (include_pch_file); | |
269 __pch += ".gch/c++.conf"; | |
270 | |
271 //std::cerr << "*** pch file " << __pch << std::endl; | |
272 preprocess.file (__pch, null_out); | |
273 } | |
274 | |
275 if (opt_dump_macros) | |
276 { | |
277 preprocess.file (input_file, null_out); | |
278 dump_macros (env, preprocess, std::cout); | |
279 return EXIT_SUCCESS; | |
280 } | |
281 | |
282 preprocess.file (input_file, out); | |
283 | |
284 if (opt_pch) | |
285 { | |
286 if (! output_file) | |
287 { | |
288 std::cerr << "*** WARNING expected a file name" << std::endl; | |
289 return EXIT_FAILURE; | |
290 } | |
291 | |
292 std::string __conf_file (output_file); | |
293 __conf_file += ".conf"; | |
294 | |
295 std::ofstream __out; | |
296 __out.open (__conf_file.c_str ()); | |
297 dump_macros (env, preprocess, __out); | |
298 __out.close (); | |
299 | |
300 std::string __pp_file (output_file); | |
301 __pp_file += ".i"; | |
302 | |
303 __out.open (__pp_file.c_str ()); | |
304 __out.write (result.c_str (), result.size ()); | |
305 __out.close (); | |
306 return EXIT_SUCCESS; | |
307 } | |
308 | |
309 std::ostream *__out = &std::cout; | |
310 std::ofstream __ofile; | |
311 | |
312 if (output_file) | |
313 { | |
314 std::string __output_file_name (output_file); | |
315 __ofile.open (output_file); | |
316 __out = &__ofile; | |
317 } | |
318 | |
319 if (include_pch_file) | |
320 { | |
321 std::string __pch (include_pch_file); | |
322 __pch += ".gch/c++.i"; | |
323 | |
324 std::ifstream __in (__pch.c_str ()); | |
325 | |
326 char buffer [1024]; | |
327 while (__in.read (buffer, 1024)) | |
328 __out->write (buffer, 1024); | |
329 | |
330 __in.close (); | |
331 } | |
332 | |
333 __out->write (result.c_str (), result.size ()); | |
334 | |
335 if (output_file) | |
336 __ofile.close (); | |
337 | |
338 return EXIT_SUCCESS; | |
339 } | |
340 | |
341 // kate: space-indent on; indent-width 2; replace-tabs on; | |
342 |