[PATCH] D80171: [analyzer] LoopUnrolling: fix crash when a parameter is a loop counter

Abbas Sabra via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 19 17:06:44 PDT 2020


AbbasSabra updated this revision to Diff 265090.
AbbasSabra added a comment.

Fix code review


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D80171

Files:
  clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
  clang/test/Analysis/loop-unrolling.cpp


Index: clang/test/Analysis/loop-unrolling.cpp
===================================================================
--- clang/test/Analysis/loop-unrolling.cpp
+++ clang/test/Analysis/loop-unrolling.cpp
@@ -500,8 +500,14 @@
   }
 }
 
-void arg_as_loop_counter(int i) {
+void parm_by_value_as_loop_counter(int i) {
   for (i = 0; i < 10; ++i) {
-    (void)i;
+    clang_analyzer_numTimesReached(); // expected-warning {{10}}
+  }
+}
+
+void parm_by_ref_as_loop_counter(int &i) {
+  for (i = 0; i < 10; ++i) {
+    clang_analyzer_numTimesReached(); // expected-warning {{4}}
   }
 }
Index: clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
+++ clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
@@ -160,8 +160,13 @@
 }
 
 static bool isPossiblyEscaped(const VarDecl *VD, ExplodedNode *N) {
-  // Global variables and parameters assumed as escaped variables.
-  if (VD->hasGlobalStorage() || VD->getKind() == Decl::ParmVar)
+  // Global variables assumed as escaped variables.
+  if (VD->hasGlobalStorage())
+    return true;
+
+  const bool isParm = VD->getKind() == Decl::ParmVar;
+  // Reference parameters are assumed as escaped variables.
+  if (isParm && VD->getType()->isReferenceType())
     return true;
 
   while (!N->pred_empty()) {
@@ -193,6 +198,11 @@
 
     N = N->getFirstPred();
   }
+
+  // Parameter declaration will not be found.
+  if (isParm)
+    return false;
+
   llvm_unreachable("Reached root without finding the declaration of VD");
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80171.265090.patch
Type: text/x-patch
Size: 1575 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200520/201a2f72/attachment.bin>


More information about the cfe-commits mailing list