forked from gcc-mirror/gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectives.c
2664 lines (2331 loc) · 77.1 KB
/
directives.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CPP Library. (Directive handling.)
Copyright (C) 1986-2016 Free Software Foundation, Inc.
Contributed by Per Bothner, 1994-95.
Based on CCCP program by Paul Rubin, June 1986
Adapted to ANSI C, Richard Stallman, Jan 1987
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "cpplib.h"
#include "internal.h"
#include "mkdeps.h"
#include "obstack.h"
/* Stack of conditionals currently in progress
(including both successful and failing conditionals). */
struct if_stack
{
struct if_stack *next;
source_location line; /* Line where condition started. */
const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
bool skip_elses; /* Can future #else / #elif be skipped? */
bool was_skipping; /* If were skipping on entry. */
int type; /* Most recent conditional for diagnostics. */
};
/* Contains a registered pragma or pragma namespace. */
typedef void (*pragma_cb) (cpp_reader *);
struct pragma_entry
{
struct pragma_entry *next;
const cpp_hashnode *pragma; /* Name and length. */
bool is_nspace;
bool is_internal;
bool is_deferred;
bool allow_expansion;
union {
pragma_cb handler;
struct pragma_entry *space;
unsigned int ident;
} u;
};
/* Values for the origin field of struct directive. KANDR directives
come from traditional (K&R) C. STDC89 directives come from the
1989 C standard. EXTENSION directives are extensions. */
#define KANDR 0
#define STDC89 1
#define EXTENSION 2
/* Values for the flags field of struct directive. COND indicates a
conditional; IF_COND an opening conditional. INCL means to treat
"..." and <...> as q-char and h-char sequences respectively. IN_I
means this directive should be handled even if -fpreprocessed is in
effect (these are the directives with callback hooks).
EXPAND is set on directives that are always macro-expanded. */
#define COND (1 << 0)
#define IF_COND (1 << 1)
#define INCL (1 << 2)
#define IN_I (1 << 3)
#define EXPAND (1 << 4)
#define DEPRECATED (1 << 5)
/* Defines one #-directive, including how to handle it. */
typedef void (*directive_handler) (cpp_reader *);
typedef struct directive directive;
struct directive
{
directive_handler handler; /* Function to handle directive. */
const uchar *name; /* Name of directive. */
unsigned short length; /* Length of name. */
unsigned char origin; /* Origin of directive. */
unsigned char flags; /* Flags describing this directive. */
};
/* Forward declarations. */
static void skip_rest_of_line (cpp_reader *);
static void check_eol (cpp_reader *, bool);
static void start_directive (cpp_reader *);
static void prepare_directive_trad (cpp_reader *);
static void end_directive (cpp_reader *, int);
static void directive_diagnostics (cpp_reader *, const directive *, int);
static void run_directive (cpp_reader *, int, const char *, size_t);
static char *glue_header_name (cpp_reader *);
static const char *parse_include (cpp_reader *, int *, const cpp_token ***,
source_location *);
static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
static unsigned int read_flag (cpp_reader *, unsigned int);
static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
static void do_diagnostic (cpp_reader *, int, int, int);
static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
static void do_include_common (cpp_reader *, enum include_type);
static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
const cpp_hashnode *);
static int count_registered_pragmas (struct pragma_entry *);
static char ** save_registered_pragmas (struct pragma_entry *, char **);
static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
char **);
static void do_pragma_once (cpp_reader *);
static void do_pragma_poison (cpp_reader *);
static void do_pragma_system_header (cpp_reader *);
static void do_pragma_dependency (cpp_reader *);
static void do_pragma_warning_or_error (cpp_reader *, bool error);
static void do_pragma_warning (cpp_reader *);
static void do_pragma_error (cpp_reader *);
static void do_linemarker (cpp_reader *);
static const cpp_token *get_token_no_padding (cpp_reader *);
static const cpp_token *get__Pragma_string (cpp_reader *);
static void destringize_and_run (cpp_reader *, const cpp_string *,
source_location);
static int parse_answer (cpp_reader *, struct answer **, int, source_location);
static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
static void handle_assertion (cpp_reader *, const char *, int);
static void do_pragma_push_macro (cpp_reader *);
static void do_pragma_pop_macro (cpp_reader *);
static void cpp_pop_definition (cpp_reader *, struct def_pragma_macro *);
/* This is the table of directive handlers. It is ordered by
frequency of occurrence; the numbers at the end are directive
counts from all the source code I have lying around (egcs and libc
CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
pcmcia-cs-3.0.9). This is no longer important as directive lookup
is now O(1). All extensions other than #warning, #include_next,
and #import are deprecated. The name is where the extension
appears to have come from. */
#define DIRECTIVE_TABLE \
D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
D(else, T_ELSE, KANDR, COND) /* 9863 */ \
D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
D(error, T_ERROR, STDC89, 0) /* 475 */ \
D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
/* #sccs is synonymous with #ident. */
#define do_sccs do_ident
/* Use the table to generate a series of prototypes, an enum for the
directive names, and an array of directive handlers. */
#define D(name, t, o, f) static void do_##name (cpp_reader *);
DIRECTIVE_TABLE
#undef D
#define D(n, tag, o, f) tag,
enum
{
DIRECTIVE_TABLE
N_DIRECTIVES
};
#undef D
#define D(name, t, origin, flags) \
{ do_##name, (const uchar *) #name, \
sizeof #name - 1, origin, flags },
static const directive dtable[] =
{
DIRECTIVE_TABLE
};
#undef D
#undef DIRECTIVE_TABLE
/* Wrapper struct directive for linemarkers.
The origin is more or less true - the original K+R cpp
did use this notation in its preprocessed output. */
static const directive linemarker_dir =
{
do_linemarker, UC"#", 1, KANDR, IN_I
};
#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
/* Skip any remaining tokens in a directive. */
static void
skip_rest_of_line (cpp_reader *pfile)
{
/* Discard all stacked contexts. */
while (pfile->context->prev)
_cpp_pop_context (pfile);
/* Sweep up all tokens remaining on the line. */
if (! SEEN_EOL ())
while (_cpp_lex_token (pfile)->type != CPP_EOF)
;
}
/* Helper function for check_oel. */
static void
check_eol_1 (cpp_reader *pfile, bool expand, int reason)
{
if (! SEEN_EOL () && (expand
? cpp_get_token (pfile)
: _cpp_lex_token (pfile))->type != CPP_EOF)
cpp_pedwarning (pfile, reason, "extra tokens at end of #%s directive",
pfile->directive->name);
}
/* Variant of check_eol used for Wendif-labels warnings. */
static void
check_eol_endif_labels (cpp_reader *pfile)
{
check_eol_1 (pfile, false, CPP_W_ENDIF_LABELS);
}
/* Ensure there are no stray tokens at the end of a directive. If
EXPAND is true, tokens macro-expanding to nothing are allowed. */
static void
check_eol (cpp_reader *pfile, bool expand)
{
check_eol_1 (pfile, expand, CPP_W_NONE);
}
/* Ensure there are no stray tokens other than comments at the end of
a directive, and gather the comments. */
static const cpp_token **
check_eol_return_comments (cpp_reader *pfile)
{
size_t c;
size_t capacity = 8;
const cpp_token **buf;
buf = XNEWVEC (const cpp_token *, capacity);
c = 0;
if (! SEEN_EOL ())
{
while (1)
{
const cpp_token *tok;
tok = _cpp_lex_token (pfile);
if (tok->type == CPP_EOF)
break;
if (tok->type != CPP_COMMENT)
cpp_error (pfile, CPP_DL_PEDWARN,
"extra tokens at end of #%s directive",
pfile->directive->name);
else
{
if (c + 1 >= capacity)
{
capacity *= 2;
buf = XRESIZEVEC (const cpp_token *, buf, capacity);
}
buf[c] = tok;
++c;
}
}
}
buf[c] = NULL;
return buf;
}
/* Called when entering a directive, _Pragma or command-line directive. */
static void
start_directive (cpp_reader *pfile)
{
/* Setup in-directive state. */
pfile->state.in_directive = 1;
pfile->state.save_comments = 0;
pfile->directive_result.type = CPP_PADDING;
/* Some handlers need the position of the # for diagnostics. */
pfile->directive_line = pfile->line_table->highest_line;
}
/* Called when leaving a directive, _Pragma or command-line directive. */
static void
end_directive (cpp_reader *pfile, int skip_line)
{
if (CPP_OPTION (pfile, traditional))
{
/* Revert change of prepare_directive_trad. */
if (!pfile->state.in_deferred_pragma)
pfile->state.prevent_expansion--;
if (pfile->directive != &dtable[T_DEFINE])
_cpp_remove_overlay (pfile);
}
else if (pfile->state.in_deferred_pragma)
;
/* We don't skip for an assembler #. */
else if (skip_line)
{
skip_rest_of_line (pfile);
if (!pfile->keep_tokens)
{
pfile->cur_run = &pfile->base_run;
pfile->cur_token = pfile->base_run.base;
}
}
/* Restore state. */
pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
pfile->state.in_directive = 0;
pfile->state.in_expression = 0;
pfile->state.angled_headers = 0;
pfile->directive = 0;
}
/* Prepare to handle the directive in pfile->directive. */
static void
prepare_directive_trad (cpp_reader *pfile)
{
if (pfile->directive != &dtable[T_DEFINE])
{
bool no_expand = (pfile->directive
&& ! (pfile->directive->flags & EXPAND));
bool was_skipping = pfile->state.skipping;
pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
|| pfile->directive == &dtable[T_ELIF]);
if (pfile->state.in_expression)
pfile->state.skipping = false;
if (no_expand)
pfile->state.prevent_expansion++;
_cpp_scan_out_logical_line (pfile, NULL, false);
if (no_expand)
pfile->state.prevent_expansion--;
pfile->state.skipping = was_skipping;
_cpp_overlay_buffer (pfile, pfile->out.base,
pfile->out.cur - pfile->out.base);
}
/* Stop ISO C from expanding anything. */
pfile->state.prevent_expansion++;
}
/* Output diagnostics for a directive DIR. INDENTED is nonzero if
the '#' was indented. */
static void
directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
{
/* Issue -pedantic or deprecated warnings for extensions. We let
-pedantic take precedence if both are applicable. */
if (! pfile->state.skipping)
{
if (dir->origin == EXTENSION
&& !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
&& CPP_PEDANTIC (pfile))
cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
else if (((dir->flags & DEPRECATED) != 0
|| (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
&& CPP_OPTION (pfile, cpp_warn_deprecated))
cpp_warning (pfile, CPP_W_DEPRECATED,
"#%s is a deprecated GCC extension", dir->name);
}
/* Traditionally, a directive is ignored unless its # is in
column 1. Therefore in code intended to work with K+R
compilers, directives added by C89 must have their #
indented, and directives present in traditional C must not.
This is true even of directives in skipped conditional
blocks. #elif cannot be used at all. */
if (CPP_WTRADITIONAL (pfile))
{
if (dir == &dtable[T_ELIF])
cpp_warning (pfile, CPP_W_TRADITIONAL,
"suggest not using #elif in traditional C");
else if (indented && dir->origin == KANDR)
cpp_warning (pfile, CPP_W_TRADITIONAL,
"traditional C ignores #%s with the # indented",
dir->name);
else if (!indented && dir->origin != KANDR)
cpp_warning (pfile, CPP_W_TRADITIONAL,
"suggest hiding #%s from traditional C with an indented #",
dir->name);
}
}
/* Check if we have a known directive. INDENTED is nonzero if the
'#' of the directive was indented. This function is in this file
to save unnecessarily exporting dtable etc. to lex.c. Returns
nonzero if the line of tokens has been handled, zero if we should
continue processing the line. */
int
_cpp_handle_directive (cpp_reader *pfile, int indented)
{
const directive *dir = 0;
const cpp_token *dname;
bool was_parsing_args = pfile->state.parsing_args;
bool was_discarding_output = pfile->state.discarding_output;
int skip = 1;
if (was_discarding_output)
pfile->state.prevent_expansion = 0;
if (was_parsing_args)
{
if (CPP_OPTION (pfile, cpp_pedantic))
cpp_error (pfile, CPP_DL_PEDWARN,
"embedding a directive within macro arguments is not portable");
pfile->state.parsing_args = 0;
pfile->state.prevent_expansion = 0;
}
start_directive (pfile);
dname = _cpp_lex_token (pfile);
if (dname->type == CPP_NAME)
{
if (dname->val.node.node->is_directive)
dir = &dtable[dname->val.node.node->directive_index];
}
/* We do not recognize the # followed by a number extension in
assembler code. */
else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
{
dir = &linemarker_dir;
if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
&& ! pfile->state.skipping)
cpp_error (pfile, CPP_DL_PEDWARN,
"style of line directive is a GCC extension");
}
if (dir)
{
/* If we have a directive that is not an opening conditional,
invalidate any control macro. */
if (! (dir->flags & IF_COND))
pfile->mi_valid = false;
/* Kluge alert. In order to be sure that code like this
#define HASH #
HASH define foo bar
does not cause '#define foo bar' to get executed when
compiled with -save-temps, we recognize directives in
-fpreprocessed mode only if the # is in column 1. macro.c
puts a space in front of any '#' at the start of a macro.
We exclude the -fdirectives-only case because macro expansion
has not been performed yet, and block comments can cause spaces
to precede the directive. */
if (CPP_OPTION (pfile, preprocessed)
&& !CPP_OPTION (pfile, directives_only)
&& (indented || !(dir->flags & IN_I)))
{
skip = 0;
dir = 0;
}
else
{
/* In failed conditional groups, all non-conditional
directives are ignored. Before doing that, whether
skipping or not, we should lex angle-bracketed headers
correctly, and maybe output some diagnostics. */
pfile->state.angled_headers = dir->flags & INCL;
pfile->state.directive_wants_padding = dir->flags & INCL;
if (! CPP_OPTION (pfile, preprocessed))
directive_diagnostics (pfile, dir, indented);
if (pfile->state.skipping && !(dir->flags & COND))
dir = 0;
}
}
else if (dname->type == CPP_EOF)
; /* CPP_EOF is the "null directive". */
else
{
/* An unknown directive. Don't complain about it in assembly
source: we don't know where the comments are, and # may
introduce assembler pseudo-ops. Don't complain about invalid
directives in skipped conditional groups (6.10 p4). */
if (CPP_OPTION (pfile, lang) == CLK_ASM)
skip = 0;
else if (!pfile->state.skipping)
cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
cpp_token_as_text (pfile, dname));
}
pfile->directive = dir;
if (CPP_OPTION (pfile, traditional))
prepare_directive_trad (pfile);
if (dir)
pfile->directive->handler (pfile);
else if (skip == 0)
_cpp_backup_tokens (pfile, 1);
end_directive (pfile, skip);
if (was_parsing_args && !pfile->state.in_deferred_pragma)
{
/* Restore state when within macro args. */
pfile->state.parsing_args = 2;
pfile->state.prevent_expansion = 1;
}
if (was_discarding_output)
pfile->state.prevent_expansion = 1;
return skip;
}
/* Directive handler wrapper used by the command line option
processor. BUF is \n terminated. */
static void
run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
{
cpp_push_buffer (pfile, (const uchar *) buf, count,
/* from_stage3 */ true);
start_directive (pfile);
/* This is a short-term fix to prevent a leading '#' being
interpreted as a directive. */
_cpp_clean_line (pfile);
pfile->directive = &dtable[dir_no];
if (CPP_OPTION (pfile, traditional))
prepare_directive_trad (pfile);
pfile->directive->handler (pfile);
end_directive (pfile, 1);
_cpp_pop_buffer (pfile);
}
/* Checks for validity the macro name in #define, #undef, #ifdef and
#ifndef directives. IS_DEF_OR_UNDEF is true if this call is
processing a #define or #undefine directive, and false
otherwise. */
static cpp_hashnode *
lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
{
const cpp_token *token = _cpp_lex_token (pfile);
/* The token immediately after #define must be an identifier. That
identifier may not be "defined", per C99 6.10.8p4.
In C++, it may not be any of the "named operators" either,
per C++98 [lex.digraph], [lex.key].
Finally, the identifier may not have been poisoned. (In that case
the lexer has issued the error message for us.) */
if (token->type == CPP_NAME)
{
cpp_hashnode *node = token->val.node.node;
if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
cpp_error (pfile, CPP_DL_ERROR,
"\"defined\" cannot be used as a macro name");
else if (is_def_or_undef
&& (node == pfile->spec_nodes.n__has_include__
|| node == pfile->spec_nodes.n__has_include_next__))
cpp_error (pfile, CPP_DL_ERROR,
"\"__has_include__\" cannot be used as a macro name");
else if (! (node->flags & NODE_POISONED))
return node;
}
else if (token->flags & NAMED_OP)
cpp_error (pfile, CPP_DL_ERROR,
"\"%s\" cannot be used as a macro name as it is an operator in C++",
NODE_NAME (token->val.node.node));
else if (token->type == CPP_EOF)
cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
pfile->directive->name);
else
cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
return NULL;
}
/* Process a #define directive. Most work is done in macro.c. */
static void
do_define (cpp_reader *pfile)
{
cpp_hashnode *node = lex_macro_node (pfile, true);
if (node)
{
/* If we have been requested to expand comments into macros,
then re-enable saving of comments. */
pfile->state.save_comments =
! CPP_OPTION (pfile, discard_comments_in_macro_exp);
if (pfile->cb.before_define)
pfile->cb.before_define (pfile);
if (_cpp_create_definition (pfile, node))
if (pfile->cb.define)
pfile->cb.define (pfile, pfile->directive_line, node);
node->flags &= ~NODE_USED;
}
}
/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
static void
do_undef (cpp_reader *pfile)
{
cpp_hashnode *node = lex_macro_node (pfile, true);
if (node)
{
if (pfile->cb.before_define)
pfile->cb.before_define (pfile);
if (pfile->cb.undef)
pfile->cb.undef (pfile, pfile->directive_line, node);
/* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
identifier is not currently defined as a macro name. */
if (node->type == NT_MACRO)
{
if (node->flags & NODE_WARN)
cpp_error (pfile, CPP_DL_WARNING,
"undefining \"%s\"", NODE_NAME (node));
else if ((node->flags & NODE_BUILTIN)
&& CPP_OPTION (pfile, warn_builtin_macro_redefined))
cpp_warning_with_line (pfile, CPP_W_BUILTIN_MACRO_REDEFINED,
pfile->directive_line, 0,
"undefining \"%s\"", NODE_NAME (node));
if (CPP_OPTION (pfile, warn_unused_macros))
_cpp_warn_if_unused_macro (pfile, node, NULL);
_cpp_free_definition (node);
}
}
check_eol (pfile, false);
}
/* Undefine a single macro/assertion/whatever. */
static int
undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
void *data_p ATTRIBUTE_UNUSED)
{
/* Body of _cpp_free_definition inlined here for speed.
Macros and assertions no longer have anything to free. */
h->type = NT_VOID;
h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
return 1;
}
/* Undefine all macros and assertions. */
void
cpp_undef_all (cpp_reader *pfile)
{
cpp_forall_identifiers (pfile, undefine_macros, NULL);
}
/* Helper routine used by parse_include. Reinterpret the current line
as an h-char-sequence (< ... >); we are looking at the first token
after the <. Returns a malloced filename. */
static char *
glue_header_name (cpp_reader *pfile)
{
const cpp_token *token;
char *buffer;
size_t len, total_len = 0, capacity = 1024;
/* To avoid lexed tokens overwriting our glued name, we can only
allocate from the string pool once we've lexed everything. */
buffer = XNEWVEC (char, capacity);
for (;;)
{
token = get_token_no_padding (pfile);
if (token->type == CPP_GREATER)
break;
if (token->type == CPP_EOF)
{
cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
break;
}
len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
if (total_len + len > capacity)
{
capacity = (capacity + len) * 2;
buffer = XRESIZEVEC (char, buffer, capacity);
}
if (token->flags & PREV_WHITE)
buffer[total_len++] = ' ';
total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
true)
- (uchar *) buffer);
}
buffer[total_len] = '\0';
return buffer;
}
/* Returns the file name of #include, #include_next, #import and
#pragma dependency. The string is malloced and the caller should
free it. Returns NULL on error. LOCATION is the source location
of the file name. */
static const char *
parse_include (cpp_reader *pfile, int *pangle_brackets,
const cpp_token ***buf, source_location *location)
{
char *fname;
const cpp_token *header;
/* Allow macro expansion. */
header = get_token_no_padding (pfile);
*location = header->src_loc;
if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
|| header->type == CPP_HEADER_NAME)
{
fname = XNEWVEC (char, header->val.str.len - 1);
memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
fname[header->val.str.len - 2] = '\0';
*pangle_brackets = header->type == CPP_HEADER_NAME;
}
else if (header->type == CPP_LESS)
{
fname = glue_header_name (pfile);
*pangle_brackets = 1;
}
else
{
const unsigned char *dir;
if (pfile->directive == &dtable[T_PRAGMA])
dir = UC"pragma dependency";
else
dir = pfile->directive->name;
cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
dir);
return NULL;
}
if (pfile->directive == &dtable[T_PRAGMA])
{
/* This pragma allows extra tokens after the file name. */
}
else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
check_eol (pfile, true);
else
{
/* If we are not discarding comments, then gather them while
doing the eol check. */
*buf = check_eol_return_comments (pfile);
}
return fname;
}
/* Handle #include, #include_next and #import. */
static void
do_include_common (cpp_reader *pfile, enum include_type type)
{
const char *fname;
int angle_brackets;
const cpp_token **buf = NULL;
source_location location;
/* Re-enable saving of comments if requested, so that the include
callback can dump comments which follow #include. */
pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
fname = parse_include (pfile, &angle_brackets, &buf, &location);
if (!fname)
{
if (buf)
XDELETEVEC (buf);
return;
}
if (!*fname)
{
cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
"empty filename in #%s",
pfile->directive->name);
XDELETEVEC (fname);
if (buf)
XDELETEVEC (buf);
return;
}
/* Prevent #include recursion. */
if (pfile->line_table->depth >= CPP_STACK_MAX)
cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
else
{
/* Get out of macro context, if we are. */
skip_rest_of_line (pfile);
if (pfile->cb.include)
pfile->cb.include (pfile, pfile->directive_line,
pfile->directive->name, fname, angle_brackets,
buf);
_cpp_stack_include (pfile, fname, angle_brackets, type, location);
}
XDELETEVEC (fname);
if (buf)
XDELETEVEC (buf);
}
static void
do_include (cpp_reader *pfile)
{
do_include_common (pfile, IT_INCLUDE);
}
static void
do_import (cpp_reader *pfile)
{
do_include_common (pfile, IT_IMPORT);
}
static void
do_include_next (cpp_reader *pfile)
{
enum include_type type = IT_INCLUDE_NEXT;
/* If this is the primary source file, warn and use the normal
search logic. */
if (cpp_in_primary_file (pfile))
{
cpp_error (pfile, CPP_DL_WARNING,
"#include_next in primary source file");
type = IT_INCLUDE;
}
do_include_common (pfile, type);
}
/* Subroutine of do_linemarker. Read possible flags after file name.
LAST is the last flag seen; 0 if this is the first flag. Return the
flag if it is valid, 0 at the end of the directive. Otherwise
complain. */
static unsigned int
read_flag (cpp_reader *pfile, unsigned int last)
{
const cpp_token *token = _cpp_lex_token (pfile);
if (token->type == CPP_NUMBER && token->val.str.len == 1)
{
unsigned int flag = token->val.str.text[0] - '0';
if (flag > last && flag <= 4
&& (flag != 4 || last == 3)
&& (flag != 2 || last == 0))
return flag;
}
if (token->type != CPP_EOF)
cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
cpp_token_as_text (pfile, token));
return 0;
}
/* Subroutine of do_line and do_linemarker. Convert a number in STR,
of length LEN, to binary; store it in NUMP, and return false if the
number was well-formed, true if not. WRAPPED is set to true if the
number did not fit into 'unsigned long'. */
static bool
strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
{
linenum_type reg = 0;
linenum_type reg_prev = 0;
uchar c;
*wrapped = false;
while (len--)
{
c = *str++;
if (!ISDIGIT (c))
return true;
reg *= 10;
reg += c - '0';
if (reg < reg_prev)
*wrapped = true;
reg_prev = reg;
}
*nump = reg;
return false;
}
/* Interpret #line command.
Note that the filename string (if any) is a true string constant
(escapes are interpreted), unlike in #line. */
static void
do_line (cpp_reader *pfile)
{
struct line_maps *line_table = pfile->line_table;
const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
/* skip_rest_of_line() may cause line table to be realloc()ed so note down
sysp right now. */
unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
const cpp_token *token;
const char *new_file = ORDINARY_MAP_FILE_NAME (map);
linenum_type new_lineno;
/* C99 raised the minimum limit on #line numbers. */
linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
bool wrapped;
/* #line commands expand macros. */
token = cpp_get_token (pfile);
if (token->type != CPP_NUMBER
|| strtolinenum (token->val.str.text, token->val.str.len,
&new_lineno, &wrapped))
{
if (token->type == CPP_EOF)
cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
else
cpp_error (pfile, CPP_DL_ERROR,
"\"%s\" after #line is not a positive integer",
cpp_token_as_text (pfile, token));
return;
}
if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
else if (wrapped)
cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
token = cpp_get_token (pfile);
if (token->type == CPP_STRING)
{
cpp_string s = { 0, 0 };
if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
&s, CPP_STRING))
new_file = (const char *)s.text;
check_eol (pfile, true);
}
else if (token->type != CPP_EOF)
{
cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
cpp_token_as_text (pfile, token));
return;
}
skip_rest_of_line (pfile);
_cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
map_sysp);
line_table->seen_line_directive = true;
}
/* Interpret the # 44 "file" [flags] notation, which has slightly
different syntax and semantics from #line: Flags are allowed,
and we never complain about the line number being too big. */
static void
do_linemarker (cpp_reader *pfile)
{
struct line_maps *line_table = pfile->line_table;
const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
const cpp_token *token;
const char *new_file = ORDINARY_MAP_FILE_NAME (map);
linenum_type new_lineno;
unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
enum lc_reason reason = LC_RENAME_VERBATIM;
int flag;
bool wrapped;
/* Back up so we can get the number again. Putting this in
_cpp_handle_directive risks two calls to _cpp_backup_tokens in
some circumstances, which can segfault. */
_cpp_backup_tokens (pfile, 1);
/* #line commands expand macros. */
token = cpp_get_token (pfile);
if (token->type != CPP_NUMBER
|| strtolinenum (token->val.str.text, token->val.str.len,
&new_lineno, &wrapped))
{
/* Unlike #line, there does not seem to be a way to get an EOF
here. So, it should be safe to always spell the token. */