[PATCH] D110625: [analyzer] canonicalize special case of structure/pointer deref

Vince Bridgers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 28 10:38:35 PDT 2021


vabridgers updated this revision to Diff 375640.
vabridgers added a comment.

update test case


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110625/new/

https://reviews.llvm.org/D110625

Files:
  clang/lib/StaticAnalyzer/Core/Store.cpp
  clang/test/Analysis/ptr-arith.c


Index: clang/test/Analysis/ptr-arith.c
===================================================================
--- clang/test/Analysis/ptr-arith.c
+++ clang/test/Analysis/ptr-arith.c
@@ -330,3 +330,18 @@
   simd_float2 x = {0, 1};
   return x[1]; // no-warning
 }
+
+struct s {
+  int v;
+  char y;
+};
+
+void clang_analyzer_dump(int);
+
+// These three expressions should produce the same sym vals.
+void struct_pointer_canon(struct s *ps) {
+  struct s ss = *ps;
+  clang_analyzer_dump((*ps).v); // expected-warning{{reg_$3<int SymRegion{reg_$0<struct s * ps>}.v>}}
+  clang_analyzer_dump(ps[0].v); // expected-warning{{reg_$3<int SymRegion{reg_$0<struct s * ps>}.v>}}
+  clang_analyzer_dump(ps->v);   // expected-warning{{reg_$3<int SymRegion{reg_$0<struct s * ps>}.v>}}
+}
Index: clang/lib/StaticAnalyzer/Core/Store.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Core/Store.cpp
+++ clang/lib/StaticAnalyzer/Core/Store.cpp
@@ -442,6 +442,15 @@
 
 SVal StoreManager::getLValueElement(QualType elementType, NonLoc Offset,
                                     SVal Base) {
+
+  // Special case, if index is 0, return the same type as if
+  // this was not an array dereference.
+  if (Offset.isZeroConstant()) {
+    QualType BT = Base.getType(this->Ctx);
+    if (!BT.isNull() && BT->getPointeeType() == elementType)
+      return Base;
+  }
+
   // If the base is an unknown or undefined value, just return it back.
   // FIXME: For absolute pointer addresses, we just return that value back as
   //  well, although in reality we should return the offset added to that


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110625.375640.patch
Type: text/x-patch
Size: 1623 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210928/11ce1540/attachment-0001.bin>


More information about the cfe-commits mailing list