X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ast.c;h=e25d2446d531cde7173e3e7ddb65e40e4d90d473;hb=6731ea31e05b733e558c3daeb6817e7b2737e803;hp=f8ead47128efff95ece59cf2c9ad1979fb2fd8eb;hpb=e9661114a5bacff4981be6e64a4c971640f0a9c0;p=cparser diff --git a/ast.c b/ast.c index f8ead47..e25d244 100644 --- 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; } } @@ -304,10 +305,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", tc); - } else { + if (isprint(*c)) { fputc(*c, out); + } else { + fprintf(out, "\\%03o", tc); } } else if (tc < 0x800) { fputc(0xC0 | (tc >> 6), out); @@ -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);