1 //===--- CloneChecker.cpp - Clone detection checker -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// CloneChecker is a checker that reports clones in the current translation
14 //===----------------------------------------------------------------------===//
16 #include "ClangSACheckers.h"
17 #include "clang/Analysis/CloneDetection.h"
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20 #include "clang/StaticAnalyzer/Core/Checker.h"
21 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
25 using namespace clang;
30 : public Checker<check::ASTCodeBody, check::EndOfTranslationUnit> {
31 mutable CloneDetector Detector;
32 mutable std::unique_ptr<BugType> BT_Exact, BT_Suspicious;
35 void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
36 BugReporter &BR) const;
38 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
39 AnalysisManager &Mgr, BugReporter &BR) const;
41 /// \brief Reports all clones to the user.
42 void reportClones(BugReporter &BR, AnalysisManager &Mgr,
43 int MinComplexity) const;
45 /// \brief Reports only suspicious clones to the user along with informaton
46 /// that explain why they are suspicious.
47 void reportSuspiciousClones(BugReporter &BR, AnalysisManager &Mgr,
48 int MinComplexity) const;
50 } // end anonymous namespace
52 void CloneChecker::checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
53 BugReporter &BR) const {
54 // Every statement that should be included in the search for clones needs to
55 // be passed to the CloneDetector.
56 Detector.analyzeCodeBody(D);
59 void CloneChecker::checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
61 BugReporter &BR) const {
62 // At this point, every statement in the translation unit has been analyzed by
63 // the CloneDetector. The only thing left to do is to report the found clones.
65 int MinComplexity = Mgr.getAnalyzerOptions().getOptionAsInteger(
66 "MinimumCloneComplexity", 10, this);
67 assert(MinComplexity >= 0);
69 bool ReportSuspiciousClones = Mgr.getAnalyzerOptions().getBooleanOption(
70 "ReportSuspiciousClones", true, this);
72 bool ReportNormalClones = Mgr.getAnalyzerOptions().getBooleanOption(
73 "ReportNormalClones", true, this);
75 if (ReportSuspiciousClones)
76 reportSuspiciousClones(BR, Mgr, MinComplexity);
78 if (ReportNormalClones)
79 reportClones(BR, Mgr, MinComplexity);
82 static PathDiagnosticLocation makeLocation(const StmtSequence &S,
83 AnalysisManager &Mgr) {
84 ASTContext &ACtx = Mgr.getASTContext();
85 return PathDiagnosticLocation::createBegin(
86 S.front(), ACtx.getSourceManager(),
87 Mgr.getAnalysisDeclContext(ACtx.getTranslationUnitDecl()));
90 void CloneChecker::reportClones(BugReporter &BR, AnalysisManager &Mgr,
91 int MinComplexity) const {
93 std::vector<CloneDetector::CloneGroup> CloneGroups;
94 Detector.findClones(CloneGroups, MinComplexity);
97 BT_Exact.reset(new BugType(this, "Exact code clone", "Code clone"));
99 for (CloneDetector::CloneGroup &Group : CloneGroups) {
100 // We group the clones by printing the first as a warning and all others
102 auto R = llvm::make_unique<BugReport>(
103 *BT_Exact, "Duplicate code detected",
104 makeLocation(Group.Sequences.front(), Mgr));
105 R->addRange(Group.Sequences.front().getSourceRange());
107 for (unsigned i = 1; i < Group.Sequences.size(); ++i)
108 R->addNote("Similar code here",
109 makeLocation(Group.Sequences[i], Mgr),
110 Group.Sequences[i].getSourceRange());
111 BR.emitReport(std::move(R));
115 void CloneChecker::reportSuspiciousClones(BugReporter &BR,
116 AnalysisManager &Mgr,
117 int MinComplexity) const {
119 std::vector<CloneDetector::SuspiciousClonePair> Clones;
120 Detector.findSuspiciousClones(Clones, MinComplexity);
124 new BugType(this, "Suspicious code clone", "Code clone"));
126 ASTContext &ACtx = BR.getContext();
127 SourceManager &SM = ACtx.getSourceManager();
128 AnalysisDeclContext *ADC =
129 Mgr.getAnalysisDeclContext(ACtx.getTranslationUnitDecl());
131 for (CloneDetector::SuspiciousClonePair &Pair : Clones) {
132 // FIXME: We are ignoring the suggestions currently, because they are
133 // only 50% accurate (even if the second suggestion is unavailable),
134 // which may confuse the user.
135 // Think how to perform more accurate suggestions?
137 auto R = llvm::make_unique<BugReport>(
139 "Potential copy-paste error; did you really mean to use '" +
140 Pair.FirstCloneInfo.Variable->getNameAsString() + "' here?",
141 PathDiagnosticLocation::createBegin(Pair.FirstCloneInfo.Mention, SM,
143 R->addRange(Pair.FirstCloneInfo.Mention->getSourceRange());
145 R->addNote("Similar code using '" +
146 Pair.SecondCloneInfo.Variable->getNameAsString() + "' here",
147 PathDiagnosticLocation::createBegin(Pair.SecondCloneInfo.Mention,
149 Pair.SecondCloneInfo.Mention->getSourceRange());
151 BR.emitReport(std::move(R));
155 //===----------------------------------------------------------------------===//
156 // Register CloneChecker
157 //===----------------------------------------------------------------------===//
159 void ento::registerCloneChecker(CheckerManager &Mgr) {
160 Mgr.registerChecker<CloneChecker>();