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);
271 line_table = XNEW (struct line_maps);
274 /* Process the options that have been parsed. */
276 process_options (void)
278 /* Allow the front end to perform consistency checks and do further
279 initialization based on the command line options. This hook also
280 sets the original filename if appropriate (e.g. foo.i -> foo.c)
281 so we can correctly initialize debug output. */
282 /*no_backend =*/ (*lang_hooks.post_options) (&main_input_filename);
285 /* Parse a -d... command line switch. */
288 decode_d_option (const char *arg)
295 case 'D': /* These are handled by the preprocessor. */
302 warning (0, "unrecognized gcc debugging option: %c", c);
311 /* An informative note. Use this for additional details on an error
314 inform (const char *gmsgid, ...)
318 va_start (ap, gmsgid);
319 fprintf (stderr, "%s: note: ", progname);
320 vfprintf (stderr, gmsgid, ap);
325 /* A warning. Use this for code which is correct according to the
326 relevant language specification but is likely to be buggy anyway. */
328 warning (int opt, const char *gmsgid, ...)
332 if CPP_OPTION (parse_in, warnings_are_errors)
335 va_start (ap, gmsgid);
336 fprintf (stderr, "%s: warning: ", progname);
337 vfprintf (stderr, gmsgid, ap);
342 /* A hard error: the code is definitely ill-formed, and an object file
343 will not be produced. */
345 error (const char *gmsgid, ...)
351 va_start (ap, gmsgid);
352 fprintf (stderr, "%s: error: ", progname);
353 vfprintf (stderr, gmsgid, ap);
358 /* An error which is severe enough that we make no attempt to
359 continue. Do not use this for internal consistency checks; that's
360 internal_error. Use of this function should be rare. */
362 fatal_error (const char *gmsgid, ...)
366 va_start (ap, gmsgid);
367 fprintf (stderr, "%s: fatal error: ", progname);
368 vfprintf (stderr, gmsgid, ap);
372 exit (FATAL_EXIT_CODE);
375 /* An internal consistency check has failed. We make no attempt to
376 continue. Note that unless there is debugging value to be had from
377 a more specific message, or some other good reason, you should use
378 abort () instead of calling this function directly. */
380 internal_error (const char *gmsgid, ...)
384 va_start (ap, gmsgid);
385 fprintf (stderr, "%s: internal compiler error: ", progname);
386 vfprintf (stderr, gmsgid, ap);
390 exit (FATAL_EXIT_CODE);
393 /* Report an internal compiler error in a friendly manner. This is
394 the function that gets called upon use of abort() in the source
395 code generally, thanks to a special macro. */
398 fancy_abort (const char *file, int line, const char *function)
400 internal_error ("in %s, at %s:%d", function, file, line);
403 /* Language-dependent initialization. Returns nonzero on success. */
405 lang_dependent_init (const char *name)
407 /* Other front-end initialization. */
408 if ((*lang_hooks.init) () == 0)
414 /* Clean up: close opened files, etc. */
419 /* Language-specific end of compilation actions. */
420 (*lang_hooks.finish) ();
423 /* Initialize the compiler, and compile the input file. */
429 /* Don't do any more if an error has already occurred. */
432 /* Language-dependent initialization. Returns true on success. */
433 lang_dependent_init (main_input_filename);
439 /* Entry point of sdcpp.
440 Exit code is FATAL_EXIT_CODE if can't open files or if there were
441 any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
443 It is not safe to call this function more than once. */
446 main (int argc, const char **argv)
448 /* Initialization of SDCPP's environment. */
449 general_init (argv[0]);
451 /* Parse the options and do minimal processing; basically just
452 enough to default flags appropriately. */
453 decode_options (argc, argv);
455 /* Exit early if we can (e.g. -help). */
456 if (!exit_after_options)
460 return (FATAL_EXIT_CODE);
462 return (SUCCESS_EXIT_CODE);