Implement and document -Wshadow-local.
authorChristoph Mallon <christoph.mallon@gmx.de>
Wed, 15 Jun 2011 16:47:29 +0000 (18:47 +0200)
committerChristoph Mallon <christoph.mallon@gmx.de>
Wed, 15 Jun 2011 16:52:00 +0000 (18:52 +0200)
- This works like -Wshadow, but only warns if the shadowed declaration is not global.
- Activate it with -Wall.
- Demote -Wshadow to -Wextra.

cparser.1
parser.c
warning.c
warning.h

index 3470227..3700bd9 100644 (file)
--- a/cparser.1
+++ b/cparser.1
@@ -119,7 +119,7 @@ In particular these are
 .Fl Wpointer-arith ,
 .Fl Wredundant-decls ,
 .Fl Wreturn-type ,
-.Fl Wshadow ,
+.Fl Wshadow-local ,
 .Fl Wsign-compare ,
 .Fl Wstrict-prototypes ,
 .Fl Wswitch-enum ,
@@ -152,6 +152,7 @@ Generate an error, when calling a function without a prior declaration.
 Activate some more warnings.
 In particular these are
 .Fl Wempty-statement ,
+.Fl Wshadow ,
 .Fl Wunused-parameter ,
 .Fl Wunused-value .
 .It Fl Wfatal-errors
@@ -207,6 +208,10 @@ Warn if + or - is used as operand of << or >>, e.g. x\ +\ y\ <<\ z.
 Warn about redundant declarations, i.e. multiple declarations of the same object or static forward declarations which have no use before their definition.
 .It Fl Wshadow
 Warn when a new declaration shadows another declaration with the same name in an outer scope.
+.It Fl Wshadow-local
+Like
+.Fl Wshadow ,
+but only warn if the shadowed declaration is not global, e.g. a local variable shadows a parameter or another local variable.
 .It Fl Wunreachable-code
 Warn when the compiler determines that a statement (or in some cases a part thereof) will never be executed.
 .It Fl Wunused
index c01e2d2..3e9709b 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -4376,7 +4376,8 @@ error_redeclaration:
                        return previous_entity;
                }
 
-               if (warning.shadow) {
+               if (warning.shadow ||
+                               (warning.shadow_local && previous_entity->base.parent_scope != file_scope)) {
                        warningf(pos, "%s '%Y' shadows %s (declared %P)",
                                        get_entity_kind_name(entity->kind), symbol,
                                        get_entity_kind_name(previous_entity->kind),
index 044af8d..d8931d7 100644 (file)
--- a/warning.c
+++ b/warning.c
@@ -60,6 +60,7 @@ warning_t warning = {
        .return_type                         = true,
        .s_are_errors                        = false,
        .shadow                              = false,
+       .shadow_local                        = false,
        .sign_compare                        = false,
        .strict_prototypes                   = true,
        .switch_default                      = false,
@@ -118,6 +119,7 @@ void print_warning_opt_help(void)
        put_help("-Wredundant-decls",               "");
        put_help("-Wreturn-type",                   "");
        put_help("-Wshadow",                        "");
+       put_help("-Wshadow-local",                  "");
        put_help("-Wsign-compare",                  "");
        put_help("-Wstrict-prototypes",             "");
        put_help("-Wswitch-default",                "");
@@ -172,7 +174,7 @@ void set_warning_opt(const char *const opt)
                SET(pointer_arith);
                SET(redundant_decls);
                SET(return_type);
-               SET(shadow);
+               SET(shadow_local);
                SET(sign_compare);
                SET(strict_prototypes);
                SET(switch_enum);
@@ -206,6 +208,7 @@ extra:
                // TODO SET(incomplete_aggregate_init);
                // TODO SET(missing_field_initializers);
                // TODO SET(pointless_comparison);
+               SET(shadow);
                SET(unused_parameter);
                SET(unused_value);
        }
@@ -238,6 +241,7 @@ extra:
        OPT("redundant-decls",                     redundant_decls);
        OPT("return-type",                         return_type);
        OPT("shadow",                              shadow);
+       OPT("shadow-local",                        shadow_local);
        OPT("sign-compare",                        sign_compare);
        OPT("strict-prototypes",                   strict_prototypes);
        OPT("switch-default",                      switch_default);
index bdad144..32f4404 100644 (file)
--- a/warning.h
+++ b/warning.h
@@ -91,6 +91,7 @@ typedef struct warning_t {
        bool sequence_point:1;                      /**< Warn about code that may have undefined semantics because of violations of sequence point rules */
 #endif
        bool shadow:1;                              /**< Warn whenever a local variable shadows another local variable, parameter or global variable or whenever a built-in function is shadowed */
+       bool shadow_local:1;
        bool sign_compare:1;                        /**< Warn when a comparison between signed and unsigned values could produce an incorrect result when the signed value is converted to unsigned */
 #if 0 // TODO
        bool strict_aliasing:1;                     /**< Warn about code which might break the strict aliasing rules that the compiler is using for optimization. */