1 /*-------------------------------------------------------------------------
2 sdcppmain.c - sdcpp: SDCC preprocessor main file, using cpplib.
4 Written by Borut Razem, 2006.
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
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.
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.
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 -------------------------------------------------------------------------*/
34 const char *progname; /* Needs to be global. */
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, \
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
55 static unsigned int sdcpp_init_options (unsigned int argc, const char **argv);
57 /* Each front end provides its own lang hook initializer. */
58 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
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. */
64 const char *main_input_filename;
66 struct line_maps line_table;
68 /* Temporarily suppress certain warnings.
69 This is set while reading code from a system header file. */
71 int in_system_header = 0;
73 /* Nonzero means change certain warnings into errors.
74 Usually these are warnings about failure to conform to some standard. */
76 int flag_pedantic_errors = 0;
78 cpp_reader *parse_in; /* Declared in c-pragma.h. */
80 /* Nonzero means `char' should be signed. */
84 /* Nonzero means don't output line number information. */
86 char flag_no_line_commands;
88 /* Nonzero causes -E output not to be done, but directives such as
89 #define that have side effects are still obeyed. */
93 /* Nonzero means dump macros in some fashion. */
95 char flag_dump_macros;
97 /* Nonzero means pass #include lines through to the output. */
99 char flag_dump_includes;
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. */
106 int flag_working_directory = -1;
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
113 static const char *src_pwd;
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
121 /* Opening quotation mark for diagnostics. */
122 const char *open_quote = "'";
124 /* Closing quotation mark for diagnostics. */
125 const char *close_quote = "'";
129 set_src_pwd (const char *pwd)
134 src_pwd = xstrdup (pwd);
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
151 /* SDCPP specific pragmas */
155 do_pragma_sdcc_hash (cpp_reader *pfile)
157 const cpp_token *tok = _cpp_lex_token (pfile);
159 if (tok->type == CPP_PLUS)
161 CPP_OPTION(pfile, allow_naked_hash)++;
163 else if (tok->type == CPP_MINUS)
165 CPP_OPTION(pfile, allow_naked_hash)--;
169 cpp_error (pfile, CPP_DL_ERROR,
170 "invalid #pragma sdcc_hash directive, need '+' or '-'");
175 pedantic_parse_number pragma */
177 do_pragma_pedantic_parse_number (cpp_reader *pfile)
179 const cpp_token *tok = _cpp_lex_token (pfile);
181 if (tok->type == CPP_PLUS)
183 CPP_OPTION(pfile, pedantic_parse_number)++;
185 else if (tok->type == CPP_MINUS)
187 CPP_OPTION(pfile, pedantic_parse_number)--;
191 cpp_error (pfile, CPP_DL_ERROR,
192 "invalid #pragma pedantic_parse_number directive, need '+' or '-'");
196 /* SDCC _asm specific
197 switch _asm block preprocessing on / off */
199 do_pragma_preproc_asm (cpp_reader *pfile)
201 const cpp_token *tok = _cpp_lex_token (pfile);
203 if (tok->type == CPP_PLUS)
205 CPP_OPTION(pfile, preproc_asm)++;
207 else if (tok->type == CPP_MINUS)
209 CPP_OPTION(pfile, preproc_asm)--;
213 cpp_error (pfile, CPP_DL_ERROR,
214 "invalid #pragma preproc_asm directive, need '+' or '-'");
218 /* SDCPP specific option initialization */
220 sdcpp_init_options (unsigned int argc, const char **argv)
222 unsigned int ret = sdcpp_common_init_options(argc, argv);
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;
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);
234 cpp_register_pragma(parse_in, 0, "pedantic_parse_number", do_pragma_pedantic_parse_number, false);
236 /* SDCC _asm specific */
237 parse_in->spec_nodes.n__asm = cpp_lookup (parse_in, DSC("_asm"));
243 print_version (FILE *file, const char *indent)
245 fprintf (file, _("GNU CPP version %s (cpplib)"), version_string);
246 #ifdef TARGET_VERSION
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]. */
256 general_init (const char *argv0)
260 p = argv0 + strlen (argv0);
261 while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
265 xmalloc_set_program_name (progname);
272 /* Process the options that have been parsed. */
274 process_options (void)
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);
283 /* Parse a -d... command line switch. */
286 decode_d_option (const char *arg)
293 case 'D': /* These are handled by the preprocessor. */
300 warning (0, "unrecognized gcc debugging option: %c", c);
309 /* An informative note. Use this for additional details on an error
312 inform (const char *gmsgid, ...)
316 va_start (ap, gmsgid);
317 fprintf (stderr, "%s: note: ", progname);
318 vfprintf (stderr, gmsgid, ap);
323 /* A warning. Use this for code which is correct according to the
324 relevant language specification but is likely to be buggy anyway. */
326 warning (int opt, const char *gmsgid, ...)
330 if CPP_OPTION (parse_in, warnings_are_errors)
333 va_start (ap, gmsgid);
334 fprintf (stderr, "%s: warning: ", progname);
335 vfprintf (stderr, gmsgid, ap);
340 /* A hard error: the code is definitely ill-formed, and an object file
341 will not be produced. */
343 error (const char *gmsgid, ...)
349 va_start (ap, gmsgid);
350 fprintf (stderr, "%s: error: ", progname);
351 vfprintf (stderr, gmsgid, ap);
356 /* An error which is severe enough that we make no attempt to
357 continue. Do not use this for internal consistency checks; that's
358 internal_error. Use of this function should be rare. */
360 fatal_error (const char *gmsgid, ...)
364 va_start (ap, gmsgid);
365 fprintf (stderr, "%s: fatal error: ", progname);
366 vfprintf (stderr, gmsgid, ap);
370 exit (FATAL_EXIT_CODE);
373 /* An internal consistency check has failed. We make no attempt to
374 continue. Note that unless there is debugging value to be had from
375 a more specific message, or some other good reason, you should use
376 abort () instead of calling this function directly. */
378 internal_error (const char *gmsgid, ...)
382 va_start (ap, gmsgid);
383 fprintf (stderr, "%s: internal compiler error: ", progname);
384 vfprintf (stderr, gmsgid, ap);
388 exit (FATAL_EXIT_CODE);
391 /* Report an internal compiler error in a friendly manner. This is
392 the function that gets called upon use of abort() in the source
393 code generally, thanks to a special macro. */
396 fancy_abort (const char *file, int line, const char *function)
398 internal_error ("in %s, at %s:%d", function, file, line);
401 /* Language-dependent initialization. Returns nonzero on success. */
403 lang_dependent_init (const char *name)
405 /* Other front-end initialization. */
406 if ((*lang_hooks.init) () == 0)
412 /* Clean up: close opened files, etc. */
417 /* Language-specific end of compilation actions. */
418 (*lang_hooks.finish) ();
421 /* Initialize the compiler, and compile the input file. */
427 /* Don't do any more if an error has already occurred. */
430 /* Language-dependent initialization. Returns true on success. */
431 lang_dependent_init (main_input_filename);
437 /* Entry point of sdcpp.
438 Exit code is FATAL_EXIT_CODE if can't open files or if there were
439 any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
441 It is not safe to call this function more than once. */
444 main (int argc, const char **argv)
446 /* Initialization of SDCPP's environment. */
447 general_init (argv[0]);
449 /* Parse the options and do minimal processing; basically just
450 enough to default flags appropriately. */
451 decode_options (argc, argv);
453 /* Exit early if we can (e.g. -help). */
454 if (!exit_after_options)
458 return (FATAL_EXIT_CODE);
460 return (SUCCESS_EXIT_CODE);