InfiniteLoopCheck.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //===--- InfiniteLoopCheck.cpp - clang-tidy -------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "InfiniteLoopCheck.h"
  9. #include "../utils/Aliasing.h"
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/ASTMatchers/ASTMatchFinder.h"
  12. #include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
  13. #include "clang/Analysis/CallGraph.h"
  14. #include "llvm/ADT/SCCIterator.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. using namespace clang::ast_matchers;
  17. using clang::tidy::utils::hasPtrOrReferenceInFunc;
  18. namespace clang {
  19. namespace ast_matchers {
  20. /// matches a Decl if it has a "no return" attribute of any kind
  21. AST_MATCHER(Decl, declHasNoReturnAttr) {
  22. return Node.hasAttr<NoReturnAttr>() || Node.hasAttr<CXX11NoReturnAttr>() ||
  23. Node.hasAttr<C11NoReturnAttr>();
  24. }
  25. /// matches a FunctionType if the type includes the GNU no return attribute
  26. AST_MATCHER(FunctionType, typeHasNoReturnAttr) {
  27. return Node.getNoReturnAttr();
  28. }
  29. } // namespace ast_matchers
  30. namespace tidy::bugprone {
  31. static internal::Matcher<Stmt>
  32. loopEndingStmt(internal::Matcher<Stmt> Internal) {
  33. internal::Matcher<QualType> isNoReturnFunType =
  34. ignoringParens(functionType(typeHasNoReturnAttr()));
  35. internal::Matcher<Decl> isNoReturnDecl =
  36. anyOf(declHasNoReturnAttr(), functionDecl(hasType(isNoReturnFunType)),
  37. varDecl(hasType(blockPointerType(pointee(isNoReturnFunType)))));
  38. return stmt(anyOf(
  39. mapAnyOf(breakStmt, returnStmt, gotoStmt, cxxThrowExpr).with(Internal),
  40. callExpr(Internal,
  41. callee(mapAnyOf(functionDecl, /* block callee */ varDecl)
  42. .with(isNoReturnDecl))),
  43. objcMessageExpr(Internal, callee(isNoReturnDecl))));
  44. }
  45. /// Return whether `Var` was changed in `LoopStmt`.
  46. static bool isChanged(const Stmt *LoopStmt, const VarDecl *Var,
  47. ASTContext *Context) {
  48. if (const auto *ForLoop = dyn_cast<ForStmt>(LoopStmt))
  49. return (ForLoop->getInc() &&
  50. ExprMutationAnalyzer(*ForLoop->getInc(), *Context)
  51. .isMutated(Var)) ||
  52. (ForLoop->getBody() &&
  53. ExprMutationAnalyzer(*ForLoop->getBody(), *Context)
  54. .isMutated(Var)) ||
  55. (ForLoop->getCond() &&
  56. ExprMutationAnalyzer(*ForLoop->getCond(), *Context).isMutated(Var));
  57. return ExprMutationAnalyzer(*LoopStmt, *Context).isMutated(Var);
  58. }
  59. /// Return whether `Cond` is a variable that is possibly changed in `LoopStmt`.
  60. static bool isVarThatIsPossiblyChanged(const Decl *Func, const Stmt *LoopStmt,
  61. const Stmt *Cond, ASTContext *Context) {
  62. if (const auto *DRE = dyn_cast<DeclRefExpr>(Cond)) {
  63. if (const auto *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
  64. if (!Var->isLocalVarDeclOrParm())
  65. return true;
  66. if (Var->getType().isVolatileQualified())
  67. return true;
  68. if (!Var->getType().getTypePtr()->isIntegerType())
  69. return true;
  70. return hasPtrOrReferenceInFunc(Func, Var) ||
  71. isChanged(LoopStmt, Var, Context);
  72. // FIXME: Track references.
  73. }
  74. } else if (isa<MemberExpr, CallExpr,
  75. ObjCIvarRefExpr, ObjCPropertyRefExpr, ObjCMessageExpr>(Cond)) {
  76. // FIXME: Handle MemberExpr.
  77. return true;
  78. } else if (const auto *CE = dyn_cast<CastExpr>(Cond)) {
  79. QualType T = CE->getType();
  80. while (true) {
  81. if (T.isVolatileQualified())
  82. return true;
  83. if (!T->isAnyPointerType() && !T->isReferenceType())
  84. break;
  85. T = T->getPointeeType();
  86. }
  87. }
  88. return false;
  89. }
  90. /// Return whether at least one variable of `Cond` changed in `LoopStmt`.
  91. static bool isAtLeastOneCondVarChanged(const Decl *Func, const Stmt *LoopStmt,
  92. const Stmt *Cond, ASTContext *Context) {
  93. if (isVarThatIsPossiblyChanged(Func, LoopStmt, Cond, Context))
  94. return true;
  95. for (const Stmt *Child : Cond->children()) {
  96. if (!Child)
  97. continue;
  98. if (isAtLeastOneCondVarChanged(Func, LoopStmt, Child, Context))
  99. return true;
  100. }
  101. return false;
  102. }
  103. /// Return the variable names in `Cond`.
  104. static std::string getCondVarNames(const Stmt *Cond) {
  105. if (const auto *DRE = dyn_cast<DeclRefExpr>(Cond)) {
  106. if (const auto *Var = dyn_cast<VarDecl>(DRE->getDecl()))
  107. return std::string(Var->getName());
  108. }
  109. std::string Result;
  110. for (const Stmt *Child : Cond->children()) {
  111. if (!Child)
  112. continue;
  113. std::string NewNames = getCondVarNames(Child);
  114. if (!Result.empty() && !NewNames.empty())
  115. Result += ", ";
  116. Result += NewNames;
  117. }
  118. return Result;
  119. }
  120. static bool isKnownToHaveValue(const Expr &Cond, const ASTContext &Ctx,
  121. bool ExpectedValue) {
  122. if (Cond.isValueDependent()) {
  123. if (const auto *BinOp = dyn_cast<BinaryOperator>(&Cond)) {
  124. // Conjunctions (disjunctions) can still be handled if at least one
  125. // conjunct (disjunct) is known to be false (true).
  126. if (!ExpectedValue && BinOp->getOpcode() == BO_LAnd)
  127. return isKnownToHaveValue(*BinOp->getLHS(), Ctx, false) ||
  128. isKnownToHaveValue(*BinOp->getRHS(), Ctx, false);
  129. if (ExpectedValue && BinOp->getOpcode() == BO_LOr)
  130. return isKnownToHaveValue(*BinOp->getLHS(), Ctx, true) ||
  131. isKnownToHaveValue(*BinOp->getRHS(), Ctx, true);
  132. if (BinOp->getOpcode() == BO_Comma)
  133. return isKnownToHaveValue(*BinOp->getRHS(), Ctx, ExpectedValue);
  134. } else if (const auto *UnOp = dyn_cast<UnaryOperator>(&Cond)) {
  135. if (UnOp->getOpcode() == UO_LNot)
  136. return isKnownToHaveValue(*UnOp->getSubExpr(), Ctx, !ExpectedValue);
  137. } else if (const auto *Paren = dyn_cast<ParenExpr>(&Cond))
  138. return isKnownToHaveValue(*Paren->getSubExpr(), Ctx, ExpectedValue);
  139. else if (const auto *ImplCast = dyn_cast<ImplicitCastExpr>(&Cond))
  140. return isKnownToHaveValue(*ImplCast->getSubExpr(), Ctx, ExpectedValue);
  141. return false;
  142. }
  143. bool Result = false;
  144. if (Cond.EvaluateAsBooleanCondition(Result, Ctx))
  145. return Result == ExpectedValue;
  146. return false;
  147. }
  148. /// populates the set `Callees` with all function (and objc method) declarations
  149. /// called in `StmtNode` if all visited call sites have resolved call targets.
  150. ///
  151. /// \return true iff all `CallExprs` visited have callees; false otherwise
  152. /// indicating there is an unresolved indirect call.
  153. static bool populateCallees(const Stmt *StmtNode,
  154. llvm::SmallSet<const Decl *, 16> &Callees) {
  155. if (const auto *Call = dyn_cast<CallExpr>(StmtNode)) {
  156. const Decl *Callee = Call->getDirectCallee();
  157. if (!Callee)
  158. return false; // unresolved call
  159. Callees.insert(Callee->getCanonicalDecl());
  160. }
  161. if (const auto *Call = dyn_cast<ObjCMessageExpr>(StmtNode)) {
  162. const Decl *Callee = Call->getMethodDecl();
  163. if (!Callee)
  164. return false; // unresolved call
  165. Callees.insert(Callee->getCanonicalDecl());
  166. }
  167. for (const Stmt *Child : StmtNode->children())
  168. if (Child && !populateCallees(Child, Callees))
  169. return false;
  170. return true;
  171. }
  172. /// returns true iff `SCC` contains `Func` and its' function set overlaps with
  173. /// `Callees`
  174. static bool overlap(ArrayRef<CallGraphNode *> SCC,
  175. const llvm::SmallSet<const Decl *, 16> &Callees,
  176. const Decl *Func) {
  177. bool ContainsFunc = false, Overlap = false;
  178. for (const CallGraphNode *GNode : SCC) {
  179. const Decl *CanDecl = GNode->getDecl()->getCanonicalDecl();
  180. ContainsFunc = ContainsFunc || (CanDecl == Func);
  181. Overlap = Overlap || Callees.contains(CanDecl);
  182. if (ContainsFunc && Overlap)
  183. return true;
  184. }
  185. return false;
  186. }
  187. /// returns true iff `Cond` involves at least one static local variable.
  188. static bool hasStaticLocalVariable(const Stmt *Cond) {
  189. if (const auto *DRE = dyn_cast<DeclRefExpr>(Cond))
  190. if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
  191. if (VD->isStaticLocal())
  192. return true;
  193. for (const Stmt *Child : Cond->children())
  194. if (Child && hasStaticLocalVariable(Child))
  195. return true;
  196. return false;
  197. }
  198. /// Tests if the loop condition `Cond` involves static local variables and
  199. /// the enclosing function `Func` is recursive.
  200. ///
  201. /// \code
  202. /// void f() {
  203. /// static int i = 10;
  204. /// i--;
  205. /// while (i >= 0) f();
  206. /// }
  207. /// \endcode
  208. /// The example above is NOT an infinite loop.
  209. static bool hasRecursionOverStaticLoopCondVariables(const Expr *Cond,
  210. const Stmt *LoopStmt,
  211. const Decl *Func,
  212. const ASTContext *Ctx) {
  213. if (!hasStaticLocalVariable(Cond))
  214. return false;
  215. llvm::SmallSet<const Decl *, 16> CalleesInLoop;
  216. if (!populateCallees(LoopStmt, CalleesInLoop)) {
  217. // If there are unresolved indirect calls, we assume there could
  218. // be recursion so to avoid false alarm.
  219. return true;
  220. }
  221. if (CalleesInLoop.empty())
  222. return false;
  223. TranslationUnitDecl *TUDecl = Ctx->getTranslationUnitDecl();
  224. CallGraph CG;
  225. CG.addToCallGraph(TUDecl);
  226. // For each `SCC` containing `Func`, if functions in the `SCC`
  227. // overlap with `CalleesInLoop`, there is a recursive call in `LoopStmt`.
  228. for (llvm::scc_iterator<CallGraph *> SCCI = llvm::scc_begin(&CG),
  229. SCCE = llvm::scc_end(&CG);
  230. SCCI != SCCE; ++SCCI) {
  231. if (!SCCI.hasCycle()) // We only care about cycles, not standalone nodes.
  232. continue;
  233. // `SCC`s are mutually disjoint, so there will be no redundancy in
  234. // comparing `SCC` with the callee set one by one.
  235. if (overlap(*SCCI, CalleesInLoop, Func->getCanonicalDecl()))
  236. return true;
  237. }
  238. return false;
  239. }
  240. void InfiniteLoopCheck::registerMatchers(MatchFinder *Finder) {
  241. const auto LoopCondition = allOf(
  242. hasCondition(
  243. expr(forCallable(decl().bind("func"))).bind("condition")),
  244. unless(hasBody(hasDescendant(
  245. loopEndingStmt(forCallable(equalsBoundNode("func")))))));
  246. Finder->addMatcher(mapAnyOf(whileStmt, doStmt, forStmt)
  247. .with(LoopCondition)
  248. .bind("loop-stmt"),
  249. this);
  250. }
  251. void InfiniteLoopCheck::check(const MatchFinder::MatchResult &Result) {
  252. const auto *Cond = Result.Nodes.getNodeAs<Expr>("condition");
  253. const auto *LoopStmt = Result.Nodes.getNodeAs<Stmt>("loop-stmt");
  254. const auto *Func = Result.Nodes.getNodeAs<Decl>("func");
  255. if (isKnownToHaveValue(*Cond, *Result.Context, false))
  256. return;
  257. bool ShouldHaveConditionVariables = true;
  258. if (const auto *While = dyn_cast<WhileStmt>(LoopStmt)) {
  259. if (const VarDecl *LoopVarDecl = While->getConditionVariable()) {
  260. if (const Expr *Init = LoopVarDecl->getInit()) {
  261. ShouldHaveConditionVariables = false;
  262. Cond = Init;
  263. }
  264. }
  265. }
  266. if (ExprMutationAnalyzer::isUnevaluated(LoopStmt, *LoopStmt, *Result.Context))
  267. return;
  268. if (isAtLeastOneCondVarChanged(Func, LoopStmt, Cond, Result.Context))
  269. return;
  270. if (hasRecursionOverStaticLoopCondVariables(Cond, LoopStmt, Func,
  271. Result.Context))
  272. return;
  273. std::string CondVarNames = getCondVarNames(Cond);
  274. if (ShouldHaveConditionVariables && CondVarNames.empty())
  275. return;
  276. if (CondVarNames.empty()) {
  277. diag(LoopStmt->getBeginLoc(),
  278. "this loop is infinite; it does not check any variables in the"
  279. " condition");
  280. } else {
  281. diag(LoopStmt->getBeginLoc(),
  282. "this loop is infinite; none of its condition variables (%0)"
  283. " are updated in the loop body")
  284. << CondVarNames;
  285. }
  286. }
  287. } // namespace tidy::bugprone
  288. } // namespace clang