Macros defined in command line are have definition generated before those defined in source file. If macros of the same name both appear in command line and source file, compiler will give out warning about redefinition, and definition in source file will overlay that in command line.
finish_options (continue)
1422 /* We're about to send user input to cpplib, so make it warn for
1423 things that we previously (when we sent it internal definitions)
1424 told it to not warn.
1425
1426 C99 permits implementation-defined characters in identifiers.
1427 The documented meaning of -std= is to turn off extensions that
1428 conflict with the specified standard, and since a strictly
1429 conforming program cannot contain a '$', we do not condition
1430 their acceptance on the -std= setting. */
1431 cpp_opts->warn_dollars = (cpp_opts->pedantic && !cpp_opts->c99);
1432
1433 cpp_change_file (parse_in, LC_RENAME, _("<command line>"));
1434 for (i = 0; i < deferred_count; i++)
1435 {
1436 struct deferred_opt *opt = &deferred_opts[i];
1437
1438 if (opt->code == OPT_D)
1439 cpp_define (parse_in, opt->arg);
1440 else if (opt->code == OPT_U)
1441 cpp_undef (parse_in, opt->arg);
1442 else if (opt->code == OPT_A)
1443 {
1444 if (opt->arg[0] == '-')
1445 cpp_unassert (parse_in, opt->arg + 1);
1446 else
1447 cpp_assert (parse_in, opt->arg);
1448 }
1449 }
Command line option “–imacro file” instructs GCC to include the macro definitions in file. These macros definition should be handled after macros introudced by –D or –U options.
finish_options (continue)
1451 /* Handle -imacros after -D and -U. */
1452 for (i = 0; i < deferred_count; i++)
1453 {
1454 struct deferred_opt *opt = &deferred_opts[i];
1455
1456 if (opt->code == OPT_imacros
1457 && cpp_push_include (parse_in, opt->arg))
1458 {
1459 /* Disable push_command_line_include callback for now. */
1460 include_cursor = deferred_count + 1;
1461 cpp_scan_nooutput (parse_in);
1462 }
1463 }
1464 }
Given the file name, GCC should locate the file before reading it.
1049 bool
1050 cpp_push_include (cpp_reader *pfile, const char *fname) in cppfiles.c
1051 {
1052 /* Make the command line directive take up a line. */
1053 pfile->line++;
1054 return _cpp_stack_include (pfile, fname, false, IT_CMDLINE);
1055 }
Notice that argument angle_brackets is false when invoked in cpp_push_include, it means file included –imacros will always be regarded as user defined file.
750 bool
751 _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets, in cppfiles.c
752 enum include_type type)
753 {
754 struct cpp_dir *dir;
755
756 dir = search_path_head (pfile, fname, angle_brackets, type);
757 if (!dir)
758 return false;
759
760 return _cpp_stack_file (pfile, _cpp_find_file (pfile, fname, dir, false,
761 angle_brackets),
762 type == IT_IMPORT);
763 }
Locating file is done by search_path_head. Remember in cpp_reader, slot angle_brackets holds searching directory for system including files, quote_include holds that for user defined file and no_search_path holds absolute path for including files.
692 static struct cpp_dir *
693 search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets, in cppfiles.c
694 enum include_type type)
695 {
696 cpp_dir *dir;
697 _cpp_file *file;
698
699 if (IS_ABSOLUTE_PATH (fname))
700 return &pfile->no_search_path;
701
702 /* pfile->buffer is NULL when processing an -include command-line flag. */
703 file = pfile->buffer == NULL ? pfile->main_file : pfile->buffer->file;
704
705 /* For #include_next, skip in the search path past the dir in which
706 the current file was found, but if it was found via an absolute
707 path use the normal search logic. */
708 if (type == IT_INCLUDE_NEXT && file->dir)
709 dir = file->dir->next;
710 else if (angle_brackets)
711 dir = pfile->bracket_include;
712 else if (type == IT_CMDLINE)
713 /* -include and -imacros use the #include "" chain with the
714 preprocessor's cwd prepended. */
715 return make_cpp_dir (pfile, "./", false);
716 else if (pfile->quote_ignores_source_dir)
717 dir = pfile->quote_include;
718 else
719 return make_cpp_dir (pfile, dir_name_of_file (file), pfile->map->sysp);
720
721 if (dir == NULL)
722 cpp_error (pfile, CPP_DL_ERROR,
723 "no include path in which to search for %s", fname);
724
725 return dir;
726 }
See that for –imacros including file, the start searching point is current directory, however, it is just the suggestion, _cpp_find_file at line 760 in _cpp_stack_include above will searching the file following the dir list till find out the one.
At line 708, is the handling of #include_next directive, it is used inside one header file to include another one, and it causes the search for the new header file to begin in the directory following the one in which the current header was found. For example, if the normal search for a header file is to look in directories A, B, C, D, and E, and if the current header file has been found in directory B, an #include_next directive in the current header file will cause a search for the newly named header file in directories C, D, and E.
Also notice that for normal file, if no start point is suggested, the header file will be searched first under directory the including file residues.
Then when file found, _cpp_stack_file at line 760 will read in the file, and refers to section Read in file for detail.
Files included by –imacros will only have macro definition retrieved. So cpp_scan_nooutput invokes cpp_get_token for macros repeatly till reach file end. cpp_get_token is also the core of preprocessor, it is the main site for preprocessing. Thus, we learn it with effort.
1136 void
1137 cpp_scan_nooutput (cpp_reader *pfile) in cppmacro.c
1138 {
1139 /* Request a CPP_EOF token at the end of this file, rather than
1140 transparently continuing with the including file. */
1141 pfile->buffer->return_at_eof = true;
1142
1143 if (CPP_OPTION (pfile, traditional))
1144 while (_cpp_read_logical_line_trad (pfile))
1145 ;
1146 else
1147 while (cpp_get_token (pfile)->type != CPP_EOF)
1148 ;
1149 }
context field of cpp_reader is a list which forms a sequence of relative independent contexts. If there is only one node in the list, it is the so-called base context, the function fetches token in the file directly; while other nodes contain groups of tokens for specified handling, for example, macro expansion, macro parameter replacement, etc. With this context list and designed cpp_hashnode, cpp_get_token can expand a macro when see it (not the place of definition).
1049 const cpp_token *
1050 cpp_get_token (cpp_reader *pfile) in cppmacro.c
1051 {
1052 const cpp_token *result;
1053
1054 for (;;)
1055 {
1056 cpp_hashnode *node;
1057 cpp_context *context = pfile->context;
1058
1059 /* Context->prev == 0 <=> base context. */
1060 if (!context->prev)
1061 result = _cpp_lex_token (pfile);
1062 else if (FIRST (context).token != LAST (context).token)
1063 {
1064 if (context->direct_p)
1065 result = FIRST (context).token++;
1066 else
1067 result = *FIRST (context).ptoken++;
1068
1069 if (result->flags & PASTE_LEFT)
1070 {
1071 paste_all_tokens (pfile, result);
1072 if (pfile->state.in_directive)
1073 continue;
1074 return padding_token (pfile, result);
1075 }
1076 }
1077 else
1078 {
1079 _cpp_pop_context (pfile);
1080 if (pfile->state.in_directive)
1081 continue;
1082 return &pfile->avoid_paste;
1083 }
Above at line 1062, FIRST and LAST point to the first and one passed the last token in the context, if both equates, means the context has been handled, and is poped by _cpp_pop_context at line 1079. in_directive at line 1972 and 1080 if nonzero, indicateds processing directive (e.g., #define, #ifdef). Normally, at finishing a context, the result would be put into a new context.
At line 1082, avoid_paste has type of CPP_PADDING, and val.source of NULL, and at line 1074, padding_token has following definition.
900 static const cpp_token *
901 padding_token (cpp_reader *pfile, const cpp_token *source) in cppmacro.c
902 {
903 cpp_token *result = _cpp_temp_token (pfile);
904
905 result->type = CPP_PADDING;
906 result->val.source = source;
907 result->flags = 0;
908 return result;
909 }
CPP_PADDING stands for white space. Takes token pasting as example, current gcc works as this. For instance, a macro: #define appA(PRE, A) PRE##A, with later invocation appA (super, man). Then the preprocessor records tokens for the appA’s expansion in a new context: “super”, “man”; and sets super’s flags as PASTE_LEFT, then returns a CPP_PADDING (see below line 1103 in cpp_get_token). But caller of cpp_get_token ususally discards this CPP_PADDING, and continue to read in token from this new context. This time condition at line 1069 is met, “super” and “man” is pasted by paste_all_tokens into “superman”, then added in another new context, and at line 1074, a CPP_PADDING is returned again. It would be abandoned too, but read in “superman” from the new context. At next time readin, as the context is empty, at line 1082, a white space is returned and removes the context. Then we are within context for macro expansion, which is empty now and returns a white space at next time readin. Then we back into base context. Clearly, it is a powerful way to protect the result from contexts (but in current gcc, such protection seems too strong). Being case with in_directive of nonzero, directive handling is taken in base context, it is impossible for context switch, no need and can’t insert white space.
We show the code of _cpp_lex_token in below again for convience.
691 const cpp_token *
692 _cpp_lex_token (cpp_reader *pfile) in cpplex.c
693 {
694 cpp_token *result;
695
696 for (;;)
697 {
698 if (pfile->cur_token == pfile->cur_run->limit)
699 {
700 pfile->cur_run = next_tokenrun (pfile->cur_run);
701 pfile->cur_token = pfile->cur_run->base;
702 }
703
704 if (pfile->lookaheads)
705 {
706 pfile->lookaheads--;
707 result = pfile->cur_token++;
708 }
709 else
710 result = _cpp_lex_direct (pfile);
711
712 if (result->flags & BOL)
713 {
714 /* Is this a directive. If _cpp_handle_directive returns
715 false, it is an assembler #. */
716 if (result->type == CPP_HASH
717 /* 6.10.3 p 11: Directives in a list of macro arguments
718 gives undefined behavior. This implementation
719 handles the directive as normal. */
720 && pfile->state.parsing_args != 1
721 && _cpp_handle_directive (pfile, result->flags & PREV_WHITE))
722 continue;
723 if (pfile->cb.line_change && !pfile->state.skipping)
724 pfile->cb.line_change (pfile, result, pfile->state.parsing_args);
725 }
726
727 /* We don't skip tokens in directives. */
728 if (pfile->state.in_directive)
729 break;
730
731 /* Outside a directive, invalidate controlling macros. At file
732 EOF, _cpp_lex_direct takes care of popping the buffer, so we never
733 get here and MI optimization works. */
734 pfile->mi_valid = false;
735
736 if (!pfile->state.skipping || result->type == CPP_EOF)
737 break;
738 }
739
740 return result;
741 }
Above at line 720, slot parsing_args indicates the stage in which we are parsing the macro. It is 0 when we parsing the definition, and is set to 1 if we are doing macro replacement and are looking for open parenthesis, and set to 2 after open parenthesis is found. To understand the meaning of the condition beginning at line 716, consider following example:
#define FUN(c) c
int main() {
FUN(
#ifdef SMALL
12,
#else
24
#endif
);
}
At line 721, _cpp_handle_directive will be invoked when seeing the definition of FUN. Argument indented is nonzero if '#' of the directive was indented, for which case traditional mode will complain. And later we will see that when seeing #ifdef at line 5, _cpp_handle_directive will be invoked again.
333 int
334 _cpp_handle_directive (cpp_reader *pfile, int indented) in cpplib.c
335 {
336 const directive *dir = 0;
337 const cpp_token *dname;
338 bool was_parsing_args = pfile->state.parsing_args;
339 int skip = 1;
340
341 if (was_parsing_args)
342 {
343 if (CPP_OPTION (pfile, pedantic))
344 cpp_error (pfile, CPP_DL_PEDWARN,
345 "embedding a directive within macro arguments is not portable");
346 pfile->state.parsing_args = 0;
347 pfile->state.prevent_expansion = 0;
348 }
349 start_directive (pfile);
350 dname = _cpp_lex_token (pfile);
351
352 if (dname->type == CPP_NAME)
353 {
354 if (dname->val.node->is_directive)
355 dir = &dtable[dname->val.node->directive_index];
356 }
357 /* We do not recognize the # followed by a number extension in
358 assembler code. */
359 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
360 {
361 dir = &linemarker_dir;
362 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
363 && ! pfile->state.skipping)
364 cpp_error (pfile, CPP_DL_PEDWARN,
365 "style of line directive is a GCC extension");
366 }
Remember that dtable is of directive type and is initialized following DIRECTIVE_TABLE (refer to _cpp_init_directives, corresponding hashnode is also created and saved in ident_hash, so at line 354 val.node in dname is the identifier).
As a GCC extension, linemarker in form of #linenum filename flags which is inserted as needed into the output (but never within a string or character constant), means that the following line originated in the file filename at line linenum. To handle it, in the system there is a global directive instance linemarker_dir (see above at line 361), it contains the operation handler – do_linemarker for the linemarker. Notice that at line 362 preprocessed of opts in cpp_reader if nonzero means we're looking at already preprocessed code (or set by option –fpreprocessed to forbid preprocessing even if files on the command line have suffixes that would indicate that the files should be reprocessed), linemarker should not be present, and pedantic of opts in cpp_reader if nonzero means to give all the error messages the ANSI standard requires.
_cpp_handle_directive (continue)
368 if (dir)
369 {
370 /* If we have a directive that is not an opening conditional,
371 invalidate any control macro. */
372 if (! (dir->flags & IF_COND))
373 pfile->mi_valid = false;
374
375 /* Kluge alert. In order to be sure that code like this
376
377 #define HASH #
378 HASH define foo bar
379
380 does not cause '#define foo bar' to get executed when
381 compiled with -save-temps, we recognize directives in
382 -fpreprocessed mode only if the # is in column 1. cppmacro.c
383 puts a space in front of any '#' at the start of a macro. */
384 if (CPP_OPTION (pfile, preprocessed)
385 && (indented || !(dir->flags & IN_I)))
386 {
387 skip = 0;
388 dir = 0;
389 }
390 else
391 {
392 /* In failed conditional groups, all non-conditional
393 directives are ignored. Before doing that, whether
394 skipping or not, we should lex angle-bracketed headers
395 correctly, and maybe output some diagnostics. */
396 pfile->state.angled_headers = dir->flags & INCL;
397 pfile->state.directive_wants_padding = dir->flags & INCL;
398 if (!CPP_OPTION (pfile, preprocessed))
399 directive_diagnostics (pfile, dir, indented);
400 if (pfile->state.skipping && !(dir->flags & COND))
401 dir = 0;
402 }
403 }
404 else if (dname->type == CPP_EOF)
405 ; /* CPP_EOF is the "null directive". */
406 else
407 {
408 /* An unknown directive. Don't complain about it in assembly
409 source: we don't know where the comments are, and # may
410 introduce assembler pseudo-ops. Don't complain about invalid
411 directives in skipped conditional groups (6.10 p4). */
412 if (CPP_OPTION (pfile, lang) == CLK_ASM)
413 skip = 0;
414 else if (!pfile->state.skipping)
415 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
416 cpp_token_as_text (pfile, dname));
417 }
If directive object is found, dir is not NULL. In preprocessing, the directive is diagnosed to give out error messages about the incorrect usage. The function is quite clear.
297 static void
298 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented) in cpplib.c
299 {
300 /* Issue -pedantic warnings for extensions. */
301 if (CPP_PEDANTIC (pfile)
302 && ! pfile->state.skipping
303 && dir->origin == EXTENSION)
304 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
305
306 /* Traditionally, a directive is ignored unless its # is in
307 column 1. Therefore in code intended to work with K+R
308 compilers, directives added by C89 must have their #
309 indented, and directives present in traditional C must not.
310 This is true even of directives in skipped conditional
311 blocks. #elif cannot be used at all. */
312 if (CPP_WTRADITIONAL (pfile))
313 {
314 if (dir == &dtable [T_ELIF])
315 cpp_error (pfile, CPP_DL_WARNING,
316 "suggest not using #elif in traditional C");
317 else if (indented && dir->origin == KANDR)
318 cpp_error (pfile, CPP_DL_WARNING,
319 "traditional C ignores #%s with the # indented",
320 dir->name);
321 else if (!indented && dir->origin != KANDR)
322 cpp_error (pfile, CPP_DL_WARNING,
323 "suggest hiding #%s from traditional C with an indented #",
324 dir->name);
325 }
326 }
If the directive is valid, it is saved into directive slot of cpp_reader. And the directive is executed by invoking the operation handler. For our case, is do_define, which has core invocation to _cpp_create_definition (refer to section Create macro definition – ISO mode).
_cpp_handle_directive (continue)
419 pfile->directive = dir;
420 if (CPP_OPTION (pfile, traditional))
421 prepare_directive_trad (pfile);
422
423 if (dir)
424 pfile->directive->handler (pfile);
425 else if (skip == 0)
426 _cpp_backup_tokens (pfile, 1);
427
428 end_directive (pfile, skip);
429 if (was_parsing_args)
430 {
431 /* Restore state when within macro args. */
432 pfile->state.parsing_args = 2;
433 pfile->state.prevent_expansion = 1;
434 }
435 return skip;
436 }
If _cpp_handle_directive can handle the directive and go ahead, it returns nonzero value, otherwise it returns zero which usualy means error status or meeting assembler code. So for normal case after returning from _cpp_handle_directive at line 721 above in _cpp_lex_token, the function fetches next token following the macro definition. This time the token is never regarded as directive, it will be returned back to cpp_get_token.
cpp_get_token (continue)
1085 if (pfile->state.in_directive && result->type == CPP_COMMENT)
1086 continue;
1087
1088 if (result->type != CPP_NAME)
1089 break;
1090
1091 node = result->val.node;
1092
1093 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1094 break;
1095
1096 if (!(node->flags & NODE_DISABLED))
1097 {
1098 if (!pfile->state.prevent_expansion
1099 && enter_macro_context (pfile, node))
1100 {
1101 if (pfile->state.in_directive)
1102 continue;
1103 return padding_token (pfile, result);
1104 }
1105 }
1106 else
1107 {
1108 /* Flag this token as always unexpandable. FIXME: move this
1109 to collect_args()?. */
1110 cpp_token *t = _cpp_temp_token (pfile);
1111 t->type = result->type;
1112 t->flags = result->flags | NO_EXPAND;
1113 t->val.str = result->val.str;
1114 result = t;
1115 }
1116
1117 break;
1118 }
1119
1120 return result;
1121 }