Repository navigation
Expand file tree
/
Copy pathpreproc.c
More file actions
5581 lines (5216 loc) · 211 KB
/
Copy pathpreproc.c
File metadata and controls
5581 lines (5216 loc) · 211 KB
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
/*
Part of the EDG Compiler Project, under the Apache License v2.0 with LLVM
Exceptions.
See https://edgcpp.org/LICENSE.txt for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
/*
preproc.c -- Preprocessing directives.
*/
/* Header files common to all files. */
#include "fe_common.h"
/* Header files used by files involved in declaration processing. */
#include "decl_hdrs.h"
#ifdef PCH_PRAGMA_GUARD
/* Mark the end of the sequence of headers subject to precompiled header
processing. */
#pragma hdrstop
#endif /* ifdef PCH_PRAGMA_GUARD */
/* Additional header files. */
#include "expr.h"
#include "macro.h"
#include "literals.h"
#if MICROSOFT_EXTENSIONS_ALLOWED
#include "ms_metadata.h"
#endif /* MICROSOFT_EXTENSIONS_ALLOWED */
/* Conditionally open the "edg" namespace. */
BEGIN_EDG_NAMESPACE
typedef struct a_pp_if_stack_entry *a_pp_if_stack_entry_ptr;
typedef struct a_pp_if_stack_entry {
/* Entry on the stack for preprocessing #ifs (and #ifdefs, etc.). */
a_source_position
if_pos; /* Position at which the #if appeared. */
a_boolean else_encountered;
/* TRUE once the #else (if any) has been seen. */
} a_pp_if_stack_entry;
STATIC_THREAD a_pp_if_stack_entry_ptr
pp_if_stack;
/* Stack of preprocessing ifs. Dynamically allocated;
can be expanded if necessary. size_pp_if_stack
gives the number of elements currently allocated.
Allocation is not per-file. */
STATIC_THREAD sizeof_t
size_pp_if_stack;
/* Allocated size in elements of pp_if_stack.
Not per-file. */
#define PP_IF_STACK_INCREMENTAL_ALLOCATION 30
/* The number of elements added to pp_if_stack each
time it is reallocated; also the initial
allocation. */
/* See preproc.h for pp_if_stack_depth and base_pp_if_stack_depth. */
STATIC_THREAD a_source_position
pos_of_curr_directive;
/* The source position of the current preprocessing
directive. */
STATIC_THREAD a_boolean
is_header_stop_dir;
/* TRUE when the directive being scanned is the
header stop directive after which a precompiled
header file should be generated. */
STATIC_THREAD a_text_buffer_ptr
header_name_buffer;
/* Text buffer used by copy_header_name. */
#if MICROSOFT_EXTENSIONS_ALLOWED
STATIC_THREAD a_hash_table_ptr
include_alias_hash_table;
/* A hash table used to search for include alias
entries. NULL if no such aliases exist. */
#if DEBUG
STATIC_THREAD unsigned long
num_include_aliases_allocated;
/* Count of include aliases allocated, for space used
purposes. */
#endif /* DEBUG */
/*
Entry used by the include_alias_hash_table to record information about
include_alias pragmas that have been encountered. This is used for the
implementation of the Microsoft include_alias pragma.
*/
typedef struct an_include_alias *an_include_alias_ptr;
typedef struct an_include_alias {
a_const_char *long_file_name;
/* The file name that is to be aliased to another
name. This contains the raw characters of the
header name token. */
sizeof_t long_file_name_length;
/* The length of long_file_name, not including the
null terminator. */
a_const_char *short_file_name;
/* The file name to be used in place of
long_file_name. This contains the file name
after conversions such as possible conversion to
UTF-8. */
} an_include_alias;
static void clear_include_alias(an_include_alias_ptr iap)
/*
Initialize the fields of an include alias entry.
*/
{
iap->long_file_name = NULL;
iap->long_file_name_length = 0;
iap->short_file_name = NULL;
} /* clear_include_alias */
static an_include_alias_ptr alloc_include_alias(void)
/*
Allocate a new include alias entry, initialize its fields, and return a
pointer to it.
*/
{
an_include_alias_ptr iap;
iap = alloc_fe_of_type(an_include_alias);
#if DEBUG
num_include_aliases_allocated++;
#endif /* DEBUG */
clear_include_alias(iap);
return iap;
} /* alloc_include_alias */
a_hash_value hash_include_alias(a_void_ptr key)
/*
Produce a hash value for an include alias entry. The key is
an_include_alias_ptr.
*/
{
a_hash_value value;
an_include_alias_ptr iap;
iap = (an_include_alias_ptr)key;
value = hash_source_string((a_void_ptr)iap->long_file_name);
return value;
} /* hash_include_alias */
a_boolean compare_include_alias(a_void_ptr entry,
a_void_ptr key)
/*
Compare an entry in the include alias hash table with an entry to be
found. "entry" and "key" are of type an_include_alias_ptr. Return TRUE
if the key matches the entry. Return TRUE if the entries match.
*/
{
an_include_alias_ptr entry_iap;
an_include_alias_ptr key_iap;
a_boolean result;
entry_iap = (an_include_alias_ptr)entry;
key_iap = (an_include_alias_ptr)key;
result = entry_iap->long_file_name_length ==
key_iap->long_file_name_length &&
strcmp(entry_iap->long_file_name, key_iap->long_file_name) == 0;
return result;
} /* compare_include_alias */
static an_include_alias_ptr find_or_create_include_alias(
a_const_char *long_name,
a_const_char *short_name,
a_boolean create)
/*
Look for long_name in the include alias hash table. If it is not found
and create is TRUE, add an entry. A pointer to the entry is returned, or
NULL if no entry was found or created. If create is TRUE and an existing
entry is found, the short file name that entry refers to is updated to
refer to the new short_name.
*/
{
an_include_alias ia;
an_include_alias_ptr *iap_in_table;
an_include_alias_ptr iap = NULL;
/* Create an include alias entry that describes the entry to be found.
This is the key used for the hash table lookup. */
clear_include_alias(&ia);
ia.long_file_name = long_name;
ia.long_file_name_length = strlen(long_name);
iap_in_table = (an_include_alias_ptr*)hash_find(
include_alias_hash_table,
(a_void_ptr)&ia, create);
if (iap_in_table != NULL) iap = *iap_in_table;
if (create) {
/* Create the entry to be referenced by the hash table. If a previous
entry was found, the short file name that it refers to will be replaced
with the new one. */
if (iap == NULL) {
iap = alloc_include_alias();
*iap_in_table = iap;
/* Copy the key entry created above into the new entry. */
*iap = ia;
} /* if */
iap->short_file_name = short_name;
} /* if */
return iap;
} /* find_or_create_include_alias */
#endif /* MICROSOFT_EXTENSIONS_ALLOWED */
a_const_char *check_for_include_alias(void)
/*
Check whether the current header name token refers to a file name for
which an include_alias pragma has been seen.
*/
{
#if MICROSOFT_EXTENSIONS_ALLOWED
an_include_alias_ptr iap;
a_const_char *result = NULL;
a_text_buffer_ptr buf = header_name_buffer;
/* The hash table will only exist if an include alias has been seen. */
if (include_alias_hash_table != NULL) {
/* Extract the raw characters of the header name from the token. */
reset_text_buffer(buf);
add_to_text_buffer(buf, start_of_curr_token, len_of_curr_token);
add_char_to_text_buffer(buf, '\0');
iap = find_or_create_include_alias(buf->buffer, (char*)NULL,
/*create=*/FALSE);
if (iap != NULL) result = iap->short_file_name;
#if DEBUG
if (db_flag_is_set("include_alias")) {
fprintf(f_debug, "Looking for alias for %s, found %s\n", buf->buffer,
result == NULL ? "NULL" : result);
} /* if */
#endif /* DEBUG */
} /* if */
return result;
#else /* !MICROSOFT_EXTENSIONS_ALLOWED */
return NULL;
#endif /* MICROSOFT_EXTENSIONS_ALLOWED */
} /* check_for_include_alias */
/* Advance declaration needed because of mutual recursion: */
static void skip_to_endif(a_boolean stop_skip_on_else_or_elif);
static void pass_directive_to_output(void);
/*
Macro that compares the current token (an identifier) against a constant
string, and returns true if the two match.
*/
#define curr_id_is(str) \
string_is(start_of_curr_token, len_of_curr_token, (str))
/*
Macro that compares the current token (an identifier) with the pragma
identifier associated with the specified pragma kind.
*/
#define curr_id_matches_pragma_id(pragma_kind) \
(len_of_curr_token == strlen(pragma_ids[(int)(pragma_kind)]) && \
strncmp(pragma_ids[(int)(pragma_kind)], start_of_curr_token, \
size_t_arg(len_of_curr_token)) == 0)
static a_pp_directive_kind identify_dir_keyword(void)
/*
Identify the keyword of the current preprocessing directive, and return the
proper value for it, or ppd_not_valid if the keyword is not valid.
The current source position is just after the "#" of the directive, with
the "#" the current token (at least logically).
*/
{
a_pp_directive_kind kind;
some_error_in_curr_directive = FALSE;
/* If preprocessing output is being produced, delete this directive.
This usually leaves a blank line in place of the directive. */
do_not_put_curr_line_in_pp_output = TRUE;
/* Skip horizontal white space following the "#", and get the keyword.
exp_digit_sequence is set so that the line number of the
line-identifying directive will be scanned correctly. */
exp_digit_sequence = TRUE;
(void)get_token();
exp_digit_sequence = FALSE;
/* Remember the position of the directive, for error purposes. */
copy_source_position(pos_curr_token, pos_of_curr_directive);
if (curr_token == tok_newline) {
/* Null directive. */
kind = ppd_null;
} else if (curr_token == tok_digit_sequence) {
/* A line-identifying directive (output from cpp); this is similar
to a #line directive, but not exactly the same. */
kind = ppd_linedef;
} else if (curr_token != tok_identifier) {
/* The token following the "#" is not an identifier: error. */
kind = ppd_not_valid;
} else {
/* Identify the directive keyword. */
/* The tests here should be in order of expected frequency. */
if ( curr_id_is("if")) {
/* #if directive. */
kind = ppd_if;
} else if (curr_id_is("ifdef")) {
/* #ifdef directive. */
kind = ppd_ifdef;
} else if (curr_id_is("ifndef")) {
/* #ifndef directive. */
kind = ppd_ifndef;
} else if (curr_id_is("else")) {
/* #else directive. */
kind = ppd_else;
} else if (curr_id_is("endif")) {
/* #endif directive. */
kind = ppd_endif;
} else if (curr_id_is("elif")) {
/* #elif directive. */
kind = ppd_elif;
} else if (curr_id_is("elifdef")) {
/* #elifdef directive. */
if (elifdef_enabled) {
kind = ppd_elifdef;
} else {
if (currently_in_pp_if_skip) {
pos_warning(ec_elifdef_not_enabled, &pos_curr_token);
} /* if */
kind = ppd_not_valid;
} /* if */
} else if (curr_id_is("elifndef")) {
/* #elifndef directive. */
if (elifdef_enabled) {
kind = ppd_elifndef;
} else {
if (currently_in_pp_if_skip) {
pos_warning(ec_elifdef_not_enabled, &pos_curr_token);
} /* if */
kind = ppd_not_valid;
} /* if */
} else if (curr_id_is("define")) {
/* #define directive. */
kind = ppd_define;
} else if (curr_id_is("include")) {
/* #include directive. */
kind = ppd_include;
} else if (curr_id_is("undef")) {
/* #undef directive. */
kind = ppd_undef;
} else if (curr_id_is("line")) {
/* #line directive. */
kind = ppd_line;
} else if (curr_id_is("pragma")) {
/* #pragma directive. */
kind = ppd_pragma;
} else if (curr_id_is("error")) {
/* #error directive. */
kind = ppd_error;
} else if ((!strict_ansi_mode || c23_mode || cpp23_mode) &&
curr_id_is("warning")) {
/* #warning directive. */
kind = ppd_warning;
#if IDENT_DIRECTIVE_AND_PRAGMA
} else if (curr_id_is("ident")) {
/* #ident directive. */
kind = ppd_ident;
#endif /* IDENT_DIRECTIVE_AND_PRAGMA */
#if ALIAS_DIRECTIVE
} else if (curr_id_is("alias")) {
/* #alias directive. */
kind = ppd_alias;
#endif /* ALIAS_DIRECTIVE */
#if ATT_PREPROCESSING_EXTENSIONS_ALLOWED
} else if (curr_id_is("assert")) {
/* #assert directive (an AT&T extension in System V release 4). */
kind = ppd_assert;
} else if (curr_id_is("unassert")) {
/* #unassert directive (an AT&T extension in System V release 4). */
kind = ppd_unassert;
#endif /* ATT_PREPROCESSING_EXTENSIONS_ALLOWED */
#if MICROSOFT_EXTENSIONS_ALLOWED
} else if (ms_compat && curr_id_is("import")) {
/* #import directive (a Microsoft extension). */
kind = ppd_import;
} else if (curr_id_is("using")) {
/* #using directive (a C++/CLI directive). A diagnostic will be issued
if C++/CLI is not enabled. */
kind = ppd_using;
#endif /* MICROSOFT_EXTENSIONS_ALLOWED */
} else if (curr_id_is("include_next")) {
/* #include_next directive. */
kind = ppd_include_next;
} else if (embed_enabled && curr_id_is("embed")) {
/* #embed directive */
kind = ppd_embed;
} else {
kind = ppd_not_valid;
} /* if */
} /* if */
return(kind);
} /* identify_dir_keyword */
static void nonstandard_pp_directive(void)
/*
Issue a diagnostic for a use of a nonstandard preprocessing directive.
*/
{
if (strict_ansi_mode) {
diagnostic(strict_ansi_discretionary_severity, ec_nonstd_pp_directive);
} /* if */
} /* nonstandard_pp_directive */
static void ignore_harmless_trailing_comment(void)
/*
Ignore a harmless comment at the end of a preprocessing directive.
If the dialect of C being compiled is pcc, this is done without
a diagnostic (pcc allows comments at the ends of several kinds of lines).
*/
{
if (curr_token != tok_newline) {
if (!pcc_preprocessing_mode) {
pos_diagnostic(strict_ansi_mode ? strict_ansi_discretionary_severity
: es_warning,
ec_extra_text_in_pp_directive, &pos_curr_token);
} /* if */
flush_to_newline();
} /* if */
} /* ignore_harmless_trailing_comment */
static void end_of_directive_processing(void)
/*
Do whatever processing is required at the end of all preprocessing directives.
This includes checking that there is no extra text beyond the expected
end of the directive.
*/
{
/* Check that everything up to the closing newline has been taken.
If not, flush to the newline, with an error only if there was no
previous error (or a warning in Microsoft mode). */
if (curr_token != tok_newline) {
if (!some_error_in_curr_directive) {
pos_diagnostic(ms_extensions ? es_warning : es_discretionary_error,
ec_extra_text_in_pp_directive, &pos_curr_token);
} /* if */
flush_to_newline();
} /* if */
} /* end_of_directive_processing */
static void scan_if_expr(a_boolean *condition)
/*
Scan the expression for a #if or #elif, and return in *condition its
truth value.
*/
{
a_boolean save_fetch_pp_tokens = fetch_pp_tokens;
a_boolean save_expand_macros = expand_macros;
a_constant_ptr temp_const = local_constant();
a_boolean save_in_pp_if_skip = currently_in_pp_if_skip;
fetch_pp_tokens = FALSE;
expand_macros = TRUE;
in_pp_if_expression = TRUE;
currently_in_pp_if_skip = FALSE;
(void)get_token();
/* Scan the conditional expression. */
scan_pp_expression(temp_const);
in_pp_if_expression = FALSE;
currently_in_pp_if_skip = save_in_pp_if_skip;
if (is_error_constant(temp_const)) {
*condition = FALSE;
some_error_in_curr_directive = TRUE;
} else {
/* The constant is guaranteed to be integer. */
/* Determine whether or not it is zero. */
*condition = !eqlit_integer_constant(temp_const, (a_host_large_integer)0);
} /* if */
fetch_pp_tokens = save_fetch_pp_tokens;
expand_macros = save_expand_macros;
release_local_constant(&temp_const);
} /* scan_if_expr */
static void proc_else(a_boolean perform_else)
/*
Scan and process an #else directive. If perform_else == TRUE, skip to
the #endif.
*/
{
if (pp_if_stack_depth <= base_pp_if_stack_depth) {
/* There was no #if corresponding to this #else. */
pos_error(ec_missing_pp_if, &error_position);
flush_to_newline();
} else if (pp_if_stack[pp_if_stack_depth].else_encountered) {
/* An #else has been seen already, so this is a second #else.
Error except when emulating pcc or early versions of the Microsoft
compiler, which didn't give an error. */
if (C_dialect == C_dialect_pcc ||
(microsoft_mode && microsoft_version < 1200)) {
pos_warning(ec_pp_else_already_appeared, &error_position);
} else {
diagnostic(es_discretionary_error, ec_pp_else_already_appeared);
} /* if */
flush_to_newline();
} else {
/* The #else is valid, process it. */
a_byte ifg_state = get_ifg_state();
if (pp_if_stack_depth == (base_pp_if_stack_depth+1) &&
ifg_state != IFG_STATE_FAIL &&
ifg_state != IFG_STATE_ONCE) {
/* We've encountered a #else at the outermost level. This
means that this file is not a candidate for suppression
of subsequent includes. */
set_ifg_state(IFG_STATE_FAIL);
} /* if */
pp_if_stack[pp_if_stack_depth].else_encountered = TRUE;
(void)get_token();
ignore_harmless_trailing_comment();
if (perform_else) {
skip_to_endif(/*stop_skip_on_else_or_elif=*/FALSE);
} /* if */
} /* if */
} /* proc_else */
static void proc_elif(a_boolean treat_as_else)
/*
Scan and process a #elif, #elifdef, or #elifndef directive. If
treat_as_else is TRUE, skip to the corresponding #endif.
*/
{
if (pp_if_stack_depth <= base_pp_if_stack_depth) {
/* There was no #if corresponding to this directive. */
pos_error(ec_missing_pp_if, &error_position);
flush_to_newline();
} else if (pp_if_stack[pp_if_stack_depth].else_encountered) {
/* #else has already appeared; the directive is not valid here. */
pos_error(ec_pp_else_already_appeared, &error_position);
flush_to_newline();
} else {
/* The directive is valid, process it. */
a_byte ifg_state = get_ifg_state();
if (pp_if_stack_depth == (base_pp_if_stack_depth+1) &&
ifg_state != IFG_STATE_FAIL &&
ifg_state != IFG_STATE_ONCE) {
/* We've encountered a #elif, etc., at the outermost level. This
means that this file is not a candidate for suppression of
subsequent includes. */
set_ifg_state(IFG_STATE_FAIL);
} /* if */
if (treat_as_else) {
/* When a #elif, etc., is hit when not skipping, it always acts like
a #else. That is, the condition is unimportant: the skip to
#endif is always done. */
flush_to_newline();
skip_to_endif(/*stop_skip_on_else_or_elif=*/FALSE);
} /* if */
} /* if */
} /* proc_elif */
static void proc_endif(void)
/*
Scan and process an #endif directive.
*/
{
if (pp_if_stack_depth <= base_pp_if_stack_depth) {
/* There was no #if corresponding to this #endif. */
pos_error(ec_missing_pp_if, &error_position);
flush_to_newline();
} else {
/* The #endif is valid, process it. */
if (pp_if_stack_depth == (base_pp_if_stack_depth+1)) {
/* Update the include file guard state for the current file. If
we've seen the opening #ifndef then go to the state that says
that we're OK as long as we don't see any more tokens from this
file. If we are in any other state (not including ONCE, then
goto the FAIL state. */
a_byte ifg_state = get_ifg_state();
if (ifg_state == IFG_STATE_INTERMED) {
set_ifg_state(IFG_STATE_ACCEPT);
} else if (ifg_state != IFG_STATE_ONCE) {
set_ifg_state(IFG_STATE_FAIL);
} /* if */
} /* if */
#if DEBUG
if (debug_level >= 3) {
fprintf(f_debug, "endif, pp_if_stack_depth = %ld\n", pp_if_stack_depth);
} /* if */
#endif /* DEBUG */
pp_if_stack_depth--;
/* Set things up for the check for extra text on the end of the line. */
(void)get_token();
ignore_harmless_trailing_comment();
} /* if */
} /* proc_endif */
static void push_pp_if_stack(void)
/*
Push a new entry on the preprocessing-if stack (pp_if_stack).
*/
{
if ((sizeof_t)pp_if_stack_depth+1 == size_pp_if_stack) {
/* Stack is full; expand it. */
sizeof_t new_size = size_pp_if_stack + PP_IF_STACK_INCREMENTAL_ALLOCATION;
pp_if_stack = (a_pp_if_stack_entry_ptr)realloc_buffer(
(char *)pp_if_stack,
(sizeof_t)(size_pp_if_stack*sizeof(a_pp_if_stack_entry)),
(sizeof_t)(new_size*sizeof(a_pp_if_stack_entry)));
size_pp_if_stack = new_size;
} /* if */
pp_if_stack_depth++;
copy_source_position(pos_of_curr_directive,
pp_if_stack[pp_if_stack_depth].if_pos);
pp_if_stack[pp_if_stack_depth].else_encountered = FALSE;
#if DEBUG
if (debug_level >= 3) {
fprintf(f_debug, "push, pp_if_stack_depth = %ld\n", pp_if_stack_depth);
} /* if */
#endif /* DEBUG */
} /* push_pp_if_stack */
static a_boolean check_if_defined_macro(a_boolean is_ifdef,
a_boolean *condition,
a_const_char **id_spelling)
/*
Scan the operand of a #ifdef, #ifndef, #elifdef, or #elifndef directive;
is_ifdef is TRUE for the "ifdef" variants and FALSE for the "ifndef" variants.
If the operand is an identifier:
- if id_spelling is non-NULL, set *id_spelling to point to a
null-terminated copy of the identifier in front end memory;
- if the identifier is the name of a macro, set *condition to TRUE for the
"ifdef" variants and FALSE for the "ifndef" variants, and vice versa if
the identifier is not the name of a macro; and
- return TRUE.
Otherwise, issue a diagnostic, set *condition to FALSE, and return FALSE.
*/
{
a_boolean result = FALSE;
a_symbol_ptr assoc_symbol;
a_symbol_header_ptr sym_hdr;
*condition = FALSE;
if (get_token() != tok_identifier) {
/* Expected an identifier. If the token is an integer, give a warning
rather than an error because UNIX code includes things like #ifdef 3b5,
which is treated as undefined. */
if ((!strict_ansi_mode || strict_ansi_error_severity != es_error) &&
isdigit((unsigned char)*start_of_curr_token)) {
pos_warning(ec_exp_identifier, &error_position);
flush_to_newline();
} else {
syntax_error(ec_exp_identifier);
some_error_in_curr_directive = TRUE;
} /* if */
} else {
a_const_char *id_ptr = start_of_curr_token;
sizeof_t id_len = len_of_curr_token;
/* Get the canonical spelling of the identifier. */
if (id_contains_ucn_or_multibyte_char) {
id_ptr = make_canonical_identifier(start_of_curr_token, &id_len,
/*force_ucn=*/FALSE);
} /* if */
if (id_spelling != NULL) {
*id_spelling = alloc_fe(id_len + 2);
strncpy((char*)*id_spelling, id_ptr, size_t_arg(id_len));
((char*)*id_spelling)[id_len] = 0;
} /* if */
/* The identifier __VA_ARGS__ is not allowed if variadic macros are
accepted, and similarly for __VA_OPT__ when va_opt_enabled is
TRUE. */
check_for_reserved_VA_id(id_len, id_ptr);
/* Look to see if there is a macro with this name. */
sym_hdr = find_symbol_header(id_ptr, id_len, &locator_for_curr_id);
assoc_symbol = find_defined_macro(sym_hdr);
if (assoc_symbol != NULL) {
*condition = is_ifdef ? TRUE : FALSE;
mark_referenced(assoc_symbol, &pos_curr_token);
} else {
*condition = is_ifdef ? FALSE : TRUE;
} /* if */
/* Move past the identifier. */
(void)get_token();
ignore_harmless_trailing_comment();
result = TRUE;
} /* if */
return result;
} /* check_if_defined_macro */
static void skip_to_endif(a_boolean stop_skip_on_else_or_elif)
/*
Skip to the #endif corresponding to the current directive. If
stop_skip_on_else_or_elif is true, also stop the skip on a corresponding
#else, #elif, #elifdef, or #elifndef. On entry, the position should be at
the newline of the preprocessing directive that is causing this skip.
*/
{
a_boolean condition;
a_boolean save_currently_in_pp_if_skip = currently_in_pp_if_skip;
a_source_position start_of_dir_position;
a_pp_directive_kind directive;
db_enter(3, "skip_to_endif");
/* Before leaving the current directive, check that all of it was taken.
If this directive should allow a trailing comment, it should have
been skipped before this routine was called. */
end_of_directive_processing();
fetch_pp_tokens = TRUE;
expand_macros = FALSE;
currently_in_pp_if_skip = TRUE;
in_preprocessing_directive = FALSE;
/* Loop until a #endif or the like is found. */
for (;;) {
/* Fetch and throw away tokens, looking for a "#" that is the first
token in its logical source line. */
for (;;) {
/* End of file indicates a missing #endif. End skip, let the driver
routine put out the error. */
if (curr_token == tok_end_of_source) {
some_error_in_curr_directive = TRUE;
goto end_skip;
} /* if */
/* On a "#" at the start of a line, end the loop. get_token will
only return tok_sharp for a "#" that is the first token on a line. */
if (get_token() == tok_sharp) break;
/* Any other token. Ignore it. */
} /* for */
/* "#" found, and it is on a different line than the previous
token, and thus the first token on its line. This is a preprocessing
directive. */
start_of_dir_position = pos_curr_token;
in_preprocessing_directive = TRUE;
/* Identify the directive and process it. */
switch ((int)(directive = identify_dir_keyword())) {
case ppd_endif:
/* #endif, valid end of if-skip. */
proc_endif();
/* See if this is an #endif that also marks a PCH header stop
position. */
if (is_header_stop_position(start_of_dir_position)) {
is_header_stop_dir = TRUE;
} /* if */
goto end_skip;
case ppd_else:
proc_else(/*perform_else=*/FALSE);
/* Stop skipping if we're supposed to stop on an else. */
if (stop_skip_on_else_or_elif) goto end_skip;
break;
case ppd_elif:
case ppd_elifdef:
case ppd_elifndef:
proc_elif(/*treat_as_else=*/FALSE);
/* Stop skipping if we're supposed to stop on an else. */
if (stop_skip_on_else_or_elif) {
if (directive == ppd_elif) {
/* Scan and evaluate the expression. */
scan_if_expr(&condition);
} else {
/* Check whether a macro is defined or not. */
(void)check_if_defined_macro(/*is_ifdef=*/directive == ppd_elifdef,
&condition, /*id_spelling=*/NULL);
} /* if */
/* If the condition is TRUE, stop skipping. */
if (condition) goto end_skip;
/* Check for extra text beyond the end of the expression or
identifier. For the case where the skip is ended, the check
is done at a higher level. */
end_of_directive_processing();
} /* if */
break;
case ppd_if:
case ppd_ifdef:
case ppd_ifndef:
/* Various #if's; we keep skipping, but we do so in a recursive
call to this routine, since we have to find and throw away the
matching #endif. */
/* Throw away the expression. */
flush_to_newline();
push_pp_if_stack();
skip_to_endif(/*stop_skip_on_else_or_elif=*/FALSE);
break;
case ppd_not_valid:
/* Unrecognized directive. Issue a warning in strict mode, and then
ignore it. */
if (strict_ansi_mode) {
pos_warning(ec_bad_pp_directive_keyword, &error_position);
some_error_in_curr_directive = TRUE;
} /* if */
break;
default:;
/* Other directives -- ignored. */
} /* switch */
in_preprocessing_directive = FALSE;
/* Keep looping, looking for directives. */
} /* for */
end_skip:;
currently_in_pp_if_skip = save_currently_in_pp_if_skip;
db_exit();
} /* skip_to_endif */
static void perform_if(a_boolean condition)
/*
Perform the processing for the beginning of an if-test: If condition
is non-zero, set flags to allow a later #else or #endif, and return.
If condition is zero, skip now to the corresponding #else or #endif,
then return.
*/
{
db_enter(3, "perform_if");
#if DEBUG
if (debug_level >= 3) {
fprintf(f_debug, "perform_if, condition = %d\n", condition);
} /* if */
#endif /* DEBUG */
push_pp_if_stack();
if (condition) {
/* The condition is TRUE. No action required, just return. */
} else {
/* The condition is FALSE. Skip to the corresponding #else, #elif,
or #endif. The #if itself should be deleted. */
skip_to_endif(/*stop_skip_on_else_or_elif=*/TRUE);
} /* if */
db_exit();
} /* perform_if */
static void check_for_if_defined_include_guard(void)
/*
This routine is called to determine whether an #if might be an include file
guard test. It is only called if the include guard state indicates
that we are at the start of an include file. It checks to see if the
current #if directive is of one of the following forms:
#if defined(X)
#if !defined(X)
If it is of one of those forms, the include guard state is updated to
indicate that a guard test of the macro X has been seen; otherwise, the
file is marked as not being a candidate for suppression of future includes.
The check is done by examining the current source line to look for one of the
patterns above. The check is very strict (e.g., comments are not allowed
to appear in a valid pattern). The include guard processing is an
optimization, so the failure to detect the presence of a guard does not
affect the proper compilation of the program.
*/
{
a_const_char *ptr;
a_boolean not_operator_present = FALSE;
a_boolean is_possible_include_guard = FALSE;
/* Local macro to skip white space characters. */
#define local_skip_white_space() while (*ptr == ' ' || *ptr == '\t') ptr++
/* The current token is the identifier token for the "if". Start scanning
after the end of that token. */
ptr = end_of_curr_token + 1;
local_skip_white_space();
/* Check for the presence of a "!". */
if (*ptr == '!') {
not_operator_present = TRUE;
ptr++;
} /* if */
local_skip_white_space();
/* Check for the string "defined". */
if (strncmp(ptr, "defined", 7) == 0) {
/* Skip past the "defined". */
ptr += 7;
local_skip_white_space();
/* Check for a "(". */
if (*ptr++ == '(') {
a_const_char *id_start;
sizeof_t id_len;
local_skip_white_space();
/* Find the end of the macro identifier. This only needs to be the
actual end of the identifier for valid cases. Other cases will be
rejected by the call of is_valid_identifier below. */
id_start = ptr;
while (*ptr != ' ' && *ptr != '\t' && *ptr != ')' && *ptr != LE_ESCAPE) {
ptr++;
} /* while */
id_len = (sizeof_t)(ptr - id_start);
local_skip_white_space();
/* Check for a "(". */
if (*ptr++ == ')') {
local_skip_white_space();
/* We should now be at the end of the line. */
if (*ptr == LE_ESCAPE && ptr[1] == LE_NEWLINE) {
a_symbol_ptr sym;
a_symbol_locator locator;
/* The line matches our pattern. If the identifier is valid,
update the include guard information. */
if (is_valid_identifier(id_start, id_len, &sym, &locator)) {
is_possible_include_guard = TRUE;
set_ifg_state(IFG_STATE_INTERMED);
if (not_operator_present) {
curr_ise->include_history->ifndef_guard = TRUE;
} else {
curr_ise->include_history->ifdef_guard = TRUE;
} /* if */
curr_ise->include_history->controlling_macro_name =
locator.symbol_header->identifier;
} /* if */
} /* if */
} /* if */
} /* if */
} /* if */
if (!is_possible_include_guard) {
/* Mark the file as not being a candidate for future suppression. */
set_ifg_state(IFG_STATE_FAIL);
} /* if */
#undef local_skip_white_space
} /* check_for_if_defined_include_guard */
static void proc_if(void)
/*
Scan and process an #if directive.
*/
{
a_boolean condition;
a_byte ifg_state = get_ifg_state();
if (ifg_state == IFG_STATE_START) {
/* We are at the start of an include file. Look for an
"#if !defined(X)" form of include guard. */
check_for_if_defined_include_guard();
} else if (ifg_state == IFG_STATE_ACCEPT) {
set_ifg_state(IFG_STATE_FAIL);
} else {
/* Do nothing if state is FAIL, INTERMED or ONCE. */
} /* if */
scan_if_expr(&condition);
perform_if(condition);
} /* proc_if */
static void proc_ifdef(a_boolean is_ifdef)
/*
Scan and process an #ifdef or #ifndef directive (is_ifdef == TRUE and
FALSE, respectively).
*/
{
a_boolean condition;
a_byte ifg_state = get_ifg_state();
a_const_char *id_ptr;
if (check_if_defined_macro(is_ifdef, &condition, &id_ptr)) {
if (ifg_state == IFG_STATE_START) {
/* If we are at the start of an include file then record
information about this @ifdef so that it can be used later to
see if subsequent includes can be suppressed. */
set_ifg_state(IFG_STATE_INTERMED);
if (is_ifdef) {
curr_ise->include_history->ifdef_guard = TRUE;
} else {
curr_ise->include_history->ifndef_guard = TRUE;
} /* if */
curr_ise->include_history->controlling_macro_name = id_ptr;
} else if (ifg_state == IFG_STATE_ACCEPT) {
set_ifg_state(IFG_STATE_FAIL);
} else {
/* Do nothing if state is FAIL, INTERMED or ONCE. */
} /* if */
} /* if */
/* Do the if processing. */
perform_if(condition);
} /* proc_ifdef */
#if RECORD_MACROS_IN_IL
static void make_il_undef_entry(a_symbol_ptr undef_sym,
a_source_position_ptr undef_pos)
/*
Create an IL entry for an #undef of undef_sym. The #undef has
source position *undef_pos.
*/
{
sizeof_t len;
char *ptr;
a_macro_ptr mp;
/* Allocate an IL area of the right size and put "#undef name" into it. */
#define UNDEF_STR "#undef "
len = sizeof(UNDEF_STR) + undef_sym->header->identifier_length;
ptr = alloc_il(len);