Bill Clarke preprocessor fix for dynamic_cast.cpptest failure on Solaris

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6243 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2004-09-14 20:31:12 +00:00
commit fea836f808
2 changed files with 66 additions and 22 deletions

View file

@ -1320,8 +1320,11 @@ Preprocessor_parse(String *s)
/* Printf(stdout,"Evaluating '%s'\n", sval); */ /* Printf(stdout,"Evaluating '%s'\n", sval); */
val = Preprocessor_expr(sval,&e); val = Preprocessor_expr(sval,&e);
if (e) { if (e) {
char * msg = Preprocessor_expr_error();
Seek(value,0,SEEK_SET); Seek(value,0,SEEK_SET);
Swig_warning(WARN_PP_EVALUATION,Getfile(value),Getline(value),"Could not evaluate '%s'\n", value); Swig_warning(WARN_PP_EVALUATION,Getfile(value),Getline(value),"Could not evaluate '%s'\n", value);
if (msg)
Swig_warning(WARN_PP_EVALUATION,Getfile(value),Getline(value),"Error: '%s'\n", msg);
allow = 0; allow = 0;
} else { } else {
if (val == 0) if (val == 0)
@ -1342,8 +1345,11 @@ Preprocessor_parse(String *s)
Seek(sval,0,SEEK_SET); Seek(sval,0,SEEK_SET);
val = Preprocessor_expr(sval,&e); val = Preprocessor_expr(sval,&e);
if (e) { if (e) {
char * msg = Preprocessor_expr_error();
Seek(value,0,SEEK_SET); Seek(value,0,SEEK_SET);
Swig_warning(WARN_PP_EVALUATION,Getfile(value),Getline(value),"Could not evaluate '%s'\n", value); Swig_warning(WARN_PP_EVALUATION,Getfile(value),Getline(value),"Could not evaluate '%s'\n", value);
if (msg)
Swig_warning(WARN_PP_EVALUATION,Getfile(value),Getline(value),"Error: '%s'\n", msg);
allow = 0; allow = 0;
} else { } else {
if (val) if (val)

View file

@ -61,20 +61,44 @@ static void init_precedence() {
expr_init = 1; expr_init = 1;
} }
#define UNARY_OP(token) (((token) == SWIG_TOKEN_NOT) || \
((token) == SWIG_TOKEN_LNOT) || \
((token) == EXPR_UMINUS))
/* Reduce a single operator on the stack */ /* Reduce a single operator on the stack */
static void reduce_op() { /* return 0 on failure, 1 on success */
if (stack[sp-1].op != EXPR_OP) { static int reduce_op() {
errmsg = "Missing operator"; long op_token = stack[sp-1].value;
sp = 0; assert(sp > 0);
return; assert(stack[sp-1].op == EXPR_OP);
/* do some basic checking first: */
if (stack[sp].op != EXPR_VALUE) {
errmsg = "Right-hand side is not value";
return 0;
} }
if (stack[sp-2].svalue || stack[sp].svalue) { if (UNARY_OP(op_token)) {
/* A string expression */ if (stack[sp].svalue) {
if (!(stack[sp-2].svalue && stack[sp].svalue)) { errmsg = "Syntax error: attempt to apply unary operator to string";
errmsg = "Can't mix strings and integers in expression"; return 0;
sp = 0;
return;
} }
} else {
/* binary operator: */
if (sp == 1) {
/* top of stack: don't attempt to use sp-2! */
errmsg = "Missing left-hand side for binary operator";
return 0;
}
if (stack[sp].op != EXPR_VALUE) {
errmsg = "Left-hand side of binary operator is not a value";
return 0;
}
if ((!stack[sp-2].svalue) != (!stack[sp].svalue)) {
errmsg = "Can't mix strings and integers in expression";
return 0;
}
}
if (stack[sp].svalue) {
/* A binary string expression */
switch(stack[sp-1].value) { switch(stack[sp-1].value) {
case SWIG_TOKEN_EQUALTO: case SWIG_TOKEN_EQUALTO:
stack[sp-2].value = (Strcmp(stack[sp-2].svalue,stack[sp].svalue) == 0); stack[sp-2].value = (Strcmp(stack[sp-2].svalue,stack[sp].svalue) == 0);
@ -89,12 +113,12 @@ static void reduce_op() {
sp -= 2; sp -= 2;
break; break;
default: default:
errmsg = "Syntax error"; errmsg = "Syntax error: bad binary operator for strings";
sp = 0; return 0;
break; break;
} }
} else { } else {
switch(stack[sp-1].value) { switch(op_token) {
case SWIG_TOKEN_STAR: case SWIG_TOKEN_STAR:
stack[sp-2].value = stack[sp-2].value * stack[sp].value; stack[sp-2].value = stack[sp-2].value * stack[sp].value;
sp -= 2; sp -= 2;
@ -180,12 +204,14 @@ static void reduce_op() {
sp -= 2; sp -= 2;
break; break;
default: default:
errmsg = "Syntax error"; errmsg = "Syntax error: bad operator";
sp = 0; return 0;
break; break;
} }
} }
stack[sp].op = EXPR_VALUE; stack[sp].op = EXPR_VALUE;
stack[sp].svalue = 0; /* ensure it's not a string! */
return 1;
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
@ -285,7 +311,7 @@ Preprocessor_expr(DOH *s, int *error) {
/* End of input. Might have to reduce if an operator is on stack */ /* End of input. Might have to reduce if an operator is on stack */
while (sp > 0) { while (sp > 0) {
if (stack[sp-1].op == EXPR_OP) { if (stack[sp-1].op == EXPR_OP) {
reduce_op(); if (!reduce_op()) goto reduce_error;
} else if (stack[sp-1].op == EXPR_GROUP) { } else if (stack[sp-1].op == EXPR_GROUP) {
errmsg = "Missing \')\'"; errmsg = "Missing \')\'";
*error = 1; *error = 1;
@ -323,7 +349,7 @@ Preprocessor_expr(DOH *s, int *error) {
stack[sp].op = EXPR_TOP; stack[sp].op = EXPR_TOP;
stack[sp].value = 0; stack[sp].value = 0;
} else { } else {
if (stack[sp-1].op != EXPR_OP) goto syntax_error; if (stack[sp-1].op != EXPR_OP) goto syntax_error_expected_operator;
op = stack[sp-1].value; /* Previous operator */ op = stack[sp-1].value; /* Previous operator */
/* Now, depending on the precedence relationship between the last operator and the current /* Now, depending on the precedence relationship between the last operator and the current
@ -331,8 +357,7 @@ Preprocessor_expr(DOH *s, int *error) {
if (prec[op] <= prec[token]) { if (prec[op] <= prec[token]) {
/* Reduce the previous operator */ /* Reduce the previous operator */
reduce_op(); if (!reduce_op()) goto reduce_error;
if (stack[sp].op != EXPR_VALUE) goto syntax_error;
} }
sp++; sp++;
stack[sp].op = EXPR_OP; stack[sp].op = EXPR_OP;
@ -346,14 +371,16 @@ Preprocessor_expr(DOH *s, int *error) {
if (sp == 0) goto extra_rparen; if (sp == 0) goto extra_rparen;
/* Might have to reduce operators first */ /* Might have to reduce operators first */
while ((sp > 0) && (stack[sp-1].op == EXPR_OP)) reduce_op(); while ((sp > 0) && (stack[sp-1].op == EXPR_OP)) {
if (!reduce_op()) goto reduce_error;
}
if ((sp == 0) || (stack[sp-1].op != EXPR_GROUP)) goto extra_rparen; if ((sp == 0) || (stack[sp-1].op != EXPR_GROUP)) goto extra_rparen;
stack[sp-1].op = EXPR_VALUE; stack[sp-1].op = EXPR_VALUE;
stack[sp-1].value = stack[sp].value; stack[sp-1].value = stack[sp].value;
sp--; sp--;
break; break;
default: default:
goto syntax_error; goto syntax_error_expected_operator;
break; break;
} }
break; break;
@ -363,11 +390,22 @@ Preprocessor_expr(DOH *s, int *error) {
abort(); abort();
} }
} }
syntax_error: syntax_error:
errmsg = "Syntax error"; errmsg = "Syntax error";
*error = 1; *error = 1;
return 0; return 0;
syntax_error_expected_operator:
errmsg = "Syntax error: expected operator";
*error = 1;
return 0;
reduce_error:
/* errmsg has been set by reduce_op */
*error = 1;
return 0;
extra_rparen: extra_rparen:
errmsg = "Extra \')\'"; errmsg = "Extra \')\'";
*error = 1; *error = 1;