[cfe-commits] r64149 - in /cfe/trunk: lib/Analysis/CheckDeadStores.cpp test/Analysis/dead-stores.c

Daniel Dunbar daniel at zuster.org
Mon Feb 9 23:38:23 PST 2009


I think it might be nice to explain "why". I don't immediately get why
this isn't worth flagging.

 - Daniel

On Mon, Feb 9, 2009 at 10:01 AM, Ted Kremenek <kremenek at apple.com> wrote:
> Author: kremenek
> Date: Mon Feb  9 12:01:00 2009
> New Revision: 64149
>
> URL: http://llvm.org/viewvc/llvm-project?rev=64149&view=rev
> Log:
> Fix PR 2514: Do not flag dead initializations for variables initialized to a constant global variable.
>
> Modified:
>    cfe/trunk/lib/Analysis/CheckDeadStores.cpp
>    cfe/trunk/test/Analysis/dead-stores.c
>
> Modified: cfe/trunk/lib/Analysis/CheckDeadStores.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CheckDeadStores.cpp?rev=64149&r1=64148&r2=64149&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Analysis/CheckDeadStores.cpp (original)
> +++ cfe/trunk/lib/Analysis/CheckDeadStores.cpp Mon Feb  9 12:01:00 2009
> @@ -195,8 +195,21 @@
>               // If x is EVER assigned a new value later, don't issue
>               // a warning.  This is because such initialization can be
>               // due to defensive programming.
> -              if (!E->isConstantInitializer(Ctx))
> -                Report(V, DeadInit, V->getLocation(), E->getSourceRange());
> +              if (E->isConstantInitializer(Ctx))
> +                return;
> +
> +              // Special case: check for initializations from constant
> +              //  variables.
> +              //
> +              //  e.g. extern const int MyConstant;
> +              //       int x = MyConstant;
> +              //
> +              if (DeclRefExpr *DRE=dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
> +                if (VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
> +                  if (VD->hasGlobalStorage() &&
> +                      VD->getType().isConstQualified()) return;
> +
> +              Report(V, DeadInit, V->getLocation(), E->getSourceRange());
>             }
>           }
>       }
>
> Modified: cfe/trunk/test/Analysis/dead-stores.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/dead-stores.c?rev=64149&r1=64148&r2=64149&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/Analysis/dead-stores.c (original)
> +++ cfe/trunk/test/Analysis/dead-stores.c Mon Feb  9 12:01:00 2009
> @@ -147,3 +147,19 @@
>
>    return (x = 10); // expected-warning{{Although the value stored to 'x' is used in the enclosing expression, the value is never actually read from 'x'}}
>  }
> +
> +// PR 3514: false positive `dead initialization` warning for init to global
> +//  http://llvm.org/bugs/show_bug.cgi?id=3514
> +extern const int MyConstant;
> +int f19(void) {
> +  int x = MyConstant;  // no-warning
> +  x = 1;
> +  return x;
> +}
> +
> +int f19b(void) { // FIXME: Should this case be considered the same as f19?
> +  const int MyConstant = 0;
> +  int x = MyConstant; // expected-warning{{never read}}
> +  x = 1;
> +  return x;
> +}
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>



More information about the cfe-commits mailing list