- implemented -Winit-self
authorMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Fri, 12 Sep 2008 21:45:15 +0000 (21:45 +0000)
committerMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Fri, 12 Sep 2008 21:45:15 +0000 (21:45 +0000)
[r21914]

parser.c
parsetest/should_warn/init_self.c [new file with mode: 0644]
warning.c
warning.h

index 380d207..5f875be 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -114,6 +114,7 @@ static scope_t            *global_scope      = NULL;
 static scope_t            *scope             = NULL;
 static declaration_t      *last_declaration  = NULL;
 static declaration_t      *current_function  = NULL;
+static declaration_t      *current_init_decl = NULL;
 static switch_statement_t *current_switch    = NULL;
 static statement_t        *current_loop      = NULL;
 static statement_t        *current_parent    = NULL;
@@ -4436,9 +4437,10 @@ static void parse_init_declarator_rest(declaration_t *declaration)
        parse_initializer_env_t env;
        env.type             = orig_type;
        env.must_be_constant = must_be_constant;
-       env.declaration      = declaration;
+       env.declaration      = current_init_decl = declaration;
 
        initializer_t *initializer = parse_initializer(&env);
+       current_init_decl = NULL;
 
        if (env.type != orig_type) {
                orig_type         = env.type;
@@ -5946,6 +5948,12 @@ static expression_t *parse_reference(void)
                                declaration->symbol, &declaration->source_position);
                }
        }
+       if (warning.init_self && declaration == current_init_decl) {
+               current_init_decl = NULL;
+               warningf(&source_position,
+                       "variable '%#T' is initialized by itself",
+                       declaration->type, declaration->symbol);
+       }
 
        return expression;
 }
diff --git a/parsetest/should_warn/init_self.c b/parsetest/should_warn/init_self.c
new file mode 100644 (file)
index 0000000..f195878
--- /dev/null
@@ -0,0 +1,6 @@
+/*$ -Winit-self $*/
+
+int main(int argc, char *argv[]) {
+       int i = i;
+       return 0;
+}
index a41b56a..c14f622 100644 (file)
--- a/warning.c
+++ b/warning.c
@@ -36,6 +36,7 @@ warning_t warning = {
        .format                        = true,
        .implicit_function_declaration = true,
        .implicit_int                  = true,
+       .init_self                     = true,
        .long_long                     = false,
        .main                          = true,
        .missing_declarations          = false,
@@ -88,6 +89,7 @@ void set_warning_opt(const char *const opt)
                SET(format);
                SET(implicit_function_declaration);
                SET(implicit_int);
+               SET(init_self);
                SET(main);
                SET(nonnull);
                SET(pointer_arith);
@@ -137,6 +139,7 @@ void set_warning_opt(const char *const opt)
        }
        OPT("implicit-function-declaration", implicit_function_declaration);
        OPT("implicit-int",                  implicit_int);
+       OPT("init-self",                     init_self);
        OPT("long-long",                     long_long);
        OPT("main",                          main);
        OPT("missing-declarations",          missing_declarations);
index 5149379..fba6f07 100644 (file)
--- a/warning.h
+++ b/warning.h
@@ -50,8 +50,8 @@ typedef struct warning_t {
        bool format:1;                        /**< Check printf-style format strings */
        bool implicit_function_declaration:1; /**< Warn whenever a function is used before being declared */
        bool implicit_int:1;                  /**< Warn when a declaration does not specify a type */
-#if 0 // TODO
        bool init_self:1;                     /**< Warn about uninitialized variables which are initialized with themselves. */
+#if 0 // TODO
        bool inline:1;                        /**< Warn if a function can not be inlined and it was declared as inline */
        bool int_to_pointer_cast:1;           /**< Warn if cast from integer to pointer of different size. */
 #endif