11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Analysis/CloneDetection.h"
14 #include "clang/Lex/Lexer.h"
15 #include "llvm/Support/Casting.h"
17 using namespace clang;
22 const ASTContext &Context) {
23 llvm::FoldingSetNodeID DataLHS, DataRHS;
24 LHS->Profile(DataLHS, Context,
false);
25 RHS->Profile(DataRHS, Context,
false);
26 return (DataLHS == DataRHS);
32 using SwitchBranch = llvm::SmallVector<const Stmt *, 2>;
39 const SwitchBranch RHS,
40 const ASTContext &Context) {
41 if (LHS.size() != RHS.size())
44 for (
size_t i = 0, Size = LHS.size(); i < Size; i++) {
49 RHS[i]->stripLabelLikeStatements(), Context)) {
61 void BranchCloneCheck::registerMatchers(MatchFinder *Finder) {
63 ifStmt(unless(allOf(isConstexpr(), isInTemplateInstantiation())),
65 hasParent(stmt(unless(ifStmt(hasElse(equalsBoundNode(
"if")))))),
66 hasElse(stmt().bind(
"else"))),
68 Finder->addMatcher(switchStmt().bind(
"switch"),
this);
69 Finder->addMatcher(conditionalOperator().bind(
"condOp"),
this);
72 void BranchCloneCheck::check(
const MatchFinder::MatchResult &Result) {
73 const ASTContext &Context = *Result.Context;
75 if (
const auto *IS = Result.Nodes.getNodeAs<IfStmt>(
"if")) {
76 const Stmt *Then = IS->getThen();
77 assert(Then &&
"An IfStmt must have a `then` branch!");
79 const Stmt *Else = Result.Nodes.getNodeAs<Stmt>(
"else");
80 assert(Else &&
"We only look for `if` statements with an `else` branch!");
82 if (!isa<IfStmt>(Else)) {
85 Else->IgnoreContainers(), Context)) {
86 diag(IS->getBeginLoc(),
"if with identical then and else branches");
87 diag(IS->getElseLoc(),
"else branch starts here", DiagnosticIDs::Note);
94 llvm::SmallVector<const Stmt *, 4>
Branches;
95 const IfStmt *Cur = IS;
100 Else = Cur->getElse();
106 Cur = dyn_cast<IfStmt>(Else);
115 llvm::BitVector KnownAsClone(N);
117 for (
size_t i = 0; i + 1 < N; i++) {
124 for (
size_t j = i + 1; j < N; j++) {
125 if (KnownAsClone[j] ||
127 Branches[j]->IgnoreContainers(), Context))
131 KnownAsClone[j] =
true;
133 if (NumCopies == 2) {
136 "repeated branch in conditional chain");
138 Lexer::getLocForEndOfToken(
Branches[i]->getEndLoc(), 0,
139 *Result.SourceManager, getLangOpts());
141 diag(End,
"end of the original", DiagnosticIDs::Note);
145 diag(
Branches[j]->getBeginLoc(),
"clone %0 starts here",
153 if (
const auto *CO = Result.Nodes.getNodeAs<ConditionalOperator>(
"condOp")) {
156 diag(CO->getQuestionLoc(),
157 "conditional operator with identical true and false expressions");
162 if (
const auto *SS = Result.Nodes.getNodeAs<SwitchStmt>(
"switch")) {
163 const CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(SS->getBody());
177 llvm::SmallVector<SwitchBranch, 4>
Branches;
178 for (
const Stmt *S : Body->body()) {
180 if (isa<SwitchCase>(S))
192 auto BeginCurrent =
Branches.begin();
193 while (BeginCurrent < End) {
194 auto EndCurrent = BeginCurrent + 1;
195 while (EndCurrent < End &&
201 if (EndCurrent > BeginCurrent + 1) {
202 diag(BeginCurrent->front()->getBeginLoc(),
203 "switch has %0 consecutive identical branches")
204 << static_cast<int>(std::distance(BeginCurrent, EndCurrent));
206 SourceLocation EndLoc = (EndCurrent - 1)->back()->getEndLoc();
211 if (EndLoc.isInvalid())
212 EndLoc = (EndCurrent - 1)->back()->getBeginLoc();
214 if (EndLoc.isMacroID())
215 EndLoc = Context.getSourceManager().getExpansionLoc(EndLoc);
216 EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
219 if (EndLoc.isValid()) {
220 diag(EndLoc,
"last of these clones ends here", DiagnosticIDs::Note);
223 BeginCurrent = EndCurrent;
228 llvm_unreachable(
"No if statement and no switch statement.");