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(stmt().bind(
"if"),
63 hasParent(stmt(unless(ifStmt(hasElse(equalsBoundNode(
"if")))))),
64 hasElse(stmt().bind(
"else"))),
66 Finder->addMatcher(switchStmt().bind(
"switch"),
this);
67 Finder->addMatcher(conditionalOperator().bind(
"condOp"),
this);
70 void BranchCloneCheck::check(
const MatchFinder::MatchResult &
Result) {
71 const ASTContext &Context = *Result.Context;
73 if (
const auto *IS = Result.Nodes.getNodeAs<IfStmt>(
"if")) {
74 const Stmt *Then = IS->getThen();
75 assert(Then &&
"An IfStmt must have a `then` branch!");
77 const Stmt *Else = Result.Nodes.getNodeAs<Stmt>(
"else");
78 assert(Else &&
"We only look for `if` statements with an `else` branch!");
80 if (!isa<IfStmt>(Else)) {
83 Else->IgnoreContainers(), Context)) {
84 diag(IS->getBeginLoc(),
"if with identical then and else branches");
85 diag(IS->getElseLoc(),
"else branch starts here", DiagnosticIDs::Note);
92 llvm::SmallVector<const Stmt *, 4>
Branches;
93 const IfStmt *Cur = IS;
96 Branches.push_back(Cur->getThen());
98 Else = Cur->getElse();
104 Cur = dyn_cast<IfStmt>(Else);
107 Branches.push_back(Else);
112 size_t N = Branches.size();
113 llvm::BitVector KnownAsClone(N);
115 for (
size_t i = 0; i + 1 < N; i++) {
122 for (
size_t j = i + 1; j < N; j++) {
123 if (KnownAsClone[j] ||
125 Branches[j]->IgnoreContainers(), Context))
129 KnownAsClone[j] =
true;
131 if (NumCopies == 2) {
133 diag(Branches[i]->getBeginLoc(),
134 "repeated branch in conditional chain");
136 Lexer::getLocForEndOfToken(Branches[i]->getEndLoc(), 0,
137 *Result.SourceManager, getLangOpts());
139 diag(End,
"end of the original", DiagnosticIDs::Note);
143 diag(Branches[j]->getBeginLoc(),
"clone %0 starts here",
151 if (
const auto *CO = Result.Nodes.getNodeAs<ConditionalOperator>(
"condOp")) {
154 diag(CO->getQuestionLoc(),
155 "conditional operator with identical true and false expressions");
160 if (
const auto *SS = Result.Nodes.getNodeAs<SwitchStmt>(
"switch")) {
161 const CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(SS->getBody());
175 llvm::SmallVector<SwitchBranch, 4>
Branches;
176 for (
const Stmt *S : Body->body()) {
178 if (isa<SwitchCase>(S))
179 Branches.emplace_back();
185 if (!Branches.empty())
186 Branches.back().push_back(S);
189 auto End = Branches.end();
190 auto BeginCurrent = Branches.begin();
191 while (BeginCurrent < End) {
192 auto EndCurrent = BeginCurrent + 1;
193 while (EndCurrent < End &&
199 if (EndCurrent > BeginCurrent + 1) {
200 diag(BeginCurrent->front()->getBeginLoc(),
201 "switch has %0 consecutive identical branches")
202 << static_cast<int>(std::distance(BeginCurrent, EndCurrent));
204 SourceLocation EndLoc = (EndCurrent - 1)->back()->getEndLoc();
209 if (EndLoc.isInvalid())
210 EndLoc = (EndCurrent - 1)->back()->getBeginLoc();
212 if (EndLoc.isMacroID())
213 EndLoc = Context.getSourceManager().getExpansionLoc(EndLoc);
214 EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
217 if (EndLoc.isValid()) {
218 diag(EndLoc,
"last of these clones ends here", DiagnosticIDs::Note);
221 BeginCurrent = EndCurrent;
226 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++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
static bool areStatementsIdentical(const Stmt *LHS, const Stmt *RHS, const ASTContext &Context)
Returns true when the statements are Type I clones of each other.