comparison libcpp/expr.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* Parse C expressions for cpplib. 1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001, 2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3 2002, 2004, 2008, 2009, 2010 Free Software Foundation.
4 Contributed by Per Bothner, 1994. 3 Contributed by Per Bothner, 1994.
5 4
6 This program is free software; you can redistribute it and/or modify it 5 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the 6 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any 7 Free Software Foundation; either version 3, or (at your option) any
57 static cpp_num num_lshift (cpp_num, size_t, size_t); 56 static cpp_num num_lshift (cpp_num, size_t, size_t);
58 static cpp_num num_rshift (cpp_num, size_t, size_t); 57 static cpp_num num_rshift (cpp_num, size_t, size_t);
59 58
60 static cpp_num append_digit (cpp_num, int, int, size_t); 59 static cpp_num append_digit (cpp_num, int, int, size_t);
61 static cpp_num parse_defined (cpp_reader *); 60 static cpp_num parse_defined (cpp_reader *);
62 static cpp_num eval_token (cpp_reader *, const cpp_token *); 61 static cpp_num eval_token (cpp_reader *, const cpp_token *, source_location);
63 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype); 62 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
64 static unsigned int interpret_float_suffix (const uchar *, size_t); 63 static unsigned int interpret_float_suffix (cpp_reader *, const uchar *, size_t);
65 static unsigned int interpret_int_suffix (const uchar *, size_t); 64 static unsigned int interpret_int_suffix (cpp_reader *, const uchar *, size_t);
66 static void check_promotion (cpp_reader *, const struct op *); 65 static void check_promotion (cpp_reader *, const struct op *);
66
67 static cpp_num parse_has_include (cpp_reader *, enum include_type);
67 68
68 /* Token type abuse to create unary plus and minus operators. */ 69 /* Token type abuse to create unary plus and minus operators. */
69 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1)) 70 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
70 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2)) 71 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
71 72
74 #define SYNTAX_ERROR(msgid) \ 75 #define SYNTAX_ERROR(msgid) \
75 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0) 76 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
76 #define SYNTAX_ERROR2(msgid, arg) \ 77 #define SYNTAX_ERROR2(msgid, arg) \
77 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \ 78 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
78 while(0) 79 while(0)
80 #define SYNTAX_ERROR_AT(loc, msgid) \
81 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
82 while(0)
83 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
84 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
85 while(0)
79 86
80 /* Subroutine of cpp_classify_number. S points to a float suffix of 87 /* Subroutine of cpp_classify_number. S points to a float suffix of
81 length LEN, possibly zero. Returns 0 for an invalid suffix, or a 88 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
82 flag vector describing the suffix. */ 89 flag vector (of CPP_N_* bits) describing the suffix. */
83 static unsigned int 90 static unsigned int
84 interpret_float_suffix (const uchar *s, size_t len) 91 interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len)
85 { 92 {
86 size_t flags; 93 size_t flags;
87 size_t f, d, l, w, q, i; 94 size_t f, d, l, w, q, i, fn, fnx, fn_bits;
88 95
89 flags = 0; 96 flags = 0;
90 f = d = l = w = q = i = 0; 97 f = d = l = w = q = i = fn = fnx = fn_bits = 0;
98
99 /* The following decimal float suffixes, from TR 24732:2009 and TS
100 18661-2:2015, are supported:
101
102 df, DF - _Decimal32.
103 dd, DD - _Decimal64.
104 dl, DL - _Decimal128.
105
106 The dN and DN suffixes for _DecimalN, and dNx and DNx for
107 _DecimalNx, defined in TS 18661-3:2015, are not supported.
108
109 Fixed-point suffixes, from TR 18037:2008, are supported. They
110 consist of three parts, in order:
111
112 (i) An optional u or U, for unsigned types.
113
114 (ii) An optional h or H, for short types, or l or L, for long
115 types, or ll or LL, for long long types. Use of ll or LL is a
116 GNU extension.
117
118 (iii) r or R, for _Fract types, or k or K, for _Accum types.
119
120 Otherwise the suffix is for a binary or standard floating-point
121 type. Such a suffix, or the absence of a suffix, may be preceded
122 or followed by i, I, j or J, to indicate an imaginary number with
123 the corresponding complex type. The following suffixes for
124 binary or standard floating-point types are supported:
125
126 f, F - float (ISO C and C++).
127 l, L - long double (ISO C and C++).
128 d, D - double, even with the FLOAT_CONST_DECIMAL64 pragma in
129 operation (from TR 24732:2009; the pragma and the suffix
130 are not included in TS 18661-2:2015).
131 w, W - machine-specific type such as __float80 (GNU extension).
132 q, Q - machine-specific type such as __float128 (GNU extension).
133 fN, FN - _FloatN (TS 18661-3:2015).
134 fNx, FNx - _FloatNx (TS 18661-3:2015). */
91 135
92 /* Process decimal float suffixes, which are two letters starting 136 /* Process decimal float suffixes, which are two letters starting
93 with d or D. Order and case are significant. */ 137 with d or D. Order and case are significant. */
94 if (len == 2 && (*s == 'd' || *s == 'D')) 138 if (len == 2 && (*s == 'd' || *s == 'D'))
95 { 139 {
107 for decimal float constants. */ 151 for decimal float constants. */
108 break; 152 break;
109 } 153 }
110 } 154 }
111 155
112 /* Recognize a fixed-point suffix. */ 156 if (CPP_OPTION (pfile, ext_numeric_literals))
113 switch (s[len-1]) 157 {
114 { 158 /* Recognize a fixed-point suffix. */
115 case 'k': case 'K': flags = CPP_N_ACCUM; break; 159 if (len != 0)
116 case 'r': case 'R': flags = CPP_N_FRACT; break; 160 switch (s[len-1])
117 default: break; 161 {
118 } 162 case 'k': case 'K': flags = CPP_N_ACCUM; break;
119 163 case 'r': case 'R': flags = CPP_N_FRACT; break;
120 /* Continue processing a fixed-point suffix. The suffix is case 164 default: break;
121 insensitive except for ll or LL. Order is significant. */ 165 }
122 if (flags) 166
123 { 167 /* Continue processing a fixed-point suffix. The suffix is case
124 if (len == 1) 168 insensitive except for ll or LL. Order is significant. */
125 return flags; 169 if (flags)
126 len--; 170 {
127
128 if (*s == 'u' || *s == 'U')
129 {
130 flags |= CPP_N_UNSIGNED;
131 if (len == 1) 171 if (len == 1)
132 return flags; 172 return flags;
133 len--; 173 len--;
134 s++; 174
135 } 175 if (*s == 'u' || *s == 'U')
136 176 {
137 switch (*s) 177 flags |= CPP_N_UNSIGNED;
138 { 178 if (len == 1)
139 case 'h': case 'H': 179 return flags;
140 if (len == 1) 180 len--;
141 return flags |= CPP_N_SMALL; 181 s++;
142 break; 182 }
143 case 'l': 183
144 if (len == 1) 184 switch (*s)
145 return flags |= CPP_N_MEDIUM; 185 {
146 if (len == 2 && s[1] == 'l') 186 case 'h': case 'H':
147 return flags |= CPP_N_LARGE; 187 if (len == 1)
148 break; 188 return flags |= CPP_N_SMALL;
149 case 'L': 189 break;
150 if (len == 1) 190 case 'l':
151 return flags |= CPP_N_MEDIUM; 191 if (len == 1)
152 if (len == 2 && s[1] == 'L') 192 return flags |= CPP_N_MEDIUM;
153 return flags |= CPP_N_LARGE; 193 if (len == 2 && s[1] == 'l')
154 break; 194 return flags |= CPP_N_LARGE;
155 default: 195 break;
156 break; 196 case 'L':
157 } 197 if (len == 1)
158 /* Anything left at this point is invalid. */ 198 return flags |= CPP_N_MEDIUM;
159 return 0; 199 if (len == 2 && s[1] == 'L')
200 return flags |= CPP_N_LARGE;
201 break;
202 default:
203 break;
204 }
205 /* Anything left at this point is invalid. */
206 return 0;
207 }
160 } 208 }
161 209
162 /* In any remaining valid suffix, the case and order don't matter. */ 210 /* In any remaining valid suffix, the case and order don't matter. */
163 while (len--) 211 while (len--)
164 switch (s[len]) 212 {
165 { 213 switch (s[0])
166 case 'f': case 'F': f++; break; 214 {
167 case 'd': case 'D': d++; break; 215 case 'f': case 'F':
168 case 'l': case 'L': l++; break; 216 f++;
169 case 'w': case 'W': w++; break; 217 if (len > 0
170 case 'q': case 'Q': q++; break; 218 && !CPP_OPTION (pfile, cplusplus)
171 case 'i': case 'I': 219 && s[1] >= '1'
172 case 'j': case 'J': i++; break; 220 && s[1] <= '9'
173 default: 221 && fn_bits == 0)
174 return 0; 222 {
175 } 223 f--;
176 224 while (len > 0
177 if (f + d + l + w + q > 1 || i > 1) 225 && s[1] >= '0'
226 && s[1] <= '9'
227 && fn_bits < CPP_FLOATN_MAX)
228 {
229 fn_bits = fn_bits * 10 + (s[1] - '0');
230 len--;
231 s++;
232 }
233 if (len > 0 && s[1] == 'x')
234 {
235 fnx++;
236 len--;
237 s++;
238 }
239 else
240 fn++;
241 }
242 break;
243 case 'd': case 'D': d++; break;
244 case 'l': case 'L': l++; break;
245 case 'w': case 'W': w++; break;
246 case 'q': case 'Q': q++; break;
247 case 'i': case 'I':
248 case 'j': case 'J': i++; break;
249 default:
250 return 0;
251 }
252 s++;
253 }
254
255 /* Reject any case of multiple suffixes specifying types, multiple
256 suffixes specifying an imaginary constant, _FloatN or _FloatNx
257 suffixes for invalid values of N, and _FloatN suffixes for values
258 of N larger than can be represented in the return value. The
259 caller is responsible for rejecting _FloatN suffixes where
260 _FloatN is not supported on the chosen target. */
261 if (f + d + l + w + q + fn + fnx > 1 || i > 1)
262 return 0;
263 if (fn_bits > CPP_FLOATN_MAX)
264 return 0;
265 if (fnx && fn_bits != 32 && fn_bits != 64 && fn_bits != 128)
266 return 0;
267 if (fn && fn_bits != 16 && fn_bits % 32 != 0)
268 return 0;
269 if (fn && fn_bits == 96)
270 return 0;
271
272 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
273 return 0;
274
275 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
178 return 0; 276 return 0;
179 277
180 return ((i ? CPP_N_IMAGINARY : 0) 278 return ((i ? CPP_N_IMAGINARY : 0)
181 | (f ? CPP_N_SMALL : 279 | (f ? CPP_N_SMALL :
182 d ? CPP_N_MEDIUM : 280 d ? CPP_N_MEDIUM :
183 l ? CPP_N_LARGE : 281 l ? CPP_N_LARGE :
184 w ? CPP_N_MD_W : 282 w ? CPP_N_MD_W :
185 q ? CPP_N_MD_Q : CPP_N_DEFAULT)); 283 q ? CPP_N_MD_Q :
284 fn ? CPP_N_FLOATN | (fn_bits << CPP_FLOATN_SHIFT) :
285 fnx ? CPP_N_FLOATNX | (fn_bits << CPP_FLOATN_SHIFT) :
286 CPP_N_DEFAULT));
287 }
288
289 /* Return the classification flags for a float suffix. */
290 unsigned int
291 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
292 {
293 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
186 } 294 }
187 295
188 /* Subroutine of cpp_classify_number. S points to an integer suffix 296 /* Subroutine of cpp_classify_number. S points to an integer suffix
189 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a 297 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
190 flag vector describing the suffix. */ 298 flag vector describing the suffix. */
191 static unsigned int 299 static unsigned int
192 interpret_int_suffix (const uchar *s, size_t len) 300 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
193 { 301 {
194 size_t u, l, i; 302 size_t u, l, i;
195 303
196 u = l = i = 0; 304 u = l = i = 0;
197 305
211 } 319 }
212 320
213 if (l > 2 || u > 1 || i > 1) 321 if (l > 2 || u > 1 || i > 1)
214 return 0; 322 return 0;
215 323
324 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
325 return 0;
326
216 return ((i ? CPP_N_IMAGINARY : 0) 327 return ((i ? CPP_N_IMAGINARY : 0)
217 | (u ? CPP_N_UNSIGNED : 0) 328 | (u ? CPP_N_UNSIGNED : 0)
218 | ((l == 0) ? CPP_N_SMALL 329 | ((l == 0) ? CPP_N_SMALL
219 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE)); 330 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
220 } 331 }
221 332
333 /* Return the classification flags for an int suffix. */
334 unsigned int
335 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
336 {
337 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
338 }
339
340 /* Return the string type corresponding to the the input user-defined string
341 literal type. If the input type is not a user-defined string literal
342 type return the input type. */
343 enum cpp_ttype
344 cpp_userdef_string_remove_type (enum cpp_ttype type)
345 {
346 if (type == CPP_STRING_USERDEF)
347 return CPP_STRING;
348 else if (type == CPP_WSTRING_USERDEF)
349 return CPP_WSTRING;
350 else if (type == CPP_STRING16_USERDEF)
351 return CPP_STRING16;
352 else if (type == CPP_STRING32_USERDEF)
353 return CPP_STRING32;
354 else if (type == CPP_UTF8STRING_USERDEF)
355 return CPP_UTF8STRING;
356 else
357 return type;
358 }
359
360 /* Return the user-defined string literal type corresponding to the input
361 string type. If the input type is not a string type return the input
362 type. */
363 enum cpp_ttype
364 cpp_userdef_string_add_type (enum cpp_ttype type)
365 {
366 if (type == CPP_STRING)
367 return CPP_STRING_USERDEF;
368 else if (type == CPP_WSTRING)
369 return CPP_WSTRING_USERDEF;
370 else if (type == CPP_STRING16)
371 return CPP_STRING16_USERDEF;
372 else if (type == CPP_STRING32)
373 return CPP_STRING32_USERDEF;
374 else if (type == CPP_UTF8STRING)
375 return CPP_UTF8STRING_USERDEF;
376 else
377 return type;
378 }
379
380 /* Return the char type corresponding to the the input user-defined char
381 literal type. If the input type is not a user-defined char literal
382 type return the input type. */
383 enum cpp_ttype
384 cpp_userdef_char_remove_type (enum cpp_ttype type)
385 {
386 if (type == CPP_CHAR_USERDEF)
387 return CPP_CHAR;
388 else if (type == CPP_WCHAR_USERDEF)
389 return CPP_WCHAR;
390 else if (type == CPP_CHAR16_USERDEF)
391 return CPP_CHAR16;
392 else if (type == CPP_CHAR32_USERDEF)
393 return CPP_CHAR32;
394 else if (type == CPP_UTF8CHAR_USERDEF)
395 return CPP_UTF8CHAR;
396 else
397 return type;
398 }
399
400 /* Return the user-defined char literal type corresponding to the input
401 char type. If the input type is not a char type return the input
402 type. */
403 enum cpp_ttype
404 cpp_userdef_char_add_type (enum cpp_ttype type)
405 {
406 if (type == CPP_CHAR)
407 return CPP_CHAR_USERDEF;
408 else if (type == CPP_WCHAR)
409 return CPP_WCHAR_USERDEF;
410 else if (type == CPP_CHAR16)
411 return CPP_CHAR16_USERDEF;
412 else if (type == CPP_CHAR32)
413 return CPP_CHAR32_USERDEF;
414 else if (type == CPP_UTF8CHAR)
415 return CPP_UTF8CHAR_USERDEF;
416 else
417 return type;
418 }
419
420 /* Return true if the token type is a user-defined string literal. */
421 bool
422 cpp_userdef_string_p (enum cpp_ttype type)
423 {
424 if (type == CPP_STRING_USERDEF
425 || type == CPP_WSTRING_USERDEF
426 || type == CPP_STRING16_USERDEF
427 || type == CPP_STRING32_USERDEF
428 || type == CPP_UTF8STRING_USERDEF)
429 return true;
430 else
431 return false;
432 }
433
434 /* Return true if the token type is a user-defined char literal. */
435 bool
436 cpp_userdef_char_p (enum cpp_ttype type)
437 {
438 if (type == CPP_CHAR_USERDEF
439 || type == CPP_WCHAR_USERDEF
440 || type == CPP_CHAR16_USERDEF
441 || type == CPP_CHAR32_USERDEF
442 || type == CPP_UTF8CHAR_USERDEF)
443 return true;
444 else
445 return false;
446 }
447
448 /* Extract the suffix from a user-defined literal string or char. */
449 const char *
450 cpp_get_userdef_suffix (const cpp_token *tok)
451 {
452 unsigned int len = tok->val.str.len;
453 const char *text = (const char *)tok->val.str.text;
454 char delim;
455 unsigned int i;
456 for (i = 0; i < len; ++i)
457 if (text[i] == '\'' || text[i] == '"')
458 break;
459 if (i == len)
460 return text + len;
461 delim = text[i];
462 for (i = len; i > 0; --i)
463 if (text[i - 1] == delim)
464 break;
465 return text + i;
466 }
467
222 /* Categorize numeric constants according to their field (integer, 468 /* Categorize numeric constants according to their field (integer,
223 floating point, or invalid), radix (decimal, octal, hexadecimal), 469 floating point, or invalid), radix (decimal, octal, hexadecimal),
224 and type suffixes. */ 470 and type suffixes.
471
472 TOKEN is the token that represents the numeric constant to
473 classify.
474
475 In C++0X if UD_SUFFIX is non null it will be assigned
476 any unrecognized suffix for a user-defined literal.
477
478 VIRTUAL_LOCATION is the virtual location for TOKEN. */
225 unsigned int 479 unsigned int
226 cpp_classify_number (cpp_reader *pfile, const cpp_token *token) 480 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
481 const char **ud_suffix, source_location virtual_location)
227 { 482 {
228 const uchar *str = token->val.str.text; 483 const uchar *str = token->val.str.text;
229 const uchar *limit; 484 const uchar *limit;
230 unsigned int max_digit, result, radix; 485 unsigned int max_digit, result, radix;
231 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag; 486 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
232 bool seen_digit; 487 bool seen_digit;
488 bool seen_digit_sep;
489
490 if (ud_suffix)
491 *ud_suffix = NULL;
233 492
234 /* If the lexer has done its job, length one can only be a single 493 /* If the lexer has done its job, length one can only be a single
235 digit. Fast-path this very common case. */ 494 digit. Fast-path this very common case. */
236 if (token->val.str.len == 1) 495 if (token->val.str.len == 1)
237 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL; 496 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
239 limit = str + token->val.str.len; 498 limit = str + token->val.str.len;
240 float_flag = NOT_FLOAT; 499 float_flag = NOT_FLOAT;
241 max_digit = 0; 500 max_digit = 0;
242 radix = 10; 501 radix = 10;
243 seen_digit = false; 502 seen_digit = false;
503 seen_digit_sep = false;
244 504
245 /* First, interpret the radix. */ 505 /* First, interpret the radix. */
246 if (*str == '0') 506 if (*str == '0')
247 { 507 {
248 radix = 8; 508 radix = 8;
249 str++; 509 str++;
250 510
251 /* Require at least one hex digit to classify it as hex. */ 511 /* Require at least one hex digit to classify it as hex. */
252 if ((*str == 'x' || *str == 'X') 512 if (*str == 'x' || *str == 'X')
253 && (str[1] == '.' || ISXDIGIT (str[1]))) 513 {
254 { 514 if (str[1] == '.' || ISXDIGIT (str[1]))
255 radix = 16; 515 {
256 str++; 516 radix = 16;
257 } 517 str++;
258 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1')) 518 }
259 { 519 else if (DIGIT_SEP (str[1]))
260 radix = 2; 520 SYNTAX_ERROR_AT (virtual_location,
261 str++; 521 "digit separator after base indicator");
522 }
523 else if (*str == 'b' || *str == 'B')
524 {
525 if (str[1] == '0' || str[1] == '1')
526 {
527 radix = 2;
528 str++;
529 }
530 else if (DIGIT_SEP (str[1]))
531 SYNTAX_ERROR_AT (virtual_location,
532 "digit separator after base indicator");
262 } 533 }
263 } 534 }
264 535
265 /* Now scan for a well-formed integer or float. */ 536 /* Now scan for a well-formed integer or float. */
266 for (;;) 537 for (;;)
267 { 538 {
268 unsigned int c = *str++; 539 unsigned int c = *str++;
269 540
270 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16)) 541 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
271 { 542 {
543 seen_digit_sep = false;
272 seen_digit = true; 544 seen_digit = true;
273 c = hex_value (c); 545 c = hex_value (c);
274 if (c > max_digit) 546 if (c > max_digit)
275 max_digit = c; 547 max_digit = c;
276 } 548 }
549 else if (DIGIT_SEP (c))
550 {
551 if (seen_digit_sep)
552 SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators");
553 seen_digit_sep = true;
554 }
277 else if (c == '.') 555 else if (c == '.')
278 { 556 {
557 if (seen_digit_sep || DIGIT_SEP (*str))
558 SYNTAX_ERROR_AT (virtual_location,
559 "digit separator adjacent to decimal point");
560 seen_digit_sep = false;
279 if (float_flag == NOT_FLOAT) 561 if (float_flag == NOT_FLOAT)
280 float_flag = AFTER_POINT; 562 float_flag = AFTER_POINT;
281 else 563 else
282 SYNTAX_ERROR ("too many decimal points in number"); 564 SYNTAX_ERROR_AT (virtual_location,
565 "too many decimal points in number");
283 } 566 }
284 else if ((radix <= 10 && (c == 'e' || c == 'E')) 567 else if ((radix <= 10 && (c == 'e' || c == 'E'))
285 || (radix == 16 && (c == 'p' || c == 'P'))) 568 || (radix == 16 && (c == 'p' || c == 'P')))
286 { 569 {
570 if (seen_digit_sep || DIGIT_SEP (*str))
571 SYNTAX_ERROR_AT (virtual_location,
572 "digit separator adjacent to exponent");
287 float_flag = AFTER_EXPON; 573 float_flag = AFTER_EXPON;
288 break; 574 break;
289 } 575 }
290 else 576 else
291 { 577 {
293 str--; 579 str--;
294 break; 580 break;
295 } 581 }
296 } 582 }
297 583
584 if (seen_digit_sep && float_flag != AFTER_EXPON)
585 SYNTAX_ERROR_AT (virtual_location,
586 "digit separator outside digit sequence");
587
298 /* The suffix may be for decimal fixed-point constants without exponent. */ 588 /* The suffix may be for decimal fixed-point constants without exponent. */
299 if (radix != 16 && float_flag == NOT_FLOAT) 589 if (radix != 16 && float_flag == NOT_FLOAT)
300 { 590 {
301 result = interpret_float_suffix (str, limit - str); 591 result = interpret_float_suffix (pfile, str, limit - str);
302 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM)) 592 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
303 { 593 {
304 result |= CPP_N_FLOATING; 594 result |= CPP_N_FLOATING;
305 /* We need to restore the radix to 10, if the radix is 8. */ 595 /* We need to restore the radix to 10, if the radix is 8. */
306 if (radix == 8) 596 if (radix == 8)
307 radix = 10; 597 radix = 10;
308 598
309 if (CPP_PEDANTIC (pfile)) 599 if (CPP_PEDANTIC (pfile))
310 cpp_error (pfile, CPP_DL_PEDWARN, 600 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
311 "fixed-point constants are a GCC extension"); 601 "fixed-point constants are a GCC extension");
312 goto syntax_ok; 602 goto syntax_ok;
313 } 603 }
314 else 604 else
315 result = 0; 605 result = 0;
316 } 606 }
319 radix = 10; 609 radix = 10;
320 610
321 if (max_digit >= radix) 611 if (max_digit >= radix)
322 { 612 {
323 if (radix == 2) 613 if (radix == 2)
324 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit); 614 SYNTAX_ERROR2_AT (virtual_location,
615 "invalid digit \"%c\" in binary constant", '0' + max_digit);
325 else 616 else
326 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit); 617 SYNTAX_ERROR2_AT (virtual_location,
618 "invalid digit \"%c\" in octal constant", '0' + max_digit);
327 } 619 }
328 620
329 if (float_flag != NOT_FLOAT) 621 if (float_flag != NOT_FLOAT)
330 { 622 {
331 if (radix == 2) 623 if (radix == 2)
332 { 624 {
333 cpp_error (pfile, CPP_DL_ERROR, 625 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
334 "invalid prefix \"0b\" for floating constant"); 626 "invalid prefix \"0b\" for floating constant");
335 return CPP_N_INVALID; 627 return CPP_N_INVALID;
336 } 628 }
337 629
338 if (radix == 16 && !seen_digit) 630 if (radix == 16 && !seen_digit)
339 SYNTAX_ERROR ("no digits in hexadecimal floating constant"); 631 SYNTAX_ERROR_AT (virtual_location,
340 632 "no digits in hexadecimal floating constant");
341 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99)) 633
342 cpp_error (pfile, CPP_DL_PEDWARN, 634 if (radix == 16 && CPP_PEDANTIC (pfile)
343 "use of C99 hexadecimal floating constant"); 635 && !CPP_OPTION (pfile, extended_numbers))
636 {
637 if (CPP_OPTION (pfile, cplusplus))
638 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
639 "use of C++17 hexadecimal floating constant");
640 else
641 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
642 "use of C99 hexadecimal floating constant");
643 }
344 644
345 if (float_flag == AFTER_EXPON) 645 if (float_flag == AFTER_EXPON)
346 { 646 {
347 if (*str == '+' || *str == '-') 647 if (*str == '+' || *str == '-')
348 str++; 648 str++;
349 649
350 /* Exponent is decimal, even if string is a hex float. */ 650 /* Exponent is decimal, even if string is a hex float. */
351 if (!ISDIGIT (*str)) 651 if (!ISDIGIT (*str))
352 SYNTAX_ERROR ("exponent has no digits"); 652 {
353 653 if (DIGIT_SEP (*str))
654 SYNTAX_ERROR_AT (virtual_location,
655 "digit separator adjacent to exponent");
656 else
657 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
658 }
354 do 659 do
355 str++; 660 {
356 while (ISDIGIT (*str)); 661 seen_digit_sep = DIGIT_SEP (*str);
662 str++;
663 }
664 while (ISDIGIT (*str) || DIGIT_SEP (*str));
357 } 665 }
358 else if (radix == 16) 666 else if (radix == 16)
359 SYNTAX_ERROR ("hexadecimal floating constants require an exponent"); 667 SYNTAX_ERROR_AT (virtual_location,
360 668 "hexadecimal floating constants require an exponent");
361 result = interpret_float_suffix (str, limit - str); 669
670 if (seen_digit_sep)
671 SYNTAX_ERROR_AT (virtual_location,
672 "digit separator outside digit sequence");
673
674 result = interpret_float_suffix (pfile, str, limit - str);
362 if (result == 0) 675 if (result == 0)
363 { 676 {
364 cpp_error (pfile, CPP_DL_ERROR, 677 if (CPP_OPTION (pfile, user_literals))
365 "invalid suffix \"%.*s\" on floating constant", 678 {
366 (int) (limit - str), str); 679 if (ud_suffix)
367 return CPP_N_INVALID; 680 *ud_suffix = (const char *) str;
681 result = CPP_N_LARGE | CPP_N_USERDEF;
682 }
683 else
684 {
685 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
686 "invalid suffix \"%.*s\" on floating constant",
687 (int) (limit - str), str);
688 return CPP_N_INVALID;
689 }
368 } 690 }
369 691
370 /* Traditional C didn't accept any floating suffixes. */ 692 /* Traditional C didn't accept any floating suffixes. */
371 if (limit != str 693 if (limit != str
372 && CPP_WTRADITIONAL (pfile) 694 && CPP_WTRADITIONAL (pfile)
373 && ! cpp_sys_macro_p (pfile)) 695 && ! cpp_sys_macro_p (pfile))
374 cpp_warning (pfile, CPP_W_TRADITIONAL, 696 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
375 "traditional C rejects the \"%.*s\" suffix", 697 "traditional C rejects the \"%.*s\" suffix",
376 (int) (limit - str), str); 698 (int) (limit - str), str);
377 699
378 /* A suffix for double is a GCC extension via decimal float support. 700 /* A suffix for double is a GCC extension via decimal float support.
379 If the suffix also specifies an imaginary value we'll catch that 701 If the suffix also specifies an imaginary value we'll catch that
380 later. */ 702 later. */
381 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile)) 703 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
382 cpp_error (pfile, CPP_DL_PEDWARN, 704 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
383 "suffix for double constant is a GCC extension"); 705 "suffix for double constant is a GCC extension");
384 706
385 /* Radix must be 10 for decimal floats. */ 707 /* Radix must be 10 for decimal floats. */
386 if ((result & CPP_N_DFLOAT) && radix != 10) 708 if ((result & CPP_N_DFLOAT) && radix != 10)
387 { 709 {
388 cpp_error (pfile, CPP_DL_ERROR, 710 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
389 "invalid suffix \"%.*s\" with hexadecimal floating constant", 711 "invalid suffix \"%.*s\" with hexadecimal floating constant",
390 (int) (limit - str), str); 712 (int) (limit - str), str);
391 return CPP_N_INVALID; 713 return CPP_N_INVALID;
392 } 714 }
393 715
394 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile)) 716 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
395 cpp_error (pfile, CPP_DL_PEDWARN, 717 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
396 "fixed-point constants are a GCC extension"); 718 "fixed-point constants are a GCC extension");
397 719
398 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile)) 720 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
399 cpp_error (pfile, CPP_DL_PEDWARN, 721 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
400 "decimal float constants are a GCC extension"); 722 "decimal float constants are a GCC extension");
401 723
402 result |= CPP_N_FLOATING; 724 result |= CPP_N_FLOATING;
403 } 725 }
404 else 726 else
405 { 727 {
406 result = interpret_int_suffix (str, limit - str); 728 result = interpret_int_suffix (pfile, str, limit - str);
407 if (result == 0) 729 if (result == 0)
408 { 730 {
409 cpp_error (pfile, CPP_DL_ERROR, 731 if (CPP_OPTION (pfile, user_literals))
410 "invalid suffix \"%.*s\" on integer constant", 732 {
411 (int) (limit - str), str); 733 if (ud_suffix)
412 return CPP_N_INVALID; 734 *ud_suffix = (const char *) str;
735 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
736 }
737 else
738 {
739 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
740 "invalid suffix \"%.*s\" on integer constant",
741 (int) (limit - str), str);
742 return CPP_N_INVALID;
743 }
413 } 744 }
414 745
415 /* Traditional C only accepted the 'L' suffix. 746 /* Traditional C only accepted the 'L' suffix.
416 Suppress warning about 'LL' with -Wno-long-long. */ 747 Suppress warning about 'LL' with -Wno-long-long. */
417 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile)) 748 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
419 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY)); 750 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
420 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE 751 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
421 && CPP_OPTION (pfile, cpp_warn_long_long); 752 && CPP_OPTION (pfile, cpp_warn_long_long);
422 753
423 if (u_or_i || large) 754 if (u_or_i || large)
424 cpp_warning (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL, 755 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
425 "traditional C rejects the \"%.*s\" suffix", 756 virtual_location, 0,
426 (int) (limit - str), str); 757 "traditional C rejects the \"%.*s\" suffix",
758 (int) (limit - str), str);
427 } 759 }
428 760
429 if ((result & CPP_N_WIDTH) == CPP_N_LARGE 761 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
430 && CPP_OPTION (pfile, cpp_warn_long_long)) 762 && CPP_OPTION (pfile, cpp_warn_long_long))
431 { 763 {
432 const char *message = CPP_OPTION (pfile, cplusplus) 764 const char *message = CPP_OPTION (pfile, cplusplus)
433 ? N_("use of C++0x long long integer constant") 765 ? N_("use of C++11 long long integer constant")
434 : N_("use of C99 long long integer constant"); 766 : N_("use of C99 long long integer constant");
435 767
436 if (CPP_OPTION (pfile, c99)) 768 if (CPP_OPTION (pfile, c99))
437 cpp_warning (pfile, CPP_W_LONG_LONG, message); 769 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
770 0, message);
438 else 771 else
439 cpp_pedwarning (pfile, CPP_W_LONG_LONG, message); 772 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
773 virtual_location, 0, message);
440 } 774 }
441 775
442 result |= CPP_N_INTEGER; 776 result |= CPP_N_INTEGER;
443 } 777 }
444 778
445 syntax_ok: 779 syntax_ok:
446 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile)) 780 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
447 cpp_error (pfile, CPP_DL_PEDWARN, 781 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
448 "imaginary constants are a GCC extension"); 782 "imaginary constants are a GCC extension");
449 if (radix == 2 && CPP_PEDANTIC (pfile)) 783 if (radix == 2
450 cpp_error (pfile, CPP_DL_PEDWARN, 784 && !CPP_OPTION (pfile, binary_constants)
451 "binary constants are a GCC extension"); 785 && CPP_PEDANTIC (pfile))
786 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
787 CPP_OPTION (pfile, cplusplus)
788 ? N_("binary constants are a C++14 feature "
789 "or GCC extension")
790 : N_("binary constants are a GCC extension"));
452 791
453 if (radix == 10) 792 if (radix == 10)
454 result |= CPP_N_DECIMAL; 793 result |= CPP_N_DECIMAL;
455 else if (radix == 16) 794 else if (radix == 16)
456 result |= CPP_N_HEX; 795 result |= CPP_N_HEX;
523 { 862 {
524 c = *p; 863 c = *p;
525 864
526 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c))) 865 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
527 c = hex_value (c); 866 c = hex_value (c);
867 else if (DIGIT_SEP (c))
868 continue;
528 else 869 else
529 break; 870 break;
530 871
531 /* Strict inequality for when max is set to zero. */ 872 /* Strict inequality for when max is set to zero. */
532 if (result.low < max) 873 if (result.low < max)
537 overflow |= result.overflow; 878 overflow |= result.overflow;
538 max = 0; 879 max = 0;
539 } 880 }
540 } 881 }
541 882
542 if (overflow) 883 if (overflow && !(type & CPP_N_USERDEF))
543 cpp_error (pfile, CPP_DL_PEDWARN, 884 cpp_error (pfile, CPP_DL_PEDWARN,
544 "integer constant is too large for its type"); 885 "integer constant is too large for its type");
545 /* If too big to be signed, consider it unsigned. Only warn for 886 /* If too big to be signed, consider it unsigned. Only warn for
546 decimal numbers. Traditional numbers were always signed (but 887 decimal numbers. Traditional numbers were always signed (but
547 we still honor an explicit U suffix); but we only have 888 we still honor an explicit U suffix); but we only have
688 } 1029 }
689 } 1030 }
690 1031
691 if (node) 1032 if (node)
692 { 1033 {
693 if (pfile->context != initial_context && CPP_PEDANTIC (pfile)) 1034 if ((pfile->context != initial_context
694 cpp_error (pfile, CPP_DL_WARNING, 1035 || initial_context != &pfile->base_context)
695 "this use of \"defined\" may not be portable"); 1036 && CPP_OPTION (pfile, warn_expansion_to_defined))
1037 cpp_pedwarning (pfile, CPP_W_EXPANSION_TO_DEFINED,
1038 "this use of \"defined\" may not be portable");
696 1039
697 _cpp_mark_macro_used (node); 1040 _cpp_mark_macro_used (node);
698 if (!(node->flags & NODE_USED)) 1041 if (!(node->flags & NODE_USED))
699 { 1042 {
700 node->flags |= NODE_USED; 1043 node->flags |= NODE_USED;
718 pfile->mi_ind_cmacro = node; 1061 pfile->mi_ind_cmacro = node;
719 } 1062 }
720 1063
721 pfile->state.prevent_expansion--; 1064 pfile->state.prevent_expansion--;
722 1065
1066 /* Do not treat conditional macros as being defined. This is due to the
1067 powerpc and spu ports using conditional macros for 'vector', 'bool', and
1068 'pixel' to act as conditional keywords. This messes up tests like #ifndef
1069 bool. */
723 result.unsignedp = false; 1070 result.unsignedp = false;
724 result.high = 0; 1071 result.high = 0;
725 result.overflow = false; 1072 result.overflow = false;
726 result.low = node && node->type == NT_MACRO; 1073 result.low = (node && node->type == NT_MACRO
1074 && (node->flags & NODE_CONDITIONAL) == 0);
727 return result; 1075 return result;
728 } 1076 }
729 1077
730 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing 1078 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
731 number or character constant, or the result of the "defined" or "#" 1079 number or character constant, or the result of the "defined" or "#"
732 operators). */ 1080 operators). */
733 static cpp_num 1081 static cpp_num
734 eval_token (cpp_reader *pfile, const cpp_token *token) 1082 eval_token (cpp_reader *pfile, const cpp_token *token,
1083 source_location virtual_location)
735 { 1084 {
736 cpp_num result; 1085 cpp_num result;
737 unsigned int temp; 1086 unsigned int temp;
738 int unsignedp = 0; 1087 int unsignedp = 0;
739 1088
741 result.overflow = false; 1090 result.overflow = false;
742 1091
743 switch (token->type) 1092 switch (token->type)
744 { 1093 {
745 case CPP_NUMBER: 1094 case CPP_NUMBER:
746 temp = cpp_classify_number (pfile, token); 1095 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1096 if (temp & CPP_N_USERDEF)
1097 cpp_error (pfile, CPP_DL_ERROR,
1098 "user-defined literal in preprocessor expression");
747 switch (temp & CPP_N_CATEGORY) 1099 switch (temp & CPP_N_CATEGORY)
748 { 1100 {
749 case CPP_N_FLOATING: 1101 case CPP_N_FLOATING:
750 cpp_error (pfile, CPP_DL_ERROR, 1102 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
751 "floating constant in preprocessor expression"); 1103 "floating constant in preprocessor expression");
752 break; 1104 break;
753 case CPP_N_INTEGER: 1105 case CPP_N_INTEGER:
754 if (!(temp & CPP_N_IMAGINARY)) 1106 if (!(temp & CPP_N_IMAGINARY))
755 return cpp_interpret_integer (pfile, token, temp); 1107 return cpp_interpret_integer (pfile, token, temp);
756 cpp_error (pfile, CPP_DL_ERROR, 1108 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
757 "imaginary number in preprocessor expression"); 1109 "imaginary number in preprocessor expression");
758 break; 1110 break;
759 1111
760 case CPP_N_INVALID: 1112 case CPP_N_INVALID:
761 /* Error already issued. */ 1113 /* Error already issued. */
762 break; 1114 break;
766 1118
767 case CPP_WCHAR: 1119 case CPP_WCHAR:
768 case CPP_CHAR: 1120 case CPP_CHAR:
769 case CPP_CHAR16: 1121 case CPP_CHAR16:
770 case CPP_CHAR32: 1122 case CPP_CHAR32:
1123 case CPP_UTF8CHAR:
771 { 1124 {
772 cppchar_t cc = cpp_interpret_charconst (pfile, token, 1125 cppchar_t cc = cpp_interpret_charconst (pfile, token,
773 &temp, &unsignedp); 1126 &temp, &unsignedp);
774 1127
775 result.high = 0; 1128 result.high = 0;
787 break; 1140 break;
788 1141
789 case CPP_NAME: 1142 case CPP_NAME:
790 if (token->val.node.node == pfile->spec_nodes.n_defined) 1143 if (token->val.node.node == pfile->spec_nodes.n_defined)
791 return parse_defined (pfile); 1144 return parse_defined (pfile);
1145 else if (token->val.node.node == pfile->spec_nodes.n__has_include__)
1146 return parse_has_include (pfile, IT_INCLUDE);
1147 else if (token->val.node.node == pfile->spec_nodes.n__has_include_next__)
1148 return parse_has_include (pfile, IT_INCLUDE_NEXT);
792 else if (CPP_OPTION (pfile, cplusplus) 1149 else if (CPP_OPTION (pfile, cplusplus)
793 && (token->val.node.node == pfile->spec_nodes.n_true 1150 && (token->val.node.node == pfile->spec_nodes.n_true
794 || token->val.node.node == pfile->spec_nodes.n_false)) 1151 || token->val.node.node == pfile->spec_nodes.n_false))
795 { 1152 {
796 result.high = 0; 1153 result.high = 0;
799 else 1156 else
800 { 1157 {
801 result.high = 0; 1158 result.high = 0;
802 result.low = 0; 1159 result.low = 0;
803 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval) 1160 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
804 cpp_warning (pfile, CPP_W_UNDEF, "\"%s\" is not defined", 1161 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
805 NODE_NAME (token->val.node.node)); 1162 "\"%s\" is not defined, evaluates to 0",
1163 NODE_NAME (token->val.node.node));
806 } 1164 }
807 break; 1165 break;
808 1166
809 case CPP_HASH: 1167 case CPP_HASH:
810 if (!pfile->state.skipping) 1168 if (!pfile->state.skipping)
811 { 1169 {
812 /* A pedantic warning takes precedence over a deprecated 1170 /* A pedantic warning takes precedence over a deprecated
813 warning here. */ 1171 warning here. */
814 if (CPP_PEDANTIC (pfile)) 1172 if (CPP_PEDANTIC (pfile))
815 cpp_error (pfile, CPP_DL_PEDWARN, 1173 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
816 "assertions are a GCC extension"); 1174 virtual_location, 0,
1175 "assertions are a GCC extension");
817 else if (CPP_OPTION (pfile, cpp_warn_deprecated)) 1176 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
818 cpp_warning (pfile, CPP_W_DEPRECATED, 1177 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
819 "assertions are a deprecated extension"); 1178 "assertions are a deprecated extension");
820 } 1179 }
821 _cpp_test_assertion (pfile, &temp); 1180 _cpp_test_assertion (pfile, &temp);
822 result.high = 0; 1181 result.high = 0;
823 result.low = temp; 1182 result.low = temp;
824 break; 1183 break;
916 _cpp_parse_expr (cpp_reader *pfile, bool is_if) 1275 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
917 { 1276 {
918 struct op *top = pfile->op_stack; 1277 struct op *top = pfile->op_stack;
919 unsigned int lex_count; 1278 unsigned int lex_count;
920 bool saw_leading_not, want_value = true; 1279 bool saw_leading_not, want_value = true;
1280 source_location virtual_location = 0;
921 1281
922 pfile->state.skip_eval = 0; 1282 pfile->state.skip_eval = 0;
923 1283
924 /* Set up detection of #if ! defined(). */ 1284 /* Set up detection of #if ! defined(). */
925 pfile->mi_ind_cmacro = 0; 1285 pfile->mi_ind_cmacro = 0;
932 for (;;) 1292 for (;;)
933 { 1293 {
934 struct op op; 1294 struct op op;
935 1295
936 lex_count++; 1296 lex_count++;
937 op.token = cpp_get_token (pfile); 1297 op.token = cpp_get_token_with_location (pfile, &virtual_location);
938 op.op = op.token->type; 1298 op.op = op.token->type;
939 op.loc = op.token->src_loc; 1299 op.loc = virtual_location;
940 1300
941 switch (op.op) 1301 switch (op.op)
942 { 1302 {
943 /* These tokens convert into values. */ 1303 /* These tokens convert into values. */
944 case CPP_NUMBER: 1304 case CPP_NUMBER:
945 case CPP_CHAR: 1305 case CPP_CHAR:
946 case CPP_WCHAR: 1306 case CPP_WCHAR:
947 case CPP_CHAR16: 1307 case CPP_CHAR16:
948 case CPP_CHAR32: 1308 case CPP_CHAR32:
1309 case CPP_UTF8CHAR:
949 case CPP_NAME: 1310 case CPP_NAME:
950 case CPP_HASH: 1311 case CPP_HASH:
951 if (!want_value) 1312 if (!want_value)
952 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"", 1313 SYNTAX_ERROR2_AT (op.loc,
953 cpp_token_as_text (pfile, op.token)); 1314 "missing binary operator before token \"%s\"",
1315 cpp_token_as_text (pfile, op.token));
954 want_value = false; 1316 want_value = false;
955 top->value = eval_token (pfile, op.token); 1317 top->value = eval_token (pfile, op.token, op.loc);
956 continue; 1318 continue;
957 1319
958 case CPP_NOT: 1320 case CPP_NOT:
959 saw_leading_not = lex_count == 1; 1321 saw_leading_not = lex_count == 1;
960 break; 1322 break;
967 op.op = CPP_UMINUS; 1329 op.op = CPP_UMINUS;
968 break; 1330 break;
969 1331
970 default: 1332 default:
971 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ) 1333 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
972 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions", 1334 SYNTAX_ERROR2_AT (op.loc,
973 cpp_token_as_text (pfile, op.token)); 1335 "token \"%s\" is not valid in preprocessor expressions",
1336 cpp_token_as_text (pfile, op.token));
974 break; 1337 break;
975 } 1338 }
976 1339
977 /* Check we have a value or operator as appropriate. */ 1340 /* Check we have a value or operator as appropriate. */
978 if (optab[op.op].flags & NO_L_OPERAND) 1341 if (optab[op.op].flags & NO_L_OPERAND)
979 { 1342 {
980 if (!want_value) 1343 if (!want_value)
981 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"", 1344 SYNTAX_ERROR2_AT (op.loc,
982 cpp_token_as_text (pfile, op.token)); 1345 "missing binary operator before token \"%s\"",
1346 cpp_token_as_text (pfile, op.token));
983 } 1347 }
984 else if (want_value) 1348 else if (want_value)
985 { 1349 {
986 /* We want a number (or expression) and haven't got one. 1350 /* We want a number (or expression) and haven't got one.
987 Try to emit a specific diagnostic. */ 1351 Try to emit a specific diagnostic. */
988 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN) 1352 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
989 SYNTAX_ERROR ("missing expression between '(' and ')'"); 1353 SYNTAX_ERROR_AT (op.loc,
1354 "missing expression between '(' and ')'");
990 1355
991 if (op.op == CPP_EOF && top->op == CPP_EOF) 1356 if (op.op == CPP_EOF && top->op == CPP_EOF)
992 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif"); 1357 SYNTAX_ERROR2_AT (op.loc,
1358 "%s with no expression", is_if ? "#if" : "#elif");
993 1359
994 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN) 1360 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
995 SYNTAX_ERROR2 ("operator '%s' has no right operand", 1361 SYNTAX_ERROR2_AT (op.loc,
996 cpp_token_as_text (pfile, top->token)); 1362 "operator '%s' has no right operand",
1363 cpp_token_as_text (pfile, top->token));
997 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF) 1364 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
998 /* Complain about missing paren during reduction. */; 1365 /* Complain about missing paren during reduction. */;
999 else 1366 else
1000 SYNTAX_ERROR2 ("operator '%s' has no left operand", 1367 SYNTAX_ERROR2_AT (op.loc,
1001 cpp_token_as_text (pfile, op.token)); 1368 "operator '%s' has no left operand",
1369 cpp_token_as_text (pfile, op.token));
1002 } 1370 }
1003 1371
1004 top = reduce (pfile, top, op.op); 1372 top = reduce (pfile, top, op.op);
1005 if (!top) 1373 if (!top)
1006 goto syntax_error; 1374 goto syntax_error;
1021 if (num_zerop (top->value)) 1389 if (num_zerop (top->value))
1022 pfile->state.skip_eval++; 1390 pfile->state.skip_eval++;
1023 break; 1391 break;
1024 case CPP_COLON: 1392 case CPP_COLON:
1025 if (top->op != CPP_QUERY) 1393 if (top->op != CPP_QUERY)
1026 SYNTAX_ERROR (" ':' without preceding '?'"); 1394 SYNTAX_ERROR_AT (op.loc,
1395 " ':' without preceding '?'");
1027 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */ 1396 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1028 pfile->state.skip_eval++; 1397 pfile->state.skip_eval++;
1029 else 1398 else
1030 pfile->state.skip_eval--; 1399 pfile->state.skip_eval--;
1031 default: 1400 default:
1038 if (++top == pfile->op_limit) 1407 if (++top == pfile->op_limit)
1039 top = _cpp_expand_op_stack (pfile); 1408 top = _cpp_expand_op_stack (pfile);
1040 1409
1041 top->op = op.op; 1410 top->op = op.op;
1042 top->token = op.token; 1411 top->token = op.token;
1043 top->loc = op.token->src_loc; 1412 top->loc = op.loc;
1044 } 1413 }
1045 1414
1046 /* The controlling macro expression is only valid if we called lex 3 1415 /* The controlling macro expression is only valid if we called lex 3
1047 times: <!> <defined expression> and <EOF>. push_conditional () 1416 times: <!> <defined expression> and <EOF>. push_conditional ()
1048 checks that we are at top-of-file. */ 1417 checks that we are at top-of-file. */
1049 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3)) 1418 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1050 pfile->mi_ind_cmacro = 0; 1419 pfile->mi_ind_cmacro = 0;
1051 1420
1052 if (top != pfile->op_stack) 1421 if (top != pfile->op_stack)
1053 { 1422 {
1054 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s", 1423 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1055 is_if ? "#if" : "#elif"); 1424 "unbalanced stack in %s",
1425 is_if ? "#if" : "#elif");
1056 syntax_error: 1426 syntax_error:
1057 return false; /* Return false on syntax error. */ 1427 return false; /* Return false on syntax error. */
1058 } 1428 }
1059 1429
1060 return !num_zerop (top->value); 1430 return !num_zerop (top->value);
1570 lhs = num_rshift (lhs, precision, n); 1940 lhs = num_rshift (lhs, precision, n);
1571 break; 1941 break;
1572 1942
1573 /* Arithmetic. */ 1943 /* Arithmetic. */
1574 case CPP_MINUS: 1944 case CPP_MINUS:
1575 rhs = num_negate (rhs, precision); 1945 result.low = lhs.low - rhs.low;
1946 result.high = lhs.high - rhs.high;
1947 if (result.low > lhs.low)
1948 result.high--;
1949 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1950 result.overflow = false;
1951
1952 result = num_trim (result, precision);
1953 if (!result.unsignedp)
1954 {
1955 bool lhsp = num_positive (lhs, precision);
1956 result.overflow = (lhsp != num_positive (rhs, precision)
1957 && lhsp != num_positive (result, precision));
1958 }
1959 return result;
1960
1576 case CPP_PLUS: 1961 case CPP_PLUS:
1577 result.low = lhs.low + rhs.low; 1962 result.low = lhs.low + rhs.low;
1578 result.high = lhs.high + rhs.high; 1963 result.high = lhs.high + rhs.high;
1579 if (result.low < lhs.low) 1964 if (result.low < lhs.low)
1580 result.high++; 1965 result.high++;
1592 1977
1593 /* Comma. */ 1978 /* Comma. */
1594 default: /* case CPP_COMMA: */ 1979 default: /* case CPP_COMMA: */
1595 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99) 1980 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1596 || !pfile->state.skip_eval)) 1981 || !pfile->state.skip_eval))
1597 cpp_error (pfile, CPP_DL_PEDWARN, 1982 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1598 "comma operator in operand of #if"); 1983 "comma operator in operand of #if");
1599 lhs = rhs; 1984 lhs = rhs;
1600 break; 1985 break;
1601 } 1986 }
1602 1987
1603 return lhs; 1988 return lhs;
1784 if (lhs_neg) 2169 if (lhs_neg)
1785 lhs = num_negate (lhs, precision); 2170 lhs = num_negate (lhs, precision);
1786 2171
1787 return lhs; 2172 return lhs;
1788 } 2173 }
2174
2175 /* Handle meeting "__has_include__" in a preprocessor expression. */
2176 static cpp_num
2177 parse_has_include (cpp_reader *pfile, enum include_type type)
2178 {
2179 cpp_num result;
2180 bool paren = false;
2181 cpp_hashnode *node = 0;
2182 const cpp_token *token;
2183 bool bracket = false;
2184 char *fname = 0;
2185
2186 result.unsignedp = false;
2187 result.high = 0;
2188 result.overflow = false;
2189 result.low = 0;
2190
2191 pfile->state.in__has_include__++;
2192
2193 token = cpp_get_token (pfile);
2194 if (token->type == CPP_OPEN_PAREN)
2195 {
2196 paren = true;
2197 token = cpp_get_token (pfile);
2198 }
2199
2200 if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
2201 {
2202 if (token->type == CPP_HEADER_NAME)
2203 bracket = true;
2204 fname = XNEWVEC (char, token->val.str.len - 1);
2205 memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
2206 fname[token->val.str.len - 2] = '\0';
2207 node = token->val.node.node;
2208 }
2209 else if (token->type == CPP_LESS)
2210 {
2211 bracket = true;
2212 fname = _cpp_bracket_include (pfile);
2213 }
2214 else
2215 cpp_error (pfile, CPP_DL_ERROR,
2216 "operator \"__has_include__\" requires a header string");
2217
2218 if (fname)
2219 {
2220 int angle_brackets = (bracket ? 1 : 0);
2221
2222 if (_cpp_has_header (pfile, fname, angle_brackets, type))
2223 result.low = 1;
2224 else
2225 result.low = 0;
2226
2227 XDELETEVEC (fname);
2228 }
2229
2230 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
2231 cpp_error (pfile, CPP_DL_ERROR,
2232 "missing ')' after \"__has_include__\"");
2233
2234 /* A possible controlling macro of the form #if !__has_include__ ().
2235 _cpp_parse_expr checks there was no other junk on the line. */
2236 if (node)
2237 pfile->mi_ind_cmacro = node;
2238
2239 pfile->state.in__has_include__--;
2240
2241 return result;
2242 }