Mercurial > hg > CbC > CbC_gcc
diff libcpp/traditional.c @ 111:04ced10e8804
gcc 7
author | kono |
---|---|
date | Fri, 27 Oct 2017 22:46:09 +0900 |
parents | a06113de4d67 |
children | 84e7813d76e9 |
line wrap: on
line diff
--- a/libcpp/traditional.c Sun Aug 21 07:07:55 2011 +0900 +++ b/libcpp/traditional.c Fri Oct 27 22:46:09 2017 +0900 @@ -1,6 +1,5 @@ /* CPP Library - traditional lexical analysis and macro expansion. - Copyright (C) 2002, 2004, 2005, 2007, 2008, 2009 - Free Software Foundation, Inc. + Copyright (C) 2002-2017 Free Software Foundation, Inc. Contributed by Neil Booth, May 2002 This program is free software; you can redistribute it and/or modify it @@ -63,6 +62,9 @@ /* The line the macro name appeared on. */ source_location line; + /* Number of parameters. */ + unsigned int paramc; + /* Zero-based index of argument being currently lexed. */ unsigned int argc; }; @@ -75,7 +77,9 @@ ls_defined_close, /* Looking for ')' of defined(). */ ls_hash, /* After # in preprocessor conditional. */ ls_predicate, /* After the predicate, maybe paren? */ - ls_answer}; /* In answer to predicate. */ + ls_answer, /* In answer to predicate. */ + ls_has_include, /* After __has_include__. */ + ls_has_include_close}; /* Looking for ')' of __has_include__. */ /* Lexing TODO: Maybe handle space in escaped newlines. Stop lex.c from recognizing comments and directives during its lexing pass. */ @@ -303,24 +307,41 @@ if (pfile->buffer->need_line && !_cpp_get_fresh_line (pfile)) return false; } - while (!_cpp_scan_out_logical_line (pfile, NULL) || pfile->state.skipping); + while (!_cpp_scan_out_logical_line (pfile, NULL, false) + || pfile->state.skipping); return pfile->buffer != NULL; } +/* Return true if NODE is a fun_like macro. */ +static inline bool +fun_like_macro (cpp_hashnode *node) +{ + if (node->flags & NODE_BUILTIN) + return node->value.builtin == BT_HAS_ATTRIBUTE; + else + return node->value.macro->fun_like; +} + /* Set up state for finding the opening '(' of a function-like macro. */ static void -maybe_start_funlike (cpp_reader *pfile, cpp_hashnode *node, const uchar *start, struct fun_macro *macro) +maybe_start_funlike (cpp_reader *pfile, cpp_hashnode *node, const uchar *start, + struct fun_macro *macro) { - unsigned int n = node->value.macro->paramc + 1; + unsigned int n; + if (node->flags & NODE_BUILTIN) + n = 1; + else + n = node->value.macro->paramc; if (macro->buff) _cpp_release_buff (pfile, macro->buff); - macro->buff = _cpp_get_buff (pfile, n * sizeof (size_t)); + macro->buff = _cpp_get_buff (pfile, (n + 1) * sizeof (size_t)); macro->args = (size_t *) BUFF_FRONT (macro->buff); macro->node = node; macro->offset = start - pfile->out.base; + macro->paramc = n; macro->argc = 0; } @@ -329,7 +350,7 @@ save_argument (struct fun_macro *macro, size_t offset) { macro->argc++; - if (macro->argc <= macro->node->value.macro->paramc) + if (macro->argc <= macro->paramc) macro->args[macro->argc] = offset; } @@ -339,9 +360,13 @@ If MACRO is non-NULL, then we are scanning the replacement list of MACRO, and we call save_replacement_text() every time we meet an - argument. */ + argument. + + If BUILTIN_MACRO_ARG is true, this is called to macro expand + arguments of builtin function-like macros. */ bool -_cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro) +_cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro, + bool builtin_macro_arg) { bool result = true; cpp_context *context; @@ -358,14 +383,18 @@ fmacro.node = NULL; fmacro.offset = 0; fmacro.line = 0; + fmacro.paramc = 0; fmacro.argc = 0; quote = 0; header_ok = pfile->state.angled_headers; CUR (pfile->context) = pfile->buffer->cur; RLIMIT (pfile->context) = pfile->buffer->rlimit; - pfile->out.cur = pfile->out.base; - pfile->out.first_line = pfile->line_table->highest_line; + if (!builtin_macro_arg) + { + pfile->out.cur = pfile->out.base; + pfile->out.first_line = pfile->line_table->highest_line; + } /* start_of_input_line is needed to make sure that directives really, really start at the first character of the line. */ start_of_input_line = pfile->buffer->cur; @@ -378,6 +407,7 @@ for (;;) { if (!context->prev + && !builtin_macro_arg && cur >= pfile->buffer->notes[pfile->buffer->cur_note].pos) { pfile->buffer->cur = cur; @@ -409,6 +439,8 @@ /* Omit the newline from the output buffer. */ pfile->out.cur = out - 1; pfile->buffer->cur = cur; + if (builtin_macro_arg) + goto done; pfile->buffer->need_line = true; CPP_INCREMENT_LINE (pfile, 0); @@ -488,8 +520,7 @@ { /* Macros invalidate MI optimization. */ pfile->mi_valid = false; - if (! (node->flags & NODE_BUILTIN) - && node->value.macro->fun_like) + if (fun_like_macro (node)) { maybe_start_funlike (pfile, node, out_start, &fmacro); lex_state = ls_fun_open; @@ -525,6 +556,13 @@ lex_state = ls_defined; continue; } + else if (pfile->state.in_expression + && (node == pfile->spec_nodes.n__has_include__ + || node == pfile->spec_nodes.n__has_include_next__)) + { + lex_state = ls_has_include; + continue; + } } break; @@ -548,6 +586,8 @@ lex_state = ls_answer; else if (lex_state == ls_defined) lex_state = ls_defined_close; + else if (lex_state == ls_has_include) + lex_state = ls_has_include_close; } break; @@ -562,6 +602,103 @@ paren_depth--; if (lex_state == ls_fun_close && paren_depth == 0) { + if (fmacro.node->flags & NODE_BUILTIN) + { + /* Handle builtin function-like macros like + __has_attribute. The already parsed arguments + are put into a buffer, which is then preprocessed + and the result is fed to _cpp_push_text_context + with disabled expansion, where the ISO preprocessor + parses it. While in traditional preprocessing + macro arguments aren't immediately expanded, they in + the end are because the macro with replaced arguments + is preprocessed again. For the builtin function-like + macros we need the argument immediately though, + if we don't preprocess them, they would behave + very differently from ISO preprocessor handling + of those builtin macros. So, this handling is + more similar to traditional preprocessing of + #if directives, where we also keep preprocessing + until everything is expanded, and then feed the + result with disabled expansion to ISO preprocessor + for handling the directives. */ + lex_state = ls_none; + save_argument (&fmacro, out - pfile->out.base); + cpp_macro m; + memset (&m, '\0', sizeof (m)); + m.paramc = fmacro.paramc; + if (_cpp_arguments_ok (pfile, &m, fmacro.node, + fmacro.argc)) + { + size_t len = fmacro.args[1] - fmacro.args[0]; + uchar *buf; + + /* Remove the macro's invocation from the + output, and push its replacement text. */ + pfile->out.cur = pfile->out.base + fmacro.offset; + CUR (context) = cur; + buf = _cpp_unaligned_alloc (pfile, len + 2); + buf[0] = '('; + memcpy (buf + 1, pfile->out.base + fmacro.args[0], + len); + buf[len + 1] = '\n'; + + const unsigned char *ctx_rlimit = RLIMIT (context); + const unsigned char *saved_cur = pfile->buffer->cur; + const unsigned char *saved_rlimit + = pfile->buffer->rlimit; + const unsigned char *saved_line_base + = pfile->buffer->line_base; + bool saved_need_line = pfile->buffer->need_line; + cpp_buffer *saved_overlaid_buffer + = pfile->overlaid_buffer; + pfile->buffer->cur = buf; + pfile->buffer->line_base = buf; + pfile->buffer->rlimit = buf + len + 1; + pfile->buffer->need_line = false; + pfile->overlaid_buffer = pfile->buffer; + bool saved_in_directive = pfile->state.in_directive; + pfile->state.in_directive = true; + cpp_context *saved_prev_context = context->prev; + context->prev = NULL; + + _cpp_scan_out_logical_line (pfile, NULL, true); + + pfile->state.in_directive = saved_in_directive; + check_output_buffer (pfile, 1); + *pfile->out.cur = '\n'; + pfile->buffer->cur = pfile->out.base + fmacro.offset; + pfile->buffer->line_base = pfile->buffer->cur; + pfile->buffer->rlimit = pfile->out.cur; + CUR (context) = pfile->buffer->cur; + RLIMIT (context) = pfile->buffer->rlimit; + + pfile->state.prevent_expansion++; + const uchar *text + = _cpp_builtin_macro_text (pfile, fmacro.node); + pfile->state.prevent_expansion--; + + context->prev = saved_prev_context; + pfile->buffer->cur = saved_cur; + pfile->buffer->rlimit = saved_rlimit; + pfile->buffer->line_base = saved_line_base; + pfile->buffer->need_line = saved_need_line; + pfile->overlaid_buffer = saved_overlaid_buffer; + pfile->out.cur = pfile->out.base + fmacro.offset; + CUR (context) = cur; + RLIMIT (context) = ctx_rlimit; + len = ustrlen (text); + buf = _cpp_unaligned_alloc (pfile, len + 1); + memcpy (buf, text, len); + buf[len] = '\n'; + text = buf; + _cpp_push_text_context (pfile, fmacro.node, + text, len); + goto new_context; + } + break; + } + cpp_macro *m = fmacro.node->value.macro; m->used = 1; @@ -578,14 +715,14 @@ { /* Remove the macro's invocation from the output, and push its replacement text. */ - pfile->out.cur = (pfile->out.base - + fmacro.offset); + pfile->out.cur = pfile->out.base + fmacro.offset; CUR (context) = cur; replace_args_and_push (pfile, &fmacro); goto new_context; } } - else if (lex_state == ls_answer || lex_state == ls_defined_close) + else if (lex_state == ls_answer || lex_state == ls_defined_close + || lex_state == ls_has_include_close) lex_state = ls_none; } break; @@ -666,7 +803,8 @@ lex_state = ls_none; else if (lex_state == ls_hash || lex_state == ls_predicate - || lex_state == ls_defined) + || lex_state == ls_defined + || lex_state == ls_has_include) lex_state = ls_none; /* ls_answer and ls_defined_close keep going until ')'. */ @@ -699,7 +837,7 @@ len = ustrlen (text); buf = _cpp_unaligned_alloc (pfile, len + 1); memcpy (buf, text, len); - buf[len]='\n'; + buf[len] = '\n'; text = buf; } else @@ -730,7 +868,7 @@ detect true recursion; instead we assume any expansion more than 20 deep since the first invocation of this macro must be recursing. */ - if (recursing && node->value.macro->fun_like) + if (recursing && fun_like_macro (node)) { size_t depth = 0; cpp_context *context = pfile->context; @@ -738,7 +876,7 @@ do { depth++; - if (context->macro == node && depth > 20) + if (context->c.macro == node && depth > 20) break; context = context->prev; } @@ -947,8 +1085,9 @@ if (is_idstart (*cur)) { + struct cpp_hashnode *id = lex_identifier (pfile, cur); ok = false; - if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur))) + if (_cpp_save_parameter (pfile, macro, id, id)) break; cur = skip_whitespace (pfile, CUR (pfile->context), true /* skip_comments */); @@ -1067,7 +1206,7 @@ CPP_OPTION (pfile, discard_comments_in_macro_exp)); pfile->state.prevent_expansion++; - _cpp_scan_out_logical_line (pfile, macro); + _cpp_scan_out_logical_line (pfile, macro, false); pfile->state.prevent_expansion--; if (!macro)