comparison libcpp/directives.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* CPP Library. (Directive handling.) 1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2 Copyright (C) 1986-2017 Free Software Foundation, Inc.
3 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 Contributed by Per Bothner, 1994-95. 3 Contributed by Per Bothner, 1994-95.
6 Based on CCCP program by Paul Rubin, June 1986 4 Based on CCCP program by Paul Rubin, June 1986
7 Adapted to ANSI C, Richard Stallman, Jan 1987 5 Adapted to ANSI C, Richard Stallman, Jan 1987
8 6
9 This program is free software; you can redistribute it and/or modify it 7 This program is free software; you can redistribute it and/or modify it
30 /* Stack of conditionals currently in progress 28 /* Stack of conditionals currently in progress
31 (including both successful and failing conditionals). */ 29 (including both successful and failing conditionals). */
32 struct if_stack 30 struct if_stack
33 { 31 {
34 struct if_stack *next; 32 struct if_stack *next;
35 linenum_type line; /* Line where condition started. */ 33 source_location line; /* Line where condition started. */
36 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ 34 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
37 bool skip_elses; /* Can future #else / #elif be skipped? */ 35 bool skip_elses; /* Can future #else / #elif be skipped? */
38 bool was_skipping; /* If were skipping on entry. */ 36 bool was_skipping; /* If were skipping on entry. */
39 int type; /* Most recent conditional for diagnostics. */ 37 int type; /* Most recent conditional for diagnostics. */
40 }; 38 };
116 char **); 114 char **);
117 static void do_pragma_once (cpp_reader *); 115 static void do_pragma_once (cpp_reader *);
118 static void do_pragma_poison (cpp_reader *); 116 static void do_pragma_poison (cpp_reader *);
119 static void do_pragma_system_header (cpp_reader *); 117 static void do_pragma_system_header (cpp_reader *);
120 static void do_pragma_dependency (cpp_reader *); 118 static void do_pragma_dependency (cpp_reader *);
119 static void do_pragma_warning_or_error (cpp_reader *, bool error);
120 static void do_pragma_warning (cpp_reader *);
121 static void do_pragma_error (cpp_reader *);
121 static void do_linemarker (cpp_reader *); 122 static void do_linemarker (cpp_reader *);
122 static const cpp_token *get_token_no_padding (cpp_reader *); 123 static const cpp_token *get_token_no_padding (cpp_reader *);
123 static const cpp_token *get__Pragma_string (cpp_reader *); 124 static const cpp_token *get__Pragma_string (cpp_reader *);
124 static void destringize_and_run (cpp_reader *, const cpp_string *); 125 static void destringize_and_run (cpp_reader *, const cpp_string *,
126 source_location);
125 static int parse_answer (cpp_reader *, struct answer **, int, source_location); 127 static int parse_answer (cpp_reader *, struct answer **, int, source_location);
126 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int); 128 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
127 static struct answer ** find_answer (cpp_hashnode *, const struct answer *); 129 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
128 static void handle_assertion (cpp_reader *, const char *, int); 130 static void handle_assertion (cpp_reader *, const char *, int);
129 static void do_pragma_push_macro (cpp_reader *); 131 static void do_pragma_push_macro (cpp_reader *);
184 static const directive dtable[] = 186 static const directive dtable[] =
185 { 187 {
186 DIRECTIVE_TABLE 188 DIRECTIVE_TABLE
187 }; 189 };
188 #undef D 190 #undef D
191
192 /* A NULL-terminated array of directive names for use
193 when suggesting corrections for misspelled directives. */
194 #define D(name, t, origin, flags) #name,
195 static const char * const directive_names[] = {
196 DIRECTIVE_TABLE
197 NULL
198 };
199 #undef D
200
189 #undef DIRECTIVE_TABLE 201 #undef DIRECTIVE_TABLE
190 202
191 /* Wrapper struct directive for linemarkers. 203 /* Wrapper struct directive for linemarkers.
192 The origin is more or less true - the original K+R cpp 204 The origin is more or less true - the original K+R cpp
193 did use this notation in its preprocessed output. */ 205 did use this notation in its preprocessed output. */
210 if (! SEEN_EOL ()) 222 if (! SEEN_EOL ())
211 while (_cpp_lex_token (pfile)->type != CPP_EOF) 223 while (_cpp_lex_token (pfile)->type != CPP_EOF)
212 ; 224 ;
213 } 225 }
214 226
215 /* Ensure there are no stray tokens at the end of a directive. If 227 /* Helper function for check_oel. */
216 EXPAND is true, tokens macro-expanding to nothing are allowed. */ 228
217 static void 229 static void
218 check_eol (cpp_reader *pfile, bool expand) 230 check_eol_1 (cpp_reader *pfile, bool expand, int reason)
219 { 231 {
220 if (! SEEN_EOL () && (expand 232 if (! SEEN_EOL () && (expand
221 ? cpp_get_token (pfile) 233 ? cpp_get_token (pfile)
222 : _cpp_lex_token (pfile))->type != CPP_EOF) 234 : _cpp_lex_token (pfile))->type != CPP_EOF)
223 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive", 235 cpp_pedwarning (pfile, reason, "extra tokens at end of #%s directive",
224 pfile->directive->name); 236 pfile->directive->name);
237 }
238
239 /* Variant of check_eol used for Wendif-labels warnings. */
240
241 static void
242 check_eol_endif_labels (cpp_reader *pfile)
243 {
244 check_eol_1 (pfile, false, CPP_W_ENDIF_LABELS);
245 }
246
247 /* Ensure there are no stray tokens at the end of a directive. If
248 EXPAND is true, tokens macro-expanding to nothing are allowed. */
249
250 static void
251 check_eol (cpp_reader *pfile, bool expand)
252 {
253 check_eol_1 (pfile, expand, CPP_W_NONE);
225 } 254 }
226 255
227 /* Ensure there are no stray tokens other than comments at the end of 256 /* Ensure there are no stray tokens other than comments at the end of
228 a directive, and gather the comments. */ 257 a directive, and gather the comments. */
229 static const cpp_token ** 258 static const cpp_token **
326 if (pfile->state.in_expression) 355 if (pfile->state.in_expression)
327 pfile->state.skipping = false; 356 pfile->state.skipping = false;
328 357
329 if (no_expand) 358 if (no_expand)
330 pfile->state.prevent_expansion++; 359 pfile->state.prevent_expansion++;
331 _cpp_scan_out_logical_line (pfile, NULL); 360 _cpp_scan_out_logical_line (pfile, NULL, false);
332 if (no_expand) 361 if (no_expand)
333 pfile->state.prevent_expansion--; 362 pfile->state.prevent_expansion--;
334 363
335 pfile->state.skipping = was_skipping; 364 pfile->state.skipping = was_skipping;
336 _cpp_overlay_buffer (pfile, pfile->out.base, 365 _cpp_overlay_buffer (pfile, pfile->out.base,
444 -fpreprocessed mode only if the # is in column 1. macro.c 473 -fpreprocessed mode only if the # is in column 1. macro.c
445 puts a space in front of any '#' at the start of a macro. 474 puts a space in front of any '#' at the start of a macro.
446 475
447 We exclude the -fdirectives-only case because macro expansion 476 We exclude the -fdirectives-only case because macro expansion
448 has not been performed yet, and block comments can cause spaces 477 has not been performed yet, and block comments can cause spaces
449 to preceed the directive. */ 478 to precede the directive. */
450 if (CPP_OPTION (pfile, preprocessed) 479 if (CPP_OPTION (pfile, preprocessed)
451 && !CPP_OPTION (pfile, directives_only) 480 && !CPP_OPTION (pfile, directives_only)
452 && (indented || !(dir->flags & IN_I))) 481 && (indented || !(dir->flags & IN_I)))
453 { 482 {
454 skip = 0; 483 skip = 0;
477 introduce assembler pseudo-ops. Don't complain about invalid 506 introduce assembler pseudo-ops. Don't complain about invalid
478 directives in skipped conditional groups (6.10 p4). */ 507 directives in skipped conditional groups (6.10 p4). */
479 if (CPP_OPTION (pfile, lang) == CLK_ASM) 508 if (CPP_OPTION (pfile, lang) == CLK_ASM)
480 skip = 0; 509 skip = 0;
481 else if (!pfile->state.skipping) 510 else if (!pfile->state.skipping)
482 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s", 511 {
483 cpp_token_as_text (pfile, dname)); 512 const char *unrecognized
513 = (const char *)cpp_token_as_text (pfile, dname);
514 const char *hint = NULL;
515
516 /* Call back into gcc to get a spelling suggestion. Ideally
517 we'd just use best_match from gcc/spellcheck.h (and filter
518 out the uncommon directives), but that requires moving it
519 to a support library. */
520 if (pfile->cb.get_suggestion)
521 hint = pfile->cb.get_suggestion (pfile, unrecognized,
522 directive_names);
523
524 if (hint)
525 {
526 rich_location richloc (pfile->line_table, dname->src_loc);
527 source_range misspelled_token_range
528 = get_range_from_loc (pfile->line_table, dname->src_loc);
529 richloc.add_fixit_replace (misspelled_token_range, hint);
530 cpp_error_at_richloc (pfile, CPP_DL_ERROR, &richloc,
531 "invalid preprocessing directive #%s;"
532 " did you mean #%s?",
533 unrecognized, hint);
534 }
535 else
536 cpp_error (pfile, CPP_DL_ERROR,
537 "invalid preprocessing directive #%s",
538 unrecognized);
539 }
484 } 540 }
485 541
486 pfile->directive = dir; 542 pfile->directive = dir;
487 if (CPP_OPTION (pfile, traditional)) 543 if (CPP_OPTION (pfile, traditional))
488 prepare_directive_trad (pfile); 544 prepare_directive_trad (pfile);
546 cpp_hashnode *node = token->val.node.node; 602 cpp_hashnode *node = token->val.node.node;
547 603
548 if (is_def_or_undef && node == pfile->spec_nodes.n_defined) 604 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
549 cpp_error (pfile, CPP_DL_ERROR, 605 cpp_error (pfile, CPP_DL_ERROR,
550 "\"defined\" cannot be used as a macro name"); 606 "\"defined\" cannot be used as a macro name");
607 else if (is_def_or_undef
608 && (node == pfile->spec_nodes.n__has_include__
609 || node == pfile->spec_nodes.n__has_include_next__))
610 cpp_error (pfile, CPP_DL_ERROR,
611 "\"__has_include__\" cannot be used as a macro name");
551 else if (! (node->flags & NODE_POISONED)) 612 else if (! (node->flags & NODE_POISONED))
552 return node; 613 return node;
553 } 614 }
554 else if (token->flags & NAMED_OP) 615 else if (token->flags & NAMED_OP)
555 cpp_error (pfile, CPP_DL_ERROR, 616 cpp_error (pfile, CPP_DL_ERROR,
607 if (node->type == NT_MACRO) 668 if (node->type == NT_MACRO)
608 { 669 {
609 if (node->flags & NODE_WARN) 670 if (node->flags & NODE_WARN)
610 cpp_error (pfile, CPP_DL_WARNING, 671 cpp_error (pfile, CPP_DL_WARNING,
611 "undefining \"%s\"", NODE_NAME (node)); 672 "undefining \"%s\"", NODE_NAME (node));
673 else if ((node->flags & NODE_BUILTIN)
674 && CPP_OPTION (pfile, warn_builtin_macro_redefined))
675 cpp_warning_with_line (pfile, CPP_W_BUILTIN_MACRO_REDEFINED,
676 pfile->directive_line, 0,
677 "undefining \"%s\"", NODE_NAME (node));
612 678
613 if (CPP_OPTION (pfile, warn_unused_macros)) 679 if (CPP_OPTION (pfile, warn_unused_macros))
614 _cpp_warn_if_unused_macro (pfile, node, NULL); 680 _cpp_warn_if_unused_macro (pfile, node, NULL);
615 681
616 _cpp_free_definition (node); 682 _cpp_free_definition (node);
787 if (pfile->cb.include) 853 if (pfile->cb.include)
788 pfile->cb.include (pfile, pfile->directive_line, 854 pfile->cb.include (pfile, pfile->directive_line,
789 pfile->directive->name, fname, angle_brackets, 855 pfile->directive->name, fname, angle_brackets,
790 buf); 856 buf);
791 857
792 _cpp_stack_include (pfile, fname, angle_brackets, type); 858 _cpp_stack_include (pfile, fname, angle_brackets, type, location);
793 } 859 }
794 860
795 XDELETEVEC (fname); 861 XDELETEVEC (fname);
796 if (buf) 862 if (buf)
797 XDELETEVEC (buf); 863 XDELETEVEC (buf);
881 Note that the filename string (if any) is a true string constant 947 Note that the filename string (if any) is a true string constant
882 (escapes are interpreted), unlike in #line. */ 948 (escapes are interpreted), unlike in #line. */
883 static void 949 static void
884 do_line (cpp_reader *pfile) 950 do_line (cpp_reader *pfile)
885 { 951 {
886 const struct line_maps *line_table = pfile->line_table; 952 struct line_maps *line_table = pfile->line_table;
887 const struct line_map *map = &line_table->maps[line_table->used - 1]; 953 const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
888 954
889 /* skip_rest_of_line() may cause line table to be realloc()ed so note down 955 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
890 sysp right now. */ 956 sysp right now. */
891 957
892 unsigned char map_sysp = map->sysp; 958 unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
893 const cpp_token *token; 959 const cpp_token *token;
894 const char *new_file = map->to_file; 960 const char *new_file = ORDINARY_MAP_FILE_NAME (map);
895 linenum_type new_lineno; 961 linenum_type new_lineno;
896 962
897 /* C99 raised the minimum limit on #line numbers. */ 963 /* C99 raised the minimum limit on #line numbers. */
898 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; 964 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
899 bool wrapped; 965 bool wrapped;
935 } 1001 }
936 1002
937 skip_rest_of_line (pfile); 1003 skip_rest_of_line (pfile);
938 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno, 1004 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
939 map_sysp); 1005 map_sysp);
1006 line_table->seen_line_directive = true;
940 } 1007 }
941 1008
942 /* Interpret the # 44 "file" [flags] notation, which has slightly 1009 /* Interpret the # 44 "file" [flags] notation, which has slightly
943 different syntax and semantics from #line: Flags are allowed, 1010 different syntax and semantics from #line: Flags are allowed,
944 and we never complain about the line number being too big. */ 1011 and we never complain about the line number being too big. */
945 static void 1012 static void
946 do_linemarker (cpp_reader *pfile) 1013 do_linemarker (cpp_reader *pfile)
947 { 1014 {
948 const struct line_maps *line_table = pfile->line_table; 1015 struct line_maps *line_table = pfile->line_table;
949 const struct line_map *map = &line_table->maps[line_table->used - 1]; 1016 const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
950 const cpp_token *token; 1017 const cpp_token *token;
951 const char *new_file = map->to_file; 1018 const char *new_file = ORDINARY_MAP_FILE_NAME (map);
952 linenum_type new_lineno; 1019 linenum_type new_lineno;
953 unsigned int new_sysp = map->sysp; 1020 unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
954 enum lc_reason reason = LC_RENAME_VERBATIM; 1021 enum lc_reason reason = LC_RENAME_VERBATIM;
955 int flag; 1022 int flag;
956 bool wrapped; 1023 bool wrapped;
957 1024
958 /* Back up so we can get the number again. Putting this in 1025 /* Back up so we can get the number again. Putting this in
1014 return; 1081 return;
1015 } 1082 }
1016 1083
1017 skip_rest_of_line (pfile); 1084 skip_rest_of_line (pfile);
1018 1085
1086 if (reason == LC_LEAVE)
1087 {
1088 /* Reread map since cpp_get_token can invalidate it with a
1089 reallocation. */
1090 map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
1091 const line_map_ordinary *from;
1092 if (MAIN_FILE_P (map)
1093 || (new_file
1094 && (from = INCLUDED_FROM (pfile->line_table, map)) != NULL
1095 && filename_cmp (ORDINARY_MAP_FILE_NAME (from), new_file) != 0))
1096 {
1097 cpp_warning (pfile, CPP_W_NONE,
1098 "file \"%s\" linemarker ignored due to "
1099 "incorrect nesting", new_file);
1100 return;
1101 }
1102 }
1019 /* Compensate for the increment in linemap_add that occurs in 1103 /* Compensate for the increment in linemap_add that occurs in
1020 _cpp_do_file_change. We're currently at the start of the line 1104 _cpp_do_file_change. We're currently at the start of the line
1021 *following* the #line directive. A separate source_location for this 1105 *following* the #line directive. A separate source_location for this
1022 location makes no sense (until we do the LC_LEAVE), and 1106 location makes no sense (until we do the LC_LEAVE), and
1023 complicates LAST_SOURCE_LINE_LOCATION. */ 1107 complicates LAST_SOURCE_LINE_LOCATION. */
1024 pfile->line_table->highest_location--; 1108 pfile->line_table->highest_location--;
1025 1109
1026 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); 1110 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
1111 line_table->seen_line_directive = true;
1027 } 1112 }
1028 1113
1029 /* Arrange the file_change callback. pfile->line has changed to 1114 /* Arrange the file_change callback. pfile->line has changed to
1030 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system 1115 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1031 header, 2 for a system header that needs to be extern "C" protected, 1116 header, 2 for a system header that needs to be extern "C" protected,
1033 void 1118 void
1034 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, 1119 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
1035 const char *to_file, linenum_type file_line, 1120 const char *to_file, linenum_type file_line,
1036 unsigned int sysp) 1121 unsigned int sysp)
1037 { 1122 {
1123 linemap_assert (reason != LC_ENTER_MACRO);
1038 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp, 1124 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1039 to_file, file_line); 1125 to_file, file_line);
1126 const line_map_ordinary *ord_map = NULL;
1040 if (map != NULL) 1127 if (map != NULL)
1041 linemap_line_start (pfile->line_table, map->to_line, 127); 1128 {
1129 ord_map = linemap_check_ordinary (map);
1130 linemap_line_start (pfile->line_table,
1131 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map),
1132 127);
1133 }
1042 1134
1043 if (pfile->cb.file_change) 1135 if (pfile->cb.file_change)
1044 pfile->cb.file_change (pfile, map); 1136 pfile->cb.file_change (pfile, ord_map);
1045 } 1137 }
1046 1138
1047 /* Report a warning or error detected by the program we are 1139 /* Report a warning or error detected by the program we are
1048 processing. Use the directive's tokens in the error message. */ 1140 processing. Use the directive's tokens in the error message. */
1049 static void 1141 static void
1259 /* New GCC-specific pragmas should be put in the GCC namespace. */ 1351 /* New GCC-specific pragmas should be put in the GCC namespace. */
1260 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison); 1352 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1261 register_pragma_internal (pfile, "GCC", "system_header", 1353 register_pragma_internal (pfile, "GCC", "system_header",
1262 do_pragma_system_header); 1354 do_pragma_system_header);
1263 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency); 1355 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1356 register_pragma_internal (pfile, "GCC", "warning", do_pragma_warning);
1357 register_pragma_internal (pfile, "GCC", "error", do_pragma_error);
1264 } 1358 }
1265 1359
1266 /* Return the number of registered pragmas in PE. */ 1360 /* Return the number of registered pragmas in PE. */
1267 1361
1268 static int 1362 static int
1343 specifies that macro expansion should happen. */ 1437 specifies that macro expansion should happen. */
1344 static void 1438 static void
1345 do_pragma (cpp_reader *pfile) 1439 do_pragma (cpp_reader *pfile)
1346 { 1440 {
1347 const struct pragma_entry *p = NULL; 1441 const struct pragma_entry *p = NULL;
1348 const cpp_token *token, *pragma_token = pfile->cur_token; 1442 const cpp_token *token, *pragma_token;
1443 source_location pragma_token_virt_loc = 0;
1349 cpp_token ns_token; 1444 cpp_token ns_token;
1350 unsigned int count = 1; 1445 unsigned int count = 1;
1351 1446
1352 pfile->state.prevent_expansion++; 1447 pfile->state.prevent_expansion++;
1353 1448
1354 token = cpp_get_token (pfile); 1449 pragma_token = token = cpp_get_token_with_location (pfile,
1450 &pragma_token_virt_loc);
1355 ns_token = *token; 1451 ns_token = *token;
1356 if (token->type == CPP_NAME) 1452 if (token->type == CPP_NAME)
1357 { 1453 {
1358 p = lookup_pragma_entry (pfile->pragmas, token->val.node.node); 1454 p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
1359 if (p && p->is_nspace) 1455 if (p && p->is_nspace)
1360 { 1456 {
1361 bool allow_name_expansion = p->allow_expansion; 1457 bool allow_name_expansion = p->allow_expansion;
1362 if (allow_name_expansion) 1458 if (allow_name_expansion)
1363 pfile->state.prevent_expansion--; 1459 pfile->state.prevent_expansion--;
1460
1364 token = cpp_get_token (pfile); 1461 token = cpp_get_token (pfile);
1365 if (token->type == CPP_NAME) 1462 if (token->type == CPP_NAME)
1366 p = lookup_pragma_entry (p->u.space, token->val.node.node); 1463 p = lookup_pragma_entry (p->u.space, token->val.node.node);
1367 else 1464 else
1368 p = NULL; 1465 p = NULL;
1374 1471
1375 if (p) 1472 if (p)
1376 { 1473 {
1377 if (p->is_deferred) 1474 if (p->is_deferred)
1378 { 1475 {
1379 pfile->directive_result.src_loc = pragma_token->src_loc; 1476 pfile->directive_result.src_loc = pragma_token_virt_loc;
1380 pfile->directive_result.type = CPP_PRAGMA; 1477 pfile->directive_result.type = CPP_PRAGMA;
1381 pfile->directive_result.flags = pragma_token->flags; 1478 pfile->directive_result.flags = pragma_token->flags;
1382 pfile->directive_result.val.pragma = p->u.ident; 1479 pfile->directive_result.val.pragma = p->u.ident;
1383 pfile->state.in_deferred_pragma = true; 1480 pfile->state.in_deferred_pragma = true;
1384 pfile->state.pragma_allow_expansion = p->allow_expansion; 1481 pfile->state.pragma_allow_expansion = p->allow_expansion;
1627 } 1724 }
1628 1725
1629 free ((void *) fname); 1726 free ((void *) fname);
1630 } 1727 }
1631 1728
1729 /* Issue a diagnostic with the message taken from the pragma. If
1730 ERROR is true, the diagnostic is a warning, otherwise, it is an
1731 error. */
1732 static void
1733 do_pragma_warning_or_error (cpp_reader *pfile, bool error)
1734 {
1735 const cpp_token *tok = _cpp_lex_token (pfile);
1736 cpp_string str;
1737 if (tok->type != CPP_STRING
1738 || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str,
1739 CPP_STRING)
1740 || str.len == 0)
1741 {
1742 cpp_error (pfile, CPP_DL_ERROR, "invalid \"#pragma GCC %s\" directive",
1743 error ? "error" : "warning");
1744 return;
1745 }
1746 cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING,
1747 "%s", str.text);
1748 free ((void *)str.text);
1749 }
1750
1751 /* Issue a warning diagnostic. */
1752 static void
1753 do_pragma_warning (cpp_reader *pfile)
1754 {
1755 do_pragma_warning_or_error (pfile, false);
1756 }
1757
1758 /* Issue an error diagnostic. */
1759 static void
1760 do_pragma_error (cpp_reader *pfile)
1761 {
1762 do_pragma_warning_or_error (pfile, true);
1763 }
1764
1632 /* Get a token but skip padding. */ 1765 /* Get a token but skip padding. */
1633 static const cpp_token * 1766 static const cpp_token *
1634 get_token_no_padding (cpp_reader *pfile) 1767 get_token_no_padding (cpp_reader *pfile)
1635 { 1768 {
1636 for (;;) 1769 for (;;)
1673 } 1806 }
1674 1807
1675 /* Destringize IN into a temporary buffer, by removing the first \ of 1808 /* Destringize IN into a temporary buffer, by removing the first \ of
1676 \" and \\ sequences, and process the result as a #pragma directive. */ 1809 \" and \\ sequences, and process the result as a #pragma directive. */
1677 static void 1810 static void
1678 destringize_and_run (cpp_reader *pfile, const cpp_string *in) 1811 destringize_and_run (cpp_reader *pfile, const cpp_string *in,
1812 source_location expansion_loc)
1679 { 1813 {
1680 const unsigned char *src, *limit; 1814 const unsigned char *src, *limit;
1681 char *dest, *result; 1815 char *dest, *result;
1682 cpp_context *saved_context; 1816 cpp_context *saved_context;
1683 cpp_token *saved_cur_token; 1817 cpp_token *saved_cur_token;
1708 this. */ 1842 this. */
1709 saved_context = pfile->context; 1843 saved_context = pfile->context;
1710 saved_cur_token = pfile->cur_token; 1844 saved_cur_token = pfile->cur_token;
1711 saved_cur_run = pfile->cur_run; 1845 saved_cur_run = pfile->cur_run;
1712 1846
1713 pfile->context = XNEW (cpp_context); 1847 pfile->context = XCNEW (cpp_context);
1714 pfile->context->macro = 0;
1715 pfile->context->prev = 0;
1716 pfile->context->next = 0;
1717 1848
1718 /* Inline run_directive, since we need to delay the _cpp_pop_buffer 1849 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1719 until we've read all of the tokens that we want. */ 1850 until we've read all of the tokens that we want. */
1720 cpp_push_buffer (pfile, (const uchar *) result, dest - result, 1851 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1721 /* from_stage3 */ true); 1852 /* from_stage3 */ true);
1756 { 1887 {
1757 maxcount = maxcount * 3 / 2; 1888 maxcount = maxcount * 3 / 2;
1758 toks = XRESIZEVEC (cpp_token, toks, maxcount); 1889 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1759 } 1890 }
1760 toks[count] = *cpp_get_token (pfile); 1891 toks[count] = *cpp_get_token (pfile);
1892 /* _Pragma is a builtin, so we're not within a macro-map, and so
1893 the token locations are set to bogus ordinary locations
1894 near to, but after that of the "_Pragma".
1895 Paper over this by setting them equal to the location of the
1896 _Pragma itself (PR preprocessor/69126). */
1897 toks[count].src_loc = expansion_loc;
1761 /* Macros have been already expanded by cpp_get_token 1898 /* Macros have been already expanded by cpp_get_token
1762 if the pragma allowed expansion. */ 1899 if the pragma allowed expansion. */
1763 toks[count++].flags |= NO_EXPAND; 1900 toks[count++].flags |= NO_EXPAND;
1764 } 1901 }
1765 while (toks[count-1].type != CPP_PRAGMA_EOL); 1902 while (toks[count-1].type != CPP_PRAGMA_EOL);
1790 _cpp_push_token_context (pfile, NULL, toks, count); 1927 _cpp_push_token_context (pfile, NULL, toks, count);
1791 } 1928 }
1792 1929
1793 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */ 1930 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1794 int 1931 int
1795 _cpp_do__Pragma (cpp_reader *pfile) 1932 _cpp_do__Pragma (cpp_reader *pfile, source_location expansion_loc)
1796 { 1933 {
1797 const cpp_token *string = get__Pragma_string (pfile); 1934 const cpp_token *string = get__Pragma_string (pfile);
1798 pfile->directive_result.type = CPP_PADDING; 1935 pfile->directive_result.type = CPP_PADDING;
1799 1936
1800 if (string) 1937 if (string)
1801 { 1938 {
1802 destringize_and_run (pfile, &string->val.str); 1939 destringize_and_run (pfile, &string->val.str, expansion_loc);
1803 return 1; 1940 return 1;
1804 } 1941 }
1805 cpp_error (pfile, CPP_DL_ERROR, 1942 cpp_error (pfile, CPP_DL_ERROR,
1806 "_Pragma takes a parenthesized string literal"); 1943 "_Pragma takes a parenthesized string literal");
1807 return 0; 1944 return 0;
1817 { 1954 {
1818 cpp_hashnode *node = lex_macro_node (pfile, false); 1955 cpp_hashnode *node = lex_macro_node (pfile, false);
1819 1956
1820 if (node) 1957 if (node)
1821 { 1958 {
1822 skip = node->type != NT_MACRO; 1959 /* Do not treat conditional macros as being defined. This is due to
1960 the powerpc and spu ports using conditional macros for 'vector',
1961 'bool', and 'pixel' to act as conditional keywords. This messes
1962 up tests like #ifndef bool. */
1963 skip = (node->type != NT_MACRO
1964 || ((node->flags & NODE_CONDITIONAL) != 0));
1823 _cpp_mark_macro_used (node); 1965 _cpp_mark_macro_used (node);
1824 if (!(node->flags & NODE_USED)) 1966 if (!(node->flags & NODE_USED))
1825 { 1967 {
1826 node->flags |= NODE_USED; 1968 node->flags |= NODE_USED;
1827 if (node->type == NT_MACRO) 1969 if (node->type == NT_MACRO)
1858 { 2000 {
1859 node = lex_macro_node (pfile, false); 2001 node = lex_macro_node (pfile, false);
1860 2002
1861 if (node) 2003 if (node)
1862 { 2004 {
1863 skip = node->type == NT_MACRO; 2005 /* Do not treat conditional macros as being defined. This is due to
2006 the powerpc and spu ports using conditional macros for 'vector',
2007 'bool', and 'pixel' to act as conditional keywords. This messes
2008 up tests like #ifndef bool. */
2009 skip = (node->type == NT_MACRO
2010 && ((node->flags & NODE_CONDITIONAL) == 0));
1864 _cpp_mark_macro_used (node); 2011 _cpp_mark_macro_used (node);
1865 if (!(node->flags & NODE_USED)) 2012 if (!(node->flags & NODE_USED))
1866 { 2013 {
1867 node->flags |= NODE_USED; 2014 node->flags |= NODE_USED;
1868 if (node->type == NT_MACRO) 2015 if (node->type == NT_MACRO)
1932 /* Invalidate any controlling macro. */ 2079 /* Invalidate any controlling macro. */
1933 ifs->mi_cmacro = 0; 2080 ifs->mi_cmacro = 0;
1934 2081
1935 /* Only check EOL if was not originally skipping. */ 2082 /* Only check EOL if was not originally skipping. */
1936 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) 2083 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1937 check_eol (pfile, false); 2084 check_eol_endif_labels (pfile);
1938 } 2085 }
1939 } 2086 }
1940 2087
1941 /* Handle a #elif directive by not changing if_stack either. See the 2088 /* Handle a #elif directive by not changing if_stack either. See the
1942 comment above do_else. */ 2089 comment above do_else. */
1956 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, 2103 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1957 "the conditional began here"); 2104 "the conditional began here");
1958 } 2105 }
1959 ifs->type = T_ELIF; 2106 ifs->type = T_ELIF;
1960 2107
1961 if (! ifs->was_skipping) 2108 /* See DR#412: "Only the first group whose control condition
1962 { 2109 evaluates to true (nonzero) is processed; any following groups
1963 bool value; 2110 are skipped and their controlling directives are processed as
1964 /* The standard mandates that the expression be parsed even 2111 if they were in a group that is skipped." */
1965 if we are skipping elses at this point -- the lexical 2112 if (ifs->skip_elses)
1966 restrictions on #elif only apply to skipped groups, but 2113 pfile->state.skipping = 1;
1967 this group is not being skipped. Temporarily set 2114 else
1968 skipping to false to get lexer warnings. */ 2115 {
1969 pfile->state.skipping = 0; 2116 pfile->state.skipping = ! _cpp_parse_expr (pfile, false);
1970 value = _cpp_parse_expr (pfile, false); 2117 ifs->skip_elses = ! pfile->state.skipping;
1971 if (ifs->skip_elses)
1972 pfile->state.skipping = 1;
1973 else
1974 {
1975 pfile->state.skipping = ! value;
1976 ifs->skip_elses = value;
1977 }
1978 } 2118 }
1979 2119
1980 /* Invalidate any controlling macro. */ 2120 /* Invalidate any controlling macro. */
1981 ifs->mi_cmacro = 0; 2121 ifs->mi_cmacro = 0;
1982 } 2122 }
1993 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if"); 2133 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1994 else 2134 else
1995 { 2135 {
1996 /* Only check EOL if was not originally skipping. */ 2136 /* Only check EOL if was not originally skipping. */
1997 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) 2137 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1998 check_eol (pfile, false); 2138 check_eol_endif_labels (pfile);
1999 2139
2000 /* If potential control macro, we go back outside again. */ 2140 /* If potential control macro, we go back outside again. */
2001 if (ifs->next == 0 && ifs->mi_cmacro) 2141 if (ifs->next == 0 && ifs->mi_cmacro)
2002 { 2142 {
2003 pfile->mi_valid = true; 2143 pfile->mi_valid = true;
2320 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */ 2460 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2321 2461
2322 void 2462 void
2323 cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...) 2463 cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2324 { 2464 {
2325 char *ptr = NULL; 2465 char *ptr;
2326 2466
2327 va_list ap; 2467 va_list ap;
2328 va_start (ap, fmt); 2468 va_start (ap, fmt);
2329 vasprintf (&ptr, fmt, ap); 2469 ptr = xvasprintf (fmt, ap);
2330 va_end (ap); 2470 va_end (ap);
2331 2471
2332 cpp_define (pfile, ptr); 2472 cpp_define (pfile, ptr);
2333 free (ptr); 2473 free (ptr);
2334 } 2474 }
2505 _cpp_pop_buffer (cpp_reader *pfile) 2645 _cpp_pop_buffer (cpp_reader *pfile)
2506 { 2646 {
2507 cpp_buffer *buffer = pfile->buffer; 2647 cpp_buffer *buffer = pfile->buffer;
2508 struct _cpp_file *inc = buffer->file; 2648 struct _cpp_file *inc = buffer->file;
2509 struct if_stack *ifs; 2649 struct if_stack *ifs;
2650 const unsigned char *to_free;
2510 2651
2511 /* Walk back up the conditional stack till we reach its level at 2652 /* Walk back up the conditional stack till we reach its level at
2512 entry to this file, issuing error messages. */ 2653 entry to this file, issuing error messages. */
2513 for (ifs = buffer->if_stack; ifs; ifs = ifs->next) 2654 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2514 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, 2655 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2518 pfile->state.skipping = 0; 2659 pfile->state.skipping = 0;
2519 2660
2520 /* _cpp_do_file_change expects pfile->buffer to be the new one. */ 2661 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2521 pfile->buffer = buffer->prev; 2662 pfile->buffer = buffer->prev;
2522 2663
2664 to_free = buffer->to_free;
2523 free (buffer->notes); 2665 free (buffer->notes);
2524 2666
2525 /* Free the buffer object now; we may want to push a new buffer 2667 /* Free the buffer object now; we may want to push a new buffer
2526 in _cpp_push_next_include_file. */ 2668 in _cpp_push_next_include_file. */
2527 obstack_free (&pfile->buffer_ob, buffer); 2669 obstack_free (&pfile->buffer_ob, buffer);
2528 2670
2529 if (inc) 2671 if (inc)
2530 { 2672 {
2531 _cpp_pop_file_buffer (pfile, inc); 2673 _cpp_pop_file_buffer (pfile, inc, to_free);
2532 2674
2533 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); 2675 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2534 } 2676 }
2535 } 2677 }
2536 2678
2546 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); 2688 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2547 node->is_directive = 1; 2689 node->is_directive = 1;
2548 node->directive_index = i; 2690 node->directive_index = i;
2549 } 2691 }
2550 } 2692 }
2693
2694 /* Extract header file from a bracket include. Parsing starts after '<'.
2695 The string is malloced and must be freed by the caller. */
2696 char *
2697 _cpp_bracket_include(cpp_reader *pfile)
2698 {
2699 return glue_header_name (pfile);
2700 }
2701