[PATCH] D95799: [analyzer] Symbolicate float values with integral casting

Denys Petrov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 1 10:07:36 PST 2021


ASDenysPetrov created this revision.
ASDenysPetrov added reviewers: NoQ, xazax.hun, baloghadamsoftware, steakhal, vsavchenko.
ASDenysPetrov added a project: clang.
Herald added subscribers: martong, Charusso, dkrupp, donat.nagy, Szelethus, mikhail.ramalho, a.sidorin, rnkovacs, szepet.
ASDenysPetrov requested review of this revision.
Herald added a subscriber: cfe-commits.

Create nonloc::SymbolVal with SymbolCast expression from //integral// type to //float// type.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95799

Files:
  clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
  clang/lib/StaticAnalyzer/Core/Store.cpp
  clang/test/Analysis/svalbuilder-float-cast.c


Index: clang/test/Analysis/svalbuilder-float-cast.c
===================================================================
--- clang/test/Analysis/svalbuilder-float-cast.c
+++ clang/test/Analysis/svalbuilder-float-cast.c
@@ -4,17 +4,23 @@
 
 void SymbolCast_of_float_type_aux(int *p) {
   *p += 0;
-  // FIXME: Ideally, all unknown values should be symbolicated.
-  clang_analyzer_denote(*p, "$x"); // expected-warning{{Not a symbol}}
+  clang_analyzer_denote(*p, "$x");
 
   *p += 1;
-  // This should NOT be (float)$x + 1. Symbol $x was never casted to float.
-  // FIXME: Ideally, this should be $x + 1.
-  clang_analyzer_express(*p); // expected-warning{{Not a symbol}}
+  // expected-warning at +3{{(float)$x + 1}}
+  // expected-warning at +2{{(double)$x + 1}}
+  // expected-warning at +1{{(long double)$x + 1}}
+  clang_analyzer_express(*p);
 }
 
 void SymbolCast_of_float_type() {
-  extern float x;
+  extern float x1;
+  extern double x2;
+  extern long double x3;
+
   void (*f)() = SymbolCast_of_float_type_aux;
-  f(&x);
+
+  f(&x1);
+  f(&x2);
+  f(&x3);
 }
Index: clang/lib/StaticAnalyzer/Core/Store.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Core/Store.cpp
+++ clang/lib/StaticAnalyzer/Core/Store.cpp
@@ -409,14 +409,8 @@
 
   // The dispatchCast() call below would convert the int into a float.
   // What we want, however, is a bit-by-bit reinterpretation of the int
-  // as a float, which usually yields nothing garbage. For now skip casts
-  // from ints to floats.
+  // as a float, which usually yields nothing garbage.
   // TODO: What other combinations of types are affected?
-  if (castTy->isFloatingType()) {
-    SymbolRef Sym = V.getAsSymbol();
-    if (Sym && !Sym->getType()->isFloatingType())
-      return UnknownVal();
-  }
 
   // When retrieving symbolic pointer and expecting a non-void pointer,
   // wrap them into element regions of the expected type if necessary.
Index: clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
+++ clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
@@ -501,8 +501,9 @@
   if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
     return evalCast(val, castTy, originalTy);
 
+  // Let evalCast handle non symbolic expressions.
   SymbolRef se = val.getAsSymbol();
-  if (!se) // Let evalCast handle non symbolic expressions.
+  if (!se || se->getType()->isFloatingType())
     return evalCast(val, castTy, originalTy);
 
   // Find the maximum value of the target type.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95799.320509.patch
Type: text/x-patch
Size: 2619 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210201/207fd5c2/attachment-0001.bin>


More information about the cfe-commits mailing list