hugeval test
[cparser] / parser.c
index b6d0f4a..338c975 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -490,6 +490,18 @@ static void add_anchor_token(int token_type) {
        ++token_anchor_set[token_type];
 }
 
+static int save_and_reset_anchor_state(int token_type) {
+       assert(0 <= token_type && token_type < T_LAST_TOKEN);
+       int count = token_anchor_set[token_type];
+       token_anchor_set[token_type] = 0;
+       return count;
+}
+
+static void restore_anchor_state(int token_type, int count) {
+       assert(0 <= token_type && token_type < T_LAST_TOKEN);
+       token_anchor_set[token_type] = count;
+}
+
 /**
  * Remove a token from the token anchor set (a multi-set).
  */
@@ -582,7 +594,10 @@ static void eat_statement(void) {
 /**
  * Report a parse error because an expected token was not found.
  */
-static __attribute__((sentinel))
+static
+#if defined __GNUC__ && __GNUC__ >= 4
+__attribute__((sentinel))
+#endif
 void parse_error_expected(const char *message, ...)
 {
        if(message != NULL) {
@@ -624,6 +639,8 @@ static void type_error_incompatible(const char *msg,
         parse_error_expected(NULL, (expected), NULL); \
                add_anchor_token(expected);                   \
         eat_until_anchor();                           \
+        if (token.type == expected)                   \
+               next_token();                             \
                rem_anchor_token(expected);                   \
         goto end_error;                               \
     }                                                 \
@@ -1045,6 +1062,7 @@ static const char *gnu_attribute_names[GNU_AK_LAST] = {
        [GNU_AK_TLS_MODEL]              = "tls_model",
        [GNU_AK_VISIBILITY]             = "visibility",
        [GNU_AK_REGPARM]                = "regparm",
+       [GNU_AK_MODE]                   = "mode",
        [GNU_AK_MODEL]                  = "model",
        [GNU_AK_TRAP_EXIT]              = "trap_exit",
        [GNU_AK_SP_SWITCH]              = "sp_switch",
@@ -1209,6 +1227,21 @@ static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
        attribute->invalid = true;
 }
 
+static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
+{
+       /* TODO: find out what is allowed here... */
+
+       /* at least: byte, word, pointer, list of machine modes
+        * __XXX___ is interpreted as XXX */
+       add_anchor_token(')');
+       expect(T_IDENTIFIER);
+       rem_anchor_token(')');
+       expect(')');
+       return;
+end_error:
+       attribute->invalid = true;
+}
+
 /**
  * parse one interrupt argument.
  */
@@ -1533,8 +1566,18 @@ static void parse_gnu_attribute(gnu_attribute_t **attributes)
                                        if(!attribute->have_arguments) {
                                                /* should have arguments */
                                                errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
-                                       } else
+                                       } else {
                                                parse_gnu_attribute_model_arg(attribute);
+                                       }
+                                       break;
+                               case GNU_AK_MODE:
+                                       if(!attribute->have_arguments) {
+                                               /* should have arguments */
+                                               errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
+                                       } else {
+                                               parse_gnu_attribute_mode_arg(attribute);
+                                       }
+                                       break;
                                case GNU_AK_INTERRUPT:
                                        /* may have one string argument */
                                        if(attribute->have_arguments)
@@ -2413,7 +2456,7 @@ static declaration_t *parse_compound_type_specifier(bool is_struct)
                        (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
                declaration->source_position = token.source_position;
                declaration->symbol          = symbol;
-               declaration->parent_scope  = scope;
+               declaration->parent_scope    = scope;
                if (symbol != NULL) {
                        environment_push(declaration);
                }
@@ -2421,13 +2464,13 @@ static declaration_t *parse_compound_type_specifier(bool is_struct)
        }
 
        if(token.type == '{') {
-               if(declaration->init.is_defined) {
+               if (declaration->init.complete) {
                        assert(symbol != NULL);
                        errorf(HERE, "multiple definitions of '%s %Y'",
                               is_struct ? "struct" : "union", symbol);
                        declaration->scope.declarations = NULL;
                }
-               declaration->init.is_defined = true;
+               declaration->init.complete = true;
 
                parse_compound_type_entries(declaration);
                parse_attributes(&attributes);
@@ -2519,14 +2562,14 @@ static type_t *parse_enum_specifier(void)
        type->enumt.declaration = declaration;
 
        if(token.type == '{') {
-               if(declaration->init.is_defined) {
+               if(declaration->init.complete) {
                        errorf(HERE, "multiple definitions of enum %Y", symbol);
                }
                if (symbol != NULL) {
                        environment_push(declaration);
                }
                append_declaration(declaration);
-               declaration->init.is_defined = 1;
+               declaration->init.complete = true;
 
                parse_enum_entries(type);
                parse_attributes(&attributes);
@@ -3207,8 +3250,9 @@ static declaration_t *parse_identifier_list(void)
                }
                last_declaration = declaration;
 
-               if(token.type != ',')
+               if (token.type != ',') {
                        break;
+               }
                next_token();
        } while(token.type == T_IDENTIFIER);
 
@@ -3261,24 +3305,30 @@ static declaration_t *parse_parameter(void)
 
 static declaration_t *parse_parameters(function_type_t *type)
 {
+       declaration_t *declarations = NULL;
+
+       eat('(');
+       add_anchor_token(')');
+       int saved_comma_state = save_and_reset_anchor_state(',');
+
        if(token.type == T_IDENTIFIER) {
                symbol_t *symbol = token.v.symbol;
                if(!is_typedef_symbol(symbol)) {
                        type->kr_style_parameters = true;
-                       return parse_identifier_list();
+                       declarations = parse_identifier_list();
+                       goto parameters_finished;
                }
        }
 
        if(token.type == ')') {
                type->unspecified_parameters = 1;
-               return NULL;
+               goto parameters_finished;
        }
        if(token.type == T_void && look_ahead(1)->type == ')') {
                next_token();
-               return NULL;
+               goto parameters_finished;
        }
 
-       declaration_t        *declarations = NULL;
        declaration_t        *declaration;
        declaration_t        *last_declaration = NULL;
        function_parameter_t *parameter;
@@ -3289,7 +3339,7 @@ static declaration_t *parse_parameters(function_type_t *type)
                case T_DOTDOTDOT:
                        next_token();
                        type->variadic = 1;
-                       return declarations;
+                       goto parameters_finished;
 
                case T_IDENTIFIER:
                case T___extension__:
@@ -3312,12 +3362,25 @@ static declaration_t *parse_parameters(function_type_t *type)
                        break;
 
                default:
-                       return declarations;
+                       goto parameters_finished;
+               }
+               if (token.type != ',') {
+                       goto parameters_finished;
                }
-               if(token.type != ',')
-                       return declarations;
                next_token();
        }
+
+
+parameters_finished:
+       rem_anchor_token(')');
+       expect(')');
+
+       restore_anchor_state(',', saved_comma_state);
+       return declarations;
+
+end_error:
+       restore_anchor_state(',', saved_comma_state);
+       return NULL;
 }
 
 typedef enum {
@@ -3412,9 +3475,6 @@ end_error:
 
 static construct_type_t *parse_function_declarator(declaration_t *declaration)
 {
-       eat('(');
-       add_anchor_token(')');
-
        type_t *type;
        if(declaration != NULL) {
                type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
@@ -3433,10 +3493,6 @@ static construct_type_t *parse_function_declarator(declaration_t *declaration)
        construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
        construct_function_type->function_type       = type;
 
-       rem_anchor_token(')');
-       expect(')');
-
-end_error:
        return (construct_type_t*) construct_function_type;
 }
 
@@ -4102,10 +4158,10 @@ static void parse_declaration(parsed_declaration_func finished_declaration)
 static void parse_kr_declaration_list(declaration_t *declaration)
 {
        type_t *type = skip_typeref(declaration->type);
-       if(!is_type_function(type))
+       if (!is_type_function(type))
                return;
 
-       if(!type->function.kr_style_parameters)
+       if (!type->function.kr_style_parameters)
                return;
 
        /* push function parameters */
@@ -5646,7 +5702,7 @@ static expression_t *parse_select_expression(unsigned precedence,
 
        declaration_t *const declaration = type_left->compound.declaration;
 
-       if(!declaration->init.is_defined) {
+       if(!declaration->init.complete) {
                errorf(HERE, "request for member '%Y' of incomplete type '%T'",
                       symbol, type_left);
                return create_invalid_expression();
@@ -6288,9 +6344,9 @@ static void semantic_comparison(binary_expression_t *expression)
                                other_expr = left;
                        }
 
-                       type_t *other_type = skip_typeref(other_expr->base.type);
                        if(const_expr != NULL) {
-                               long val = fold_constant(const_expr);
+                               type_t *other_type = skip_typeref(other_expr->base.type);
+                               long    val        = fold_constant(const_expr);
                                /* TODO: check if val can be represented by other_type */
                                (void) other_type;
                                (void) val;