bool* arguments can be used by wrappers
[lqt.git] / cpptoxml / parser / rpp / pp-main.cpp
blob724ecf9c3ef5e5be6d790b5fcb617016152346f4
1 /****************************************************************************
2 **
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
4 ** Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
5 **
6 ** This file is part of $PRODUCT$.
7 **
8 ** $CPP_LICENSE$
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 ****************************************************************************/
15 #include <fstream>
16 #include "pp.h"
18 using namespace rpp;
20 #ifndef GCC_MACHINE
21 # define GCC_MACHINE "i386-redhat-linux"
22 #endif
24 #ifndef GCC_VERSION
25 # define GCC_VERSION "4.1.1"
26 #endif
28 void usage ()
30 std::cerr << "usage: rpp file.cpp" << std::endl;
31 ::exit (EXIT_FAILURE);
34 void dump_macros (pp_environment &env, pp &, std::ostream &__out)
36 for (pp_environment::const_iterator it = env.first_macro (); it != env.last_macro (); ++it)
38 pp_macro const *m = *it;
40 if (m->hidden)
41 continue;
43 std::string id (m->name->begin (), m->name->end ());
44 __out << "#define " << id;
46 if (m->f.function_like)
48 __out << "(";
50 for (std::size_t i = 0; i < m->formals.size (); ++i)
52 if (i != 0)
53 __out << ", ";
55 pp_fast_string const *f = m->formals [i];
56 std::string name (f->begin (), f->end ());
57 __out << name;
60 if (m->variadics)
61 __out << "...";
63 __out << ")";
66 __out << "\t";
67 if (m->definition)
69 std::string def (m->definition->begin (), m->definition->end ());
70 __out << def;
73 __out << std::endl;
77 int main (int, char *argv [])
79 char const *input_file = 0;
80 char const *output_file = 0;
81 char const *include_pch_file = 0;
82 bool opt_help = false;
83 bool opt_dump_macros = false;
84 bool opt_pch = false;
86 pp_environment env;
87 pp preprocess(env);
89 std::string result;
90 result.reserve (20 * 1024); // 20K
92 pp_output_iterator<std::string> out (result);
93 pp_null_output_iterator null_out;
95 preprocess.push_include_path ("/usr/include");
96 preprocess.push_include_path ("/usr/lib/gcc/" GCC_MACHINE "/" GCC_VERSION "/include");
98 preprocess.push_include_path ("/usr/include/c++/" GCC_VERSION);
99 preprocess.push_include_path ("/usr/include/c++/" GCC_VERSION "/" GCC_MACHINE);
101 std::string extra_args;
103 while (const char *arg = *++argv)
105 if (arg [0] != '-')
106 input_file = arg;
108 else if (! strcmp (arg, "-help"))
109 opt_help = true;
111 else if (! strcmp (arg, "-dM"))
112 opt_dump_macros = true;
114 else if (! strcmp (arg, "-pch"))
115 opt_pch = true;
117 else if (! strcmp (arg, "-msse"))
119 pp_macro __macro;
120 __macro.name = pp_symbol::get ("__SSE__", 7);
121 env.bind (__macro.name, __macro);
123 __macro.name = pp_symbol::get ("__MMX__", 7);
124 env.bind (__macro.name, __macro);
127 else if (! strcmp (arg, "-include"))
129 if (argv [1])
130 include_pch_file = *++argv;
133 else if (! strncmp (arg, "-o", 2))
135 arg += 2;
137 if (! arg [0] && argv [1])
138 arg = *++argv;
140 if (arg)
141 output_file = arg;
144 else if (! strncmp (arg, "-conf", 8))
146 if (argv [1])
147 preprocess.file (*++argv, null_out);
150 else if (! strncmp (arg, "-I", 2))
152 arg += 2;
154 if (! arg [0] && argv [1])
155 arg = *++argv;
157 if (arg)
158 preprocess.push_include_path (arg);
161 else if (! strncmp (arg, "-U", 2))
163 arg += 2;
165 if (! arg [0] && argv [1])
166 arg = *++argv;
168 if (arg)
170 env.unbind (arg, strlen (arg));
174 else if (! strncmp (arg, "-D", 2))
176 arg += 2;
178 if (! arg [0] && argv [1])
179 arg = *++argv;
181 if (arg)
183 pp_macro __macro;
185 char const *end = arg;
186 char const *eq = 0;
188 for (; *end; ++end)
190 if (*end == '=')
191 eq = end;
194 if (eq != 0)
196 __macro.name = pp_symbol::get (arg, eq - arg);
197 __macro.definition = pp_symbol::get (eq + 1, end - (eq + 1));
200 else
202 __macro.name = pp_symbol::get (arg, end - arg);
203 __macro.definition = 0;
206 env.bind (__macro.name, __macro);
209 else
211 extra_args += " ";
212 extra_args += arg;
216 if (! input_file || opt_help)
218 usage ();
219 return EXIT_FAILURE;
222 std::string __ifile (input_file);
223 bool is_c_file = false;
224 if (__ifile.size () > 2 && __ifile [__ifile.size () - 1] == 'c' && __ifile [__ifile.size () - 2] == '.')
226 is_c_file = true;
227 env.unbind ("__cplusplus", 11);
229 pp_macro __macro;
230 __macro.name = pp_symbol::get ("__null");
231 __macro.definition = pp_symbol::get ("((void*) 0)");
232 env.bind (__macro.name, __macro);
234 // turn off the pch
235 include_pch_file = 0;
237 else if (include_pch_file)
239 std::string __pch (include_pch_file);
240 __pch += ".gch/c++.conf";
242 //std::cerr << "*** pch file " << __pch << std::endl;
243 preprocess.file (__pch, null_out);
246 if (opt_dump_macros)
248 preprocess.file (input_file, null_out);
249 dump_macros (env, preprocess, std::cout);
250 return EXIT_SUCCESS;
253 preprocess.file (input_file, out);
255 if (opt_pch)
257 if (! output_file)
259 std::cerr << "*** WARNING expected a file name" << std::endl;
260 return EXIT_FAILURE;
263 std::string __conf_file (output_file);
264 __conf_file += ".conf";
266 std::ofstream __out;
267 __out.open (__conf_file.c_str ());
268 dump_macros (env, preprocess, __out);
269 __out.close ();
271 std::string __pp_file (output_file);
272 __pp_file += ".i";
274 __out.open (__pp_file.c_str ());
275 __out.write (result.c_str (), result.size ());
276 __out.close ();
277 return EXIT_SUCCESS;
280 std::ostream *__out = &std::cout;
281 std::ofstream __ofile;
283 if (output_file)
285 std::string __output_file_name (output_file);
286 __ofile.open (output_file);
287 __out = &__ofile;
290 if (include_pch_file)
292 std::string __pch (include_pch_file);
293 __pch += ".gch/c++.i";
295 std::ifstream __in (__pch.c_str ());
297 char buffer [1024];
298 while (__in.read (buffer, 1024))
299 __out->write (buffer, 1024);
301 __in.close ();
304 __out->write (result.c_str (), result.size ());
306 if (output_file)
307 __ofile.close ();
309 return EXIT_SUCCESS;
312 // kate: space-indent on; indent-width 2; replace-tabs on;