comparison libcpp/macro.c @ 55:77e2b8dfacca gcc-4.4.5

update it from 4.4.3 to 4.5.0
author ryoma <e075725@ie.u-ryukyu.ac.jp>
date Fri, 12 Feb 2010 23:39:51 +0900
parents 855418dad1a3
children b7f97abdc517
comparison
equal deleted inserted replaced
52:c156f1bd5cd9 55:77e2b8dfacca
367 { 367 {
368 const cpp_token *token = arg->first[i]; 368 const cpp_token *token = arg->first[i];
369 369
370 if (token->type == CPP_PADDING) 370 if (token->type == CPP_PADDING)
371 { 371 {
372 if (source == NULL) 372 if (source == NULL
373 || (!(source->flags & PREV_WHITE)
374 && token->val.source == NULL))
373 source = token->val.source; 375 source = token->val.source;
374 continue; 376 continue;
375 } 377 }
376 378
377 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR 379 escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
378 || token->type == CPP_WSTRING || token->type == CPP_WCHAR 380 || token->type == CPP_WSTRING || token->type == CPP_WCHAR
379 || token->type == CPP_STRING32 || token->type == CPP_CHAR32 381 || token->type == CPP_STRING32 || token->type == CPP_CHAR32
380 || token->type == CPP_STRING16 || token->type == CPP_CHAR16); 382 || token->type == CPP_STRING16 || token->type == CPP_CHAR16
383 || token->type == CPP_UTF8STRING);
381 384
382 /* Room for each char being written in octal, initial space and 385 /* Room for each char being written in octal, initial space and
383 final quote and NUL. */ 386 final quote and NUL. */
384 len = cpp_token_len (token); 387 len = cpp_token_len (token);
385 if (escape_it) 388 if (escape_it)
798 } 801 }
799 802
800 return NULL; 803 return NULL;
801 } 804 }
802 805
806 /* Return the real number of tokens in the expansion of MACRO. */
807 static inline unsigned int
808 macro_real_token_count (const cpp_macro *macro)
809 {
810 unsigned int i;
811 if (__builtin_expect (!macro->extra_tokens, true))
812 return macro->count;
813 for (i = 0; i < macro->count; i++)
814 if (macro->exp.tokens[i].type == CPP_PASTE)
815 return i;
816 abort ();
817 }
818
803 /* Push the context of a macro with hash entry NODE onto the context 819 /* Push the context of a macro with hash entry NODE onto the context
804 stack. If we can successfully expand the macro, we push a context 820 stack. If we can successfully expand the macro, we push a context
805 containing its yet-to-be-rescanned replacement list and return one. 821 containing its yet-to-be-rescanned replacement list and return one.
806 If there were additionally any unexpanded deferred #pragma directives 822 If there were additionally any unexpanded deferred #pragma directives
807 among macro arguments, push another context containing the 823 among macro arguments, push another context containing the
867 node->flags |= NODE_USED; 883 node->flags |= NODE_USED;
868 if (pfile->cb.used_define) 884 if (pfile->cb.used_define)
869 pfile->cb.used_define (pfile, pfile->directive_line, node); 885 pfile->cb.used_define (pfile, pfile->directive_line, node);
870 } 886 }
871 887
888 if (pfile->cb.used)
889 pfile->cb.used (pfile, result->src_loc, node);
890
872 macro->used = 1; 891 macro->used = 1;
873 892
874 if (macro->paramc == 0) 893 if (macro->paramc == 0)
875 _cpp_push_token_context (pfile, node, macro->exp.tokens, macro->count); 894 _cpp_push_token_context (pfile, node, macro->exp.tokens,
895 macro_real_token_count (macro));
876 896
877 if (pragma_buff) 897 if (pragma_buff)
878 { 898 {
879 if (!pfile->state.in_directive) 899 if (!pfile->state.in_directive)
880 _cpp_push_token_context (pfile, NULL, 900 _cpp_push_token_context (pfile, NULL,
910 unsigned int i, total; 930 unsigned int i, total;
911 const cpp_token *src, *limit; 931 const cpp_token *src, *limit;
912 const cpp_token **dest, **first; 932 const cpp_token **dest, **first;
913 macro_arg *arg; 933 macro_arg *arg;
914 _cpp_buff *buff; 934 _cpp_buff *buff;
935 unsigned int count;
915 936
916 /* First, fully macro-expand arguments, calculating the number of 937 /* First, fully macro-expand arguments, calculating the number of
917 tokens in the final expansion as we go. The ordering of the if 938 tokens in the final expansion as we go. The ordering of the if
918 statements below is subtle; we must handle stringification before 939 statements below is subtle; we must handle stringification before
919 pasting. */ 940 pasting. */
920 total = macro->count; 941 count = macro_real_token_count (macro);
921 limit = macro->exp.tokens + macro->count; 942 total = count;
943 limit = macro->exp.tokens + count;
922 944
923 for (src = macro->exp.tokens; src < limit; src++) 945 for (src = macro->exp.tokens; src < limit; src++)
924 if (src->type == CPP_MACRO_ARG) 946 if (src->type == CPP_MACRO_ARG)
925 { 947 {
926 /* Leading and trailing padding tokens. */ 948 /* Leading and trailing padding tokens. */
927 total += 2; 949 total += 2;
928 950
929 /* We have an argument. If it is not being stringified or 951 /* We have an argument. If it is not being stringified or
930 pasted it is macro-replaced before insertion. */ 952 pasted it is macro-replaced before insertion. */
931 arg = &args[src->val.arg_no - 1]; 953 arg = &args[src->val.macro_arg.arg_no - 1];
932 954
933 if (src->flags & STRINGIFY_ARG) 955 if (src->flags & STRINGIFY_ARG)
934 { 956 {
935 if (!arg->stringified) 957 if (!arg->stringified)
936 arg->stringified = stringify_arg (pfile, arg); 958 arg->stringified = stringify_arg (pfile, arg);
962 *dest++ = src; 984 *dest++ = src;
963 continue; 985 continue;
964 } 986 }
965 987
966 paste_flag = 0; 988 paste_flag = 0;
967 arg = &args[src->val.arg_no - 1]; 989 arg = &args[src->val.macro_arg.arg_no - 1];
968 if (src->flags & STRINGIFY_ARG) 990 if (src->flags & STRINGIFY_ARG)
969 count = 1, from = &arg->stringified; 991 count = 1, from = &arg->stringified;
970 else if (src->flags & PASTE_LEFT) 992 else if (src->flags & PASTE_LEFT)
971 count = arg->count, from = arg->first; 993 count = arg->count, from = arg->first;
972 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)) 994 else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
974 count = arg->count, from = arg->first; 996 count = arg->count, from = arg->first;
975 if (dest != first) 997 if (dest != first)
976 { 998 {
977 if (dest[-1]->type == CPP_COMMA 999 if (dest[-1]->type == CPP_COMMA
978 && macro->variadic 1000 && macro->variadic
979 && src->val.arg_no == macro->paramc) 1001 && src->val.macro_arg.arg_no == macro->paramc)
980 { 1002 {
981 /* Swallow a pasted comma if from == NULL, otherwise 1003 /* Swallow a pasted comma if from == NULL, otherwise
982 drop the paste flag. */ 1004 drop the paste flag. */
983 if (from == NULL) 1005 if (from == NULL)
984 dest--; 1006 dest--;
1015 cpp_error (pfile, CPP_DL_PEDWARN, 1037 cpp_error (pfile, CPP_DL_PEDWARN,
1016 "invoking macro %s argument %d: " 1038 "invoking macro %s argument %d: "
1017 "empty macro arguments are undefined" 1039 "empty macro arguments are undefined"
1018 " in ISO C90 and ISO C++98", 1040 " in ISO C90 and ISO C++98",
1019 NODE_NAME (node), 1041 NODE_NAME (node),
1020 src->val.arg_no); 1042 src->val.macro_arg.arg_no);
1021 } 1043 }
1022 1044
1023 /* Avoid paste on RHS (even case count == 0). */ 1045 /* Avoid paste on RHS (even case count == 0). */
1024 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)) 1046 if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
1025 *dest++ = &pfile->avoid_paste; 1047 *dest++ = &pfile->avoid_paste;
1241 continue; 1263 continue;
1242 1264
1243 if (result->type != CPP_NAME) 1265 if (result->type != CPP_NAME)
1244 break; 1266 break;
1245 1267
1246 node = result->val.node; 1268 node = result->val.node.node;
1247 1269
1248 if (node->type != NT_MACRO || (result->flags & NO_EXPAND)) 1270 if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
1249 break; 1271 break;
1250 1272
1251 if (!(node->flags & NODE_DISABLED)) 1273 if (!(node->flags & NODE_DISABLED))
1533 "macro parameters must be comma-separated"); 1555 "macro parameters must be comma-separated");
1534 return false; 1556 return false;
1535 } 1557 }
1536 prev_ident = 1; 1558 prev_ident = 1;
1537 1559
1538 if (_cpp_save_parameter (pfile, macro, token->val.node)) 1560 if (_cpp_save_parameter (pfile, macro, token->val.node.node))
1539 return false; 1561 return false;
1540 continue; 1562 continue;
1541 1563
1542 case CPP_CLOSE_PAREN: 1564 case CPP_CLOSE_PAREN:
1543 if (prev_ident || macro->paramc == 0) 1565 if (prev_ident || macro->paramc == 0)
1606 token = _cpp_lex_direct (pfile); 1628 token = _cpp_lex_direct (pfile);
1607 pfile->cur_token = saved_cur_token; 1629 pfile->cur_token = saved_cur_token;
1608 1630
1609 /* Is this a parameter? */ 1631 /* Is this a parameter? */
1610 if (token->type == CPP_NAME 1632 if (token->type == CPP_NAME
1611 && (token->val.node->flags & NODE_MACRO_ARG) != 0) 1633 && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
1612 { 1634 {
1613 token->type = CPP_MACRO_ARG; 1635 token->type = CPP_MACRO_ARG;
1614 token->val.arg_no = token->val.node->value.arg_index; 1636 token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
1615 } 1637 }
1616 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 1638 else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
1617 && (token->type == CPP_STRING || token->type == CPP_CHAR)) 1639 && (token->type == CPP_STRING || token->type == CPP_CHAR))
1618 check_trad_stringification (pfile, macro, &token->val.str); 1640 check_trad_stringification (pfile, macro, &token->val.str);
1619 1641
1626 cpp_token *token; 1648 cpp_token *token;
1627 const cpp_token *ctoken; 1649 const cpp_token *ctoken;
1628 bool following_paste_op = false; 1650 bool following_paste_op = false;
1629 const char *paste_op_error_msg = 1651 const char *paste_op_error_msg =
1630 N_("'##' cannot appear at either end of a macro expansion"); 1652 N_("'##' cannot appear at either end of a macro expansion");
1653 unsigned int num_extra_tokens = 0;
1631 1654
1632 /* Get the first token of the expansion (or the '(' of a 1655 /* Get the first token of the expansion (or the '(' of a
1633 function-like macro). */ 1656 function-like macro). */
1634 ctoken = _cpp_lex_token (pfile); 1657 ctoken = _cpp_lex_token (pfile);
1635 1658
1703 function-like macros when lexing the subsequent token. */ 1726 function-like macros when lexing the subsequent token. */
1704 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like) 1727 if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
1705 { 1728 {
1706 if (token->type == CPP_MACRO_ARG) 1729 if (token->type == CPP_MACRO_ARG)
1707 { 1730 {
1731 if (token->flags & PREV_WHITE)
1732 token->flags |= SP_PREV_WHITE;
1733 if (token[-1].flags & DIGRAPH)
1734 token->flags |= SP_DIGRAPH;
1708 token->flags &= ~PREV_WHITE; 1735 token->flags &= ~PREV_WHITE;
1709 token->flags |= STRINGIFY_ARG; 1736 token->flags |= STRINGIFY_ARG;
1710 token->flags |= token[-1].flags & PREV_WHITE; 1737 token->flags |= token[-1].flags & PREV_WHITE;
1711 token[-1] = token[0]; 1738 token[-1] = token[0];
1712 macro->count--; 1739 macro->count--;
1742 { 1769 {
1743 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); 1770 cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
1744 return false; 1771 return false;
1745 } 1772 }
1746 1773
1747 --macro->count; 1774 if (token[-1].flags & PASTE_LEFT)
1748 token[-1].flags |= PASTE_LEFT; 1775 {
1776 macro->extra_tokens = 1;
1777 num_extra_tokens++;
1778 token->val.token_no = macro->count - 1;
1779 }
1780 else
1781 {
1782 --macro->count;
1783 token[-1].flags |= PASTE_LEFT;
1784 if (token->flags & DIGRAPH)
1785 token[-1].flags |= SP_DIGRAPH;
1786 if (token->flags & PREV_WHITE)
1787 token[-1].flags |= SP_PREV_WHITE;
1788 }
1749 } 1789 }
1750 1790
1751 following_paste_op = (token->type == CPP_PASTE); 1791 following_paste_op = (token->type == CPP_PASTE);
1752 token = lex_expansion_token (pfile, macro); 1792 token = lex_expansion_token (pfile, macro);
1753 } 1793 }
1766 if (pfile->hash_table->alloc_subobject) 1806 if (pfile->hash_table->alloc_subobject)
1767 { 1807 {
1768 cpp_token *tokns = 1808 cpp_token *tokns =
1769 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token) 1809 (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
1770 * macro->count); 1810 * macro->count);
1771 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count); 1811 if (num_extra_tokens)
1812 {
1813 /* Place second and subsequent ## or %:%: tokens in
1814 sequences of consecutive such tokens at the end of the
1815 list to preserve information about where they appear, how
1816 they are spelt and whether they are preceded by
1817 whitespace without otherwise interfering with macro
1818 expansion. */
1819 cpp_token *normal_dest = tokns;
1820 cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
1821 unsigned int i;
1822 for (i = 0; i < macro->count; i++)
1823 {
1824 if (macro->exp.tokens[i].type == CPP_PASTE)
1825 *extra_dest++ = macro->exp.tokens[i];
1826 else
1827 *normal_dest++ = macro->exp.tokens[i];
1828 }
1829 }
1830 else
1831 memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
1772 macro->exp.tokens = tokns; 1832 macro->exp.tokens = tokns;
1773 } 1833 }
1774 else 1834 else
1775 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count]; 1835 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
1776 1836
1795 macro->paramc = 0; 1855 macro->paramc = 0;
1796 macro->variadic = 0; 1856 macro->variadic = 0;
1797 macro->used = !CPP_OPTION (pfile, warn_unused_macros); 1857 macro->used = !CPP_OPTION (pfile, warn_unused_macros);
1798 macro->count = 0; 1858 macro->count = 0;
1799 macro->fun_like = 0; 1859 macro->fun_like = 0;
1860 macro->extra_tokens = 0;
1800 /* To suppress some diagnostics. */ 1861 /* To suppress some diagnostics. */
1801 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0; 1862 macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
1802 1863
1803 if (CPP_OPTION (pfile, traditional)) 1864 if (CPP_OPTION (pfile, traditional))
1804 ok = _cpp_create_trad_definition (pfile, macro); 1865 ok = _cpp_create_trad_definition (pfile, macro);
1831 if (CPP_OPTION (pfile, warn_unused_macros)) 1892 if (CPP_OPTION (pfile, warn_unused_macros))
1832 _cpp_warn_if_unused_macro (pfile, node, NULL); 1893 _cpp_warn_if_unused_macro (pfile, node, NULL);
1833 1894
1834 if (warn_of_redefinition (pfile, node, macro)) 1895 if (warn_of_redefinition (pfile, node, macro))
1835 { 1896 {
1836 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0, 1897 bool warned;
1837 "\"%s\" redefined", NODE_NAME (node)); 1898 warned = cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1838 1899 pfile->directive_line, 0,
1839 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) 1900 "\"%s\" redefined", NODE_NAME (node));
1840 cpp_error_with_line (pfile, CPP_DL_PEDWARN, 1901
1902 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
1903 cpp_error_with_line (pfile, CPP_DL_NOTE,
1841 node->value.macro->line, 0, 1904 node->value.macro->line, 0,
1842 "this is the location of the previous definition"); 1905 "this is the location of the previous definition");
1843 } 1906 }
1844 } 1907 }
1845 1908
1940 /* This should match below where we fill in the buffer. */ 2003 /* This should match below where we fill in the buffer. */
1941 if (CPP_OPTION (pfile, traditional)) 2004 if (CPP_OPTION (pfile, traditional))
1942 len += _cpp_replacement_text_len (macro); 2005 len += _cpp_replacement_text_len (macro);
1943 else 2006 else
1944 { 2007 {
1945 for (i = 0; i < macro->count; i++) 2008 unsigned int count = macro_real_token_count (macro);
2009 for (i = 0; i < count; i++)
1946 { 2010 {
1947 cpp_token *token = &macro->exp.tokens[i]; 2011 cpp_token *token = &macro->exp.tokens[i];
1948 2012
1949 if (token->type == CPP_MACRO_ARG) 2013 if (token->type == CPP_MACRO_ARG)
1950 len += NODE_LEN (macro->params[token->val.arg_no - 1]); 2014 len += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
1951 else 2015 else
1952 len += cpp_token_len (token); 2016 len += cpp_token_len (token);
1953 2017
1954 if (token->flags & STRINGIFY_ARG) 2018 if (token->flags & STRINGIFY_ARG)
1955 len++; /* "#" */ 2019 len++; /* "#" */
2004 if (CPP_OPTION (pfile, traditional)) 2068 if (CPP_OPTION (pfile, traditional))
2005 buffer = _cpp_copy_replacement_text (macro, buffer); 2069 buffer = _cpp_copy_replacement_text (macro, buffer);
2006 else if (macro->count) 2070 else if (macro->count)
2007 /* Expansion tokens. */ 2071 /* Expansion tokens. */
2008 { 2072 {
2009 for (i = 0; i < macro->count; i++) 2073 unsigned int count = macro_real_token_count (macro);
2074 for (i = 0; i < count; i++)
2010 { 2075 {
2011 cpp_token *token = &macro->exp.tokens[i]; 2076 cpp_token *token = &macro->exp.tokens[i];
2012 2077
2013 if (token->flags & PREV_WHITE) 2078 if (token->flags & PREV_WHITE)
2014 *buffer++ = ' '; 2079 *buffer++ = ' ';
2016 *buffer++ = '#'; 2081 *buffer++ = '#';
2017 2082
2018 if (token->type == CPP_MACRO_ARG) 2083 if (token->type == CPP_MACRO_ARG)
2019 { 2084 {
2020 memcpy (buffer, 2085 memcpy (buffer,
2021 NODE_NAME (macro->params[token->val.arg_no - 1]), 2086 NODE_NAME (macro->params[token->val.macro_arg.arg_no - 1]),
2022 NODE_LEN (macro->params[token->val.arg_no - 1])); 2087 NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]));
2023 buffer += NODE_LEN (macro->params[token->val.arg_no - 1]); 2088 buffer += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
2024 } 2089 }
2025 else 2090 else
2026 buffer = cpp_spell_token (pfile, token, buffer, false); 2091 buffer = cpp_spell_token (pfile, token, buffer, false);
2027 2092
2028 if (token->flags & PASTE_LEFT) 2093 if (token->flags & PASTE_LEFT)