When loop counter is a function parameter "isPossiblyEscaped" will not find
the variable declaration which lead to hitting "llvm_unreachable".
Parameters of reference type should be escaped like global variables;
otherwise treat them as unescaped.
Patch by Abbas Sabra!
Differential Revision: https://reviews.llvm.org/D80171
if (VD->hasGlobalStorage())
return true;
+ const bool isParm = isa<ParmVarDecl>(VD);
+ // Reference parameters are assumed as escaped variables.
+ if (isParm && VD->getType()->isReferenceType())
+ return true;
+
while (!N->pred_empty()) {
// FIXME: getStmtForDiagnostics() does nasty things in order to provide
// a valid statement for body farms, do we need this behavior here?
N = N->getFirstPred();
}
+
+ // Parameter declaration will not be found.
+ if (isParm)
+ return false;
+
llvm_unreachable("Reached root without finding the declaration of VD");
}
clang_analyzer_numTimesReached(); // expected-warning {{6}}
}
}
+
+void parm_by_value_as_loop_counter(int i) {
+ for (i = 0; i < 10; ++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}}
+ }
+}