116 const Stmt *Stmt2,
bool IgnoreSideEffects) {
117 if (!Stmt1 || !Stmt2)
118 return !Stmt1 && !Stmt2;
122 if (Stmt1->getStmtClass() != Stmt2->getStmtClass())
125 const auto *Expr1 = dyn_cast<Expr>(Stmt1);
126 const auto *Expr2 = dyn_cast<Expr>(Stmt2);
128 if (Expr1 && Expr2) {
131 if (!IgnoreSideEffects && Expr1->HasSideEffects(Ctx) &&
132 Expr2->HasSideEffects(Ctx))
136 if ((Expr1->getExprLoc().isMacroID()) || (Expr2->getExprLoc().isMacroID()))
140 Expr::const_child_iterator I1 = Expr1->child_begin();
141 Expr::const_child_iterator I2 = Expr2->child_begin();
142 while (I1 != Expr1->child_end() && I2 != Expr2->child_end()) {
150 if (I1 != Expr1->child_end())
152 if (I2 != Expr2->child_end())
156 switch (Stmt1->getStmtClass()) {
159 case Stmt::CallExprClass:
160 case Stmt::ArraySubscriptExprClass:
161 case Stmt::ArraySectionExprClass:
162 case Stmt::OMPArrayShapingExprClass:
163 case Stmt::OMPIteratorExprClass:
164 case Stmt::ImplicitCastExprClass:
165 case Stmt::ParenExprClass:
166 case Stmt::BreakStmtClass:
167 case Stmt::ContinueStmtClass:
168 case Stmt::NullStmtClass:
170 case Stmt::CStyleCastExprClass: {
171 const auto *CastExpr1 = cast<CStyleCastExpr>(Stmt1);
172 const auto *CastExpr2 = cast<CStyleCastExpr>(Stmt2);
174 return CastExpr1->getTypeAsWritten() == CastExpr2->getTypeAsWritten();
176 case Stmt::ReturnStmtClass: {
177 const auto *ReturnStmt1 = cast<ReturnStmt>(Stmt1);
178 const auto *ReturnStmt2 = cast<ReturnStmt>(Stmt2);
181 ReturnStmt2->getRetValue(), IgnoreSideEffects);
183 case Stmt::ForStmtClass: {
184 const auto *ForStmt1 = cast<ForStmt>(Stmt1);
185 const auto *ForStmt2 = cast<ForStmt>(Stmt2);
201 case Stmt::DoStmtClass: {
202 const auto *DStmt1 = cast<DoStmt>(Stmt1);
203 const auto *DStmt2 = cast<DoStmt>(Stmt2);
213 case Stmt::WhileStmtClass: {
214 const auto *WStmt1 = cast<WhileStmt>(Stmt1);
215 const auto *WStmt2 = cast<WhileStmt>(Stmt2);
225 case Stmt::IfStmtClass: {
226 const auto *IStmt1 = cast<IfStmt>(Stmt1);
227 const auto *IStmt2 = cast<IfStmt>(Stmt2);
240 case Stmt::CompoundStmtClass: {
241 const auto *CompStmt1 = cast<CompoundStmt>(Stmt1);
242 const auto *CompStmt2 = cast<CompoundStmt>(Stmt2);
244 if (CompStmt1->size() != CompStmt2->size())
247 if (!llvm::all_of(llvm::zip(CompStmt1->body(), CompStmt2->body()),
248 [&Ctx, IgnoreSideEffects](
249 std::tuple<const Stmt *, const Stmt *> StmtPair) {
250 const Stmt *Stmt0 = std::get<0>(StmtPair);
251 const Stmt *Stmt1 = std::get<1>(StmtPair);
252 return isIdenticalStmt(Ctx, Stmt0, Stmt1,
260 case Stmt::CompoundAssignOperatorClass:
261 case Stmt::BinaryOperatorClass: {
262 const auto *BinOp1 = cast<BinaryOperator>(Stmt1);
263 const auto *BinOp2 = cast<BinaryOperator>(Stmt2);
264 return BinOp1->getOpcode() == BinOp2->getOpcode();
266 case Stmt::CharacterLiteralClass: {
267 const auto *CharLit1 = cast<CharacterLiteral>(Stmt1);
268 const auto *CharLit2 = cast<CharacterLiteral>(Stmt2);
269 return CharLit1->getValue() == CharLit2->getValue();
271 case Stmt::DeclRefExprClass: {
272 const auto *DeclRef1 = cast<DeclRefExpr>(Stmt1);
273 const auto *DeclRef2 = cast<DeclRefExpr>(Stmt2);
274 return DeclRef1->getDecl() == DeclRef2->getDecl();
276 case Stmt::IntegerLiteralClass: {
277 const auto *IntLit1 = cast<IntegerLiteral>(Stmt1);
278 const auto *IntLit2 = cast<IntegerLiteral>(Stmt2);
280 const llvm::APInt I1 = IntLit1->getValue();
281 const llvm::APInt I2 = IntLit2->getValue();
282 if (I1.getBitWidth() != I2.getBitWidth())
286 case Stmt::FloatingLiteralClass: {
287 const auto *FloatLit1 = cast<FloatingLiteral>(Stmt1);
288 const auto *FloatLit2 = cast<FloatingLiteral>(Stmt2);
289 return FloatLit1->getValue().bitwiseIsEqual(FloatLit2->getValue());
291 case Stmt::StringLiteralClass: {
292 const auto *StringLit1 = cast<StringLiteral>(Stmt1);
293 const auto *StringLit2 = cast<StringLiteral>(Stmt2);
294 return StringLit1->getBytes() == StringLit2->getBytes();
296 case Stmt::MemberExprClass: {
297 const auto *MemberStmt1 = cast<MemberExpr>(Stmt1);
298 const auto *MemberStmt2 = cast<MemberExpr>(Stmt2);
299 return MemberStmt1->getMemberDecl() == MemberStmt2->getMemberDecl();
301 case Stmt::UnaryOperatorClass: {
302 const auto *UnaryOp1 = cast<UnaryOperator>(Stmt1);
303 const auto *UnaryOp2 = cast<UnaryOperator>(Stmt2);
304 return UnaryOp1->getOpcode() == UnaryOp2->getOpcode();
310 const ASTContext &Context = *Result.Context;
312 if (
const auto *IS = Result.Nodes.getNodeAs<IfStmt>(
"if")) {
313 const Stmt *Then = IS->getThen();
314 assert(Then &&
"An IfStmt must have a `then` branch!");
316 const Stmt *Else = Result.Nodes.getNodeAs<Stmt>(
"else");
317 assert(Else &&
"We only look for `if` statements with an `else` branch!");
319 if (!isa<IfStmt>(Else)) {
322 Else->IgnoreContainers(), Context)) {
323 diag(IS->getBeginLoc(),
"if with identical then and else branches");
324 diag(IS->getElseLoc(),
"else branch starts here", DiagnosticIDs::Note);
331 llvm::SmallVector<const Stmt *, 4> Branches;
332 const IfStmt *Cur = IS;
335 Branches.push_back(Cur->getThen());
337 Else = Cur->getElse();
343 Cur = dyn_cast<IfStmt>(Else);
346 Branches.push_back(Else);
351 const size_t N = Branches.size();
352 llvm::BitVector KnownAsClone(N);
354 for (
size_t I = 0; I + 1 < N; I++) {
361 for (
size_t J = I + 1; J < N; J++) {
363 Branches[I]->IgnoreContainers(),
364 Branches[J]->IgnoreContainers(), Context))
368 KnownAsClone[J] =
true;
370 if (NumCopies == 2) {
372 diag(Branches[I]->getBeginLoc(),
373 "repeated branch body in conditional chain");
374 const SourceLocation End =
375 Lexer::getLocForEndOfToken(Branches[I]->getEndLoc(), 0,
376 *Result.SourceManager, getLangOpts());
378 diag(End,
"end of the original", DiagnosticIDs::Note);
382 diag(Branches[J]->getBeginLoc(),
"clone %0 starts here",
390 if (
const auto *CO = Result.Nodes.getNodeAs<ConditionalOperator>(
"condOp")) {
394 diag(CO->getQuestionLoc(),
395 "conditional operator with identical true and false expressions");
400 if (
const auto *SS = Result.Nodes.getNodeAs<SwitchStmt>(
"switch")) {
401 const auto *Body = dyn_cast_or_null<CompoundStmt>(SS->getBody());
415 llvm::SmallVector<SwitchBranch, 4> Branches;
416 for (
const Stmt *S : Body->body()) {
418 if (isa<SwitchCase>(S))
419 Branches.emplace_back();
425 if (!Branches.empty())
426 Branches.back().push_back(S);
429 auto *End = Branches.end();
430 auto *BeginCurrent = Branches.begin();
431 while (BeginCurrent < End) {
437 auto *EndCurrent = BeginCurrent + 1;
438 while (EndCurrent < End &&
445 if (EndCurrent == (BeginCurrent + 1)) {
447 BeginCurrent = EndCurrent;
451 diag(BeginCurrent->front()->getBeginLoc(),
452 "switch has %0 consecutive identical branches")
453 <<
static_cast<int>(std::distance(BeginCurrent, EndCurrent));
455 SourceLocation EndLoc = (EndCurrent - 1)->back()->getEndLoc();
460 if (EndLoc.isInvalid())
461 EndLoc = (EndCurrent - 1)->back()->getBeginLoc();
462 if (EndLoc.isMacroID())
463 EndLoc = Context.getSourceManager().getExpansionLoc(EndLoc);
464 EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
466 if (EndLoc.isValid()) {
467 diag(EndLoc,
"last of these clones ends here", DiagnosticIDs::Note);
469 BeginCurrent = EndCurrent;
474 if (
const auto *IS = Result.Nodes.getNodeAs<IfStmt>(
"ifWithDescendantIf")) {
475 const Stmt *Then = IS->getThen();
476 const auto *CS = dyn_cast<CompoundStmt>(Then);
477 if (CS && (!CS->body_empty())) {
478 const auto *InnerIf = dyn_cast<IfStmt>(*CS->body_begin());
479 if (InnerIf &&
isIdenticalStmt(Context, IS->getCond(), InnerIf->getCond(),
481 diag(IS->getBeginLoc(),
"if with identical inner if statement");
482 diag(InnerIf->getBeginLoc(),
"inner if starts here",
483 DiagnosticIDs::Note);
489 llvm_unreachable(
"No if statement and no switch statement.");