2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 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"));
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"));
395 /*@null@*/ /*@observer@*/ char *operator;
399 static struct token tokentab2[] = {
404 { "==", CPP_EQUALTOK },
408 { "++", CPPREADER_ERRORTOK },
409 { "--", CPPREADER_ERRORTOK },
410 { NULL, CPPREADER_ERRORTOK }
413 /* Read one token. */
415 struct operation cppReader_lex (cppReader *pfile)
419 register struct token *toktab;
420 enum cpp_token token;
422 char *tok_start, *tok_end;
427 old_written = size_toInt (cppReader_getWritten (pfile));
428 cppSkipHspace (pfile);
429 ic = cppBufPeek (cppReader_getBufferSafe (pfile));
440 token = cppGetToken (pfile);
441 tok_start = pfile->token_buffer + old_written;
442 tok_end = cppReader_getPWritten (pfile);
443 pfile->limit = tok_start;
447 case CPP_EOF: /* Should not happen ... */
452 if (cstring_isDefined (cppReader_getBufferSafe (pfile)->fname))
457 (void) cppReader_popBuffer (pfile);
459 case CPP_HSPACE: case CPP_COMMENT:
462 return cppReader_parseNumber (pfile, tok_start, tok_end - tok_start);
464 cppReader_errorLit (pfile,
465 cstring_makeLiteralTemp ("string constants not allowed in #if expressions"));
466 op.op = CPPREADER_ERRORTOK;
469 /* This code for reading a character constant
470 handles multicharacter constants and wide characters.
471 It is mostly copied from c-lex.c. */
475 size_t width = MAX_CHAR_TYPE_SIZE;
478 char *ptr = tok_start;
479 #ifdef MULTIBYTE_CHARS
480 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
482 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + 1];
489 width = MAX_WCHAR_TYPE_SIZE;
490 #ifdef MULTIBYTE_CHARS
491 max_chars = MB_CUR_MAX;
498 max_chars = size_toInt (MAX_LONG_TYPE_SIZE / width);
502 while (ptr < tok_end && ((c = *ptr++) != '\''))
506 c = cppReader_parseEscape (pfile, &ptr);
507 if (width < HOST_BITS_PER_INT && c >= (1 << width))
511 cstring_makeLiteralTemp ("Escape sequence out of range for character"));
517 /* Merge character into result; ignore excess chars. */
518 if (num_chars < max_chars + 1)
520 if (width < HOST_BITS_PER_INT)
522 result = (int) ((unsigned) result << width) | (c & ((1 << width) - 1));
529 token_buffer[num_chars - 1] = c;
533 token_buffer[num_chars] = 0;
536 cppReader_errorLit (pfile,
537 cstring_makeLiteralTemp ("malformatted character constant"));
538 else if (num_chars == 0)
539 cppReader_errorLit (pfile,
540 cstring_makeLiteralTemp ("empty character constant"));
541 else if (num_chars > max_chars)
543 num_chars = max_chars;
544 cppReader_errorLit (pfile,
545 cstring_makeLiteralTemp ("character constant too long"));
547 else if (num_chars != 1 && ! cppReader_isTraditional (pfile))
549 cppReader_warningLit (pfile,
550 cstring_makeLiteralTemp ("multi-character character constant"));
557 /* If char type is signed, sign-extend the constant. */
560 int num_bits = num_chars * width;
562 if ((cppReader_lookup ("__CHAR_UNSIGNED__",
563 sizeof ("__CHAR_UNSIGNED__") - 1, -1) != NULL)
564 || (((unsigned) result >> (num_bits - 1)) & 1) == 0)
567 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
572 = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
577 #ifdef MULTIBYTE_CHARS
578 /* Set the initial shift state and convert the next sequence. */
580 /* In all locales L'\0' is zero and mbtowc will return zero,
583 || (num_chars == 1 && token_buffer[0] != '\0'))
586 (void) mbtowc (NULL, NULL, 0);
587 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
590 cppReader_pedwarn (pfile,"Ignoring invalid multibyte character");
597 /* This is always a signed type. */
598 op.unsignedp = FALSE;
604 return cppReader_parseNumber (pfile, "0", 0);
607 /* See if it is a special token of length 2. */
608 if (tok_start + 2 == tok_end)
610 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
612 if (tok_start[0] == toktab->operator[0]
613 && tok_start[1] == toktab->operator[1])
615 /*@loopbreak@*/ break;
619 if (toktab->token == CPPREADER_ERRORTOK)
621 cppReader_error (pfile, message ("`%s' not allowed in operand of `#if'", cstring_fromChars (tok_start)));
624 op.op = toktab->token;
638 /* Parse a C escape sequence. STRING_PTR points to a variable
639 containing a pointer to the string to parse. That pointer
640 is updated past the characters we use. The value of the
641 escape sequence is returned.
643 A negative value means the sequence \ newline was seen,
644 which is supposed to be equivalent to nothing at all.
646 If \ is followed by a null character, we return a negative
647 value and leave the string pointer pointing at the null character.
649 If \ is followed by 000, we return 0 and leave the string pointer
650 after the zeros. A value of 0 does not mean end of string. */
653 cppReader_parseEscape (cppReader *pfile, char **string_ptr)
655 char c = *(*string_ptr)++;
665 if (cppReader_isPedantic (pfile))
667 cppReader_pedwarn (pfile,
668 message ("non-ANSI-standard escape sequence, `\\%c'", c));
674 return TARGET_NEWLINE;
696 int i = (int) c - '0';
701 c = *(*string_ptr)++;
702 if (c >= '0' && c <= '7')
704 i = ((unsigned) i << 3) + c - '0';
710 /*@loopbreak@*/ break;
713 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
715 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
716 cppReader_pedwarnLit (pfile,
717 cstring_makeLiteralTemp ("octal character constant does not fit in a byte"));
723 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
726 c = *(*string_ptr)++;
728 if (c >= '0' && c <= '9')
730 digit = (unsigned int) (c - '0');
732 else if (c >= 'a' && c <= 'f')
734 digit = (unsigned int) (c - 'a') + 10;
736 else if (c >= 'A' && c <= 'F')
738 digit = (unsigned int) (c - 'A') + 10;
743 /*@loopbreak@*/ break;
745 overflow |= i ^ (i << 4 >> 4);
746 i = (i << 4) + digit;
750 if (digits_found == 0)
752 cppReader_errorLit (pfile,
753 cstring_makeLiteralTemp ("\\x used with no following hex digits"));
756 if ((overflow | (i & ~((1 << BITS_PER_UNIT) - 1))) != 0)
758 i &= (1 << BITS_PER_UNIT) - 1;
759 cppReader_pedwarnLit (pfile,
760 cstring_makeLiteralTemp ("hex character constant does not fit in a byte"));
771 integer_overflow (cppReader *pfile)
773 if (cppReader_isPedantic (pfile))
774 cppReader_pedwarnLit (pfile,
775 cstring_makeLiteralTemp ("integer overflow in preprocessor expression"));
779 left_shift (cppReader *pfile, long a, bool unsignedp, size_t b)
781 if (b >= HOST_BITS_PER_LONG)
783 if (!unsignedp && a != 0)
785 integer_overflow (pfile);
792 return (unsigned long) a << b;
800 integer_overflow (pfile);
808 right_shift (long a, bool unsignedp, unsigned long b)
810 if (b >= HOST_BITS_PER_LONG)
811 return (unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1));
813 return (unsigned long) a >> b;
818 /* These priorities are all even, so we can handle associatively. */
820 /*@constant int PAREN_INNER_PRIO@*/
821 #define PAREN_INNER_PRIO 0
823 /*@constant int COMMA_PRIO@*/
826 /*@constant int COND_PRIO@*/
827 #define COND_PRIO (COMMA_PRIO+2)
829 /*@constant int OROR_PRIO@*/
830 #define OROR_PRIO (COND_PRIO+2)
832 /*@constant int ANDAND_PRIO@*/
833 #define ANDAND_PRIO (OROR_PRIO+2)
835 /*@constant int OR_PRIO@*/
836 #define OR_PRIO (ANDAND_PRIO+2)
838 /*@constant int XOR_PRIO@*/
839 #define XOR_PRIO (OR_PRIO+2)
841 /*@constant int AND_PRIO@*/
842 #define AND_PRIO (XOR_PRIO+2)
844 /*@constant int CPP_EQUAL_PRIO@*/
845 #define CPP_EQUAL_PRIO (AND_PRIO+2)
847 /*@constant int LESS_PRIO@*/
848 #define LESS_PRIO (CPP_EQUAL_PRIO+2)
850 /*@constant int SHIFT_PRIO@*/
851 #define SHIFT_PRIO (LESS_PRIO+2)
853 /*@constant int PLUS_PRIO@*/
854 #define PLUS_PRIO (SHIFT_PRIO+2)
856 /*@constant int MUL_PRIO@*/
857 #define MUL_PRIO (PLUS_PRIO+2)
859 /*@constant int UNARY_PRIO@*/
860 #define UNARY_PRIO (MUL_PRIO+2)
862 /*@constant int PAREN_OUTER_PRIO@*/
863 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
866 #define COMPARE(OP) \
867 top->unsignedp = FALSE;\
868 top->value = ((unsigned1 || unsigned2) \
869 ? (unsigned long) v1 OP (unsigned long) v2 \
870 : ((long) v1 OP (long) v2)) ? 1 : 0
872 /* Parse and evaluate a C expression, reading from PFILE.
873 Returns the value of the expression. */
875 /*@constant int INIT_STACK_SIZE@*/
876 # define INIT_STACK_SIZE 20
879 cppReader_parseExpression (cppReader *pfile)
881 /* The implementation is an operator precedence parser,
882 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
884 The stack base is 'stack', and the current stack pointer is 'top'.
885 There is a stack element for each operator (only),
886 and the most recently pushed operator is 'top->op'.
887 An operand (value) is stored in the 'value' field of the stack
888 element of the operator that precedes it.
889 In that case the 'flags' field has the HAVE_VALUE flag set. */
891 struct operation init_stack[INIT_STACK_SIZE];
892 struct operation *stack = init_stack;
893 struct operation *limit = stack + INIT_STACK_SIZE;
894 register struct operation *top = stack;
895 int lprio, rprio = 0;
896 int skip_evaluation = 0;
907 op = cppReader_lex (pfile);
909 /* See if the token is an operand, in which case go to set_value.
910 If the token is an operator, figure out its left and right
911 priorities, and then goto maybe_reduce. */
916 top->value = 0, top->unsignedp = FALSE;
920 top->value = op.value;
921 top->unsignedp = op.unsignedp;
924 lprio = 0; goto maybe_reduce;
926 /* Is this correct if unary ? FIXME */
927 flags = RIGHT_OPERAND_REQUIRED;
928 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
930 flags = RIGHT_OPERAND_REQUIRED;
931 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
932 case '*': case '/': case '%':
933 lprio = MUL_PRIO; goto binop;
934 case '<': case '>': case LEQ: case GEQ:
935 lprio = LESS_PRIO; goto binop;
936 case CPP_EQUALTOK: case NOTEQUAL:
937 lprio = CPP_EQUAL_PRIO; goto binop;
939 lprio = SHIFT_PRIO; goto binop;
940 case '&': lprio = AND_PRIO; goto binop;
941 case '^': lprio = XOR_PRIO; goto binop;
942 case '|': lprio = OR_PRIO; goto binop;
943 case ANDAND: lprio = ANDAND_PRIO; goto binop;
944 case OROR: lprio = OROR_PRIO; goto binop;
946 lprio = COMMA_PRIO; goto binop;
948 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
951 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
954 lprio = COND_PRIO; rprio = COND_PRIO;
957 lprio = COND_PRIO + 1; rprio = COND_PRIO;
960 flags = LEFT_OPERAND_REQUIRED | RIGHT_OPERAND_REQUIRED;
966 message ("Invalid character in #if: %c",
972 /* Push a value onto the stack. */
973 if ((top->flags & HAVE_VALUE) != 0)
975 cppReader_errorLit (pfile,
976 cstring_makeLiteralTemp ("syntax error in #if"));
979 top->flags |= HAVE_VALUE;
983 /* Push an operator, and check if we can reduce now. */
984 while (top->rprio > lprio)
987 long v1 = top[-1].value, v2 = top[0].value;
988 bool unsigned1 = top[-1].unsignedp;
989 bool unsigned2 = top[0].unsignedp;
993 if (((top[1].flags & LEFT_OPERAND_REQUIRED) != 0)
994 && ((top[0].flags & HAVE_VALUE) == 0))
996 cppReader_errorLit (pfile,
997 cstring_makeLiteralTemp ("syntax error - missing left operand"));
1000 if (((top[1].flags & RIGHT_OPERAND_REQUIRED) != 0)
1001 && ((top[1].flags & HAVE_VALUE) == 0))
1003 cppReader_errorLit (pfile,
1004 cstring_makeLiteralTemp ("syntax error - missing right operand"));
1007 /* top[0].value = (top[1].op)(v1, v2);*/
1011 if ((top->flags & HAVE_VALUE) == 0)
1014 top->unsignedp = unsigned2;
1015 top->flags |= HAVE_VALUE;
1019 top->value = v1 + v2;
1020 top->unsignedp = unsigned1 || unsigned2;
1021 if (!top->unsignedp && (skip_evaluation == 0)
1022 && ! possibleSumSign (v1, v2, top->value))
1023 integer_overflow (pfile);
1025 /*@switchbreak@*/ break;
1027 if ((top->flags & HAVE_VALUE) == 0)
1030 if ((skip_evaluation == 0)
1031 && (top->value & v2) < 0 && !unsigned2)
1032 integer_overflow (pfile);
1033 top->unsignedp = unsigned2;
1034 top->flags |= HAVE_VALUE;
1038 top->value = v1 - v2;
1039 top->unsignedp = unsigned1 || unsigned2;
1040 if (!top->unsignedp && (skip_evaluation == 0)
1041 && !possibleSumSign (top->value, v2, v1))
1043 integer_overflow (pfile);
1046 /*@switchbreak@*/ break;
1048 top->unsignedp = unsigned1 || unsigned2;
1052 top->value = (unsigned long) v1 * v2;
1054 else if (skip_evaluation == 0)
1056 top->value = v1 * v2;
1058 && (top->value / v1 != v2
1059 || (top->value & v1 & v2) < 0))
1061 integer_overflow (pfile);
1069 /*@switchbreak@*/ break;
1071 if (skip_evaluation != 0)
1072 /*@switchbreak@*/ break;
1075 cppReader_errorLit (pfile,
1076 cstring_makeLiteralTemp ("Division by zero in #if"));
1079 top->unsignedp = unsigned1 || unsigned2;
1081 top->value = (unsigned long) v1 / v2;
1084 top->value = v1 / v2;
1085 if ((top->value & v1 & v2) < 0)
1086 integer_overflow (pfile);
1088 /*@switchbreak@*/ break;
1090 if (skip_evaluation != 0)
1091 /*@switchbreak@*/ break;
1094 cppReader_errorLit (pfile,
1095 cstring_makeLiteralTemp ("Division by zero in #if"));
1098 top->unsignedp = unsigned1 || unsigned2;
1100 top->value = (unsigned long) v1 % v2;
1102 top->value = v1 % v2;
1103 /*@switchbreak@*/ break;
1105 if ((top->flags & HAVE_VALUE) != 0)
1107 cppReader_errorLit (pfile,
1108 cstring_makeLiteralTemp ("Syntax error"));
1112 top->value = (v2 == 0) ? 1 : 0;
1113 top->unsignedp = FALSE;
1114 top->flags |= HAVE_VALUE;
1115 /*@switchbreak@*/ break;
1117 if ((top->flags & HAVE_VALUE) != 0)
1119 cppReader_errorLit (pfile,
1120 cstring_makeLiteralTemp ("syntax error"));
1124 top->unsignedp = unsigned2;
1125 top->flags |= HAVE_VALUE;
1126 /*@switchbreak@*/ break;
1127 case '<': COMPARE(<); /*@switchbreak@*/ break;
1128 case '>': COMPARE(>); /*@switchbreak@*/ break;
1129 case LEQ: COMPARE(<=); /*@switchbreak@*/ break;
1130 case GEQ: COMPARE(>=); /*@switchbreak@*/ break;
1132 top->value = (v1 == v2) ? 1 : 0;
1133 top->unsignedp = FALSE;
1134 /*@switchbreak@*/ break;
1136 top->value = (v1 != v2) ? 1 : 0;
1137 top->unsignedp = FALSE;
1138 /*@switchbreak@*/ break;
1140 if (skip_evaluation != 0)
1142 /*@switchbreak@*/ break;
1145 top->unsignedp = unsigned1;
1146 if (v2 < 0 && ! unsigned2)
1147 top->value = right_shift (v1, unsigned1, -v2);
1149 top->value = left_shift (pfile, v1, unsigned1, v2);
1150 /*@switchbreak@*/ break;
1152 if (skip_evaluation != 0)
1154 /*@switchbreak@*/ break;
1156 top->unsignedp = unsigned1;
1157 if (v2 < 0 && ! unsigned2)
1158 top->value = left_shift (pfile, v1, unsigned1, -v2);
1160 top->value = right_shift (v1, unsigned1, v2);
1161 /*@switchbreak@*/ break;
1164 #define LOGICAL(OP) \
1165 top->value = v1 OP v2;\
1166 top->unsignedp = unsigned1 || unsigned2;
1168 case '&': LOGICAL(&); /*@switchbreak@*/ break;
1169 case '^': LOGICAL(^); /*@switchbreak@*/ break;
1170 case '|': LOGICAL(|); /*@switchbreak@*/ break;
1172 top->value = ((v1 != 0) && (v2 != 0)) ? 1 : 0;
1173 top->unsignedp = FALSE;
1179 /*@switchbreak@*/ break;
1181 top->value = ((v1 != 0) || (v2 != 0)) ? 1 : 0;
1182 top->unsignedp = FALSE;
1187 /*@switchbreak@*/ break;
1189 if (cppReader_isPedantic (pfile))
1190 cppReader_pedwarnLit (pfile,
1191 cstring_makeLiteralTemp ("comma operator in operand of `#if'"));
1193 top->unsignedp = unsigned2;
1194 /*@switchbreak@*/ break;
1196 cppReader_errorLit (pfile,
1197 cstring_makeLiteralTemp ("syntax error in #if"));
1200 if (top[0].op != '?')
1202 cppReader_errorLit (pfile,
1203 cstring_makeLiteralTemp ("syntax error ':' without preceding '?'"));
1206 else if (((top[1].flags & HAVE_VALUE) == 0)
1207 || ((top[-1].flags & HAVE_VALUE) == 0)
1208 || ((top[0].flags & HAVE_VALUE) == 0))
1210 cppReader_errorLit (pfile,
1211 cstring_makeLiteralTemp ("bad syntax for ?: operator"));
1217 if (top->value != 0)
1222 top->value = (top->value != 0) ? v1 : v2;
1223 top->unsignedp = unsigned1 || unsigned2;
1225 /*@switchbreak@*/ break;
1227 if (((top[1].flags & HAVE_VALUE) != 0)
1228 || ((top[0].flags & HAVE_VALUE) == 0)
1230 || ((top[-1].flags & HAVE_VALUE) != 0))
1232 cppReader_errorLit (pfile,
1233 cstring_makeLiteralTemp ("mismatched parentheses in #if"));
1240 top->unsignedp = unsigned1;
1241 top->flags |= HAVE_VALUE;
1243 /*@switchbreak@*/ break;
1246 top[1].op >= ' ' && top[1].op <= '~'
1247 ? "unimplemented operator '%c'\n"
1248 : "unimplemented operator '\\%03o'\n",
1258 cppReader_errorLit (pfile,
1259 cstring_makeLiteralTemp ("internal error in #if expression"));
1264 if (stack != init_stack)
1268 } /*@=branchstate@*/
1274 /* Check for and handle stack overflow. */
1277 struct operation *new_stack;
1278 int old_size = (char *) limit - (char *) stack;
1279 size_t new_size = size_fromInt (2 * old_size);
1281 if (stack != init_stack)
1283 new_stack = (struct operation *) drealloc ((char *) stack,
1288 new_stack = (struct operation *) dmalloc (new_size);
1290 /* Bug: the parameters were in the wrong order! */
1291 memcpy ((char *) new_stack, (char *) stack, old_size);
1293 } /*@=branchstate@*/
1296 top = (struct operation *) ((char *) new_stack + old_size);
1297 limit = (struct operation *) ((char *) new_stack + new_size);
1299 } /*@=branchstate@*/
1304 if ((op.op == OROR && (top[-1].value != 0))
1305 || (op.op == ANDAND && (top[-1].value == 0))
1306 || (op.op == '?' && (top[-1].value == 0)))
1310 else if (op.op == ':')
1312 if (top[-2].value != 0) /* Was condition true? */
1328 if (stack != init_stack)
1332 } /*@=branchstate@*/
1335 cppReader_skipRestOfLine (pfile);