r349798 - Revert "[analyzer] pr38668: Do not attempt to cast loaded values..."

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 20 11:36:06 PST 2018


Author: dergachev
Date: Thu Dec 20 11:36:06 2018
New Revision: 349798

URL: http://llvm.org/viewvc/llvm-project?rev=349798&view=rev
Log:
Revert "[analyzer] pr38668: Do not attempt to cast loaded values..."

This reverts commit r349701.

The patch was incorrect. The whole point of CastRetrievedVal()
is to handle the case in which the type from which the cast is made
(i.e., the "type" of value `V`) has nothing to do with the type of
the region it was loaded from (i.e., `R->getValueType()`).

Differential Revision: https://reviews.llvm.org/D55875

rdar://problem/45062567

Modified:
    cfe/trunk/lib/StaticAnalyzer/Core/Store.cpp
    cfe/trunk/test/Analysis/casts.c
    cfe/trunk/test/Analysis/pointer-to-member.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Core/Store.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/Store.cpp?rev=349798&r1=349797&r2=349798&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/Store.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/Store.cpp Thu Dec 20 11:36:06 2018
@@ -394,28 +394,14 @@ SVal StoreManager::attemptDownCast(SVal
   return UnknownVal();
 }
 
-static bool isScalarEnoughToAttemptACast(QualType T) {
-  return T->isIntegralOrEnumerationType() || T->isAnyPointerType() ||
-         T->isReferenceType();
-}
-
 /// CastRetrievedVal - Used by subclasses of StoreManager to implement
 ///  implicit casts that arise from loads from regions that are reinterpreted
 ///  as another region.
 SVal StoreManager::CastRetrievedVal(SVal V, const TypedValueRegion *R,
-                                    QualType CastTy) {
-  if (CastTy.isNull() || V.isUnknownOrUndef())
+                                    QualType castTy) {
+  if (castTy.isNull() || V.isUnknownOrUndef())
     return V;
 
-  QualType OrigTy = R->getValueType();
-
-  if (!isScalarEnoughToAttemptACast(OrigTy) ||
-      !isScalarEnoughToAttemptACast(CastTy)) {
-    if (OrigTy.getUnqualifiedType() == CastTy.getUnqualifiedType())
-      return V;
-    return UnknownVal();
-  }
-
   // When retrieving symbolic pointer and expecting a non-void pointer,
   // wrap them into element regions of the expected type if necessary.
   // SValBuilder::dispatchCast() doesn't do that, but it is necessary to
@@ -424,13 +410,13 @@ SVal StoreManager::CastRetrievedVal(SVal
   // We might need to do that for non-void pointers as well.
   // FIXME: We really need a single good function to perform casts for us
   // correctly every time we need it.
-  if (CastTy->isPointerType() && !CastTy->isVoidPointerType())
+  if (castTy->isPointerType() && !castTy->isVoidPointerType())
     if (const auto *SR = dyn_cast_or_null<SymbolicRegion>(V.getAsRegion()))
       if (SR->getSymbol()->getType().getCanonicalType() !=
-          CastTy.getCanonicalType())
-        return loc::MemRegionVal(castRegion(SR, CastTy));
+          castTy.getCanonicalType())
+        return loc::MemRegionVal(castRegion(SR, castTy));
 
-  return svalBuilder.dispatchCast(V, CastTy);
+  return svalBuilder.dispatchCast(V, castTy);
 }
 
 SVal StoreManager::getLValueFieldOrIvar(const Decl *D, SVal Base) {

Modified: cfe/trunk/test/Analysis/casts.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/casts.c?rev=349798&r1=349797&r2=349798&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/casts.c (original)
+++ cfe/trunk/test/Analysis/casts.c Thu Dec 20 11:36:06 2018
@@ -213,14 +213,3 @@ void no_crash_on_symsym_cast_to_long() {
 }
 
 #endif
-
-char no_crash_SymbolCast_of_float_type_aux(int *p) {
-  *p += 1;
-  return *p;
-}
-
-void no_crash_SymbolCast_of_float_type() {
-  extern float x;
-  char (*f)() = no_crash_SymbolCast_of_float_type_aux;
-  f(&x);
-}

Modified: cfe/trunk/test/Analysis/pointer-to-member.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/pointer-to-member.cpp?rev=349798&r1=349797&r2=349798&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/pointer-to-member.cpp (original)
+++ cfe/trunk/test/Analysis/pointer-to-member.cpp Thu Dec 20 11:36:06 2018
@@ -253,10 +253,11 @@ void test() {
   clang_analyzer_eval(&A::y); // expected-warning{{TRUE}}
   clang_analyzer_eval(&A::z); // expected-warning{{TRUE}}
 
+  // FIXME: These should be true.
   int A::*l = &A::x, A::*m = &A::y, A::*n = &A::z;
-  clang_analyzer_eval(l); // expected-warning{{TRUE}}
-  clang_analyzer_eval(m); // expected-warning{{TRUE}}
-  clang_analyzer_eval(n); // expected-warning{{TRUE}}
+  clang_analyzer_eval(l); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval(m); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval(n); // expected-warning{{UNKNOWN}}
 
   // FIXME: These should be true as well.
   A a;




More information about the cfe-commits mailing list