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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
|
/* CPP Library - lexical analysis.
Copyright (C) 2000 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
Broken out to separate file, Zack Weinberg, Mar 2000
Single-pass line tokenization by Neil Booth, April 2000
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 2, 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; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* This lexer works with a single pass of the file. Recently I
re-wrote it to minimize the places where we step backwards in the
input stream, to make future changes to support multi-byte
character sets fairly straight-forward.
There is now only one routine where we do step backwards:
skip_escaped_newlines. This routine could probably also be changed
so that it doesn't need to step back. One possibility is to use a
trick similar to that used in lex_period and lex_percent. Two
extra characters might be needed, but skip_escaped_newlines itself
would probably be the only place that needs to be aware of that,
and changes to the remaining routines would probably only be needed
if they process a backslash. */
#include "config.h"
#include "system.h"
#include "cpplib.h"
#include "cpphash.h"
#include "symcat.h"
/* Tokens with SPELL_STRING store their spelling in the token list,
and it's length in the token->val.name.len. */
enum spell_type
{
SPELL_OPERATOR = 0,
SPELL_CHAR,
SPELL_IDENT,
SPELL_STRING,
SPELL_NONE
};
struct token_spelling
{
enum spell_type category;
const unsigned char *name;
};
const unsigned char *digraph_spellings [] = {U"%:", U"%:%:", U"<:",
U":>", U"<%", U"%>"};
#define OP(e, s) { SPELL_OPERATOR, U s },
#define TK(e, s) { s, U STRINGX (e) },
const struct token_spelling token_spellings [N_TTYPES] = {TTYPE_TABLE };
#undef OP
#undef TK
#define TOKEN_SPELL(token) (token_spellings[(token)->type].category)
#define TOKEN_NAME(token) (token_spellings[(token)->type].name)
static cppchar_t handle_newline PARAMS ((cpp_buffer *, cppchar_t));
static cppchar_t skip_escaped_newlines PARAMS ((cpp_buffer *, cppchar_t));
static cppchar_t get_effective_char PARAMS ((cpp_buffer *));
static int skip_block_comment PARAMS ((cpp_reader *));
static int skip_line_comment PARAMS ((cpp_reader *));
static void adjust_column PARAMS ((cpp_reader *));
static void skip_whitespace PARAMS ((cpp_reader *, cppchar_t));
static cpp_hashnode *parse_identifier PARAMS ((cpp_reader *, cppchar_t));
static void parse_number PARAMS ((cpp_reader *, cpp_string *, cppchar_t, int));
static int unescaped_terminator_p PARAMS ((cpp_reader *, const U_CHAR *));
static void parse_string PARAMS ((cpp_reader *, cpp_token *, cppchar_t));
static void unterminated PARAMS ((cpp_reader *, int));
static int trigraph_ok PARAMS ((cpp_reader *, cppchar_t));
static void save_comment PARAMS ((cpp_reader *, cpp_token *, const U_CHAR *));
static void lex_percent PARAMS ((cpp_buffer *, cpp_token *));
static void lex_dot PARAMS ((cpp_reader *, cpp_token *));
static int name_p PARAMS ((cpp_reader *, const cpp_string *));
static cpp_chunk *new_chunk PARAMS ((unsigned int));
static int chunk_suitable PARAMS ((cpp_pool *, cpp_chunk *, unsigned int));
/* Utility routine:
Compares, the token TOKEN to the NUL-terminated string STRING.
TOKEN must be a CPP_NAME. Returns 1 for equal, 0 for unequal. */
int
cpp_ideq (token, string)
const cpp_token *token;
const char *string;
{
if (token->type != CPP_NAME)
return 0;
return !ustrcmp (token->val.node->name, (const U_CHAR *) string);
}
/* Call when meeting a newline. Returns the character after the newline
(or carriage-return newline combination), or EOF. */
static cppchar_t
handle_newline (buffer, newline_char)
cpp_buffer *buffer;
cppchar_t newline_char;
{
cppchar_t next = EOF;
buffer->col_adjust = 0;
buffer->lineno++;
buffer->line_base = buffer->cur;
/* Handle CR-LF and LF-CR combinations, get the next character. */
if (buffer->cur < buffer->rlimit)
{
next = *buffer->cur++;
if (next + newline_char == '\r' + '\n')
{
buffer->line_base = buffer->cur;
if (buffer->cur < buffer->rlimit)
next = *buffer->cur++;
else
next = EOF;
}
}
buffer->read_ahead = next;
return next;
}
/* Subroutine of skip_escaped_newlines; called when a trigraph is
encountered. It warns if necessary, and returns true if the
trigraph should be honoured. FROM_CHAR is the third character of a
trigraph, and presumed to be the previous character for position
reporting. */
static int
trigraph_ok (pfile, from_char)
cpp_reader *pfile;
cppchar_t from_char;
{
int accept = CPP_OPTION (pfile, trigraphs);
/* Don't warn about trigraphs in comments. */
if (CPP_OPTION (pfile, warn_trigraphs) && !pfile->state.lexing_comment)
{
cpp_buffer *buffer = pfile->buffer;
if (accept)
cpp_warning_with_line (pfile, buffer->lineno, CPP_BUF_COL (buffer) - 2,
"trigraph ??%c converted to %c",
(int) from_char,
(int) _cpp_trigraph_map[from_char]);
else if (buffer->cur != buffer->last_Wtrigraphs)
{
buffer->last_Wtrigraphs = buffer->cur;
cpp_warning_with_line (pfile, buffer->lineno,
CPP_BUF_COL (buffer) - 2,
"trigraph ??%c ignored", (int) from_char);
}
}
return accept;
}
/* Assumes local variables buffer and result. */
#define ACCEPT_CHAR(t) \
do { result->type = t; buffer->read_ahead = EOF; } while (0)
/* When we move to multibyte character sets, add to these something
that saves and restores the state of the multibyte conversion
library. This probably involves saving and restoring a "cookie".
In the case of glibc it is an 8-byte structure, so is not a high
overhead operation. In any case, it's out of the fast path. */
#define SAVE_STATE() do { saved_cur = buffer->cur; } while (0)
#define RESTORE_STATE() do { buffer->cur = saved_cur; } while (0)
/* Skips any escaped newlines introduced by NEXT, which is either a
'?' or a '\\'. Returns the next character, which will also have
been placed in buffer->read_ahead. This routine performs
preprocessing stages 1 and 2 of the ISO C standard. */
static cppchar_t
skip_escaped_newlines (buffer, next)
cpp_buffer *buffer;
cppchar_t next;
{
/* Only do this if we apply stages 1 and 2. */
if (!buffer->from_stage3)
{
cppchar_t next1;
const unsigned char *saved_cur;
int space;
do
{
if (buffer->cur == buffer->rlimit)
break;
SAVE_STATE ();
if (next == '?')
{
next1 = *buffer->cur++;
if (next1 != '?' || buffer->cur == buffer->rlimit)
{
RESTORE_STATE ();
break;
}
next1 = *buffer->cur++;
if (!_cpp_trigraph_map[next1]
|| !trigraph_ok (buffer->pfile, next1))
{
RESTORE_STATE ();
break;
}
/* We have a full trigraph here. */
next = _cpp_trigraph_map[next1];
if (next != '\\' || buffer->cur == buffer->rlimit)
break;
SAVE_STATE ();
}
/* We have a backslash, and room for at least one more character. */
space = 0;
do
{
next1 = *buffer->cur++;
if (!is_nvspace (next1))
break;
space = 1;
}
while (buffer->cur < buffer->rlimit);
if (!is_vspace (next1))
{
RESTORE_STATE ();
break;
}
if (space)
cpp_warning (buffer->pfile,
"backslash and newline separated by space");
next = handle_newline (buffer, next1);
if (next == EOF)
cpp_pedwarn (buffer->pfile, "backslash-newline at end of file");
}
while (next == '\\' || next == '?');
}
buffer->read_ahead = next;
return next;
}
/* Obtain the next character, after trigraph conversion and skipping
an arbitrary string of escaped newlines. The common case of no
trigraphs or escaped newlines falls through quickly. */
static cppchar_t
get_effective_char (buffer)
cpp_buffer *buffer;
{
cppchar_t next = EOF;
if (buffer->cur < buffer->rlimit)
{
next = *buffer->cur++;
/* '?' can introduce trigraphs (and therefore backslash); '\\'
can introduce escaped newlines, which we want to skip, or
UCNs, which, depending upon lexer state, we will handle in
the future. */
if (next == '?' || next == '\\')
next = skip_escaped_newlines (buffer, next);
}
buffer->read_ahead = next;
return next;
}
/* Skip a C-style block comment. We find the end of the comment by
seeing if an asterisk is before every '/' we encounter. Returns
non-zero if comment terminated by EOF, zero otherwise. */
static int
skip_block_comment (pfile)
cpp_reader *pfile;
{
cpp_buffer *buffer = pfile->buffer;
cppchar_t c = EOF, prevc = EOF;
pfile->state.lexing_comment = 1;
while (buffer->cur != buffer->rlimit)
{
prevc = c, c = *buffer->cur++;
next_char:
/* FIXME: For speed, create a new character class of characters
of interest inside block comments. */
if (c == '?' || c == '\\')
c = skip_escaped_newlines (buffer, c);
/* People like decorating comments with '*', so check for '/'
instead for efficiency. */
if (c == '/')
{
if (prevc == '*')
break;
/* Warn about potential nested comments, but not if the '/'
comes immediately before the true comment delimeter.
Don't bother to get it right across escaped newlines. */
if (CPP_OPTION (pfile, warn_comments)
&& buffer->cur != buffer->rlimit)
{
prevc = c, c = *buffer->cur++;
if (c == '*' && buffer->cur != buffer->rlimit)
{
prevc = c, c = *buffer->cur++;
if (c != '/')
cpp_warning_with_line (pfile, CPP_BUF_LINE (buffer),
CPP_BUF_COL (buffer),
"\"/*\" within comment");
}
goto next_char;
}
}
else if (is_vspace (c))
{
prevc = c, c = handle_newline (buffer, c);
goto next_char;
}
else if (c == '\t')
adjust_column (pfile);
}
pfile->state.lexing_comment = 0;
buffer->read_ahead = EOF;
return c != '/' || prevc != '*';
}
/* Skip a C++ line comment. Handles escaped newlines. Returns
non-zero if a multiline comment. The following new line, if any,
is left in buffer->read_ahead. */
static int
skip_line_comment (pfile)
cpp_reader *pfile;
{
cpp_buffer *buffer = pfile->buffer;
unsigned int orig_lineno = buffer->lineno;
cppchar_t c;
pfile->state.lexing_comment = 1;
do
{
c = EOF;
if (buffer->cur == buffer->rlimit)
break;
c = *buffer->cur++;
if (c == '?' || c == '\\')
c = skip_escaped_newlines (buffer, c);
}
while (!is_vspace (c));
pfile->state.lexing_comment = 0;
buffer->read_ahead = c; /* Leave any newline for caller. */
return orig_lineno != buffer->lineno;
}
/* pfile->buffer->cur is one beyond the \t character. Update
col_adjust so we track the column correctly. */
static void
adjust_column (pfile)
cpp_reader *pfile;
{
cpp_buffer *buffer = pfile->buffer;
unsigned int col = CPP_BUF_COL (buffer) - 1; /* Zero-based column. */
/* Round it up to multiple of the tabstop, but subtract 1 since the
tab itself occupies a character position. */
buffer->col_adjust += (CPP_OPTION (pfile, tabstop)
- col % CPP_OPTION (pfile, tabstop)) - 1;
}
/* Skips whitespace, saving the next non-whitespace character.
Adjusts pfile->col_adjust to account for tabs. Without this,
tokens might be assigned an incorrect column. */
static void
skip_whitespace (pfile, c)
cpp_reader *pfile;
cppchar_t c;
{
cpp_buffer *buffer = pfile->buffer;
unsigned int warned = 0;
do
{
/* Horizontal space always OK. */
if (c == ' ')
;
else if (c == '\t')
adjust_column (pfile);
/* Just \f \v or \0 left. */
else if (c == '\0')
{
if (!warned)
{
cpp_warning (pfile, "null character(s) ignored");
warned = 1;
}
}
else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
cpp_pedwarn_with_line (pfile, CPP_BUF_LINE (buffer),
CPP_BUF_COL (buffer),
"%s in preprocessing directive",
c == '\f' ? "form feed" : "vertical tab");
c = EOF;
if (buffer->cur == buffer->rlimit)
break;
c = *buffer->cur++;
}
/* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
while (is_nvspace (c));
/* Remember the next character. */
buffer->read_ahead = c;
}
/* See if the characters of a number token are valid in a name (no
'.', '+' or '-'). */
static int
name_p (pfile, string)
cpp_reader *pfile;
const cpp_string *string;
{
unsigned int i;
for (i = 0; i < string->len; i++)
if (!is_idchar (string->text[i]))
return 0;
return 1;
}
/* Parse an identifier, skipping embedded backslash-newlines.
Calculate the hash value of the token while parsing, for improved
performance. The hashing algorithm *must* match cpp_lookup(). */
static cpp_hashnode *
parse_identifier (pfile, c)
cpp_reader *pfile;
cppchar_t c;
{
cpp_hashnode *result;
cpp_buffer *buffer = pfile->buffer;
unsigned char *dest, *limit;
unsigned int r = 0, saw_dollar = 0;
dest = POOL_FRONT (&pfile->ident_pool);
limit = POOL_LIMIT (&pfile->ident_pool);
do
{
do
{
/* Need room for terminating null. */
if (dest + 1 >= limit)
limit = _cpp_next_chunk (&pfile->ident_pool, 0, &dest);
*dest++ = c;
r = HASHSTEP (r, c);
if (c == '$')
saw_dollar++;
c = EOF;
if (buffer->cur == buffer->rlimit)
break;
c = *buffer->cur++;
}
while (is_idchar (c));
/* Potential escaped newline? */
if (c != '?' && c != '\\')
break;
c = skip_escaped_newlines (buffer, c);
}
while (is_idchar (c));
/* Remember the next character. */
buffer->read_ahead = c;
/* $ is not a identifier character in the standard, but is commonly
accepted as an extension. Don't warn about it in skipped
conditional blocks. */
if (saw_dollar && CPP_PEDANTIC (pfile) && ! pfile->skipping)
cpp_pedwarn (pfile, "'$' character(s) in identifier");
/* Identifiers are null-terminated. */
*dest = '\0';
/* This routine commits the memory if necessary. */
result = _cpp_lookup_with_hash (pfile,
dest - POOL_FRONT (&pfile->ident_pool), r);
/* Some identifiers require diagnostics when lexed. */
if (result->flags & NODE_DIAGNOSTIC && !pfile->skipping)
{
/* It is allowed to poison the same identifier twice. */
if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
cpp_error (pfile, "attempt to use poisoned \"%s\"", result->name);
/* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
replacement list of a variable-arguments macro. */
if (result == pfile->spec_nodes.n__VA_ARGS__
&& !pfile->state.va_args_ok)
cpp_pedwarn (pfile, "__VA_ARGS__ can only appear in the expansion of a C99 variable-argument macro");
}
return result;
}
/* Parse a number, skipping embedded backslash-newlines. */
static void
parse_number (pfile, number, c, leading_period)
cpp_reader *pfile;
cpp_string *number;
cppchar_t c;
int leading_period;
{
cpp_buffer *buffer = pfile->buffer;
cpp_pool *pool = pfile->string_pool;
unsigned char *dest, *limit;
dest = POOL_FRONT (pool);
limit = POOL_LIMIT (pool);
/* Place a leading period. */
if (leading_period)
{
if (dest >= limit)
limit = _cpp_next_chunk (pool, 0, &dest);
*dest++ = '.';
}
do
{
do
{
/* Need room for terminating null. */
if (dest + 1 >= limit)
limit = _cpp_next_chunk (pool, 0, &dest);
*dest++ = c;
c = EOF;
if (buffer->cur == buffer->rlimit)
break;
c = *buffer->cur++;
}
while (is_numchar (c) || c == '.' || VALID_SIGN (c, dest[-1]));
/* Potential escaped newline? */
if (c != '?' && c != '\\')
break;
c = skip_escaped_newlines (buffer, c);
}
while (is_numchar (c) || c == '.' || VALID_SIGN (c, dest[-1]));
/* Remember the next character. */
buffer->read_ahead = c;
/* Null-terminate the number. */
*dest = '\0';
number->text = POOL_FRONT (pool);
number->len = dest - number->text;
POOL_COMMIT (pool, number->len + 1);
}
/* Subroutine of parse_string. Emits error for unterminated strings. */
static void
unterminated (pfile, term)
cpp_reader *pfile;
int term;
{
cpp_error (pfile, "missing terminating %c character", term);
if (term == '\"' && pfile->mlstring_pos.line
&& pfile->mlstring_pos.line != pfile->lexer_pos.line)
{
cpp_error_with_line (pfile, pfile->mlstring_pos.line,
pfile->mlstring_pos.col,
"possible start of unterminated string literal");
pfile->mlstring_pos.line = 0;
}
}
/* Subroutine of parse_string. */
static int
unescaped_terminator_p (pfile, dest)
cpp_reader *pfile;
const unsigned char *dest;
{
const unsigned char *start, *temp;
/* In #include-style directives, terminators are not escapeable. */
if (pfile->state.angled_headers)
return 1;
start = POOL_FRONT (pfile->string_pool);
/* An odd number of consecutive backslashes represents an escaped
terminator. */
for (temp = dest; temp > start && temp[-1] == '\\'; temp--)
;
return ((dest - temp) & 1) == 0;
}
/* Parses a string, character constant, or angle-bracketed header file
name. Handles embedded trigraphs and escaped newlines.
Multi-line strings are allowed, but they are deprecated within
directives. */
static void
parse_string (pfile, token, terminator)
cpp_reader *pfile;
cpp_token *token;
cppchar_t terminator;
{
cpp_buffer *buffer = pfile->buffer;
cpp_pool *pool = pfile->string_pool;
unsigned char *dest, *limit;
cppchar_t c;
unsigned int nulls = 0;
dest = POOL_FRONT (pool);
limit = POOL_LIMIT (pool);
for (;;)
{
if (buffer->cur == buffer->rlimit)
{
c = EOF;
unterminated (pfile, terminator);
break;
}
c = *buffer->cur++;
have_char:
/* Handle trigraphs, escaped newlines etc. */
if (c == '?' || c == '\\')
c = skip_escaped_newlines (buffer, c);
if (c == terminator && unescaped_terminator_p (pfile, dest))
{
c = EOF;
break;
}
else if (is_vspace (c))
{
/* In assembly language, silently terminate string and
character literals at end of line. This is a kludge
around not knowing where comments are. */
if (CPP_OPTION (pfile, lang_asm) && terminator != '>')
break;
/* Character constants and header names may not extend over
multiple lines. In Standard C, neither may strings.
Unfortunately, we accept multiline strings as an
extension, except in #include family directives. */
if (terminator != '"' || pfile->state.angled_headers)
{
unterminated (pfile, terminator);
break;
}
if (pfile->mlstring_pos.line == 0)
{
pfile->mlstring_pos = pfile->lexer_pos;
if (CPP_PEDANTIC (pfile))
cpp_pedwarn (pfile, "multi-line string constant");
}
handle_newline (buffer, c); /* Stores to read_ahead. */
c = '\n';
}
else if (c == '\0')
{
if (nulls++ == 0)
cpp_warning (pfile, "null character(s) preserved in literal");
}
/* No terminating null for strings - they could contain nulls. */
if (dest >= limit)
limit = _cpp_next_chunk (pool, 0, &dest);
*dest++ = c;
/* If we had a new line, the next character is in read_ahead. */
if (c != '\n')
continue;
c = buffer->read_ahead;
if (c != EOF)
goto have_char;
}
/* Remember the next character. */
buffer->read_ahead = c;
token->val.str.text = POOL_FRONT (pool);
token->val.str.len = dest - token->val.str.text;
POOL_COMMIT (pool, token->val.str.len);
}
/* The stored comment includes the comment start and any terminator. */
static void
save_comment (pfile, token, from)
cpp_reader *pfile;
cpp_token *token;
const unsigned char *from;
{
unsigned char *buffer;
unsigned int len;
len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */
/* C++ comments probably (not definitely) have moved past a new
line, which we don't want to save in the comment. */
if (pfile->buffer->read_ahead != EOF)
len--;
buffer = _cpp_pool_alloc (pfile->string_pool, len);
token->type = CPP_COMMENT;
token->val.str.len = len;
token->val.str.text = buffer;
buffer[0] = '/';
memcpy (buffer + 1, from, len - 1);
}
/* Subroutine of lex_token to handle '%'. A little tricky, since we
want to avoid stepping back when lexing %:%X. */
static void
lex_percent (buffer, result)
cpp_buffer *buffer;
cpp_token *result;
{
cppchar_t c;
result->type = CPP_MOD;
/* Parsing %:%X could leave an extra character. */
if (buffer->extra_char == EOF)
c = get_effective_char (buffer);
else
{
c = buffer->read_ahead = buffer->extra_char;
buffer->extra_char = EOF;
}
if (c == '=')
ACCEPT_CHAR (CPP_MOD_EQ);
else if (CPP_OPTION (buffer->pfile, digraphs))
{
if (c == ':')
{
result->flags |= DIGRAPH;
ACCEPT_CHAR (CPP_HASH);
if (get_effective_char (buffer) == '%')
{
buffer->extra_char = get_effective_char (buffer);
if (buffer->extra_char == ':')
{
buffer->extra_char = EOF;
ACCEPT_CHAR (CPP_PASTE);
}
else
/* We'll catch the extra_char when we're called back. */
buffer->read_ahead = '%';
}
}
else if (c == '>')
{
result->flags |= DIGRAPH;
ACCEPT_CHAR (CPP_CLOSE_BRACE);
}
}
}
/* Subroutine of lex_token to handle '.'. This is tricky, since we
want to avoid stepping back when lexing '...' or '.123'. In the
latter case we should also set a flag for parse_number. */
static void
lex_dot (pfile, result)
cpp_reader *pfile;
cpp_token *result;
{
cpp_buffer *buffer = pfile->buffer;
cppchar_t c;
/* Parsing ..X could leave an extra character. */
if (buffer->extra_char == EOF)
c = get_effective_char (buffer);
else
{
c = buffer->read_ahead = buffer->extra_char;
buffer->extra_char = EOF;
}
/* All known character sets have 0...9 contiguous. */
if (c >= '0' && c <= '9')
{
result->type = CPP_NUMBER;
parse_number (pfile, &result->val.str, c, 1);
}
else
{
result->type = CPP_DOT;
if (c == '.')
{
buffer->extra_char = get_effective_char (buffer);
if (buffer->extra_char == '.')
{
buffer->extra_char = EOF;
ACCEPT_CHAR (CPP_ELLIPSIS);
}
else
/* We'll catch the extra_char when we're called back. */
buffer->read_ahead = '.';
}
else if (c == '*' && CPP_OPTION (pfile, cplusplus))
ACCEPT_CHAR (CPP_DOT_STAR);
}
}
void
_cpp_lex_token (pfile, result)
cpp_reader *pfile;
cpp_token *result;
{
cppchar_t c;
cpp_buffer *buffer;
const unsigned char *comment_start;
unsigned char bol = pfile->state.next_bol;
done_directive:
buffer = pfile->buffer;
pfile->state.next_bol = 0;
result->flags = 0;
next_char:
pfile->lexer_pos.line = buffer->lineno;
next_char2:
pfile->lexer_pos.col = CPP_BUF_COLUMN (buffer, buffer->cur);
c = buffer->read_ahead;
if (c == EOF && buffer->cur < buffer->rlimit)
{
c = *buffer->cur++;
pfile->lexer_pos.col++;
}
do_switch:
buffer->read_ahead = EOF;
switch (c)
{
case EOF:
/* Non-empty files should end in a newline. Ignore for command
line and _Pragma buffers. */
if (pfile->lexer_pos.col != 0 && !buffer->from_stage3)
cpp_pedwarn (pfile, "no newline at end of file");
pfile->state.next_bol = 1;
pfile->skipping = 0; /* In case missing #endif. */
result->type = CPP_EOF;
/* Don't do MI optimisation. */
return;
case ' ': case '\t': case '\f': case '\v': case '\0':
skip_whitespace (pfile, c);
result->flags |= PREV_WHITE;
goto next_char2;
case '\n': case '\r':
if (!pfile->state.in_directive)
{
handle_newline (buffer, c);
bol = 1;
pfile->lexer_pos.output_line = buffer->lineno;
/* Newlines in arguments are white space (6.10.3.10).
Otherwise, clear any white space flag. */
if (pfile->state.parsing_args)
result->flags |= PREV_WHITE;
else
result->flags &= ~PREV_WHITE;
goto next_char;
}
/* Don't let directives spill over to the next line. */
buffer->read_ahead = c;
pfile->state.next_bol = 1;
result->type = CPP_EOF;
break;
case '?':
case '\\':
/* These could start an escaped newline, or '?' a trigraph. Let
skip_escaped_newlines do all the work. */
{
unsigned int lineno = buffer->lineno;
c = skip_escaped_newlines (buffer, c);
if (lineno != buffer->lineno)
/* We had at least one escaped newline of some sort, and the
next character is in buffer->read_ahead. Update the
token's line and column. */
goto next_char;
/* We are either the original '?' or '\\', or a trigraph. */
result->type = CPP_QUERY;
buffer->read_ahead = EOF;
if (c == '\\')
goto random_char;
else if (c != '?')
goto do_switch;
}
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
result->type = CPP_NUMBER;
parse_number (pfile, &result->val.str, c, 0);
break;
case '$':
if (!CPP_OPTION (pfile, dollars_in_ident))
goto random_char;
/* Fall through... */
case '_':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
case 's': case 't': case 'u': case 'v': case 'w': case 'x':
case 'y': case 'z':
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
case 'Y': case 'Z':
result->type = CPP_NAME;
result->val.node = parse_identifier (pfile, c);
/* 'L' may introduce wide characters or strings. */
if (result->val.node == pfile->spec_nodes.n_L)
{
c = buffer->read_ahead; /* For make_string. */
if (c == '\'' || c == '"')
{
ACCEPT_CHAR (c == '"' ? CPP_WSTRING: CPP_WCHAR);
goto make_string;
}
}
/* Convert named operators to their proper types. */
else if (result->val.node->flags & NODE_OPERATOR)
{
result->flags |= NAMED_OP;
result->type = result->val.node->value.operator;
}
break;
case '\'':
case '"':
result->type = c == '"' ? CPP_STRING: CPP_CHAR;
make_string:
parse_string (pfile, result, c);
break;
case '/':
/* A potential block or line comment. */
comment_start = buffer->cur;
result->type = CPP_DIV;
c = get_effective_char (buffer);
if (c == '=')
ACCEPT_CHAR (CPP_DIV_EQ);
if (c != '/' && c != '*')
break;
if (c == '*')
{
if (skip_block_comment (pfile))
cpp_error_with_line (pfile, pfile->lexer_pos.line,
pfile->lexer_pos.col,
"unterminated comment");
}
else
{
if (!CPP_OPTION (pfile, cplusplus_comments)
&& !CPP_IN_SYSTEM_HEADER (pfile))
break;
/* We silently allow C++ comments in system headers,
irrespective of conformance mode, because lots of
broken systems do that and trying to clean it up in
fixincludes is a nightmare. */
if (CPP_OPTION (pfile, c89) && CPP_PEDANTIC (pfile)
&& ! buffer->warned_cplusplus_comments)
{
cpp_pedwarn (pfile,
"C++ style comments are not allowed in ISO C89");
cpp_pedwarn (pfile,
"(this will be reported only once per input file)");
buffer->warned_cplusplus_comments = 1;
}
/* Skip_line_comment updates buffer->read_ahead. */
if (skip_line_comment (pfile))
cpp_warning_with_line (pfile, pfile->lexer_pos.line,
pfile->lexer_pos.col,
"multi-line comment");
}
/* Skipping the comment has updated buffer->read_ahead. */
if (!pfile->state.save_comments)
{
result->flags |= PREV_WHITE;
goto next_char;
}
/* Save the comment as a token in its own right. */
save_comment (pfile, result, comment_start);
/* Don't do MI optimisation. */
return;
case '<':
if (pfile->state.angled_headers)
{
result->type = CPP_HEADER_NAME;
c = '>'; /* terminator. */
goto make_string;
}
result->type = CPP_LESS;
c = get_effective_char (buffer);
if (c == '=')
ACCEPT_CHAR (CPP_LESS_EQ);
else if (c == '<')
{
ACCEPT_CHAR (CPP_LSHIFT);
if (get_effective_char (buffer) == '=')
ACCEPT_CHAR (CPP_LSHIFT_EQ);
}
else if (c == '?' && CPP_OPTION (pfile, cplusplus))
{
ACCEPT_CHAR (CPP_MIN);
if (get_effective_char (buffer) == '=')
ACCEPT_CHAR (CPP_MIN_EQ);
}
else if (c == ':' && CPP_OPTION (pfile, digraphs))
{
ACCEPT_CHAR (CPP_OPEN_SQUARE);
result->flags |= DIGRAPH;
}
else if (c == '%' && CPP_OPTION (pfile, digraphs))
{
ACCEPT_CHAR (CPP_OPEN_BRACE);
result->flags |= DIGRAPH;
}
break;
case '>':
result->type = CPP_GREATER;
c = get_effective_char (buffer);
if (c == '=')
ACCEPT_CHAR (CPP_GREATER_EQ);
else if (c == '>')
{
ACCEPT_CHAR (CPP_RSHIFT);
if (get_effective_char (buffer) == '=')
ACCEPT_CHAR (CPP_RSHIFT_EQ);
}
else if (c == '?' && CPP_OPTION (pfile, cplusplus))
{
ACCEPT_CHAR (CPP_MAX);
if (get_effective_char (buffer) == '=')
ACCEPT_CHAR (CPP_MAX_EQ);
}
break;
case '%':
lex_percent (buffer, result);
if (result->type == CPP_HASH)
goto do_hash;
break;
case '.':
lex_dot (pfile, result);
break;
case '+':
result->type = CPP_PLUS;
c = get_effective_char (buffer);
if (c == '=')
ACCEPT_CHAR (CPP_PLUS_EQ);
else if (c == '+')
ACCEPT_CHAR (CPP_PLUS_PLUS);
break;
case '-':
result->type = CPP_MINUS;
c = get_effective_char (buffer);
if (c == '>')
{
ACCEPT_CHAR (CPP_DEREF);
if (CPP_OPTION (pfile, cplusplus)
&& get_effective_char (buffer) == '*')
ACCEPT_CHAR (CPP_DEREF_STAR);
}
else if (c == '=')
ACCEPT_CHAR (CPP_MINUS_EQ);
else if (c == '-')
ACCEPT_CHAR (CPP_MINUS_MINUS);
break;
case '*':
result->type = CPP_MULT;
if (get_effective_char (buffer) == '=')
ACCEPT_CHAR (CPP_MULT_EQ);
break;
case '=':
result->type = CPP_EQ;
if (get_effective_char (buffer) == '=')
ACCEPT_CHAR (CPP_EQ_EQ);
break;
case '!':
result->type = CPP_NOT;
if (get_effective_char (buffer) == '=')
ACCEPT_CHAR (CPP_NOT_EQ);
break;
case '&':
result->type = CPP_AND;
c = get_effective_char (buffer);
if (c == '=')
ACCEPT_CHAR (CPP_AND_EQ);
else if (c == '&')
ACCEPT_CHAR (CPP_AND_AND);
break;
case '#':
c = buffer->extra_char; /* Can be set by error condition below. */
if (c != EOF)
{
buffer->read_ahead = c;
buffer->extra_char = EOF;
}
else
c = get_effective_char (buffer);
if (c == '#')
{
ACCEPT_CHAR (CPP_PASTE);
break;
}
result->type = CPP_HASH;
do_hash:
if (bol)
{
if (pfile->state.parsing_args)
{
/* 6.10.3 paragraph 11: If there are sequences of
preprocessing tokens within the list of arguments that
would otherwise act as preprocessing directives, the
behavior is undefined.
This implementation will report a hard error, terminate
the macro invocation, and proceed to process the
directive. */
cpp_error (pfile,
"directives may not be used inside a macro argument");
/* Put a '#' in lookahead, return CPP_EOF for parse_arg. */
buffer->extra_char = buffer->read_ahead;
buffer->read_ahead = '#';
pfile->state.next_bol = 1;
result->type = CPP_EOF;
/* Get whitespace right - newline_in_args sets it. */
if (pfile->lexer_pos.col == 1)
result->flags &= ~PREV_WHITE;
}
else
{
/* This is the hash introducing a directive. */
if (_cpp_handle_directive (pfile, result->flags & PREV_WHITE))
goto done_directive; /* bol still 1. */
/* This is in fact an assembler #. */
}
}
break;
case '|':
result->type = CPP_OR;
c = get_effective_char (buffer);
if (c == '=')
ACCEPT_CHAR (CPP_OR_EQ);
else if (c == '|')
ACCEPT_CHAR (CPP_OR_OR);
break;
case '^':
result->type = CPP_XOR;
if (get_effective_char (buffer) == '=')
ACCEPT_CHAR (CPP_XOR_EQ);
break;
case ':':
result->type = CPP_COLON;
c = get_effective_char (buffer);
if (c == ':' && CPP_OPTION (pfile, cplusplus))
ACCEPT_CHAR (CPP_SCOPE);
else if (c == '>' && CPP_OPTION (pfile, digraphs))
{
result->flags |= DIGRAPH;
ACCEPT_CHAR (CPP_CLOSE_SQUARE);
}
break;
case '~': result->type = CPP_COMPL; break;
case ',': result->type = CPP_COMMA; break;
case '(': result->type = CPP_OPEN_PAREN; break;
case ')': result->type = CPP_CLOSE_PAREN; break;
case '[': result->type = CPP_OPEN_SQUARE; break;
case ']': result->type = CPP_CLOSE_SQUARE; break;
case '{': result->type = CPP_OPEN_BRACE; break;
case '}': result->type = CPP_CLOSE_BRACE; break;
case ';': result->type = CPP_SEMICOLON; break;
case '@':
if (CPP_OPTION (pfile, objc))
{
/* In Objective C, '@' may begin keywords or strings, like
@keyword or @"string". It would be nice to call
get_effective_char here and test the result. However, we
would then need to pass 2 characters to parse_identifier,
making it ugly and slowing down its main loop. Instead,
we assume we have an identifier, and recover if not. */
result->type = CPP_NAME;
result->val.node = parse_identifier (pfile, c);
if (result->val.node->length != 1)
break;
/* OK, so it wasn't an identifier. Maybe a string? */
if (buffer->read_ahead == '"')
{
c = '"';
ACCEPT_CHAR (CPP_OSTRING);
goto make_string;
}
}
goto random_char;
random_char:
default:
result->type = CPP_OTHER;
result->val.c = c;
break;
}
/* If not in a directive, this token invalidates controlling macros. */
if (!pfile->state.in_directive)
pfile->mi_state = MI_FAILED;
}
/* An upper bound on the number of bytes needed to spell a token,
including preceding whitespace. */
unsigned int
cpp_token_len (token)
const cpp_token *token;
{
unsigned int len;
switch (TOKEN_SPELL (token))
{
default: len = 0; break;
case SPELL_STRING: len = token->val.str.len; break;
case SPELL_IDENT: len = token->val.node->length; break;
}
/* 1 for whitespace, 4 for comment delimeters. */
return len + 5;
}
/* Write the spelling of a token TOKEN to BUFFER. The buffer must
already contain the enough space to hold the token's spelling.
Returns a pointer to the character after the last character
written. */
unsigned char *
cpp_spell_token (pfile, token, buffer)
cpp_reader *pfile; /* Would be nice to be rid of this... */
const cpp_token *token;
unsigned char *buffer;
{
switch (TOKEN_SPELL (token))
{
case SPELL_OPERATOR:
{
const unsigned char *spelling;
unsigned char c;
if (token->flags & DIGRAPH)
spelling = digraph_spellings[token->type - CPP_FIRST_DIGRAPH];
else if (token->flags & NAMED_OP)
goto spell_ident;
else
spelling = TOKEN_NAME (token);
while ((c = *spelling++) != '\0')
*buffer++ = c;
}
break;
case SPELL_IDENT:
spell_ident:
memcpy (buffer, token->val.node->name, token->val.node->length);
buffer += token->val.node->length;
break;
case SPELL_STRING:
{
int left, right, tag;
switch (token->type)
{
case CPP_STRING: left = '"'; right = '"'; tag = '\0'; break;
case CPP_WSTRING: left = '"'; right = '"'; tag = 'L'; break;
case CPP_OSTRING: left = '"'; right = '"'; tag = '@'; break;
case CPP_CHAR: left = '\''; right = '\''; tag = '\0'; break;
case CPP_WCHAR: left = '\''; right = '\''; tag = 'L'; break;
case CPP_HEADER_NAME: left = '<'; right = '>'; tag = '\0'; break;
default: left = '\0'; right = '\0'; tag = '\0'; break;
}
if (tag) *buffer++ = tag;
if (left) *buffer++ = left;
memcpy (buffer, token->val.str.text, token->val.str.len);
buffer += token->val.str.len;
if (right) *buffer++ = right;
}
break;
case SPELL_CHAR:
*buffer++ = token->val.c;
break;
case SPELL_NONE:
cpp_ice (pfile, "Unspellable token %s", TOKEN_NAME (token));
break;
}
return buffer;
}
/* Returns a token as a null-terminated string. The string is
temporary, and automatically freed later. Useful for diagnostics. */
unsigned char *
cpp_token_as_text (pfile, token)
cpp_reader *pfile;
const cpp_token *token;
{
unsigned int len = cpp_token_len (token);
unsigned char *start = _cpp_pool_alloc (&pfile->temp_string_pool, len), *end;
end = cpp_spell_token (pfile, token, start);
end[0] = '\0';
return start;
}
/* Used by C front ends. Should really move to using cpp_token_as_text. */
const char *
cpp_type2name (type)
enum cpp_ttype type;
{
return (const char *) token_spellings[type].name;
}
/* Writes the spelling of token to FP. Separate from cpp_spell_token
for efficiency - to avoid double-buffering. Also, outputs a space
if PREV_WHITE is flagged. */
void
cpp_output_token (token, fp)
const cpp_token *token;
FILE *fp;
{
if (token->flags & PREV_WHITE)
putc (' ', fp);
switch (TOKEN_SPELL (token))
{
case SPELL_OPERATOR:
{
const unsigned char *spelling;
if (token->flags & DIGRAPH)
spelling = digraph_spellings[token->type - CPP_FIRST_DIGRAPH];
else if (token->flags & NAMED_OP)
goto spell_ident;
else
spelling = TOKEN_NAME (token);
ufputs (spelling, fp);
}
break;
spell_ident:
case SPELL_IDENT:
ufputs (token->val.node->name, fp);
break;
case SPELL_STRING:
{
int left, right, tag;
switch (token->type)
{
case CPP_STRING: left = '"'; right = '"'; tag = '\0'; break;
case CPP_WSTRING: left = '"'; right = '"'; tag = 'L'; break;
case CPP_OSTRING: left = '"'; right = '"'; tag = '@'; break;
case CPP_CHAR: left = '\''; right = '\''; tag = '\0'; break;
case CPP_WCHAR: left = '\''; right = '\''; tag = 'L'; break;
case CPP_HEADER_NAME: left = '<'; right = '>'; tag = '\0'; break;
default: left = '\0'; right = '\0'; tag = '\0'; break;
}
if (tag) putc (tag, fp);
if (left) putc (left, fp);
fwrite (token->val.str.text, 1, token->val.str.len, fp);
if (right) putc (right, fp);
}
break;
case SPELL_CHAR:
putc (token->val.c, fp);
break;
case SPELL_NONE:
/* An error, most probably. */
break;
}
}
/* Compare two tokens. */
int
_cpp_equiv_tokens (a, b)
const cpp_token *a, *b;
{
if (a->type == b->type && a->flags == b->flags)
switch (TOKEN_SPELL (a))
{
default: /* Keep compiler happy. */
case SPELL_OPERATOR:
return 1;
case SPELL_CHAR:
return a->val.c == b->val.c; /* Character. */
case SPELL_NONE:
return (a->type != CPP_MACRO_ARG || a->val.arg_no == b->val.arg_no);
case SPELL_IDENT:
return a->val.node == b->val.node;
case SPELL_STRING:
return (a->val.str.len == b->val.str.len
&& !memcmp (a->val.str.text, b->val.str.text,
a->val.str.len));
}
return 0;
}
#if 0
/* Compare two token lists. */
int
_cpp_equiv_toklists (a, b)
const struct toklist *a, *b;
{
unsigned int i, count;
count = a->limit - a->first;
if (count != (b->limit - b->first))
return 0;
for (i = 0; i < count; i++)
if (! _cpp_equiv_tokens (&a->first[i], &b->first[i]))
return 0;
return 1;
}
#endif
/* Determine whether two tokens can be pasted together, and if so,
what the resulting token is. Returns CPP_EOF if the tokens cannot
be pasted, or the appropriate type for the merged token if they
can. */
enum cpp_ttype
cpp_can_paste (pfile, token1, token2, digraph)
cpp_reader * pfile;
const cpp_token *token1, *token2;
int* digraph;
{
enum cpp_ttype a = token1->type, b = token2->type;
int cxx = CPP_OPTION (pfile, cplusplus);
/* Treat named operators as if they were ordinary NAMEs. */
if (token1->flags & NAMED_OP)
a = CPP_NAME;
if (token2->flags & NAMED_OP)
b = CPP_NAME;
if (a <= CPP_LAST_EQ && b == CPP_EQ)
return a + (CPP_EQ_EQ - CPP_EQ);
switch (a)
{
case CPP_GREATER:
if (b == a) return CPP_RSHIFT;
if (b == CPP_QUERY && cxx) return CPP_MAX;
if (b == CPP_GREATER_EQ) return CPP_RSHIFT_EQ;
break;
case CPP_LESS:
if (b == a) return CPP_LSHIFT;
if (b == CPP_QUERY && cxx) return CPP_MIN;
if (b == CPP_LESS_EQ) return CPP_LSHIFT_EQ;
if (CPP_OPTION (pfile, digraphs))
{
if (b == CPP_COLON)
{*digraph = 1; return CPP_OPEN_SQUARE;} /* <: digraph */
if (b == CPP_MOD)
{*digraph = 1; return CPP_OPEN_BRACE;} /* <% digraph */
}
break;
case CPP_PLUS: if (b == a) return CPP_PLUS_PLUS; break;
case CPP_AND: if (b == a) return CPP_AND_AND; break;
case CPP_OR: if (b == a) return CPP_OR_OR; break;
case CPP_MINUS:
if (b == a) return CPP_MINUS_MINUS;
if (b == CPP_GREATER) return CPP_DEREF;
break;
case CPP_COLON:
if (b == a && cxx) return CPP_SCOPE;
if (b == CPP_GREATER && CPP_OPTION (pfile, digraphs))
{*digraph = 1; return CPP_CLOSE_SQUARE;} /* :> digraph */
break;
case CPP_MOD:
if (CPP_OPTION (pfile, digraphs))
{
if (b == CPP_GREATER)
{*digraph = 1; return CPP_CLOSE_BRACE;} /* %> digraph */
if (b == CPP_COLON)
{*digraph = 1; return CPP_HASH;} /* %: digraph */
}
break;
case CPP_DEREF:
if (b == CPP_MULT && cxx) return CPP_DEREF_STAR;
break;
case CPP_DOT:
if (b == CPP_MULT && cxx) return CPP_DOT_STAR;
if (b == CPP_NUMBER) return CPP_NUMBER;
break;
case CPP_HASH:
if (b == a && (token1->flags & DIGRAPH) == (token2->flags & DIGRAPH))
/* %:%: digraph */
{*digraph = (token1->flags & DIGRAPH); return CPP_PASTE;}
break;
case CPP_NAME:
if (b == CPP_NAME) return CPP_NAME;
if (b == CPP_NUMBER
&& name_p (pfile, &token2->val.str)) return CPP_NAME;
if (b == CPP_CHAR
&& token1->val.node == pfile->spec_nodes.n_L) return CPP_WCHAR;
if (b == CPP_STRING
&& token1->val.node == pfile->spec_nodes.n_L) return CPP_WSTRING;
break;
case CPP_NUMBER:
if (b == CPP_NUMBER) return CPP_NUMBER;
if (b == CPP_NAME) return CPP_NUMBER;
if (b == CPP_DOT) return CPP_NUMBER;
/* Numbers cannot have length zero, so this is safe. */
if ((b == CPP_PLUS || b == CPP_MINUS)
&& VALID_SIGN ('+', token1->val.str.text[token1->val.str.len - 1]))
return CPP_NUMBER;
break;
case CPP_OTHER:
if (CPP_OPTION (pfile, objc) && token1->val.c == '@')
{
if (b == CPP_NAME) return CPP_NAME;
if (b == CPP_STRING) return CPP_OSTRING;
}
default:
break;
}
return CPP_EOF;
}
/* Returns nonzero if a space should be inserted to avoid an
accidental token paste for output. For simplicity, it is
conservative, and occasionally advises a space where one is not
needed, e.g. "." and ".2". */
int
cpp_avoid_paste (pfile, token1, token2)
cpp_reader *pfile;
const cpp_token *token1, *token2;
{
enum cpp_ttype a = token1->type, b = token2->type;
cppchar_t c;
if (token1->flags & NAMED_OP)
a = CPP_NAME;
if (token2->flags & NAMED_OP)
b = CPP_NAME;
c = EOF;
if (token2->flags & DIGRAPH)
c = digraph_spellings[b - CPP_FIRST_DIGRAPH][0];
else if (token_spellings[b].category == SPELL_OPERATOR)
c = token_spellings[b].name[0];
/* Quickly get everything that can paste with an '='. */
if (a <= CPP_LAST_EQ && c == '=')
return 1;
switch (a)
{
case CPP_GREATER: return c == '>' || c == '?';
case CPP_LESS: return c == '<' || c == '?' || c == '%' || c == ':';
case CPP_PLUS: return c == '+';
case CPP_MINUS: return c == '-' || c == '>';
case CPP_DIV: return c == '/' || c == '*'; /* Comments. */
case CPP_MOD: return c == ':' || c == '>';
case CPP_AND: return c == '&';
case CPP_OR: return c == '|';
case CPP_COLON: return c == ':' || c == '>';
case CPP_DEREF: return c == '*';
case CPP_DOT: return c == '.' || c == '%';
case CPP_HASH: return c == '#' || c == '%'; /* Digraph form. */
case CPP_NAME: return ((b == CPP_NUMBER
&& name_p (pfile, &token2->val.str))
|| b == CPP_NAME
|| b == CPP_CHAR || b == CPP_STRING); /* L */
case CPP_NUMBER: return (b == CPP_NUMBER || b == CPP_NAME
|| c == '.' || c == '+' || c == '-');
case CPP_OTHER: return (CPP_OPTION (pfile, objc)
&& token1->val.c == '@'
&& (b == CPP_NAME || b == CPP_STRING));
default: break;
}
return 0;
}
/* Output all the remaining tokens on the current line, and a newline
character, to FP. Leading whitespace is removed. */
void
cpp_output_line (pfile, fp)
cpp_reader *pfile;
FILE *fp;
{
cpp_token token;
cpp_get_token (pfile, &token);
token.flags &= ~PREV_WHITE;
while (token.type != CPP_EOF)
{
cpp_output_token (&token, fp);
cpp_get_token (pfile, &token);
}
putc ('\n', fp);
}
/* Memory pools. */
struct dummy
{
char c;
union
{
double d;
int *p;
} u;
};
#define DEFAULT_ALIGNMENT (offsetof (struct dummy, u))
static int
chunk_suitable (pool, chunk, size)
cpp_pool *pool;
cpp_chunk *chunk;
unsigned int size;
{
/* Being at least twice SIZE means we can use memcpy in
_cpp_next_chunk rather than memmove. Besides, it's a good idea
anyway. */
return (chunk && pool->locked != chunk
&& (unsigned int) (chunk->limit - chunk->base) >= size * 2);
}
/* Returns the end of the new pool. PTR points to a char in the old
pool, and is updated to point to the same char in the new pool. */
unsigned char *
_cpp_next_chunk (pool, len, ptr)
cpp_pool *pool;
unsigned int len;
unsigned char **ptr;
{
cpp_chunk *chunk = pool->cur->next;
/* LEN is the minimum size we want in the new pool. */
len += POOL_ROOM (pool);
if (! chunk_suitable (pool, chunk, len))
{
chunk = new_chunk (POOL_SIZE (pool) * 2 + len);
chunk->next = pool->cur->next;
pool->cur->next = chunk;
}
/* Update the pointer before changing chunk's front. */
if (ptr)
*ptr += chunk->base - POOL_FRONT (pool);
memcpy (chunk->base, POOL_FRONT (pool), POOL_ROOM (pool));
chunk->front = chunk->base;
pool->cur = chunk;
return POOL_LIMIT (pool);
}
static cpp_chunk *
new_chunk (size)
unsigned int size;
{
unsigned char *base;
cpp_chunk *result;
size = ALIGN (size, DEFAULT_ALIGNMENT);
base = (unsigned char *) xmalloc (size + sizeof (cpp_chunk));
/* Put the chunk descriptor at the end. Then chunk overruns will
cause obvious chaos. */
result = (cpp_chunk *) (base + size);
result->base = base;
result->front = base;
result->limit = base + size;
result->next = 0;
return result;
}
void
_cpp_init_pool (pool, size, align, temp)
cpp_pool *pool;
unsigned int size, align, temp;
{
if (align == 0)
align = DEFAULT_ALIGNMENT;
if (align & (align - 1))
abort ();
pool->align = align;
pool->cur = new_chunk (size);
pool->locked = 0;
pool->locks = 0;
if (temp)
pool->cur->next = pool->cur;
}
void
_cpp_lock_pool (pool)
cpp_pool *pool;
{
if (pool->locks++ == 0)
pool->locked = pool->cur;
}
void
_cpp_unlock_pool (pool)
cpp_pool *pool;
{
if (--pool->locks == 0)
pool->locked = 0;
}
void
_cpp_free_pool (pool)
cpp_pool *pool;
{
cpp_chunk *chunk = pool->cur, *next;
do
{
next = chunk->next;
free (chunk->base);
chunk = next;
}
while (chunk && chunk != pool->cur);
}
/* Reserve LEN bytes from a memory pool. */
unsigned char *
_cpp_pool_reserve (pool, len)
cpp_pool *pool;
unsigned int len;
{
len = ALIGN (len, pool->align);
if (len > (unsigned int) POOL_ROOM (pool))
_cpp_next_chunk (pool, len, 0);
return POOL_FRONT (pool);
}
/* Allocate LEN bytes from a memory pool. */
unsigned char *
_cpp_pool_alloc (pool, len)
cpp_pool *pool;
unsigned int len;
{
unsigned char *result = _cpp_pool_reserve (pool, len);
POOL_COMMIT (pool, len);
return result;
}
|