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 splint: splint@cs.virginia.edu
21 ** To report a bug: splint-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@*/
59 # include "splintMacros.nf"
64 # include "cpperror.h"
66 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
67 number with SUM's sign, where A, B, and SUM are all C integers. */
69 /*@function static bool possibleSumSign (sef int, int, int)
70 modifies nothing ; @*/
72 #define possibleSumSign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
74 /* these are guesses! */
76 /*@constant int BITS_PER_UNIT = 8@*/
77 # define BITS_PER_UNIT 8
79 /*@constant size_t BITS_PER_CHAR@*/
80 # define BITS_PER_CHAR 8
82 /*@constant size_t BITS_PER_WORD@*/
83 # define BITS_PER_WORD 32
85 /*@constant size_t HOST_BITS_PER_INT@*/
86 # define HOST_BITS_PER_INT 32
88 /*@constant size_t HOST_BITS_PER_LONG = 32@*/
89 # define HOST_BITS_PER_LONG 32
91 /*@constant char TARGET_BELL@*/
92 # define TARGET_BELL (char) 6
94 /*@constant char TARGET_BS@*/
95 # define TARGET_BS (char) 7
97 /*@constant char TARGET_FF@*/
98 # define TARGET_FF (char) 8
100 /*@constant char TARGET_NEWLINE@*/
101 # define TARGET_NEWLINE '\n'
103 /*@constant char TARGET_CR@*/
104 # define TARGET_CR '\n'
106 /*@constant char TARGET_TAB@*/
107 # define TARGET_TAB '\t'
109 /*@constant char TARGET_VT@*/
110 # define TARGET_VT '\v'
112 #ifdef MULTIBYTE_CHARS
119 #ifndef INT_TYPE_SIZE
120 /*@constant size_t INT_TYPE_SIZE@*/
121 #define INT_TYPE_SIZE BITS_PER_WORD
124 #ifndef LONG_TYPE_SIZE
125 /*@constant size_t LONG_TYPE_SIZE@*/
126 #define LONG_TYPE_SIZE BITS_PER_WORD
129 #ifndef WCHAR_TYPE_SIZE
130 /*@constant size_t WCHAR_TYPE_SIZE@*/
131 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
134 # ifndef CHAR_TYPE_SIZE
135 /*@constant size_t CHAR_TYPE_SIZE@*/
136 # define CHAR_TYPE_SIZE BITS_PER_CHAR
139 #ifndef MAX_CHAR_TYPE_SIZE
140 /*@constant size_t MAX_CHAR_TYPE_SIZE@*/
141 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
144 #ifndef MAX_LONG_TYPE_SIZE
145 /*@constant size_t MAX_LONG_TYPE_SIZE@*/
146 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
149 #ifndef MAX_WCHAR_TYPE_SIZE
150 /*@constant size_t MAX_WCHAR_TYPE_SIZE@*/
151 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
154 static struct operation cppexp_lex (cppReader *);
155 static void integer_overflow (cppReader *);
156 static long left_shift (cppReader *, long, bool p_unsignedp, size_t);
157 static long right_shift (long, bool p_unsignedp, unsigned long);
159 /*@constant short CPPREADER_ERRORTOK@*/
160 #define CPPREADER_ERRORTOK 299
162 /*@constant int OROR@*/
165 /*@constant int ANDAND@*/
168 /*@constant int CPP_EQUALTOK@*/
169 #define CPP_EQUALTOK 302
171 /*@constant int NOTEQUAL@*/
174 /*@constant int LEQ@*/
177 /*@constant int GEQ@*/
180 /*@constant int LSH@*/
183 /*@constant int RSH@*/
186 /*@constant int NAME@*/
189 /*@constant short CPPEXP_INT@*/
190 #define CPPEXP_INT 309
192 /*@constant short CPPEXP_CHAR@*/
193 #define CPPEXP_CHAR 310
195 /*@constant int LEFT_OPERAND_REQUIRED@*/
196 #define LEFT_OPERAND_REQUIRED 1
198 /*@constant int RIGHT_OPERAND_REQUIRED@*/
199 #define RIGHT_OPERAND_REQUIRED 2
201 /*@constant int HAVE_VALUE@*/
204 #ifndef HOST_BITS_PER_WIDE_INT
206 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
207 /*@constant int HOST_BITS_PER_WIDE_INT@*/
208 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
210 #define HOST_WIDE_INT long
212 /*@constant int HOST_BITS_PER_WIDE_INT@*/
213 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
215 #define HOST_WIDE_INT long
223 /* Priority of op (relative to it right operand). */
224 /*@reldef@*/ char rprio;
226 /*@reldef@*/ char flags;
228 /* true if value should be treated as unsigned */
229 /*@reldef@*/ bool unsignedp;
231 /* The value logically "right" of op. */
232 /*@reldef@*/ HOST_WIDE_INT value;
235 /* Take care of parsing a number (anything that starts with a digit).
236 LEN is the number of characters in it. */
238 /* maybe needs to actually deal with floating point numbers */
241 cppReader_parseNumber (cppReader *pfile, char *start, int olen) /*@requires maxRead(start) >= (olen - 1) @*/
248 unsigned long nd, ULONG_MAX_over_base;
251 bool overflow = FALSE;
252 int digit, largest_digit = 0;
253 bool spec_long = FALSE;
255 op.unsignedp = FALSE;
257 for (i = 0; i < len; i++)
259 /*drl bee: is*/ if (p[i] == '.') {
260 /* It's a float since it contains a point. */
263 cstring_makeLiteralTemp
264 ("Floating point numbers not allowed in #if expressions"));
265 op.op = CPPREADER_ERRORTOK;
270 if (len >= 3 && (mstring_equalPrefix (p, "0x")
271 || mstring_equalPrefix (p, "0X")))
280 /* drl: see if there is a reason that we shouldn't do
291 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
292 ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
294 for (; len > 0; len--) {
297 if (c >= '0' && c <= '9')
299 digit = (int) (c - '0');
301 else if (base == 16 && c >= 'a' && c <= 'f')
303 digit = (int) (c - 'a') + 10;
305 else if (base == 16 && c >= 'A' && c <= 'F')
307 digit = (int) (c - 'A') + 10;
311 /* `l' means long, and `u' means unsigned. */
314 if (c == 'l' || c == 'L')
317 cppReader_errorLit (pfile,
318 cstring_makeLiteralTemp ("two `l's in integer constant"));
321 else if (c == 'u' || c == 'U')
324 cppReader_errorLit (pfile,
325 cstring_makeLiteralTemp ("two `u's in integer constant"));
330 /*@innerbreak@*/ break;
335 /*@innerbreak@*/ break;
338 /*drl bee: ltc*/ c = *p++;
340 /* Don't look for any more digits after the suffixes. */
344 if (largest_digit < digit)
346 largest_digit = digit;
349 nd = (long unsigned) (n * base + digit);
350 overflow |= (ULONG_MAX_over_base < (unsigned long) n)
351 | (nd < (unsigned long) n);
359 cstring_makeLiteralTemp ("Invalid number in #if expression"));
360 op.op = CPPREADER_ERRORTOK;
364 if (base <= largest_digit)
368 cstring_makeLiteralTemp
369 ("Integer constant contains digits beyond the radix"));
374 /*@i23 add flags for all these...*/
377 cstring_makeLiteralTemp ("Integer constant out of range"));
380 /* If too big to be signed, consider it unsigned. */
381 if ((long) n < 0 && ! op.unsignedp)
387 cstring_makeLiteralTemp ("Integer constant is so large that it is unsigned"));
395 DPRINTF (("Parse number: %d", op.value));
400 /*@null@*/ /*@observer@*/ char *operator;
404 static struct token tokentab2[] = {
409 { "==", CPP_EQUALTOK },
413 { "++", CPPREADER_ERRORTOK },
414 { "--", CPPREADER_ERRORTOK },
415 { NULL, CPPREADER_ERRORTOK }
418 /* Read one token. */
420 struct operation cppexp_lex (cppReader *pfile)
424 register struct token *toktab;
425 enum cpp_token token;
427 char *tok_start, *tok_end;
432 old_written = size_toInt (cpplib_getWritten (pfile));
433 cppSkipHspace (pfile);
434 ic = cpplib_bufPeek (cppReader_getBufferSafe (pfile));
438 DPRINTF (("Read: %c", c));
446 token = cpplib_getTokenForceExpand (pfile);
448 tok_start = pfile->token_buffer + old_written;
449 tok_end = cpplib_getPWritten (pfile);
451 DPRINTF (("Token: %s < %s", tok_start, tok_end));
453 pfile->limit = tok_start;
457 case CPP_EOF: /* Should not happen ... */
462 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->fname))
467 (void) cppReader_popBuffer (pfile);
469 case CPP_HSPACE: case CPP_COMMENT:
472 return cppReader_parseNumber (pfile, tok_start, tok_end - tok_start);
474 cppReader_errorLit (pfile,
475 cstring_makeLiteralTemp ("string constants not allowed in #if expressions"));
476 op.op = CPPREADER_ERRORTOK;
479 /* This code for reading a character constant
480 handles multicharacter constants and wide characters.
481 It is mostly copied from c-lex.c. */
485 size_t width = MAX_CHAR_TYPE_SIZE;
488 char *ptr = tok_start;
489 #ifdef MULTIBYTE_CHARS
490 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
492 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + 1];
499 width = MAX_WCHAR_TYPE_SIZE;
500 #ifdef MULTIBYTE_CHARS
501 max_chars = MB_CUR_MAX;
508 max_chars = size_toInt (MAX_LONG_TYPE_SIZE / width);
512 /*drl bee: hda*/ while (ptr < tok_end && ((c = *ptr++) != '\''))
516 c = cppReader_parseEscape (pfile, &ptr);
517 if (width < HOST_BITS_PER_INT && c >= (1 << width))
521 cstring_makeLiteralTemp ("Escape sequence out of range for character"));
527 /* Merge character into result; ignore excess chars. */
528 if (num_chars < max_chars + 1)
530 if (width < HOST_BITS_PER_INT)
532 result = (int) ((unsigned) result << width) | (c & ((1 << width) - 1));
539 token_buffer[num_chars - 1] = c;
543 /*drl bee: dad*/ token_buffer[num_chars] = 0;
546 cppReader_errorLit (pfile,
547 cstring_makeLiteralTemp ("malformatted character constant"));
548 else if (num_chars == 0)
549 cppReader_errorLit (pfile,
550 cstring_makeLiteralTemp ("empty character constant"));
551 else if (num_chars > max_chars)
553 num_chars = max_chars;
554 cppReader_errorLit (pfile,
555 cstring_makeLiteralTemp ("character constant too long"));
557 else if (num_chars != 1 && ! cppReader_isTraditional (pfile))
559 cppReader_warningLit (pfile,
560 cstring_makeLiteralTemp ("multi-character character constant"));
567 /* If char type is signed, sign-extend the constant. */
570 int num_bits = num_chars * width;
572 if ((cpphash_lookup ("__CHAR_UNSIGNED__",
573 sizeof ("__CHAR_UNSIGNED__") - 1, -1) != NULL)
574 || (((unsigned) result >> (int_toNonNegative (num_bits - 1))) & 1) == 0)
577 = result & ((unsigned long) ~0
578 >> int_toNonNegative ((HOST_BITS_PER_LONG - num_bits)));
583 = result | ~((unsigned long) ~0
584 >> int_toNonNegative ((HOST_BITS_PER_LONG - num_bits)));
589 #ifdef MULTIBYTE_CHARS
590 /* Set the initial shift state and convert the next sequence. */
592 /* In all locales L'\0' is zero and mbtowc will return zero,
595 || (num_chars == 1 && token_buffer[0] != '\0'))
598 (void) mbtowc (NULL, NULL, 0);
599 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
602 cppReader_pedwarn (pfile,"Ignoring invalid multibyte character");
609 /* This is always a signed type. */
610 op.unsignedp = FALSE;
617 return cppReader_parseNumber (pfile, "0", 0);
620 /* See if it is a special token of length 2. */
621 if (tok_start + 2 == tok_end)
623 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
625 /*drl bee: hda*/ if (tok_start[0] == /*drl bee: hda*/ toktab->operator[0]
626 && /*drl bee: hda*/ tok_start[1] == /*drl bee: hda*/ toktab->operator[1])
628 /*@loopbreak@*/ break;
632 if (toktab->token == CPPREADER_ERRORTOK)
634 cppReader_error (pfile, message ("`%s' not allowed in operand of `#if'", cstring_fromChars (tok_start)));
637 op.op = toktab->token;
651 /* Parse a C escape sequence. STRING_PTR points to a variable
652 containing a pointer to the string to parse. That pointer
653 is updated past the characters we use. The value of the
654 escape sequence is returned.
656 A negative value means the sequence \ newline was seen,
657 which is supposed to be equivalent to nothing at all.
659 If \ is followed by a null character, we return a negative
660 value and leave the string pointer pointing at the null character.
662 If \ is followed by 000, we return 0 and leave the string pointer
663 after the zeros. A value of 0 does not mean end of string. */
666 cppReader_parseEscape (cppReader *pfile, char **string_ptr)
668 /*drl bee: pbr*/ char c = *(*string_ptr)++;
678 if (cppReader_isPedantic (pfile))
680 cppReader_pedwarn (pfile,
681 message ("non-ANSI-standard escape sequence, `\\%c'", c));
687 return TARGET_NEWLINE;
709 int i = (int) c - '0';
714 c = *(*string_ptr)++;
715 if (c >= '0' && c <= '7')
717 i = ((unsigned) i << 3) + c - '0';
722 /*drl bee: pbr*/ (*string_ptr)--;
723 /*@loopbreak@*/ break;
726 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
728 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
729 cppReader_pedwarnLit (pfile,
730 cstring_makeLiteralTemp ("octal character constant does not fit in a byte"));
736 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
739 c = *(*string_ptr)++;
741 if (c >= '0' && c <= '9')
743 digit = (unsigned int) (c - '0');
745 else if (c >= 'a' && c <= 'f')
747 digit = (unsigned int) (c - 'a') + 10;
749 else if (c >= 'A' && c <= 'F')
751 digit = (unsigned int) (c - 'A') + 10;
756 /*@loopbreak@*/ break;
758 overflow |= i ^ (i << 4 >> 4);
759 i = (i << 4) + digit;
763 if (digits_found == 0)
765 cppReader_errorLit (pfile,
766 cstring_makeLiteralTemp ("\\x used with no following hex digits"));
769 if ((overflow | (i & ~((1 << int_toNonNegative (BITS_PER_UNIT)) - 1))) != 0)
771 i &= (1 << BITS_PER_UNIT) - 1;
772 cppReader_pedwarnLit (pfile,
773 cstring_makeLiteralTemp ("hex character constant does not fit in a byte"));
784 integer_overflow (cppReader *pfile)
786 if (cppReader_isPedantic (pfile))
787 cppReader_pedwarnLit (pfile,
788 cstring_makeLiteralTemp ("integer overflow in preprocessor expression"));
792 left_shift (cppReader *pfile, long a, bool unsignedp, size_t b)
794 if (b >= HOST_BITS_PER_LONG)
796 if (!unsignedp && a != 0)
798 integer_overflow (pfile);
805 return (unsigned long) a << b;
809 long l = int_toNonNegative (a) << b;
812 # pragma warning( disable : 4018 )
815 if (int_toNonNegative (l) >> b != a)
817 integer_overflow (pfile);
825 right_shift (long a, bool unsignedp, unsigned long b)
827 if (b >= HOST_BITS_PER_LONG)
828 return (unsignedp ? 0 : int_toNonNegative (a) >> (HOST_BITS_PER_LONG - 1));
830 return (unsigned long) a >> b;
832 return int_toNonNegative (a) >> b;
835 /* These priorities are all even, so we can handle associatively. */
837 /*@constant int PAREN_INNER_PRIO@*/
838 #define PAREN_INNER_PRIO 0
840 /*@constant int COMMA_PRIO@*/
843 /*@constant int COND_PRIO@*/
844 #define COND_PRIO (COMMA_PRIO+2)
846 /*@constant int OROR_PRIO@*/
847 #define OROR_PRIO (COND_PRIO+2)
849 /*@constant int ANDAND_PRIO@*/
850 #define ANDAND_PRIO (OROR_PRIO+2)
852 /*@constant int OR_PRIO@*/
853 #define OR_PRIO (ANDAND_PRIO+2)
855 /*@constant int XOR_PRIO@*/
856 #define XOR_PRIO (OR_PRIO+2)
858 /*@constant int AND_PRIO@*/
859 #define AND_PRIO (XOR_PRIO+2)
861 /*@constant int CPP_EQUAL_PRIO@*/
862 #define CPP_EQUAL_PRIO (AND_PRIO+2)
864 /*@constant int LESS_PRIO@*/
865 #define LESS_PRIO (CPP_EQUAL_PRIO+2)
867 /*@constant int SHIFT_PRIO@*/
868 #define SHIFT_PRIO (LESS_PRIO+2)
870 /*@constant int PLUS_PRIO@*/
871 #define PLUS_PRIO (SHIFT_PRIO+2)
873 /*@constant int MUL_PRIO@*/
874 #define MUL_PRIO (PLUS_PRIO+2)
876 /*@constant int UNARY_PRIO@*/
877 #define UNARY_PRIO (MUL_PRIO+2)
879 /*@constant int PAREN_OUTER_PRIO@*/
880 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
883 #define COMPARE(OP) \
884 top->unsignedp = FALSE;\
885 top->value = ((unsigned1 || unsigned2) \
886 ? (unsigned long) v1 OP (unsigned long) v2 \
887 : ((long) v1 OP (long) v2)) ? 1 : 0
889 /* Parse and evaluate a C expression, reading from PFILE.
890 Returns the value of the expression. */
892 /*@constant int INIT_STACK_SIZE@*/
893 # define INIT_STACK_SIZE 20
896 cppReader_parseExpression (cppReader *pfile)
898 /* The implementation is an operator precedence parser,
899 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
901 The stack base is 'stack', and the current stack pointer is 'top'.
902 There is a stack element for each operator (only),
903 and the most recently pushed operator is 'top->op'.
904 An operand (value) is stored in the 'value' field of the stack
905 element of the operator that precedes it.
906 In that case the 'flags' field has the HAVE_VALUE flag set. */
908 struct operation init_stack[INIT_STACK_SIZE];
909 struct operation *stack = init_stack;
910 struct operation *limit = stack + INIT_STACK_SIZE;
911 register struct operation *top = stack;
912 int lprio, rprio = 0;
913 int skip_evaluation = 0;
924 op = cppexp_lex (pfile);
926 /* See if the token is an operand, in which case go to set_value.
927 If the token is an operator, figure out its left and right
928 priorities, and then goto maybe_reduce. */
933 top->value = 0, top->unsignedp = FALSE;
937 top->value = op.value;
938 top->unsignedp = op.unsignedp;
941 lprio = 0; goto maybe_reduce;
943 /* Is this correct if unary ? FIXME */
944 flags = RIGHT_OPERAND_REQUIRED;
945 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
947 flags = RIGHT_OPERAND_REQUIRED;
948 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
949 case '*': case '/': case '%':
950 lprio = MUL_PRIO; goto binop;
951 case '<': case '>': case LEQ: case GEQ:
952 lprio = LESS_PRIO; goto binop;
953 case CPP_EQUALTOK: case NOTEQUAL:
954 lprio = CPP_EQUAL_PRIO; goto binop;
956 lprio = SHIFT_PRIO; goto binop;
957 case '&': lprio = AND_PRIO; goto binop;
958 case '^': lprio = XOR_PRIO; goto binop;
959 case '|': lprio = OR_PRIO; goto binop;
960 case ANDAND: lprio = ANDAND_PRIO; goto binop;
961 case OROR: lprio = OROR_PRIO; goto binop;
963 lprio = COMMA_PRIO; goto binop;
965 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
968 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
971 lprio = COND_PRIO; rprio = COND_PRIO;
974 lprio = COND_PRIO + 1; rprio = COND_PRIO;
977 flags = LEFT_OPERAND_REQUIRED | RIGHT_OPERAND_REQUIRED;
983 message ("Invalid character in #if: %c",
989 /* Push a value onto the stack. */
990 if ((top->flags & HAVE_VALUE) != 0)
992 cppReader_errorLit (pfile,
993 cstring_makeLiteralTemp ("syntax error in #if"));
996 top->flags |= HAVE_VALUE;
1000 /* Push an operator, and check if we can reduce now. */
1001 while (top->rprio > lprio)
1004 long v1 = top[-1].value, v2 = top[0].value;
1005 bool unsigned1 = top[-1].unsignedp;
1006 bool unsigned2 = top[0].unsignedp;
1010 if (((top[1].flags & LEFT_OPERAND_REQUIRED) != 0)
1011 && ((top[0].flags & HAVE_VALUE) == 0))
1013 cppReader_errorLit (pfile,
1014 cstring_makeLiteralTemp ("syntax error - missing left operand"));
1017 if (((top[1].flags & RIGHT_OPERAND_REQUIRED) != 0)
1018 && ((top[1].flags & HAVE_VALUE) == 0))
1020 cppReader_errorLit (pfile,
1021 cstring_makeLiteralTemp ("syntax error - missing right operand"));
1024 /* top[0].value = (top[1].op)(v1, v2);*/
1028 if ((top->flags & HAVE_VALUE) == 0)
1031 top->unsignedp = unsigned2;
1032 top->flags |= HAVE_VALUE;
1036 top->value = v1 + v2;
1037 top->unsignedp = unsigned1 || unsigned2;
1038 if (!top->unsignedp && (skip_evaluation == 0)
1039 && ! possibleSumSign (v1, v2, top->value))
1040 integer_overflow (pfile);
1042 /*@switchbreak@*/ break;
1044 if ((top->flags & HAVE_VALUE) == 0)
1047 if ((skip_evaluation == 0)
1048 && (top->value & v2) < 0 && !unsigned2)
1049 integer_overflow (pfile);
1050 top->unsignedp = unsigned2;
1051 top->flags |= HAVE_VALUE;
1055 top->value = v1 - v2;
1056 top->unsignedp = unsigned1 || unsigned2;
1057 if (!top->unsignedp && (skip_evaluation == 0)
1058 && !possibleSumSign (top->value, v2, v1))
1060 integer_overflow (pfile);
1063 /*@switchbreak@*/ break;
1065 top->unsignedp = unsigned1 || unsigned2;
1069 top->value = (unsigned long) v1 * v2;
1071 else if (skip_evaluation == 0)
1073 top->value = v1 * v2;
1075 && (top->value / v1 != v2
1076 || (top->value & v1 & v2) < 0))
1078 integer_overflow (pfile);
1086 /*@switchbreak@*/ break;
1088 if (skip_evaluation != 0)
1089 /*@switchbreak@*/ break;
1092 cppReader_errorLit (pfile,
1093 cstring_makeLiteralTemp ("Division by zero in #if"));
1096 top->unsignedp = unsigned1 || unsigned2;
1098 top->value = (unsigned long) v1 / v2;
1101 top->value = v1 / v2;
1102 if ((top->value & v1 & v2) < 0)
1103 integer_overflow (pfile);
1105 /*@switchbreak@*/ break;
1107 if (skip_evaluation != 0)
1108 /*@switchbreak@*/ break;
1111 cppReader_errorLit (pfile,
1112 cstring_makeLiteralTemp ("Division by zero in #if"));
1115 top->unsignedp = unsigned1 || unsigned2;
1117 top->value = (unsigned long) v1 % v2;
1119 top->value = v1 % v2;
1120 /*@switchbreak@*/ break;
1122 if ((top->flags & HAVE_VALUE) != 0)
1124 cppReader_errorLit (pfile,
1125 cstring_makeLiteralTemp ("Syntax error"));
1129 top->value = (v2 == 0) ? 1 : 0;
1130 top->unsignedp = FALSE;
1131 top->flags |= HAVE_VALUE;
1132 /*@switchbreak@*/ break;
1134 if ((top->flags & HAVE_VALUE) != 0)
1136 cppReader_errorLit (pfile,
1137 cstring_makeLiteralTemp ("syntax error"));
1141 top->unsignedp = unsigned2;
1142 top->flags |= HAVE_VALUE;
1143 /*@switchbreak@*/ break;
1144 case '<': COMPARE(<); /*@switchbreak@*/ break;
1145 case '>': COMPARE(>); /*@switchbreak@*/ break;
1146 case LEQ: COMPARE(<=); /*@switchbreak@*/ break;
1147 case GEQ: COMPARE(>=); /*@switchbreak@*/ break;
1149 top->value = (v1 == v2) ? 1 : 0;
1150 top->unsignedp = FALSE;
1151 /*@switchbreak@*/ break;
1153 top->value = (v1 != v2) ? 1 : 0;
1154 top->unsignedp = FALSE;
1155 /*@switchbreak@*/ break;
1157 if (skip_evaluation != 0)
1159 /*@switchbreak@*/ break;
1162 top->unsignedp = unsigned1;
1163 if (v2 < 0 && ! unsigned2)
1164 top->value = right_shift (v1, unsigned1, -v2);
1166 top->value = left_shift (pfile, v1, unsigned1, v2);
1167 /*@switchbreak@*/ break;
1169 if (skip_evaluation != 0)
1171 /*@switchbreak@*/ break;
1173 top->unsignedp = unsigned1;
1174 if (v2 < 0 && ! unsigned2)
1175 top->value = left_shift (pfile, v1, unsigned1, -v2);
1177 top->value = right_shift (v1, unsigned1, v2);
1178 /*@switchbreak@*/ break;
1181 #define LOGICAL(OP) \
1182 top->value = v1 OP v2;\
1183 top->unsignedp = unsigned1 || unsigned2;
1185 case '&': LOGICAL(&); /*@switchbreak@*/ break;
1186 case '^': LOGICAL(^); /*@switchbreak@*/ break;
1187 case '|': LOGICAL(|); /*@switchbreak@*/ break;
1189 top->value = ((v1 != 0) && (v2 != 0)) ? 1 : 0;
1190 top->unsignedp = FALSE;
1196 /*@switchbreak@*/ break;
1198 top->value = ((v1 != 0) || (v2 != 0)) ? 1 : 0;
1199 top->unsignedp = FALSE;
1204 /*@switchbreak@*/ break;
1206 if (cppReader_isPedantic (pfile))
1207 cppReader_pedwarnLit (pfile,
1208 cstring_makeLiteralTemp ("comma operator in operand of `#if'"));
1210 top->unsignedp = unsigned2;
1211 /*@switchbreak@*/ break;
1213 cppReader_errorLit (pfile,
1214 cstring_makeLiteralTemp ("syntax error in #if"));
1217 if (top[0].op != '?')
1219 cppReader_errorLit (pfile,
1220 cstring_makeLiteralTemp ("syntax error ':' without preceding '?'"));
1223 else if (((top[1].flags & HAVE_VALUE) == 0)
1224 || ((top[-1].flags & HAVE_VALUE) == 0)
1225 || ((top[0].flags & HAVE_VALUE) == 0))
1227 cppReader_errorLit (pfile,
1228 cstring_makeLiteralTemp ("bad syntax for ?: operator"));
1234 if (top->value != 0)
1239 top->value = (top->value != 0) ? v1 : v2;
1240 top->unsignedp = unsigned1 || unsigned2;
1242 /*@switchbreak@*/ break;
1244 if (((top[1].flags & HAVE_VALUE) != 0)
1245 || ((top[0].flags & HAVE_VALUE) == 0)
1247 || ((top[-1].flags & HAVE_VALUE) != 0))
1249 cppReader_errorLit (pfile,
1250 cstring_makeLiteralTemp ("mismatched parentheses in #if"));
1257 top->unsignedp = unsigned1;
1258 top->flags |= HAVE_VALUE;
1260 /*@switchbreak@*/ break;
1264 top[1].op >= ' ' && top[1].op <= '~'
1265 ? "unimplemented operator '%c'\n"
1266 : "unimplemented operator '\\%03o'\n",
1277 cppReader_errorLit (pfile,
1278 cstring_makeLiteralTemp ("internal error in #if expression"));
1283 if (stack != init_stack)
1287 } /*@=branchstate@*/
1293 /* Check for and handle stack overflow. */
1296 struct operation *new_stack;
1297 int old_size = (char *) limit - (char *) stack;
1298 size_t new_size = size_fromInt (2 * old_size);
1300 if (stack != init_stack)
1302 new_stack = (struct operation *) drealloc ((char *) stack,
1307 new_stack = (struct operation *) dmalloc (new_size);
1309 /* Bug: the parameters were in the wrong order! */
1310 memcpy ((char *) new_stack, (char *) stack, old_size);
1312 } /*@=branchstate@*/
1315 top = (struct operation *) ((char *) new_stack + old_size);
1316 limit = (struct operation *) ((char *) new_stack + new_size);
1318 } /*@=branchstate@*/
1323 if ((op.op == OROR && (top[-1].value != 0))
1324 || (op.op == ANDAND && (top[-1].value == 0))
1325 || (op.op == '?' && (top[-1].value == 0)))
1329 else if (op.op == ':')
1331 if (top[-2].value != 0) /* Was condition true? */
1347 if (stack != init_stack)
1351 } /*@=branchstate@*/
1354 cppReader_skipRestOfLine (pfile);