From 45002a9bc0d85c57215aabd153e8a95825ece7d2 Mon Sep 17 00:00:00 2001 From: Christoph Mallon Date: Mon, 25 Aug 2008 07:06:40 +0000 Subject: [PATCH] Implement -Wdeclaration-after-statement. [r21427] --- parser.c | 10 ++++++++++ warning.c | 2 ++ warning.h | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/parser.c b/parser.c index 2b0897d..0f2dd11 100644 --- a/parser.c +++ b/parser.c @@ -8932,6 +8932,7 @@ static statement_t *parse_compound_statement(bool inside_expression_statement) statement_t *last_statement = NULL; + bool only_decls_so_far = true; while (token.type != '}' && token.type != T_EOF) { statement_t *sub_statement = intern_parse_statement(); if (is_invalid_statement(sub_statement)) { @@ -8941,6 +8942,15 @@ static statement_t *parse_compound_statement(bool inside_expression_statement) continue; } + if (warning.declaration_after_statement) { + if (sub_statement->kind != STATEMENT_DECLARATION) { + only_decls_so_far = false; + } else if (!only_decls_so_far) { + warningf(&sub_statement->base.source_position, + "ISO C90 forbids mixed declarations and code"); + } + } + if (last_statement != NULL) { last_statement->base.next = sub_statement; } else { diff --git a/warning.c b/warning.c index 5aa8aa9..5dd407c 100644 --- a/warning.c +++ b/warning.c @@ -24,6 +24,7 @@ warning_t warning = { .attribute = true, .char_subscripts = true, + .declaration_after_statement = false, .deprecated_declarations = true, .empty_statement = false, .fatal_errors = false, @@ -93,6 +94,7 @@ void set_warning_opt(const char *const opt) } OPT("attribute", attribute); OPT("char-subscripts", char_subscripts); + OPT("declaration-after-statement", declaration_after_statement); OPT("deprecated-declarations", deprecated_declarations); OPT("empty-statement", empty_statement); OPT("error", s_are_errors); diff --git a/warning.h b/warning.h index 24ae114..0229078 100644 --- a/warning.h +++ b/warning.h @@ -37,8 +37,8 @@ typedef struct warning_t { bool cast_align:1; /**< Warn whenever a pointer is cast such that the required alignment of the target is increased */ bool cast_qual:1; /**< Warn whenever a pointer is cast so as to remove a type qualifier from the target type */ bool conversion:1; /**< Warn if a prototype causes a type conversion that is different from what would happen to the same argument in the absence of a prototype */ - bool declaration_after_statement:1; /**< Warn when a declaration is found after a statement in a block */ #endif + bool declaration_after_statement:1; /**< Warn when a declaration is found after a statement in a block */ bool deprecated_declarations:1; /* TODO implement for types */ /**< Warn about uses of functions, variables and types marked as deprecated by using the 'deprecated' attribute */ #if 0 // TODO bool div_by_zero:1; /**< Warn about compile-time integer division by zero */ -- 2.20.1