1 /****************************************************************************
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
4 ** Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
6 ** This file is part of $PRODUCT$.
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 ****************************************************************************/
21 # define GCC_MACHINE "i386-redhat-linux"
25 # define GCC_VERSION "4.1.1"
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
;
43 std::string
id (m
->name
->begin (), m
->name
->end ());
44 __out
<< "#define " << id
;
46 if (m
->f
.function_like
)
50 for (std::size_t i
= 0; i
< m
->formals
.size (); ++i
)
55 pp_fast_string
const *f
= m
->formals
[i
];
56 std::string
name (f
->begin (), f
->end ());
69 std::string
def (m
->definition
->begin (), m
->definition
->end ());
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;
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
)
108 else if (! strcmp (arg
, "-help"))
111 else if (! strcmp (arg
, "-dM"))
112 opt_dump_macros
= true;
114 else if (! strcmp (arg
, "-pch"))
117 else if (! strcmp (arg
, "-msse"))
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"))
130 include_pch_file
= *++argv
;
133 else if (! strncmp (arg
, "-o", 2))
137 if (! arg
[0] && argv
[1])
144 else if (! strncmp (arg
, "-conf", 8))
147 preprocess
.file (*++argv
, null_out
);
150 else if (! strncmp (arg
, "-I", 2))
154 if (! arg
[0] && argv
[1])
158 preprocess
.push_include_path (arg
);
161 else if (! strncmp (arg
, "-U", 2))
165 if (! arg
[0] && argv
[1])
170 env
.unbind (arg
, strlen (arg
));
174 else if (! strncmp (arg
, "-D", 2))
178 if (! arg
[0] && argv
[1])
185 char const *end
= arg
;
196 __macro
.name
= pp_symbol::get (arg
, eq
- arg
);
197 __macro
.definition
= pp_symbol::get (eq
+ 1, end
- (eq
+ 1));
202 __macro
.name
= pp_symbol::get (arg
, end
- arg
);
203 __macro
.definition
= 0;
206 env
.bind (__macro
.name
, __macro
);
216 if (! input_file
|| opt_help
)
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] == '.')
227 env
.unbind ("__cplusplus", 11);
230 __macro
.name
= pp_symbol::get ("__null");
231 __macro
.definition
= pp_symbol::get ("((void*) 0)");
232 env
.bind (__macro
.name
, __macro
);
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
);
248 preprocess
.file (input_file
, null_out
);
249 dump_macros (env
, preprocess
, std::cout
);
253 preprocess
.file (input_file
, out
);
259 std::cerr
<< "*** WARNING expected a file name" << std::endl
;
263 std::string
__conf_file (output_file
);
264 __conf_file
+= ".conf";
267 __out
.open (__conf_file
.c_str ());
268 dump_macros (env
, preprocess
, __out
);
271 std::string
__pp_file (output_file
);
274 __out
.open (__pp_file
.c_str ());
275 __out
.write (result
.c_str (), result
.size ());
280 std::ostream
*__out
= &std::cout
;
281 std::ofstream __ofile
;
285 std::string
__output_file_name (output_file
);
286 __ofile
.open (output_file
);
290 if (include_pch_file
)
292 std::string
__pch (include_pch_file
);
293 __pch
+= ".gch/c++.i";
295 std::ifstream
__in (__pch
.c_str ());
298 while (__in
.read (buffer
, 1024))
299 __out
->write (buffer
, 1024);
304 __out
->write (result
.c_str (), result
.size ());
312 // kate: space-indent on; indent-width 2; replace-tabs on;