11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Analysis/CloneDetection.h" 14 #include "llvm/Support/Casting.h" 16 using namespace clang;
21 const ASTContext &Context) {
22 llvm::FoldingSetNodeID DataLHS, DataRHS;
23 LHS->Profile(DataLHS, Context,
false);
24 RHS->Profile(DataRHS, Context,
false);
25 return (DataLHS == DataRHS);
31 using SwitchBranch = llvm::SmallVector<const Stmt *, 2>;
38 const SwitchBranch RHS,
39 const ASTContext &Context) {
40 if (LHS.size() != RHS.size())
43 for (
size_t i = 0, Size = LHS.size(); i < Size; i++) {
48 RHS[i]->stripLabelLikeStatements(), Context)) {
60 void BranchCloneCheck::registerMatchers(MatchFinder *Finder) {
62 ifStmt(unless(allOf(isConstexpr(), isInTemplateInstantiation())),
64 hasParent(stmt(unless(ifStmt(hasElse(equalsBoundNode(
"if")))))),
65 hasElse(stmt().bind(
"else"))),
67 Finder->addMatcher(switchStmt().bind(
"switch"),
this);
68 Finder->addMatcher(conditionalOperator().bind(
"condOp"),
this);
71 void BranchCloneCheck::check(
const MatchFinder::MatchResult &Result) {
72 const ASTContext &Context = *Result.Context;
74 if (
const auto *IS = Result.Nodes.getNodeAs<IfStmt>(
"if")) {
75 const Stmt *Then = IS->getThen();
76 assert(Then &&
"An IfStmt must have a `then` branch!");
78 const Stmt *Else = Result.Nodes.getNodeAs<Stmt>(
"else");
79 assert(Else &&
"We only look for `if` statements with an `else` branch!");
81 if (!isa<IfStmt>(Else)) {
84 Else->IgnoreContainers(), Context)) {
85 diag(IS->getBeginLoc(),
"if with identical then and else branches");
86 diag(IS->getElseLoc(),
"else branch starts here", DiagnosticIDs::Note);
93 llvm::SmallVector<const Stmt *, 4>
Branches;
94 const IfStmt *Cur = IS;
97 Branches.push_back(Cur->getThen());
99 Else = Cur->getElse();
105 Cur = dyn_cast<IfStmt>(Else);
108 Branches.push_back(Else);
113 size_t N = Branches.size();
114 llvm::BitVector KnownAsClone(N);
116 for (
size_t i = 0; i + 1 < N; i++) {
123 for (
size_t j = i + 1; j < N; j++) {
124 if (KnownAsClone[j] ||
126 Branches[j]->IgnoreContainers(), Context))
130 KnownAsClone[j] =
true;
132 if (NumCopies == 2) {
134 diag(Branches[i]->getBeginLoc(),
135 "repeated branch in conditional chain");
137 Lexer::getLocForEndOfToken(Branches[i]->getEndLoc(), 0,
138 *Result.SourceManager, getLangOpts());
140 diag(End,
"end of the original", DiagnosticIDs::Note);
144 diag(Branches[j]->getBeginLoc(),
"clone %0 starts here",
152 if (
const auto *CO = Result.Nodes.getNodeAs<ConditionalOperator>(
"condOp")) {
155 diag(CO->getQuestionLoc(),
156 "conditional operator with identical true and false expressions");
161 if (
const auto *SS = Result.Nodes.getNodeAs<SwitchStmt>(
"switch")) {
162 const CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(SS->getBody());
176 llvm::SmallVector<SwitchBranch, 4>
Branches;
177 for (
const Stmt *S : Body->body()) {
179 if (isa<SwitchCase>(S))
180 Branches.emplace_back();
186 if (!Branches.empty())
187 Branches.back().push_back(S);
190 auto End = Branches.end();
191 auto BeginCurrent = Branches.begin();
192 while (BeginCurrent < End) {
193 auto EndCurrent = BeginCurrent + 1;
194 while (EndCurrent < End &&
200 if (EndCurrent > BeginCurrent + 1) {
201 diag(BeginCurrent->front()->getBeginLoc(),
202 "switch has %0 consecutive identical branches")
203 << static_cast<int>(std::distance(BeginCurrent, EndCurrent));
205 SourceLocation EndLoc = (EndCurrent - 1)->back()->getEndLoc();
210 if (EndLoc.isInvalid())
211 EndLoc = (EndCurrent - 1)->back()->getBeginLoc();
213 if (EndLoc.isMacroID())
214 EndLoc = Context.getSourceManager().getExpansionLoc(EndLoc);
215 EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
218 if (EndLoc.isValid()) {
219 diag(EndLoc,
"last of these clones ends here", DiagnosticIDs::Note);
222 BeginCurrent = EndCurrent;
227 llvm_unreachable(
"No if statement and no switch statement.");
static bool areSwitchBranchesIdentical(const SwitchBranch LHS, const SwitchBranch RHS, const ASTContext &Context)
Determines if the bodies of two branches in a switch statements are Type I clones of each other...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static bool areStatementsIdentical(const Stmt *LHS, const Stmt *RHS, const ASTContext &Context)
Returns true when the statements are Type I clones of each other.