From 0ddf41d34d511b339e0bb5a59673765f1bf0b3a5 Mon Sep 17 00:00:00 2001 From: Michal Krol Date: Thu, 17 Sep 2009 11:51:35 +0200 Subject: glsl/pp: Add remaining error messages. --- src/glsl/pp/sl_pp_context.c | 1 + src/glsl/pp/sl_pp_context.h | 2 +- src/glsl/pp/sl_pp_define.c | 19 ++++++++++++------ src/glsl/pp/sl_pp_expression.c | 2 ++ src/glsl/pp/sl_pp_if.c | 35 +++++++++++++++++++------------- src/glsl/pp/sl_pp_line.c | 45 ++++++++---------------------------------- src/glsl/pp/sl_pp_macro.c | 18 ++++++++++++++++- src/glsl/pp/sl_pp_pragma.c | 1 + src/glsl/pp/sl_pp_version.c | 42 ++++----------------------------------- 9 files changed, 68 insertions(+), 97 deletions(-) diff --git a/src/glsl/pp/sl_pp_context.c b/src/glsl/pp/sl_pp_context.c index 88a002c1c7..b196d8102a 100644 --- a/src/glsl/pp/sl_pp_context.c +++ b/src/glsl/pp/sl_pp_context.c @@ -85,6 +85,7 @@ sl_pp_context_add_unique_str(struct sl_pp_context *context, } if (!context->cstr_pool) { + strcpy(context->error_msg, "out of memory"); return -1; } diff --git a/src/glsl/pp/sl_pp_context.h b/src/glsl/pp/sl_pp_context.h index 5826f9448d..8bed142045 100644 --- a/src/glsl/pp/sl_pp_context.h +++ b/src/glsl/pp/sl_pp_context.h @@ -47,7 +47,7 @@ struct sl_pp_context { unsigned int if_stack[SL_PP_MAX_IF_NESTING]; unsigned int if_ptr; - int if_value; + unsigned int if_value; char error_msg[SL_PP_MAX_ERROR_MSG]; diff --git a/src/glsl/pp/sl_pp_define.c b/src/glsl/pp/sl_pp_define.c index 9bc9fb5359..391178aa69 100644 --- a/src/glsl/pp/sl_pp_define.c +++ b/src/glsl/pp/sl_pp_define.c @@ -41,7 +41,8 @@ skip_whitespace(const struct sl_pp_token_info *input, static int -_parse_formal_args(const struct sl_pp_token_info *input, +_parse_formal_args(struct sl_pp_context *context, + const struct sl_pp_token_info *input, unsigned int *first, unsigned int last, struct sl_pp_macro *macro) @@ -57,7 +58,7 @@ _parse_formal_args(const struct sl_pp_token_info *input, return 0; } } else { - /* Expected either an identifier or `)'. */ + strcpy(context->error_msg, "expected either an identifier or `)'"); return -1; } @@ -65,12 +66,13 @@ _parse_formal_args(const struct sl_pp_token_info *input, for (;;) { if (*first < last && input[*first].token != SL_PP_IDENTIFIER) { - /* Expected an identifier. */ + strcpy(context->error_msg, "expected an identifier"); return -1; } *arg = malloc(sizeof(struct sl_pp_macro_formal_arg)); if (!*arg) { + strcpy(context->error_msg, "out of memory"); return -1; } @@ -90,14 +92,16 @@ _parse_formal_args(const struct sl_pp_token_info *input, (*first)++; return 0; } else { - /* Expected either `,' or `)'. */ + strcpy(context->error_msg, "expected either `,' or `)'"); return -1; } } else { - /* Expected either `,' or `)'. */ + strcpy(context->error_msg, "expected either `,' or `)'"); return -1; } } + + /* Should not gete here. */ } @@ -118,6 +122,7 @@ sl_pp_process_define(struct sl_pp_context *context, first++; } if (macro_name == -1) { + strcpy(context->error_msg, "expected an identifier"); return -1; } @@ -130,6 +135,7 @@ sl_pp_process_define(struct sl_pp_context *context, if (!macro) { macro = sl_pp_macro_new(); if (!macro) { + strcpy(context->error_msg, "out of memory"); return -1; } @@ -149,7 +155,7 @@ sl_pp_process_define(struct sl_pp_context *context, */ if (first < last && input[first].token == SL_PP_LPAREN) { first++; - if (_parse_formal_args(input, &first, last, macro)) { + if (_parse_formal_args(context, input, &first, last, macro)) { return -1; } } @@ -164,6 +170,7 @@ sl_pp_process_define(struct sl_pp_context *context, macro->body = malloc(sizeof(struct sl_pp_token_info) * body_len); if (!macro->body) { + strcpy(context->error_msg, "out of memory"); return -1; } diff --git a/src/glsl/pp/sl_pp_expression.c b/src/glsl/pp/sl_pp_expression.c index a692430abb..6b2329ed1a 100644 --- a/src/glsl/pp/sl_pp_expression.c +++ b/src/glsl/pp/sl_pp_expression.c @@ -47,6 +47,7 @@ _parse_primary(struct parse_context *ctx, ctx->input++; } else { if (ctx->input->token != SL_PP_LPAREN) { + strcpy(ctx->context->error_msg, "expected `('"); return -1; } ctx->input++; @@ -54,6 +55,7 @@ _parse_primary(struct parse_context *ctx, return -1; } if (ctx->input->token != SL_PP_RPAREN) { + strcpy(ctx->context->error_msg, "expected `)'"); return -1; } ctx->input++; diff --git a/src/glsl/pp/sl_pp_if.c b/src/glsl/pp/sl_pp_if.c index 90b8051237..44bbefa357 100644 --- a/src/glsl/pp/sl_pp_if.c +++ b/src/glsl/pp/sl_pp_if.c @@ -59,7 +59,7 @@ _parse_defined(struct sl_pp_context *context, } if (input[*pi].token != SL_PP_IDENTIFIER) { - /* Identifier expected. */ + strcpy(context->error_msg, "expected an identifier"); return -1; } @@ -75,7 +75,7 @@ _parse_defined(struct sl_pp_context *context, if (parens) { skip_whitespace(input, pi); if (input[*pi].token != SL_PP_RPAREN) { - /* `)' expected */ + strcpy(context->error_msg, "expected `)'"); return -1; } (*pi)++; @@ -91,10 +91,15 @@ _parse_defined(struct sl_pp_context *context, return -1; } - return sl_pp_process_out(state, &result); + if (sl_pp_process_out(state, &result)) { + strcpy(context->error_msg, "out of memory"); + return -1; + } + + return 0; } -static int +static unsigned int _evaluate_if_stack(struct sl_pp_context *context) { unsigned int i; @@ -119,7 +124,7 @@ _parse_if(struct sl_pp_context *context, int result; if (!context->if_ptr) { - /* #if nesting too deep. */ + strcpy(context->error_msg, "`#if' nesting too deep"); return -1; } @@ -151,6 +156,7 @@ _parse_if(struct sl_pp_context *context, default: if (sl_pp_process_out(&state, &input[i])) { + strcpy(context->error_msg, "out of memory"); free(state.out); return -1; } @@ -160,6 +166,7 @@ _parse_if(struct sl_pp_context *context, eof.token = SL_PP_EOF; if (sl_pp_process_out(&state, &eof)) { + strcpy(context->error_msg, "out of memory"); free(state.out); return -1; } @@ -182,13 +189,13 @@ static int _parse_else(struct sl_pp_context *context) { if (context->if_ptr == SL_PP_MAX_IF_NESTING) { - /* No matching #if. */ + strcpy(context->error_msg, "no matching `#if'"); return -1; } /* Bit b1 indicates we already went through #else. */ if (context->if_stack[context->if_ptr] & 2) { - /* No matching #if. */ + strcpy(context->error_msg, "no matching `#if'"); return -1; } @@ -217,7 +224,7 @@ sl_pp_process_ifdef(struct sl_pp_context *context, unsigned int i; if (!context->if_ptr) { - /* #if nesting too deep. */ + strcpy(context->error_msg, "`#if' nesting too deep"); return -1; } @@ -246,12 +253,12 @@ sl_pp_process_ifdef(struct sl_pp_context *context, break; default: - /* Expected an identifier. */ + strcpy(context->error_msg, "expected an identifier"); return -1; } } - /* Expected an identifier. */ + strcpy(context->error_msg, "expected an identifier"); return -1; } @@ -264,7 +271,7 @@ sl_pp_process_ifndef(struct sl_pp_context *context, unsigned int i; if (!context->if_ptr) { - /* #if nesting too deep. */ + strcpy(context->error_msg, "`#if' nesting too deep"); return -1; } @@ -293,12 +300,12 @@ sl_pp_process_ifndef(struct sl_pp_context *context, break; default: - /* Expected an identifier. */ + strcpy(context->error_msg, "expected an identifier"); return -1; } } - /* Expected an identifier. */ + strcpy(context->error_msg, "expected an identifier"); return -1; } @@ -341,7 +348,7 @@ sl_pp_process_endif(struct sl_pp_context *context, unsigned int last) { if (context->if_ptr == SL_PP_MAX_IF_NESTING) { - /* No matching #if. */ + strcpy(context->error_msg, "no matching `#if'"); return -1; } diff --git a/src/glsl/pp/sl_pp_line.c b/src/glsl/pp/sl_pp_line.c index a56417a861..c38f4b0f2e 100644 --- a/src/glsl/pp/sl_pp_line.c +++ b/src/glsl/pp/sl_pp_line.c @@ -29,31 +29,6 @@ #include "sl_pp_process.h" -static int -_parse_integer(const char *input, - unsigned int *number) -{ - unsigned int n = 0; - - while (*input >= '0' && *input <= '9') { - if (n * 10 < n) { - /* Overflow. */ - return -1; - } - - n = n * 10 + (*input++ - '0'); - } - - if (*input != '\0') { - /* Invalid decimal number. */ - return -1; - } - - *number = n; - return 0; -} - - int sl_pp_process_line(struct sl_pp_context *context, const struct sl_pp_token_info *input, @@ -65,7 +40,6 @@ sl_pp_process_line(struct sl_pp_context *context, struct sl_pp_process_state state; int line_number = -1; int file_number = -1; - const char *str; unsigned int line; memset(&state, 0, sizeof(state)); @@ -84,6 +58,7 @@ sl_pp_process_line(struct sl_pp_context *context, default: if (sl_pp_process_out(&state, &input[i])) { + strcpy(context->error_msg, "out of memory"); free(state.out); return -1; } @@ -94,7 +69,7 @@ sl_pp_process_line(struct sl_pp_context *context, if (state.out_len > 0 && state.out[0].token == SL_PP_NUMBER) { line_number = state.out[0].data.number; } else { - strcpy(context->error_msg, "expected number after `#line'"); + strcpy(context->error_msg, "expected a number after `#line'"); free(state.out); return -1; } @@ -103,13 +78,13 @@ sl_pp_process_line(struct sl_pp_context *context, if (state.out[1].token == SL_PP_NUMBER) { file_number = state.out[1].data.number; } else { - strcpy(context->error_msg, "expected number after line number"); + strcpy(context->error_msg, "expected a number after line number"); free(state.out); return -1; } if (state.out_len > 2) { - strcpy(context->error_msg, "expected end of line after file number"); + strcpy(context->error_msg, "expected an end of line after file number"); free(state.out); return -1; } @@ -117,10 +92,7 @@ sl_pp_process_line(struct sl_pp_context *context, free(state.out); - str = sl_pp_context_cstr(context, line_number); - if (_parse_integer(str, &line)) { - return -1; - } + line = atoi(sl_pp_context_cstr(context, line_number)); if (context->line != line) { struct sl_pp_token_info ti; @@ -128,6 +100,7 @@ sl_pp_process_line(struct sl_pp_context *context, ti.token = SL_PP_LINE; ti.data.line = line; if (sl_pp_process_out(pstate, &ti)) { + strcpy(context->error_msg, "out of memory"); return -1; } @@ -137,10 +110,7 @@ sl_pp_process_line(struct sl_pp_context *context, if (file_number != -1) { unsigned int file; - str = sl_pp_context_cstr(context, file_number); - if (_parse_integer(str, &file)) { - return -1; - } + file_number = atoi(sl_pp_context_cstr(context, file_number)); if (context->file != file) { struct sl_pp_token_info ti; @@ -148,6 +118,7 @@ sl_pp_process_line(struct sl_pp_context *context, ti.token = SL_PP_FILE; ti.data.file = file; if (sl_pp_process_out(pstate, &ti)) { + strcpy(context->error_msg, "out of memory"); return -1; } diff --git a/src/glsl/pp/sl_pp_macro.c b/src/glsl/pp/sl_pp_macro.c index d14c982555..7793562781 100644 --- a/src/glsl/pp/sl_pp_macro.c +++ b/src/glsl/pp/sl_pp_macro.c @@ -107,7 +107,12 @@ _out_number(struct sl_pp_context *context, ti.token = SL_PP_NUMBER; ti.data.number = sl_pp_context_add_unique_str(context, buf); - return sl_pp_process_out(state, &ti); + if (sl_pp_process_out(state, &ti)) { + strcpy(context->error_msg, "out of memory"); + return -1; + } + + return 0; } int @@ -125,6 +130,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, unsigned int j; if (input[*pi].token != SL_PP_IDENTIFIER) { + strcpy(context->error_msg, "expected an identifier"); return -1; } @@ -172,6 +178,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, if (!macro) { if (!mute) { if (sl_pp_process_out(state, &input[*pi])) { + strcpy(context->error_msg, "out of memory"); return -1; } } @@ -184,6 +191,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, if (macro->num_args >= 0) { skip_whitespace(input, pi); if (input[*pi].token != SL_PP_LPAREN) { + strcpy(context->error_msg, "expected `('"); return -1; } (*pi)++; @@ -203,6 +211,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, *pmacro = sl_pp_macro_new(); if (!*pmacro) { + strcpy(context->error_msg, "out of memory"); return -1; } @@ -219,6 +228,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, if (j < (unsigned int)macro->num_args - 1) { done = 1; } else { + strcpy(context->error_msg, "too many actual macro arguments"); return -1; } } else { @@ -236,6 +246,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, if (j == (unsigned int)macro->num_args - 1) { done = 1; } else { + strcpy(context->error_msg, "too few actual macro arguments"); return -1; } } else { @@ -245,6 +256,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, break; case SL_PP_EOF: + strcpy(context->error_msg, "too few actual macro arguments"); return -1; default: @@ -254,6 +266,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, (**pmacro).body = malloc(sizeof(struct sl_pp_token_info) * body_len); if (!(**pmacro).body) { + strcpy(context->error_msg, "out of memory"); return -1; } @@ -301,6 +314,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, if (macro->num_args == 0) { skip_whitespace(input, pi); if (input[*pi].token != SL_PP_RPAREN) { + strcpy(context->error_msg, "expected `)'"); return -1; } (*pi)++; @@ -310,6 +324,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, switch (macro->body[j].token) { case SL_PP_NEWLINE: if (sl_pp_process_out(state, ¯o->body[j])) { + strcpy(context->error_msg, "out of memory"); return -1; } j++; @@ -328,6 +343,7 @@ sl_pp_macro_expand(struct sl_pp_context *context, default: if (!mute) { if (sl_pp_process_out(state, ¯o->body[j])) { + strcpy(context->error_msg, "out of memory"); return -1; } } diff --git a/src/glsl/pp/sl_pp_pragma.c b/src/glsl/pp/sl_pp_pragma.c index 059bc6f288..1cd9fd8234 100644 --- a/src/glsl/pp/sl_pp_pragma.c +++ b/src/glsl/pp/sl_pp_pragma.c @@ -99,6 +99,7 @@ sl_pp_process_pragma(struct sl_pp_context *context, /* Ignore the tokens that follow. */ if (sl_pp_process_out(state, &out)) { + strcpy(context->error_msg, "out of memory"); return -1; } diff --git a/src/glsl/pp/sl_pp_version.c b/src/glsl/pp/sl_pp_version.c index 82acdd1d5a..6cd63f4925 100644 --- a/src/glsl/pp/sl_pp_version.c +++ b/src/glsl/pp/sl_pp_version.c @@ -25,34 +25,10 @@ * **************************************************************************/ +#include #include "sl_pp_version.h" -static int -_parse_integer(const char *input, - unsigned int *number) -{ - unsigned int n = 0; - - while (*input >= '0' && *input <= '9') { - if (n * 10 < n) { - /* Overflow. */ - return -1; - } - - n = n * 10 + (*input++ - '0'); - } - - if (*input != '\0') { - /* Invalid decimal number. */ - return -1; - } - - *number = n; - return 0; -} - - int sl_pp_version(struct sl_pp_context *context, const struct sl_pp_token_info *input, @@ -130,19 +106,9 @@ sl_pp_version(struct sl_pp_context *context, break; case SL_PP_NUMBER: - { - const char *num = sl_pp_context_cstr(context, input[i].data.number); - - if (!num) { - return -1; - } - if (_parse_integer(num, version)) { - strcpy(context->error_msg, "expected version number after `#version'"); - return -1; - } - i++; - found_number = 1; - } + *version = atoi(sl_pp_context_cstr(context, input[i].data.number)); + i++; + found_number = 1; break; default: -- cgit v1.2.3