Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/bugprone/StandaloneEmptyCheck.cpp

raw
1 //===--- StandaloneEmptyCheck.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
9 #include "StandaloneEmptyCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/AST/DeclBase.h"
13 #include "clang/AST/DeclCXX.h"
14 #include "clang/AST/Expr.h"
15 #include "clang/AST/ExprCXX.h"
16 #include "clang/AST/Stmt.h"
17 #include "clang/AST/Type.h"
18 #include "clang/ASTMatchers/ASTMatchFinder.h"
19 #include "clang/ASTMatchers/ASTMatchers.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Lex/Lexer.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Casting.h"
26
27 namespace clang::tidy::bugprone {
28
29 using ast_matchers::BoundNodes;
30 using ast_matchers::callee;
31 using ast_matchers::callExpr;
32 using ast_matchers::cxxMemberCallExpr;
33 using ast_matchers::cxxMethodDecl;
34 using ast_matchers::expr;
35 using ast_matchers::functionDecl;
36 using ast_matchers::hasName;
37 using ast_matchers::hasParent;
38 using ast_matchers::ignoringImplicit;
39 using ast_matchers::ignoringParenImpCasts;
40 using ast_matchers::MatchFinder;
41 using ast_matchers::optionally;
42 using ast_matchers::returns;
43 using ast_matchers::stmt;
44 using ast_matchers::stmtExpr;
45 using ast_matchers::unless;
46 using ast_matchers::voidType;
47
48 const Expr *getCondition(const BoundNodes &Nodes, const StringRef NodeId) {
49 const auto *If = Nodes.getNodeAs<IfStmt>(NodeId);
50 if (If != nullptr)
51 return If->getCond();
52
53 const auto *For = Nodes.getNodeAs<ForStmt>(NodeId);
54 if (For != nullptr)
55 return For->getCond();
56
57 const auto *While = Nodes.getNodeAs<WhileStmt>(NodeId);
58 if (While != nullptr)
59 return While->getCond();
60
61 const auto *Do = Nodes.getNodeAs<DoStmt>(NodeId);
62 if (Do != nullptr)
63 return Do->getCond();
64
65 const auto *Switch = Nodes.getNodeAs<SwitchStmt>(NodeId);
66 if (Switch != nullptr)
67 return Switch->getCond();
68
69 return nullptr;
70 }
71
72 void StandaloneEmptyCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
73 const auto NonMemberMatcher = expr(ignoringImplicit(ignoringParenImpCasts(
74 callExpr(
75 hasParent(stmt(optionally(hasParent(stmtExpr().bind("stexpr"))))
76 .bind("parent")),
77 callee(functionDecl(hasName("empty"), unless(returns(voidType())))))
78 .bind("empty"))));
79 const auto MemberMatcher =
80 expr(ignoringImplicit(ignoringParenImpCasts(cxxMemberCallExpr(
81 hasParent(stmt(optionally(hasParent(stmtExpr().bind("stexpr"))))
82 .bind("parent")),
83 callee(cxxMethodDecl(hasName("empty"),
84 unless(returns(voidType()))))))))
85 .bind("empty");
86
87 Finder->addMatcher(MemberMatcher, this);
88 Finder->addMatcher(NonMemberMatcher, this);
89 }
90
91 void StandaloneEmptyCheck::check(const MatchFinder::MatchResult &Result) {
92 // Skip if the parent node is Expr.
93 if (Result.Nodes.getNodeAs<Expr>("parent"))
94 return;
95
96 const auto PParentStmtExpr = Result.Nodes.getNodeAs<Expr>("stexpr");
97 const auto ParentCompStmt = Result.Nodes.getNodeAs<CompoundStmt>("parent");
98 const auto *ParentCond = getCondition(Result.Nodes, "parent");
99 const auto *ParentReturnStmt = Result.Nodes.getNodeAs<ReturnStmt>("parent");
100
101 if (const auto *MemberCall =
102 Result.Nodes.getNodeAs<CXXMemberCallExpr>("empty")) {
103 // Skip if it's a condition of the parent statement.
104 if (ParentCond == MemberCall->getExprStmt())
105 return;
106 // Skip if it's the last statement in the GNU extension
107 // statement expression.
108 if (PParentStmtExpr && ParentCompStmt &&
109 ParentCompStmt->body_back() == MemberCall->getExprStmt())
110 return;
111 // Skip if it's a return statement
112 if (ParentReturnStmt)
113 return;
114
115 SourceLocation MemberLoc = MemberCall->getBeginLoc();
116 SourceLocation ReplacementLoc = MemberCall->getExprLoc();
117 SourceRange ReplacementRange = SourceRange(ReplacementLoc, ReplacementLoc);
118
119 ASTContext &Context = MemberCall->getRecordDecl()->getASTContext();
120 DeclarationName Name =
121 Context.DeclarationNames.getIdentifier(&Context.Idents.get("clear"));
122
123 auto Candidates = MemberCall->getRecordDecl()->lookupDependentName(
124 Name, [](const NamedDecl *ND) {
125 return isa<CXXMethodDecl>(ND) &&
126 llvm::cast<CXXMethodDecl>(ND)->getMinRequiredArguments() ==
127 0 &&
128 !llvm::cast<CXXMethodDecl>(ND)->isConst();
129 });
130
131 bool HasClear = !Candidates.empty();
132 if (HasClear) {
133 const CXXMethodDecl *Clear = llvm::cast<CXXMethodDecl>(Candidates.at(0));
134 QualType RangeType = MemberCall->getImplicitObjectArgument()->getType();
135 bool QualifierIncompatible =
136 (!Clear->isVolatile() && RangeType.isVolatileQualified()) ||
137 RangeType.isConstQualified();
138 if (!QualifierIncompatible) {
139 diag(MemberLoc,
140 "ignoring the result of 'empty()'; did you mean 'clear()'? ")
141 << FixItHint::CreateReplacement(ReplacementRange, "clear");
142 return;
143 }
144 }
145
146 diag(MemberLoc, "ignoring the result of 'empty()'");
147
148 } else if (const auto *NonMemberCall =
149 Result.Nodes.getNodeAs<CallExpr>("empty")) {
150 if (ParentCond == NonMemberCall->getExprStmt())
151 return;
152 if (PParentStmtExpr && ParentCompStmt &&
153 ParentCompStmt->body_back() == NonMemberCall->getExprStmt())
154 return;
155 if (ParentReturnStmt)
156 return;
157
158 SourceLocation NonMemberLoc = NonMemberCall->getExprLoc();
159 SourceLocation NonMemberEndLoc = NonMemberCall->getEndLoc();
160
161 const Expr *Arg = NonMemberCall->getArg(0);
162 CXXRecordDecl *ArgRecordDecl = Arg->getType()->getAsCXXRecordDecl();
163 if (ArgRecordDecl == nullptr)
164 return;
165
166 ASTContext &Context = ArgRecordDecl->getASTContext();
167 DeclarationName Name =
168 Context.DeclarationNames.getIdentifier(&Context.Idents.get("clear"));
169
170 auto Candidates =
171 ArgRecordDecl->lookupDependentName(Name, [](const NamedDecl *ND) {
172 return isa<CXXMethodDecl>(ND) &&
173 llvm::cast<CXXMethodDecl>(ND)->getMinRequiredArguments() ==
174 0 &&
175 !llvm::cast<CXXMethodDecl>(ND)->isConst();
176 });
177
178 bool HasClear = !Candidates.empty();
179
180 if (HasClear) {
181 const CXXMethodDecl *Clear = llvm::cast<CXXMethodDecl>(Candidates.at(0));
182 bool QualifierIncompatible =
183 (!Clear->isVolatile() && Arg->getType().isVolatileQualified()) ||
184 Arg->getType().isConstQualified();
185 if (!QualifierIncompatible) {
186 std::string ReplacementText =
187 std::string(Lexer::getSourceText(
188 CharSourceRange::getTokenRange(Arg->getSourceRange()),
189 *Result.SourceManager, getLangOpts())) +
190 ".clear()";
191 SourceRange ReplacementRange =
192 SourceRange(NonMemberLoc, NonMemberEndLoc);
193 diag(NonMemberLoc,
194 "ignoring the result of '%0'; did you mean 'clear()'?")
195 << llvm::dyn_cast<NamedDecl>(NonMemberCall->getCalleeDecl())
196 ->getQualifiedNameAsString()
197 << FixItHint::CreateReplacement(ReplacementRange, ReplacementText);
198 return;
199 }
200 }
201
202 diag(NonMemberLoc, "ignoring the result of '%0'")
203 << llvm::dyn_cast<NamedDecl>(NonMemberCall->getCalleeDecl())
204 ->getQualifiedNameAsString();
205 }
206 }
207
208 } // namespace clang::tidy::bugprone