blob: 527368b8607a46bd0bc2ae38d8fa5634e0e5f595 [file] [log] [blame]
Zack Weinberg45b966d2000-03-13 22:01:08 +00001/* CPP Library - lexical analysis.
Joseph Myersa48e3dd2011-08-18 16:13:49 +01002 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010,
3 2011 Free Software Foundation, Inc.
Zack Weinberg45b966d2000-03-13 22:01:08 +00004 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7 Broken out to separate file, Zack Weinberg, Mar 2000
8
9This program is free software; you can redistribute it and/or modify it
10under the terms of the GNU General Public License as published by the
Jakub Jelinek748086b2009-04-09 17:00:19 +020011Free Software Foundation; either version 3, or (at your option) any
Zack Weinberg45b966d2000-03-13 22:01:08 +000012later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
Jakub Jelinek748086b2009-04-09 17:00:19 +020020along with this program; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
Zack Weinberg45b966d2000-03-13 22:01:08 +000022
23#include "config.h"
24#include "system.h"
Zack Weinberg45b966d2000-03-13 22:01:08 +000025#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000026#include "internal.h"
Zack Weinberg45b966d2000-03-13 22:01:08 +000027
Neil Booth93c803682000-10-28 17:59:06 +000028enum spell_type
Zack Weinbergf9a0e962000-07-13 02:32:41 +000029{
Neil Booth93c803682000-10-28 17:59:06 +000030 SPELL_OPERATOR = 0,
Neil Booth93c803682000-10-28 17:59:06 +000031 SPELL_IDENT,
Neil Booth6338b352003-04-23 22:44:06 +000032 SPELL_LITERAL,
Neil Booth93c803682000-10-28 17:59:06 +000033 SPELL_NONE
Zack Weinbergf9a0e962000-07-13 02:32:41 +000034};
35
Neil Booth93c803682000-10-28 17:59:06 +000036struct token_spelling
Zack Weinbergf9a0e962000-07-13 02:32:41 +000037{
Neil Booth93c803682000-10-28 17:59:06 +000038 enum spell_type category;
39 const unsigned char *name;
Zack Weinbergf9a0e962000-07-13 02:32:41 +000040};
41
Zack Weinberg8206c792001-10-11 21:21:57 +000042static const unsigned char *const digraph_spellings[] =
Kris Van Heesb6baa672008-04-18 13:58:08 +000043{ UC"%:", UC"%:%:", UC"<:", UC":>", UC"<%", UC"%>" };
Neil Booth93c803682000-10-28 17:59:06 +000044
Kris Van Heesb6baa672008-04-18 13:58:08 +000045#define OP(e, s) { SPELL_OPERATOR, UC s },
46#define TK(e, s) { SPELL_ ## s, UC #e },
Zack Weinberg8206c792001-10-11 21:21:57 +000047static const struct token_spelling token_spellings[N_TTYPES] = { TTYPE_TABLE };
Neil Booth93c803682000-10-28 17:59:06 +000048#undef OP
49#undef TK
50
51#define TOKEN_SPELL(token) (token_spellings[(token)->type].category)
52#define TOKEN_NAME(token) (token_spellings[(token)->type].name)
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +000053
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000054static void add_line_note (cpp_buffer *, const uchar *, unsigned int);
55static int skip_line_comment (cpp_reader *);
56static void skip_whitespace (cpp_reader *, cppchar_t);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000057static void lex_string (cpp_reader *, cpp_token *, const uchar *);
58static void save_comment (cpp_reader *, cpp_token *, const uchar *, cppchar_t);
Matthew Gingell631d0d32008-10-05 12:35:36 +000059static void store_comment (cpp_reader *, cpp_token *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000060static void create_literal (cpp_reader *, cpp_token *, const uchar *,
61 unsigned int, enum cpp_ttype);
62static bool warn_in_comment (cpp_reader *, _cpp_line_note *);
63static int name_p (cpp_reader *, const cpp_string *);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000064static tokenrun *next_tokenrun (tokenrun *);
Neil Booth0d9f2342000-09-18 18:43:05 +000065
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000066static _cpp_buff *new_buff (size_t);
Zack Weinberg15dad1d2000-05-18 15:55:46 +000067
Neil Booth9d10c9a2003-03-06 23:12:30 +000068
Zack Weinberg6d2c2042000-04-30 17:30:25 +000069/* Utility routine:
Zack Weinberg6d2c2042000-04-30 17:30:25 +000070
Zack Weinbergbfb9dc72000-07-08 19:00:39 +000071 Compares, the token TOKEN to the NUL-terminated string STRING.
72 TOKEN must be a CPP_NAME. Returns 1 for equal, 0 for unequal. */
Zack Weinberg6d2c2042000-04-30 17:30:25 +000073int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000074cpp_ideq (const cpp_token *token, const char *string)
Zack Weinberg6d2c2042000-04-30 17:30:25 +000075{
Zack Weinbergbfb9dc72000-07-08 19:00:39 +000076 if (token->type != CPP_NAME)
Zack Weinberg6d2c2042000-04-30 17:30:25 +000077 return 0;
Zack Weinbergbfb9dc72000-07-08 19:00:39 +000078
Joseph Myers9a0c6182009-05-10 15:27:32 +010079 return !ustrcmp (NODE_NAME (token->val.node.node), (const uchar *) string);
Zack Weinberg6d2c2042000-04-30 17:30:25 +000080}
81
Neil Booth26aea072003-04-19 00:22:51 +000082/* Record a note TYPE at byte POS into the current cleaned logical
83 line. */
Neil Booth87062812001-10-20 09:00:53 +000084static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000085add_line_note (cpp_buffer *buffer, const uchar *pos, unsigned int type)
Neil Booth0d9f2342000-09-18 18:43:05 +000086{
Neil Booth26aea072003-04-19 00:22:51 +000087 if (buffer->notes_used == buffer->notes_cap)
Zack Weinbergc5a04732000-04-25 19:32:36 +000088 {
Neil Booth26aea072003-04-19 00:22:51 +000089 buffer->notes_cap = buffer->notes_cap * 2 + 200;
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000090 buffer->notes = XRESIZEVEC (_cpp_line_note, buffer->notes,
91 buffer->notes_cap);
Zack Weinbergc5a04732000-04-25 19:32:36 +000092 }
Neil Booth0d9f2342000-09-18 18:43:05 +000093
Neil Booth26aea072003-04-19 00:22:51 +000094 buffer->notes[buffer->notes_used].pos = pos;
95 buffer->notes[buffer->notes_used].type = type;
96 buffer->notes_used++;
Zack Weinbergc5a04732000-04-25 19:32:36 +000097}
98
Richard Henderson246a2fc2010-08-21 12:05:40 -070099
100/* Fast path to find line special characters using optimized character
101 scanning algorithms. Anything complicated falls back to the slow
102 path below. Since this loop is very hot it's worth doing these kinds
103 of optimizations.
104
105 One of the paths through the ifdefs should provide
106
107 const uchar *search_line_fast (const uchar *s, const uchar *end);
108
109 Between S and END, search for \n, \r, \\, ?. Return a pointer to
110 the found character.
111
112 Note that the last character of the buffer is *always* a newline,
113 as forced by _cpp_convert_input. This fact can be used to avoid
114 explicitly looking for the end of the buffer. */
115
116/* Configure gives us an ifdef test. */
117#ifndef WORDS_BIGENDIAN
118#define WORDS_BIGENDIAN 0
119#endif
120
121/* We'd like the largest integer that fits into a register. There's nothing
122 in <stdint.h> that gives us that. For most hosts this is unsigned long,
123 but MS decided on an LLP64 model. Thankfully when building with GCC we
124 can get the "real" word size. */
125#ifdef __GNUC__
126typedef unsigned int word_type __attribute__((__mode__(__word__)));
127#else
128typedef unsigned long word_type;
129#endif
130
131/* The code below is only expecting sizes 4 or 8.
132 Die at compile-time if this expectation is violated. */
133typedef char check_word_type_size
134 [(sizeof(word_type) == 8 || sizeof(word_type) == 4) * 2 - 1];
135
136/* Return X with the first N bytes forced to values that won't match one
137 of the interesting characters. Note that NUL is not interesting. */
138
139static inline word_type
140acc_char_mask_misalign (word_type val, unsigned int n)
141{
142 word_type mask = -1;
143 if (WORDS_BIGENDIAN)
144 mask >>= n * 8;
145 else
146 mask <<= n * 8;
147 return val & mask;
148}
149
150/* Return X replicated to all byte positions within WORD_TYPE. */
151
152static inline word_type
153acc_char_replicate (uchar x)
154{
155 word_type ret;
156
157 ret = (x << 24) | (x << 16) | (x << 8) | x;
158 if (sizeof(word_type) == 8)
159 ret = (ret << 16 << 16) | ret;
160 return ret;
161}
162
163/* Return non-zero if some byte of VAL is (probably) C. */
164
165static inline word_type
166acc_char_cmp (word_type val, word_type c)
167{
168#if defined(__GNUC__) && defined(__alpha__)
169 /* We can get exact results using a compare-bytes instruction.
170 Get (val == c) via (0 >= (val ^ c)). */
171 return __builtin_alpha_cmpbge (0, val ^ c);
172#else
173 word_type magic = 0x7efefefeU;
174 if (sizeof(word_type) == 8)
175 magic = (magic << 16 << 16) | 0xfefefefeU;
176 magic |= 1;
177
178 val ^= c;
179 return ((val + magic) ^ ~val) & ~magic;
180#endif
181}
182
183/* Given the result of acc_char_cmp is non-zero, return the index of
184 the found character. If this was a false positive, return -1. */
185
186static inline int
187acc_char_index (word_type cmp ATTRIBUTE_UNUSED,
188 word_type val ATTRIBUTE_UNUSED)
189{
190#if defined(__GNUC__) && defined(__alpha__) && !WORDS_BIGENDIAN
191 /* The cmpbge instruction sets *bits* of the result corresponding to
192 matches in the bytes with no false positives. */
193 return __builtin_ctzl (cmp);
194#else
195 unsigned int i;
196
197 /* ??? It would be nice to force unrolling here,
198 and have all of these constants folded. */
199 for (i = 0; i < sizeof(word_type); ++i)
200 {
201 uchar c;
202 if (WORDS_BIGENDIAN)
203 c = (val >> (sizeof(word_type) - i - 1) * 8) & 0xff;
204 else
205 c = (val >> i * 8) & 0xff;
206
207 if (c == '\n' || c == '\r' || c == '\\' || c == '?')
208 return i;
209 }
210
211 return -1;
212#endif
213}
214
215/* A version of the fast scanner using bit fiddling techniques.
216
217 For 32-bit words, one would normally perform 16 comparisons and
218 16 branches. With this algorithm one performs 24 arithmetic
219 operations and one branch. Whether this is faster with a 32-bit
220 word size is going to be somewhat system dependent.
221
222 For 64-bit words, we eliminate twice the number of comparisons
223 and branches without increasing the number of arithmetic operations.
224 It's almost certainly going to be a win with 64-bit word size. */
225
226static const uchar * search_line_acc_char (const uchar *, const uchar *)
227 ATTRIBUTE_UNUSED;
228
229static const uchar *
230search_line_acc_char (const uchar *s, const uchar *end ATTRIBUTE_UNUSED)
231{
232 const word_type repl_nl = acc_char_replicate ('\n');
233 const word_type repl_cr = acc_char_replicate ('\r');
234 const word_type repl_bs = acc_char_replicate ('\\');
235 const word_type repl_qm = acc_char_replicate ('?');
236
237 unsigned int misalign;
238 const word_type *p;
239 word_type val, t;
240
241 /* Align the buffer. Mask out any bytes from before the beginning. */
242 p = (word_type *)((uintptr_t)s & -sizeof(word_type));
243 val = *p;
244 misalign = (uintptr_t)s & (sizeof(word_type) - 1);
245 if (misalign)
246 val = acc_char_mask_misalign (val, misalign);
247
248 /* Main loop. */
249 while (1)
250 {
251 t = acc_char_cmp (val, repl_nl);
252 t |= acc_char_cmp (val, repl_cr);
253 t |= acc_char_cmp (val, repl_bs);
254 t |= acc_char_cmp (val, repl_qm);
255
256 if (__builtin_expect (t != 0, 0))
257 {
258 int i = acc_char_index (t, val);
259 if (i >= 0)
260 return (const uchar *)p + i;
261 }
262
263 val = *++p;
264 }
265}
266
Rainer Orth789d73c2010-08-24 17:23:35 +0000267/* Disable on Solaris 2/x86 until the following problems can be properly
268 autoconfed:
269
270 The Solaris 8 assembler cannot assemble SSE2/SSE4.2 insns.
271 The Solaris 9 assembler cannot assemble SSE4.2 insns.
272 Before Solaris 9 Update 6, SSE insns cannot be executed.
273 The Solaris 10+ assembler tags objects with the instruction set
274 extensions used, so SSE4.2 executables cannot run on machines that
275 don't support that extension. */
276
277#if (GCC_VERSION >= 4005) && (defined(__i386__) || defined(__x86_64__)) && !(defined(__sun__) && defined(__svr4__))
Richard Henderson246a2fc2010-08-21 12:05:40 -0700278
279/* Replicated character data to be shared between implementations.
280 Recall that outside of a context with vector support we can't
281 define compatible vector types, therefore these are all defined
282 in terms of raw characters. */
283static const char repl_chars[4][16] __attribute__((aligned(16))) = {
284 { '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n',
285 '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n' },
286 { '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r',
287 '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r' },
288 { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\',
289 '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' },
290 { '?', '?', '?', '?', '?', '?', '?', '?',
291 '?', '?', '?', '?', '?', '?', '?', '?' },
292};
293
294/* A version of the fast scanner using MMX vectorized byte compare insns.
295
296 This uses the PMOVMSKB instruction which was introduced with "MMX2",
Uros Bizjakef230b32011-05-22 20:53:32 +0200297 which was packaged into SSE1; it is also present in the AMD MMX
Richard Henderson246a2fc2010-08-21 12:05:40 -0700298 extension. Mark the function as using "sse" so that we emit a real
299 "emms" instruction, rather than the 3dNOW "femms" instruction. */
300
301static const uchar *
302#ifndef __SSE__
303__attribute__((__target__("sse")))
304#endif
305search_line_mmx (const uchar *s, const uchar *end ATTRIBUTE_UNUSED)
306{
307 typedef char v8qi __attribute__ ((__vector_size__ (8)));
308 typedef int __m64 __attribute__ ((__vector_size__ (8), __may_alias__));
309
310 const v8qi repl_nl = *(const v8qi *)repl_chars[0];
311 const v8qi repl_cr = *(const v8qi *)repl_chars[1];
312 const v8qi repl_bs = *(const v8qi *)repl_chars[2];
313 const v8qi repl_qm = *(const v8qi *)repl_chars[3];
314
315 unsigned int misalign, found, mask;
316 const v8qi *p;
317 v8qi data, t, c;
318
319 /* Align the source pointer. While MMX doesn't generate unaligned data
320 faults, this allows us to safely scan to the end of the buffer without
321 reading beyond the end of the last page. */
322 misalign = (uintptr_t)s & 7;
323 p = (const v8qi *)((uintptr_t)s & -8);
324 data = *p;
325
326 /* Create a mask for the bytes that are valid within the first
327 16-byte block. The Idea here is that the AND with the mask
328 within the loop is "free", since we need some AND or TEST
329 insn in order to set the flags for the branch anyway. */
330 mask = -1u << misalign;
331
332 /* Main loop processing 8 bytes at a time. */
333 goto start;
334 do
335 {
336 data = *++p;
337 mask = -1;
338
339 start:
340 t = __builtin_ia32_pcmpeqb(data, repl_nl);
341 c = __builtin_ia32_pcmpeqb(data, repl_cr);
342 t = (v8qi) __builtin_ia32_por ((__m64)t, (__m64)c);
343 c = __builtin_ia32_pcmpeqb(data, repl_bs);
344 t = (v8qi) __builtin_ia32_por ((__m64)t, (__m64)c);
345 c = __builtin_ia32_pcmpeqb(data, repl_qm);
346 t = (v8qi) __builtin_ia32_por ((__m64)t, (__m64)c);
347 found = __builtin_ia32_pmovmskb (t);
348 found &= mask;
349 }
350 while (!found);
351
352 __builtin_ia32_emms ();
353
354 /* FOUND contains 1 in bits for which we matched a relevant
355 character. Conversion to the byte index is trivial. */
356 found = __builtin_ctz(found);
357 return (const uchar *)p + found;
358}
359
360/* A version of the fast scanner using SSE2 vectorized byte compare insns. */
361
362static const uchar *
363#ifndef __SSE2__
364__attribute__((__target__("sse2")))
365#endif
366search_line_sse2 (const uchar *s, const uchar *end ATTRIBUTE_UNUSED)
367{
368 typedef char v16qi __attribute__ ((__vector_size__ (16)));
369
370 const v16qi repl_nl = *(const v16qi *)repl_chars[0];
371 const v16qi repl_cr = *(const v16qi *)repl_chars[1];
372 const v16qi repl_bs = *(const v16qi *)repl_chars[2];
373 const v16qi repl_qm = *(const v16qi *)repl_chars[3];
374
375 unsigned int misalign, found, mask;
376 const v16qi *p;
377 v16qi data, t;
378
379 /* Align the source pointer. */
380 misalign = (uintptr_t)s & 15;
381 p = (const v16qi *)((uintptr_t)s & -16);
382 data = *p;
383
384 /* Create a mask for the bytes that are valid within the first
385 16-byte block. The Idea here is that the AND with the mask
386 within the loop is "free", since we need some AND or TEST
387 insn in order to set the flags for the branch anyway. */
388 mask = -1u << misalign;
389
390 /* Main loop processing 16 bytes at a time. */
391 goto start;
392 do
393 {
394 data = *++p;
395 mask = -1;
396
397 start:
398 t = __builtin_ia32_pcmpeqb128(data, repl_nl);
399 t |= __builtin_ia32_pcmpeqb128(data, repl_cr);
400 t |= __builtin_ia32_pcmpeqb128(data, repl_bs);
401 t |= __builtin_ia32_pcmpeqb128(data, repl_qm);
402 found = __builtin_ia32_pmovmskb128 (t);
403 found &= mask;
404 }
405 while (!found);
406
407 /* FOUND contains 1 in bits for which we matched a relevant
408 character. Conversion to the byte index is trivial. */
409 found = __builtin_ctz(found);
410 return (const uchar *)p + found;
411}
412
Richard Henderson6f173e52010-08-24 14:08:05 -0700413#ifdef HAVE_SSE4
Richard Henderson246a2fc2010-08-21 12:05:40 -0700414/* A version of the fast scanner using SSE 4.2 vectorized string insns. */
415
416static const uchar *
417#ifndef __SSE4_2__
418__attribute__((__target__("sse4.2")))
419#endif
420search_line_sse42 (const uchar *s, const uchar *end)
421{
422 typedef char v16qi __attribute__ ((__vector_size__ (16)));
423 static const v16qi search = { '\n', '\r', '?', '\\' };
424
425 uintptr_t si = (uintptr_t)s;
426 uintptr_t index;
427
428 /* Check for unaligned input. */
429 if (si & 15)
430 {
431 if (__builtin_expect (end - s < 16, 0)
432 && __builtin_expect ((si & 0xfff) > 0xff0, 0))
433 {
434 /* There are less than 16 bytes left in the buffer, and less
435 than 16 bytes left on the page. Reading 16 bytes at this
436 point might generate a spurious page fault. Defer to the
437 SSE2 implementation, which already handles alignment. */
438 return search_line_sse2 (s, end);
439 }
440
441 /* ??? The builtin doesn't understand that the PCMPESTRI read from
442 memory need not be aligned. */
443 __asm ("%vpcmpestri $0, (%1), %2"
444 : "=c"(index) : "r"(s), "x"(search), "a"(4), "d"(16));
445 if (__builtin_expect (index < 16, 0))
446 goto found;
447
448 /* Advance the pointer to an aligned address. We will re-scan a
449 few bytes, but we no longer need care for reading past the
450 end of a page, since we're guaranteed a match. */
451 s = (const uchar *)((si + 16) & -16);
452 }
453
454 /* Main loop, processing 16 bytes at a time. By doing the whole loop
455 in inline assembly, we can make proper use of the flags set. */
456 __asm ( "sub $16, %1\n"
457 " .balign 16\n"
458 "0: add $16, %1\n"
459 " %vpcmpestri $0, (%1), %2\n"
460 " jnc 0b"
461 : "=&c"(index), "+r"(s)
462 : "x"(search), "a"(4), "d"(16));
463
464 found:
465 return s + index;
466}
467
Richard Henderson6f173e52010-08-24 14:08:05 -0700468#else
469/* Work around out-dated assemblers without sse4 support. */
470#define search_line_sse42 search_line_sse2
471#endif
472
Richard Henderson246a2fc2010-08-21 12:05:40 -0700473/* Check the CPU capabilities. */
474
475#include "../gcc/config/i386/cpuid.h"
476
477typedef const uchar * (*search_line_fast_type) (const uchar *, const uchar *);
478static search_line_fast_type search_line_fast;
479
480static void __attribute__((constructor))
481init_vectorized_lexer (void)
482{
483 unsigned dummy, ecx = 0, edx = 0;
484 search_line_fast_type impl = search_line_acc_char;
485 int minimum = 0;
486
487#if defined(__SSE4_2__)
488 minimum = 3;
489#elif defined(__SSE2__)
490 minimum = 2;
Uros Bizjakef230b32011-05-22 20:53:32 +0200491#elif defined(__SSE__)
Richard Henderson246a2fc2010-08-21 12:05:40 -0700492 minimum = 1;
493#endif
494
495 if (minimum == 3)
496 impl = search_line_sse42;
497 else if (__get_cpuid (1, &dummy, &dummy, &ecx, &edx) || minimum == 2)
498 {
499 if (minimum == 3 || (ecx & bit_SSE4_2))
500 impl = search_line_sse42;
501 else if (minimum == 2 || (edx & bit_SSE2))
502 impl = search_line_sse2;
503 else if (minimum == 1 || (edx & bit_SSE))
504 impl = search_line_mmx;
505 }
506 else if (__get_cpuid (0x80000001, &dummy, &dummy, &dummy, &edx))
507 {
Uros Bizjak5e70c0b2011-05-22 21:04:54 +0200508 if (minimum == 1
509 || (edx & (bit_MMXEXT | bit_CMOV)) == (bit_MMXEXT | bit_CMOV))
Richard Henderson246a2fc2010-08-21 12:05:40 -0700510 impl = search_line_mmx;
511 }
512
513 search_line_fast = impl;
514}
515
Richard Henderson01956312011-03-18 13:20:35 -0700516#elif (GCC_VERSION >= 4005) && defined(__ALTIVEC__)
Richard Henderson246a2fc2010-08-21 12:05:40 -0700517
518/* A vection of the fast scanner using AltiVec vectorized byte compares. */
519/* ??? Unfortunately, attribute(target("altivec")) is not yet supported,
520 so we can't compile this function without -maltivec on the command line
521 (or implied by some other switch). */
522
523static const uchar *
524search_line_fast (const uchar *s, const uchar *end ATTRIBUTE_UNUSED)
525{
526 typedef __attribute__((altivec(vector))) unsigned char vc;
527
528 const vc repl_nl = {
529 '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n',
530 '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'
531 };
532 const vc repl_cr = {
533 '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r',
534 '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r'
535 };
536 const vc repl_bs = {
537 '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\',
538 '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\'
539 };
540 const vc repl_qm = {
541 '?', '?', '?', '?', '?', '?', '?', '?',
542 '?', '?', '?', '?', '?', '?', '?', '?',
543 };
544 const vc ones = {
545 -1, -1, -1, -1, -1, -1, -1, -1,
546 -1, -1, -1, -1, -1, -1, -1, -1,
547 };
548 const vc zero = { 0 };
549
550 vc data, mask, t;
551
552 /* Altivec loads automatically mask addresses with -16. This lets us
553 issue the first load as early as possible. */
554 data = __builtin_vec_ld(0, (const vc *)s);
555
556 /* Discard bytes before the beginning of the buffer. Do this by
557 beginning with all ones and shifting in zeros according to the
558 mis-alignment. The LVSR instruction pulls the exact shift we
559 want from the address. */
560 mask = __builtin_vec_lvsr(0, s);
561 mask = __builtin_vec_perm(zero, ones, mask);
562 data &= mask;
563
564 /* While altivec loads mask addresses, we still need to align S so
565 that the offset we compute at the end is correct. */
566 s = (const uchar *)((uintptr_t)s & -16);
567
568 /* Main loop processing 16 bytes at a time. */
569 goto start;
570 do
571 {
572 vc m_nl, m_cr, m_bs, m_qm;
573
574 s += 16;
575 data = __builtin_vec_ld(0, (const vc *)s);
576
577 start:
578 m_nl = (vc) __builtin_vec_cmpeq(data, repl_nl);
579 m_cr = (vc) __builtin_vec_cmpeq(data, repl_cr);
580 m_bs = (vc) __builtin_vec_cmpeq(data, repl_bs);
581 m_qm = (vc) __builtin_vec_cmpeq(data, repl_qm);
582 t = (m_nl | m_cr) | (m_bs | m_qm);
583
584 /* T now contains 0xff in bytes for which we matched one of the relevant
585 characters. We want to exit the loop if any byte in T is non-zero.
586 Below is the expansion of vec_any_ne(t, zero). */
587 }
588 while (!__builtin_vec_vcmpeq_p(/*__CR6_LT_REV*/3, t, zero));
589
590 {
591#define N (sizeof(vc) / sizeof(long))
592
593 typedef char check_count[(N == 2 || N == 4) * 2 - 1];
594 union {
595 vc v;
596 unsigned long l[N];
597 } u;
598 unsigned long l, i = 0;
599
600 u.v = t;
601
602 /* Find the first word of T that is non-zero. */
603 switch (N)
604 {
605 case 4:
606 l = u.l[i++];
607 if (l != 0)
608 break;
609 s += sizeof(unsigned long);
610 l = u.l[i++];
611 if (l != 0)
612 break;
613 s += sizeof(unsigned long);
614 case 2:
615 l = u.l[i++];
616 if (l != 0)
617 break;
618 s += sizeof(unsigned long);
619 l = u.l[i];
620 }
621
622 /* L now contains 0xff in bytes for which we matched one of the
623 relevant characters. We can find the byte index by finding
624 its bit index and dividing by 8. */
625 l = __builtin_clzl(l) >> 3;
626 return s + l;
627
628#undef N
629 }
630}
631
632#else
633
634/* We only have one accellerated alternative. Use a direct call so that
635 we encourage inlining. */
636
637#define search_line_fast search_line_acc_char
638
639#endif
640
Neil Booth26aea072003-04-19 00:22:51 +0000641/* Returns with a logical line that contains no escaped newlines or
642 trigraphs. This is a time-critical inner loop. */
643void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000644_cpp_clean_line (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000645{
Neil Booth26aea072003-04-19 00:22:51 +0000646 cpp_buffer *buffer;
647 const uchar *s;
648 uchar c, *d, *p;
Neil Booth29401c32001-08-22 20:37:20 +0000649
Neil Booth26aea072003-04-19 00:22:51 +0000650 buffer = pfile->buffer;
651 buffer->cur_note = buffer->notes_used = 0;
652 buffer->cur = buffer->line_base = buffer->next_line;
653 buffer->need_line = false;
Richard Henderson246a2fc2010-08-21 12:05:40 -0700654 s = buffer->next_line;
Neil Booth26aea072003-04-19 00:22:51 +0000655
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000656 if (!buffer->from_stage3)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000657 {
Ian Lance Taylor7af45bd2006-12-29 15:43:55 +0000658 const uchar *pbackslash = NULL;
659
Richard Henderson246a2fc2010-08-21 12:05:40 -0700660 /* Fast path. This is the common case of an un-escaped line with
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000661 no trigraphs. The primary win here is by not writing any
662 data back to memory until we have to. */
Richard Henderson246a2fc2010-08-21 12:05:40 -0700663 while (1)
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000664 {
Richard Henderson246a2fc2010-08-21 12:05:40 -0700665 /* Perform an optimized search for \n, \r, \\, ?. */
666 s = search_line_fast (s, buffer->rlimit);
667
668 c = *s;
669 if (c == '\\')
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000670 {
Richard Henderson246a2fc2010-08-21 12:05:40 -0700671 /* Record the location of the backslash and continue. */
672 pbackslash = s++;
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000673 }
Richard Henderson246a2fc2010-08-21 12:05:40 -0700674 else if (__builtin_expect (c == '?', 0))
675 {
676 if (__builtin_expect (s[1] == '?', false)
Ian Lance Taylor7af45bd2006-12-29 15:43:55 +0000677 && _cpp_trigraph_map[s[2]])
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000678 {
Richard Henderson246a2fc2010-08-21 12:05:40 -0700679 /* Have a trigraph. We may or may not have to convert
680 it. Add a line note regardless, for -Wtrigraphs. */
681 add_line_note (buffer, s, s[2]);
682 if (CPP_OPTION (pfile, trigraphs))
683 {
684 /* We do, and that means we have to switch to the
685 slow path. */
686 d = (uchar *) s;
687 *d = _cpp_trigraph_map[s[2]];
688 s += 2;
689 goto slow_path;
690 }
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000691 }
Richard Henderson246a2fc2010-08-21 12:05:40 -0700692 /* Not a trigraph. Continue on fast-path. */
693 s++;
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000694 }
Richard Henderson246a2fc2010-08-21 12:05:40 -0700695 else
696 break;
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000697 }
698
Richard Henderson246a2fc2010-08-21 12:05:40 -0700699 /* This must be \r or \n. We're either done, or we'll be forced
700 to write back to the buffer and continue on the slow path. */
701 d = (uchar *) s;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000702
Richard Henderson246a2fc2010-08-21 12:05:40 -0700703 if (__builtin_expect (s == buffer->rlimit, false))
704 goto done;
705
706 /* DOS line ending? */
707 if (__builtin_expect (c == '\r', false) && s[1] == '\n')
708 {
709 s++;
710 if (s == buffer->rlimit)
711 goto done;
712 }
713
714 if (__builtin_expect (pbackslash == NULL, true))
715 goto done;
716
717 /* Check for escaped newline. */
718 p = d;
719 while (is_nvspace (p[-1]))
720 p--;
721 if (p - 1 != pbackslash)
722 goto done;
723
724 /* Have an escaped newline; process it and proceed to
725 the slow path. */
726 add_line_note (buffer, p - 1, p != d ? ' ' : '\\');
727 d = p - 2;
728 buffer->next_line = p - 1;
729
730 slow_path:
731 while (1)
Neil Booth0d9f2342000-09-18 18:43:05 +0000732 {
Neil Booth26aea072003-04-19 00:22:51 +0000733 c = *++s;
734 *++d = c;
735
736 if (c == '\n' || c == '\r')
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000737 {
Richard Henderson246a2fc2010-08-21 12:05:40 -0700738 /* Handle DOS line endings. */
Neil Booth26aea072003-04-19 00:22:51 +0000739 if (c == '\r' && s != buffer->rlimit && s[1] == '\n')
740 s++;
741 if (s == buffer->rlimit)
Neil Booth87062812001-10-20 09:00:53 +0000742 break;
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000743
Neil Booth26aea072003-04-19 00:22:51 +0000744 /* Escaped? */
745 p = d;
746 while (p != buffer->next_line && is_nvspace (p[-1]))
747 p--;
748 if (p == buffer->next_line || p[-1] != '\\')
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000749 break;
Neil Booth26aea072003-04-19 00:22:51 +0000750
Neil Booth41c32c92003-04-20 19:02:53 +0000751 add_line_note (buffer, p - 1, p != d ? ' ': '\\');
Neil Booth26aea072003-04-19 00:22:51 +0000752 d = p - 2;
753 buffer->next_line = p - 1;
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000754 }
Neil Booth26aea072003-04-19 00:22:51 +0000755 else if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]])
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000756 {
Neil Booth26aea072003-04-19 00:22:51 +0000757 /* Add a note regardless, for the benefit of -Wtrigraphs. */
Neil Booth41c32c92003-04-20 19:02:53 +0000758 add_line_note (buffer, d, s[2]);
Neil Booth26aea072003-04-19 00:22:51 +0000759 if (CPP_OPTION (pfile, trigraphs))
760 {
761 *d = _cpp_trigraph_map[s[2]];
762 s += 2;
763 }
Neil Bootha5c3ccc2000-10-30 22:29:00 +0000764 }
Neil Booth0d9f2342000-09-18 18:43:05 +0000765 }
Neil Booth26aea072003-04-19 00:22:51 +0000766 }
767 else
768 {
Richard Henderson246a2fc2010-08-21 12:05:40 -0700769 while (*s != '\n' && *s != '\r')
Neil Booth26aea072003-04-19 00:22:51 +0000770 s++;
Neil Booth26aea072003-04-19 00:22:51 +0000771 d = (uchar *) s;
772
773 /* Handle DOS line endings. */
774 if (*s == '\r' && s != buffer->rlimit && s[1] == '\n')
775 s++;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000776 }
Zack Weinbergc5a04732000-04-25 19:32:36 +0000777
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000778 done:
Neil Booth26aea072003-04-19 00:22:51 +0000779 *d = '\n';
Neil Booth41c32c92003-04-20 19:02:53 +0000780 /* A sentinel note that should never be processed. */
781 add_line_note (buffer, d + 1, '\n');
Neil Booth26aea072003-04-19 00:22:51 +0000782 buffer->next_line = s + 1;
783}
784
Neil Bootha8eb6042003-05-04 20:03:55 +0000785/* Return true if the trigraph indicated by NOTE should be warned
786 about in a comment. */
787static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000788warn_in_comment (cpp_reader *pfile, _cpp_line_note *note)
Neil Bootha8eb6042003-05-04 20:03:55 +0000789{
790 const uchar *p;
791
792 /* Within comments we don't warn about trigraphs, unless the
793 trigraph forms an escaped newline, as that may change
Kazu Hirata6356f892003-06-12 19:01:08 +0000794 behavior. */
Neil Bootha8eb6042003-05-04 20:03:55 +0000795 if (note->type != '/')
796 return false;
797
798 /* If -trigraphs, then this was an escaped newline iff the next note
799 is coincident. */
800 if (CPP_OPTION (pfile, trigraphs))
801 return note[1].pos == note->pos;
802
803 /* Otherwise, see if this forms an escaped newline. */
804 p = note->pos + 3;
805 while (is_nvspace (*p))
806 p++;
807
808 /* There might have been escaped newlines between the trigraph and the
809 newline we found. Hence the position test. */
810 return (*p == '\n' && p < note[1].pos);
811}
812
Neil Booth26aea072003-04-19 00:22:51 +0000813/* Process the notes created by add_line_note as far as the current
814 location. */
815void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000816_cpp_process_line_notes (cpp_reader *pfile, int in_comment)
Neil Booth26aea072003-04-19 00:22:51 +0000817{
818 cpp_buffer *buffer = pfile->buffer;
819
820 for (;;)
821 {
822 _cpp_line_note *note = &buffer->notes[buffer->cur_note];
823 unsigned int col;
824
825 if (note->pos > buffer->cur)
826 break;
827
828 buffer->cur_note++;
829 col = CPP_BUF_COLUMN (buffer, note->pos + 1);
830
Neil Booth41c32c92003-04-20 19:02:53 +0000831 if (note->type == '\\' || note->type == ' ')
Neil Booth26aea072003-04-19 00:22:51 +0000832 {
Neil Booth41c32c92003-04-20 19:02:53 +0000833 if (note->type == ' ' && !in_comment)
Per Bothner500bee02004-04-22 19:22:27 -0700834 cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
Neil Booth26aea072003-04-19 00:22:51 +0000835 "backslash and newline separated by space");
Neil Booth41c32c92003-04-20 19:02:53 +0000836
Neil Booth26aea072003-04-19 00:22:51 +0000837 if (buffer->next_line > buffer->rlimit)
838 {
Per Bothner500bee02004-04-22 19:22:27 -0700839 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, col,
Neil Booth26aea072003-04-19 00:22:51 +0000840 "backslash-newline at end of file");
841 /* Prevent "no newline at end of file" warning. */
842 buffer->next_line = buffer->rlimit;
843 }
844
845 buffer->line_base = note->pos;
Per Bothner12f9df42004-02-11 07:29:30 -0800846 CPP_INCREMENT_LINE (pfile, 0);
Neil Booth26aea072003-04-19 00:22:51 +0000847 }
Neil Booth41c32c92003-04-20 19:02:53 +0000848 else if (_cpp_trigraph_map[note->type])
849 {
Neil Bootha8eb6042003-05-04 20:03:55 +0000850 if (CPP_OPTION (pfile, warn_trigraphs)
851 && (!in_comment || warn_in_comment (pfile, note)))
Neil Booth41c32c92003-04-20 19:02:53 +0000852 {
853 if (CPP_OPTION (pfile, trigraphs))
Simon Baldwin87cf0652010-04-07 17:18:10 +0000854 cpp_warning_with_line (pfile, CPP_W_TRIGRAPHS,
855 pfile->line_table->highest_line, col,
856 "trigraph ??%c converted to %c",
857 note->type,
858 (int) _cpp_trigraph_map[note->type]);
Neil Booth41c32c92003-04-20 19:02:53 +0000859 else
Geoffrey Keating905bd7b2003-07-22 02:21:16 +0000860 {
Simon Baldwin87cf0652010-04-07 17:18:10 +0000861 cpp_warning_with_line
862 (pfile, CPP_W_TRIGRAPHS,
863 pfile->line_table->highest_line, col,
Geoffrey Keating905bd7b2003-07-22 02:21:16 +0000864 "trigraph ??%c ignored, use -trigraphs to enable",
865 note->type);
866 }
Neil Booth41c32c92003-04-20 19:02:53 +0000867 }
868 }
Jason Merrill00a81b82010-03-29 16:07:29 -0400869 else if (note->type == 0)
870 /* Already processed in lex_raw_string. */;
Neil Booth41c32c92003-04-20 19:02:53 +0000871 else
872 abort ();
Neil Booth26aea072003-04-19 00:22:51 +0000873 }
Zack Weinbergc5a04732000-04-25 19:32:36 +0000874}
875
Neil Booth0d9f2342000-09-18 18:43:05 +0000876/* Skip a C-style block comment. We find the end of the comment by
877 seeing if an asterisk is before every '/' we encounter. Returns
Neil Booth6f572ac2003-04-19 16:34:33 +0000878 nonzero if comment terminated by EOF, zero otherwise.
879
880 Buffer->cur points to the initial asterisk of the comment. */
Neil Booth26aea072003-04-19 00:22:51 +0000881bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000882_cpp_skip_block_comment (cpp_reader *pfile)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000883{
884 cpp_buffer *buffer = pfile->buffer;
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000885 const uchar *cur = buffer->cur;
886 uchar c;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000887
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000888 cur++;
889 if (*cur == '/')
890 cur++;
Neil Booth26aea072003-04-19 00:22:51 +0000891
892 for (;;)
Neil Booth0d9f2342000-09-18 18:43:05 +0000893 {
Neil Booth0d9f2342000-09-18 18:43:05 +0000894 /* People like decorating comments with '*', so check for '/'
895 instead for efficiency. */
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000896 c = *cur++;
897
Zack Weinbergc5a04732000-04-25 19:32:36 +0000898 if (c == '/')
899 {
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000900 if (cur[-2] == '*')
Neil Booth0d9f2342000-09-18 18:43:05 +0000901 break;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000902
Neil Booth0d9f2342000-09-18 18:43:05 +0000903 /* Warn about potential nested comments, but not if the '/'
Joseph Myersa1f300c2001-11-23 02:05:19 +0000904 comes immediately before the true comment delimiter.
Zack Weinbergc5a04732000-04-25 19:32:36 +0000905 Don't bother to get it right across escaped newlines. */
Neil Booth0d9f2342000-09-18 18:43:05 +0000906 if (CPP_OPTION (pfile, warn_comments)
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000907 && cur[0] == '*' && cur[1] != '/')
908 {
909 buffer->cur = cur;
Simon Baldwin87cf0652010-04-07 17:18:10 +0000910 cpp_warning_with_line (pfile, CPP_W_COMMENTS,
911 pfile->line_table->highest_line,
912 CPP_BUF_COL (buffer),
913 "\"/*\" within comment");
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000914 }
Zack Weinbergc5a04732000-04-25 19:32:36 +0000915 }
Neil Booth26aea072003-04-19 00:22:51 +0000916 else if (c == '\n')
917 {
Per Bothner12f9df42004-02-11 07:29:30 -0800918 unsigned int cols;
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000919 buffer->cur = cur - 1;
Neil Booth26aea072003-04-19 00:22:51 +0000920 _cpp_process_line_notes (pfile, true);
921 if (buffer->next_line >= buffer->rlimit)
922 return true;
923 _cpp_clean_line (pfile);
Per Bothner12f9df42004-02-11 07:29:30 -0800924
925 cols = buffer->next_line - buffer->line_base;
926 CPP_INCREMENT_LINE (pfile, cols);
927
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000928 cur = buffer->cur;
Neil Booth26aea072003-04-19 00:22:51 +0000929 }
Zack Weinbergc5a04732000-04-25 19:32:36 +0000930 }
Zack Weinbergc5a04732000-04-25 19:32:36 +0000931
Zack Weinbergd08dcf82003-10-13 18:53:28 +0000932 buffer->cur = cur;
Neil Bootha8eb6042003-05-04 20:03:55 +0000933 _cpp_process_line_notes (pfile, true);
Neil Booth26aea072003-04-19 00:22:51 +0000934 return false;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000935}
936
Neil Booth480709c2001-10-21 14:04:42 +0000937/* Skip a C++ line comment, leaving buffer->cur pointing to the
Kazu Hiratada7d8302002-09-22 02:03:17 +0000938 terminating newline. Handles escaped newlines. Returns nonzero
Neil Booth480709c2001-10-21 14:04:42 +0000939 if a multiline comment. */
Zack Weinbergc5a04732000-04-25 19:32:36 +0000940static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000941skip_line_comment (cpp_reader *pfile)
Neil Booth0d9f2342000-09-18 18:43:05 +0000942{
Neil Boothcbcff6d2000-09-23 21:41:41 +0000943 cpp_buffer *buffer = pfile->buffer;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000944 source_location orig_line = pfile->line_table->highest_line;
Neil Booth0d9f2342000-09-18 18:43:05 +0000945
Neil Booth26aea072003-04-19 00:22:51 +0000946 while (*buffer->cur != '\n')
947 buffer->cur++;
Neil Booth0d9f2342000-09-18 18:43:05 +0000948
Neil Booth26aea072003-04-19 00:22:51 +0000949 _cpp_process_line_notes (pfile, true);
Per Bothner500bee02004-04-22 19:22:27 -0700950 return orig_line != pfile->line_table->highest_line;
Neil Booth0d9f2342000-09-18 18:43:05 +0000951}
952
Neil Booth26aea072003-04-19 00:22:51 +0000953/* Skips whitespace, saving the next non-whitespace character. */
Neil Booth0d9f2342000-09-18 18:43:05 +0000954static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000955skip_whitespace (cpp_reader *pfile, cppchar_t c)
Zack Weinbergc5a04732000-04-25 19:32:36 +0000956{
957 cpp_buffer *buffer = pfile->buffer;
Neil Boothf7d151f2003-04-19 07:41:15 +0000958 bool saw_NUL = false;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000959
Neil Booth0d9f2342000-09-18 18:43:05 +0000960 do
Zack Weinbergc5a04732000-04-25 19:32:36 +0000961 {
Neil Booth91fcd152000-07-09 09:19:44 +0000962 /* Horizontal space always OK. */
Neil Booth26aea072003-04-19 00:22:51 +0000963 if (c == ' ' || c == '\t')
Neil Booth0d9f2342000-09-18 18:43:05 +0000964 ;
Neil Booth0d9f2342000-09-18 18:43:05 +0000965 /* Just \f \v or \0 left. */
Neil Booth91fcd152000-07-09 09:19:44 +0000966 else if (c == '\0')
Neil Boothf7d151f2003-04-19 07:41:15 +0000967 saw_NUL = true;
Neil Booth93c803682000-10-28 17:59:06 +0000968 else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
Per Bothner500bee02004-04-22 19:22:27 -0700969 cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line,
Neil Boothebef4e82002-04-14 18:42:47 +0000970 CPP_BUF_COL (buffer),
971 "%s in preprocessing directive",
972 c == '\f' ? "form feed" : "vertical tab");
Zack Weinbergc5a04732000-04-25 19:32:36 +0000973
Neil Booth0d9f2342000-09-18 18:43:05 +0000974 c = *buffer->cur++;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000975 }
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000976 /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
Neil Booth0d9f2342000-09-18 18:43:05 +0000977 while (is_nvspace (c));
Zack Weinbergc5a04732000-04-25 19:32:36 +0000978
Neil Boothf7d151f2003-04-19 07:41:15 +0000979 if (saw_NUL)
John David Anglin0527bc42003-11-01 22:56:54 +0000980 cpp_error (pfile, CPP_DL_WARNING, "null character(s) ignored");
Neil Boothf7d151f2003-04-19 07:41:15 +0000981
Neil Booth480709c2001-10-21 14:04:42 +0000982 buffer->cur--;
Zack Weinbergc5a04732000-04-25 19:32:36 +0000983}
984
Neil Booth93c803682000-10-28 17:59:06 +0000985/* See if the characters of a number token are valid in a name (no
986 '.', '+' or '-'). */
987static int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000988name_p (cpp_reader *pfile, const cpp_string *string)
Neil Booth93c803682000-10-28 17:59:06 +0000989{
990 unsigned int i;
991
992 for (i = 0; i < string->len; i++)
993 if (!is_idchar (string->text[i]))
994 return 0;
995
Kazu Hiratadf383482002-05-22 22:02:16 +0000996 return 1;
Neil Booth93c803682000-10-28 17:59:06 +0000997}
998
Geoffrey Keating50668cf2005-03-15 00:36:33 +0000999/* After parsing an identifier or other sequence, produce a warning about
1000 sequences not in NFC/NFKC. */
1001static void
1002warn_about_normalization (cpp_reader *pfile,
1003 const cpp_token *token,
1004 const struct normalize_state *s)
1005{
1006 if (CPP_OPTION (pfile, warn_normalize) < NORMALIZE_STATE_RESULT (s)
1007 && !pfile->state.skipping)
1008 {
1009 /* Make sure that the token is printed using UCNs, even
1010 if we'd otherwise happily print UTF-8. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001011 unsigned char *buf = XNEWVEC (unsigned char, cpp_token_len (token));
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001012 size_t sz;
1013
1014 sz = cpp_spell_token (pfile, token, buf, false) - buf;
1015 if (NORMALIZE_STATE_RESULT (s) == normalized_C)
Simon Baldwin87cf0652010-04-07 17:18:10 +00001016 cpp_warning_with_line (pfile, CPP_W_NORMALIZE, token->src_loc, 0,
1017 "`%.*s' is not in NFKC", (int) sz, buf);
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001018 else
Simon Baldwin87cf0652010-04-07 17:18:10 +00001019 cpp_warning_with_line (pfile, CPP_W_NORMALIZE, token->src_loc, 0,
1020 "`%.*s' is not in NFC", (int) sz, buf);
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001021 }
1022}
1023
Neil Boothbced6ed2003-04-19 11:59:44 +00001024/* Returns TRUE if the sequence starting at buffer->cur is invalid in
Neil Booth1613e522003-04-20 07:29:23 +00001025 an identifier. FIRST is TRUE if this starts an identifier. */
Neil Boothbced6ed2003-04-19 11:59:44 +00001026static bool
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001027forms_identifier_p (cpp_reader *pfile, int first,
1028 struct normalize_state *state)
Neil Boothbced6ed2003-04-19 11:59:44 +00001029{
Neil Booth1613e522003-04-20 07:29:23 +00001030 cpp_buffer *buffer = pfile->buffer;
Neil Boothbced6ed2003-04-19 11:59:44 +00001031
Neil Booth1613e522003-04-20 07:29:23 +00001032 if (*buffer->cur == '$')
Neil Boothbced6ed2003-04-19 11:59:44 +00001033 {
Neil Booth1613e522003-04-20 07:29:23 +00001034 if (!CPP_OPTION (pfile, dollars_in_ident))
1035 return false;
Neil Boothbced6ed2003-04-19 11:59:44 +00001036
Neil Booth1613e522003-04-20 07:29:23 +00001037 buffer->cur++;
Hans-Peter Nilsson78b88112003-06-12 06:09:15 +00001038 if (CPP_OPTION (pfile, warn_dollars) && !pfile->state.skipping)
Neil Booth1613e522003-04-20 07:29:23 +00001039 {
Hans-Peter Nilsson78b88112003-06-12 06:09:15 +00001040 CPP_OPTION (pfile, warn_dollars) = 0;
John David Anglin0527bc42003-11-01 22:56:54 +00001041 cpp_error (pfile, CPP_DL_PEDWARN, "'$' in identifier or number");
Neil Booth1613e522003-04-20 07:29:23 +00001042 }
1043
1044 return true;
1045 }
1046
1047 /* Is this a syntactically valid UCN? */
Joseph Myersaf15a2f2005-09-20 21:31:37 +01001048 if (CPP_OPTION (pfile, extended_identifiers)
Geoffrey Keating6baba9b2005-03-15 09:55:41 +00001049 && *buffer->cur == '\\'
Neil Booth1613e522003-04-20 07:29:23 +00001050 && (buffer->cur[1] == 'u' || buffer->cur[1] == 'U'))
1051 {
1052 buffer->cur += 2;
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001053 if (_cpp_valid_ucn (pfile, &buffer->cur, buffer->rlimit, 1 + !first,
1054 state))
Neil Booth1613e522003-04-20 07:29:23 +00001055 return true;
1056 buffer->cur -= 2;
1057 }
1058
1059 return false;
Neil Boothbced6ed2003-04-19 11:59:44 +00001060}
1061
Kai Tietz17e7cb82009-11-11 18:37:19 +00001062/* Helper function to get the cpp_hashnode of the identifier BASE. */
1063static cpp_hashnode *
1064lex_identifier_intern (cpp_reader *pfile, const uchar *base)
1065{
1066 cpp_hashnode *result;
1067 const uchar *cur;
1068 unsigned int len;
1069 unsigned int hash = HT_HASHSTEP (0, *base);
1070
1071 cur = base + 1;
1072 while (ISIDNUM (*cur))
1073 {
1074 hash = HT_HASHSTEP (hash, *cur);
1075 cur++;
1076 }
1077 len = cur - base;
1078 hash = HT_HASHFINISH (hash, len);
1079 result = CPP_HASHNODE (ht_lookup_with_hash (pfile->hash_table,
1080 base, len, hash, HT_ALLOC));
1081
1082 /* Rarely, identifiers require diagnostics when lexed. */
1083 if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
1084 && !pfile->state.skipping, 0))
1085 {
1086 /* It is allowed to poison the same identifier twice. */
1087 if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
1088 cpp_error (pfile, CPP_DL_ERROR, "attempt to use poisoned \"%s\"",
1089 NODE_NAME (result));
1090
1091 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
1092 replacement list of a variadic macro. */
1093 if (result == pfile->spec_nodes.n__VA_ARGS__
1094 && !pfile->state.va_args_ok)
1095 cpp_error (pfile, CPP_DL_PEDWARN,
1096 "__VA_ARGS__ can only appear in the expansion"
1097 " of a C99 variadic macro");
1098
1099 /* For -Wc++-compat, warn about use of C++ named operators. */
1100 if (result->flags & NODE_WARN_OPERATOR)
Simon Baldwin87cf0652010-04-07 17:18:10 +00001101 cpp_warning (pfile, CPP_W_CXX_OPERATOR_NAMES,
1102 "identifier \"%s\" is a special operator name in C++",
1103 NODE_NAME (result));
Kai Tietz17e7cb82009-11-11 18:37:19 +00001104 }
1105
1106 return result;
1107}
1108
1109/* Get the cpp_hashnode of an identifier specified by NAME in
1110 the current cpp_reader object. If none is found, NULL is returned. */
1111cpp_hashnode *
1112_cpp_lex_identifier (cpp_reader *pfile, const char *name)
1113{
1114 cpp_hashnode *result;
1115 result = lex_identifier_intern (pfile, (uchar *) name);
1116 return result;
1117}
1118
Neil Boothbced6ed2003-04-19 11:59:44 +00001119/* Lex an identifier starting at BUFFER->CUR - 1. */
Neil Booth0d9f2342000-09-18 18:43:05 +00001120static cpp_hashnode *
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001121lex_identifier (cpp_reader *pfile, const uchar *base, bool starts_ucn,
1122 struct normalize_state *nst)
Zack Weinbergc5a04732000-04-25 19:32:36 +00001123{
Neil Booth93c803682000-10-28 17:59:06 +00001124 cpp_hashnode *result;
Geoffrey Keating47e20492005-03-12 10:44:06 +00001125 const uchar *cur;
Zack Weinbergc6e83802004-06-05 20:58:06 +00001126 unsigned int len;
1127 unsigned int hash = HT_HASHSTEP (0, *base);
Zack Weinbergc5a04732000-04-25 19:32:36 +00001128
Zack Weinbergc6e83802004-06-05 20:58:06 +00001129 cur = pfile->buffer->cur;
Geoffrey Keating47e20492005-03-12 10:44:06 +00001130 if (! starts_ucn)
1131 while (ISIDNUM (*cur))
1132 {
1133 hash = HT_HASHSTEP (hash, *cur);
1134 cur++;
1135 }
1136 pfile->buffer->cur = cur;
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001137 if (starts_ucn || forms_identifier_p (pfile, false, nst))
Neil Booth10cf9bd2002-03-22 07:23:21 +00001138 {
Geoffrey Keating47e20492005-03-12 10:44:06 +00001139 /* Slower version for identifiers containing UCNs (or $). */
1140 do {
1141 while (ISIDNUM (*pfile->buffer->cur))
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001142 {
1143 pfile->buffer->cur++;
1144 NORMALIZE_STATE_UPDATE_IDNUM (nst);
1145 }
1146 } while (forms_identifier_p (pfile, false, nst));
Geoffrey Keating47e20492005-03-12 10:44:06 +00001147 result = _cpp_interpret_identifier (pfile, base,
1148 pfile->buffer->cur - base);
Zack Weinberg2c3fcba2001-09-10 22:34:03 +00001149 }
Geoffrey Keating47e20492005-03-12 10:44:06 +00001150 else
1151 {
1152 len = cur - base;
1153 hash = HT_HASHFINISH (hash, len);
Zack Weinberg2c3fcba2001-09-10 22:34:03 +00001154
Tom Tromey2bf41bf2008-02-20 02:16:43 +00001155 result = CPP_HASHNODE (ht_lookup_with_hash (pfile->hash_table,
1156 base, len, hash, HT_ALLOC));
Geoffrey Keating47e20492005-03-12 10:44:06 +00001157 }
Neil Boothbced6ed2003-04-19 11:59:44 +00001158
1159 /* Rarely, identifiers require diagnostics when lexed. */
Zack Weinberg2c3fcba2001-09-10 22:34:03 +00001160 if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
1161 && !pfile->state.skipping, 0))
1162 {
1163 /* It is allowed to poison the same identifier twice. */
1164 if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
John David Anglin0527bc42003-11-01 22:56:54 +00001165 cpp_error (pfile, CPP_DL_ERROR, "attempt to use poisoned \"%s\"",
Zack Weinberg2c3fcba2001-09-10 22:34:03 +00001166 NODE_NAME (result));
1167
1168 /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
1169 replacement list of a variadic macro. */
1170 if (result == pfile->spec_nodes.n__VA_ARGS__
1171 && !pfile->state.va_args_ok)
John David Anglin0527bc42003-11-01 22:56:54 +00001172 cpp_error (pfile, CPP_DL_PEDWARN,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001173 "__VA_ARGS__ can only appear in the expansion"
1174 " of a C99 variadic macro");
Ian Lance Taylor3d8b2a92009-06-12 19:43:25 +00001175
1176 /* For -Wc++-compat, warn about use of C++ named operators. */
1177 if (result->flags & NODE_WARN_OPERATOR)
Simon Baldwin87cf0652010-04-07 17:18:10 +00001178 cpp_warning (pfile, CPP_W_CXX_OPERATOR_NAMES,
1179 "identifier \"%s\" is a special operator name in C++",
1180 NODE_NAME (result));
Zack Weinberg2c3fcba2001-09-10 22:34:03 +00001181 }
1182
1183 return result;
1184}
1185
Neil Boothbced6ed2003-04-19 11:59:44 +00001186/* Lex a number to NUMBER starting at BUFFER->CUR - 1. */
Zack Weinbergc5a04732000-04-25 19:32:36 +00001187static void
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001188lex_number (cpp_reader *pfile, cpp_string *number,
1189 struct normalize_state *nst)
Zack Weinbergc5a04732000-04-25 19:32:36 +00001190{
Neil Booth562a5c22002-04-21 18:46:42 +00001191 const uchar *cur;
Neil Boothbced6ed2003-04-19 11:59:44 +00001192 const uchar *base;
1193 uchar *dest;
Zack Weinbergc5a04732000-04-25 19:32:36 +00001194
Neil Boothbced6ed2003-04-19 11:59:44 +00001195 base = pfile->buffer->cur - 1;
1196 do
Neil Booth93c803682000-10-28 17:59:06 +00001197 {
Neil Boothbced6ed2003-04-19 11:59:44 +00001198 cur = pfile->buffer->cur;
Neil Booth10cf9bd2002-03-22 07:23:21 +00001199
Neil Boothbced6ed2003-04-19 11:59:44 +00001200 /* N.B. ISIDNUM does not include $. */
1201 while (ISIDNUM (*cur) || *cur == '.' || VALID_SIGN (*cur, cur[-1]))
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001202 {
1203 cur++;
1204 NORMALIZE_STATE_UPDATE_IDNUM (nst);
1205 }
Neil Booth10cf9bd2002-03-22 07:23:21 +00001206
Neil Booth10cf9bd2002-03-22 07:23:21 +00001207 pfile->buffer->cur = cur;
Neil Booth93c803682000-10-28 17:59:06 +00001208 }
Geoffrey Keating50668cf2005-03-15 00:36:33 +00001209 while (forms_identifier_p (pfile, false, nst));
Neil Boothbced6ed2003-04-19 11:59:44 +00001210
1211 number->len = cur - base;
1212 dest = _cpp_unaligned_alloc (pfile, number->len + 1);
1213 memcpy (dest, base, number->len);
1214 dest[number->len] = '\0';
1215 number->text = dest;
Neil Booth0d9f2342000-09-18 18:43:05 +00001216}
1217
Neil Booth6338b352003-04-23 22:44:06 +00001218/* Create a token of type TYPE with a literal spelling. */
Zack Weinbergc5a04732000-04-25 19:32:36 +00001219static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001220create_literal (cpp_reader *pfile, cpp_token *token, const uchar *base,
1221 unsigned int len, enum cpp_ttype type)
Neil Booth0d9f2342000-09-18 18:43:05 +00001222{
Neil Booth6338b352003-04-23 22:44:06 +00001223 uchar *dest = _cpp_unaligned_alloc (pfile, len + 1);
Neil Booth0d9f2342000-09-18 18:43:05 +00001224
Neil Booth6338b352003-04-23 22:44:06 +00001225 memcpy (dest, base, len);
1226 dest[len] = '\0';
1227 token->type = type;
1228 token->val.str.len = len;
1229 token->val.str.text = dest;
1230}
1231
Jason Merrill00a81b82010-03-29 16:07:29 -04001232/* Subroutine of lex_raw_string: Append LEN chars from BASE to the buffer
1233 sequence from *FIRST_BUFF_P to LAST_BUFF_P. */
1234
1235static void
1236bufring_append (cpp_reader *pfile, const uchar *base, size_t len,
1237 _cpp_buff **first_buff_p, _cpp_buff **last_buff_p)
1238{
1239 _cpp_buff *first_buff = *first_buff_p;
1240 _cpp_buff *last_buff = *last_buff_p;
1241
1242 if (first_buff == NULL)
1243 first_buff = last_buff = _cpp_get_buff (pfile, len);
1244 else if (len > BUFF_ROOM (last_buff))
1245 {
1246 size_t room = BUFF_ROOM (last_buff);
1247 memcpy (BUFF_FRONT (last_buff), base, room);
1248 BUFF_FRONT (last_buff) += room;
1249 base += room;
1250 len -= room;
1251 last_buff = _cpp_append_extend_buff (pfile, last_buff, len);
1252 }
1253
1254 memcpy (BUFF_FRONT (last_buff), base, len);
1255 BUFF_FRONT (last_buff) += len;
1256
1257 *first_buff_p = first_buff;
1258 *last_buff_p = last_buff;
1259}
1260
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001261/* Lexes a raw string. The stored string contains the spelling, including
Jason Merrill00a81b82010-03-29 16:07:29 -04001262 double quotes, delimiter string, '(' and ')', any leading
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001263 'L', 'u', 'U' or 'u8' and 'R' modifier. It returns the type of the
1264 literal, or CPP_OTHER if it was not properly terminated.
1265
1266 The spelling is NUL-terminated, but it is not guaranteed that this
1267 is the first NUL since embedded NULs are preserved. */
1268
1269static void
1270lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base,
1271 const uchar *cur)
1272{
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001273 const uchar *raw_prefix;
1274 unsigned int raw_prefix_len = 0;
1275 enum cpp_ttype type;
1276 size_t total_len = 0;
1277 _cpp_buff *first_buff = NULL, *last_buff = NULL;
Jason Merrill00a81b82010-03-29 16:07:29 -04001278 _cpp_line_note *note = &pfile->buffer->notes[pfile->buffer->cur_note];
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001279
1280 type = (*base == 'L' ? CPP_WSTRING :
1281 *base == 'U' ? CPP_STRING32 :
1282 *base == 'u' ? (base[1] == '8' ? CPP_UTF8STRING : CPP_STRING16)
1283 : CPP_STRING);
1284
1285 raw_prefix = cur + 1;
1286 while (raw_prefix_len < 16)
1287 {
1288 switch (raw_prefix[raw_prefix_len])
1289 {
Jason Merrill52150622010-03-29 11:00:43 -04001290 case ' ': case '(': case ')': case '\\': case '\t':
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001291 case '\v': case '\f': case '\n': default:
1292 break;
1293 /* Basic source charset except the above chars. */
1294 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1295 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1296 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1297 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1298 case 'y': case 'z':
1299 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1300 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1301 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1302 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1303 case 'Y': case 'Z':
1304 case '0': case '1': case '2': case '3': case '4': case '5':
1305 case '6': case '7': case '8': case '9':
Jason Merrill52150622010-03-29 11:00:43 -04001306 case '_': case '{': case '}': case '#': case '[': case ']':
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001307 case '<': case '>': case '%': case ':': case ';': case '.':
1308 case '?': case '*': case '+': case '-': case '/': case '^':
1309 case '&': case '|': case '~': case '!': case '=': case ',':
Jason Merrill52150622010-03-29 11:00:43 -04001310 case '"': case '\'':
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001311 raw_prefix_len++;
1312 continue;
1313 }
1314 break;
1315 }
1316
Jason Merrill52150622010-03-29 11:00:43 -04001317 if (raw_prefix[raw_prefix_len] != '(')
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001318 {
1319 int col = CPP_BUF_COLUMN (pfile->buffer, raw_prefix + raw_prefix_len)
1320 + 1;
1321 if (raw_prefix_len == 16)
1322 cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc, col,
1323 "raw string delimiter longer than 16 characters");
1324 else
1325 cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc, col,
1326 "invalid character '%c' in raw string delimiter",
1327 (int) raw_prefix[raw_prefix_len]);
1328 pfile->buffer->cur = raw_prefix - 1;
1329 create_literal (pfile, token, base, raw_prefix - 1 - base, CPP_OTHER);
1330 return;
1331 }
1332
1333 cur = raw_prefix + raw_prefix_len + 1;
1334 for (;;)
1335 {
Jason Merrill00a81b82010-03-29 16:07:29 -04001336#define BUF_APPEND(STR,LEN) \
1337 do { \
1338 bufring_append (pfile, (const uchar *)(STR), (LEN), \
1339 &first_buff, &last_buff); \
1340 total_len += (LEN); \
1341 } while (0);
1342
1343 cppchar_t c;
1344
1345 /* If we previously performed any trigraph or line splicing
1346 transformations, undo them within the body of the raw string. */
1347 while (note->pos < cur)
1348 ++note;
1349 for (; note->pos == cur; ++note)
1350 {
1351 switch (note->type)
1352 {
1353 case '\\':
1354 case ' ':
1355 /* Restore backslash followed by newline. */
1356 BUF_APPEND (base, cur - base);
1357 base = cur;
1358 BUF_APPEND ("\\", 1);
1359 after_backslash:
1360 if (note->type == ' ')
1361 {
1362 /* GNU backslash whitespace newline extension. FIXME
1363 could be any sequence of non-vertical space. When we
1364 can properly restore any such sequence, we should mark
1365 this note as handled so _cpp_process_line_notes
1366 doesn't warn. */
1367 BUF_APPEND (" ", 1);
1368 }
1369
1370 BUF_APPEND ("\n", 1);
1371 break;
1372
1373 case 0:
1374 /* Already handled. */
1375 break;
1376
1377 default:
1378 if (_cpp_trigraph_map[note->type])
1379 {
1380 /* Don't warn about this trigraph in
1381 _cpp_process_line_notes, since trigraphs show up as
1382 trigraphs in raw strings. */
Jakub Jelinekd947ada2010-04-06 09:02:40 +02001383 uchar type = note->type;
Jason Merrill00a81b82010-03-29 16:07:29 -04001384 note->type = 0;
1385
1386 if (!CPP_OPTION (pfile, trigraphs))
1387 /* If we didn't convert the trigraph in the first
1388 place, don't do anything now either. */
1389 break;
1390
1391 BUF_APPEND (base, cur - base);
1392 base = cur;
1393 BUF_APPEND ("??", 2);
1394
1395 /* ??/ followed by newline gets two line notes, one for
1396 the trigraph and one for the backslash/newline. */
1397 if (type == '/' && note[1].pos == cur)
1398 {
1399 if (note[1].type != '\\'
1400 && note[1].type != ' ')
1401 abort ();
1402 BUF_APPEND ("/", 1);
1403 ++note;
1404 goto after_backslash;
1405 }
1406 /* The ) from ??) could be part of the suffix. */
1407 else if (type == ')'
1408 && strncmp ((const char *) cur+1,
1409 (const char *) raw_prefix,
1410 raw_prefix_len) == 0
1411 && cur[raw_prefix_len+1] == '"')
1412 {
Jakub Jelinek6cfae072011-04-24 01:32:09 +02001413 BUF_APPEND (")", 1);
1414 base++;
1415 cur += raw_prefix_len + 2;
Jason Merrill00a81b82010-03-29 16:07:29 -04001416 goto break_outer_loop;
1417 }
1418 else
1419 {
1420 /* Skip the replacement character. */
1421 base = ++cur;
1422 BUF_APPEND (&type, 1);
1423 }
1424 }
1425 else
1426 abort ();
1427 break;
1428 }
1429 }
1430 c = *cur++;
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001431
Jason Merrill52150622010-03-29 11:00:43 -04001432 if (c == ')'
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001433 && strncmp ((const char *) cur, (const char *) raw_prefix,
1434 raw_prefix_len) == 0
1435 && cur[raw_prefix_len] == '"')
1436 {
1437 cur += raw_prefix_len + 1;
1438 break;
1439 }
1440 else if (c == '\n')
1441 {
1442 if (pfile->state.in_directive
1443 || pfile->state.parsing_args
1444 || pfile->state.in_deferred_pragma)
1445 {
1446 cur--;
1447 type = CPP_OTHER;
1448 cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc, 0,
1449 "unterminated raw string");
1450 break;
1451 }
1452
Jason Merrill00a81b82010-03-29 16:07:29 -04001453 BUF_APPEND (base, cur - base);
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001454
1455 if (pfile->buffer->cur < pfile->buffer->rlimit)
1456 CPP_INCREMENT_LINE (pfile, 0);
1457 pfile->buffer->need_line = true;
1458
Jason Merrill00a81b82010-03-29 16:07:29 -04001459 pfile->buffer->cur = cur-1;
1460 _cpp_process_line_notes (pfile, false);
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001461 if (!_cpp_get_fresh_line (pfile))
1462 {
1463 source_location src_loc = token->src_loc;
1464 token->type = CPP_EOF;
1465 /* Tell the compiler the line number of the EOF token. */
1466 token->src_loc = pfile->line_table->highest_line;
1467 token->flags = BOL;
1468 if (first_buff != NULL)
1469 _cpp_release_buff (pfile, first_buff);
1470 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1471 "unterminated raw string");
1472 return;
1473 }
1474
1475 cur = base = pfile->buffer->cur;
Jason Merrill00a81b82010-03-29 16:07:29 -04001476 note = &pfile->buffer->notes[pfile->buffer->cur_note];
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001477 }
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001478 }
Jason Merrill00a81b82010-03-29 16:07:29 -04001479 break_outer_loop:
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001480
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001481 pfile->buffer->cur = cur;
1482 if (first_buff == NULL)
1483 create_literal (pfile, token, base, cur - base, type);
1484 else
1485 {
1486 uchar *dest = _cpp_unaligned_alloc (pfile, total_len + (cur - base) + 1);
1487
1488 token->type = type;
1489 token->val.str.len = total_len + (cur - base);
1490 token->val.str.text = dest;
1491 last_buff = first_buff;
1492 while (last_buff != NULL)
1493 {
1494 memcpy (dest, last_buff->base,
1495 BUFF_FRONT (last_buff) - last_buff->base);
1496 dest += BUFF_FRONT (last_buff) - last_buff->base;
1497 last_buff = last_buff->next;
1498 }
1499 _cpp_release_buff (pfile, first_buff);
1500 memcpy (dest, base, cur - base);
1501 dest[cur - base] = '\0';
1502 }
1503}
1504
Neil Booth6338b352003-04-23 22:44:06 +00001505/* Lexes a string, character constant, or angle-bracketed header file
1506 name. The stored string contains the spelling, including opening
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001507 quote and any leading 'L', 'u', 'U' or 'u8' and optional
1508 'R' modifier. It returns the type of the literal, or CPP_OTHER
1509 if it was not properly terminated, or CPP_LESS for an unterminated
1510 header name which must be relexed as normal tokens.
Neil Booth6338b352003-04-23 22:44:06 +00001511
1512 The spelling is NUL-terminated, but it is not guaranteed that this
1513 is the first NUL since embedded NULs are preserved. */
1514static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001515lex_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
Neil Booth6338b352003-04-23 22:44:06 +00001516{
1517 bool saw_NUL = false;
1518 const uchar *cur;
1519 cppchar_t terminator;
1520 enum cpp_ttype type;
1521
1522 cur = base;
1523 terminator = *cur++;
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001524 if (terminator == 'L' || terminator == 'U')
Neil Booth6338b352003-04-23 22:44:06 +00001525 terminator = *cur++;
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001526 else if (terminator == 'u')
1527 {
1528 terminator = *cur++;
1529 if (terminator == '8')
1530 terminator = *cur++;
1531 }
1532 if (terminator == 'R')
1533 {
1534 lex_raw_string (pfile, token, base, cur);
1535 return;
1536 }
1537 if (terminator == '"')
Kris Van Heesb6baa672008-04-18 13:58:08 +00001538 type = (*base == 'L' ? CPP_WSTRING :
1539 *base == 'U' ? CPP_STRING32 :
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02001540 *base == 'u' ? (base[1] == '8' ? CPP_UTF8STRING : CPP_STRING16)
1541 : CPP_STRING);
Neil Booth6338b352003-04-23 22:44:06 +00001542 else if (terminator == '\'')
Kris Van Heesb6baa672008-04-18 13:58:08 +00001543 type = (*base == 'L' ? CPP_WCHAR :
1544 *base == 'U' ? CPP_CHAR32 :
1545 *base == 'u' ? CPP_CHAR16 : CPP_CHAR);
Neil Booth6338b352003-04-23 22:44:06 +00001546 else
1547 terminator = '>', type = CPP_HEADER_NAME;
Neil Booth93c803682000-10-28 17:59:06 +00001548
Neil Booth0d9f2342000-09-18 18:43:05 +00001549 for (;;)
1550 {
Neil Booth6338b352003-04-23 22:44:06 +00001551 cppchar_t c = *cur++;
Neil Booth7868b4a2001-03-04 12:02:02 +00001552
Neil Booth6f572ac2003-04-19 16:34:33 +00001553 /* In #include-style directives, terminators are not escapable. */
Neil Booth6338b352003-04-23 22:44:06 +00001554 if (c == '\\' && !pfile->state.angled_headers && *cur != '\n')
1555 cur++;
1556 else if (c == terminator)
Neil Boothbced6ed2003-04-19 11:59:44 +00001557 break;
Neil Booth6338b352003-04-23 22:44:06 +00001558 else if (c == '\n')
Neil Booth0d9f2342000-09-18 18:43:05 +00001559 {
Neil Booth6338b352003-04-23 22:44:06 +00001560 cur--;
Joseph Myers4bb09c22009-02-21 21:25:39 +00001561 /* Unmatched quotes always yield undefined behavior, but
1562 greedy lexing means that what appears to be an unterminated
1563 header name may actually be a legitimate sequence of tokens. */
1564 if (terminator == '>')
1565 {
1566 token->type = CPP_LESS;
1567 return;
1568 }
Neil Booth6338b352003-04-23 22:44:06 +00001569 type = CPP_OTHER;
1570 break;
Neil Booth0d9f2342000-09-18 18:43:05 +00001571 }
Neil Booth6338b352003-04-23 22:44:06 +00001572 else if (c == '\0')
1573 saw_NUL = true;
Neil Booth0d9f2342000-09-18 18:43:05 +00001574 }
1575
Neil Booth6338b352003-04-23 22:44:06 +00001576 if (saw_NUL && !pfile->state.skipping)
John David Anglin0527bc42003-11-01 22:56:54 +00001577 cpp_error (pfile, CPP_DL_WARNING,
1578 "null character(s) preserved in literal");
Neil Booth0d9f2342000-09-18 18:43:05 +00001579
Joseph Myersc663e302006-09-13 02:04:18 +01001580 if (type == CPP_OTHER && CPP_OPTION (pfile, lang) != CLK_ASM)
1581 cpp_error (pfile, CPP_DL_PEDWARN, "missing terminating %c character",
1582 (int) terminator);
1583
Neil Booth6338b352003-04-23 22:44:06 +00001584 pfile->buffer->cur = cur;
1585 create_literal (pfile, token, base, cur - base, type);
Neil Booth0d9f2342000-09-18 18:43:05 +00001586}
1587
Matthew Gingell631d0d32008-10-05 12:35:36 +00001588/* Return the comment table. The client may not make any assumption
1589 about the ordering of the table. */
1590cpp_comment_table *
1591cpp_get_comments (cpp_reader *pfile)
1592{
1593 return &pfile->comments;
1594}
1595
1596/* Append a comment to the end of the comment table. */
1597static void
1598store_comment (cpp_reader *pfile, cpp_token *token)
1599{
1600 int len;
1601
1602 if (pfile->comments.allocated == 0)
1603 {
1604 pfile->comments.allocated = 256;
1605 pfile->comments.entries = (cpp_comment *) xmalloc
1606 (pfile->comments.allocated * sizeof (cpp_comment));
1607 }
1608
1609 if (pfile->comments.count == pfile->comments.allocated)
1610 {
1611 pfile->comments.allocated *= 2;
1612 pfile->comments.entries = (cpp_comment *) xrealloc
1613 (pfile->comments.entries,
1614 pfile->comments.allocated * sizeof (cpp_comment));
1615 }
1616
1617 len = token->val.str.len;
1618
1619 /* Copy comment. Note, token may not be NULL terminated. */
1620 pfile->comments.entries[pfile->comments.count].comment =
1621 (char *) xmalloc (sizeof (char) * (len + 1));
1622 memcpy (pfile->comments.entries[pfile->comments.count].comment,
1623 token->val.str.text, len);
1624 pfile->comments.entries[pfile->comments.count].comment[len] = '\0';
1625
1626 /* Set source location. */
1627 pfile->comments.entries[pfile->comments.count].sloc = token->src_loc;
1628
1629 /* Increment the count of entries in the comment table. */
1630 pfile->comments.count++;
1631}
1632
Neil Booth93c803682000-10-28 17:59:06 +00001633/* The stored comment includes the comment start and any terminator. */
Neil Booth0d9f2342000-09-18 18:43:05 +00001634static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001635save_comment (cpp_reader *pfile, cpp_token *token, const unsigned char *from,
1636 cppchar_t type)
Zack Weinbergc5a04732000-04-25 19:32:36 +00001637{
Neil Booth5d7ee2f2000-05-10 09:39:18 +00001638 unsigned char *buffer;
Kai Tietz651a20b2010-11-16 19:50:17 +00001639 unsigned int len, clen, i;
Kazu Hiratadf383482002-05-22 22:02:16 +00001640
Neil Booth1c6d33e2000-09-25 22:39:51 +00001641 len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */
Neil Booth480709c2001-10-21 14:04:42 +00001642
Neil Booth35422032000-10-29 09:56:00 +00001643 /* C++ comments probably (not definitely) have moved past a new
1644 line, which we don't want to save in the comment. */
Neil Booth480709c2001-10-21 14:04:42 +00001645 if (is_vspace (pfile->buffer->cur[-1]))
Neil Booth35422032000-10-29 09:56:00 +00001646 len--;
Jason Thorpe477cdac2002-04-07 03:12:23 +00001647
Kai Tietz651a20b2010-11-16 19:50:17 +00001648 /* If we are currently in a directive or in argument parsing, then
1649 we need to store all C++ comments as C comments internally, and
1650 so we need to allocate a little extra space in that case.
Jason Thorpe477cdac2002-04-07 03:12:23 +00001651
1652 Note that the only time we encounter a directive here is
1653 when we are saving comments in a "#define". */
Kai Tietz651a20b2010-11-16 19:50:17 +00001654 clen = ((pfile->state.in_directive || pfile->state.parsing_args)
1655 && type == '/') ? len + 2 : len;
Jason Thorpe477cdac2002-04-07 03:12:23 +00001656
1657 buffer = _cpp_unaligned_alloc (pfile, clen);
Kazu Hiratadf383482002-05-22 22:02:16 +00001658
Neil Booth0d9f2342000-09-18 18:43:05 +00001659 token->type = CPP_COMMENT;
Jason Thorpe477cdac2002-04-07 03:12:23 +00001660 token->val.str.len = clen;
Neil Booth0d9f2342000-09-18 18:43:05 +00001661 token->val.str.text = buffer;
Neil Boothd1d9a6b2000-05-27 23:19:56 +00001662
Neil Booth1c6d33e2000-09-25 22:39:51 +00001663 buffer[0] = '/';
1664 memcpy (buffer + 1, from, len - 1);
Jason Thorpe477cdac2002-04-07 03:12:23 +00001665
Kazu Hirata1eeeb6a2002-04-30 20:48:55 +00001666 /* Finish conversion to a C comment, if necessary. */
Kai Tietz651a20b2010-11-16 19:50:17 +00001667 if ((pfile->state.in_directive || pfile->state.parsing_args) && type == '/')
Jason Thorpe477cdac2002-04-07 03:12:23 +00001668 {
1669 buffer[1] = '*';
1670 buffer[clen - 2] = '*';
1671 buffer[clen - 1] = '/';
Kai Tietz651a20b2010-11-16 19:50:17 +00001672 /* As there can be in a C++ comments illegal sequences for C comments
1673 we need to filter them out. */
1674 for (i = 2; i < (clen - 2); i++)
1675 if (buffer[i] == '/' && (buffer[i - 1] == '*' || buffer[i + 1] == '*'))
1676 buffer[i] = '|';
Jason Thorpe477cdac2002-04-07 03:12:23 +00001677 }
Matthew Gingell631d0d32008-10-05 12:35:36 +00001678
1679 /* Finally store this comment for use by clients of libcpp. */
1680 store_comment (pfile, token);
Neil Booth0d9f2342000-09-18 18:43:05 +00001681}
1682
Neil Booth5fddcff2001-09-11 07:00:12 +00001683/* Allocate COUNT tokens for RUN. */
1684void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001685_cpp_init_tokenrun (tokenrun *run, unsigned int count)
Neil Booth5fddcff2001-09-11 07:00:12 +00001686{
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001687 run->base = XNEWVEC (cpp_token, count);
Neil Booth5fddcff2001-09-11 07:00:12 +00001688 run->limit = run->base + count;
1689 run->next = NULL;
1690}
1691
1692/* Returns the next tokenrun, or creates one if there is none. */
1693static tokenrun *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001694next_tokenrun (tokenrun *run)
Neil Booth5fddcff2001-09-11 07:00:12 +00001695{
1696 if (run->next == NULL)
1697 {
Bernardo Innocenti72bb2c32004-07-24 20:04:42 +02001698 run->next = XNEW (tokenrun);
Neil Boothbdcbe492001-09-13 20:05:17 +00001699 run->next->prev = run;
Neil Booth5fddcff2001-09-11 07:00:12 +00001700 _cpp_init_tokenrun (run->next, 250);
1701 }
1702
1703 return run->next;
1704}
1705
Tom Tromey92582b72011-10-17 09:59:12 +00001706/* Return the number of not yet processed token in the the current
1707 context. */
1708int
1709_cpp_remaining_tokens_num_in_context (cpp_reader *pfile)
1710{
1711 cpp_context *context = pfile->context;
1712 if (context->tokens_kind == TOKENS_KIND_DIRECT)
Dodji Seketelicbbcf652011-10-20 08:49:29 +00001713 return (LAST (context).token - FIRST (context).token);
Tom Tromey92582b72011-10-17 09:59:12 +00001714 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
1715 || context->tokens_kind == TOKENS_KIND_EXTENDED)
Dodji Seketelicbbcf652011-10-20 08:49:29 +00001716 return (LAST (context).ptoken - FIRST (context).ptoken);
Tom Tromey92582b72011-10-17 09:59:12 +00001717 else
1718 abort ();
1719}
1720
1721/* Returns the token present at index INDEX in the current context.
1722 If INDEX is zero, the next token to be processed is returned. */
1723static const cpp_token*
1724_cpp_token_from_context_at (cpp_reader *pfile, int index)
1725{
1726 cpp_context *context = pfile->context;
1727 if (context->tokens_kind == TOKENS_KIND_DIRECT)
1728 return &(FIRST (context).token[index]);
1729 else if (context->tokens_kind == TOKENS_KIND_INDIRECT
1730 || context->tokens_kind == TOKENS_KIND_EXTENDED)
1731 return FIRST (context).ptoken[index];
1732 else
1733 abort ();
1734}
1735
Ben Elliston5950c3c2008-07-14 05:09:48 +00001736/* Look ahead in the input stream. */
1737const cpp_token *
1738cpp_peek_token (cpp_reader *pfile, int index)
1739{
1740 cpp_context *context = pfile->context;
1741 const cpp_token *peektok;
1742 int count;
1743
1744 /* First, scan through any pending cpp_context objects. */
1745 while (context->prev)
1746 {
Tom Tromey92582b72011-10-17 09:59:12 +00001747 ptrdiff_t sz = _cpp_remaining_tokens_num_in_context (pfile);
Ben Elliston5950c3c2008-07-14 05:09:48 +00001748
1749 if (index < (int) sz)
Tom Tromey92582b72011-10-17 09:59:12 +00001750 return _cpp_token_from_context_at (pfile, index);
Ben Elliston5950c3c2008-07-14 05:09:48 +00001751 index -= (int) sz;
1752 context = context->prev;
1753 }
1754
1755 /* We will have to read some new tokens after all (and do so
1756 without invalidating preceding tokens). */
1757 count = index;
1758 pfile->keep_tokens++;
1759
1760 do
1761 {
1762 peektok = _cpp_lex_token (pfile);
1763 if (peektok->type == CPP_EOF)
1764 return peektok;
1765 }
1766 while (index--);
1767
1768 _cpp_backup_tokens_direct (pfile, count + 1);
1769 pfile->keep_tokens--;
1770
1771 return peektok;
1772}
1773
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001774/* Allocate a single token that is invalidated at the same time as the
1775 rest of the tokens on the line. Has its line and col set to the
1776 same as the last lexed token, so that diagnostics appear in the
1777 right place. */
1778cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001779_cpp_temp_token (cpp_reader *pfile)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001780{
1781 cpp_token *old, *result;
Ben Elliston5950c3c2008-07-14 05:09:48 +00001782 ptrdiff_t sz = pfile->cur_run->limit - pfile->cur_token;
1783 ptrdiff_t la = (ptrdiff_t) pfile->lookaheads;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001784
1785 old = pfile->cur_token - 1;
Ben Elliston5950c3c2008-07-14 05:09:48 +00001786 /* Any pre-existing lookaheads must not be clobbered. */
1787 if (la)
1788 {
1789 if (sz <= la)
1790 {
1791 tokenrun *next = next_tokenrun (pfile->cur_run);
1792
1793 if (sz < la)
1794 memmove (next->base + 1, next->base,
1795 (la - sz) * sizeof (cpp_token));
1796
1797 next->base[0] = pfile->cur_run->limit[-1];
1798 }
1799
1800 if (sz > 1)
1801 memmove (pfile->cur_token + 1, pfile->cur_token,
1802 MIN (la, sz - 1) * sizeof (cpp_token));
1803 }
1804
1805 if (!sz && pfile->cur_token == pfile->cur_run->limit)
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001806 {
1807 pfile->cur_run = next_tokenrun (pfile->cur_run);
1808 pfile->cur_token = pfile->cur_run->base;
1809 }
1810
1811 result = pfile->cur_token++;
Per Bothner12f9df42004-02-11 07:29:30 -08001812 result->src_loc = old->src_loc;
Neil Booth4ed5bcf2001-09-24 22:53:12 +00001813 return result;
1814}
1815
Neil Booth14baae02001-09-17 18:26:12 +00001816/* Lex a token into RESULT (external interface). Takes care of issues
1817 like directive handling, token lookahead, multiple include
Joseph Myersa1f300c2001-11-23 02:05:19 +00001818 optimization and skipping. */
Neil Booth345894b2001-09-16 13:44:29 +00001819const cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001820_cpp_lex_token (cpp_reader *pfile)
Neil Booth0d9f2342000-09-18 18:43:05 +00001821{
Neil Boothbdcbe492001-09-13 20:05:17 +00001822 cpp_token *result;
Neil Booth5fddcff2001-09-11 07:00:12 +00001823
Neil Boothbdcbe492001-09-13 20:05:17 +00001824 for (;;)
Neil Booth5fddcff2001-09-11 07:00:12 +00001825 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001826 if (pfile->cur_token == pfile->cur_run->limit)
Neil Booth5fddcff2001-09-11 07:00:12 +00001827 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001828 pfile->cur_run = next_tokenrun (pfile->cur_run);
1829 pfile->cur_token = pfile->cur_run->base;
1830 }
Tom Tromeyee380362007-01-30 15:46:01 +00001831 /* We assume that the current token is somewhere in the current
1832 run. */
1833 if (pfile->cur_token < pfile->cur_run->base
1834 || pfile->cur_token >= pfile->cur_run->limit)
1835 abort ();
Neil Boothbdcbe492001-09-13 20:05:17 +00001836
1837 if (pfile->lookaheads)
Neil Booth14baae02001-09-17 18:26:12 +00001838 {
1839 pfile->lookaheads--;
1840 result = pfile->cur_token++;
1841 }
Neil Boothbdcbe492001-09-13 20:05:17 +00001842 else
Neil Booth14baae02001-09-17 18:26:12 +00001843 result = _cpp_lex_direct (pfile);
Neil Boothbdcbe492001-09-13 20:05:17 +00001844
1845 if (result->flags & BOL)
1846 {
Neil Boothbdcbe492001-09-13 20:05:17 +00001847 /* Is this a directive. If _cpp_handle_directive returns
1848 false, it is an assembler #. */
1849 if (result->type == CPP_HASH
Neil Boothe808ec92002-02-27 07:24:53 +00001850 /* 6.10.3 p 11: Directives in a list of macro arguments
1851 gives undefined behavior. This implementation
1852 handles the directive as normal. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001853 && pfile->state.parsing_args != 1)
Zack Weinberg21b11492004-09-09 19:16:56 +00001854 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001855 if (_cpp_handle_directive (pfile, result->flags & PREV_WHITE))
Zack Weinberg21b11492004-09-09 19:16:56 +00001856 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001857 if (pfile->directive_result.type == CPP_PADDING)
1858 continue;
Zack Weinberg21b11492004-09-09 19:16:56 +00001859 result = &pfile->directive_result;
Zack Weinberg21b11492004-09-09 19:16:56 +00001860 }
1861 }
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001862 else if (pfile->state.in_deferred_pragma)
1863 result = &pfile->directive_result;
Zack Weinberg21b11492004-09-09 19:16:56 +00001864
Neil Booth97293892001-09-14 22:04:46 +00001865 if (pfile->cb.line_change && !pfile->state.skipping)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001866 pfile->cb.line_change (pfile, result, pfile->state.parsing_args);
Neil Booth5fddcff2001-09-11 07:00:12 +00001867 }
1868
Neil Boothbdcbe492001-09-13 20:05:17 +00001869 /* We don't skip tokens in directives. */
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001870 if (pfile->state.in_directive || pfile->state.in_deferred_pragma)
Neil Boothbdcbe492001-09-13 20:05:17 +00001871 break;
Neil Booth5fddcff2001-09-11 07:00:12 +00001872
Neil Boothbdcbe492001-09-13 20:05:17 +00001873 /* Outside a directive, invalidate controlling macros. At file
Neil Booth14baae02001-09-17 18:26:12 +00001874 EOF, _cpp_lex_direct takes care of popping the buffer, so we never
Kazu Hirata6356f892003-06-12 19:01:08 +00001875 get here and MI optimization works. */
Neil Booth5fddcff2001-09-11 07:00:12 +00001876 pfile->mi_valid = false;
Neil Boothbdcbe492001-09-13 20:05:17 +00001877
1878 if (!pfile->state.skipping || result->type == CPP_EOF)
1879 break;
Neil Booth5fddcff2001-09-11 07:00:12 +00001880 }
1881
Neil Booth345894b2001-09-16 13:44:29 +00001882 return result;
Neil Booth5fddcff2001-09-11 07:00:12 +00001883}
1884
Neil Booth26aea072003-04-19 00:22:51 +00001885/* Returns true if a fresh line has been loaded. */
1886bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001887_cpp_get_fresh_line (cpp_reader *pfile)
Neil Booth004cb262002-05-17 20:16:48 +00001888{
Per Bothner22234f52004-02-18 14:02:39 -08001889 int return_at_eof;
1890
Neil Booth26aea072003-04-19 00:22:51 +00001891 /* We can't get a new line until we leave the current directive. */
1892 if (pfile->state.in_directive)
1893 return false;
Kazu Hiratadf383482002-05-22 22:02:16 +00001894
Neil Booth26aea072003-04-19 00:22:51 +00001895 for (;;)
Neil Booth1a769162002-06-11 05:36:17 +00001896 {
Neil Booth26aea072003-04-19 00:22:51 +00001897 cpp_buffer *buffer = pfile->buffer;
1898
1899 if (!buffer->need_line)
1900 return true;
1901
1902 if (buffer->next_line < buffer->rlimit)
1903 {
1904 _cpp_clean_line (pfile);
1905 return true;
1906 }
1907
1908 /* First, get out of parsing arguments state. */
1909 if (pfile->state.parsing_args)
Neil Booth1a769162002-06-11 05:36:17 +00001910 return false;
1911
Neil Booth26aea072003-04-19 00:22:51 +00001912 /* End of buffer. Non-empty files should end in a newline. */
1913 if (buffer->buf != buffer->rlimit
1914 && buffer->next_line > buffer->rlimit
1915 && !buffer->from_stage3)
Neil Booth004cb262002-05-17 20:16:48 +00001916 {
Dave Korned0e74e2007-05-31 02:06:48 +00001917 /* Clip to buffer size. */
Neil Booth26aea072003-04-19 00:22:51 +00001918 buffer->next_line = buffer->rlimit;
Neil Booth004cb262002-05-17 20:16:48 +00001919 }
Per Bothner22234f52004-02-18 14:02:39 -08001920
1921 return_at_eof = buffer->return_at_eof;
Neil Booth26aea072003-04-19 00:22:51 +00001922 _cpp_pop_buffer (pfile);
Per Bothner22234f52004-02-18 14:02:39 -08001923 if (pfile->buffer == NULL || return_at_eof)
Per Bothnera506c552003-10-02 07:20:38 +00001924 return false;
Neil Booth26aea072003-04-19 00:22:51 +00001925 }
Neil Booth004cb262002-05-17 20:16:48 +00001926}
1927
Neil Booth6f572ac2003-04-19 16:34:33 +00001928#define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \
1929 do \
1930 { \
1931 result->type = ELSE_TYPE; \
1932 if (*buffer->cur == CHAR) \
1933 buffer->cur++, result->type = THEN_TYPE; \
1934 } \
1935 while (0)
Neil Booth480709c2001-10-21 14:04:42 +00001936
Neil Booth14baae02001-09-17 18:26:12 +00001937/* Lex a token into pfile->cur_token, which is also incremented, to
1938 get diagnostics pointing to the correct location.
1939
1940 Does not handle issues such as token lookahead, multiple-include
Kazu Hirataf1ba6652003-06-28 19:43:01 +00001941 optimization, directives, skipping etc. This function is only
Neil Booth14baae02001-09-17 18:26:12 +00001942 suitable for use by _cpp_lex_token, and in special cases like
1943 lex_expansion_token which doesn't care for any of these issues.
1944
1945 When meeting a newline, returns CPP_EOF if parsing a directive,
1946 otherwise returns to the start of the token buffer if permissible.
1947 Returns the location of the lexed token. */
1948cpp_token *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001949_cpp_lex_direct (cpp_reader *pfile)
Neil Booth5fddcff2001-09-11 07:00:12 +00001950{
Neil Booth0d9f2342000-09-18 18:43:05 +00001951 cppchar_t c;
Neil Boothadb84b42000-11-08 23:08:07 +00001952 cpp_buffer *buffer;
Neil Booth0d9f2342000-09-18 18:43:05 +00001953 const unsigned char *comment_start;
Neil Booth14baae02001-09-17 18:26:12 +00001954 cpp_token *result = pfile->cur_token++;
Neil Booth0d9f2342000-09-18 18:43:05 +00001955
Neil Booth5fddcff2001-09-11 07:00:12 +00001956 fresh_line:
Neil Booth26aea072003-04-19 00:22:51 +00001957 result->flags = 0;
Per Bothner2be570f2003-08-28 18:07:42 -07001958 buffer = pfile->buffer;
Per Bothnera506c552003-10-02 07:20:38 +00001959 if (buffer->need_line)
Neil Booth26aea072003-04-19 00:22:51 +00001960 {
Richard Hendersonbc4071d2006-01-04 08:33:38 -08001961 if (pfile->state.in_deferred_pragma)
1962 {
1963 result->type = CPP_PRAGMA_EOL;
1964 pfile->state.in_deferred_pragma = false;
1965 if (!pfile->state.pragma_allow_expansion)
1966 pfile->state.prevent_expansion--;
1967 return result;
1968 }
Neil Booth26aea072003-04-19 00:22:51 +00001969 if (!_cpp_get_fresh_line (pfile))
1970 {
1971 result->type = CPP_EOF;
Neil Booth9ff78682003-04-26 21:03:51 +00001972 if (!pfile->state.in_directive)
1973 {
1974 /* Tell the compiler the line number of the EOF token. */
Per Bothner500bee02004-04-22 19:22:27 -07001975 result->src_loc = pfile->line_table->highest_line;
Neil Booth9ff78682003-04-26 21:03:51 +00001976 result->flags = BOL;
1977 }
Neil Booth26aea072003-04-19 00:22:51 +00001978 return result;
1979 }
1980 if (!pfile->keep_tokens)
1981 {
1982 pfile->cur_run = &pfile->base_run;
1983 result = pfile->base_run.base;
1984 pfile->cur_token = result + 1;
1985 }
1986 result->flags = BOL;
1987 if (pfile->state.parsing_args == 2)
1988 result->flags |= PREV_WHITE;
1989 }
Per Bothnera506c552003-10-02 07:20:38 +00001990 buffer = pfile->buffer;
Neil Booth5fddcff2001-09-11 07:00:12 +00001991 update_tokens_line:
Per Bothner500bee02004-04-22 19:22:27 -07001992 result->src_loc = pfile->line_table->highest_line;
Neil Booth0d9f2342000-09-18 18:43:05 +00001993
Neil Booth5fddcff2001-09-11 07:00:12 +00001994 skipped_white:
Neil Booth26aea072003-04-19 00:22:51 +00001995 if (buffer->cur >= buffer->notes[buffer->cur_note].pos
1996 && !pfile->overlaid_buffer)
1997 {
1998 _cpp_process_line_notes (pfile, false);
Per Bothner500bee02004-04-22 19:22:27 -07001999 result->src_loc = pfile->line_table->highest_line;
Neil Booth26aea072003-04-19 00:22:51 +00002000 }
Neil Booth480709c2001-10-21 14:04:42 +00002001 c = *buffer->cur++;
Per Bothner12f9df42004-02-11 07:29:30 -08002002
Gabriel Charettee3dfef42011-08-22 20:41:07 +00002003 if (pfile->forced_token_location_p)
2004 result->src_loc = *pfile->forced_token_location_p;
2005 else
2006 result->src_loc = linemap_position_for_column (pfile->line_table,
2007 CPP_BUF_COLUMN (buffer, buffer->cur));
Neil Booth5fddcff2001-09-11 07:00:12 +00002008
Neil Booth0d9f2342000-09-18 18:43:05 +00002009 switch (c)
2010 {
Neil Booth4d6baaf2001-11-26 23:44:54 +00002011 case ' ': case '\t': case '\f': case '\v': case '\0':
2012 result->flags |= PREV_WHITE;
Neil Booth26aea072003-04-19 00:22:51 +00002013 skip_whitespace (pfile, c);
2014 goto skipped_white;
Neil Booth4d6baaf2001-11-26 23:44:54 +00002015
Neil Booth26aea072003-04-19 00:22:51 +00002016 case '\n':
Per Bothner12f9df42004-02-11 07:29:30 -08002017 if (buffer->cur < buffer->rlimit)
2018 CPP_INCREMENT_LINE (pfile, 0);
Neil Booth26aea072003-04-19 00:22:51 +00002019 buffer->need_line = true;
2020 goto fresh_line;
Neil Booth0d9f2342000-09-18 18:43:05 +00002021
Neil Booth0d9f2342000-09-18 18:43:05 +00002022 case '0': case '1': case '2': case '3': case '4':
2023 case '5': case '6': case '7': case '8': case '9':
Geoffrey Keating50668cf2005-03-15 00:36:33 +00002024 {
2025 struct normalize_state nst = INITIAL_NORMALIZE_STATE;
2026 result->type = CPP_NUMBER;
2027 lex_number (pfile, &result->val.str, &nst);
2028 warn_about_normalization (pfile, result, &nst);
2029 break;
2030 }
Neil Booth0d9f2342000-09-18 18:43:05 +00002031
Neil Booth0abc6a62001-11-27 22:31:34 +00002032 case 'L':
Kris Van Heesb6baa672008-04-18 13:58:08 +00002033 case 'u':
2034 case 'U':
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02002035 case 'R':
2036 /* 'L', 'u', 'U', 'u8' or 'R' may introduce wide characters,
2037 wide strings or raw strings. */
Joseph Myersa48e3dd2011-08-18 16:13:49 +01002038 if (c == 'L' || CPP_OPTION (pfile, rliterals)
2039 || (c != 'R' && CPP_OPTION (pfile, uliterals)))
Neil Boothbced6ed2003-04-19 11:59:44 +00002040 {
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02002041 if ((*buffer->cur == '\'' && c != 'R')
2042 || *buffer->cur == '"'
2043 || (*buffer->cur == 'R'
2044 && c != 'R'
2045 && buffer->cur[1] == '"'
Joseph Myersa48e3dd2011-08-18 16:13:49 +01002046 && CPP_OPTION (pfile, rliterals))
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02002047 || (*buffer->cur == '8'
2048 && c == 'u'
2049 && (buffer->cur[1] == '"'
Joseph Myersa48e3dd2011-08-18 16:13:49 +01002050 || (buffer->cur[1] == 'R' && buffer->cur[2] == '"'
2051 && CPP_OPTION (pfile, rliterals)))))
Kris Van Heesb6baa672008-04-18 13:58:08 +00002052 {
2053 lex_string (pfile, result, buffer->cur - 1);
2054 break;
2055 }
Neil Boothbced6ed2003-04-19 11:59:44 +00002056 }
Kazu Hiratadf383482002-05-22 22:02:16 +00002057 /* Fall through. */
Neil Booth0abc6a62001-11-27 22:31:34 +00002058
Neil Booth0d9f2342000-09-18 18:43:05 +00002059 case '_':
2060 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2061 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2062 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
Kris Van Heesb6baa672008-04-18 13:58:08 +00002063 case 's': case 't': case 'v': case 'w': case 'x':
Neil Booth0d9f2342000-09-18 18:43:05 +00002064 case 'y': case 'z':
2065 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
Neil Booth0abc6a62001-11-27 22:31:34 +00002066 case 'G': case 'H': case 'I': case 'J': case 'K':
Jakub Jelinek2c6e3f52009-10-19 23:41:15 +02002067 case 'M': case 'N': case 'O': case 'P': case 'Q':
Kris Van Heesb6baa672008-04-18 13:58:08 +00002068 case 'S': case 'T': case 'V': case 'W': case 'X':
Neil Booth0d9f2342000-09-18 18:43:05 +00002069 case 'Y': case 'Z':
2070 result->type = CPP_NAME;
Geoffrey Keating50668cf2005-03-15 00:36:33 +00002071 {
2072 struct normalize_state nst = INITIAL_NORMALIZE_STATE;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002073 result->val.node.node = lex_identifier (pfile, buffer->cur - 1, false,
2074 &nst);
Geoffrey Keating50668cf2005-03-15 00:36:33 +00002075 warn_about_normalization (pfile, result, &nst);
2076 }
Neil Booth0d9f2342000-09-18 18:43:05 +00002077
Neil Booth0d9f2342000-09-18 18:43:05 +00002078 /* Convert named operators to their proper types. */
Joseph Myers9a0c6182009-05-10 15:27:32 +01002079 if (result->val.node.node->flags & NODE_OPERATOR)
Neil Booth0d9f2342000-09-18 18:43:05 +00002080 {
2081 result->flags |= NAMED_OP;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002082 result->type = (enum cpp_ttype) result->val.node.node->directive_index;
Neil Booth0d9f2342000-09-18 18:43:05 +00002083 }
2084 break;
2085
2086 case '\'':
2087 case '"':
Neil Booth6338b352003-04-23 22:44:06 +00002088 lex_string (pfile, result, buffer->cur - 1);
Neil Booth0d9f2342000-09-18 18:43:05 +00002089 break;
2090
2091 case '/':
Neil Booth1c6d33e2000-09-25 22:39:51 +00002092 /* A potential block or line comment. */
2093 comment_start = buffer->cur;
Neil Booth6f572ac2003-04-19 16:34:33 +00002094 c = *buffer->cur;
2095
Neil Booth1c6d33e2000-09-25 22:39:51 +00002096 if (c == '*')
2097 {
Neil Booth26aea072003-04-19 00:22:51 +00002098 if (_cpp_skip_block_comment (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +00002099 cpp_error (pfile, CPP_DL_ERROR, "unterminated comment");
Neil Booth0d9f2342000-09-18 18:43:05 +00002100 }
Neil Booth480709c2001-10-21 14:04:42 +00002101 else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments)
Per Bothner12f9df42004-02-11 07:29:30 -08002102 || cpp_in_system_header (pfile)))
Neil Booth0d9f2342000-09-18 18:43:05 +00002103 {
Neil Boothbdb05a72000-11-26 17:31:13 +00002104 /* Warn about comments only if pedantically GNUC89, and not
2105 in system headers. */
2106 if (CPP_OPTION (pfile, lang) == CLK_GNUC89 && CPP_PEDANTIC (pfile)
Neil Bootha94c1192000-09-25 23:35:10 +00002107 && ! buffer->warned_cplusplus_comments)
Neil Booth0d9f2342000-09-18 18:43:05 +00002108 {
John David Anglin0527bc42003-11-01 22:56:54 +00002109 cpp_error (pfile, CPP_DL_PEDWARN,
Gabriel Dos Reis56508302002-07-21 21:35:17 +00002110 "C++ style comments are not allowed in ISO C90");
John David Anglin0527bc42003-11-01 22:56:54 +00002111 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothebef4e82002-04-14 18:42:47 +00002112 "(this will be reported only once per input file)");
Neil Booth1c6d33e2000-09-25 22:39:51 +00002113 buffer->warned_cplusplus_comments = 1;
Neil Booth0d9f2342000-09-18 18:43:05 +00002114 }
Neil Booth1c6d33e2000-09-25 22:39:51 +00002115
Jakub Jelinek01ef6562001-04-11 11:43:10 +02002116 if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments))
Simon Baldwin87cf0652010-04-07 17:18:10 +00002117 cpp_warning (pfile, CPP_W_COMMENTS, "multi-line comment");
Neil Booth0d9f2342000-09-18 18:43:05 +00002118 }
Neil Booth480709c2001-10-21 14:04:42 +00002119 else if (c == '=')
2120 {
Neil Booth6f572ac2003-04-19 16:34:33 +00002121 buffer->cur++;
Neil Booth480709c2001-10-21 14:04:42 +00002122 result->type = CPP_DIV_EQ;
2123 break;
2124 }
2125 else
2126 {
Neil Booth480709c2001-10-21 14:04:42 +00002127 result->type = CPP_DIV;
2128 break;
2129 }
Neil Booth1c6d33e2000-09-25 22:39:51 +00002130
Neil Booth1c6d33e2000-09-25 22:39:51 +00002131 if (!pfile->state.save_comments)
2132 {
2133 result->flags |= PREV_WHITE;
Neil Booth5fddcff2001-09-11 07:00:12 +00002134 goto update_tokens_line;
Neil Booth1c6d33e2000-09-25 22:39:51 +00002135 }
2136
2137 /* Save the comment as a token in its own right. */
Jason Thorpe477cdac2002-04-07 03:12:23 +00002138 save_comment (pfile, result, comment_start, c);
Neil Boothbdcbe492001-09-13 20:05:17 +00002139 break;
Neil Booth0d9f2342000-09-18 18:43:05 +00002140
2141 case '<':
2142 if (pfile->state.angled_headers)
2143 {
Neil Booth6338b352003-04-23 22:44:06 +00002144 lex_string (pfile, result, buffer->cur - 1);
Joseph Myers4bb09c22009-02-21 21:25:39 +00002145 if (result->type != CPP_LESS)
2146 break;
Neil Booth0d9f2342000-09-18 18:43:05 +00002147 }
2148
Neil Booth6f572ac2003-04-19 16:34:33 +00002149 result->type = CPP_LESS;
2150 if (*buffer->cur == '=')
2151 buffer->cur++, result->type = CPP_LESS_EQ;
2152 else if (*buffer->cur == '<')
Neil Booth0d9f2342000-09-18 18:43:05 +00002153 {
Neil Booth6f572ac2003-04-19 16:34:33 +00002154 buffer->cur++;
2155 IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT);
Neil Booth0d9f2342000-09-18 18:43:05 +00002156 }
Neil Booth6f572ac2003-04-19 16:34:33 +00002157 else if (CPP_OPTION (pfile, digraphs))
Neil Booth480709c2001-10-21 14:04:42 +00002158 {
Neil Booth6f572ac2003-04-19 16:34:33 +00002159 if (*buffer->cur == ':')
2160 {
2161 buffer->cur++;
2162 result->flags |= DIGRAPH;
2163 result->type = CPP_OPEN_SQUARE;
2164 }
2165 else if (*buffer->cur == '%')
2166 {
2167 buffer->cur++;
2168 result->flags |= DIGRAPH;
2169 result->type = CPP_OPEN_BRACE;
2170 }
Neil Booth480709c2001-10-21 14:04:42 +00002171 }
Neil Booth0d9f2342000-09-18 18:43:05 +00002172 break;
2173
2174 case '>':
Neil Booth6f572ac2003-04-19 16:34:33 +00002175 result->type = CPP_GREATER;
2176 if (*buffer->cur == '=')
2177 buffer->cur++, result->type = CPP_GREATER_EQ;
2178 else if (*buffer->cur == '>')
Neil Booth0d9f2342000-09-18 18:43:05 +00002179 {
Neil Booth6f572ac2003-04-19 16:34:33 +00002180 buffer->cur++;
2181 IF_NEXT_IS ('=', CPP_RSHIFT_EQ, CPP_RSHIFT);
2182 }
Neil Booth0d9f2342000-09-18 18:43:05 +00002183 break;
2184
Neil Boothcbcff6d2000-09-23 21:41:41 +00002185 case '%':
Neil Booth6f572ac2003-04-19 16:34:33 +00002186 result->type = CPP_MOD;
2187 if (*buffer->cur == '=')
2188 buffer->cur++, result->type = CPP_MOD_EQ;
2189 else if (CPP_OPTION (pfile, digraphs))
Neil Booth480709c2001-10-21 14:04:42 +00002190 {
Neil Booth6f572ac2003-04-19 16:34:33 +00002191 if (*buffer->cur == ':')
Neil Booth480709c2001-10-21 14:04:42 +00002192 {
Neil Booth6f572ac2003-04-19 16:34:33 +00002193 buffer->cur++;
2194 result->flags |= DIGRAPH;
2195 result->type = CPP_HASH;
2196 if (*buffer->cur == '%' && buffer->cur[1] == ':')
Joseph Myers9a0c6182009-05-10 15:27:32 +01002197 buffer->cur += 2, result->type = CPP_PASTE, result->val.token_no = 0;
Neil Booth480709c2001-10-21 14:04:42 +00002198 }
Neil Booth6f572ac2003-04-19 16:34:33 +00002199 else if (*buffer->cur == '>')
2200 {
2201 buffer->cur++;
2202 result->flags |= DIGRAPH;
2203 result->type = CPP_CLOSE_BRACE;
2204 }
Neil Booth480709c2001-10-21 14:04:42 +00002205 }
Neil Booth0d9f2342000-09-18 18:43:05 +00002206 break;
2207
Neil Boothcbcff6d2000-09-23 21:41:41 +00002208 case '.':
Neil Booth480709c2001-10-21 14:04:42 +00002209 result->type = CPP_DOT;
Neil Booth6f572ac2003-04-19 16:34:33 +00002210 if (ISDIGIT (*buffer->cur))
Neil Booth480709c2001-10-21 14:04:42 +00002211 {
Geoffrey Keating50668cf2005-03-15 00:36:33 +00002212 struct normalize_state nst = INITIAL_NORMALIZE_STATE;
Neil Booth480709c2001-10-21 14:04:42 +00002213 result->type = CPP_NUMBER;
Geoffrey Keating50668cf2005-03-15 00:36:33 +00002214 lex_number (pfile, &result->val.str, &nst);
2215 warn_about_normalization (pfile, result, &nst);
Neil Booth480709c2001-10-21 14:04:42 +00002216 }
Neil Booth6f572ac2003-04-19 16:34:33 +00002217 else if (*buffer->cur == '.' && buffer->cur[1] == '.')
2218 buffer->cur += 2, result->type = CPP_ELLIPSIS;
2219 else if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
2220 buffer->cur++, result->type = CPP_DOT_STAR;
Neil Booth0d9f2342000-09-18 18:43:05 +00002221 break;
2222
2223 case '+':
Neil Booth6f572ac2003-04-19 16:34:33 +00002224 result->type = CPP_PLUS;
2225 if (*buffer->cur == '+')
2226 buffer->cur++, result->type = CPP_PLUS_PLUS;
2227 else if (*buffer->cur == '=')
2228 buffer->cur++, result->type = CPP_PLUS_EQ;
Neil Booth0d9f2342000-09-18 18:43:05 +00002229 break;
2230
2231 case '-':
Neil Booth6f572ac2003-04-19 16:34:33 +00002232 result->type = CPP_MINUS;
2233 if (*buffer->cur == '>')
Neil Booth0d9f2342000-09-18 18:43:05 +00002234 {
Neil Booth6f572ac2003-04-19 16:34:33 +00002235 buffer->cur++;
Neil Booth480709c2001-10-21 14:04:42 +00002236 result->type = CPP_DEREF;
Neil Booth6f572ac2003-04-19 16:34:33 +00002237 if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
2238 buffer->cur++, result->type = CPP_DEREF_STAR;
Neil Booth0d9f2342000-09-18 18:43:05 +00002239 }
Neil Booth6f572ac2003-04-19 16:34:33 +00002240 else if (*buffer->cur == '-')
2241 buffer->cur++, result->type = CPP_MINUS_MINUS;
2242 else if (*buffer->cur == '=')
2243 buffer->cur++, result->type = CPP_MINUS_EQ;
Neil Booth0d9f2342000-09-18 18:43:05 +00002244 break;
2245
2246 case '&':
Neil Booth6f572ac2003-04-19 16:34:33 +00002247 result->type = CPP_AND;
2248 if (*buffer->cur == '&')
2249 buffer->cur++, result->type = CPP_AND_AND;
2250 else if (*buffer->cur == '=')
2251 buffer->cur++, result->type = CPP_AND_EQ;
Neil Booth0d9f2342000-09-18 18:43:05 +00002252 break;
Kazu Hiratadf383482002-05-22 22:02:16 +00002253
Neil Booth0d9f2342000-09-18 18:43:05 +00002254 case '|':
Neil Booth6f572ac2003-04-19 16:34:33 +00002255 result->type = CPP_OR;
2256 if (*buffer->cur == '|')
2257 buffer->cur++, result->type = CPP_OR_OR;
2258 else if (*buffer->cur == '=')
2259 buffer->cur++, result->type = CPP_OR_EQ;
Neil Booth0d9f2342000-09-18 18:43:05 +00002260 break;
2261
2262 case ':':
Neil Booth6f572ac2003-04-19 16:34:33 +00002263 result->type = CPP_COLON;
2264 if (*buffer->cur == ':' && CPP_OPTION (pfile, cplusplus))
2265 buffer->cur++, result->type = CPP_SCOPE;
2266 else if (*buffer->cur == '>' && CPP_OPTION (pfile, digraphs))
Neil Booth0d9f2342000-09-18 18:43:05 +00002267 {
Neil Booth6f572ac2003-04-19 16:34:33 +00002268 buffer->cur++;
Neil Booth0d9f2342000-09-18 18:43:05 +00002269 result->flags |= DIGRAPH;
Neil Booth480709c2001-10-21 14:04:42 +00002270 result->type = CPP_CLOSE_SQUARE;
2271 }
Neil Booth0d9f2342000-09-18 18:43:05 +00002272 break;
2273
Neil Booth480709c2001-10-21 14:04:42 +00002274 case '*': IF_NEXT_IS ('=', CPP_MULT_EQ, CPP_MULT); break;
2275 case '=': IF_NEXT_IS ('=', CPP_EQ_EQ, CPP_EQ); break;
2276 case '!': IF_NEXT_IS ('=', CPP_NOT_EQ, CPP_NOT); break;
2277 case '^': IF_NEXT_IS ('=', CPP_XOR_EQ, CPP_XOR); break;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002278 case '#': IF_NEXT_IS ('#', CPP_PASTE, CPP_HASH); result->val.token_no = 0; break;
Neil Booth480709c2001-10-21 14:04:42 +00002279
Neil Booth26aea072003-04-19 00:22:51 +00002280 case '?': result->type = CPP_QUERY; break;
Neil Booth0d9f2342000-09-18 18:43:05 +00002281 case '~': result->type = CPP_COMPL; break;
2282 case ',': result->type = CPP_COMMA; break;
2283 case '(': result->type = CPP_OPEN_PAREN; break;
2284 case ')': result->type = CPP_CLOSE_PAREN; break;
2285 case '[': result->type = CPP_OPEN_SQUARE; break;
2286 case ']': result->type = CPP_CLOSE_SQUARE; break;
2287 case '{': result->type = CPP_OPEN_BRACE; break;
2288 case '}': result->type = CPP_CLOSE_BRACE; break;
2289 case ';': result->type = CPP_SEMICOLON; break;
2290
Kazu Hirata40f03652002-09-26 22:25:14 +00002291 /* @ is a punctuator in Objective-C. */
Zack Weinbergcc937582001-03-07 01:32:01 +00002292 case '@': result->type = CPP_ATSIGN; break;
Neil Booth0d9f2342000-09-18 18:43:05 +00002293
Neil Booth0abc6a62001-11-27 22:31:34 +00002294 case '$':
Neil Booth1613e522003-04-20 07:29:23 +00002295 case '\\':
2296 {
2297 const uchar *base = --buffer->cur;
Geoffrey Keating50668cf2005-03-15 00:36:33 +00002298 struct normalize_state nst = INITIAL_NORMALIZE_STATE;
Neil Booth0abc6a62001-11-27 22:31:34 +00002299
Geoffrey Keating50668cf2005-03-15 00:36:33 +00002300 if (forms_identifier_p (pfile, true, &nst))
Neil Booth1613e522003-04-20 07:29:23 +00002301 {
2302 result->type = CPP_NAME;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002303 result->val.node.node = lex_identifier (pfile, base, true, &nst);
Geoffrey Keating50668cf2005-03-15 00:36:33 +00002304 warn_about_normalization (pfile, result, &nst);
Neil Booth1613e522003-04-20 07:29:23 +00002305 break;
2306 }
2307 buffer->cur++;
Neil Booth10676942003-04-22 19:28:00 +00002308 }
Neil Booth1613e522003-04-20 07:29:23 +00002309
Neil Booth10676942003-04-22 19:28:00 +00002310 default:
Neil Booth6338b352003-04-23 22:44:06 +00002311 create_literal (pfile, result, buffer->cur - 1, 1, CPP_OTHER);
2312 break;
Neil Booth0d9f2342000-09-18 18:43:05 +00002313 }
Neil Boothbdcbe492001-09-13 20:05:17 +00002314
2315 return result;
Zack Weinbergc5a04732000-04-25 19:32:36 +00002316}
2317
Neil Booth59325652003-04-24 20:03:57 +00002318/* An upper bound on the number of bytes needed to spell TOKEN.
2319 Does not include preceding whitespace. */
Neil Booth93c803682000-10-28 17:59:06 +00002320unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002321cpp_token_len (const cpp_token *token)
Zack Weinbergc5a04732000-04-25 19:32:36 +00002322{
Neil Booth93c803682000-10-28 17:59:06 +00002323 unsigned int len;
Zack Weinbergc5a04732000-04-25 19:32:36 +00002324
Neil Booth93c803682000-10-28 17:59:06 +00002325 switch (TOKEN_SPELL (token))
Zack Weinbergc5a04732000-04-25 19:32:36 +00002326 {
Joseph Myerscc955282008-11-29 12:21:10 +00002327 default: len = 6; break;
Neil Booth6338b352003-04-23 22:44:06 +00002328 case SPELL_LITERAL: len = token->val.str.len; break;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002329 case SPELL_IDENT: len = NODE_LEN (token->val.node.node) * 10; break;
Zack Weinbergc5a04732000-04-25 19:32:36 +00002330 }
Neil Booth59325652003-04-24 20:03:57 +00002331
2332 return len;
Zack Weinberg041c3192000-07-04 01:58:21 +00002333}
2334
Geoffrey Keating47e20492005-03-12 10:44:06 +00002335/* Parse UTF-8 out of NAMEP and place a \U escape in BUFFER.
2336 Return the number of bytes read out of NAME. (There are always
2337 10 bytes written to BUFFER.) */
2338
2339static size_t
2340utf8_to_ucn (unsigned char *buffer, const unsigned char *name)
2341{
2342 int j;
2343 int ucn_len = 0;
2344 int ucn_len_c;
2345 unsigned t;
2346 unsigned long utf32;
2347
2348 /* Compute the length of the UTF-8 sequence. */
2349 for (t = *name; t & 0x80; t <<= 1)
2350 ucn_len++;
2351
2352 utf32 = *name & (0x7F >> ucn_len);
2353 for (ucn_len_c = 1; ucn_len_c < ucn_len; ucn_len_c++)
2354 {
2355 utf32 = (utf32 << 6) | (*++name & 0x3F);
2356
2357 /* Ill-formed UTF-8. */
2358 if ((*name & ~0x3F) != 0x80)
2359 abort ();
2360 }
2361
2362 *buffer++ = '\\';
2363 *buffer++ = 'U';
2364 for (j = 7; j >= 0; j--)
2365 *buffer++ = "0123456789abcdef"[(utf32 >> (4 * j)) & 0xF];
2366 return ucn_len;
2367}
2368
Manuel López-Ibáñezcfc93532009-04-22 15:32:18 +00002369/* Given a token TYPE corresponding to a digraph, return a pointer to
2370 the spelling of the digraph. */
2371static const unsigned char *
2372cpp_digraph2name (enum cpp_ttype type)
2373{
2374 return digraph_spellings[(int) type - (int) CPP_FIRST_DIGRAPH];
2375}
Geoffrey Keating47e20492005-03-12 10:44:06 +00002376
Neil Booth3fef5b22000-05-08 22:22:49 +00002377/* Write the spelling of a token TOKEN to BUFFER. The buffer must
Zack Weinbergcf00a882000-07-08 02:33:00 +00002378 already contain the enough space to hold the token's spelling.
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002379 Returns a pointer to the character after the last character written.
Geoffrey Keating47e20492005-03-12 10:44:06 +00002380 FORSTRING is true if this is to be the spelling after translation
2381 phase 1 (this is different for UCNs).
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002382 FIXME: Would be nice if we didn't need the PFILE argument. */
Neil Booth93c803682000-10-28 17:59:06 +00002383unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002384cpp_spell_token (cpp_reader *pfile, const cpp_token *token,
Geoffrey Keating47e20492005-03-12 10:44:06 +00002385 unsigned char *buffer, bool forstring)
Neil Booth3fef5b22000-05-08 22:22:49 +00002386{
Zack Weinberg96be6992000-07-18 23:25:06 +00002387 switch (TOKEN_SPELL (token))
Neil Booth3fef5b22000-05-08 22:22:49 +00002388 {
Neil Booth5d7ee2f2000-05-10 09:39:18 +00002389 case SPELL_OPERATOR:
Neil Booth3fef5b22000-05-08 22:22:49 +00002390 {
2391 const unsigned char *spelling;
2392 unsigned char c;
2393
2394 if (token->flags & DIGRAPH)
Manuel López-Ibáñezcfc93532009-04-22 15:32:18 +00002395 spelling = cpp_digraph2name (token->type);
Zack Weinberg92936ec2000-07-19 20:18:08 +00002396 else if (token->flags & NAMED_OP)
2397 goto spell_ident;
Neil Booth3fef5b22000-05-08 22:22:49 +00002398 else
Zack Weinberg96be6992000-07-18 23:25:06 +00002399 spelling = TOKEN_NAME (token);
Kazu Hiratadf383482002-05-22 22:02:16 +00002400
Neil Booth3fef5b22000-05-08 22:22:49 +00002401 while ((c = *spelling++) != '\0')
2402 *buffer++ = c;
2403 }
2404 break;
2405
Zack Weinberg47ad4132001-10-06 23:11:27 +00002406 spell_ident:
Neil Booth5d7ee2f2000-05-10 09:39:18 +00002407 case SPELL_IDENT:
Geoffrey Keating47e20492005-03-12 10:44:06 +00002408 if (forstring)
2409 {
Joseph Myers9a0c6182009-05-10 15:27:32 +01002410 memcpy (buffer, NODE_NAME (token->val.node.node),
2411 NODE_LEN (token->val.node.node));
2412 buffer += NODE_LEN (token->val.node.node);
Geoffrey Keating47e20492005-03-12 10:44:06 +00002413 }
2414 else
2415 {
2416 size_t i;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002417 const unsigned char * name = NODE_NAME (token->val.node.node);
Geoffrey Keating47e20492005-03-12 10:44:06 +00002418
Joseph Myers9a0c6182009-05-10 15:27:32 +01002419 for (i = 0; i < NODE_LEN (token->val.node.node); i++)
Geoffrey Keating47e20492005-03-12 10:44:06 +00002420 if (name[i] & ~0x7F)
2421 {
2422 i += utf8_to_ucn (buffer, name + i) - 1;
2423 buffer += 10;
2424 }
2425 else
Joseph Myers9a0c6182009-05-10 15:27:32 +01002426 *buffer++ = NODE_NAME (token->val.node.node)[i];
Geoffrey Keating47e20492005-03-12 10:44:06 +00002427 }
Neil Booth5d7ee2f2000-05-10 09:39:18 +00002428 break;
Neil Booth3fef5b22000-05-08 22:22:49 +00002429
Neil Booth6338b352003-04-23 22:44:06 +00002430 case SPELL_LITERAL:
Zack Weinberg47ad4132001-10-06 23:11:27 +00002431 memcpy (buffer, token->val.str.text, token->val.str.len);
2432 buffer += token->val.str.len;
2433 break;
2434
Neil Booth3fef5b22000-05-08 22:22:49 +00002435 case SPELL_NONE:
John David Anglin0527bc42003-11-01 22:56:54 +00002436 cpp_error (pfile, CPP_DL_ICE,
2437 "unspellable token %s", TOKEN_NAME (token));
Neil Booth3fef5b22000-05-08 22:22:49 +00002438 break;
2439 }
2440
2441 return buffer;
2442}
2443
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002444/* Returns TOKEN spelt as a null-terminated string. The string is
2445 freed when the reader is destroyed. Useful for diagnostics. */
Neil Booth93c803682000-10-28 17:59:06 +00002446unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002447cpp_token_as_text (cpp_reader *pfile, const cpp_token *token)
Neil Booth59325652003-04-24 20:03:57 +00002448{
2449 unsigned int len = cpp_token_len (token) + 1;
Neil Boothece54d52001-09-28 09:40:22 +00002450 unsigned char *start = _cpp_unaligned_alloc (pfile, len), *end;
Zack Weinberg041c3192000-07-04 01:58:21 +00002451
Geoffrey Keating47e20492005-03-12 10:44:06 +00002452 end = cpp_spell_token (pfile, token, start, false);
Neil Booth93c803682000-10-28 17:59:06 +00002453 end[0] = '\0';
Zack Weinberg041c3192000-07-04 01:58:21 +00002454
Neil Booth93c803682000-10-28 17:59:06 +00002455 return start;
Zack Weinberg041c3192000-07-04 01:58:21 +00002456}
2457
Manuel López-Ibáñezcfc93532009-04-22 15:32:18 +00002458/* Returns a pointer to a string which spells the token defined by
2459 TYPE and FLAGS. Used by C front ends, which really should move to
2460 using cpp_token_as_text. */
Neil Booth93c803682000-10-28 17:59:06 +00002461const char *
Manuel López-Ibáñezcfc93532009-04-22 15:32:18 +00002462cpp_type2name (enum cpp_ttype type, unsigned char flags)
Zack Weinberg041c3192000-07-04 01:58:21 +00002463{
Manuel López-Ibáñezcfc93532009-04-22 15:32:18 +00002464 if (flags & DIGRAPH)
2465 return (const char *) cpp_digraph2name (type);
2466 else if (flags & NAMED_OP)
2467 return cpp_named_operator2name (type);
2468
Neil Booth93c803682000-10-28 17:59:06 +00002469 return (const char *) token_spellings[type].name;
Zack Weinberg041c3192000-07-04 01:58:21 +00002470}
2471
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002472/* Writes the spelling of token to FP, without any preceding space.
2473 Separated from cpp_spell_token for efficiency - to avoid stdio
2474 double-buffering. */
Zack Weinberg041c3192000-07-04 01:58:21 +00002475void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002476cpp_output_token (const cpp_token *token, FILE *fp)
Zack Weinberg041c3192000-07-04 01:58:21 +00002477{
Neil Booth93c803682000-10-28 17:59:06 +00002478 switch (TOKEN_SPELL (token))
Zack Weinberg041c3192000-07-04 01:58:21 +00002479 {
Neil Booth93c803682000-10-28 17:59:06 +00002480 case SPELL_OPERATOR:
2481 {
2482 const unsigned char *spelling;
Zack Weinberg3b681e92001-09-28 07:00:27 +00002483 int c;
Neil Booth93c803682000-10-28 17:59:06 +00002484
2485 if (token->flags & DIGRAPH)
Manuel López-Ibáñezcfc93532009-04-22 15:32:18 +00002486 spelling = cpp_digraph2name (token->type);
Neil Booth93c803682000-10-28 17:59:06 +00002487 else if (token->flags & NAMED_OP)
2488 goto spell_ident;
2489 else
2490 spelling = TOKEN_NAME (token);
2491
Zack Weinberg3b681e92001-09-28 07:00:27 +00002492 c = *spelling;
2493 do
2494 putc (c, fp);
2495 while ((c = *++spelling) != '\0');
Neil Booth93c803682000-10-28 17:59:06 +00002496 }
2497 break;
2498
2499 spell_ident:
2500 case SPELL_IDENT:
Geoffrey Keating47e20492005-03-12 10:44:06 +00002501 {
2502 size_t i;
Joseph Myers9a0c6182009-05-10 15:27:32 +01002503 const unsigned char * name = NODE_NAME (token->val.node.node);
Geoffrey Keating47e20492005-03-12 10:44:06 +00002504
Joseph Myers9a0c6182009-05-10 15:27:32 +01002505 for (i = 0; i < NODE_LEN (token->val.node.node); i++)
Geoffrey Keating47e20492005-03-12 10:44:06 +00002506 if (name[i] & ~0x7F)
2507 {
2508 unsigned char buffer[10];
2509 i += utf8_to_ucn (buffer, name + i) - 1;
2510 fwrite (buffer, 1, 10, fp);
2511 }
2512 else
Joseph Myers9a0c6182009-05-10 15:27:32 +01002513 fputc (NODE_NAME (token->val.node.node)[i], fp);
Geoffrey Keating47e20492005-03-12 10:44:06 +00002514 }
2515 break;
Neil Booth93c803682000-10-28 17:59:06 +00002516
Neil Booth6338b352003-04-23 22:44:06 +00002517 case SPELL_LITERAL:
Zack Weinberg47ad4132001-10-06 23:11:27 +00002518 fwrite (token->val.str.text, 1, token->val.str.len, fp);
2519 break;
2520
Neil Booth93c803682000-10-28 17:59:06 +00002521 case SPELL_NONE:
2522 /* An error, most probably. */
2523 break;
Zack Weinberg041c3192000-07-04 01:58:21 +00002524 }
Zack Weinberg041c3192000-07-04 01:58:21 +00002525}
2526
Neil Booth93c803682000-10-28 17:59:06 +00002527/* Compare two tokens. */
2528int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002529_cpp_equiv_tokens (const cpp_token *a, const cpp_token *b)
Neil Booth93c803682000-10-28 17:59:06 +00002530{
2531 if (a->type == b->type && a->flags == b->flags)
2532 switch (TOKEN_SPELL (a))
2533 {
2534 default: /* Keep compiler happy. */
2535 case SPELL_OPERATOR:
Joseph Myers9a0c6182009-05-10 15:27:32 +01002536 /* token_no is used to track where multiple consecutive ##
Joseph Myersaa508502009-04-19 18:10:56 +01002537 tokens were originally located. */
Joseph Myers9a0c6182009-05-10 15:27:32 +01002538 return (a->type != CPP_PASTE || a->val.token_no == b->val.token_no);
Neil Booth93c803682000-10-28 17:59:06 +00002539 case SPELL_NONE:
Joseph Myers9a0c6182009-05-10 15:27:32 +01002540 return (a->type != CPP_MACRO_ARG
2541 || a->val.macro_arg.arg_no == b->val.macro_arg.arg_no);
Neil Booth93c803682000-10-28 17:59:06 +00002542 case SPELL_IDENT:
Joseph Myers9a0c6182009-05-10 15:27:32 +01002543 return a->val.node.node == b->val.node.node;
Neil Booth6338b352003-04-23 22:44:06 +00002544 case SPELL_LITERAL:
Neil Booth93c803682000-10-28 17:59:06 +00002545 return (a->val.str.len == b->val.str.len
2546 && !memcmp (a->val.str.text, b->val.str.text,
2547 a->val.str.len));
2548 }
2549
2550 return 0;
2551}
2552
Neil Booth93c803682000-10-28 17:59:06 +00002553/* Returns nonzero if a space should be inserted to avoid an
2554 accidental token paste for output. For simplicity, it is
2555 conservative, and occasionally advises a space where one is not
2556 needed, e.g. "." and ".2". */
Neil Booth93c803682000-10-28 17:59:06 +00002557int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002558cpp_avoid_paste (cpp_reader *pfile, const cpp_token *token1,
2559 const cpp_token *token2)
Zack Weinberg041c3192000-07-04 01:58:21 +00002560{
Neil Booth93c803682000-10-28 17:59:06 +00002561 enum cpp_ttype a = token1->type, b = token2->type;
2562 cppchar_t c;
Zack Weinberg041c3192000-07-04 01:58:21 +00002563
Neil Booth93c803682000-10-28 17:59:06 +00002564 if (token1->flags & NAMED_OP)
2565 a = CPP_NAME;
2566 if (token2->flags & NAMED_OP)
2567 b = CPP_NAME;
Zack Weinberg041c3192000-07-04 01:58:21 +00002568
Neil Booth93c803682000-10-28 17:59:06 +00002569 c = EOF;
2570 if (token2->flags & DIGRAPH)
John David Anglin37b85242001-03-02 01:11:50 +00002571 c = digraph_spellings[(int) b - (int) CPP_FIRST_DIGRAPH][0];
Neil Booth93c803682000-10-28 17:59:06 +00002572 else if (token_spellings[b].category == SPELL_OPERATOR)
2573 c = token_spellings[b].name[0];
Zack Weinberg417f3e32000-07-11 23:20:53 +00002574
Neil Booth93c803682000-10-28 17:59:06 +00002575 /* Quickly get everything that can paste with an '='. */
John David Anglin37b85242001-03-02 01:11:50 +00002576 if ((int) a <= (int) CPP_LAST_EQ && c == '=')
Zack Weinberg041c3192000-07-04 01:58:21 +00002577 return 1;
2578
Neil Booth93c803682000-10-28 17:59:06 +00002579 switch (a)
2580 {
Steve Ellceyb52dbbf2006-08-14 23:13:54 +00002581 case CPP_GREATER: return c == '>';
2582 case CPP_LESS: return c == '<' || c == '%' || c == ':';
Neil Booth93c803682000-10-28 17:59:06 +00002583 case CPP_PLUS: return c == '+';
2584 case CPP_MINUS: return c == '-' || c == '>';
2585 case CPP_DIV: return c == '/' || c == '*'; /* Comments. */
2586 case CPP_MOD: return c == ':' || c == '>';
2587 case CPP_AND: return c == '&';
2588 case CPP_OR: return c == '|';
2589 case CPP_COLON: return c == ':' || c == '>';
2590 case CPP_DEREF: return c == '*';
Neil Booth26ec42e2001-01-28 11:22:23 +00002591 case CPP_DOT: return c == '.' || c == '%' || b == CPP_NUMBER;
Neil Booth93c803682000-10-28 17:59:06 +00002592 case CPP_HASH: return c == '#' || c == '%'; /* Digraph form. */
2593 case CPP_NAME: return ((b == CPP_NUMBER
2594 && name_p (pfile, &token2->val.str))
2595 || b == CPP_NAME
2596 || b == CPP_CHAR || b == CPP_STRING); /* L */
2597 case CPP_NUMBER: return (b == CPP_NUMBER || b == CPP_NAME
2598 || c == '.' || c == '+' || c == '-');
Neil Booth1613e522003-04-20 07:29:23 +00002599 /* UCNs */
Neil Booth10676942003-04-22 19:28:00 +00002600 case CPP_OTHER: return ((token1->val.str.text[0] == '\\'
2601 && b == CPP_NAME)
Neil Booth1613e522003-04-20 07:29:23 +00002602 || (CPP_OPTION (pfile, objc)
Neil Booth10676942003-04-22 19:28:00 +00002603 && token1->val.str.text[0] == '@'
Neil Booth1613e522003-04-20 07:29:23 +00002604 && (b == CPP_NAME || b == CPP_STRING)));
Neil Booth93c803682000-10-28 17:59:06 +00002605 default: break;
2606 }
Zack Weinberg041c3192000-07-04 01:58:21 +00002607
2608 return 0;
2609}
2610
Neil Booth93c803682000-10-28 17:59:06 +00002611/* Output all the remaining tokens on the current line, and a newline
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002612 character, to FP. Leading whitespace is removed. If there are
2613 macros, special token padding is not performed. */
Neil Booth93c803682000-10-28 17:59:06 +00002614void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002615cpp_output_line (cpp_reader *pfile, FILE *fp)
Zack Weinberg041c3192000-07-04 01:58:21 +00002616{
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002617 const cpp_token *token;
Zack Weinberg041c3192000-07-04 01:58:21 +00002618
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002619 token = cpp_get_token (pfile);
2620 while (token->type != CPP_EOF)
Zack Weinberg6ead1e92000-07-31 23:47:19 +00002621 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +00002622 cpp_output_token (token, fp);
2623 token = cpp_get_token (pfile);
2624 if (token->flags & PREV_WHITE)
2625 putc (' ', fp);
Zack Weinberg6ead1e92000-07-31 23:47:19 +00002626 }
2627
Neil Booth93c803682000-10-28 17:59:06 +00002628 putc ('\n', fp);
Zack Weinberg041c3192000-07-04 01:58:21 +00002629}
2630
Tom Tromey5d6342e2008-05-21 21:52:57 +00002631/* Return a string representation of all the remaining tokens on the
2632 current line. The result is allocated using xmalloc and must be
2633 freed by the caller. */
2634unsigned char *
2635cpp_output_line_to_string (cpp_reader *pfile, const unsigned char *dir_name)
2636{
2637 const cpp_token *token;
2638 unsigned int out = dir_name ? ustrlen (dir_name) : 0;
2639 unsigned int alloced = 120 + out;
2640 unsigned char *result = (unsigned char *) xmalloc (alloced);
2641
2642 /* If DIR_NAME is empty, there are no initial contents. */
2643 if (dir_name)
2644 {
2645 sprintf ((char *) result, "#%s ", dir_name);
2646 out += 2;
2647 }
2648
2649 token = cpp_get_token (pfile);
2650 while (token->type != CPP_EOF)
2651 {
2652 unsigned char *last;
2653 /* Include room for a possible space and the terminating nul. */
2654 unsigned int len = cpp_token_len (token) + 2;
2655
2656 if (out + len > alloced)
2657 {
2658 alloced *= 2;
2659 if (out + len > alloced)
2660 alloced = out + len;
2661 result = (unsigned char *) xrealloc (result, alloced);
2662 }
2663
2664 last = cpp_spell_token (pfile, token, &result[out], 0);
2665 out = last - result;
2666
2667 token = cpp_get_token (pfile);
2668 if (token->flags & PREV_WHITE)
2669 result[out++] = ' ';
2670 }
2671
2672 result[out] = '\0';
2673 return result;
2674}
2675
Neil Booth1e013d22001-09-26 21:44:35 +00002676/* Memory buffers. Changing these three constants can have a dramatic
2677 effect on performance. The values here are reasonable defaults,
2678 but might be tuned. If you adjust them, be sure to test across a
2679 range of uses of cpplib, including heavy nested function-like macro
2680 expansion. Also check the change in peak memory usage (NJAMD is a
2681 good tool for this). */
2682#define MIN_BUFF_SIZE 8000
Neil Booth87062812001-10-20 09:00:53 +00002683#define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2)
Neil Booth1e013d22001-09-26 21:44:35 +00002684#define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \
2685 (MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2)
Neil Booth93c803682000-10-28 17:59:06 +00002686
Neil Booth87062812001-10-20 09:00:53 +00002687#if MIN_BUFF_SIZE > BUFF_SIZE_UPPER_BOUND (0)
2688 #error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE!
2689#endif
2690
Neil Boothc9e7a602001-09-27 12:59:38 +00002691/* Create a new allocation buffer. Place the control block at the end
2692 of the buffer, so that buffer overflows will cause immediate chaos. */
Neil Boothb8af0ca2001-09-26 17:52:50 +00002693static _cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002694new_buff (size_t len)
Neil Boothb8af0ca2001-09-26 17:52:50 +00002695{
2696 _cpp_buff *result;
Neil Boothece54d52001-09-28 09:40:22 +00002697 unsigned char *base;
Neil Boothb8af0ca2001-09-26 17:52:50 +00002698
Neil Booth1e013d22001-09-26 21:44:35 +00002699 if (len < MIN_BUFF_SIZE)
2700 len = MIN_BUFF_SIZE;
Neil Boothc70f6ed2002-06-07 06:26:32 +00002701 len = CPP_ALIGN (len);
Neil Boothb8af0ca2001-09-26 17:52:50 +00002702
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00002703 base = XNEWVEC (unsigned char, len + sizeof (_cpp_buff));
Neil Boothb8af0ca2001-09-26 17:52:50 +00002704 result = (_cpp_buff *) (base + len);
2705 result->base = base;
2706 result->cur = base;
2707 result->limit = base + len;
2708 result->next = NULL;
2709 return result;
2710}
2711
2712/* Place a chain of unwanted allocation buffers on the free list. */
2713void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002714_cpp_release_buff (cpp_reader *pfile, _cpp_buff *buff)
Neil Boothb8af0ca2001-09-26 17:52:50 +00002715{
2716 _cpp_buff *end = buff;
2717
2718 while (end->next)
2719 end = end->next;
2720 end->next = pfile->free_buffs;
2721 pfile->free_buffs = buff;
2722}
2723
2724/* Return a free buffer of size at least MIN_SIZE. */
2725_cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002726_cpp_get_buff (cpp_reader *pfile, size_t min_size)
Neil Boothb8af0ca2001-09-26 17:52:50 +00002727{
2728 _cpp_buff *result, **p;
2729
2730 for (p = &pfile->free_buffs;; p = &(*p)->next)
2731 {
Neil Booth61420882001-09-28 13:25:38 +00002732 size_t size;
Neil Booth1e013d22001-09-26 21:44:35 +00002733
2734 if (*p == NULL)
Neil Boothb8af0ca2001-09-26 17:52:50 +00002735 return new_buff (min_size);
Neil Booth1e013d22001-09-26 21:44:35 +00002736 result = *p;
2737 size = result->limit - result->base;
2738 /* Return a buffer that's big enough, but don't waste one that's
2739 way too big. */
Richard Earnshaw34f52712001-10-17 16:20:04 +00002740 if (size >= min_size && size <= BUFF_SIZE_UPPER_BOUND (min_size))
Neil Boothb8af0ca2001-09-26 17:52:50 +00002741 break;
2742 }
2743
2744 *p = result->next;
2745 result->next = NULL;
2746 result->cur = result->base;
2747 return result;
2748}
2749
Kazu Hirata4fe9b912001-10-09 06:03:16 +00002750/* Creates a new buffer with enough space to hold the uncommitted
Neil Booth8c3b2692001-09-30 10:03:11 +00002751 remaining bytes of BUFF, and at least MIN_EXTRA more bytes. Copies
2752 the excess bytes to the new buffer. Chains the new buffer after
2753 BUFF, and returns the new buffer. */
Neil Boothb8af0ca2001-09-26 17:52:50 +00002754_cpp_buff *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002755_cpp_append_extend_buff (cpp_reader *pfile, _cpp_buff *buff, size_t min_extra)
Neil Boothb8af0ca2001-09-26 17:52:50 +00002756{
Neil Booth61420882001-09-28 13:25:38 +00002757 size_t size = EXTENDED_BUFF_SIZE (buff, min_extra);
Neil Booth8c3b2692001-09-30 10:03:11 +00002758 _cpp_buff *new_buff = _cpp_get_buff (pfile, size);
Neil Boothb8af0ca2001-09-26 17:52:50 +00002759
Neil Booth8c3b2692001-09-30 10:03:11 +00002760 buff->next = new_buff;
2761 memcpy (new_buff->base, buff->cur, BUFF_ROOM (buff));
2762 return new_buff;
2763}
2764
Kazu Hirata4fe9b912001-10-09 06:03:16 +00002765/* Creates a new buffer with enough space to hold the uncommitted
Neil Booth8c3b2692001-09-30 10:03:11 +00002766 remaining bytes of the buffer pointed to by BUFF, and at least
2767 MIN_EXTRA more bytes. Copies the excess bytes to the new buffer.
2768 Chains the new buffer before the buffer pointed to by BUFF, and
2769 updates the pointer to point to the new buffer. */
2770void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002771_cpp_extend_buff (cpp_reader *pfile, _cpp_buff **pbuff, size_t min_extra)
Neil Booth8c3b2692001-09-30 10:03:11 +00002772{
2773 _cpp_buff *new_buff, *old_buff = *pbuff;
2774 size_t size = EXTENDED_BUFF_SIZE (old_buff, min_extra);
2775
2776 new_buff = _cpp_get_buff (pfile, size);
2777 memcpy (new_buff->base, old_buff->cur, BUFF_ROOM (old_buff));
2778 new_buff->next = old_buff;
2779 *pbuff = new_buff;
Neil Boothb8af0ca2001-09-26 17:52:50 +00002780}
2781
2782/* Free a chain of buffers starting at BUFF. */
2783void
Andreas Jaeger5671bf22003-07-07 21:11:59 +02002784_cpp_free_buff (_cpp_buff *buff)
Neil Boothb8af0ca2001-09-26 17:52:50 +00002785{
2786 _cpp_buff *next;
2787
2788 for (; buff; buff = next)
2789 {
2790 next = buff->next;
2791 free (buff->base);
2792 }
2793}
Neil Booth93c803682000-10-28 17:59:06 +00002794
Neil Boothece54d52001-09-28 09:40:22 +00002795/* Allocate permanent, unaligned storage of length LEN. */
2796unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002797_cpp_unaligned_alloc (cpp_reader *pfile, size_t len)
Neil Boothece54d52001-09-28 09:40:22 +00002798{
2799 _cpp_buff *buff = pfile->u_buff;
2800 unsigned char *result = buff->cur;
2801
2802 if (len > (size_t) (buff->limit - result))
2803 {
2804 buff = _cpp_get_buff (pfile, len);
2805 buff->next = pfile->u_buff;
2806 pfile->u_buff = buff;
2807 result = buff->cur;
2808 }
2809
2810 buff->cur = result + len;
2811 return result;
2812}
2813
Neil Booth87062812001-10-20 09:00:53 +00002814/* Allocate permanent, unaligned storage of length LEN from a_buff.
2815 That buffer is used for growing allocations when saving macro
2816 replacement lists in a #define, and when parsing an answer to an
2817 assertion in #assert, #unassert or #if (and therefore possibly
2818 whilst expanding macros). It therefore must not be used by any
2819 code that they might call: specifically the lexer and the guts of
2820 the macro expander.
2821
2822 All existing other uses clearly fit this restriction: storing
2823 registered pragmas during initialization. */
Neil Booth93c803682000-10-28 17:59:06 +00002824unsigned char *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00002825_cpp_aligned_alloc (cpp_reader *pfile, size_t len)
Neil Booth93c803682000-10-28 17:59:06 +00002826{
Neil Booth8c3b2692001-09-30 10:03:11 +00002827 _cpp_buff *buff = pfile->a_buff;
2828 unsigned char *result = buff->cur;
Neil Booth93c803682000-10-28 17:59:06 +00002829
Neil Booth8c3b2692001-09-30 10:03:11 +00002830 if (len > (size_t) (buff->limit - result))
Zack Weinberg041c3192000-07-04 01:58:21 +00002831 {
Neil Booth8c3b2692001-09-30 10:03:11 +00002832 buff = _cpp_get_buff (pfile, len);
2833 buff->next = pfile->a_buff;
2834 pfile->a_buff = buff;
2835 result = buff->cur;
Zack Weinberg041c3192000-07-04 01:58:21 +00002836 }
2837
Neil Booth8c3b2692001-09-30 10:03:11 +00002838 buff->cur = result + len;
Neil Booth93c803682000-10-28 17:59:06 +00002839 return result;
Zack Weinberg041c3192000-07-04 01:58:21 +00002840}
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002841
2842/* Say which field of TOK is in use. */
2843
2844enum cpp_token_fld_kind
2845cpp_token_val_index (cpp_token *tok)
2846{
2847 switch (TOKEN_SPELL (tok))
2848 {
2849 case SPELL_IDENT:
2850 return CPP_TOKEN_FLD_NODE;
2851 case SPELL_LITERAL:
2852 return CPP_TOKEN_FLD_STR;
Joseph Myersaa508502009-04-19 18:10:56 +01002853 case SPELL_OPERATOR:
2854 if (tok->type == CPP_PASTE)
Joseph Myers9a0c6182009-05-10 15:27:32 +01002855 return CPP_TOKEN_FLD_TOKEN_NO;
Joseph Myersaa508502009-04-19 18:10:56 +01002856 else
2857 return CPP_TOKEN_FLD_NONE;
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002858 case SPELL_NONE:
2859 if (tok->type == CPP_MACRO_ARG)
2860 return CPP_TOKEN_FLD_ARG_NO;
2861 else if (tok->type == CPP_PADDING)
2862 return CPP_TOKEN_FLD_SOURCE;
Zack Weinberg21b11492004-09-09 19:16:56 +00002863 else if (tok->type == CPP_PRAGMA)
Richard Hendersonbc4071d2006-01-04 08:33:38 -08002864 return CPP_TOKEN_FLD_PRAGMA;
Geoffrey Keatingd8044162004-06-09 20:10:13 +00002865 /* else fall through */
2866 default:
2867 return CPP_TOKEN_FLD_NONE;
2868 }
2869}
Gabriel Charettee3dfef42011-08-22 20:41:07 +00002870
2871/* All tokens lexed in R after calling this function will be forced to have
2872 their source_location the same as the location referenced by P, until
2873 cpp_stop_forcing_token_locations is called for R. */
2874
2875void
2876cpp_force_token_locations (cpp_reader *r, source_location *p)
2877{
2878 r->forced_token_location_p = p;
2879}
2880
2881/* Go back to assigning locations naturally for lexed tokens. */
2882
2883void
2884cpp_stop_forcing_token_locations (cpp_reader *r)
2885{
2886 r->forced_token_location_p = NULL;
2887}