Bug Summary

File:build/libcpp/expr.c
Warning:line 2177, column 38
The result of the left shift is undefined due to shifting by '18446744073709551553', which is greater or equal to the width of type 'cpp_num_part'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name expr.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model static -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/objdir/libcpp -resource-dir /usr/lib64/clang/13.0.0 -I /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libcpp -I . -I /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libcpp/../include -I /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libcpp/include -I /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libcpp -I . -I /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libcpp/../include -I /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libcpp/include -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/11/../../../../include/c++/11 -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/11/../../../../include/c++/11/x86_64-suse-linux -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/11/../../../../include/c++/11/backward -internal-isystem /usr/lib64/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib64/gcc/x86_64-suse-linux/11/../../../../x86_64-suse-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-narrowing -Wwrite-strings -Wno-long-long -fdeprecated-macro -fdebug-compilation-dir=/home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/objdir/libcpp -ferror-limit 19 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=plist-html -analyzer-config silence-checkers=core.NullDereference -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/objdir/clang-static-analyzer/2021-11-20-133755-20252-1/report-0GbjJ3.plist -x c++ /home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libcpp/expr.c
1/* Parse C expressions for cpplib.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994.
4
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
7Free Software Foundation; either version 3, or (at your option) any
8later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; see the file COPYING3. If not see
17<http://www.gnu.org/licenses/>. */
18
19#include "config.h"
20#include "system.h"
21#include "cpplib.h"
22#include "internal.h"
23
24#define PART_PRECISION(sizeof (cpp_num_part) * 8) (sizeof (cpp_num_part) * CHAR_BIT8)
25#define HALF_MASK(~(cpp_num_part) 0 >> ((sizeof (cpp_num_part) * 8) / 2)
)
(~(cpp_num_part) 0 >> (PART_PRECISION(sizeof (cpp_num_part) * 8) / 2))
26#define LOW_PART(num_part)(num_part & (~(cpp_num_part) 0 >> ((sizeof (cpp_num_part
) * 8) / 2)))
(num_part & HALF_MASK(~(cpp_num_part) 0 >> ((sizeof (cpp_num_part) * 8) / 2)
)
)
27#define HIGH_PART(num_part)(num_part >> ((sizeof (cpp_num_part) * 8) / 2)) (num_part >> (PART_PRECISION(sizeof (cpp_num_part) * 8) / 2))
28
29struct op
30{
31 const cpp_token *token; /* The token forming op (for diagnostics). */
32 cpp_num value; /* The value logically "right" of op. */
33 location_t loc; /* The location of this value. */
34 enum cpp_ttype op;
35};
36
37/* Some simple utility routines on double integers. */
38#define num_zerop(num)((num.low | num.high) == 0) ((num.low | num.high) == 0)
39#define num_eq(num1, num2)(num1.low == num2.low && num1.high == num2.high) (num1.low == num2.low && num1.high == num2.high)
40static bool num_positive (cpp_num, size_t);
41static bool num_greater_eq (cpp_num, cpp_num, size_t);
42static cpp_num num_trim (cpp_num, size_t);
43static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
44
45static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
46static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
47static cpp_num num_negate (cpp_num, size_t);
48static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
49static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
50 enum cpp_ttype);
51static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
52 enum cpp_ttype);
53static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
54static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype,
55 location_t);
56static cpp_num num_lshift (cpp_num, size_t, size_t);
57static cpp_num num_rshift (cpp_num, size_t, size_t);
58
59static cpp_num append_digit (cpp_num, int, int, size_t);
60static cpp_num parse_defined (cpp_reader *);
61static cpp_num eval_token (cpp_reader *, const cpp_token *, location_t);
62static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
63static unsigned int interpret_float_suffix (cpp_reader *, const uchar *, size_t);
64static unsigned int interpret_int_suffix (cpp_reader *, const uchar *, size_t);
65static void check_promotion (cpp_reader *, const struct op *);
66
67/* Token type abuse to create unary plus and minus operators. */
68#define CPP_UPLUS((enum cpp_ttype) (CPP_LAST_CPP_OP + 1)) ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69#define CPP_UMINUS((enum cpp_ttype) (CPP_LAST_CPP_OP + 2)) ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
70
71/* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
73#define SYNTAX_ERROR(msgid)do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error
; } while(0)
\
74 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
75#define SYNTAX_ERROR2(msgid, arg)do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error
; } while(0)
\
76 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
77 while(0)
78#define SYNTAX_ERROR_AT(loc, msgid)do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid
); goto syntax_error; } while(0)
\
79 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
80 while(0)
81#define SYNTAX_ERROR2_AT(loc, msgid, arg)do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid
, arg); goto syntax_error; } while(0)
\
82 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
83 while(0)
84
85/* Subroutine of cpp_classify_number. S points to a float suffix of
86 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
87 flag vector (of CPP_N_* bits) describing the suffix. */
88static unsigned int
89interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len)
90{
91 size_t orig_len = len;
92 const uchar *orig_s = s;
93 size_t flags;
94 size_t f, d, l, w, q, i, fn, fnx, fn_bits;
95
96 flags = 0;
97 f = d = l = w = q = i = fn = fnx = fn_bits = 0;
98
99 /* The following decimal float suffixes, from TR 24732:2009, TS
100 18661-2:2015 and C2X, 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). */
135
136 /* Process decimal float suffixes, which are two letters starting
137 with d or D. Order and case are significant. */
138 if (len == 2 && (*s == 'd' || *s == 'D'))
139 {
140 bool uppercase = (*s == 'D');
141 switch (s[1])
142 {
143 case 'f': return (!uppercase ? (CPP_N_DFLOAT0x4000 | CPP_N_SMALL0x0010): 0); break;
144 case 'F': return (uppercase ? (CPP_N_DFLOAT0x4000 | CPP_N_SMALL0x0010) : 0); break;
145 case 'd': return (!uppercase ? (CPP_N_DFLOAT0x4000 | CPP_N_MEDIUM0x0020): 0); break;
146 case 'D': return (uppercase ? (CPP_N_DFLOAT0x4000 | CPP_N_MEDIUM0x0020) : 0); break;
147 case 'l': return (!uppercase ? (CPP_N_DFLOAT0x4000 | CPP_N_LARGE0x0040) : 0); break;
148 case 'L': return (uppercase ? (CPP_N_DFLOAT0x4000 | CPP_N_LARGE0x0040) : 0); break;
149 default:
150 /* Additional two-character suffixes beginning with D are not
151 for decimal float constants. */
152 break;
153 }
154 }
155
156 if (CPP_OPTION (pfile, ext_numeric_literals)((pfile)->opts.ext_numeric_literals))
157 {
158 /* Recognize a fixed-point suffix. */
159 if (len != 0)
160 switch (s[len-1])
161 {
162 case 'k': case 'K': flags = CPP_N_ACCUM0x200000; break;
163 case 'r': case 'R': flags = CPP_N_FRACT0x100000; break;
164 default: break;
165 }
166
167 /* Continue processing a fixed-point suffix. The suffix is case
168 insensitive except for ll or LL. Order is significant. */
169 if (flags)
170 {
171 if (len == 1)
172 return flags;
173 len--;
174
175 if (*s == 'u' || *s == 'U')
176 {
177 flags |= CPP_N_UNSIGNED0x1000;
178 if (len == 1)
179 return flags;
180 len--;
181 s++;
182 }
183
184 switch (*s)
185 {
186 case 'h': case 'H':
187 if (len == 1)
188 return flags |= CPP_N_SMALL0x0010;
189 break;
190 case 'l':
191 if (len == 1)
192 return flags |= CPP_N_MEDIUM0x0020;
193 if (len == 2 && s[1] == 'l')
194 return flags |= CPP_N_LARGE0x0040;
195 break;
196 case 'L':
197 if (len == 1)
198 return flags |= CPP_N_MEDIUM0x0020;
199 if (len == 2 && s[1] == 'L')
200 return flags |= CPP_N_LARGE0x0040;
201 break;
202 default:
203 break;
204 }
205 /* Anything left at this point is invalid. */
206 return 0;
207 }
208 }
209
210 /* In any remaining valid suffix, the case and order don't matter. */
211 while (len--)
212 {
213 switch (s[0])
214 {
215 case 'f': case 'F':
216 f++;
217 if (len > 0
218 && !CPP_OPTION (pfile, cplusplus)((pfile)->opts.cplusplus)
219 && s[1] >= '1'
220 && s[1] <= '9'
221 && fn_bits == 0)
222 {
223 f--;
224 while (len > 0
225 && s[1] >= '0'
226 && s[1] <= '9'
227 && fn_bits < CPP_FLOATN_MAX0xF0)
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_MAX0xF0)
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)
273 {
274 if (!CPP_OPTION (pfile, ext_numeric_literals)((pfile)->opts.ext_numeric_literals))
275 return 0;
276
277 /* In C++14 and up these suffixes are in the standard library, so treat
278 them as user-defined literals. */
279 if (CPP_OPTION (pfile, cplusplus)((pfile)->opts.cplusplus)
280 && CPP_OPTION (pfile, lang)((pfile)->opts.lang) > CLK_CXX11
281 && orig_s[0] == 'i'
282 && (orig_len == 1
283 || (orig_len == 2
284 && (orig_s[1] == 'f' || orig_s[1] == 'l'))))
285 return 0;
286 }
287
288 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals)((pfile)->opts.ext_numeric_literals))
289 return 0;
290
291 return ((i ? CPP_N_IMAGINARY0x2000 : 0)
292 | (f ? CPP_N_SMALL0x0010 :
293 d ? CPP_N_MEDIUM0x0020 :
294 l ? CPP_N_LARGE0x0040 :
295 w ? CPP_N_MD_W0x10000 :
296 q ? CPP_N_MD_Q0x20000 :
297 fn ? CPP_N_FLOATN0x400000 | (fn_bits << CPP_FLOATN_SHIFT24) :
298 fnx ? CPP_N_FLOATNX0x800000 | (fn_bits << CPP_FLOATN_SHIFT24) :
299 CPP_N_DEFAULT0x8000));
300}
301
302/* Return the classification flags for a float suffix. */
303unsigned int
304cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
305{
306 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
307}
308
309/* Subroutine of cpp_classify_number. S points to an integer suffix
310 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
311 flag vector describing the suffix. */
312static unsigned int
313interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
314{
315 size_t orig_len = len;
316 size_t u, l, i, z;
317
318 u = l = i = z = 0;
319
320 while (len--)
321 switch (s[len])
322 {
323 case 'z': case 'Z': z++; break;
324 case 'u': case 'U': u++; break;
325 case 'i': case 'I':
326 case 'j': case 'J': i++; break;
327 case 'l': case 'L': l++;
328 /* If there are two Ls, they must be adjacent and the same case. */
329 if (l == 2 && s[len] != s[len + 1])
330 return 0;
331 break;
332 default:
333 return 0;
334 }
335
336 if (l > 2 || u > 1 || i > 1 || z > 1)
337 return 0;
338
339 if (z)
340 {
341 if (l > 0 || i > 0)
342 return 0;
343 if (!CPP_OPTION (pfile, cplusplus)((pfile)->opts.cplusplus))
344 return 0;
345 }
346
347 if (i)
348 {
349 if (!CPP_OPTION (pfile, ext_numeric_literals)((pfile)->opts.ext_numeric_literals))
350 return 0;
351
352 /* In C++14 and up these suffixes are in the standard library, so treat
353 them as user-defined literals. */
354 if (CPP_OPTION (pfile, cplusplus)((pfile)->opts.cplusplus)
355 && CPP_OPTION (pfile, lang)((pfile)->opts.lang) > CLK_CXX11
356 && s[0] == 'i'
357 && (orig_len == 1 || (orig_len == 2 && s[1] == 'l')))
358 return 0;
359 }
360
361 return ((i ? CPP_N_IMAGINARY0x2000 : 0)
362 | (u ? CPP_N_UNSIGNED0x1000 : 0)
363 | ((l == 0) ? CPP_N_SMALL0x0010
364 : (l == 1) ? CPP_N_MEDIUM0x0020 : CPP_N_LARGE0x0040)
365 | (z ? CPP_N_SIZE_T0x2000000 : 0));
366}
367
368/* Return the classification flags for an int suffix. */
369unsigned int
370cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
371{
372 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
373}
374
375/* Return the string type corresponding to the the input user-defined string
376 literal type. If the input type is not a user-defined string literal
377 type return the input type. */
378enum cpp_ttype
379cpp_userdef_string_remove_type (enum cpp_ttype type)
380{
381 if (type == CPP_STRING_USERDEF)
382 return CPP_STRING;
383 else if (type == CPP_WSTRING_USERDEF)
384 return CPP_WSTRING;
385 else if (type == CPP_STRING16_USERDEF)
386 return CPP_STRING16;
387 else if (type == CPP_STRING32_USERDEF)
388 return CPP_STRING32;
389 else if (type == CPP_UTF8STRING_USERDEF)
390 return CPP_UTF8STRING;
391 else
392 return type;
393}
394
395/* Return the user-defined string literal type corresponding to the input
396 string type. If the input type is not a string type return the input
397 type. */
398enum cpp_ttype
399cpp_userdef_string_add_type (enum cpp_ttype type)
400{
401 if (type == CPP_STRING)
402 return CPP_STRING_USERDEF;
403 else if (type == CPP_WSTRING)
404 return CPP_WSTRING_USERDEF;
405 else if (type == CPP_STRING16)
406 return CPP_STRING16_USERDEF;
407 else if (type == CPP_STRING32)
408 return CPP_STRING32_USERDEF;
409 else if (type == CPP_UTF8STRING)
410 return CPP_UTF8STRING_USERDEF;
411 else
412 return type;
413}
414
415/* Return the char type corresponding to the the input user-defined char
416 literal type. If the input type is not a user-defined char literal
417 type return the input type. */
418enum cpp_ttype
419cpp_userdef_char_remove_type (enum cpp_ttype type)
420{
421 if (type == CPP_CHAR_USERDEF)
422 return CPP_CHAR;
423 else if (type == CPP_WCHAR_USERDEF)
424 return CPP_WCHAR;
425 else if (type == CPP_CHAR16_USERDEF)
426 return CPP_CHAR16;
427 else if (type == CPP_CHAR32_USERDEF)
428 return CPP_CHAR32;
429 else if (type == CPP_UTF8CHAR_USERDEF)
430 return CPP_UTF8CHAR;
431 else
432 return type;
433}
434
435/* Return the user-defined char literal type corresponding to the input
436 char type. If the input type is not a char type return the input
437 type. */
438enum cpp_ttype
439cpp_userdef_char_add_type (enum cpp_ttype type)
440{
441 if (type == CPP_CHAR)
442 return CPP_CHAR_USERDEF;
443 else if (type == CPP_WCHAR)
444 return CPP_WCHAR_USERDEF;
445 else if (type == CPP_CHAR16)
446 return CPP_CHAR16_USERDEF;
447 else if (type == CPP_CHAR32)
448 return CPP_CHAR32_USERDEF;
449 else if (type == CPP_UTF8CHAR)
450 return CPP_UTF8CHAR_USERDEF;
451 else
452 return type;
453}
454
455/* Return true if the token type is a user-defined string literal. */
456bool
457cpp_userdef_string_p (enum cpp_ttype type)
458{
459 if (type == CPP_STRING_USERDEF
460 || type == CPP_WSTRING_USERDEF
461 || type == CPP_STRING16_USERDEF
462 || type == CPP_STRING32_USERDEF
463 || type == CPP_UTF8STRING_USERDEF)
464 return true;
465 else
466 return false;
467}
468
469/* Return true if the token type is a user-defined char literal. */
470bool
471cpp_userdef_char_p (enum cpp_ttype type)
472{
473 if (type == CPP_CHAR_USERDEF
474 || type == CPP_WCHAR_USERDEF
475 || type == CPP_CHAR16_USERDEF
476 || type == CPP_CHAR32_USERDEF
477 || type == CPP_UTF8CHAR_USERDEF)
478 return true;
479 else
480 return false;
481}
482
483/* Extract the suffix from a user-defined literal string or char. */
484const char *
485cpp_get_userdef_suffix (const cpp_token *tok)
486{
487 unsigned int len = tok->val.str.len;
488 const char *text = (const char *)tok->val.str.text;
489 char delim;
490 unsigned int i;
491 for (i = 0; i < len; ++i)
492 if (text[i] == '\'' || text[i] == '"')
493 break;
494 if (i == len)
495 return text + len;
496 delim = text[i];
497 for (i = len; i > 0; --i)
498 if (text[i - 1] == delim)
499 break;
500 return text + i;
501}
502
503/* Categorize numeric constants according to their field (integer,
504 floating point, or invalid), radix (decimal, octal, hexadecimal),
505 and type suffixes.
506
507 TOKEN is the token that represents the numeric constant to
508 classify.
509
510 In C++0X if UD_SUFFIX is non null it will be assigned
511 any unrecognized suffix for a user-defined literal.
512
513 VIRTUAL_LOCATION is the virtual location for TOKEN. */
514unsigned int
515cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
516 const char **ud_suffix, location_t virtual_location)
517{
518 const uchar *str = token->val.str.text;
519 const uchar *limit;
520 unsigned int max_digit, result, radix;
521 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
522 bool seen_digit;
523 bool seen_digit_sep;
524
525 if (ud_suffix)
526 *ud_suffix = NULL__null;
527
528 /* If the lexer has done its job, length one can only be a single
529 digit. Fast-path this very common case. */
530 if (token->val.str.len == 1)
531 return CPP_N_INTEGER0x0001 | CPP_N_SMALL0x0010 | CPP_N_DECIMAL0x0100;
532
533 limit = str + token->val.str.len;
534 float_flag = NOT_FLOAT;
535 max_digit = 0;
536 radix = 10;
537 seen_digit = false;
538 seen_digit_sep = false;
539
540 /* First, interpret the radix. */
541 if (*str == '0')
542 {
543 radix = 8;
544 str++;
545
546 /* Require at least one hex digit to classify it as hex. */
547 if (*str == 'x' || *str == 'X')
548 {
549 if (str[1] == '.' || ISXDIGIT (str[1])(_sch_istable[(str[1]) & 0xff] & (unsigned short)(_sch_isxdigit
))
)
550 {
551 radix = 16;
552 str++;
553 }
554 else if (DIGIT_SEP (str[1])((str[1]) == '\'' && ((pfile)->opts.digit_separators
))
)
555 SYNTAX_ERROR_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator after base indicator"); goto syntax_error
; } while(0)
556 "digit separator after base indicator")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator after base indicator"); goto syntax_error
; } while(0)
;
557 }
558 else if (*str == 'b' || *str == 'B')
559 {
560 if (str[1] == '0' || str[1] == '1')
561 {
562 radix = 2;
563 str++;
564 }
565 else if (DIGIT_SEP (str[1])((str[1]) == '\'' && ((pfile)->opts.digit_separators
))
)
566 SYNTAX_ERROR_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator after base indicator"); goto syntax_error
; } while(0)
567 "digit separator after base indicator")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator after base indicator"); goto syntax_error
; } while(0)
;
568 }
569 }
570
571 /* Now scan for a well-formed integer or float. */
572 for (;;)
573 {
574 unsigned int c = *str++;
575
576 if (ISDIGIT (c)(_sch_istable[(c) & 0xff] & (unsigned short)(_sch_isdigit
))
|| (ISXDIGIT (c)(_sch_istable[(c) & 0xff] & (unsigned short)(_sch_isxdigit
))
&& radix == 16))
577 {
578 seen_digit_sep = false;
579 seen_digit = true;
580 c = hex_value (c)((unsigned int) _hex_value[(unsigned char) (c)]);
581 if (c > max_digit)
582 max_digit = c;
583 }
584 else if (DIGIT_SEP (c)((c) == '\'' && ((pfile)->opts.digit_separators)))
585 seen_digit_sep = true;
586 else if (c == '.')
587 {
588 if (seen_digit_sep || DIGIT_SEP (*str)((*str) == '\'' && ((pfile)->opts.digit_separators
))
)
589 SYNTAX_ERROR_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator adjacent to decimal point"); goto syntax_error
; } while(0)
590 "digit separator adjacent to decimal point")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator adjacent to decimal point"); goto syntax_error
; } while(0)
;
591 seen_digit_sep = false;
592 if (float_flag == NOT_FLOAT)
593 float_flag = AFTER_POINT;
594 else
595 SYNTAX_ERROR_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "too many decimal points in number"); goto syntax_error
; } while(0)
596 "too many decimal points in number")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "too many decimal points in number"); goto syntax_error
; } while(0)
;
597 }
598 else if ((radix <= 10 && (c == 'e' || c == 'E'))
599 || (radix == 16 && (c == 'p' || c == 'P')))
600 {
601 if (seen_digit_sep || DIGIT_SEP (*str)((*str) == '\'' && ((pfile)->opts.digit_separators
))
)
602 SYNTAX_ERROR_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator adjacent to exponent"); goto syntax_error
; } while(0)
603 "digit separator adjacent to exponent")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator adjacent to exponent"); goto syntax_error
; } while(0)
;
604 float_flag = AFTER_EXPON;
605 break;
606 }
607 else
608 {
609 /* Start of suffix. */
610 str--;
611 break;
612 }
613 }
614
615 if (seen_digit_sep && float_flag != AFTER_EXPON)
616 SYNTAX_ERROR_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator outside digit sequence"); goto syntax_error
; } while(0)
617 "digit separator outside digit sequence")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator outside digit sequence"); goto syntax_error
; } while(0)
;
618
619 /* The suffix may be for decimal fixed-point constants without exponent. */
620 if (radix != 16 && float_flag == NOT_FLOAT)
621 {
622 result = interpret_float_suffix (pfile, str, limit - str);
623 if ((result & CPP_N_FRACT0x100000) || (result & CPP_N_ACCUM0x200000))
624 {
625 result |= CPP_N_FLOATING0x0002;
626 /* We need to restore the radix to 10, if the radix is 8. */
627 if (radix == 8)
628 radix = 10;
629
630 if (CPP_PEDANTIC (pfile)((pfile)->opts.cpp_pedantic))
631 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
632 "fixed-point constants are a GCC extension");
633 goto syntax_ok;
634 }
635 else
636 result = 0;
637 }
638
639 if (float_flag != NOT_FLOAT && radix == 8)
640 radix = 10;
641
642 if (max_digit >= radix)
643 {
644 if (radix == 2)
645 SYNTAX_ERROR2_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "invalid digit \"%c\" in binary constant", '0' + max_digit
); goto syntax_error; } while(0)
646 "invalid digit \"%c\" in binary constant", '0' + max_digit)do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "invalid digit \"%c\" in binary constant", '0' + max_digit
); goto syntax_error; } while(0)
;
647 else
648 SYNTAX_ERROR2_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "invalid digit \"%c\" in octal constant", '0' + max_digit
); goto syntax_error; } while(0)
649 "invalid digit \"%c\" in octal constant", '0' + max_digit)do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "invalid digit \"%c\" in octal constant", '0' + max_digit
); goto syntax_error; } while(0)
;
650 }
651
652 if (float_flag != NOT_FLOAT)
653 {
654 if (radix == 2)
655 {
656 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
657 "invalid prefix \"0b\" for floating constant");
658 return CPP_N_INVALID0x0000;
659 }
660
661 if (radix == 16 && !seen_digit)
662 SYNTAX_ERROR_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "no digits in hexadecimal floating constant"); goto syntax_error
; } while(0)
663 "no digits in hexadecimal floating constant")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "no digits in hexadecimal floating constant"); goto syntax_error
; } while(0)
;
664
665 if (radix == 16 && CPP_PEDANTIC (pfile)((pfile)->opts.cpp_pedantic)
666 && !CPP_OPTION (pfile, extended_numbers)((pfile)->opts.extended_numbers))
667 {
668 if (CPP_OPTION (pfile, cplusplus)((pfile)->opts.cplusplus))
669 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
670 "use of C++17 hexadecimal floating constant");
671 else
672 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
673 "use of C99 hexadecimal floating constant");
674 }
675
676 if (float_flag == AFTER_EXPON)
677 {
678 if (*str == '+' || *str == '-')
679 str++;
680
681 /* Exponent is decimal, even if string is a hex float. */
682 if (!ISDIGIT (*str)(_sch_istable[(*str) & 0xff] & (unsigned short)(_sch_isdigit
))
)
683 {
684 if (DIGIT_SEP (*str)((*str) == '\'' && ((pfile)->opts.digit_separators
))
)
685 SYNTAX_ERROR_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator adjacent to exponent"); goto syntax_error
; } while(0)
686 "digit separator adjacent to exponent")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator adjacent to exponent"); goto syntax_error
; } while(0)
;
687 else
688 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "exponent has no digits"); goto syntax_error; } while(0
)
;
689 }
690 do
691 {
692 seen_digit_sep = DIGIT_SEP (*str)((*str) == '\'' && ((pfile)->opts.digit_separators
))
;
693 str++;
694 }
695 while (ISDIGIT (*str)(_sch_istable[(*str) & 0xff] & (unsigned short)(_sch_isdigit
))
|| DIGIT_SEP (*str)((*str) == '\'' && ((pfile)->opts.digit_separators
))
);
696 }
697 else if (radix == 16)
698 SYNTAX_ERROR_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "hexadecimal floating constants require an exponent"); goto
syntax_error; } while(0)
699 "hexadecimal floating constants require an exponent")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "hexadecimal floating constants require an exponent"); goto
syntax_error; } while(0)
;
700
701 if (seen_digit_sep)
702 SYNTAX_ERROR_AT (virtual_location,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator outside digit sequence"); goto syntax_error
; } while(0)
703 "digit separator outside digit sequence")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (virtual_location
), 0, "digit separator outside digit sequence"); goto syntax_error
; } while(0)
;
704
705 result = interpret_float_suffix (pfile, str, limit - str);
706 if (result == 0)
707 {
708 if (CPP_OPTION (pfile, user_literals)((pfile)->opts.user_literals))
709 {
710 if (ud_suffix)
711 *ud_suffix = (const char *) str;
712 result = CPP_N_LARGE0x0040 | CPP_N_USERDEF0x1000000;
713 }
714 else
715 {
716 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
717 "invalid suffix \"%.*s\" on floating constant",
718 (int) (limit - str), str);
719 return CPP_N_INVALID0x0000;
720 }
721 }
722
723 /* Traditional C didn't accept any floating suffixes. */
724 if (limit != str
725 && CPP_WTRADITIONAL (pfile)((pfile)->opts.cpp_warn_traditional)
726 && ! cpp_sys_macro_p (pfile))
727 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
728 "traditional C rejects the \"%.*s\" suffix",
729 (int) (limit - str), str);
730
731 /* A suffix for double is a GCC extension via decimal float support.
732 If the suffix also specifies an imaginary value we'll catch that
733 later. */
734 if ((result == CPP_N_MEDIUM0x0020) && CPP_PEDANTIC (pfile)((pfile)->opts.cpp_pedantic))
735 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
736 "suffix for double constant is a GCC extension");
737
738 /* Radix must be 10 for decimal floats. */
739 if ((result & CPP_N_DFLOAT0x4000) && radix != 10)
740 {
741 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
742 "invalid suffix \"%.*s\" with hexadecimal floating constant",
743 (int) (limit - str), str);
744 return CPP_N_INVALID0x0000;
745 }
746
747 if ((result & (CPP_N_FRACT0x100000 | CPP_N_ACCUM0x200000)) && CPP_PEDANTIC (pfile)((pfile)->opts.cpp_pedantic))
748 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
749 "fixed-point constants are a GCC extension");
750
751 if (result & CPP_N_DFLOAT0x4000)
752 {
753 if (CPP_PEDANTIC (pfile)((pfile)->opts.cpp_pedantic) && !CPP_OPTION (pfile, dfp_constants)((pfile)->opts.dfp_constants))
754 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
755 "decimal float constants are a C2X feature");
756 else if (CPP_OPTION (pfile, cpp_warn_c11_c2x_compat)((pfile)->opts.cpp_warn_c11_c2x_compat) > 0)
757 cpp_warning_with_line (pfile, CPP_W_C11_C2X_COMPAT,
758 virtual_location, 0,
759 "decimal float constants are a C2X feature");
760 }
761
762 result |= CPP_N_FLOATING0x0002;
763 }
764 else
765 {
766 result = interpret_int_suffix (pfile, str, limit - str);
767 if (result == 0)
768 {
769 if (CPP_OPTION (pfile, user_literals)((pfile)->opts.user_literals))
770 {
771 if (ud_suffix)
772 *ud_suffix = (const char *) str;
773 result = CPP_N_UNSIGNED0x1000 | CPP_N_LARGE0x0040 | CPP_N_USERDEF0x1000000;
774 }
775 else
776 {
777 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
778 "invalid suffix \"%.*s\" on integer constant",
779 (int) (limit - str), str);
780 return CPP_N_INVALID0x0000;
781 }
782 }
783
784 /* Traditional C only accepted the 'L' suffix.
785 Suppress warning about 'LL' with -Wno-long-long. */
786 if (CPP_WTRADITIONAL (pfile)((pfile)->opts.cpp_warn_traditional) && ! cpp_sys_macro_p (pfile))
787 {
788 int u_or_i = (result & (CPP_N_UNSIGNED0x1000|CPP_N_IMAGINARY0x2000));
789 int large = (result & CPP_N_WIDTH0x00F0) == CPP_N_LARGE0x0040
790 && CPP_OPTION (pfile, cpp_warn_long_long)((pfile)->opts.cpp_warn_long_long);
791
792 if (u_or_i || large)
793 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
794 virtual_location, 0,
795 "traditional C rejects the \"%.*s\" suffix",
796 (int) (limit - str), str);
797 }
798
799 if ((result & CPP_N_WIDTH0x00F0) == CPP_N_LARGE0x0040
800 && CPP_OPTION (pfile, cpp_warn_long_long)((pfile)->opts.cpp_warn_long_long))
801 {
802 const char *message = CPP_OPTION (pfile, cplusplus)((pfile)->opts.cplusplus)
803 ? N_("use of C++11 long long integer constant")"use of C++11 long long integer constant"
804 : N_("use of C99 long long integer constant")"use of C99 long long integer constant";
805
806 if (CPP_OPTION (pfile, c99)((pfile)->opts.c99))
807 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
808 0, message);
809 else
810 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
811 virtual_location, 0, message);
812 }
813
814 if ((result & CPP_N_SIZE_T0x2000000) == CPP_N_SIZE_T0x2000000
815 && !CPP_OPTION (pfile, size_t_literals)((pfile)->opts.size_t_literals))
816 {
817 const char *message = (result & CPP_N_UNSIGNED0x1000) == CPP_N_UNSIGNED0x1000
818 ? N_("use of C++23 %<size_t%> integer constant")"use of C++23 %<size_t%> integer constant"
819 : N_("use of C++23 %<make_signed_t<size_t>%> integer constant")"use of C++23 %<make_signed_t<size_t>%> integer constant";
820 cpp_warning_with_line (pfile, CPP_W_SIZE_T_LITERALS,
821 virtual_location, 0, message);
822 }
823
824 result |= CPP_N_INTEGER0x0001;
825 }
826
827 syntax_ok:
828 if ((result & CPP_N_IMAGINARY0x2000) && CPP_PEDANTIC (pfile)((pfile)->opts.cpp_pedantic))
829 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
830 "imaginary constants are a GCC extension");
831 if (radix == 2)
832 {
833 if (!CPP_OPTION (pfile, binary_constants)((pfile)->opts.binary_constants)
834 && CPP_PEDANTIC (pfile)((pfile)->opts.cpp_pedantic))
835 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
836 CPP_OPTION (pfile, cplusplus)((pfile)->opts.cplusplus)
837 ? N_("binary constants are a C++14 feature ""binary constants are a C++14 feature " "or GCC extension"
838 "or GCC extension")"binary constants are a C++14 feature " "or GCC extension"
839 : N_("binary constants are a C2X feature ""binary constants are a C2X feature " "or GCC extension"
840 "or GCC extension")"binary constants are a C2X feature " "or GCC extension");
841 else if (CPP_OPTION (pfile, cpp_warn_c11_c2x_compat)((pfile)->opts.cpp_warn_c11_c2x_compat) > 0)
842 cpp_warning_with_line (pfile, CPP_W_C11_C2X_COMPAT,
843 virtual_location, 0,
844 "binary constants are a C2X feature");
845 }
846
847 if (radix == 10)
848 result |= CPP_N_DECIMAL0x0100;
849 else if (radix == 16)
850 result |= CPP_N_HEX0x0200;
851 else if (radix == 2)
852 result |= CPP_N_BINARY0x0800;
853 else
854 result |= CPP_N_OCTAL0x0400;
855
856 return result;
857
858 syntax_error:
859 return CPP_N_INVALID0x0000;
860}
861
862/* cpp_interpret_integer converts an integer constant into a cpp_num,
863 of precision options->precision.
864
865 We do not provide any interface for decimal->float conversion,
866 because the preprocessor doesn't need it and we don't want to
867 drag in GCC's floating point emulator. */
868cpp_num
869cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
870 unsigned int type)
871{
872 const uchar *p, *end;
873 cpp_num result;
874
875 result.low = 0;
876 result.high = 0;
877 result.unsignedp = !!(type & CPP_N_UNSIGNED0x1000);
878 result.overflow = false;
879
880 p = token->val.str.text;
881 end = p + token->val.str.len;
882
883 /* Common case of a single digit. */
884 if (token->val.str.len == 1)
885 result.low = p[0] - '0';
886 else
887 {
888 cpp_num_part max;
889 size_t precision = CPP_OPTION (pfile, precision)((pfile)->opts.precision);
890 unsigned int base = 10, c = 0;
891 bool overflow = false;
892
893 if ((type & CPP_N_RADIX0x0F00) == CPP_N_OCTAL0x0400)
894 {
895 base = 8;
896 p++;
897 }
898 else if ((type & CPP_N_RADIX0x0F00) == CPP_N_HEX0x0200)
899 {
900 base = 16;
901 p += 2;
902 }
903 else if ((type & CPP_N_RADIX0x0F00) == CPP_N_BINARY0x0800)
904 {
905 base = 2;
906 p += 2;
907 }
908
909 /* We can add a digit to numbers strictly less than this without
910 needing the precision and slowness of double integers. */
911 max = ~(cpp_num_part) 0;
912 if (precision < PART_PRECISION(sizeof (cpp_num_part) * 8))
913 max >>= PART_PRECISION(sizeof (cpp_num_part) * 8) - precision;
914 max = (max - base + 1) / base + 1;
915
916 for (; p < end; p++)
917 {
918 c = *p;
919
920 if (ISDIGIT (c)(_sch_istable[(c) & 0xff] & (unsigned short)(_sch_isdigit
))
|| (base == 16 && ISXDIGIT (c)(_sch_istable[(c) & 0xff] & (unsigned short)(_sch_isxdigit
))
))
921 c = hex_value (c)((unsigned int) _hex_value[(unsigned char) (c)]);
922 else if (DIGIT_SEP (c)((c) == '\'' && ((pfile)->opts.digit_separators)))
923 continue;
924 else
925 break;
926
927 /* Strict inequality for when max is set to zero. */
928 if (result.low < max)
929 result.low = result.low * base + c;
930 else
931 {
932 result = append_digit (result, c, base, precision);
933 overflow |= result.overflow;
934 max = 0;
935 }
936 }
937
938 if (overflow && !(type & CPP_N_USERDEF0x1000000))
939 cpp_error (pfile, CPP_DL_PEDWARN,
940 "integer constant is too large for its type");
941 /* If too big to be signed, consider it unsigned. Only warn for
942 decimal numbers. Traditional numbers were always signed (but
943 we still honor an explicit U suffix); but we only have
944 traditional semantics in directives. */
945 else if (!result.unsignedp
946 && !(CPP_OPTION (pfile, traditional)((pfile)->opts.traditional)
947 && pfile->state.in_directive)
948 && !num_positive (result, precision))
949 {
950 /* This is for constants within the range of uintmax_t but
951 not that of intmax_t. For such decimal constants, a
952 diagnostic is required for C99 as the selected type must
953 be signed and not having a type is a constraint violation
954 (DR#298, TC3), so this must be a pedwarn. For C90,
955 unsigned long is specified to be used for a constant that
956 does not fit in signed long; if uintmax_t has the same
957 range as unsigned long this means only a warning is
958 appropriate here. C90 permits the preprocessor to use a
959 wider range than unsigned long in the compiler, so if
960 uintmax_t is wider than unsigned long no diagnostic is
961 required for such constants in preprocessor #if
962 expressions and the compiler will pedwarn for such
963 constants outside the range of unsigned long that reach
964 the compiler so a diagnostic is not required there
965 either; thus, pedwarn for C99 but use a plain warning for
966 C90. */
967 if (base == 10)
968 cpp_error (pfile, (CPP_OPTION (pfile, c99)((pfile)->opts.c99)
969 ? CPP_DL_PEDWARN
970 : CPP_DL_WARNING),
971 "integer constant is so large that it is unsigned");
972 result.unsignedp = true;
973 }
974 }
975
976 return result;
977}
978
979/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
980static cpp_num
981append_digit (cpp_num num, int digit, int base, size_t precision)
982{
983 cpp_num result;
984 unsigned int shift;
985 bool overflow;
986 cpp_num_part add_high, add_low;
987
988 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
989 need to worry about add_high overflowing. */
990 switch (base)
991 {
992 case 2:
993 shift = 1;
994 break;
995
996 case 16:
997 shift = 4;
998 break;
999
1000 default:
1001 shift = 3;
1002 }
1003 overflow = !!(num.high >> (PART_PRECISION(sizeof (cpp_num_part) * 8) - shift));
1004 result.high = num.high << shift;
1005 result.low = num.low << shift;
1006 result.high |= num.low >> (PART_PRECISION(sizeof (cpp_num_part) * 8) - shift);
1007 result.unsignedp = num.unsignedp;
1008
1009 if (base == 10)
1010 {
1011 add_low = num.low << 1;
1012 add_high = (num.high << 1) + (num.low >> (PART_PRECISION(sizeof (cpp_num_part) * 8) - 1));
1013 }
1014 else
1015 add_high = add_low = 0;
1016
1017 if (add_low + digit < add_low)
1018 add_high++;
1019 add_low += digit;
1020
1021 if (result.low + add_low < result.low)
1022 add_high++;
1023 if (result.high + add_high < result.high)
1024 overflow = true;
1025
1026 result.low += add_low;
1027 result.high += add_high;
1028 result.overflow = overflow;
1029
1030 /* The above code catches overflow of a cpp_num type. This catches
1031 overflow of the (possibly shorter) target precision. */
1032 num.low = result.low;
1033 num.high = result.high;
1034 result = num_trim (result, precision);
1035 if (!num_eq (result, num)(result.low == num.low && result.high == num.high))
1036 result.overflow = true;
1037
1038 return result;
1039}
1040
1041/* Handle meeting "defined" in a preprocessor expression. */
1042static cpp_num
1043parse_defined (cpp_reader *pfile)
1044{
1045 cpp_num result;
1046 int paren = 0;
1047 cpp_hashnode *node = 0;
1048 const cpp_token *token;
1049 cpp_context *initial_context = pfile->context;
1050
1051 /* Don't expand macros. */
1052 pfile->state.prevent_expansion++;
1053
1054 token = cpp_get_token (pfile);
1055 if (token->type == CPP_OPEN_PAREN)
1056 {
1057 paren = 1;
1058 token = cpp_get_token (pfile);
1059 }
1060
1061 if (token->type == CPP_NAME)
1062 {
1063 node = token->val.node.node;
1064 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
1065 {
1066 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
1067 node = 0;
1068 }
1069 }
1070 else
1071 {
1072 cpp_error (pfile, CPP_DL_ERROR,
1073 "operator \"defined\" requires an identifier");
1074 if (token->flags & NAMED_OP(1 << 4))
1075 {
1076 cpp_token op;
1077
1078 op.flags = 0;
1079 op.type = token->type;
1080 cpp_error (pfile, CPP_DL_ERROR,
1081 "(\"%s\" is an alternative token for \"%s\" in C++)",
1082 cpp_token_as_text (pfile, token),
1083 cpp_token_as_text (pfile, &op));
1084 }
1085 }
1086
1087 bool is_defined = false;
1088 if (node)
1089 {
1090 if ((pfile->context != initial_context
1091 || initial_context != &pfile->base_context)
1092 && CPP_OPTION (pfile, warn_expansion_to_defined)((pfile)->opts.warn_expansion_to_defined))
1093 cpp_pedwarning (pfile, CPP_W_EXPANSION_TO_DEFINED,
1094 "this use of \"defined\" may not be portable");
1095 is_defined = _cpp_defined_macro_p (node);
1096 if (!_cpp_maybe_notify_macro_use (pfile, node, token->src_loc))
1097 /* It wasn't a macro after all. */
1098 is_defined = false;
1099 _cpp_mark_macro_used (node)(cpp_user_macro_p (node) ? (node)->value.macro->used = 1
: 0)
;
1100
1101 /* A possible controlling macro of the form #if !defined ().
1102 _cpp_parse_expr checks there was no other junk on the line. */
1103 pfile->mi_ind_cmacro = node;
1104 }
1105
1106 pfile->state.prevent_expansion--;
1107
1108 /* Do not treat conditional macros as being defined. This is due to the
1109 powerpc port using conditional macros for 'vector', 'bool', and 'pixel'
1110 to act as conditional keywords. This messes up tests like #ifndef
1111 bool. */
1112 result.unsignedp = false;
1113 result.high = 0;
1114 result.overflow = false;
1115 result.low = is_defined;
1116 return result;
1117}
1118
1119/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1120 number or character constant, or the result of the "defined" or "#"
1121 operators). */
1122static cpp_num
1123eval_token (cpp_reader *pfile, const cpp_token *token,
1124 location_t virtual_location)
1125{
1126 cpp_num result;
1127 unsigned int temp;
1128 int unsignedp = 0;
1129
1130 result.unsignedp = false;
1131 result.overflow = false;
1132
1133 switch (token->type)
1134 {
1135 case CPP_NUMBER:
1136 temp = cpp_classify_number (pfile, token, NULL__null, virtual_location);
1137 if (temp & CPP_N_USERDEF0x1000000)
1138 cpp_error (pfile, CPP_DL_ERROR,
1139 "user-defined literal in preprocessor expression");
1140 switch (temp & CPP_N_CATEGORY0x000F)
1141 {
1142 case CPP_N_FLOATING0x0002:
1143 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1144 "floating constant in preprocessor expression");
1145 break;
1146 case CPP_N_INTEGER0x0001:
1147 if (!(temp & CPP_N_IMAGINARY0x2000))
1148 return cpp_interpret_integer (pfile, token, temp);
1149 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1150 "imaginary number in preprocessor expression");
1151 break;
1152
1153 case CPP_N_INVALID0x0000:
1154 /* Error already issued. */
1155 break;
1156 }
1157 result.high = result.low = 0;
1158 break;
1159
1160 case CPP_WCHAR:
1161 case CPP_CHAR:
1162 case CPP_CHAR16:
1163 case CPP_CHAR32:
1164 case CPP_UTF8CHAR:
1165 {
1166 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1167 &temp, &unsignedp);
1168
1169 result.high = 0;
1170 result.low = cc;
1171 /* Sign-extend the result if necessary. */
1172 if (!unsignedp && (cppchar_signed_t) cc < 0)
1173 {
1174 if (PART_PRECISION(sizeof (cpp_num_part) * 8) > BITS_PER_CPPCHAR_T(8 * sizeof (cppchar_t)))
1175 result.low |= ~(~(cpp_num_part) 0
1176 >> (PART_PRECISION(sizeof (cpp_num_part) * 8) - BITS_PER_CPPCHAR_T(8 * sizeof (cppchar_t))));
1177 result.high = ~(cpp_num_part) 0;
1178 result = num_trim (result, CPP_OPTION (pfile, precision)((pfile)->opts.precision));
1179 }
1180 }
1181 break;
1182
1183 case CPP_NAME:
1184 if (token->val.node.node == pfile->spec_nodes.n_defined)
1185 return parse_defined (pfile);
1186 else if (CPP_OPTION (pfile, cplusplus)((pfile)->opts.cplusplus)
1187 && (token->val.node.node == pfile->spec_nodes.n_true
1188 || token->val.node.node == pfile->spec_nodes.n_false))
1189 {
1190 result.high = 0;
1191 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1192 }
1193 else
1194 {
1195 result.high = 0;
1196 result.low = 0;
1197 if (CPP_OPTION (pfile, warn_undef)((pfile)->opts.warn_undef) && !pfile->state.skip_eval)
1198 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1199 "\"%s\" is not defined, evaluates to 0",
1200 NODE_NAME (token->val.node.node)(((&(token->val.node.node)->ident))->str));
1201 }
1202 break;
1203
1204 case CPP_HASH:
1205 if (!pfile->state.skipping)
1206 {
1207 /* A pedantic warning takes precedence over a deprecated
1208 warning here. */
1209 if (CPP_PEDANTIC (pfile)((pfile)->opts.cpp_pedantic))
1210 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1211 virtual_location, 0,
1212 "assertions are a GCC extension");
1213 else if (CPP_OPTION (pfile, cpp_warn_deprecated)((pfile)->opts.cpp_warn_deprecated))
1214 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1215 "assertions are a deprecated extension");
1216 }
1217 _cpp_test_assertion (pfile, &temp);
1218 result.high = 0;
1219 result.low = temp;
1220 break;
1221
1222 default:
1223 abort ()fancy_abort ("/home/marxin/BIG/buildbot/buildworker/marxinbox-gcc-clang-static-analyzer/build/libcpp/expr.c"
, 1223, __FUNCTION__)
;
1224 }
1225
1226 result.unsignedp = !!unsignedp;
1227 return result;
1228}
1229
1230/* Operator precedence and flags table.
1231
1232After an operator is returned from the lexer, if it has priority less
1233than the operator on the top of the stack, we reduce the stack by one
1234operator and repeat the test. Since equal priorities do not reduce,
1235this is naturally right-associative.
1236
1237We handle left-associative operators by decrementing the priority of
1238just-lexed operators by one, but retaining the priority of operators
1239already on the stack.
1240
1241The remaining cases are '(' and ')'. We handle '(' by skipping the
1242reduction phase completely. ')' is given lower priority than
1243everything else, including '(', effectively forcing a reduction of the
1244parenthesized expression. If there is a matching '(', the routine
1245reduce() exits immediately. If the normal exit route sees a ')', then
1246there cannot have been a matching '(' and an error message is output.
1247
1248The parser assumes all shifted operators require a left operand unless
1249the flag NO_L_OPERAND is set. These semantics are automatic; any
1250extra semantics need to be handled with operator-specific code. */
1251
1252/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1253 operand changes because of integer promotions. */
1254#define NO_L_OPERAND(1 << 0) (1 << 0)
1255#define LEFT_ASSOC(1 << 1) (1 << 1)
1256#define CHECK_PROMOTION(1 << 2) (1 << 2)
1257
1258/* Operator to priority map. Must be in the same order as the first
1259 N entries of enum cpp_ttype. */
1260static const struct cpp_operator
1261{
1262 uchar prio;
1263 uchar flags;
1264} optab[] =
1265{
1266 /* EQ */ {0, 0}, /* Shouldn't happen. */
1267 /* NOT */ {16, NO_L_OPERAND(1 << 0)},
1268 /* GREATER */ {12, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1269 /* LESS */ {12, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1270 /* PLUS */ {14, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1271 /* MINUS */ {14, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1272 /* MULT */ {15, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1273 /* DIV */ {15, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1274 /* MOD */ {15, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1275 /* AND */ {9, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1276 /* OR */ {7, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1277 /* XOR */ {8, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1278 /* RSHIFT */ {13, LEFT_ASSOC(1 << 1)},
1279 /* LSHIFT */ {13, LEFT_ASSOC(1 << 1)},
1280
1281 /* COMPL */ {16, NO_L_OPERAND(1 << 0)},
1282 /* AND_AND */ {6, LEFT_ASSOC(1 << 1)},
1283 /* OR_OR */ {5, LEFT_ASSOC(1 << 1)},
1284 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1285 However, there are some special cases for these in reduce(). */
1286 /* QUERY */ {4, 0},
1287 /* COLON */ {4, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1288 /* COMMA */ {4, LEFT_ASSOC(1 << 1)},
1289 /* OPEN_PAREN */ {1, NO_L_OPERAND(1 << 0)},
1290 /* CLOSE_PAREN */ {0, 0},
1291 /* EOF */ {0, 0},
1292 /* EQ_EQ */ {11, LEFT_ASSOC(1 << 1)},
1293 /* NOT_EQ */ {11, LEFT_ASSOC(1 << 1)},
1294 /* GREATER_EQ */ {12, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1295 /* LESS_EQ */ {12, LEFT_ASSOC(1 << 1) | CHECK_PROMOTION(1 << 2)},
1296 /* UPLUS */ {16, NO_L_OPERAND(1 << 0)},
1297 /* UMINUS */ {16, NO_L_OPERAND(1 << 0)}
1298};
1299
1300/* Parse and evaluate a C expression, reading from PFILE.
1301 Returns the truth value of the expression.
1302
1303 The implementation is an operator precedence parser, i.e. a
1304 bottom-up parser, using a stack for not-yet-reduced tokens.
1305
1306 The stack base is op_stack, and the current stack pointer is 'top'.
1307 There is a stack element for each operator (only), and the most
1308 recently pushed operator is 'top->op'. An operand (value) is
1309 stored in the 'value' field of the stack element of the operator
1310 that precedes it. */
1311bool
1312_cpp_parse_expr (cpp_reader *pfile, bool is_if)
1313{
1314 struct op *top = pfile->op_stack;
1315 unsigned int lex_count;
1316 bool saw_leading_not, want_value = true;
1317 location_t virtual_location = 0;
1318
1319 pfile->state.skip_eval = 0;
1320
1321 /* Set up detection of #if ! defined(). */
1322 pfile->mi_ind_cmacro = 0;
1323 saw_leading_not = false;
1324 lex_count = 0;
1325
1326 /* Lowest priority operator prevents further reductions. */
1327 top->op = CPP_EOF;
1328
1329 for (;;)
1
Loop condition is true. Entering loop body
17
Loop condition is true. Entering loop body
1330 {
1331 struct op op;
1332
1333 lex_count++;
1334 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1335 op.op = op.token->type;
1336 op.loc = virtual_location;
1337
1338 switch (op.op)
2
Control jumps to the 'default' case at line 1369
18
Control jumps to 'case CPP_NOT:' at line 1357
1339 {
1340 /* These tokens convert into values. */
1341 case CPP_NUMBER:
1342 case CPP_CHAR:
1343 case CPP_WCHAR:
1344 case CPP_CHAR16:
1345 case CPP_CHAR32:
1346 case CPP_UTF8CHAR:
1347 case CPP_NAME:
1348 case CPP_HASH:
1349 if (!want_value)
1350 SYNTAX_ERROR2_AT (op.loc,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "missing binary operator before token \"%s\""
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
1351 "missing binary operator before token \"%s\"",do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "missing binary operator before token \"%s\""
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
1352 cpp_token_as_text (pfile, op.token))do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "missing binary operator before token \"%s\""
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
;
1353 want_value = false;
1354 top->value = eval_token (pfile, op.token, op.loc);
1355 continue;
1356
1357 case CPP_NOT:
1358 saw_leading_not = lex_count == 1;
1359 break;
19
Execution continues on line 1378
1360 case CPP_PLUS:
1361 if (want_value)
1362 op.op = CPP_UPLUS((enum cpp_ttype) (CPP_LAST_CPP_OP + 1));
1363 break;
1364 case CPP_MINUS:
1365 if (want_value)
1366 op.op = CPP_UMINUS((enum cpp_ttype) (CPP_LAST_CPP_OP + 2));
1367 break;
1368
1369 default:
1370 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
3
Assuming field 'op' is > CPP_EQ
4
Assuming field 'op' is < CPP_PLUS_EQ
5
Taking false branch
1371 SYNTAX_ERROR2_AT (op.loc,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "token \"%s\" is not valid in preprocessor expressions"
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
1372 "token \"%s\" is not valid in preprocessor expressions",do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "token \"%s\" is not valid in preprocessor expressions"
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
1373 cpp_token_as_text (pfile, op.token))do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "token \"%s\" is not valid in preprocessor expressions"
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
;
1374 break;
6
Execution continues on line 1378
1375 }
1376
1377 /* Check we have a value or operator as appropriate. */
1378 if (optab[op.op].flags & NO_L_OPERAND(1 << 0))
7
Assuming the condition is true
8
Taking true branch
20
Assuming the condition is true
21
Taking true branch
1379 {
1380 if (!want_value
8.1
'want_value' is true
21.1
'want_value' is true
)
9
Taking false branch
22
Taking false branch
1381 SYNTAX_ERROR2_AT (op.loc,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "missing binary operator before token \"%s\""
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
1382 "missing binary operator before token \"%s\"",do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "missing binary operator before token \"%s\""
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
1383 cpp_token_as_text (pfile, op.token))do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "missing binary operator before token \"%s\""
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
;
1384 }
1385 else if (want_value)
1386 {
1387 /* We want a number (or expression) and haven't got one.
1388 Try to emit a specific diagnostic. */
1389 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1390 SYNTAX_ERROR_AT (op.loc,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "missing expression between '(' and ')'"
); goto syntax_error; } while(0)
1391 "missing expression between '(' and ')'")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "missing expression between '(' and ')'"
); goto syntax_error; } while(0)
;
1392
1393 if (op.op == CPP_EOF && top->op == CPP_EOF)
1394 SYNTAX_ERROR2_AT (op.loc,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "%s with no expression"
, is_if ? "#if" : "#elif"); goto syntax_error; } while(0)
1395 "%s with no expression", is_if ? "#if" : "#elif")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "%s with no expression"
, is_if ? "#if" : "#elif"); goto syntax_error; } while(0)
;
1396
1397 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1398 SYNTAX_ERROR2_AT (op.loc,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "operator '%s' has no right operand"
, cpp_token_as_text (pfile, top->token)); goto syntax_error
; } while(0)
1399 "operator '%s' has no right operand",do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "operator '%s' has no right operand"
, cpp_token_as_text (pfile, top->token)); goto syntax_error
; } while(0)
1400 cpp_token_as_text (pfile, top->token))do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "operator '%s' has no right operand"
, cpp_token_as_text (pfile, top->token)); goto syntax_error
; } while(0)
;
1401 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1402 /* Complain about missing paren during reduction. */;
1403 else
1404 SYNTAX_ERROR2_AT (op.loc,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "operator '%s' has no left operand"
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
1405 "operator '%s' has no left operand",do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "operator '%s' has no left operand"
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
1406 cpp_token_as_text (pfile, op.token))do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, "operator '%s' has no left operand"
, cpp_token_as_text (pfile, op.token)); goto syntax_error; } while
(0)
;
1407 }
1408
1409 top = reduce (pfile, top, op.op);
23
Calling 'reduce'
1410 if (!top
9.1
'top' is non-null
)
10
Taking false branch
1411 goto syntax_error;
1412
1413 if (op.op == CPP_EOF)
11
Assuming field 'op' is not equal to CPP_EOF
12
Taking false branch
1414 break;
1415
1416 switch (op.op)
13
Control jumps to the 'default' case at line 1437
1417 {
1418 case CPP_CLOSE_PAREN:
1419 continue;
1420 case CPP_OR_OR:
1421 if (!num_zerop (top->value)((top->value.low | top->value.high) == 0))
1422 pfile->state.skip_eval++;
1423 break;
1424 case CPP_AND_AND:
1425 case CPP_QUERY:
1426 if (num_zerop (top->value)((top->value.low | top->value.high) == 0))
1427 pfile->state.skip_eval++;
1428 break;
1429 case CPP_COLON:
1430 if (top->op != CPP_QUERY)
1431 SYNTAX_ERROR_AT (op.loc,do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, " ':' without preceding '?'"
); goto syntax_error; } while(0)
1432 " ':' without preceding '?'")do { cpp_error_with_line (pfile, CPP_DL_ERROR, (op.loc), 0, " ':' without preceding '?'"
); goto syntax_error; } while(0)
;
1433 if (!num_zerop (top[-1].value)((top[-1].value.low | top[-1].value.high) == 0)) /* Was '?' condition true? */
1434 pfile->state.skip_eval++;
1435 else
1436 pfile->state.skip_eval--;
1437 default:
1438 break;
14
Execution continues on line 1441
1439 }
1440
1441 want_value = true;
1442
1443 /* Check for and handle stack overflow. */
1444 if (++top == pfile->op_limit)
15
Assuming the condition is false
16
Taking false branch
1445 top = _cpp_expand_op_stack (pfile);
1446
1447 top->op = op.op;
1448 top->token = op.token;
1449 top->loc = op.loc;
1450 }
1451
1452 /* The controlling macro expression is only valid if we called lex 3
1453 times: <!> <defined expression> and <EOF>. push_conditional ()
1454 checks that we are at top-of-file. */
1455 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1456 pfile->mi_ind_cmacro = 0;
1457
1458 if (top != pfile->op_stack)
1459 {
1460 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1461 "unbalanced stack in %s",
1462 is_if ? "#if" : "#elif");
1463 syntax_error:
1464 return false; /* Return false on syntax error. */
1465 }
1466
1467 return !num_zerop (top->value)((top->value.low | top->value.high) == 0);
1468}
1469
1470/* Reduce the operator / value stack if possible, in preparation for
1471 pushing operator OP. Returns NULL on error, otherwise the top of
1472 the stack. */
1473static struct op *
1474reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1475{
1476 unsigned int prio;
1477
1478 if (top->op
23.1
Field 'op' is > CPP_EQ
<= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
24
Taking false branch
1479 {
1480 bad_op:
1481 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1482 return 0;
1483 }
1484
1485 if (op
24.1
'op' is not equal to CPP_OPEN_PAREN
== CPP_OPEN_PAREN)
25
Taking false branch
1486 return top;
1487
1488 /* Decrement the priority of left-associative operators to force a
1489 reduction with operators of otherwise equal priority. */
1490 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC(1 << 1)) != 0);
26
Assuming the condition is false
1491 while (prio < optab[top->op].prio)
27
Assuming 'prio' is < field 'prio'
28
Loop condition is true. Entering loop body
1492 {
1493 if (CPP_OPTION (pfile, warn_num_sign_change)((pfile)->opts.warn_num_sign_change)
29
Assuming field 'warn_num_sign_change' is 0
1494 && optab[top->op].flags & CHECK_PROMOTION(1 << 2))
1495 check_promotion (pfile, top);
1496
1497 switch (top->op)
30
Control jumps to 'case CPP_DIV:' at line 1546
1498 {
1499 case CPP_UPLUS((enum cpp_ttype) (CPP_LAST_CPP_OP + 1)):
1500 case CPP_UMINUS((enum cpp_ttype) (CPP_LAST_CPP_OP + 2)):
1501 case CPP_NOT:
1502 case CPP_COMPL:
1503 top[-1].value = num_unary_op (pfile, top->value, top->op);
1504 top[-1].loc = top->loc;
1505 break;
1506
1507 case CPP_PLUS:
1508 case CPP_MINUS:
1509 case CPP_RSHIFT:
1510 case CPP_LSHIFT:
1511 case CPP_COMMA:
1512 top[-1].value = num_binary_op (pfile, top[-1].value,
1513 top->value, top->op);
1514 top[-1].loc = top->loc;
1515 break;
1516
1517 case CPP_GREATER:
1518 case CPP_LESS:
1519 case CPP_GREATER_EQ:
1520 case CPP_LESS_EQ:
1521 top[-1].value
1522 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1523 top[-1].loc = top->loc;
1524 break;
1525
1526 case CPP_EQ_EQ:
1527 case CPP_NOT_EQ:
1528 top[-1].value
1529 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1530 top[-1].loc = top->loc;
1531 break;
1532
1533 case CPP_AND:
1534 case CPP_OR:
1535 case CPP_XOR:
1536 top[-1].value
1537 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1538 top[-1].loc = top->loc;
1539 break;
1540
1541 case CPP_MULT:
1542 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1543 top[-1].loc = top->loc;
1544 break;
1545
1546 case CPP_DIV:
1547 case CPP_MOD:
1548 top[-1].value = num_div_op (pfile, top[-1].value,
31
Calling 'num_div_op'
1549 top->value, top->op, top->loc);
1550 top[-1].loc = top->loc;
1551 break;
1552
1553 case CPP_OR_OR:
1554 top--;
1555 if (!num_zerop (top->value)((top->value.low | top->value.high) == 0))
1556 pfile->state.skip_eval--;
1557 top->value.low = (!num_zerop (top->value)((top->value.low | top->value.high) == 0)
1558 || !num_zerop (top[1].value)((top[1].value.low | top[1].value.high) == 0));
1559 top->value.high = 0;
1560 top->value.unsignedp = false;
1561 top->value.overflow = false;
1562 top->loc = top[1].loc;
1563 continue;
1564
1565 case CPP_AND_AND:
1566 top--;
1567 if (num_zerop (top->value)((top->value.low | top->value.high) == 0))
1568 pfile->state.skip_eval--;
1569 top->value.low = (!num_zerop (top->value)((top->value.low | top->value.high) == 0)
1570 && !num_zerop (top[1].value)((top[1].value.low | top[1].value.high) == 0));
1571 top->value.high = 0;
1572 top->value.unsignedp = false;
1573 top->value.overflow = false;
1574 top->loc = top[1].loc;
1575 continue;
1576
1577 case CPP_OPEN_PAREN:
1578 if (op != CPP_CLOSE_PAREN)
1579 {
1580 cpp_error_with_line (pfile, CPP_DL_ERROR,
1581 top->token->src_loc,
1582 0, "missing ')' in expression");
1583 return 0;
1584 }
1585 top--;
1586 top->value = top[1].value;
1587 top->loc = top[1].loc;
1588 return top;
1589
1590 case CPP_COLON:
1591 top -= 2;
1592 if (!num_zerop (top->value)((top->value.low | top->value.high) == 0))
1593 {
1594 pfile->state.skip_eval--;
1595 top->value = top[1].value;
1596 top->loc = top[1].loc;
1597 }
1598 else
1599 {
1600 top->value = top[2].value;
1601 top->loc = top[2].loc;
1602 }
1603 top->value.unsignedp = (top[1].value.unsignedp
1604 || top[2].value.unsignedp);
1605 continue;
1606
1607 case CPP_QUERY:
1608 /* COMMA and COLON should not reduce a QUERY operator. */
1609 if (op == CPP_COMMA || op == CPP_COLON)
1610 return top;
1611 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1612 return 0;
1613
1614 default:
1615 goto bad_op;
1616 }
1617
1618 top--;
1619 if (top->value.overflow && !pfile->state.skip_eval)
1620 cpp_error (pfile, CPP_DL_PEDWARN,
1621 "integer overflow in preprocessor expression");
1622 }
1623
1624 if (op == CPP_CLOSE_PAREN)
1625 {
1626 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1627 return 0;
1628 }
1629
1630 return top;
1631}
1632
1633/* Returns the position of the old top of stack after expansion. */
1634struct op *
1635_cpp_expand_op_stack (cpp_reader *pfile)
1636{
1637 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1638 size_t new_size = old_size * 2 + 20;
1639
1640 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size)((struct op *) xrealloc ((void *) (pfile->op_stack), sizeof
(struct op) * (new_size)))
;
1641 pfile->op_limit = pfile->op_stack + new_size;
1642
1643 return pfile->op_stack + old_size;
1644}
1645
1646/* Emits a warning if the effective sign of either operand of OP
1647 changes because of integer promotions. */
1648static void
1649check_promotion (cpp_reader *pfile, const struct op *op)
1650{
1651 if (op->value.unsignedp == op[-1].value.unsignedp)
1652 return;
1653
1654 if (op->value.unsignedp)
1655 {
1656 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)((pfile)->opts.precision)))
1657 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1658 "the left operand of \"%s\" changes sign when promoted",
1659 cpp_token_as_text (pfile, op->token));
1660 }
1661 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)((pfile)->opts.precision)))
1662 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1663 "the right operand of \"%s\" changes sign when promoted",
1664 cpp_token_as_text (pfile, op->token));
1665}
1666
1667/* Clears the unused high order bits of the number pointed to by PNUM. */
1668static cpp_num
1669num_trim (cpp_num num, size_t precision)
1670{
1671 if (precision > PART_PRECISION(sizeof (cpp_num_part) * 8))
1672 {
1673 precision -= PART_PRECISION(sizeof (cpp_num_part) * 8);
1674 if (precision < PART_PRECISION(sizeof (cpp_num_part) * 8))
1675 num.high &= ((cpp_num_part) 1 << precision) - 1;
1676 }
1677 else
1678 {
1679 if (precision < PART_PRECISION(sizeof (cpp_num_part) * 8))
1680 num.low &= ((cpp_num_part) 1 << precision) - 1;
1681 num.high = 0;
1682 }
1683
1684 return num;
1685}
1686
1687/* True iff A (presumed signed) >= 0. */
1688static bool
1689num_positive (cpp_num num, size_t precision)
1690{
1691 if (precision > PART_PRECISION(sizeof (cpp_num_part) * 8))
1692 {
1693 precision -= PART_PRECISION(sizeof (cpp_num_part) * 8);
1694 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1695 }
1696
1697 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1698}
1699
1700/* Sign extend a number, with PRECISION significant bits and all
1701 others assumed clear, to fill out a cpp_num structure. */
1702cpp_num
1703cpp_num_sign_extend (cpp_num num, size_t precision)
1704{
1705 if (!num.unsignedp)
1706 {
1707 if (precision > PART_PRECISION(sizeof (cpp_num_part) * 8))
1708 {
1709 precision -= PART_PRECISION(sizeof (cpp_num_part) * 8);
1710 if (precision < PART_PRECISION(sizeof (cpp_num_part) * 8)
1711 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1712 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION(sizeof (cpp_num_part) * 8) - precision));
1713 }
1714 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1715 {
1716 if (precision < PART_PRECISION(sizeof (cpp_num_part) * 8))
1717 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION(sizeof (cpp_num_part) * 8) - precision));
1718 num.high = ~(cpp_num_part) 0;
1719 }
1720 }
1721
1722 return num;
1723}
1724
1725/* Returns the negative of NUM. */
1726static cpp_num
1727num_negate (cpp_num num, size_t precision)
1728{
1729 cpp_num copy;
1730
1731 copy = num;
1732 num.high = ~num.high;
1733 num.low = ~num.low;
1734 if (++num.low == 0)
1735 num.high++;
1736 num = num_trim (num, precision);
1737 num.overflow = (!num.unsignedp && num_eq (num, copy)(num.low == copy.low && num.high == copy.high) && !num_zerop (num)((num.low | num.high) == 0));
1738
1739 return num;
1740}
1741
1742/* Returns true if A >= B. */
1743static bool
1744num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1745{
1746 bool unsignedp;
1747
1748 unsignedp = pa.unsignedp
45.1
Field 'unsignedp' is true
|| pb.unsignedp;
1749
1750 if (!unsignedp
45.2
'unsignedp' is true
)
46
Taking false branch
1751 {
1752 /* Both numbers have signed type. If they are of different
1753 sign, the answer is the sign of A. */
1754 unsignedp = num_positive (pa, precision);
1755
1756 if (unsignedp != num_positive (pb, precision))
1757 return unsignedp;
1758
1759 /* Otherwise we can do an unsigned comparison. */
1760 }
1761
1762 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
47
Assuming 'pa.high' is > 'pb.high'
48
Returning the value 1, which participates in a condition later
1763}
1764
1765/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1766static cpp_num
1767num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED__attribute__ ((__unused__)),
1768 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1769{
1770 lhs.overflow = false;
1771 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1772
1773 /* As excess precision is zeroed, there is no need to num_trim () as
1774 these operations cannot introduce a set bit there. */
1775 if (op == CPP_AND)
1776 {
1777 lhs.low &= rhs.low;
1778 lhs.high &= rhs.high;
1779 }
1780 else if (op == CPP_OR)
1781 {
1782 lhs.low |= rhs.low;
1783 lhs.high |= rhs.high;
1784 }
1785 else
1786 {
1787 lhs.low ^= rhs.low;
1788 lhs.high ^= rhs.high;
1789 }
1790
1791 return lhs;
1792}
1793
1794/* Returns LHS OP RHS, where OP is an inequality. */
1795static cpp_num
1796num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1797 enum cpp_ttype op)
1798{
1799 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision)((pfile)->opts.precision));
1800
1801 if (op == CPP_GREATER_EQ)
1802 lhs.low = gte;
1803 else if (op == CPP_LESS)
1804 lhs.low = !gte;
1805 else if (op == CPP_GREATER)
1806 lhs.low = gte && !num_eq (lhs, rhs)(lhs.low == rhs.low && lhs.high == rhs.high);
1807 else /* CPP_LESS_EQ. */
1808 lhs.low = !gte || num_eq (lhs, rhs)(lhs.low == rhs.low && lhs.high == rhs.high);
1809
1810 lhs.high = 0;
1811 lhs.overflow = false;
1812 lhs.unsignedp = false;
1813 return lhs;
1814}
1815
1816/* Returns LHS OP RHS, where OP is == or !=. */
1817static cpp_num
1818num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED__attribute__ ((__unused__)),
1819 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1820{
1821 /* Work around a 3.0.4 bug; see PR 6950. */
1822 bool eq = num_eq (lhs, rhs)(lhs.low == rhs.low && lhs.high == rhs.high);
1823 if (op == CPP_NOT_EQ)
1824 eq = !eq;
1825 lhs.low = eq;
1826 lhs.high = 0;
1827 lhs.overflow = false;
1828 lhs.unsignedp = false;
1829 return lhs;
1830}
1831
1832/* Shift NUM, of width PRECISION, right by N bits. */
1833static cpp_num
1834num_rshift (cpp_num num, size_t precision, size_t n)
1835{
1836 cpp_num_part sign_mask;
1837 bool x = num_positive (num, precision);
1838
1839 if (num.unsignedp || x)
1840 sign_mask = 0;
1841 else
1842 sign_mask = ~(cpp_num_part) 0;
1843
1844 if (n >= precision)
1845 num.high = num.low = sign_mask;
1846 else
1847 {
1848 /* Sign-extend. */
1849 if (precision < PART_PRECISION(sizeof (cpp_num_part) * 8))
1850 num.high = sign_mask, num.low |= sign_mask << precision;
1851 else if (precision < 2 * PART_PRECISION(sizeof (cpp_num_part) * 8))
1852 num.high |= sign_mask << (precision - PART_PRECISION(sizeof (cpp_num_part) * 8));
1853
1854 if (n >= PART_PRECISION(sizeof (cpp_num_part) * 8))
1855 {
1856 n -= PART_PRECISION(sizeof (cpp_num_part) * 8);
1857 num.low = num.high;
1858 num.high = sign_mask;
1859 }
1860
1861 if (n)
1862 {
1863 num.low = (num.low >> n) | (num.high << (PART_PRECISION(sizeof (cpp_num_part) * 8) - n));
1864 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION(sizeof (cpp_num_part) * 8) - n));
1865 }
1866 }
1867
1868 num = num_trim (num, precision);
1869 num.overflow = false;
1870 return num;
1871}
1872
1873/* Shift NUM, of width PRECISION, left by N bits. */
1874static cpp_num
1875num_lshift (cpp_num num, size_t precision, size_t n)
1876{
1877 if (n >= precision)
1878 {
1879 num.overflow = !num.unsignedp && !num_zerop (num)((num.low | num.high) == 0);
1880 num.high = num.low = 0;
1881 }
1882 else
1883 {
1884 cpp_num orig, maybe_orig;
1885 size_t m = n;
1886
1887 orig = num;
1888 if (m >= PART_PRECISION(sizeof (cpp_num_part) * 8))
1889 {
1890 m -= PART_PRECISION(sizeof (cpp_num_part) * 8);
1891 num.high = num.low;
1892 num.low = 0;
1893 }
1894 if (m)
1895 {
1896 num.high = (num.high << m) | (num.low >> (PART_PRECISION(sizeof (cpp_num_part) * 8) - m));
1897 num.low <<= m;
1898 }
1899 num = num_trim (num, precision);
1900
1901 if (num.unsignedp)
1902 num.overflow = false;
1903 else
1904 {
1905 maybe_orig = num_rshift (num, precision, n);
1906 num.overflow = !num_eq (orig, maybe_orig)(orig.low == maybe_orig.low && orig.high == maybe_orig
.high)
;
1907 }
1908 }
1909
1910 return num;
1911}
1912
1913/* The four unary operators: +, -, ! and ~. */
1914static cpp_num
1915num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1916{
1917 switch (op)
1918 {
1919 case CPP_UPLUS((enum cpp_ttype) (CPP_LAST_CPP_OP + 1)):
1920 if (CPP_WTRADITIONAL (pfile)((pfile)->opts.cpp_warn_traditional) && !pfile->state.skip_eval)
1921 cpp_warning (pfile, CPP_W_TRADITIONAL,
1922 "traditional C rejects the unary plus operator");
1923 num.overflow = false;
1924 break;
1925
1926 case CPP_UMINUS((enum cpp_ttype) (CPP_LAST_CPP_OP + 2)):
1927 num = num_negate (num, CPP_OPTION (pfile, precision)((pfile)->opts.precision));
1928 break;
1929
1930 case CPP_COMPL:
1931 num.high = ~num.high;
1932 num.low = ~num.low;
1933 num = num_trim (num, CPP_OPTION (pfile, precision)((pfile)->opts.precision));
1934 num.overflow = false;
1935 break;
1936
1937 default: /* case CPP_NOT: */
1938 num.low = num_zerop (num)((num.low | num.high) == 0);
1939 num.high = 0;
1940 num.overflow = false;
1941 num.unsignedp = false;
1942 break;
1943 }
1944
1945 return num;
1946}
1947
1948/* The various binary operators. */
1949static cpp_num
1950num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1951{
1952 cpp_num result;
1953 size_t precision = CPP_OPTION (pfile, precision)((pfile)->opts.precision);
1954 size_t n;
1955
1956 switch (op)
1957 {
1958 /* Shifts. */
1959 case CPP_LSHIFT:
1960 case CPP_RSHIFT:
1961 if (!rhs.unsignedp && !num_positive (rhs, precision))
1962 {
1963 /* A negative shift is a positive shift the other way. */
1964 if (op == CPP_LSHIFT)
1965 op = CPP_RSHIFT;
1966 else
1967 op = CPP_LSHIFT;
1968 rhs = num_negate (rhs, precision);
1969 }
1970 if (rhs.high)
1971 n = ~0; /* Maximal. */
1972 else
1973 n = rhs.low;
1974 if (op == CPP_LSHIFT)
1975 lhs = num_lshift (lhs, precision, n);
1976 else
1977 lhs = num_rshift (lhs, precision, n);
1978 break;
1979
1980 /* Arithmetic. */
1981 case CPP_MINUS:
1982 result.low = lhs.low - rhs.low;
1983 result.high = lhs.high - rhs.high;
1984 if (result.low > lhs.low)
1985 result.high--;
1986 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1987 result.overflow = false;
1988
1989 result = num_trim (result, precision);
1990 if (!result.unsignedp)
1991 {
1992 bool lhsp = num_positive (lhs, precision);
1993 result.overflow = (lhsp != num_positive (rhs, precision)
1994 && lhsp != num_positive (result, precision));
1995 }
1996 return result;
1997
1998 case CPP_PLUS:
1999 result.low = lhs.low + rhs.low;
2000 result.high = lhs.high + rhs.high;
2001 if (result.low < lhs.low)
2002 result.high++;
2003 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
2004 result.overflow = false;
2005
2006 result = num_trim (result, precision);
2007 if (!result.unsignedp)
2008 {
2009 bool lhsp = num_positive (lhs, precision);
2010 result.overflow = (lhsp == num_positive (rhs, precision)
2011 && lhsp != num_positive (result, precision));
2012 }
2013 return result;
2014
2015 /* Comma. */
2016 default: /* case CPP_COMMA: */
2017 if (CPP_PEDANTIC (pfile)((pfile)->opts.cpp_pedantic) && (!CPP_OPTION (pfile, c99)((pfile)->opts.c99)
2018 || !pfile->state.skip_eval))
2019 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2020 "comma operator in operand of #if");
2021 lhs = rhs;
2022 break;
2023 }
2024
2025 return lhs;
2026}
2027
2028/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
2029 cannot overflow. */
2030static cpp_num
2031num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
2032{
2033 cpp_num result;
2034 cpp_num_part middle[2], temp;
2035
2036 result.low = LOW_PART (lhs)(lhs & (~(cpp_num_part) 0 >> ((sizeof (cpp_num_part
) * 8) / 2)))
* LOW_PART (rhs)(rhs & (~(cpp_num_part) 0 >> ((sizeof (cpp_num_part
) * 8) / 2)))
;
2037 result.high = HIGH_PART (lhs)(lhs >> ((sizeof (cpp_num_part) * 8) / 2)) * HIGH_PART (rhs)(rhs >> ((sizeof (cpp_num_part) * 8) / 2));
2038
2039 middle[0] = LOW_PART (lhs)(lhs & (~(cpp_num_part) 0 >> ((sizeof (cpp_num_part
) * 8) / 2)))
* HIGH_PART (rhs)(rhs >> ((sizeof (cpp_num_part) * 8) / 2));
2040 middle[1] = HIGH_PART (lhs)(lhs >> ((sizeof (cpp_num_part) * 8) / 2)) * LOW_PART (rhs)(rhs & (~(cpp_num_part) 0 >> ((sizeof (cpp_num_part
) * 8) / 2)))
;
2041
2042 temp = result.low;
2043 result.low += LOW_PART (middle[0])(middle[0] & (~(cpp_num_part) 0 >> ((sizeof (cpp_num_part
) * 8) / 2)))
<< (PART_PRECISION(sizeof (cpp_num_part) * 8) / 2);
2044 if (result.low < temp)
2045 result.high++;
2046
2047 temp = result.low;
2048 result.low += LOW_PART (middle[1])(middle[1] & (~(cpp_num_part) 0 >> ((sizeof (cpp_num_part
) * 8) / 2)))
<< (PART_PRECISION(sizeof (cpp_num_part) * 8) / 2);
2049 if (result.low < temp)
2050 result.high++;
2051
2052 result.high += HIGH_PART (middle[0])(middle[0] >> ((sizeof (cpp_num_part) * 8) / 2));
2053 result.high += HIGH_PART (middle[1])(middle[1] >> ((sizeof (cpp_num_part) * 8) / 2));
2054 result.unsignedp = true;
2055 result.overflow = false;
2056
2057 return result;
2058}
2059
2060/* Multiply two preprocessing numbers. */
2061static cpp_num
2062num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
2063{
2064 cpp_num result, temp;
2065 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2066 bool overflow, negate = false;
2067 size_t precision = CPP_OPTION (pfile, precision)((pfile)->opts.precision);
2068
2069 /* Prepare for unsigned multiplication. */
2070 if (!unsignedp)
2071 {
2072 if (!num_positive (lhs, precision))
2073 negate = !negate, lhs = num_negate (lhs, precision);
2074 if (!num_positive (rhs, precision))
2075 negate = !negate, rhs = num_negate (rhs, precision);
2076 }
2077
2078 overflow = lhs.high && rhs.high;
2079 result = num_part_mul (lhs.low, rhs.low);
2080
2081 temp = num_part_mul (lhs.high, rhs.low);
2082 result.high += temp.low;
2083 if (temp.high)
2084 overflow = true;
2085
2086 temp = num_part_mul (lhs.low, rhs.high);
2087 result.high += temp.low;
2088 if (temp.high)
2089 overflow = true;
2090
2091 temp.low = result.low, temp.high = result.high;
2092 result = num_trim (result, precision);
2093 if (!num_eq (result, temp)(result.low == temp.low && result.high == temp.high))
2094 overflow = true;
2095
2096 if (negate)
2097 result = num_negate (result, precision);
2098
2099 if (unsignedp)
2100 result.overflow = false;
2101 else
2102 result.overflow = overflow || (num_positive (result, precision) ^ !negate
2103 && !num_zerop (result)((result.low | result.high) == 0));
2104 result.unsignedp = unsignedp;
2105
2106 return result;
2107}
2108
2109/* Divide two preprocessing numbers, LHS and RHS, returning the answer
2110 or the remainder depending upon OP. LOCATION is the source location
2111 of this operator (for diagnostics). */
2112
2113static cpp_num
2114num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
2115 location_t location)
2116{
2117 cpp_num result, sub;
2118 cpp_num_part mask;
2119 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
32
Assuming field 'unsignedp' is false
2120 bool negate = false, lhs_neg = false;
2121 size_t i, precision = CPP_OPTION (pfile, precision)((pfile)->opts.precision);
2122
2123 /* Prepare for unsigned division. */
2124 if (!unsignedp)
33
Assuming 'unsignedp' is true
34
Taking false branch
2125 {
2126 if (!num_positive (lhs, precision))
2127 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
2128 if (!num_positive (rhs, precision))
2129 negate = !negate, rhs = num_negate (rhs, precision);
2130 }
2131
2132 /* Find the high bit. */
2133 if (rhs.high)
35
Assuming field 'high' is not equal to 0
36
Taking true branch
2134 {
2135 i = precision - 1;
2136 mask = (cpp_num_part) 1 << (i - PART_PRECISION(sizeof (cpp_num_part) * 8));
2137 for (; ; i--, mask >>= 1)
37
Loop condition is true. Entering loop body
40
Loop condition is true. Entering loop body
2138 if (rhs.high & mask)
38
Assuming the condition is false
39
Taking false branch
41
Assuming the condition is true
42
Taking true branch
2139 break;
43
Execution continues on line 2165
2140 }
2141 else if (rhs.low)
2142 {
2143 if (precision > PART_PRECISION(sizeof (cpp_num_part) * 8))
2144 i = precision - PART_PRECISION(sizeof (cpp_num_part) * 8) - 1;
2145 else
2146 i = precision - 1;
2147 mask = (cpp_num_part) 1 << i;
2148 for (; ; i--, mask >>= 1)
2149 if (rhs.low & mask)
2150 break;
2151 }
2152 else
2153 {
2154 if (!pfile->state.skip_eval)
2155 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2156 "division by zero in #if");
2157 return lhs;
2158 }
2159
2160 /* First nonzero bit of RHS is bit I. Do naive division by
2161 shifting the RHS fully left, and subtracting from LHS if LHS is
2162 at least as big, and then repeating but with one less shift.
2163 This is not very efficient, but is easy to understand. */
2164
2165 rhs.unsignedp = true;
2166 lhs.unsignedp = true;
2167 i = precision - i - 1;
2168 sub = num_lshift (rhs, precision, i);
2169
2170 result.high = result.low = 0;
2171 for (;;)
44
Loop condition is true. Entering loop body
2172 {
2173 if (num_greater_eq (lhs, sub, precision))
45
Calling 'num_greater_eq'
49
Returning from 'num_greater_eq'
50
Taking true branch
2174 {
2175 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2176 if (i >= PART_PRECISION(sizeof (cpp_num_part) * 8))
51
Assuming the condition is true
52
Taking true branch
2177 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION(sizeof (cpp_num_part) * 8));
53
The result of the left shift is undefined due to shifting by '18446744073709551553', which is greater or equal to the width of type 'cpp_num_part'
2178 else
2179 result.low |= (cpp_num_part) 1 << i;
2180 }
2181 if (i-- == 0)
2182 break;
2183 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION(sizeof (cpp_num_part) * 8) - 1));
2184 sub.high >>= 1;
2185 }
2186
2187 /* We divide so that the remainder has the sign of the LHS. */
2188 if (op == CPP_DIV)
2189 {
2190 result.unsignedp = unsignedp;
2191 result.overflow = false;
2192 if (!unsignedp)
2193 {
2194 if (negate)
2195 result = num_negate (result, precision);
2196 result.overflow = (num_positive (result, precision) ^ !negate
2197 && !num_zerop (result)((result.low | result.high) == 0));
2198 }
2199
2200 return result;
2201 }
2202
2203 /* CPP_MOD. */
2204 lhs.unsignedp = unsignedp;
2205 lhs.overflow = false;
2206 if (lhs_neg)
2207 lhs = num_negate (lhs, precision);
2208
2209 return lhs;
2210}
2211