Remove the unused STATEMENT_LOCAL_LABEL/struct local_label_statement_t. It never...
[cparser] / ast.c
diff --git a/ast.c b/ast.c
index e9cee0a..f09b346 100644 (file)
--- a/ast.c
+++ b/ast.c
 #include <stdlib.h>
 #include <ctype.h>
 
-#ifdef __INTEL_COMPILER
+#if defined(__INTEL_COMPILER)
 #include <mathimf.h>
+#elif defined(__CYGWIN__)
+#include "win32/cygwin_math_ext.h"
 #else
 #include <math.h>
 #endif
@@ -259,7 +261,7 @@ static void print_quoted_string(const string_t *const string, char border, int s
                        /* FALLTHROUGH */
                default:
                        if (!isprint(*c)) {
-                               fprintf(out, "\\%03o", (unsigned)*c);
+                               fprintf(out, "\\%03o", (unsigned)(unsigned char)*c);
                                break;
                        }
                        fputc(*c, out);
@@ -302,10 +304,10 @@ static void print_quoted_wide_string(const wide_string_t *const wstr,
                        default: {
                                const unsigned tc = *c;
                                if (tc < 0x80U) {
-                                       if (!isprint(*c)) {
-                                               fprintf(out, "\\%03o", (char)*c);
-                                       } else {
+                                       if (isprint(*c)) {
                                                fputc(*c, out);
+                                       } else {
+                                               fprintf(out, "\\%03o", tc);
                                        }
                                } else if (tc < 0x800) {
                                        fputc(0xC0 | (tc >> 6),   out);
@@ -758,10 +760,13 @@ static void print_expression_prec(const expression_t *expression, unsigned top_p
        if (expression->kind == EXPR_UNARY_CAST_IMPLICIT && !print_implicit_casts) {
                expression = expression->unary.value;
        }
-       unsigned prec = get_expression_precedence(expression->base.kind);
-       if (print_parenthesis && top_prec != PREC_BOTTOM)
-               top_prec = PREC_TOP;
-       if (top_prec > prec)
+
+       bool parenthesized =
+               expression->base.parenthesized                 ||
+               (print_parenthesis && top_prec != PREC_BOTTOM) ||
+               top_prec > get_expression_precedence(expression->base.kind);
+
+       if (parenthesized)
                fputc('(', out);
        switch (expression->kind) {
        case EXPR_UNKNOWN:
@@ -848,7 +853,7 @@ static void print_expression_prec(const expression_t *expression, unsigned top_p
                fprintf(out, "some expression of type %d", (int)expression->kind);
                break;
        }
-       if (top_prec > prec)
+       if (parenthesized)
                fputc(')', out);
 }
 
@@ -874,7 +879,7 @@ static void print_compound_statement(const compound_statement_t *block)
        }
        --indent;
        print_indent();
-       fputs("}\n", out);
+       fputs(block->stmt_expr ? "}" : "}\n", out);
 }
 
 /**
@@ -994,25 +999,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);
@@ -1121,18 +1107,19 @@ static void print_for_statement(const for_statement_t *statement)
                if (entity->base.next != NULL) {
                        panic("multiple declarations in for statement not supported yet");
                }
-               fputc(' ', out);
        } else {
                if (statement->initialisation) {
                        print_expression(statement->initialisation);
                }
-               fputs("; ", out);
+               fputc(';', out);
        }
        if (statement->condition != NULL) {
+               fputc(' ', out);
                print_expression(statement->condition);
        }
-       fputs("; ", out);
+       fputc(';', out);
        if (statement->step != NULL) {
+               fputc(' ', out);
                print_expression(statement->step);
        }
        fputs(") ", out);
@@ -1266,9 +1253,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;
@@ -1321,16 +1305,15 @@ void print_statement(const statement_t *statement)
  */
 static void print_storage_class(storage_class_tag_t storage_class)
 {
-       const char *text;
        switch (storage_class) {
        case STORAGE_CLASS_NONE:     return;
-       case STORAGE_CLASS_TYPEDEF:  text = "typedef ";  break;
-       case STORAGE_CLASS_EXTERN:   text = "extern ";   break;
-       case STORAGE_CLASS_STATIC:   text = "static ";   break;
-       case STORAGE_CLASS_AUTO:     text = "auto ";     break;
-       case STORAGE_CLASS_REGISTER: text = "register "; break;
+       case STORAGE_CLASS_TYPEDEF:  fputs("typedef ",  out); return;
+       case STORAGE_CLASS_EXTERN:   fputs("extern ",   out); return;
+       case STORAGE_CLASS_STATIC:   fputs("static ",   out); return;
+       case STORAGE_CLASS_AUTO:     fputs("auto ",     out); return;
+       case STORAGE_CLASS_REGISTER: fputs("register ", out); return;
        }
-       fputs(text, out);
+       panic("invalid storage class");
 }
 
 /**
@@ -1348,7 +1331,7 @@ void print_initializer(const initializer_t *initializer)
        switch (initializer->kind) {
        case INITIALIZER_VALUE: {
                const initializer_value_t *value = &initializer->value;
-               print_expression(value->value);
+               print_assignment_expression(value->value);
                return;
        }
        case INITIALIZER_LIST: {
@@ -1614,9 +1597,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;