Use streq() instead of strcmp() == 0.
[cparser] / parser.c
index 6803e98..da35413 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -23,6 +23,7 @@
 #include <stdarg.h>
 #include <stdbool.h>
 
+#include "adt/strutil.h"
 #include "parser.h"
 #include "diagnostic.h"
 #include "format_check.h"
@@ -93,7 +94,7 @@ static switch_statement_t  *current_switch    = NULL;
 static statement_t         *current_loop      = NULL;
 static statement_t         *current_parent    = NULL;
 static ms_try_statement_t  *current_try       = NULL;
-static linkage_kind_t       current_linkage   = LINKAGE_INVALID;
+static linkage_kind_t       current_linkage;
 static goto_statement_t    *goto_first        = NULL;
 static goto_statement_t   **goto_anchor       = NULL;
 static label_statement_t   *label_first       = NULL;
@@ -283,8 +284,8 @@ static void semantic_comparison(binary_expression_t *expression);
 static size_t get_statement_struct_size(statement_kind_t kind)
 {
        static const size_t sizes[] = {
-               [STATEMENT_INVALID]     = sizeof(invalid_statement_t),
-               [STATEMENT_EMPTY]       = sizeof(empty_statement_t),
+               [STATEMENT_ERROR]       = sizeof(statement_base_t),
+               [STATEMENT_EMPTY]       = sizeof(statement_base_t),
                [STATEMENT_COMPOUND]    = sizeof(compound_statement_t),
                [STATEMENT_RETURN]      = sizeof(return_statement_t),
                [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
@@ -316,7 +317,7 @@ static size_t get_statement_struct_size(statement_kind_t kind)
 static size_t get_expression_struct_size(expression_kind_t kind)
 {
        static const size_t sizes[] = {
-               [EXPR_INVALID]                    = sizeof(expression_base_t),
+               [EXPR_ERROR]                      = sizeof(expression_base_t),
                [EXPR_REFERENCE]                  = sizeof(reference_expression_t),
                [EXPR_REFERENCE_ENUM_VALUE]       = sizeof(reference_expression_t),
                [EXPR_LITERAL_BOOLEAN]            = sizeof(literal_expression_t),
@@ -397,17 +398,19 @@ static expression_t *allocate_expression_zero(expression_kind_t kind)
  * Creates a new invalid expression at the source position
  * of the current token.
  */
-static expression_t *create_invalid_expression(void)
+static expression_t *create_error_expression(void)
 {
-       return allocate_expression_zero(EXPR_INVALID);
+       expression_t *expression = allocate_expression_zero(EXPR_ERROR);
+       expression->base.type = type_error_type;
+       return expression;
 }
 
 /**
  * Creates a new invalid statement.
  */
-static statement_t *create_invalid_statement(void)
+static statement_t *create_error_statement(void)
 {
-       return allocate_statement_zero(STATEMENT_INVALID);
+       return allocate_statement_zero(STATEMENT_ERROR);
 }
 
 /**
@@ -705,7 +708,6 @@ static void scope_pop(scope_t *old_scope)
 static entity_t *get_entity(const symbol_t *const symbol,
                             namespace_tag_t namespc)
 {
-       assert(namespc != NAMESPACE_INVALID);
        entity_t *entity = symbol->entity;
        for (; entity != NULL; entity = entity->base.symbol_next) {
                if ((namespace_tag_t)entity->base.namespc == namespc)
@@ -738,7 +740,7 @@ static void stack_push(stack_entry_t **stack_ptr, entity_t *entity)
 {
        symbol_t           *symbol  = entity->base.symbol;
        entity_namespace_t  namespc = entity->base.namespc;
-       assert(namespc != NAMESPACE_INVALID);
+       assert(namespc != 0);
 
        /* replace/add entity into entity list of the symbol */
        entity_t **anchor;
@@ -852,22 +854,11 @@ static void label_pop_to(size_t new_top)
        stack_pop_to(&label_stack, new_top);
 }
 
-static int get_akind_rank(atomic_type_kind_t akind)
-{
-       return (int) akind;
-}
-
-/**
- * Return the type rank for an atomic type.
- */
-static int get_rank(const type_t *type)
+static atomic_type_kind_t get_akind(const type_t *type)
 {
-       assert(!is_typeref(type));
-       if (type->kind == TYPE_ENUM)
-               return get_akind_rank(type->enumt.akind);
-
-       assert(type->kind == TYPE_ATOMIC);
-       return get_akind_rank(type->atomic.akind);
+       assert(type->kind == TYPE_ATOMIC || type->kind == TYPE_COMPLEX
+              || type->kind == TYPE_IMAGINARY || type->kind == TYPE_ENUM);
+       return type->atomic.akind;
 }
 
 /**
@@ -878,7 +869,7 @@ static int get_rank(const type_t *type)
  */
 static type_t *promote_integer(type_t *type)
 {
-       if (get_rank(type) < get_akind_rank(ATOMIC_TYPE_INT))
+       if (get_akind_rank(get_akind(type)) < get_akind_rank(ATOMIC_TYPE_INT))
                type = type_int;
 
        return type;
@@ -1081,22 +1072,6 @@ static string_t parse_string_literals(void)
        return result;
 }
 
-/**
- * compare two string, ignoring double underscores on the second.
- */
-static int strcmp_underscore(const char *s1, const char *s2)
-{
-       if (s2[0] == '_' && s2[1] == '_') {
-               size_t len2 = strlen(s2);
-               size_t len1 = strlen(s1);
-               if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
-                       return strncmp(s1, s2+2, len2-4);
-               }
-       }
-
-       return strcmp(s1, s2);
-}
-
 static attribute_t *allocate_attribute_zero(attribute_kind_t kind)
 {
        attribute_t *attribute = allocate_ast_zero(sizeof(*attribute));
@@ -1241,8 +1216,7 @@ static attribute_t *parse_attribute_gnu_single(void)
                }
 
                const char *attribute_name = get_attribute_name(kind);
-               if (attribute_name != NULL
-                               && strcmp_underscore(attribute_name, name) == 0)
+               if (attribute_name != NULL && streq_underscore(attribute_name, name))
                        break;
        }
 
@@ -1359,19 +1333,17 @@ static entity_t *determine_lhs_ent(expression_t *const expr,
                                ent     = determine_lhs_ent(ref, lhs_ent);
                                lhs_ent = ent;
                        } else {
-                               mark_vars_read(expr->select.compound, lhs_ent);
+                               mark_vars_read(ref, lhs_ent);
                        }
                        mark_vars_read(expr->array_access.index, lhs_ent);
                        return ent;
                }
 
                case EXPR_SELECT: {
-                       if (is_type_compound(skip_typeref(expr->base.type))) {
+                       mark_vars_read(expr->select.compound, lhs_ent);
+                       if (is_type_compound(skip_typeref(expr->base.type)))
                                return determine_lhs_ent(expr->select.compound, lhs_ent);
-                       } else {
-                               mark_vars_read(expr->select.compound, lhs_ent);
-                               return NULL;
-                       }
+                       return NULL;
                }
 
                case EXPR_UNARY_DEREFERENCE: {
@@ -1448,10 +1420,13 @@ static void mark_vars_read(expression_t *const expr, entity_t *lhs_ent)
                        return;
 
                case EXPR_ARRAY_ACCESS: {
+                       mark_vars_read(expr->array_access.index, lhs_ent);
                        expression_t *const ref = expr->array_access.array_ref;
+                       if (!is_type_array(skip_typeref(revert_automatic_type_conversion(ref)))) {
+                               if (lhs_ent == ENT_ANY)
+                                       lhs_ent = NULL;
+                       }
                        mark_vars_read(ref, lhs_ent);
-                       lhs_ent = determine_lhs_ent(ref, lhs_ent);
-                       mark_vars_read(expr->array_access.index, lhs_ent);
                        return;
                }
 
@@ -1547,7 +1522,7 @@ unary:
                        return;
 
                EXPR_LITERAL_CASES
-               case EXPR_INVALID:
+               case EXPR_ERROR:
                case EXPR_STRING_LITERAL:
                case EXPR_WIDE_STRING_LITERAL:
                case EXPR_COMPOUND_LITERAL: // TODO init?
@@ -1842,11 +1817,10 @@ static void descend_into_subtype(type_path_t *path)
        top->type              = top_type;
 
        if (is_type_compound(top_type)) {
-               compound_t *compound  = top_type->compound.compound;
-               entity_t   *entry     = compound->members.entities;
+               compound_t *const compound = top_type->compound.compound;
+               entity_t   *const entry    = skip_unnamed_bitfields(compound->members.entities);
 
                if (entry != NULL) {
-                       assert(entry->kind == ENTITY_COMPOUND_MEMBER);
                        top->v.compound_entry = &entry->declaration;
                        path->top_type = entry->declaration.type;
                } else {
@@ -1986,7 +1960,7 @@ static void advance_current_object(type_path_t *path, size_t top_path_level)
        } else if (is_type_struct(type)) {
                declaration_t *entry = top->v.compound_entry;
 
-               entity_t *next_entity = entry->base.next;
+               entity_t *const next_entity = skip_unnamed_bitfields(entry->base.next);
                if (next_entity != NULL) {
                        assert(is_declaration(next_entity));
                        entry = &next_entity->declaration;
@@ -2516,9 +2490,9 @@ static type_t *parse_enum_specifier(void)
                entity->base.parent_scope    = current_scope;
        }
 
-       type_t *const type = allocate_type_zero(TYPE_ENUM);
-       type->enumt.enume  = &entity->enume;
-       type->enumt.akind  = ATOMIC_TYPE_INT;
+       type_t *const type     = allocate_type_zero(TYPE_ENUM);
+       type->enumt.enume      = &entity->enume;
+       type->enumt.base.akind = ATOMIC_TYPE_INT;
 
        if (token.kind == '{') {
                if (symbol != NULL) {
@@ -2637,9 +2611,9 @@ static attribute_t *parse_attribute_ms_property(attribute_t *attribute)
 
                symbol_t **prop;
                symbol_t  *symbol = token.identifier.symbol;
-               if (strcmp(symbol->string, "put") == 0) {
+               if (streq(symbol->string, "put")) {
                        prop = &property->put_symbol;
-               } else if (strcmp(symbol->string, "get") == 0) {
+               } else if (streq(symbol->string, "get")) {
                        prop = &property->get_symbol;
                } else {
                        errorf(HERE, "expected put or get in property declspec");
@@ -2675,7 +2649,7 @@ static attribute_t *parse_microsoft_extended_decl_modifier_single(void)
                for (attribute_kind_t k = ATTRIBUTE_MS_FIRST; k <= ATTRIBUTE_MS_LAST;
                     ++k) {
                        const char *attribute_name = get_attribute_name(k);
-                       if (attribute_name != NULL && strcmp(attribute_name, name) == 0) {
+                       if (attribute_name != NULL && streq(attribute_name, name)) {
                                kind = k;
                                break;
                        }
@@ -3144,15 +3118,13 @@ warn_about_long_long:
                }
 
                if (type_specifiers & SPECIFIER_COMPLEX) {
-                       type                = allocate_type_zero(TYPE_COMPLEX);
-                       type->complex.akind = atomic_type;
+                       type = allocate_type_zero(TYPE_COMPLEX);
                } else if (type_specifiers & SPECIFIER_IMAGINARY) {
-                       type                  = allocate_type_zero(TYPE_IMAGINARY);
-                       type->imaginary.akind = atomic_type;
+                       type = allocate_type_zero(TYPE_IMAGINARY);
                } else {
-                       type                 = allocate_type_zero(TYPE_ATOMIC);
-                       type->atomic.akind   = atomic_type;
+                       type = allocate_type_zero(TYPE_ATOMIC);
                }
+               type->atomic.akind = atomic_type;
                newtype = true;
        } else if (type_specifiers != 0) {
                errorf(&specifiers->source_position, "multiple datatypes in declaration");
@@ -3335,8 +3307,7 @@ end_error:
 }
 
 typedef enum construct_type_kind_t {
-       CONSTRUCT_INVALID,
-       CONSTRUCT_POINTER,
+       CONSTRUCT_POINTER = 1,
        CONSTRUCT_REFERENCE,
        CONSTRUCT_FUNCTION,
        CONSTRUCT_ARRAY
@@ -3633,8 +3604,6 @@ static type_t *construct_declarator_type(construct_type_t *construct_list,
        for (; iter != NULL; iter = iter->base.next) {
                source_position_t const* const pos = &iter->base.pos;
                switch (iter->kind) {
-               case CONSTRUCT_INVALID:
-                       break;
                case CONSTRUCT_FUNCTION: {
                        construct_function_type_t *function      = &iter->function;
                        type_t                    *function_type = function->function_type;
@@ -3649,7 +3618,7 @@ static type_t *construct_declarator_type(construct_type_t *construct_list,
                                errorf(pos, "function returning array is not allowed");
                        } else {
                                if (skipped_return_type->base.qualifiers != 0) {
-                                       warningf(WARN_OTHER, pos, "type qualifiers in return type of function type are meaningless");
+                                       warningf(WARN_IGNORED_QUALIFIERS, pos, "type qualifiers in return type of function type are meaningless");
                                }
                        }
 
@@ -3903,6 +3872,10 @@ static entity_t *parse_declarator(const declaration_specifiers_t *specifiers,
                handle_entity_attributes(attributes, entity);
        }
 
+       if (entity->kind == ENTITY_FUNCTION && !freestanding) {
+               adapt_special_functions(&entity->function);
+       }
+
        return entity;
 }
 
@@ -3993,7 +3966,7 @@ warn_arg_count:
  */
 static bool is_sym_main(const symbol_t *const sym)
 {
-       return strcmp(sym->string, "main") == 0;
+       return streq(sym->string, "main");
 }
 
 static void error_redefined_as_different_kind(const source_position_t *pos,
@@ -4119,8 +4092,22 @@ entity_t *record_entity(entity_t *entity, const bool is_definition)
                                goto finish;
                        }
                        if (previous_entity->kind == ENTITY_TYPEDEF) {
-                               /* TODO: C++ allows this for exactly the same type */
-                               errorf(pos, "redefinition of '%N' (declared %P)", entity, ppos);
+                               type_t *const type      = skip_typeref(entity->typedefe.type);
+                               type_t *const prev_type
+                                       = skip_typeref(previous_entity->typedefe.type);
+                               if (c_mode & _CXX) {
+                                       /* C++ allows double typedef if they are identical
+                                        * (after skipping typedefs) */
+                                       if (type == prev_type)
+                                               goto finish;
+                               } else {
+                                       /* GCC extension: redef in system headers is allowed */
+                                       if ((pos->is_system_header || ppos->is_system_header) &&
+                                           types_compatible(type, prev_type))
+                                               goto finish;
+                               }
+                               errorf(pos, "redefinition of '%N' (declared %P)",
+                                      entity, ppos);
                                goto finish;
                        }
 
@@ -4200,7 +4187,7 @@ warn_redundant_declaration: ;
                                                merge_in_attributes(decl, prev_decl->attributes);
                                        } else if (!is_definition        &&
                                                        is_type_valid(prev_type) &&
-                                                       strcmp(ppos->input_name, "<builtin>") != 0) {
+                                                       !pos->is_system_header) {
                                                warningf(WARN_REDUNDANT_DECLS, pos, "redundant declaration for '%Y' (declared %P)", symbol, ppos);
                                        }
                                } else if (current_function == NULL) {
@@ -4777,7 +4764,7 @@ static bool expression_returns(expression_t const *const expr)
                case EXPR_BUILTIN_CONSTANT_P:
                case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
                case EXPR_OFFSETOF:
-               case EXPR_INVALID:
+               case EXPR_ERROR:
                        return true;
 
                case EXPR_STATEMENT: {
@@ -4871,7 +4858,7 @@ static void check_reachable(statement_t *const stmt)
        statement_t *last = stmt;
        statement_t *next;
        switch (stmt->kind) {
-               case STATEMENT_INVALID:
+               case STATEMENT_ERROR:
                case STATEMENT_EMPTY:
                case STATEMENT_ASM:
                        next = stmt->base.next;
@@ -5143,7 +5130,7 @@ found_break_parent:
                }
 
                switch (next->kind) {
-                       case STATEMENT_INVALID:
+                       case STATEMENT_ERROR:
                        case STATEMENT_EMPTY:
                        case STATEMENT_DECLARATION:
                        case STATEMENT_EXPRESSION:
@@ -5804,7 +5791,7 @@ static expression_t *expected_expression_error(void)
        }
        next_token();
 
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 static type_t *get_string_type(void)
@@ -6201,7 +6188,8 @@ static entity_t *parse_qualified_identifier(void)
        if (entity == NULL) {
                if (!strict_mode && token.kind == '(') {
                        /* an implicitly declared function */
-                       warningf(WARN_IMPLICIT_FUNCTION_DECLARATION, &pos, "implicit declaration of function '%Y'", symbol);
+                       warningf(WARN_IMPLICIT_FUNCTION_DECLARATION, &pos,
+                                "implicit declaration of function '%Y'", symbol);
                        entity = create_implicit_function(symbol, &pos);
                } else {
                        errorf(&pos, "unknown identifier '%Y' found.", symbol);
@@ -6357,7 +6345,7 @@ static expression_t *parse_cast(void)
 
        return cast;
 end_error:
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 /**
@@ -6572,14 +6560,14 @@ static expression_t *parse_offsetof(void)
        descend_into_subtype(&path);
 
        if (!walk_designator(&path, designator, true)) {
-               return create_invalid_expression();
+               return create_error_expression();
        }
 
        DEL_ARR_F(path.path);
 
        return expression;
 end_error:
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 /**
@@ -6615,7 +6603,7 @@ static expression_t *parse_va_start(void)
        }
        expect(')', end_error);
 end_error:
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 /**
@@ -6639,7 +6627,7 @@ static expression_t *parse_va_arg(void)
 
        return expression;
 end_error:
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 /**
@@ -6668,7 +6656,7 @@ static expression_t *parse_va_copy(void)
 
        return expression;
 end_error:
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 /**
@@ -6689,7 +6677,7 @@ static expression_t *parse_builtin_constant(void)
 
        return expression;
 end_error:
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 /**
@@ -6714,7 +6702,7 @@ static expression_t *parse_builtin_types_compatible(void)
 
        return expression;
 end_error:
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 /**
@@ -6771,7 +6759,7 @@ static expression_t *parse_compare_builtin(void)
 
        return expression;
 end_error:
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 /**
@@ -6792,7 +6780,7 @@ static expression_t *parse_assume(void)
        expression->base.type = type_void;
        return expression;
 end_error:
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 /**
@@ -6829,7 +6817,7 @@ static expression_t *parse_label_address(void)
        eat(T_ANDAND);
        if (token.kind != T_IDENTIFIER) {
                parse_error_expected("while parsing label address", T_IDENTIFIER, NULL);
-               return create_invalid_expression();
+               return create_error_expression();
        }
 
        label_t *const label = get_label();
@@ -6933,13 +6921,13 @@ static expression_t *parse_primary_expression(void)
                parse_declaration_specifiers(&specifiers);
                type_t const *const type = parse_abstract_declarator(specifiers.type);
                errorf(&pos, "encountered type '%T' while parsing expression", type);
-               return create_invalid_expression();
+               return create_error_expression();
        }
        }
 
        errorf(HERE, "unexpected token %K, expected an expression", &token);
        eat_until_anchor();
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 static expression_t *parse_array_expression(expression_t *left)
@@ -7093,7 +7081,7 @@ static expression_t *parse_select_expression(expression_t *addr)
 
        if (token.kind != T_IDENTIFIER) {
                parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
-               return create_invalid_expression();
+               return create_error_expression();
        }
        symbol_t *symbol = token.identifier.symbol;
        next_token();
@@ -7127,14 +7115,14 @@ static expression_t *parse_select_expression(expression_t *addr)
                               "request for member '%Y' in something not a struct or union, but '%T'",
                               symbol, type_left);
                }
-               return create_invalid_expression();
+               return create_error_expression();
        }
 
        compound_t *compound = type_left->compound.compound;
        if (!compound->complete) {
                errorf(&pos, "request for member '%Y' in incomplete type '%T'",
                       symbol, type_left);
-               return create_invalid_expression();
+               return create_error_expression();
        }
 
        type_qualifiers_t  qualifiers = type_left->base.qualifiers;
@@ -7143,7 +7131,7 @@ static expression_t *parse_select_expression(expression_t *addr)
 
        if (result == NULL) {
                errorf(&pos, "'%T' has no member named '%Y'", orig_type, symbol);
-               return create_invalid_expression();
+               return create_error_expression();
        }
 
        return result;
@@ -7207,32 +7195,25 @@ static void check_call_argument(type_t          *expected_type,
 /**
  * Handle the semantic restrictions of builtin calls
  */
-static void handle_builtin_argument_restrictions(call_expression_t *call) {
-       switch (call->function->reference.entity->function.btk) {
-               case bk_gnu_builtin_return_address:
-               case bk_gnu_builtin_frame_address: {
+static void handle_builtin_argument_restrictions(call_expression_t *call)
+{
+       entity_t *entity = call->function->reference.entity;
+       switch (entity->function.btk) {
+       case BUILTIN_FIRM:
+               switch (entity->function.b.firm_builtin_kind) {
+               case ir_bk_return_address:
+               case ir_bk_frame_address: {
                        /* argument must be constant */
                        call_argument_t *argument = call->arguments;
 
                        if (is_constant_expression(argument->expression) == EXPR_CLASS_VARIABLE) {
                                errorf(&call->base.source_position,
-                                      "argument of '%Y' must be a constant expression",
-                                      call->function->reference.entity->base.symbol);
-                       }
-                       break;
-               }
-               case bk_gnu_builtin_object_size:
-                       if (call->arguments == NULL)
-                               break;
-
-                       call_argument_t *arg = call->arguments->next;
-                       if (arg != NULL && is_constant_expression(arg->expression) == EXPR_CLASS_VARIABLE) {
-                               errorf(&call->base.source_position,
-                                          "second argument of '%Y' must be a constant expression",
+                                          "argument of '%Y' must be a constant expression",
                                           call->function->reference.entity->base.symbol);
                        }
                        break;
-               case bk_gnu_builtin_prefetch:
+               }
+               case ir_bk_prefetch:
                        /* second and third argument must be constant if existent */
                        if (call->arguments == NULL)
                                break;
@@ -7242,22 +7223,37 @@ static void handle_builtin_argument_restrictions(call_expression_t *call) {
                        if (rw != NULL) {
                                if (is_constant_expression(rw->expression) == EXPR_CLASS_VARIABLE) {
                                        errorf(&call->base.source_position,
-                                              "second argument of '%Y' must be a constant expression",
-                                              call->function->reference.entity->base.symbol);
+                                                  "second argument of '%Y' must be a constant expression",
+                                                  call->function->reference.entity->base.symbol);
                                }
                                locality = rw->next;
                        }
                        if (locality != NULL) {
                                if (is_constant_expression(locality->expression) == EXPR_CLASS_VARIABLE) {
                                        errorf(&call->base.source_position,
-                                              "third argument of '%Y' must be a constant expression",
-                                              call->function->reference.entity->base.symbol);
+                                                  "third argument of '%Y' must be a constant expression",
+                                                  call->function->reference.entity->base.symbol);
                                }
                                locality = rw->next;
                        }
                        break;
                default:
                        break;
+               }
+
+       case BUILTIN_OBJECT_SIZE:
+               if (call->arguments == NULL)
+                       break;
+
+               call_argument_t *arg = call->arguments->next;
+               if (arg != NULL && is_constant_expression(arg->expression) == EXPR_CLASS_VARIABLE) {
+                       errorf(&call->base.source_position,
+                                  "second argument of '%Y' must be a constant expression",
+                                  call->function->reference.entity->base.symbol);
+               }
+               break;
+       default:
+               break;
        }
 }
 
@@ -7353,7 +7349,7 @@ static expression_t *parse_call_expression(expression_t *expression)
        if (expression->kind == EXPR_REFERENCE) {
                reference_expression_t *reference = &expression->reference;
                if (reference->entity->kind == ENTITY_FUNCTION &&
-                   reference->entity->function.btk != bk_none)
+                   reference->entity->function.btk != BUILTIN_NONE)
                        handle_builtin_argument_restrictions(call);
        }
 
@@ -7580,7 +7576,7 @@ static expression_t *parse_builtin_classify_type(void)
 
        return result;
 end_error:
-       return create_invalid_expression();
+       return create_error_expression();
 }
 
 /**
@@ -7731,6 +7727,13 @@ static void semantic_incdec(unary_expression_t *expression)
        expression->base.type = orig_type;
 }
 
+static void promote_unary_int_expr(unary_expression_t *const expr, type_t *const type)
+{
+       type_t *const res_type = promote_integer(type);
+       expr->base.type = res_type;
+       expr->value     = create_implicit_cast(expr->value, res_type);
+}
+
 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
 {
        type_t *const orig_type = expression->value->base.type;
@@ -7742,9 +7745,11 @@ static void semantic_unexpr_arithmetic(unary_expression_t *expression)
                                "operation needs an arithmetic type");
                }
                return;
+       } else if (is_type_integer(type)) {
+               promote_unary_int_expr(expression, type);
+       } else {
+               expression->base.type = orig_type;
        }
-
-       expression->base.type = orig_type;
 }
 
 static void semantic_unexpr_plus(unary_expression_t *expression)
@@ -7773,7 +7778,7 @@ static void semantic_unexpr_integer(unary_expression_t *expression)
                return;
        }
 
-       expression->base.type = orig_type;
+       promote_unary_int_expr(expression, type);
 }
 
 static void semantic_dereference(unary_expression_t *expression)
@@ -7923,45 +7928,44 @@ static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
        if (type_left == type_right)
                return type_left;
 
-       bool const signed_left  = is_type_signed(type_left);
-       bool const signed_right = is_type_signed(type_right);
-       int const  rank_left    = get_rank(type_left);
-       int const  rank_right   = get_rank(type_right);
+       bool     const signed_left  = is_type_signed(type_left);
+       bool     const signed_right = is_type_signed(type_right);
+       unsigned const rank_left    = get_akind_rank(get_akind(type_left));
+       unsigned const rank_right   = get_akind_rank(get_akind(type_right));
 
        if (signed_left == signed_right)
                return rank_left >= rank_right ? type_left : type_right;
 
-       int     s_rank;
-       int     u_rank;
+       unsigned           s_rank;
+       unsigned           u_rank;
+       atomic_type_kind_t s_akind;
+       atomic_type_kind_t u_akind;
        type_t *s_type;
        type_t *u_type;
        if (signed_left) {
-               s_rank = rank_left;
                s_type = type_left;
-               u_rank = rank_right;
                u_type = type_right;
        } else {
-               s_rank = rank_right;
                s_type = type_right;
-               u_rank = rank_left;
                u_type = type_left;
        }
+       s_akind = get_akind(s_type);
+       u_akind = get_akind(u_type);
+       s_rank  = get_akind_rank(s_akind);
+       u_rank  = get_akind_rank(u_akind);
 
        if (u_rank >= s_rank)
                return u_type;
 
-       /* casting rank to atomic_type_kind is a bit hacky, but makes things
-        * easier here... */
-       if (get_atomic_type_size((atomic_type_kind_t) s_rank)
-                       > get_atomic_type_size((atomic_type_kind_t) u_rank))
+       if (get_atomic_type_size(s_akind) > get_atomic_type_size(u_akind))
                return s_type;
 
-       switch (s_rank) {
-               case ATOMIC_TYPE_INT:      return type_unsigned_int;
-               case ATOMIC_TYPE_LONG:     return type_unsigned_long;
-               case ATOMIC_TYPE_LONGLONG: return type_unsigned_long_long;
+       switch (s_akind) {
+       case ATOMIC_TYPE_INT:      return type_unsigned_int;
+       case ATOMIC_TYPE_LONG:     return type_unsigned_long;
+       case ATOMIC_TYPE_LONGLONG: return type_unsigned_long_long;
 
-               default: panic("invalid atomic type");
+       default: panic("invalid atomic type");
        }
 }
 
@@ -8523,7 +8527,7 @@ static void semantic_binexpr_assign(binary_expression_t *expression)
 static bool expression_has_effect(const expression_t *const expr)
 {
        switch (expr->kind) {
-               case EXPR_INVALID:                    return true; /* do NOT warn */
+               case EXPR_ERROR:                      return true; /* do NOT warn */
                case EXPR_REFERENCE:                  return false;
                case EXPR_REFERENCE_ENUM_VALUE:       return false;
                case EXPR_LABEL_ADDRESS:              return false;
@@ -9033,7 +9037,7 @@ end_of_asm:
 
        return statement;
 end_error:
-       return create_invalid_statement();
+       return create_error_statement();
 }
 
 static statement_t *parse_label_inner_statement(statement_t const *const label, char const *const label_kind)
@@ -9042,7 +9046,7 @@ static statement_t *parse_label_inner_statement(statement_t const *const label,
        switch (token.kind) {
                case '}':
                        errorf(&label->base.source_position, "%s at end of compound statement", label_kind);
-                       inner_stmt = create_invalid_statement();
+                       inner_stmt = create_error_statement();
                        break;
 
                case ';':
@@ -9273,8 +9277,18 @@ end_error:
        statement->ifs.true_statement = true_stmt;
        rem_anchor_token(T_else);
 
+       if (true_stmt->kind == STATEMENT_EMPTY) {
+               warningf(WARN_EMPTY_BODY, HERE,
+                       "suggest braces around empty body in an ‘if’ statement");
+       }
+
        if (next_if(T_else)) {
                statement->ifs.false_statement = parse_inner_statement();
+
+               if (statement->ifs.false_statement->kind == STATEMENT_EMPTY) {
+                       warningf(WARN_EMPTY_BODY, HERE,
+                                       "suggest braces around empty body in an ‘if’ statement");
+               }
        } else if (true_stmt->kind == STATEMENT_IF &&
                        true_stmt->ifs.false_statement != NULL) {
                source_position_t const *const pos = &true_stmt->base.source_position;
@@ -9346,7 +9360,7 @@ static statement_t *parse_switch(void)
        type_t       *      type = skip_typeref(expr->base.type);
        if (is_type_integer(type)) {
                type = promote_integer(type);
-               if (get_rank(type) >= get_akind_rank(ATOMIC_TYPE_LONG)) {
+               if (get_akind_rank(get_akind(type)) >= get_akind_rank(ATOMIC_TYPE_LONG)) {
                        warningf(WARN_TRADITIONAL, &expr->base.source_position, "'%T' switch expression not converted to '%T' in ISO C", type, type_int);
                }
        } else if (is_type_valid(type)) {
@@ -9372,7 +9386,7 @@ static statement_t *parse_switch(void)
        return statement;
 end_error:
        POP_PARENT();
-       return create_invalid_statement();
+       return create_error_statement();
 }
 
 static statement_t *parse_loop_body(statement_t *const loop)
@@ -9414,7 +9428,7 @@ static statement_t *parse_while(void)
        return statement;
 end_error:
        POP_PARENT();
-       return create_invalid_statement();
+       return create_error_statement();
 }
 
 /**
@@ -9449,7 +9463,7 @@ static statement_t *parse_do(void)
        return statement;
 end_error:
        POP_PARENT();
-       return create_invalid_statement();
+       return create_error_statement();
 }
 
 /**
@@ -9520,7 +9534,7 @@ end_error2:
        /* fallthrough */
 
 end_error1:
-       return create_invalid_statement();
+       return create_error_statement();
 }
 
 /**
@@ -9560,7 +9574,7 @@ static statement_t *parse_goto(void)
                else
                        parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
                eat_until_anchor();
-               return create_invalid_statement();
+               return create_error_statement();
        }
 
        /* remember the goto's in a list for later checking */
@@ -9828,11 +9842,11 @@ static statement_t *parse_ms_try_statment(void)
                statement->ms_try.final_statement = parse_compound_statement(false);
        } else {
                parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
-               return create_invalid_statement();
+               return create_error_statement();
        }
        return statement;
 end_error:
-       return create_invalid_statement();
+       return create_error_statement();
 }
 
 static statement_t *parse_empty_statement(void)
@@ -10015,7 +10029,7 @@ static statement_t *intern_parse_statement(void)
 
        default:
                errorf(HERE, "unexpected token %K while parsing statement", &token);
-               statement = create_invalid_statement();
+               statement = create_error_statement();
                if (!at_anchor())
                        next_token();
                break;
@@ -10158,7 +10172,7 @@ static statement_t *parse_compound_statement(bool inside_expression_statement)
                        break;
                }
                statement_t *sub_statement = intern_parse_statement();
-               if (is_invalid_statement(sub_statement)) {
+               if (sub_statement->kind == STATEMENT_ERROR) {
                        /* an error occurred. if we are at an anchor, return */
                        if (at_anchor())
                                goto end_error;
@@ -10357,13 +10371,13 @@ static void parse_linkage_specification(void)
 
        linkage_kind_t old_linkage = current_linkage;
        linkage_kind_t new_linkage;
-       if (strcmp(linkage, "C") == 0) {
+       if (streq(linkage, "C")) {
                new_linkage = LINKAGE_C;
-       } else if (strcmp(linkage, "C++") == 0) {
+       } else if (streq(linkage, "C++")) {
                new_linkage = LINKAGE_CXX;
        } else {
                errorf(&pos, "linkage string \"%s\" not recognized", linkage);
-               new_linkage = LINKAGE_INVALID;
+               new_linkage = LINKAGE_C;
        }
        current_linkage = new_linkage;
 
@@ -10410,7 +10424,7 @@ static void parse_external(void)
 
                case ';':
                        if (!strict_mode) {
-                               warningf(WARN_OTHER, HERE, "stray ';' outside of function");
+                               warningf(WARN_STRAY_SEMICOLON, HERE, "stray ';' outside of function");
                                next_token();
                                return;
                        }