2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 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://www.splint.org
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"
65 # include "cpperror.h"
67 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
68 number with SUM's sign, where A, B, and SUM are all C integers. */
70 /*@function static bool possibleSumSign (sef int, int, int)
71 modifies nothing ; @*/
73 #define possibleSumSign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
75 /* these are guesses! */
77 /*@constant int BITS_PER_UNIT@*/
78 # define BITS_PER_UNIT 8
80 /*@constant size_t BITS_PER_CHAR@*/
81 # define BITS_PER_CHAR 8
83 /*@constant size_t BITS_PER_WORD@*/
84 # define BITS_PER_WORD 32
86 /*@constant size_t HOST_BITS_PER_INT@*/
87 # define HOST_BITS_PER_INT 32
89 /*@constant size_t HOST_BITS_PER_LONG@*/
90 # define HOST_BITS_PER_LONG 32
92 /*@constant char TARGET_BELL@*/
93 # define TARGET_BELL (char) 6
95 /*@constant char TARGET_BS@*/
96 # define TARGET_BS (char) 7
98 /*@constant char TARGET_FF@*/
99 # define TARGET_FF (char) 8
101 /*@constant char TARGET_NEWLINE@*/
102 # define TARGET_NEWLINE '\n'
104 /*@constant char TARGET_CR@*/
105 # define TARGET_CR '\n'
107 /*@constant char TARGET_TAB@*/
108 # define TARGET_TAB '\t'
110 /*@constant char TARGET_VT@*/
111 # define TARGET_VT '\v'
113 #ifdef MULTIBYTE_CHARS
120 #ifndef INT_TYPE_SIZE
121 /*@constant size_t INT_TYPE_SIZE@*/
122 #define INT_TYPE_SIZE BITS_PER_WORD
125 #ifndef LONG_TYPE_SIZE
126 /*@constant size_t LONG_TYPE_SIZE@*/
127 #define LONG_TYPE_SIZE BITS_PER_WORD
130 #ifndef WCHAR_TYPE_SIZE
131 /*@constant size_t WCHAR_TYPE_SIZE@*/
132 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
135 # ifndef CHAR_TYPE_SIZE
136 /*@constant size_t CHAR_TYPE_SIZE@*/
137 # define CHAR_TYPE_SIZE BITS_PER_CHAR
140 #ifndef MAX_CHAR_TYPE_SIZE
141 /*@constant size_t MAX_CHAR_TYPE_SIZE@*/
142 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
145 #ifndef MAX_LONG_TYPE_SIZE
146 /*@constant size_t MAX_LONG_TYPE_SIZE@*/
147 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
150 #ifndef MAX_WCHAR_TYPE_SIZE
151 /*@constant size_t MAX_WCHAR_TYPE_SIZE@*/
152 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
155 static struct operation cppexp_lex (cppReader *);
156 static void integer_overflow (cppReader *);
157 static long left_shift (cppReader *, long, bool p_unsignedp, size_t);
158 static long right_shift (long, bool p_unsignedp, unsigned long);
160 /*@constant short CPPREADER_ERRORTOK@*/
161 #define CPPREADER_ERRORTOK 299
163 /*@constant int OROR@*/
166 /*@constant int ANDAND@*/
169 /*@constant int CPP_EQUALTOK@*/
170 #define CPP_EQUALTOK 302
172 /*@constant int NOTEQUAL@*/
175 /*@constant int LEQ@*/
178 /*@constant int GEQ@*/
181 /*@constant int LSH@*/
184 /*@constant int RSH@*/
187 /*@constant int NAME@*/
190 /*@constant short CPPEXP_INT@*/
191 #define CPPEXP_INT 309
193 /*@constant short CPPEXP_CHAR@*/
194 #define CPPEXP_CHAR 310
196 /*@constant int LEFT_OPERAND_REQUIRED@*/
197 #define LEFT_OPERAND_REQUIRED 1
199 /*@constant int RIGHT_OPERAND_REQUIRED@*/
200 #define RIGHT_OPERAND_REQUIRED 2
202 /*@constant int HAVE_VALUE@*/
205 #ifndef HOST_BITS_PER_WIDE_INT
207 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
208 /*@constant int HOST_BITS_PER_WIDE_INT@*/
209 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
211 #define HOST_WIDE_INT long
213 /*@constant int HOST_BITS_PER_WIDE_INT@*/
214 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
216 #define HOST_WIDE_INT long
224 /* Priority of op (relative to it right operand). */
225 /*@reldef@*/ char rprio;
227 /*@reldef@*/ char flags;
229 /* true if value should be treated as unsigned */
230 /*@reldef@*/ bool unsignedp;
232 /* The value logically "right" of op. */
233 /*@reldef@*/ HOST_WIDE_INT value;
236 /* Take care of parsing a number (anything that starts with a digit).
237 LEN is the number of characters in it. */
239 /* maybe needs to actually deal with floating point numbers */
242 cppReader_parseNumber (cppReader *pfile, char *start, int olen)
249 unsigned long nd, ULONG_MAX_over_base;
252 bool overflow = FALSE;
253 int digit, largest_digit = 0;
254 bool spec_long = FALSE;
256 op.unsignedp = FALSE;
258 for (i = 0; i < len; i++)
261 /* It's a float since it contains a point. */
264 cstring_makeLiteralTemp
265 ("Floating point numbers not allowed in #if expressions"));
266 op.op = CPPREADER_ERRORTOK;
271 if (len >= 3 && (mstring_equalPrefix (p, "0x")
272 || mstring_equalPrefix (p, "0X")))
287 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
288 ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
290 for (; len > 0; len--) {
293 if (c >= '0' && c <= '9')
295 digit = (int) (c - '0');
297 else if (base == 16 && c >= 'a' && c <= 'f')
299 digit = (int) (c - 'a') + 10;
301 else if (base == 16 && c >= 'A' && c <= 'F')
303 digit = (int) (c - 'A') + 10;
307 /* `l' means long, and `u' means unsigned. */
310 if (c == 'l' || c == 'L')
313 cppReader_errorLit (pfile,
314 cstring_makeLiteralTemp ("two `l's in integer constant"));
317 else if (c == 'u' || c == 'U')
320 cppReader_errorLit (pfile,
321 cstring_makeLiteralTemp ("two `u's in integer constant"));
326 /*@innerbreak@*/ break;
331 /*@innerbreak@*/ break;
336 /* Don't look for any more digits after the suffixes. */
340 if (largest_digit < digit)
342 largest_digit = digit;
345 nd = (long unsigned) (n * base + digit);
346 overflow |= (ULONG_MAX_over_base < (unsigned long) n)
347 | (nd < (unsigned long) n);
355 cstring_makeLiteralTemp ("Invalid number in #if expression"));
356 op.op = CPPREADER_ERRORTOK;
360 if (base <= largest_digit)
364 cstring_makeLiteralTemp
365 ("Integer constant contains digits beyond the radix"));
370 /*@i23 add flags for all these...*/
373 cstring_makeLiteralTemp ("Integer constant out of range"));
376 /* If too big to be signed, consider it unsigned. */
377 if ((long) n < 0 && ! op.unsignedp)
383 cstring_makeLiteralTemp ("Integer constant is so large that it is unsigned"));
391 DPRINTF (("Parse number: %d", op.value));
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 cppexp_lex (cppReader *pfile)
420 register struct token *toktab;
421 enum cpp_token token;
423 char *tok_start, *tok_end;
428 old_written = size_toInt (cpplib_getWritten (pfile));
429 cppSkipHspace (pfile);
430 ic = cpplib_bufPeek (cppReader_getBufferSafe (pfile));
434 DPRINTF (("Read: %c", c));
442 token = cpplib_getTokenForceExpand (pfile);
444 tok_start = pfile->token_buffer + old_written;
445 tok_end = cpplib_getPWritten (pfile);
447 DPRINTF (("Token: %s < %s", tok_start, tok_end));
449 pfile->limit = tok_start;
453 case CPP_EOF: /* Should not happen ... */
458 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->fname))
463 (void) cppReader_popBuffer (pfile);
465 case CPP_HSPACE: case CPP_COMMENT:
468 return cppReader_parseNumber (pfile, tok_start, tok_end - tok_start);
470 cppReader_errorLit (pfile,
471 cstring_makeLiteralTemp ("string constants not allowed in #if expressions"));
472 op.op = CPPREADER_ERRORTOK;
475 /* This code for reading a character constant
476 handles multicharacter constants and wide characters.
477 It is mostly copied from c-lex.c. */
481 size_t width = MAX_CHAR_TYPE_SIZE;
484 char *ptr = tok_start;
485 #ifdef MULTIBYTE_CHARS
486 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
488 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + 1];
495 width = MAX_WCHAR_TYPE_SIZE;
496 #ifdef MULTIBYTE_CHARS
497 max_chars = MB_CUR_MAX;
504 max_chars = size_toInt (MAX_LONG_TYPE_SIZE / width);
508 while (ptr < tok_end && ((c = *ptr++) != '\''))
512 c = cppReader_parseEscape (pfile, &ptr);
513 if (width < HOST_BITS_PER_INT && c >= (1 << width))
517 cstring_makeLiteralTemp ("Escape sequence out of range for character"));
523 /* Merge character into result; ignore excess chars. */
524 if (num_chars < max_chars + 1)
526 if (width < HOST_BITS_PER_INT)
528 result = (int) ((unsigned) result << width) | (c & ((1 << width) - 1));
535 token_buffer[num_chars - 1] = c;
539 token_buffer[num_chars] = 0;
542 cppReader_errorLit (pfile,
543 cstring_makeLiteralTemp ("malformatted character constant"));
544 else if (num_chars == 0)
545 cppReader_errorLit (pfile,
546 cstring_makeLiteralTemp ("empty character constant"));
547 else if (num_chars > max_chars)
549 num_chars = max_chars;
550 cppReader_errorLit (pfile,
551 cstring_makeLiteralTemp ("character constant too long"));
553 else if (num_chars != 1 && ! cppReader_isTraditional (pfile))
555 cppReader_warningLit (pfile,
556 cstring_makeLiteralTemp ("multi-character character constant"));
563 /* If char type is signed, sign-extend the constant. */
566 int num_bits = num_chars * width;
568 if ((cpphash_lookup ("__CHAR_UNSIGNED__",
569 sizeof ("__CHAR_UNSIGNED__") - 1, -1) != NULL)
570 || (((unsigned) result >> (num_bits - 1)) & 1) == 0)
573 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
578 = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
583 #ifdef MULTIBYTE_CHARS
584 /* Set the initial shift state and convert the next sequence. */
586 /* In all locales L'\0' is zero and mbtowc will return zero,
589 || (num_chars == 1 && token_buffer[0] != '\0'))
592 (void) mbtowc (NULL, NULL, 0);
593 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
596 cppReader_pedwarn (pfile,"Ignoring invalid multibyte character");
603 /* This is always a signed type. */
604 op.unsignedp = FALSE;
611 return cppReader_parseNumber (pfile, "0", 0);
614 /* See if it is a special token of length 2. */
615 if (tok_start + 2 == tok_end)
617 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
619 if (tok_start[0] == toktab->operator[0]
620 && tok_start[1] == toktab->operator[1])
622 /*@loopbreak@*/ break;
626 if (toktab->token == CPPREADER_ERRORTOK)
628 cppReader_error (pfile, message ("`%s' not allowed in operand of `#if'", cstring_fromChars (tok_start)));
631 op.op = toktab->token;
645 /* Parse a C escape sequence. STRING_PTR points to a variable
646 containing a pointer to the string to parse. That pointer
647 is updated past the characters we use. The value of the
648 escape sequence is returned.
650 A negative value means the sequence \ newline was seen,
651 which is supposed to be equivalent to nothing at all.
653 If \ is followed by a null character, we return a negative
654 value and leave the string pointer pointing at the null character.
656 If \ is followed by 000, we return 0 and leave the string pointer
657 after the zeros. A value of 0 does not mean end of string. */
660 cppReader_parseEscape (cppReader *pfile, char **string_ptr)
662 char c = *(*string_ptr)++;
672 if (cppReader_isPedantic (pfile))
674 cppReader_pedwarn (pfile,
675 message ("non-ANSI-standard escape sequence, `\\%c'", c));
681 return TARGET_NEWLINE;
703 int i = (int) c - '0';
708 c = *(*string_ptr)++;
709 if (c >= '0' && c <= '7')
711 i = ((unsigned) i << 3) + c - '0';
717 /*@loopbreak@*/ break;
720 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
722 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
723 cppReader_pedwarnLit (pfile,
724 cstring_makeLiteralTemp ("octal character constant does not fit in a byte"));
730 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
733 c = *(*string_ptr)++;
735 if (c >= '0' && c <= '9')
737 digit = (unsigned int) (c - '0');
739 else if (c >= 'a' && c <= 'f')
741 digit = (unsigned int) (c - 'a') + 10;
743 else if (c >= 'A' && c <= 'F')
745 digit = (unsigned int) (c - 'A') + 10;
750 /*@loopbreak@*/ break;
752 overflow |= i ^ (i << 4 >> 4);
753 i = (i << 4) + digit;
757 if (digits_found == 0)
759 cppReader_errorLit (pfile,
760 cstring_makeLiteralTemp ("\\x used with no following hex digits"));
763 if ((overflow | (i & ~((1 << BITS_PER_UNIT) - 1))) != 0)
765 i &= (1 << BITS_PER_UNIT) - 1;
766 cppReader_pedwarnLit (pfile,
767 cstring_makeLiteralTemp ("hex character constant does not fit in a byte"));
778 integer_overflow (cppReader *pfile)
780 if (cppReader_isPedantic (pfile))
781 cppReader_pedwarnLit (pfile,
782 cstring_makeLiteralTemp ("integer overflow in preprocessor expression"));
786 left_shift (cppReader *pfile, long a, bool unsignedp, size_t b)
788 if (b >= HOST_BITS_PER_LONG)
790 if (!unsignedp && a != 0)
792 integer_overflow (pfile);
799 return (unsigned long) a << b;
807 integer_overflow (pfile);
815 right_shift (long a, bool unsignedp, unsigned long b)
817 if (b >= HOST_BITS_PER_LONG)
818 return (unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1));
820 return (unsigned long) a >> b;
825 /* These priorities are all even, so we can handle associatively. */
827 /*@constant int PAREN_INNER_PRIO@*/
828 #define PAREN_INNER_PRIO 0
830 /*@constant int COMMA_PRIO@*/
833 /*@constant int COND_PRIO@*/
834 #define COND_PRIO (COMMA_PRIO+2)
836 /*@constant int OROR_PRIO@*/
837 #define OROR_PRIO (COND_PRIO+2)
839 /*@constant int ANDAND_PRIO@*/
840 #define ANDAND_PRIO (OROR_PRIO+2)
842 /*@constant int OR_PRIO@*/
843 #define OR_PRIO (ANDAND_PRIO+2)
845 /*@constant int XOR_PRIO@*/
846 #define XOR_PRIO (OR_PRIO+2)
848 /*@constant int AND_PRIO@*/
849 #define AND_PRIO (XOR_PRIO+2)
851 /*@constant int CPP_EQUAL_PRIO@*/
852 #define CPP_EQUAL_PRIO (AND_PRIO+2)
854 /*@constant int LESS_PRIO@*/
855 #define LESS_PRIO (CPP_EQUAL_PRIO+2)
857 /*@constant int SHIFT_PRIO@*/
858 #define SHIFT_PRIO (LESS_PRIO+2)
860 /*@constant int PLUS_PRIO@*/
861 #define PLUS_PRIO (SHIFT_PRIO+2)
863 /*@constant int MUL_PRIO@*/
864 #define MUL_PRIO (PLUS_PRIO+2)
866 /*@constant int UNARY_PRIO@*/
867 #define UNARY_PRIO (MUL_PRIO+2)
869 /*@constant int PAREN_OUTER_PRIO@*/
870 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
873 #define COMPARE(OP) \
874 top->unsignedp = FALSE;\
875 top->value = ((unsigned1 || unsigned2) \
876 ? (unsigned long) v1 OP (unsigned long) v2 \
877 : ((long) v1 OP (long) v2)) ? 1 : 0
879 /* Parse and evaluate a C expression, reading from PFILE.
880 Returns the value of the expression. */
882 /*@constant int INIT_STACK_SIZE@*/
883 # define INIT_STACK_SIZE 20
886 cppReader_parseExpression (cppReader *pfile)
888 /* The implementation is an operator precedence parser,
889 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
891 The stack base is 'stack', and the current stack pointer is 'top'.
892 There is a stack element for each operator (only),
893 and the most recently pushed operator is 'top->op'.
894 An operand (value) is stored in the 'value' field of the stack
895 element of the operator that precedes it.
896 In that case the 'flags' field has the HAVE_VALUE flag set. */
898 struct operation init_stack[INIT_STACK_SIZE];
899 struct operation *stack = init_stack;
900 struct operation *limit = stack + INIT_STACK_SIZE;
901 register struct operation *top = stack;
902 int lprio, rprio = 0;
903 int skip_evaluation = 0;
914 op = cppexp_lex (pfile);
916 /* See if the token is an operand, in which case go to set_value.
917 If the token is an operator, figure out its left and right
918 priorities, and then goto maybe_reduce. */
923 top->value = 0, top->unsignedp = FALSE;
927 top->value = op.value;
928 top->unsignedp = op.unsignedp;
931 lprio = 0; goto maybe_reduce;
933 /* Is this correct if unary ? FIXME */
934 flags = RIGHT_OPERAND_REQUIRED;
935 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
937 flags = RIGHT_OPERAND_REQUIRED;
938 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
939 case '*': case '/': case '%':
940 lprio = MUL_PRIO; goto binop;
941 case '<': case '>': case LEQ: case GEQ:
942 lprio = LESS_PRIO; goto binop;
943 case CPP_EQUALTOK: case NOTEQUAL:
944 lprio = CPP_EQUAL_PRIO; goto binop;
946 lprio = SHIFT_PRIO; goto binop;
947 case '&': lprio = AND_PRIO; goto binop;
948 case '^': lprio = XOR_PRIO; goto binop;
949 case '|': lprio = OR_PRIO; goto binop;
950 case ANDAND: lprio = ANDAND_PRIO; goto binop;
951 case OROR: lprio = OROR_PRIO; goto binop;
953 lprio = COMMA_PRIO; goto binop;
955 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
958 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
961 lprio = COND_PRIO; rprio = COND_PRIO;
964 lprio = COND_PRIO + 1; rprio = COND_PRIO;
967 flags = LEFT_OPERAND_REQUIRED | RIGHT_OPERAND_REQUIRED;
973 message ("Invalid character in #if: %c",
979 /* Push a value onto the stack. */
980 if ((top->flags & HAVE_VALUE) != 0)
982 cppReader_errorLit (pfile,
983 cstring_makeLiteralTemp ("syntax error in #if"));
986 top->flags |= HAVE_VALUE;
990 /* Push an operator, and check if we can reduce now. */
991 while (top->rprio > lprio)
994 long v1 = top[-1].value, v2 = top[0].value;
995 bool unsigned1 = top[-1].unsignedp;
996 bool unsigned2 = top[0].unsignedp;
1000 if (((top[1].flags & LEFT_OPERAND_REQUIRED) != 0)
1001 && ((top[0].flags & HAVE_VALUE) == 0))
1003 cppReader_errorLit (pfile,
1004 cstring_makeLiteralTemp ("syntax error - missing left operand"));
1007 if (((top[1].flags & RIGHT_OPERAND_REQUIRED) != 0)
1008 && ((top[1].flags & HAVE_VALUE) == 0))
1010 cppReader_errorLit (pfile,
1011 cstring_makeLiteralTemp ("syntax error - missing right operand"));
1014 /* top[0].value = (top[1].op)(v1, v2);*/
1018 if ((top->flags & HAVE_VALUE) == 0)
1021 top->unsignedp = unsigned2;
1022 top->flags |= HAVE_VALUE;
1026 top->value = v1 + v2;
1027 top->unsignedp = unsigned1 || unsigned2;
1028 if (!top->unsignedp && (skip_evaluation == 0)
1029 && ! possibleSumSign (v1, v2, top->value))
1030 integer_overflow (pfile);
1032 /*@switchbreak@*/ break;
1034 if ((top->flags & HAVE_VALUE) == 0)
1037 if ((skip_evaluation == 0)
1038 && (top->value & v2) < 0 && !unsigned2)
1039 integer_overflow (pfile);
1040 top->unsignedp = unsigned2;
1041 top->flags |= HAVE_VALUE;
1045 top->value = v1 - v2;
1046 top->unsignedp = unsigned1 || unsigned2;
1047 if (!top->unsignedp && (skip_evaluation == 0)
1048 && !possibleSumSign (top->value, v2, v1))
1050 integer_overflow (pfile);
1053 /*@switchbreak@*/ break;
1055 top->unsignedp = unsigned1 || unsigned2;
1059 top->value = (unsigned long) v1 * v2;
1061 else if (skip_evaluation == 0)
1063 top->value = v1 * v2;
1065 && (top->value / v1 != v2
1066 || (top->value & v1 & v2) < 0))
1068 integer_overflow (pfile);
1076 /*@switchbreak@*/ break;
1078 if (skip_evaluation != 0)
1079 /*@switchbreak@*/ break;
1082 cppReader_errorLit (pfile,
1083 cstring_makeLiteralTemp ("Division by zero in #if"));
1086 top->unsignedp = unsigned1 || unsigned2;
1088 top->value = (unsigned long) v1 / v2;
1091 top->value = v1 / v2;
1092 if ((top->value & v1 & v2) < 0)
1093 integer_overflow (pfile);
1095 /*@switchbreak@*/ break;
1097 if (skip_evaluation != 0)
1098 /*@switchbreak@*/ break;
1101 cppReader_errorLit (pfile,
1102 cstring_makeLiteralTemp ("Division by zero in #if"));
1105 top->unsignedp = unsigned1 || unsigned2;
1107 top->value = (unsigned long) v1 % v2;
1109 top->value = v1 % v2;
1110 /*@switchbreak@*/ break;
1112 if ((top->flags & HAVE_VALUE) != 0)
1114 cppReader_errorLit (pfile,
1115 cstring_makeLiteralTemp ("Syntax error"));
1119 top->value = (v2 == 0) ? 1 : 0;
1120 top->unsignedp = FALSE;
1121 top->flags |= HAVE_VALUE;
1122 /*@switchbreak@*/ break;
1124 if ((top->flags & HAVE_VALUE) != 0)
1126 cppReader_errorLit (pfile,
1127 cstring_makeLiteralTemp ("syntax error"));
1131 top->unsignedp = unsigned2;
1132 top->flags |= HAVE_VALUE;
1133 /*@switchbreak@*/ break;
1134 case '<': COMPARE(<); /*@switchbreak@*/ break;
1135 case '>': COMPARE(>); /*@switchbreak@*/ break;
1136 case LEQ: COMPARE(<=); /*@switchbreak@*/ break;
1137 case GEQ: COMPARE(>=); /*@switchbreak@*/ break;
1139 top->value = (v1 == v2) ? 1 : 0;
1140 top->unsignedp = FALSE;
1141 /*@switchbreak@*/ break;
1143 top->value = (v1 != v2) ? 1 : 0;
1144 top->unsignedp = FALSE;
1145 /*@switchbreak@*/ break;
1147 if (skip_evaluation != 0)
1149 /*@switchbreak@*/ break;
1152 top->unsignedp = unsigned1;
1153 if (v2 < 0 && ! unsigned2)
1154 top->value = right_shift (v1, unsigned1, -v2);
1156 top->value = left_shift (pfile, v1, unsigned1, v2);
1157 /*@switchbreak@*/ break;
1159 if (skip_evaluation != 0)
1161 /*@switchbreak@*/ break;
1163 top->unsignedp = unsigned1;
1164 if (v2 < 0 && ! unsigned2)
1165 top->value = left_shift (pfile, v1, unsigned1, -v2);
1167 top->value = right_shift (v1, unsigned1, v2);
1168 /*@switchbreak@*/ break;
1171 #define LOGICAL(OP) \
1172 top->value = v1 OP v2;\
1173 top->unsignedp = unsigned1 || unsigned2;
1175 case '&': LOGICAL(&); /*@switchbreak@*/ break;
1176 case '^': LOGICAL(^); /*@switchbreak@*/ break;
1177 case '|': LOGICAL(|); /*@switchbreak@*/ break;
1179 top->value = ((v1 != 0) && (v2 != 0)) ? 1 : 0;
1180 top->unsignedp = FALSE;
1186 /*@switchbreak@*/ break;
1188 top->value = ((v1 != 0) || (v2 != 0)) ? 1 : 0;
1189 top->unsignedp = FALSE;
1194 /*@switchbreak@*/ break;
1196 if (cppReader_isPedantic (pfile))
1197 cppReader_pedwarnLit (pfile,
1198 cstring_makeLiteralTemp ("comma operator in operand of `#if'"));
1200 top->unsignedp = unsigned2;
1201 /*@switchbreak@*/ break;
1203 cppReader_errorLit (pfile,
1204 cstring_makeLiteralTemp ("syntax error in #if"));
1207 if (top[0].op != '?')
1209 cppReader_errorLit (pfile,
1210 cstring_makeLiteralTemp ("syntax error ':' without preceding '?'"));
1213 else if (((top[1].flags & HAVE_VALUE) == 0)
1214 || ((top[-1].flags & HAVE_VALUE) == 0)
1215 || ((top[0].flags & HAVE_VALUE) == 0))
1217 cppReader_errorLit (pfile,
1218 cstring_makeLiteralTemp ("bad syntax for ?: operator"));
1224 if (top->value != 0)
1229 top->value = (top->value != 0) ? v1 : v2;
1230 top->unsignedp = unsigned1 || unsigned2;
1232 /*@switchbreak@*/ break;
1234 if (((top[1].flags & HAVE_VALUE) != 0)
1235 || ((top[0].flags & HAVE_VALUE) == 0)
1237 || ((top[-1].flags & HAVE_VALUE) != 0))
1239 cppReader_errorLit (pfile,
1240 cstring_makeLiteralTemp ("mismatched parentheses in #if"));
1247 top->unsignedp = unsigned1;
1248 top->flags |= HAVE_VALUE;
1250 /*@switchbreak@*/ break;
1254 top[1].op >= ' ' && top[1].op <= '~'
1255 ? "unimplemented operator '%c'\n"
1256 : "unimplemented operator '\\%03o'\n",
1267 cppReader_errorLit (pfile,
1268 cstring_makeLiteralTemp ("internal error in #if expression"));
1273 if (stack != init_stack)
1277 } /*@=branchstate@*/
1283 /* Check for and handle stack overflow. */
1286 struct operation *new_stack;
1287 int old_size = (char *) limit - (char *) stack;
1288 size_t new_size = size_fromInt (2 * old_size);
1290 if (stack != init_stack)
1292 new_stack = (struct operation *) drealloc ((char *) stack,
1297 new_stack = (struct operation *) dmalloc (new_size);
1299 /* Bug: the parameters were in the wrong order! */
1300 memcpy ((char *) new_stack, (char *) stack, old_size);
1302 } /*@=branchstate@*/
1305 top = (struct operation *) ((char *) new_stack + old_size);
1306 limit = (struct operation *) ((char *) new_stack + new_size);
1308 } /*@=branchstate@*/
1313 if ((op.op == OROR && (top[-1].value != 0))
1314 || (op.op == ANDAND && (top[-1].value == 0))
1315 || (op.op == '?' && (top[-1].value == 0)))
1319 else if (op.op == ':')
1321 if (top[-2].value != 0) /* Was condition true? */
1337 if (stack != init_stack)
1341 } /*@=branchstate@*/
1344 cppReader_skipRestOfLine (pfile);