Commit b22ef131 by Neil Booth Committed by Neil Booth

cppexp.c: wrap long lines.

	* cppexp.c: wrap long lines.  New macros CPP_ICE, SYNTAX_ERROR
	and SYNTAX_ERROR2. Replace `' in messages with ''.
	(op_to_str): Make re-entrant.
	(_cpp_parse_expr): Implement new error macros. Use | rather
	than || to logically or 2 boolean integers.  Simply expression
	checking we have a left operand iff needed.

From-SVN: r32890
parent d799cfa6
2000-04-03 Neil Booth <NeilB@earthling.net>
* cppexp.c: wrap long lines. New macros CPP_ICE, SYNTAX_ERROR
and SYNTAX_ERROR2. Replace `' in messages with ''.
(op_to_str): Make re-entrant.
(_cpp_parse_expr): Implement new error macros. Use | rather
than || to logically or 2 boolean integers. Simply expression
checking we have a left operand iff needed.
2000-04-03 Nick Clifton <nickc@cygnus.com> 2000-04-03 Nick Clifton <nickc@cygnus.com>
* Makefile.in (diagnostic.o): Depend upon diagnostic.c * Makefile.in (diagnostic.o): Depend upon diagnostic.c
......
...@@ -90,7 +90,7 @@ static struct operation parse_defined PARAMS ((cpp_reader *)); ...@@ -90,7 +90,7 @@ static struct operation parse_defined PARAMS ((cpp_reader *));
static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, U_CHAR **, static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, U_CHAR **,
HOST_WIDEST_INT)); HOST_WIDEST_INT));
static struct operation lex PARAMS ((cpp_reader *, int)); static struct operation lex PARAMS ((cpp_reader *, int));
static const char * op_to_str PARAMS ((op_t)); static const char * op_to_str PARAMS ((op_t, char *));
#define ERROR 299 #define ERROR 299
#define OROR 300 #define OROR 300
...@@ -159,7 +159,8 @@ parse_number (pfile, start, end) ...@@ -159,7 +159,8 @@ parse_number (pfile, start, end)
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
digit = c - '0'; digit = c - '0';
else if (base == 16 && c >= 'a' && c <= 'f') /* FIXME: assumes ASCII */ /* FIXME: assumes ASCII */
else if (base == 16 && c >= 'a' && c <= 'f')
digit = c - 'a' + 10; digit = c - 'a' + 10;
else if (base == 16 && c >= 'A' && c <= 'F') else if (base == 16 && c >= 'A' && c <= 'F')
digit = c - 'A' + 10; digit = c - 'A' + 10;
...@@ -181,9 +182,9 @@ parse_number (pfile, start, end) ...@@ -181,9 +182,9 @@ parse_number (pfile, start, end)
op.unsignedp++; op.unsignedp++;
else else
{ {
/* Decrement p here so that the error for an invalid number /* Decrement p here so that the error for an invalid
will be generated below in the case where this is the number will be generated below in the case where
last character in the buffer. */ this is the last character in the buffer. */
p--; p--;
break; break;
} }
...@@ -209,17 +210,18 @@ parse_number (pfile, start, end) ...@@ -209,17 +210,18 @@ parse_number (pfile, start, end)
} }
else if (spec_long > (CPP_OPTION (pfile, c89) ? 1 : 2)) else if (spec_long > (CPP_OPTION (pfile, c89) ? 1 : 2))
{ {
cpp_error (pfile, "too many `l' suffixes in integer constant"); cpp_error (pfile, "too many 'l' suffixes in integer constant");
goto error; goto error;
} }
else if (op.unsignedp > 1) else if (op.unsignedp > 1)
{ {
cpp_error (pfile, "too many `u' suffixes in integer constant"); cpp_error (pfile, "too many 'u' suffixes in integer constant");
goto error; goto error;
} }
if (base <= largest_digit) if (base <= largest_digit)
cpp_pedwarn (pfile, "integer constant contains digits beyond the radix"); cpp_pedwarn (pfile,
"integer constant contains digits beyond the radix");
if (overflow) if (overflow)
cpp_pedwarn (pfile, "integer constant out of range"); cpp_pedwarn (pfile, "integer constant out of range");
...@@ -279,7 +281,8 @@ parse_charconst (pfile, start, end) ...@@ -279,7 +281,8 @@ parse_charconst (pfile, start, end)
c = parse_escape (pfile, &ptr, mask); c = parse_escape (pfile, &ptr, mask);
if (width < HOST_BITS_PER_INT if (width < HOST_BITS_PER_INT
&& (unsigned int) c >= (unsigned int)(1 << width)) && (unsigned int) c >= (unsigned int)(1 << width))
cpp_pedwarn (pfile, "escape sequence out of range for character"); cpp_pedwarn (pfile,
"escape sequence out of range for character");
} }
/* Merge character into result; ignore excess chars. */ /* Merge character into result; ignore excess chars. */
...@@ -375,7 +378,7 @@ parse_defined (pfile) ...@@ -375,7 +378,7 @@ parse_defined (pfile)
oops: oops:
CPP_SET_WRITTEN (pfile, old_written); CPP_SET_WRITTEN (pfile, old_written);
pfile->no_macro_expand--; pfile->no_macro_expand--;
cpp_error (pfile, "`defined' without an identifier"); cpp_error (pfile, "'defined' without an identifier");
op.op = ERROR; op.op = ERROR;
return op; return op;
...@@ -431,7 +434,8 @@ lex (pfile, skip_evaluation) ...@@ -431,7 +434,8 @@ lex (pfile, skip_evaluation)
return parse_number (pfile, tok_start, tok_end); return parse_number (pfile, tok_start, tok_end);
case CPP_STRING: case CPP_STRING:
case CPP_WSTRING: case CPP_WSTRING:
cpp_error (pfile, "string constants are not allowed in #if expressions"); cpp_error (pfile,
"string constants are not allowed in #if expressions");
op.op = ERROR; op.op = ERROR;
return op; return op;
...@@ -448,7 +452,7 @@ lex (pfile, skip_evaluation) ...@@ -448,7 +452,7 @@ lex (pfile, skip_evaluation)
op.value = 0; op.value = 0;
if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation) if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
cpp_warning (pfile, "`%.*s' is not defined", cpp_warning (pfile, "'%.*s' is not defined",
(int) (tok_end - tok_start), tok_start); (int) (tok_end - tok_start), tok_start);
return op; return op;
...@@ -467,7 +471,7 @@ lex (pfile, skip_evaluation) ...@@ -467,7 +471,7 @@ lex (pfile, skip_evaluation)
&& tok_start[1] == toktab->operator[1]) && tok_start[1] == toktab->operator[1])
break; break;
if (toktab->token == ERROR) if (toktab->token == ERROR)
cpp_error (pfile, "`%s' not allowed in operand of `#if'", cpp_error (pfile, "'%s' not allowed in operand of #if",
tok_start); tok_start);
op.op = toktab->token; op.op = toktab->token;
return op; return op;
...@@ -479,12 +483,14 @@ lex (pfile, skip_evaluation) ...@@ -479,12 +483,14 @@ lex (pfile, skip_evaluation)
} }
} }
/* Convert an operator ID to a string. */ /* Convert an operator ID to a string. BUFF is a buffer at least 5
characters long which might be used to store the string. */
/* XXX FIXME: Remove BUFF when new lexer is implemented. */
static const char * static const char *
op_to_str (op) op_to_str (op, buff)
op_t op; op_t op;
char *buff;
{ {
static char str[5]; /* XXX static variable. */
const struct token *toktab; const struct token *toktab;
/* See if it is a special token of length 2. */ /* See if it is a special token of length 2. */
...@@ -493,10 +499,10 @@ op_to_str (op) ...@@ -493,10 +499,10 @@ op_to_str (op)
return toktab->operator; return toktab->operator;
if (ISGRAPH (op)) if (ISGRAPH (op))
sprintf (str, "%c", (int) op); sprintf (buff, "%c", (int) op);
else else
sprintf (str, "\\%03o", (int) op); sprintf (buff, "\\%03o", (int) op);
return str; return buff;
} }
/* Parse a C escape sequence. STRING_PTR points to a variable /* Parse a C escape sequence. STRING_PTR points to a variable
...@@ -529,7 +535,7 @@ parse_escape (pfile, string_ptr, result_mask) ...@@ -529,7 +535,7 @@ parse_escape (pfile, string_ptr, result_mask)
case 'e': case 'e':
case 'E': case 'E':
if (CPP_PEDANTIC (pfile)) if (CPP_PEDANTIC (pfile))
cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c); cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, '\\%c'", c);
return TARGET_ESC; return TARGET_ESC;
case 'f': case 'f':
return TARGET_FF; return TARGET_FF;
...@@ -723,7 +729,7 @@ be handled with operator-specific code. */ ...@@ -723,7 +729,7 @@ be handled with operator-specific code. */
#define COMPARE(OP) \ #define COMPARE(OP) \
top->unsignedp = 0; \ top->unsignedp = 0; \
top->value = (unsigned1 || unsigned2) \ top->value = (unsigned1 | unsigned2) \
? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \ ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
: (v1 OP v2) : (v1 OP v2)
#define EQUALITY(OP) \ #define EQUALITY(OP) \
...@@ -731,7 +737,16 @@ be handled with operator-specific code. */ ...@@ -731,7 +737,16 @@ be handled with operator-specific code. */
top->unsignedp = 0; top->unsignedp = 0;
#define LOGICAL(OP) \ #define LOGICAL(OP) \
top->value = v1 OP v2; \ top->value = v1 OP v2; \
top->unsignedp = unsigned1 || unsigned2; top->unsignedp = unsigned1 | unsigned2;
/* With -O2, gcc appears to produce nice code, moving the error
message load and subsequent jump completely out of the main path. */
#define CPP_ICE(msgid) \
do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
#define SYNTAX_ERROR(msgid) \
do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
#define SYNTAX_ERROR2(msgid, arg) \
do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
/* Parse and evaluate a C expression, reading from PFILE. /* Parse and evaluate a C expression, reading from PFILE.
Returns the truth value of the expression. */ Returns the truth value of the expression. */
...@@ -758,6 +773,7 @@ _cpp_parse_expr (pfile) ...@@ -758,6 +773,7 @@ _cpp_parse_expr (pfile)
long old_written = CPP_WRITTEN (pfile); long old_written = CPP_WRITTEN (pfile);
int skip_evaluation = 0; int skip_evaluation = 0;
int result; int result;
char buff[5];
pfile->parsing_if_directive++; pfile->parsing_if_directive++;
/* We've finished when we try to reduce this. */ /* We've finished when we try to reduce this. */
...@@ -782,7 +798,7 @@ _cpp_parse_expr (pfile) ...@@ -782,7 +798,7 @@ _cpp_parse_expr (pfile)
switch (op.op) switch (op.op)
{ {
case NAME: case NAME:
cpp_ice (pfile, "lex returns a NAME"); CPP_ICE ("lex returns a NAME");
case ERROR: case ERROR:
goto syntax_error; goto syntax_error;
case '#': case '#':
...@@ -791,18 +807,14 @@ _cpp_parse_expr (pfile) ...@@ -791,18 +807,14 @@ _cpp_parse_expr (pfile)
already issued an error. */ already issued an error. */
goto syntax_error; goto syntax_error;
default: default:
cpp_error (pfile, "invalid character in #if"); SYNTAX_ERROR ("invalid character in #if");
goto syntax_error;
push_immediate:
case INT: case INT:
case CHAR: case CHAR:
push_immediate:
/* Push a value onto the stack. */ /* Push a value onto the stack. */
if (top->flags & HAVE_VALUE) if (top->flags & HAVE_VALUE)
{ SYNTAX_ERROR ("missing binary operator");
cpp_error (pfile, "missing binary operator");
goto syntax_error;
}
top->value = op.value; top->value = op.value;
top->unsignedp = op.unsignedp; top->unsignedp = op.unsignedp;
top->flags |= HAVE_VALUE; top->flags |= HAVE_VALUE;
...@@ -855,11 +867,10 @@ _cpp_parse_expr (pfile) ...@@ -855,11 +867,10 @@ _cpp_parse_expr (pfile)
if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0) if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
{ {
if (top->op == '(') if (top->op == '(')
cpp_error (pfile, "void expression between '(' and ')'"); SYNTAX_ERROR ("void expression between '(' and ')'");
else else
cpp_error (pfile, "operator `%s' has no right operand", SYNTAX_ERROR2 ("operator '%s' has no right operand",
op_to_str (top->op)); op_to_str (top->op, buff));
goto syntax_error;
} }
unsigned2 = top->unsignedp, v2 = top->value; unsigned2 = top->unsignedp, v2 = top->value;
...@@ -879,7 +890,7 @@ _cpp_parse_expr (pfile) ...@@ -879,7 +890,7 @@ _cpp_parse_expr (pfile)
else else
{ {
top->value = v1 + v2; top->value = v1 + v2;
top->unsignedp = unsigned1 || unsigned2; top->unsignedp = unsigned1 | unsigned2;
if (! top->unsignedp && ! skip_evaluation if (! top->unsignedp && ! skip_evaluation
&& ! possible_sum_sign (v1, v2, top->value)) && ! possible_sum_sign (v1, v2, top->value))
integer_overflow (pfile); integer_overflow (pfile);
...@@ -898,14 +909,14 @@ _cpp_parse_expr (pfile) ...@@ -898,14 +909,14 @@ _cpp_parse_expr (pfile)
else else
{ /* Binary '-' */ { /* Binary '-' */
top->value = v1 - v2; top->value = v1 - v2;
top->unsignedp = unsigned1 || unsigned2; top->unsignedp = unsigned1 | unsigned2;
if (! top->unsignedp && ! skip_evaluation if (! top->unsignedp && ! skip_evaluation
&& ! possible_sum_sign (top->value, v2, v1)) && ! possible_sum_sign (top->value, v2, v1))
integer_overflow (pfile); integer_overflow (pfile);
} }
break; break;
case '*': case '*':
top->unsignedp = unsigned1 || unsigned2; top->unsignedp = unsigned1 | unsigned2;
if (top->unsignedp) if (top->unsignedp)
top->value = (unsigned HOST_WIDEST_INT) v1 * v2; top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
else if (!skip_evaluation) else if (!skip_evaluation)
...@@ -921,11 +932,8 @@ _cpp_parse_expr (pfile) ...@@ -921,11 +932,8 @@ _cpp_parse_expr (pfile)
if (skip_evaluation) if (skip_evaluation)
break; break;
if (v2 == 0) if (v2 == 0)
{ SYNTAX_ERROR ("division by zero in #if");
cpp_error (pfile, "division by zero in #if"); top->unsignedp = unsigned1 | unsigned2;
v2 = 1;
}
top->unsignedp = unsigned1 || unsigned2;
if (top[1].op == '/') if (top[1].op == '/')
{ {
if (top->unsignedp) if (top->unsignedp)
...@@ -992,45 +1000,33 @@ _cpp_parse_expr (pfile) ...@@ -992,45 +1000,33 @@ _cpp_parse_expr (pfile)
break; break;
case ',': case ',':
if (CPP_PEDANTIC (pfile)) if (CPP_PEDANTIC (pfile))
cpp_pedwarn (pfile, "comma operator in operand of `#if'"); cpp_pedwarn (pfile, "comma operator in operand of #if");
top->value = v2; top->value = v2;
top->unsignedp = unsigned2; top->unsignedp = unsigned2;
break; break;
case '?': case '?':
cpp_error (pfile, "syntax error '?' without following ':'"); SYNTAX_ERROR ("syntax error '?' without following ':'");
goto syntax_error;
case ':': case ':':
if (top[0].op != '?') if (top[0].op != '?')
{ SYNTAX_ERROR ("syntax error ':' without preceding '?'");
cpp_error (pfile,
"syntax error ':' without preceding '?'");
goto syntax_error;
}
top--; top--;
if (top->value) skip_evaluation--; if (top->value) skip_evaluation--;
top->value = top->value ? v1 : v2; top->value = top->value ? v1 : v2;
top->unsignedp = unsigned1 || unsigned2; top->unsignedp = unsigned1 | unsigned2;
break; break;
case '(': case '(':
if (op.op != ')') if (op.op != ')')
{ SYNTAX_ERROR ("missing ')' in expression");
cpp_error (pfile, "missing ')' in expression");
goto syntax_error;
}
op.value = v2; op.value = v2;
op.unsignedp = unsigned2; op.unsignedp = unsigned2;
goto push_immediate; goto push_immediate;
default: default:
cpp_error (pfile, "unimplemented operator `%s'", SYNTAX_ERROR2 ("unimplemented operator '%s'",
op_to_str (top[1].op)); op_to_str (top[1].op, buff));
break;
case FINISHED: case FINISHED:
/* Reducing this dummy operator indicates we've finished. */ /* Reducing this dummy operator indicates we've finished. */
if (op.op == ')') if (op.op == ')')
{ SYNTAX_ERROR ("missing '(' in expression");
cpp_error (pfile, "missing '(' in expression");
goto syntax_error;
}
goto done; goto done;
} }
} }
...@@ -1051,15 +1047,17 @@ _cpp_parse_expr (pfile) ...@@ -1051,15 +1047,17 @@ _cpp_parse_expr (pfile)
skip_reduction: skip_reduction:
/* Check we have a left operand iff we need one. */ /* Check we have a left operand iff we need one. */
if (((flags & NO_L_OPERAND) != 0) ^ ((top->flags & HAVE_VALUE) == 0))
{
if (flags & NO_L_OPERAND) if (flags & NO_L_OPERAND)
cpp_error (pfile, "missing binary operator before `%s'", {
op_to_str (op.op)); if (top->flags & HAVE_VALUE)
SYNTAX_ERROR2 ("missing binary operator before '%s'",
op_to_str (op.op, buff));
}
else else
cpp_error (pfile, "operator `%s' has no left operand", {
op_to_str (op.op)); if (!(top->flags & HAVE_VALUE))
goto syntax_error; SYNTAX_ERROR2 ("operator '%s' has no left operand",
op_to_str (op.op, buff));
} }
/* Check for and handle stack overflow. */ /* Check for and handle stack overflow. */
...@@ -1089,13 +1087,10 @@ _cpp_parse_expr (pfile) ...@@ -1089,13 +1087,10 @@ _cpp_parse_expr (pfile)
done: done:
result = (top[1].value != 0); result = (top[1].value != 0);
if (top != stack) if (top != stack)
{ CPP_ICE ("unbalanced stack in #if expression");
cpp_ice (pfile, "unbalanced stack in #if expression");
goto syntax_error;
}
else if (!(top[1].flags & HAVE_VALUE)) else if (!(top[1].flags & HAVE_VALUE))
{ {
cpp_error (pfile, "#if with no expression"); SYNTAX_ERROR ("#if with no expression");
syntax_error: syntax_error:
_cpp_skip_rest_of_line (pfile); _cpp_skip_rest_of_line (pfile);
result = 0; /* Return 0 on syntax error. */ result = 0; /* Return 0 on syntax error. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment