* get rid of diagnistic.[ch], pretty-print.[ch],
[fw/sdcc] / support / cpp2 / sdcpp.c
1 /*-------------------------------------------------------------------------
2   sdcppmain.c - sdcpp: SDCC preprocessor main file, using cpplib.
3
4   Written by Borut Razem, 2006.
5
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20   In other words, you are welcome to use, share and improve this program.
21   You are forbidden to forbid anyone else to use, share and improve
22   what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #include "config.h"
26 #include "system.h"
27 #include "cpplib.h"
28 #include "internal.h"
29 #include "version.h"
30 #include "mkdeps.h"
31 #include "opts.h"
32 #include "intl.h"
33
34 const char *progname;           /* Needs to be global.  */
35
36 /* From laghooks-def.h */
37 /* The whole thing.  The structure is defined in langhooks.h.  */
38 #define LANG_HOOKS_INITIALIZER { \
39   LANG_HOOKS_INIT_OPTIONS, \
40   LANG_HOOKS_HANDLE_OPTION, \
41   LANG_HOOKS_MISSING_ARGUMENT, \
42   LANG_HOOKS_POST_OPTIONS, \
43   LANG_HOOKS_INIT, \
44   LANG_HOOKS_FINISH, \
45 }
46
47 /* From c-lang.c */
48 #define LANG_HOOKS_INIT_OPTIONS sdcpp_init_options
49 #define LANG_HOOKS_HANDLE_OPTION sdcpp_common_handle_option
50 #define LANG_HOOKS_MISSING_ARGUMENT sdcpp_common_missing_argument
51 #define LANG_HOOKS_POST_OPTIONS sdcpp_common_post_options
52 #define LANG_HOOKS_INIT sdcpp_common_init
53 #define LANG_HOOKS_FINISH sdcpp_common_finish
54
55 static unsigned int sdcpp_init_options (unsigned int argc, const char **argv);
56
57 /* Each front end provides its own lang hook initializer.  */
58 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
59
60 /* Name of top-level original source file (what was input to cpp).
61    This comes from the #-command at the beginning of the actual input.
62    If there isn't any there, then this is the cc1 input file name.  */
63
64 const char *main_input_filename;
65
66 struct line_maps line_table;
67
68 /* Temporarily suppress certain warnings.
69    This is set while reading code from a system header file.  */
70
71 int in_system_header = 0;
72
73 /* Nonzero means change certain warnings into errors.
74    Usually these are warnings about failure to conform to some standard.  */
75
76 int flag_pedantic_errors = 0;
77
78 cpp_reader *parse_in;           /* Declared in c-pragma.h.  */
79
80 /* Nonzero means `char' should be signed.  */
81
82 int flag_signed_char;
83
84 /* Nonzero means don't output line number information.  */
85
86 char flag_no_line_commands;
87
88 /* Nonzero causes -E output not to be done, but directives such as
89    #define that have side effects are still obeyed.  */
90
91 char flag_no_output;
92
93 /* Nonzero means dump macros in some fashion.  */
94
95 char flag_dump_macros;
96
97 /* Nonzero means pass #include lines through to the output.  */
98
99 char flag_dump_includes;
100
101 /* 0 means we want the preprocessor to not emit line directives for
102    the current working directory.  1 means we want it to do it.  -1
103    means we should decide depending on whether debugging information
104    is being emitted or not.  */
105
106 int flag_working_directory = -1;
107
108 /* The current working directory of a translation.  It's generally the
109    directory from which compilation was initiated, but a preprocessed
110    file may specify the original directory in which it was
111    created.  */
112
113 static const char *src_pwd;
114
115 /* Initialize src_pwd with the given string, and return true.  If it
116    was already initialized, return false.  As a special case, it may
117    be called with a NULL argument to test whether src_pwd has NOT been
118    initialized yet.  */
119
120 /* From intl.c */
121 /* Opening quotation mark for diagnostics.  */
122 const char *open_quote = "'";
123
124 /* Closing quotation mark for diagnostics.  */
125 const char *close_quote = "'";
126 /* ----------- */
127
128 bool
129 set_src_pwd (const char *pwd)
130 {
131   if (src_pwd)
132     return false;
133
134   src_pwd = xstrdup (pwd);
135   return true;
136 }
137
138 /* Return the directory from which the translation unit was initiated,
139    in case set_src_pwd() was not called before to assign it a
140    different value.  */
141
142 const char *
143 get_src_pwd (void)
144 {
145   if (! src_pwd)
146     src_pwd = getpwd ();
147
148    return src_pwd;
149 }
150
151 /* SDCPP specific pragmas */
152 /* SDCC specific
153    sdcc_hash pragma */
154 static void
155 do_pragma_sdcc_hash (cpp_reader *pfile)
156 {
157     const cpp_token *tok = _cpp_lex_token (pfile);
158
159     if (tok->type == CPP_PLUS)
160     {
161         CPP_OPTION(pfile, allow_naked_hash)++;
162     }
163     else if (tok->type == CPP_MINUS)
164     {
165         CPP_OPTION(pfile, allow_naked_hash)--;
166     }
167     else
168     {
169         cpp_error (pfile, CPP_DL_ERROR,
170                    "invalid #pragma sdcc_hash directive, need '+' or '-'");
171     }
172 }
173
174 /* SDCC specific
175    pedantic_parse_number pragma */
176 static void
177 do_pragma_pedantic_parse_number (cpp_reader *pfile)
178 {
179     const cpp_token *tok = _cpp_lex_token (pfile);
180
181   if (tok->type == CPP_PLUS)
182     {
183       CPP_OPTION(pfile, pedantic_parse_number)++;
184     }
185   else if (tok->type == CPP_MINUS)
186     {
187       CPP_OPTION(pfile, pedantic_parse_number)--;
188     }
189   else
190     {
191       cpp_error (pfile, CPP_DL_ERROR,
192                  "invalid #pragma pedantic_parse_number directive, need '+' or '-'");
193     }
194 }
195
196 /* SDCC _asm specific
197    switch _asm block preprocessing on / off */
198 static void
199 do_pragma_preproc_asm (cpp_reader *pfile)
200 {
201   const cpp_token *tok = _cpp_lex_token (pfile);
202
203   if (tok->type == CPP_PLUS)
204     {
205       CPP_OPTION(pfile, preproc_asm)++;
206     }
207   else if (tok->type == CPP_MINUS)
208     {
209       CPP_OPTION(pfile, preproc_asm)--;
210     }
211   else
212     {
213       cpp_error (pfile, CPP_DL_ERROR,
214                  "invalid #pragma preproc_asm directive, need '+' or '-'");
215     }
216 }
217
218 /* SDCPP specific option initialization */
219 static unsigned int
220 sdcpp_init_options (unsigned int argc, const char **argv)
221 {
222   unsigned int ret = sdcpp_common_init_options(argc, argv);
223
224   CPP_OPTION (parse_in, allow_naked_hash) = 0;
225   CPP_OPTION (parse_in, preproc_asm) = 1;
226   CPP_OPTION (parse_in, pedantic_parse_number) = 0;
227   CPP_OPTION (parse_in, obj_ext) = NULL;
228
229   /* Kevin abuse for SDCC. */
230   cpp_register_pragma(parse_in, 0, "sdcc_hash", do_pragma_sdcc_hash, false);
231   /* SDCC _asm specific */
232   cpp_register_pragma(parse_in, 0, "preproc_asm", do_pragma_preproc_asm, false);
233   /* SDCC specific */
234   cpp_register_pragma(parse_in, 0, "pedantic_parse_number", do_pragma_pedantic_parse_number, false);
235
236   /* SDCC _asm specific */
237   parse_in->spec_nodes.n__asm = cpp_lookup (parse_in, DSC("_asm"));
238
239   return ret;
240 }
241
242 void
243 print_version (FILE *file, const char *indent)
244 {
245   fprintf (file, _("GNU CPP version %s (cpplib)"), version_string);
246 #ifdef TARGET_VERSION
247   TARGET_VERSION;
248 #endif
249   fputc ('\n', file);
250 }
251
252 /* Initialization of the front end environment, before command line
253    options are parsed.  Signal handlers, internationalization etc.
254    ARGV0 is main's argv[0].  */
255 static void
256 general_init (const char *argv0)
257 {
258   const char *p;
259
260   p = argv0 + strlen (argv0);
261   while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
262     --p;
263   progname = p;
264
265   xmalloc_set_program_name (progname);
266
267   hex_init ();
268
269   gcc_init_libintl ();
270 }
271
272 /* Process the options that have been parsed.  */
273 static void
274 process_options (void)
275 {
276   /* Allow the front end to perform consistency checks and do further
277      initialization based on the command line options.  This hook also
278      sets the original filename if appropriate (e.g. foo.i -> foo.c)
279      so we can correctly initialize debug output.  */
280   /*no_backend =*/ (*lang_hooks.post_options) (&main_input_filename);
281 }
282
283 /* Parse a -d... command line switch.  */
284
285 void
286 decode_d_option (const char *arg)
287 {
288   int c;
289
290   while (*arg)
291     switch (c = *arg++)
292       {
293       case 'D': /* These are handled by the preprocessor.  */
294       case 'I':
295       case 'M':
296       case 'N':
297         break;
298
299       default:
300         warning (0, "unrecognized gcc debugging option: %c", c);
301         break;
302       }
303 }
304
305 /* Diagnostic */
306
307 int errorcount = 0;
308
309 /* An informative note.  Use this for additional details on an error
310    message.  */
311 void
312 inform (const char *gmsgid, ...)
313 {
314   va_list ap;
315
316   va_start (ap, gmsgid);
317   fprintf (stderr, "%s: note: ", progname);
318   vfprintf (stderr, gmsgid, ap);
319   va_end (ap);
320 }
321
322 /* A warning.  Use this for code which is correct according to the
323    relevant language specification but is likely to be buggy anyway.  */
324 void
325 warning (int opt, const char *gmsgid, ...)
326 {
327   va_list ap;
328
329   if CPP_OPTION (parse_in, warnings_are_errors)
330     ++errorcount;
331
332   va_start (ap, gmsgid);
333   fprintf (stderr, "%s: warning: ", progname);
334   vfprintf (stderr, gmsgid, ap);
335   va_end (ap);
336 }
337
338 /* A hard error: the code is definitely ill-formed, and an object file
339    will not be produced.  */
340 void
341 error (const char *gmsgid, ...)
342 {
343   va_list ap;
344
345   ++errorcount;
346
347   va_start (ap, gmsgid);
348   fprintf (stderr, "%s: error: ", progname);
349   vfprintf (stderr, gmsgid, ap);
350   va_end (ap);
351 }
352
353 /* An error which is severe enough that we make no attempt to
354    continue.  Do not use this for internal consistency checks; that's
355    internal_error.  Use of this function should be rare.  */
356 void
357 fatal_error (const char *gmsgid, ...)
358 {
359   va_list ap;
360
361   va_start (ap, gmsgid);
362   fprintf (stderr, "%s: fatal error: ", progname);
363   vfprintf (stderr, gmsgid, ap);
364   va_end (ap);
365
366   gcc_unreachable ();
367 }
368
369 /* An internal consistency check has failed.  We make no attempt to
370    continue.  Note that unless there is debugging value to be had from
371    a more specific message, or some other good reason, you should use
372    abort () instead of calling this function directly.  */
373 void
374 internal_error (const char *gmsgid, ...)
375 {
376   va_list ap;
377
378   va_start (ap, gmsgid);
379   fprintf (stderr, "%s: internal compiler error: ", progname);
380   vfprintf (stderr, gmsgid, ap);
381   va_end (ap);
382
383   gcc_unreachable ();
384 }
385
386 /* Report an internal compiler error in a friendly manner.  This is
387    the function that gets called upon use of abort() in the source
388    code generally, thanks to a special macro.  */
389
390 void
391 fancy_abort (const char *file, int line, const char *function)
392 {
393   internal_error ("in %s, at %s:%d", function, file, line);
394 }
395
396 /* Language-dependent initialization.  Returns nonzero on success.  */
397 static int
398 lang_dependent_init (const char *name)
399 {
400   /* Other front-end initialization.  */
401   if ((*lang_hooks.init) () == 0)
402     return 0;
403
404   return 1;
405 }
406
407 /* Clean up: close opened files, etc.  */
408
409 static void
410 finalize (void)
411 {
412   /* Language-specific end of compilation actions.  */
413   (*lang_hooks.finish) ();
414 }
415
416 /* Initialize the compiler, and compile the input file.  */
417 static void
418 do_compile (void)
419 {
420   process_options ();
421
422   /* Don't do any more if an error has already occurred.  */
423   if (!errorcount)
424     {
425       /* Language-dependent initialization.  Returns true on success.  */
426       lang_dependent_init (main_input_filename);
427
428       finalize ();
429     }
430 }
431
432 /* Entry point of sdcpp.
433    Exit code is FATAL_EXIT_CODE if can't open files or if there were
434    any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
435
436    It is not safe to call this function more than once.  */
437
438 int
439 main (unsigned int argc, const char **argv)
440 {
441   /* Initialization of SDCPP's environment.  */
442   general_init (argv[0]);
443
444   /* Parse the options and do minimal processing; basically just
445      enough to default flags appropriately.  */
446   decode_options (argc, argv);
447
448   /* Exit early if we can (e.g. -help).  */
449   if (!exit_after_options)
450     do_compile ();
451
452   if (errorcount)
453     return (FATAL_EXIT_CODE);
454
455   return (SUCCESS_EXIT_CODE);
456 }