2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** 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
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
27 /* Parse C expressions for CCCP.
28 Copyright (C) 1987, 1992, 1994, 1995, 1997 Free Software Foundation.
30 This program is free software; you can redistribute it and/or modify it
31 under the terms of the GNU General Public License as published by the
32 Free Software Foundation; either version 2, or (at your option) any
35 This program is distributed in the hope that it will be useful,
36 but WITHOUT ANY WARRANTY; without even the implied warranty of
37 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 GNU General Public License for more details.
40 You should have received a copy of the GNU General Public License
41 along with this program; if not, write to the Free Software
42 Foundation, 59 Temple Place - Suite 330,
43 Boston, MA 02111-1307, USA.
45 In other words, you are welcome to use, share and improve this program.
46 You are forbidden to forbid anyone else to use, share and improve
47 what you give them. Help stamp out software-hoarding!
49 Written by Per Bothner 1994. */
51 /* Parse a C expression from text in a string */
56 /*@+matchanyintegral@*/
60 # include "lclintMacros.nf"
66 # include "cpperror.h"
68 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
69 number with SUM's sign, where A, B, and SUM are all C integers. */
71 /*@function static bool possibleSumSign (sef int, int, int)
72 modifies nothing ; @*/
74 #define possibleSumSign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
76 /* these are guesses! */
78 /*@constant int BITS_PER_UNIT@*/
79 # define BITS_PER_UNIT 8
81 /*@constant size_t BITS_PER_CHAR@*/
82 # define BITS_PER_CHAR 8
84 /*@constant size_t BITS_PER_WORD@*/
85 # define BITS_PER_WORD 32
87 /*@constant size_t HOST_BITS_PER_INT@*/
88 # define HOST_BITS_PER_INT 32
90 /*@constant size_t HOST_BITS_PER_LONG@*/
91 # define HOST_BITS_PER_LONG 32
93 /*@constant char TARGET_BELL@*/
94 # define TARGET_BELL (char) 6
96 /*@constant char TARGET_BS@*/
97 # define TARGET_BS (char) 7
99 /*@constant char TARGET_FF@*/
100 # define TARGET_FF (char) 8
102 /*@constant char TARGET_NEWLINE@*/
103 # define TARGET_NEWLINE '\n'
105 /*@constant char TARGET_CR@*/
106 # define TARGET_CR '\n'
108 /*@constant char TARGET_TAB@*/
109 # define TARGET_TAB '\t'
111 /*@constant char TARGET_VT@*/
112 # define TARGET_VT '\v'
114 #ifdef MULTIBYTE_CHARS
121 #ifndef INT_TYPE_SIZE
122 /*@constant size_t INT_TYPE_SIZE@*/
123 #define INT_TYPE_SIZE BITS_PER_WORD
126 #ifndef LONG_TYPE_SIZE
127 /*@constant size_t LONG_TYPE_SIZE@*/
128 #define LONG_TYPE_SIZE BITS_PER_WORD
131 #ifndef WCHAR_TYPE_SIZE
132 /*@constant size_t WCHAR_TYPE_SIZE@*/
133 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
136 # ifndef CHAR_TYPE_SIZE
137 /*@constant size_t CHAR_TYPE_SIZE@*/
138 # define CHAR_TYPE_SIZE BITS_PER_CHAR
141 #ifndef MAX_CHAR_TYPE_SIZE
142 /*@constant size_t MAX_CHAR_TYPE_SIZE@*/
143 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
146 #ifndef MAX_LONG_TYPE_SIZE
147 /*@constant size_t MAX_LONG_TYPE_SIZE@*/
148 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
151 #ifndef MAX_WCHAR_TYPE_SIZE
152 /*@constant size_t MAX_WCHAR_TYPE_SIZE@*/
153 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
156 static struct operation cppReader_lex (cppReader *);
157 static void integer_overflow (cppReader *);
158 static long left_shift (cppReader *, long, bool p_unsignedp, size_t);
159 static long right_shift (long, bool p_unsignedp, unsigned long);
161 /*@constant short CPPREADER_ERRORTOK@*/
162 #define CPPREADER_ERRORTOK 299
164 /*@constant int OROR@*/
167 /*@constant int ANDAND@*/
170 /*@constant int CPP_EQUALTOK@*/
171 #define CPP_EQUALTOK 302
173 /*@constant int NOTEQUAL@*/
176 /*@constant int LEQ@*/
179 /*@constant int GEQ@*/
182 /*@constant int LSH@*/
185 /*@constant int RSH@*/
188 /*@constant int NAME@*/
191 /*@constant short CPPEXP_INT@*/
192 #define CPPEXP_INT 309
194 /*@constant short CPPEXP_CHAR@*/
195 #define CPPEXP_CHAR 310
197 /*@constant int LEFT_OPERAND_REQUIRED@*/
198 #define LEFT_OPERAND_REQUIRED 1
200 /*@constant int RIGHT_OPERAND_REQUIRED@*/
201 #define RIGHT_OPERAND_REQUIRED 2
203 /*@constant int HAVE_VALUE@*/
206 #ifndef HOST_BITS_PER_WIDE_INT
208 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
209 /*@constant int HOST_BITS_PER_WIDE_INT@*/
210 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
212 #define HOST_WIDE_INT long
214 /*@constant int HOST_BITS_PER_WIDE_INT@*/
215 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
217 #define HOST_WIDE_INT long
225 /* Priority of op (relative to it right operand). */
226 /*@reldef@*/ char rprio;
228 /*@reldef@*/ char flags;
230 /* true if value should be treated as unsigned */
231 /*@reldef@*/ bool unsignedp;
233 /* The value logically "right" of op. */
234 /*@reldef@*/ HOST_WIDE_INT value;
237 /* Take care of parsing a number (anything that starts with a digit).
238 LEN is the number of characters in it. */
240 /* maybe needs to actually deal with floating point numbers */
243 cppReader_parseNumber (cppReader *pfile, char *start, int olen)
250 unsigned long nd, ULONG_MAX_over_base;
253 bool overflow = FALSE;
254 int digit, largest_digit = 0;
255 bool spec_long = FALSE;
257 op.unsignedp = FALSE;
259 for (i = 0; i < len; i++)
262 /* It's a float since it contains a point. */
265 cstring_makeLiteralTemp
266 ("Floating point numbers not allowed in #if expressions"));
267 op.op = CPPREADER_ERRORTOK;
272 if (len >= 3 && (mstring_equalPrefix (p, "0x")
273 || mstring_equalPrefix (p, "0X")))
288 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
289 ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
291 for (; len > 0; len--) {
294 if (c >= '0' && c <= '9')
296 digit = (int) (c - '0');
298 else if (base == 16 && c >= 'a' && c <= 'f')
300 digit = (int) (c - 'a') + 10;
302 else if (base == 16 && c >= 'A' && c <= 'F')
304 digit = (int) (c - 'A') + 10;
308 /* `l' means long, and `u' means unsigned. */
311 if (c == 'l' || c == 'L')
314 cppReader_errorLit (pfile,
315 cstring_makeLiteralTemp ("two `l's in integer constant"));
318 else if (c == 'u' || c == 'U')
321 cppReader_errorLit (pfile,
322 cstring_makeLiteralTemp ("two `u's in integer constant"));
327 /*@innerbreak@*/ break;
332 /*@innerbreak@*/ break;
337 /* Don't look for any more digits after the suffixes. */
341 if (largest_digit < digit)
343 largest_digit = digit;
346 nd = (long unsigned) (n * base + digit);
347 overflow |= (ULONG_MAX_over_base < (unsigned long) n)
348 | (nd < (unsigned long) n);
356 cstring_makeLiteralTemp ("Invalid number in #if expression"));
357 op.op = CPPREADER_ERRORTOK;
361 if (base <= largest_digit)
365 cstring_makeLiteralTemp
366 ("Integer constant contains digits beyond the radix"));
371 /*@i23 add flags for all these...*/
374 cstring_makeLiteralTemp ("Integer constant out of range"));
377 /* If too big to be signed, consider it unsigned. */
378 if ((long) n < 0 && ! op.unsignedp)
384 cstring_makeLiteralTemp ("Integer constant is so large that it is unsigned"));
396 /*@null@*/ /*@observer@*/ char *operator;
400 static struct token tokentab2[] = {
405 { "==", CPP_EQUALTOK },
409 { "++", CPPREADER_ERRORTOK },
410 { "--", CPPREADER_ERRORTOK },
411 { NULL, CPPREADER_ERRORTOK }
414 /* Read one token. */
416 struct operation cppReader_lex (cppReader *pfile)
420 register struct token *toktab;
421 enum cpp_token token;
423 char *tok_start, *tok_end;
428 old_written = size_toInt (cppReader_getWritten (pfile));
429 cppSkipHspace (pfile);
430 ic = cppBufPeek (cppReader_getBufferSafe (pfile));
441 token = cppGetToken (pfile);
442 tok_start = pfile->token_buffer + old_written;
443 tok_end = cppReader_getPWritten (pfile);
444 pfile->limit = tok_start;
448 case CPP_EOF: /* Should not happen ... */
453 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->fname))
458 (void) cppReader_popBuffer (pfile);
460 case CPP_HSPACE: case CPP_COMMENT:
463 return cppReader_parseNumber (pfile, tok_start, tok_end - tok_start);
465 cppReader_errorLit (pfile,
466 cstring_makeLiteralTemp ("string constants not allowed in #if expressions"));
467 op.op = CPPREADER_ERRORTOK;
470 /* This code for reading a character constant
471 handles multicharacter constants and wide characters.
472 It is mostly copied from c-lex.c. */
476 size_t width = MAX_CHAR_TYPE_SIZE;
479 char *ptr = tok_start;
480 #ifdef MULTIBYTE_CHARS
481 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
483 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + 1];
490 width = MAX_WCHAR_TYPE_SIZE;
491 #ifdef MULTIBYTE_CHARS
492 max_chars = MB_CUR_MAX;
499 max_chars = size_toInt (MAX_LONG_TYPE_SIZE / width);
503 while (ptr < tok_end && ((c = *ptr++) != '\''))
507 c = cppReader_parseEscape (pfile, &ptr);
508 if (width < HOST_BITS_PER_INT && c >= (1 << width))
512 cstring_makeLiteralTemp ("Escape sequence out of range for character"));
518 /* Merge character into result; ignore excess chars. */
519 if (num_chars < max_chars + 1)
521 if (width < HOST_BITS_PER_INT)
523 result = (int) ((unsigned) result << width) | (c & ((1 << width) - 1));
530 token_buffer[num_chars - 1] = c;
534 token_buffer[num_chars] = 0;
537 cppReader_errorLit (pfile,
538 cstring_makeLiteralTemp ("malformatted character constant"));
539 else if (num_chars == 0)
540 cppReader_errorLit (pfile,
541 cstring_makeLiteralTemp ("empty character constant"));
542 else if (num_chars > max_chars)
544 num_chars = max_chars;
545 cppReader_errorLit (pfile,
546 cstring_makeLiteralTemp ("character constant too long"));
548 else if (num_chars != 1 && ! cppReader_isTraditional (pfile))
550 cppReader_warningLit (pfile,
551 cstring_makeLiteralTemp ("multi-character character constant"));
558 /* If char type is signed, sign-extend the constant. */
561 int num_bits = num_chars * width;
563 if ((cppReader_lookup ("__CHAR_UNSIGNED__",
564 sizeof ("__CHAR_UNSIGNED__") - 1, -1) != NULL)
565 || (((unsigned) result >> (num_bits - 1)) & 1) == 0)
568 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
573 = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
578 #ifdef MULTIBYTE_CHARS
579 /* Set the initial shift state and convert the next sequence. */
581 /* In all locales L'\0' is zero and mbtowc will return zero,
584 || (num_chars == 1 && token_buffer[0] != '\0'))
587 (void) mbtowc (NULL, NULL, 0);
588 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
591 cppReader_pedwarn (pfile,"Ignoring invalid multibyte character");
598 /* This is always a signed type. */
599 op.unsignedp = FALSE;
605 return cppReader_parseNumber (pfile, "0", 0);
608 /* See if it is a special token of length 2. */
609 if (tok_start + 2 == tok_end)
611 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
613 if (tok_start[0] == toktab->operator[0]
614 && tok_start[1] == toktab->operator[1])
616 /*@loopbreak@*/ break;
620 if (toktab->token == CPPREADER_ERRORTOK)
622 cppReader_error (pfile, message ("`%s' not allowed in operand of `#if'", cstring_fromChars (tok_start)));
625 op.op = toktab->token;
639 /* Parse a C escape sequence. STRING_PTR points to a variable
640 containing a pointer to the string to parse. That pointer
641 is updated past the characters we use. The value of the
642 escape sequence is returned.
644 A negative value means the sequence \ newline was seen,
645 which is supposed to be equivalent to nothing at all.
647 If \ is followed by a null character, we return a negative
648 value and leave the string pointer pointing at the null character.
650 If \ is followed by 000, we return 0 and leave the string pointer
651 after the zeros. A value of 0 does not mean end of string. */
654 cppReader_parseEscape (cppReader *pfile, char **string_ptr)
656 char c = *(*string_ptr)++;
666 if (cppReader_isPedantic (pfile))
668 cppReader_pedwarn (pfile,
669 message ("non-ANSI-standard escape sequence, `\\%c'", c));
675 return TARGET_NEWLINE;
697 int i = (int) c - '0';
702 c = *(*string_ptr)++;
703 if (c >= '0' && c <= '7')
705 i = ((unsigned) i << 3) + c - '0';
711 /*@loopbreak@*/ break;
714 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
716 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
717 cppReader_pedwarnLit (pfile,
718 cstring_makeLiteralTemp ("octal character constant does not fit in a byte"));
724 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
727 c = *(*string_ptr)++;
729 if (c >= '0' && c <= '9')
731 digit = (unsigned int) (c - '0');
733 else if (c >= 'a' && c <= 'f')
735 digit = (unsigned int) (c - 'a') + 10;
737 else if (c >= 'A' && c <= 'F')
739 digit = (unsigned int) (c - 'A') + 10;
744 /*@loopbreak@*/ break;
746 overflow |= i ^ (i << 4 >> 4);
747 i = (i << 4) + digit;
751 if (digits_found == 0)
753 cppReader_errorLit (pfile,
754 cstring_makeLiteralTemp ("\\x used with no following hex digits"));
757 if ((overflow | (i & ~((1 << BITS_PER_UNIT) - 1))) != 0)
759 i &= (1 << BITS_PER_UNIT) - 1;
760 cppReader_pedwarnLit (pfile,
761 cstring_makeLiteralTemp ("hex character constant does not fit in a byte"));
772 integer_overflow (cppReader *pfile)
774 if (cppReader_isPedantic (pfile))
775 cppReader_pedwarnLit (pfile,
776 cstring_makeLiteralTemp ("integer overflow in preprocessor expression"));
780 left_shift (cppReader *pfile, long a, bool unsignedp, size_t b)
782 if (b >= HOST_BITS_PER_LONG)
784 if (!unsignedp && a != 0)
786 integer_overflow (pfile);
793 return (unsigned long) a << b;
801 integer_overflow (pfile);
809 right_shift (long a, bool unsignedp, unsigned long b)
811 if (b >= HOST_BITS_PER_LONG)
812 return (unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1));
814 return (unsigned long) a >> b;
819 /* These priorities are all even, so we can handle associatively. */
821 /*@constant int PAREN_INNER_PRIO@*/
822 #define PAREN_INNER_PRIO 0
824 /*@constant int COMMA_PRIO@*/
827 /*@constant int COND_PRIO@*/
828 #define COND_PRIO (COMMA_PRIO+2)
830 /*@constant int OROR_PRIO@*/
831 #define OROR_PRIO (COND_PRIO+2)
833 /*@constant int ANDAND_PRIO@*/
834 #define ANDAND_PRIO (OROR_PRIO+2)
836 /*@constant int OR_PRIO@*/
837 #define OR_PRIO (ANDAND_PRIO+2)
839 /*@constant int XOR_PRIO@*/
840 #define XOR_PRIO (OR_PRIO+2)
842 /*@constant int AND_PRIO@*/
843 #define AND_PRIO (XOR_PRIO+2)
845 /*@constant int CPP_EQUAL_PRIO@*/
846 #define CPP_EQUAL_PRIO (AND_PRIO+2)
848 /*@constant int LESS_PRIO@*/
849 #define LESS_PRIO (CPP_EQUAL_PRIO+2)
851 /*@constant int SHIFT_PRIO@*/
852 #define SHIFT_PRIO (LESS_PRIO+2)
854 /*@constant int PLUS_PRIO@*/
855 #define PLUS_PRIO (SHIFT_PRIO+2)
857 /*@constant int MUL_PRIO@*/
858 #define MUL_PRIO (PLUS_PRIO+2)
860 /*@constant int UNARY_PRIO@*/
861 #define UNARY_PRIO (MUL_PRIO+2)
863 /*@constant int PAREN_OUTER_PRIO@*/
864 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
867 #define COMPARE(OP) \
868 top->unsignedp = FALSE;\
869 top->value = ((unsigned1 || unsigned2) \
870 ? (unsigned long) v1 OP (unsigned long) v2 \
871 : ((long) v1 OP (long) v2)) ? 1 : 0
873 /* Parse and evaluate a C expression, reading from PFILE.
874 Returns the value of the expression. */
876 /*@constant int INIT_STACK_SIZE@*/
877 # define INIT_STACK_SIZE 20
880 cppReader_parseExpression (cppReader *pfile)
882 /* The implementation is an operator precedence parser,
883 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
885 The stack base is 'stack', and the current stack pointer is 'top'.
886 There is a stack element for each operator (only),
887 and the most recently pushed operator is 'top->op'.
888 An operand (value) is stored in the 'value' field of the stack
889 element of the operator that precedes it.
890 In that case the 'flags' field has the HAVE_VALUE flag set. */
892 struct operation init_stack[INIT_STACK_SIZE];
893 struct operation *stack = init_stack;
894 struct operation *limit = stack + INIT_STACK_SIZE;
895 register struct operation *top = stack;
896 int lprio, rprio = 0;
897 int skip_evaluation = 0;
908 op = cppReader_lex (pfile);
910 /* See if the token is an operand, in which case go to set_value.
911 If the token is an operator, figure out its left and right
912 priorities, and then goto maybe_reduce. */
917 top->value = 0, top->unsignedp = FALSE;
921 top->value = op.value;
922 top->unsignedp = op.unsignedp;
925 lprio = 0; goto maybe_reduce;
927 /* Is this correct if unary ? FIXME */
928 flags = RIGHT_OPERAND_REQUIRED;
929 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
931 flags = RIGHT_OPERAND_REQUIRED;
932 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
933 case '*': case '/': case '%':
934 lprio = MUL_PRIO; goto binop;
935 case '<': case '>': case LEQ: case GEQ:
936 lprio = LESS_PRIO; goto binop;
937 case CPP_EQUALTOK: case NOTEQUAL:
938 lprio = CPP_EQUAL_PRIO; goto binop;
940 lprio = SHIFT_PRIO; goto binop;
941 case '&': lprio = AND_PRIO; goto binop;
942 case '^': lprio = XOR_PRIO; goto binop;
943 case '|': lprio = OR_PRIO; goto binop;
944 case ANDAND: lprio = ANDAND_PRIO; goto binop;
945 case OROR: lprio = OROR_PRIO; goto binop;
947 lprio = COMMA_PRIO; goto binop;
949 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
952 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
955 lprio = COND_PRIO; rprio = COND_PRIO;
958 lprio = COND_PRIO + 1; rprio = COND_PRIO;
961 flags = LEFT_OPERAND_REQUIRED | RIGHT_OPERAND_REQUIRED;
967 message ("Invalid character in #if: %c",
973 /* Push a value onto the stack. */
974 if ((top->flags & HAVE_VALUE) != 0)
976 cppReader_errorLit (pfile,
977 cstring_makeLiteralTemp ("syntax error in #if"));
980 top->flags |= HAVE_VALUE;
984 /* Push an operator, and check if we can reduce now. */
985 while (top->rprio > lprio)
988 long v1 = top[-1].value, v2 = top[0].value;
989 bool unsigned1 = top[-1].unsignedp;
990 bool unsigned2 = top[0].unsignedp;
994 if (((top[1].flags & LEFT_OPERAND_REQUIRED) != 0)
995 && ((top[0].flags & HAVE_VALUE) == 0))
997 cppReader_errorLit (pfile,
998 cstring_makeLiteralTemp ("syntax error - missing left operand"));
1001 if (((top[1].flags & RIGHT_OPERAND_REQUIRED) != 0)
1002 && ((top[1].flags & HAVE_VALUE) == 0))
1004 cppReader_errorLit (pfile,
1005 cstring_makeLiteralTemp ("syntax error - missing right operand"));
1008 /* top[0].value = (top[1].op)(v1, v2);*/
1012 if ((top->flags & HAVE_VALUE) == 0)
1015 top->unsignedp = unsigned2;
1016 top->flags |= HAVE_VALUE;
1020 top->value = v1 + v2;
1021 top->unsignedp = unsigned1 || unsigned2;
1022 if (!top->unsignedp && (skip_evaluation == 0)
1023 && ! possibleSumSign (v1, v2, top->value))
1024 integer_overflow (pfile);
1026 /*@switchbreak@*/ break;
1028 if ((top->flags & HAVE_VALUE) == 0)
1031 if ((skip_evaluation == 0)
1032 && (top->value & v2) < 0 && !unsigned2)
1033 integer_overflow (pfile);
1034 top->unsignedp = unsigned2;
1035 top->flags |= HAVE_VALUE;
1039 top->value = v1 - v2;
1040 top->unsignedp = unsigned1 || unsigned2;
1041 if (!top->unsignedp && (skip_evaluation == 0)
1042 && !possibleSumSign (top->value, v2, v1))
1044 integer_overflow (pfile);
1047 /*@switchbreak@*/ break;
1049 top->unsignedp = unsigned1 || unsigned2;
1053 top->value = (unsigned long) v1 * v2;
1055 else if (skip_evaluation == 0)
1057 top->value = v1 * v2;
1059 && (top->value / v1 != v2
1060 || (top->value & v1 & v2) < 0))
1062 integer_overflow (pfile);
1070 /*@switchbreak@*/ break;
1072 if (skip_evaluation != 0)
1073 /*@switchbreak@*/ break;
1076 cppReader_errorLit (pfile,
1077 cstring_makeLiteralTemp ("Division by zero in #if"));
1080 top->unsignedp = unsigned1 || unsigned2;
1082 top->value = (unsigned long) v1 / v2;
1085 top->value = v1 / v2;
1086 if ((top->value & v1 & v2) < 0)
1087 integer_overflow (pfile);
1089 /*@switchbreak@*/ break;
1091 if (skip_evaluation != 0)
1092 /*@switchbreak@*/ break;
1095 cppReader_errorLit (pfile,
1096 cstring_makeLiteralTemp ("Division by zero in #if"));
1099 top->unsignedp = unsigned1 || unsigned2;
1101 top->value = (unsigned long) v1 % v2;
1103 top->value = v1 % v2;
1104 /*@switchbreak@*/ break;
1106 if ((top->flags & HAVE_VALUE) != 0)
1108 cppReader_errorLit (pfile,
1109 cstring_makeLiteralTemp ("Syntax error"));
1113 top->value = (v2 == 0) ? 1 : 0;
1114 top->unsignedp = FALSE;
1115 top->flags |= HAVE_VALUE;
1116 /*@switchbreak@*/ break;
1118 if ((top->flags & HAVE_VALUE) != 0)
1120 cppReader_errorLit (pfile,
1121 cstring_makeLiteralTemp ("syntax error"));
1125 top->unsignedp = unsigned2;
1126 top->flags |= HAVE_VALUE;
1127 /*@switchbreak@*/ break;
1128 case '<': COMPARE(<); /*@switchbreak@*/ break;
1129 case '>': COMPARE(>); /*@switchbreak@*/ break;
1130 case LEQ: COMPARE(<=); /*@switchbreak@*/ break;
1131 case GEQ: COMPARE(>=); /*@switchbreak@*/ break;
1133 top->value = (v1 == v2) ? 1 : 0;
1134 top->unsignedp = FALSE;
1135 /*@switchbreak@*/ break;
1137 top->value = (v1 != v2) ? 1 : 0;
1138 top->unsignedp = FALSE;
1139 /*@switchbreak@*/ break;
1141 if (skip_evaluation != 0)
1143 /*@switchbreak@*/ break;
1146 top->unsignedp = unsigned1;
1147 if (v2 < 0 && ! unsigned2)
1148 top->value = right_shift (v1, unsigned1, -v2);
1150 top->value = left_shift (pfile, v1, unsigned1, v2);
1151 /*@switchbreak@*/ break;
1153 if (skip_evaluation != 0)
1155 /*@switchbreak@*/ break;
1157 top->unsignedp = unsigned1;
1158 if (v2 < 0 && ! unsigned2)
1159 top->value = left_shift (pfile, v1, unsigned1, -v2);
1161 top->value = right_shift (v1, unsigned1, v2);
1162 /*@switchbreak@*/ break;
1165 #define LOGICAL(OP) \
1166 top->value = v1 OP v2;\
1167 top->unsignedp = unsigned1 || unsigned2;
1169 case '&': LOGICAL(&); /*@switchbreak@*/ break;
1170 case '^': LOGICAL(^); /*@switchbreak@*/ break;
1171 case '|': LOGICAL(|); /*@switchbreak@*/ break;
1173 top->value = ((v1 != 0) && (v2 != 0)) ? 1 : 0;
1174 top->unsignedp = FALSE;
1180 /*@switchbreak@*/ break;
1182 top->value = ((v1 != 0) || (v2 != 0)) ? 1 : 0;
1183 top->unsignedp = FALSE;
1188 /*@switchbreak@*/ break;
1190 if (cppReader_isPedantic (pfile))
1191 cppReader_pedwarnLit (pfile,
1192 cstring_makeLiteralTemp ("comma operator in operand of `#if'"));
1194 top->unsignedp = unsigned2;
1195 /*@switchbreak@*/ break;
1197 cppReader_errorLit (pfile,
1198 cstring_makeLiteralTemp ("syntax error in #if"));
1201 if (top[0].op != '?')
1203 cppReader_errorLit (pfile,
1204 cstring_makeLiteralTemp ("syntax error ':' without preceding '?'"));
1207 else if (((top[1].flags & HAVE_VALUE) == 0)
1208 || ((top[-1].flags & HAVE_VALUE) == 0)
1209 || ((top[0].flags & HAVE_VALUE) == 0))
1211 cppReader_errorLit (pfile,
1212 cstring_makeLiteralTemp ("bad syntax for ?: operator"));
1218 if (top->value != 0)
1223 top->value = (top->value != 0) ? v1 : v2;
1224 top->unsignedp = unsigned1 || unsigned2;
1226 /*@switchbreak@*/ break;
1228 if (((top[1].flags & HAVE_VALUE) != 0)
1229 || ((top[0].flags & HAVE_VALUE) == 0)
1231 || ((top[-1].flags & HAVE_VALUE) != 0))
1233 cppReader_errorLit (pfile,
1234 cstring_makeLiteralTemp ("mismatched parentheses in #if"));
1241 top->unsignedp = unsigned1;
1242 top->flags |= HAVE_VALUE;
1244 /*@switchbreak@*/ break;
1248 top[1].op >= ' ' && top[1].op <= '~'
1249 ? "unimplemented operator '%c'\n"
1250 : "unimplemented operator '\\%03o'\n",
1261 cppReader_errorLit (pfile,
1262 cstring_makeLiteralTemp ("internal error in #if expression"));
1267 if (stack != init_stack)
1271 } /*@=branchstate@*/
1277 /* Check for and handle stack overflow. */
1280 struct operation *new_stack;
1281 int old_size = (char *) limit - (char *) stack;
1282 size_t new_size = size_fromInt (2 * old_size);
1284 if (stack != init_stack)
1286 new_stack = (struct operation *) drealloc ((char *) stack,
1291 new_stack = (struct operation *) dmalloc (new_size);
1293 /* Bug: the parameters were in the wrong order! */
1294 memcpy ((char *) new_stack, (char *) stack, old_size);
1296 } /*@=branchstate@*/
1299 top = (struct operation *) ((char *) new_stack + old_size);
1300 limit = (struct operation *) ((char *) new_stack + new_size);
1302 } /*@=branchstate@*/
1307 if ((op.op == OROR && (top[-1].value != 0))
1308 || (op.op == ANDAND && (top[-1].value == 0))
1309 || (op.op == '?' && (top[-1].value == 0)))
1313 else if (op.op == ':')
1315 if (top[-2].value != 0) /* Was condition true? */
1331 if (stack != init_stack)
1335 } /*@=branchstate@*/
1338 cppReader_skipRestOfLine (pfile);