Implement -Wnested-externs.
authorChristoph Mallon <christoph.mallon@gmx.de>
Mon, 25 Aug 2008 07:27:04 +0000 (07:27 +0000)
committerChristoph Mallon <christoph.mallon@gmx.de>
Mon, 25 Aug 2008 07:27:04 +0000 (07:27 +0000)
[r21428]

parser.c
warning.c
warning.h

index 0f2dd11..38dffce 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -4114,7 +4114,7 @@ static declaration_t *internal_record_declaration(
        const symbol_t *const symbol  = declaration->symbol;
        const namespace_t     namespc = (namespace_t)declaration->namespc;
 
-       assert(declaration->symbol != NULL);
+       assert(symbol != NULL);
        declaration_t *previous_declaration = get_declaration(symbol, namespc);
 
        type_t *const orig_type = declaration->type;
@@ -4132,6 +4132,13 @@ static declaration_t *internal_record_declaration(
                check_type_of_main(declaration, &type->function);
        }
 
+       if (warning.nested_externs                             &&
+           declaration->storage_class == STORAGE_CLASS_EXTERN &&
+           scope                      != global_scope) {
+               warningf(&declaration->source_position,
+                        "nested extern declaration of '%#T'", declaration->type, symbol);
+       }
+
        assert(declaration != previous_declaration);
        if (previous_declaration != NULL
                        && previous_declaration->parent_scope == scope) {
index 5dd407c..a5880ce 100644 (file)
--- a/warning.c
+++ b/warning.c
@@ -36,6 +36,7 @@ warning_t warning = {
        .missing_declarations          = false,
        .missing_prototypes            = false,
        .multichar                     = true,
+       .nested_externs                = false,
        .nonnull                       = true,
        .redundant_decls               = true,
        .return_type                   = true,
@@ -123,6 +124,7 @@ void set_warning_opt(const char *const opt)
        OPT("missing-declarations",          missing_declarations);
        OPT("missing-prototypes",            missing_prototypes);
        OPT("multichar",                     multichar);
+       OPT("nested-externs",                nested_externs);
        OPT("nonnull",                       nonnull);
        OPT("redundant-decls",               redundant_decls);
        OPT("return-type",                   return_type);
index 0229078..e36d630 100644 (file)
--- a/warning.h
+++ b/warning.h
@@ -66,8 +66,8 @@ typedef struct warning_t {
 #endif
        bool missing_prototypes:1;            /**< Warn if a global function is defined without a previous prototype declaration */
        bool multichar:1;                     /**< Warn if a multicharacter constant ('FOOF') is used. */
-#if 0 // TODO
        bool nested_externs:1;                /**< Warn if an 'extern' declaration is encountered within a function */
+#if 0 // TODO
        bool packed:1;                        /**< Warn if a structure is given the packed attribute, but the packed attribute has no effect on the layout or size of the structure */
        bool padded:1;                        /**< Warn if padding is included in a structure, either to align an element of the structure or to align the whole structure */
        bool parentheses:1;                   /**< Warn if parentheses are omitted in certain contexts (assignment where truth value is expected, if-else-braces) */