10#include "../utils/Aliasing.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
14#include "clang/Analysis/CallGraph.h"
15#include "llvm/ADT/SCCIterator.h"
18using clang::ast_matchers::internal::Matcher;
22namespace tidy::bugprone {
27 return Node.hasAttr<NoReturnAttr>() ||
Node.hasAttr<CXX11NoReturnAttr>() ||
28 Node.hasAttr<C11NoReturnAttr>();
33 return Node.getNoReturnAttr();
38 Matcher<QualType> IsNoReturnFunType =
39 ignoringParens(functionType(typeHasNoReturnAttr()));
40 Matcher<Decl> IsNoReturnDecl =
41 anyOf(declHasNoReturnAttr(), functionDecl(hasType(IsNoReturnFunType)),
42 varDecl(hasType(blockPointerType(pointee(IsNoReturnFunType)))));
45 mapAnyOf(breakStmt, returnStmt, gotoStmt, cxxThrowExpr).with(Internal),
47 callee(mapAnyOf(functionDecl, varDecl)
48 .with(IsNoReturnDecl))),
49 objcMessageExpr(Internal, callee(IsNoReturnDecl))));
53static bool isChanged(
const Stmt *LoopStmt,
const VarDecl *Var,
54 ASTContext *Context) {
55 if (
const auto *ForLoop = dyn_cast<ForStmt>(LoopStmt))
56 return (ForLoop->getInc() &&
57 ExprMutationAnalyzer(*ForLoop->getInc(), *Context)
59 (ForLoop->getBody() &&
60 ExprMutationAnalyzer(*ForLoop->getBody(), *Context)
62 (ForLoop->getCond() &&
63 ExprMutationAnalyzer(*ForLoop->getCond(), *Context).isMutated(Var));
65 return ExprMutationAnalyzer(*LoopStmt, *Context).isMutated(Var);
70 const Stmt *Cond, ASTContext *Context) {
71 if (
const auto *DRE = dyn_cast<DeclRefExpr>(Cond)) {
72 if (
const auto *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
73 if (!Var->isLocalVarDeclOrParm())
76 if (Var->getType().isVolatileQualified())
79 if (!Var->getType().getTypePtr()->isIntegerType())
82 return hasPtrOrReferenceInFunc(Func, Var) ||
86 }
else if (isa<MemberExpr, CallExpr,
87 ObjCIvarRefExpr, ObjCPropertyRefExpr, ObjCMessageExpr>(Cond)) {
90 }
else if (
const auto *
CE = dyn_cast<CastExpr>(Cond)) {
91 QualType T =
CE->getType();
93 if (T.isVolatileQualified())
96 if (!T->isAnyPointerType() && !T->isReferenceType())
99 T = T->getPointeeType();
108 const Stmt *Cond, ASTContext *Context) {
112 for (
const Stmt *Child : Cond->children()) {
124 if (
const auto *DRE = dyn_cast<DeclRefExpr>(Cond)) {
125 if (
const auto *Var = dyn_cast<VarDecl>(DRE->getDecl()))
126 return std::string(Var->getName());
130 for (
const Stmt *Child : Cond->children()) {
135 if (!Result.empty() && !NewNames.empty())
143 bool ExpectedValue) {
144 if (Cond.isValueDependent()) {
145 if (
const auto *BinOp = dyn_cast<BinaryOperator>(&Cond)) {
148 if (!ExpectedValue && BinOp->getOpcode() == BO_LAnd)
151 if (ExpectedValue && BinOp->getOpcode() == BO_LOr)
154 if (BinOp->getOpcode() == BO_Comma)
156 }
else if (
const auto *UnOp = dyn_cast<UnaryOperator>(&Cond)) {
157 if (UnOp->getOpcode() == UO_LNot)
159 }
else if (
const auto *Paren = dyn_cast<ParenExpr>(&Cond))
161 else if (
const auto *ImplCast = dyn_cast<ImplicitCastExpr>(&Cond))
166 if (Cond.EvaluateAsBooleanCondition(Result, Ctx))
167 return Result == ExpectedValue;
177 llvm::SmallSet<const Decl *, 16> &Callees) {
178 if (
const auto *Call = dyn_cast<CallExpr>(StmtNode)) {
179 const Decl *Callee = Call->getDirectCallee();
183 Callees.insert(Callee->getCanonicalDecl());
185 if (
const auto *Call = dyn_cast<ObjCMessageExpr>(StmtNode)) {
186 const Decl *Callee = Call->getMethodDecl();
190 Callees.insert(Callee->getCanonicalDecl());
192 for (
const Stmt *Child : StmtNode->children())
200static bool overlap(ArrayRef<CallGraphNode *> SCC,
201 const llvm::SmallSet<const Decl *, 16> &Callees,
203 bool ContainsFunc =
false, Overlap =
false;
205 for (
const CallGraphNode *GNode : SCC) {
206 const Decl *CanDecl = GNode->getDecl()->getCanonicalDecl();
208 ContainsFunc = ContainsFunc || (CanDecl == Func);
209 Overlap = Overlap || Callees.contains(CanDecl);
210 if (ContainsFunc && Overlap)
218 if (
const auto *DRE = dyn_cast<DeclRefExpr>(Cond))
219 if (
const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
220 if (VD->isStaticLocal())
222 for (
const Stmt *Child : Cond->children())
240 const Stmt *LoopStmt,
242 const ASTContext *Ctx) {
246 llvm::SmallSet<const Decl *, 16> CalleesInLoop;
253 if (CalleesInLoop.empty())
256 TranslationUnitDecl *TUDecl = Ctx->getTranslationUnitDecl();
259 CG.addToCallGraph(TUDecl);
262 for (llvm::scc_iterator<CallGraph *> SCCI = llvm::scc_begin(&CG),
263 SCCE = llvm::scc_end(&CG);
264 SCCI != SCCE; ++SCCI) {
265 if (!SCCI.hasCycle())
269 if (
overlap(*SCCI, CalleesInLoop, Func->getCanonicalDecl()))
276 const auto LoopCondition = allOf(
278 expr(forCallable(decl().bind(
"func"))).bind(
"condition")),
279 unless(hasBody(hasDescendant(
282 Finder->addMatcher(mapAnyOf(whileStmt, doStmt, forStmt)
289 const auto *Cond = Result.Nodes.getNodeAs<Expr>(
"condition");
290 const auto *LoopStmt = Result.Nodes.getNodeAs<Stmt>(
"loop-stmt");
291 const auto *Func = Result.Nodes.getNodeAs<
Decl>(
"func");
296 bool ShouldHaveConditionVariables =
true;
297 if (
const auto *While = dyn_cast<WhileStmt>(LoopStmt)) {
298 if (
const VarDecl *LoopVarDecl = While->getConditionVariable()) {
299 if (
const Expr *Init = LoopVarDecl->getInit()) {
300 ShouldHaveConditionVariables =
false;
306 if (ExprMutationAnalyzer::isUnevaluated(LoopStmt, *Result.Context))
316 if (ShouldHaveConditionVariables && CondVarNames.empty())
319 if (CondVarNames.empty()) {
320 diag(LoopStmt->getBeginLoc(),
321 "this loop is infinite; it does not check any variables in the"
324 diag(LoopStmt->getBeginLoc(),
325 "this loop is infinite; none of its condition variables (%0)"
326 " are updated in the loop body")
const FunctionDecl * Decl
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static bool overlap(ArrayRef< CallGraphNode * > SCC, const llvm::SmallSet< const Decl *, 16 > &Callees, const Decl *Func)
returns true iff SCC contains Func and its' function set overlaps with Callees
static bool isChanged(const Stmt *LoopStmt, const VarDecl *Var, ASTContext *Context)
Return whether Var was changed in LoopStmt.
static Matcher< Stmt > loopEndingStmt(Matcher< Stmt > Internal)
static bool isKnownToHaveValue(const Expr &Cond, const ASTContext &Ctx, bool ExpectedValue)
static bool hasStaticLocalVariable(const Stmt *Cond)
returns true iff Cond involves at least one static local variable.
static bool isVarThatIsPossiblyChanged(const Decl *Func, const Stmt *LoopStmt, const Stmt *Cond, ASTContext *Context)
Return whether Cond is a variable that is possibly changed in LoopStmt.
static bool hasRecursionOverStaticLoopCondVariables(const Expr *Cond, const Stmt *LoopStmt, const Decl *Func, const ASTContext *Ctx)
Tests if the loop condition Cond involves static local variables and the enclosing function Func is r...
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)
static bool populateCallees(const Stmt *StmtNode, llvm::SmallSet< const Decl *, 16 > &Callees)
populates the set Callees with all function (and objc method) declarations called in StmtNode if all ...
static std::string getCondVarNames(const Stmt *Cond)
Return the variable names in Cond.
static bool isAtLeastOneCondVarChanged(const Decl *Func, const Stmt *LoopStmt, const Stmt *Cond, ASTContext *Context)
Return whether at least one variable of Cond changed in LoopStmt.
bool hasPtrOrReferenceInFunc(const Decl *Func, const VarDecl *Var)
Returns whether Var has a pointer or reference in Func.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//