Restore error check lost in r23661: The type of a function definition must not be...
[cparser] / ast.c
diff --git a/ast.c b/ast.c
index 5910085..e25d244 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -241,10 +241,11 @@ static void print_quoted_string(const string_t *const string, char border, int s
        fputc(border, out);
        const char *end = string->begin + string->size - skip;
        for (const char *c = string->begin; c != end; ++c) {
-               if (*c == border) {
+               unsigned char const tc = *c;
+               if (tc == border) {
                        fputc('\\', out);
                }
-               switch (*c) {
+               switch (tc) {
                case '\\':  fputs("\\\\", out); break;
                case '\a':  fputs("\\a", out); break;
                case '\b':  fputs("\\b", out); break;
@@ -260,11 +261,11 @@ static void print_quoted_string(const string_t *const string, char border, int s
                        }
                        /* FALLTHROUGH */
                default:
-                       if (!isprint(*c)) {
-                               fprintf(out, "\\%03o", (unsigned)(unsigned char)*c);
-                               break;
+                       if (tc < 0x80 && !isprint(tc)) {
+                               fprintf(out, "\\%03o", (unsigned)tc);
+                       } else {
+                               fputc(tc, out);
                        }
-                       fputc(*c, out);
                        break;
                }
        }
@@ -999,25 +1000,6 @@ static void print_case_label(const case_label_statement_t *statement)
        }
 }
 
-static void print_local_label(const local_label_statement_t *statement)
-{
-       fputs("__label__ ", out);
-
-       bool      first  = true;
-       entity_t *entity = statement->labels_begin;
-       for (;
-               entity != statement->labels_end->base.next;
-               entity = entity->base.next) {
-               if (!first) {
-                       fputs(", ", out);
-               } else {
-                       first = false;
-               }
-               fputs(entity->base.symbol->string, out);
-       }
-       fputs(";\n", out);
-}
-
 static void print_typedef(const entity_t *entity)
 {
        fputs("typedef ", out);
@@ -1057,7 +1039,7 @@ static void print_declaration_statement(
 
        entity_t *const end = statement->declarations_end->base.next;
        for (; entity != end; entity = entity->base.next) {
-               if (!is_declaration(entity) && entity->kind != ENTITY_TYPEDEF)
+               if (entity->kind == ENTITY_ENUM_VALUE)
                        continue;
                if (is_generated_entity(entity))
                        continue;
@@ -1068,13 +1050,7 @@ static void print_declaration_statement(
                        first = false;
                }
 
-               if (entity->kind == ENTITY_TYPEDEF) {
-                       print_typedef(entity);
-               } else {
-                       assert(is_declaration(entity));
-                       print_declaration(entity);
-               }
-
+               print_entity(entity);
                fputc('\n', out);
        }
 }
@@ -1272,9 +1248,6 @@ void print_statement(const statement_t *statement)
        case STATEMENT_LABEL:
                print_label_statement(&statement->label);
                break;
-       case STATEMENT_LOCAL_LABEL:
-               print_local_label(&statement->local_label);
-               break;
        case STATEMENT_GOTO:
                print_goto_statement(&statement->gotos);
                break;
@@ -1619,9 +1592,11 @@ void print_entity(const entity_t *entity)
        case ENTITY_NAMESPACE:
                print_namespace(&entity->namespacee);
                return;
+       case ENTITY_LOCAL_LABEL:
+               fprintf(out, "__label__ %s;", entity->base.symbol->string);
+               return;
        case ENTITY_LABEL:
        case ENTITY_ENUM_VALUE:
-       case ENTITY_LOCAL_LABEL:
                panic("print_entity used on unexpected entity type");
        case ENTITY_INVALID:
                break;
@@ -1953,8 +1928,6 @@ bool is_constant_expression(const expression_t *expression)
        case EXPR_BINARY_BITWISE_AND:
        case EXPR_BINARY_BITWISE_OR:
        case EXPR_BINARY_BITWISE_XOR:
-       case EXPR_BINARY_LOGICAL_AND:
-       case EXPR_BINARY_LOGICAL_OR:
        case EXPR_BINARY_SHIFTLEFT:
        case EXPR_BINARY_SHIFTRIGHT:
        case EXPR_BINARY_ISGREATER:
@@ -1966,6 +1939,24 @@ bool is_constant_expression(const expression_t *expression)
                return is_constant_expression(expression->binary.left)
                        && is_constant_expression(expression->binary.right);
 
+       case EXPR_BINARY_LOGICAL_AND: {
+               expression_t const *const left = expression->binary.left;
+               if (!is_constant_expression(left))
+                       return false;
+               if (fold_constant(left) == 0)
+                       return true;
+               return is_constant_expression(expression->binary.right);
+       }
+
+       case EXPR_BINARY_LOGICAL_OR: {
+               expression_t const *const left = expression->binary.left;
+               if (!is_constant_expression(left))
+                       return false;
+               if (fold_constant(left) != 0)
+                       return true;
+               return is_constant_expression(expression->binary.right);
+       }
+
        case EXPR_COMPOUND_LITERAL:
                return is_constant_initializer(expression->compound_literal.initializer);