10#include "../utils/ASTUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/RecursiveASTVisitor.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Analysis/CloneDetection.h"
15#include "clang/Lex/Lexer.h"
16#include "llvm/Support/Casting.h"
24using SwitchBranch = llvm::SmallVector<const Stmt *, 2>;
31 const SwitchBranch &RHS,
32 const ASTContext &Context) {
33 if (LHS.size() != RHS.size())
36 for (
size_t I = 0, Size = LHS.size(); I < Size; I++) {
41 RHS[I]->stripLabelLikeStatements(),
54 bool TraverseLambdaExpr(LambdaExpr *, DataRecursionQueue * =
nullptr) {
58 bool TraverseDecl(
Decl *) {
62 bool TraverseSwitchStmt(SwitchStmt *, DataRecursionQueue * =
nullptr) {
66 bool TraverseSwitchCase(SwitchCase *, DataRecursionQueue * =
nullptr) {
70 bool TraverseDefaultStmt(DefaultStmt *, DataRecursionQueue * =
nullptr) {
74 bool TraverseAttributedStmt(AttributedStmt *S) {
78 for (
const Attr *A : S->getAttrs()) {
79 if (isa<FallThroughAttr>(A))
87 for (
const Stmt *Elem : Branch) {
88 if (!Visitor.TraverseStmt(
const_cast<Stmt *
>(Elem)))
98 ifStmt(unless(allOf(isConstexpr(), isInTemplateInstantiation())),
100 hasParent(stmt(unless(ifStmt(hasElse(equalsBoundNode(
"if")))))),
101 hasElse(stmt().bind(
"else"))),
103 Finder->addMatcher(switchStmt().bind(
"switch"),
this);
104 Finder->addMatcher(conditionalOperator().bind(
"condOp"),
this);
108 const ASTContext &Context = *Result.Context;
110 if (
const auto *IS = Result.Nodes.getNodeAs<IfStmt>(
"if")) {
111 const Stmt *Then = IS->getThen();
112 assert(Then &&
"An IfStmt must have a `then` branch!");
114 const Stmt *Else = Result.Nodes.getNodeAs<Stmt>(
"else");
115 assert(Else &&
"We only look for `if` statements with an `else` branch!");
117 if (!isa<IfStmt>(Else)) {
120 Else->IgnoreContainers(), Context)) {
121 diag(IS->getBeginLoc(),
"if with identical then and else branches");
122 diag(IS->getElseLoc(),
"else branch starts here", DiagnosticIDs::Note);
129 llvm::SmallVector<const Stmt *, 4>
Branches;
130 const IfStmt *Cur = IS;
135 Else = Cur->getElse();
141 Cur = dyn_cast<IfStmt>(Else);
150 llvm::BitVector KnownAsClone(N);
152 for (
size_t I = 0; I + 1 < N; I++) {
159 for (
size_t J = I + 1; J < N; J++) {
162 Branches[J]->IgnoreContainers(), Context))
166 KnownAsClone[J] =
true;
168 if (NumCopies == 2) {
171 "repeated branch body in conditional chain");
173 Lexer::getLocForEndOfToken(
Branches[I]->getEndLoc(), 0,
176 diag(End,
"end of the original", DiagnosticIDs::Note);
180 diag(
Branches[J]->getBeginLoc(),
"clone %0 starts here",
188 if (
const auto *CO = Result.Nodes.getNodeAs<ConditionalOperator>(
"condOp")) {
192 diag(CO->getQuestionLoc(),
193 "conditional operator with identical true and false expressions");
198 if (
const auto *SS = Result.Nodes.getNodeAs<SwitchStmt>(
"switch")) {
199 const auto *Body = dyn_cast_or_null<CompoundStmt>(SS->getBody());
213 llvm::SmallVector<SwitchBranch, 4>
Branches;
214 for (
const Stmt *S : Body->body()) {
216 if (isa<SwitchCase>(S))
228 auto *BeginCurrent =
Branches.begin();
229 while (BeginCurrent < End) {
235 auto *EndCurrent = BeginCurrent + 1;
236 while (EndCurrent < End &&
243 if (EndCurrent == (BeginCurrent + 1)) {
245 BeginCurrent = EndCurrent;
249 diag(BeginCurrent->front()->getBeginLoc(),
250 "switch has %0 consecutive identical branches")
251 <<
static_cast<int>(std::distance(BeginCurrent, EndCurrent));
253 SourceLocation EndLoc = (EndCurrent - 1)->back()->getEndLoc();
258 if (EndLoc.isInvalid())
259 EndLoc = (EndCurrent - 1)->back()->getBeginLoc();
260 if (EndLoc.isMacroID())
261 EndLoc = Context.getSourceManager().getExpansionLoc(EndLoc);
262 EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
264 if (EndLoc.isValid()) {
265 diag(EndLoc,
"last of these clones ends here", DiagnosticIDs::Note);
267 BeginCurrent = EndCurrent;
272 llvm_unreachable(
"No if statement and no switch statement.");
const FunctionDecl * Decl
static bool isFallthroughSwitchBranch(const SwitchBranch &Branch)
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
bool areStatementsIdentical(const Stmt *FirstStmt, const Stmt *SecondStmt, const ASTContext &Context, bool Canonical)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//