[cfe-commits] r168843 - in /cfe/trunk: lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp test/Analysis/misc-ps-region-store.cpp

Jordan Rose jordan_rose at apple.com
Wed Nov 28 16:54:24 PST 2012


On Nov 28, 2012, at 16:50 , Ted Kremenek <kremenek at apple.com> wrote:

> Author: kremenek
> Date: Wed Nov 28 18:50:20 2012
> New Revision: 168843
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=168843&view=rev
> Log:
> Correctly handle IntegralToBool casts in C++ in the static analyzer.  Fixes <rdar://problem/12759044>.
> 
> Modified:
>    cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
>    cfe/trunk/test/Analysis/misc-ps-region-store.cpp
> 
> Modified: cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp?rev=168843&r1=168842&r2=168843&view=diff
> ==============================================================================
> --- cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp (original)
> +++ cfe/trunk/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp Wed Nov 28 18:50:20 2012
> @@ -101,6 +101,12 @@
>   if (!isa<nonloc::ConcreteInt>(val))
>     return UnknownVal();
> 
> +  // Handle casts to a boolean type.
> +  if (castTy->isBooleanType()) {
> +    bool b = cast<nonloc::ConcreteInt>(val).getValue().getBoolValue();
> +    return makeTruthVal(b, castTy);
> +  }

We can do better about nonloc::LocAsInteger and evalCastFromLoc as well. We would be able to do better about symbols if we had a state around for casts, but we don't.


>   // Only handle casts from integers to integers - if val is an integer constant
>   // being cast to a non integer type, produce unknown.
>   if (!isLocType && !castTy->isIntegerType())
> @@ -735,7 +741,7 @@
>         NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
>         if (!LeftIndex)
>           return UnknownVal();
> -        LeftIndexVal = evalCastFromNonLoc(*LeftIndex, resultTy);
> +        LeftIndexVal = evalCastFromNonLoc(*LeftIndex, ArrayIndexTy);
>         LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
>         if (!LeftIndex)
>           return UnknownVal();
> @@ -745,7 +751,7 @@
>         NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
>         if (!RightIndex)
>           return UnknownVal();
> -        RightIndexVal = evalCastFromNonLoc(*RightIndex, resultTy);
> +        RightIndexVal = evalCastFromNonLoc(*RightIndex, ArrayIndexTy);
>         RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
>         if (!RightIndex)
>           return UnknownVal();
> 
> Modified: cfe/trunk/test/Analysis/misc-ps-region-store.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/misc-ps-region-store.cpp?rev=168843&r1=168842&r2=168843&view=diff
> ==============================================================================
> --- cfe/trunk/test/Analysis/misc-ps-region-store.cpp (original)
> +++ cfe/trunk/test/Analysis/misc-ps-region-store.cpp Wed Nov 28 18:50:20 2012
> @@ -694,3 +694,14 @@
>   static const Rdar12755044_foo Rdar12755044_foo_list[] = { { { } } };
>   return Rdar12755044_foo_list; // no-warning
> }
> +
> +// Test the correct handling of integer to bool conversions.  Previously
> +// this resulted in a false positive because integers were being truncated
> +// and not tested for non-zero.
> +void rdar12759044() {
> +  int flag = 512;
> +  if (!(flag & 512)) {
> +   int *p = 0;
> +   *p = 0xDEADBEEF; // no-warning
> +  }
> +}
> 
> 
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121128/164ff7c7/attachment.html>


More information about the cfe-commits mailing list