blob: 1885b0e39203c44d2945f33ddf15548f0ee7a72f [file] [log] [blame]
Artem Dergachevba816322016-07-26 18:13:12 +00001//===--- CloneChecker.cpp - Clone detection checker -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// CloneChecker is a checker that reports clones in the current translation
12/// unit.
13///
14//===----------------------------------------------------------------------===//
15
16#include "ClangSACheckers.h"
17#include "clang/Analysis/CloneDetection.h"
18#include "clang/Basic/Diagnostic.h"
Artem Dergachev4eca0de2016-10-08 10:54:30 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Artem Dergachevba816322016-07-26 18:13:12 +000020#include "clang/StaticAnalyzer/Core/Checker.h"
21#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Artem Dergachev4eca0de2016-10-08 10:54:30 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Artem Dergachevba816322016-07-26 18:13:12 +000023#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
24
25using namespace clang;
26using namespace ento;
27
28namespace {
29class CloneChecker
30 : public Checker<check::ASTCodeBody, check::EndOfTranslationUnit> {
Artem Dergachev96034ca2016-07-26 19:05:22 +000031 mutable CloneDetector Detector;
Artem Dergachev4eca0de2016-10-08 10:54:30 +000032 mutable std::unique_ptr<BugType> BT_Exact, BT_Suspicious;
Artem Dergachevba816322016-07-26 18:13:12 +000033
34public:
35 void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
36 BugReporter &BR) const;
37
38 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
39 AnalysisManager &Mgr, BugReporter &BR) const;
Artem Dergachev2fc19852016-08-18 12:29:41 +000040
Artem Dergachevda9e7182017-04-06 14:34:07 +000041 /// Reports all clones to the user.
Artem Dergachev4eca0de2016-10-08 10:54:30 +000042 void reportClones(BugReporter &BR, AnalysisManager &Mgr,
Artem Dergachevda9e7182017-04-06 14:34:07 +000043 std::vector<CloneDetector::CloneGroup> &CloneGroups) const;
Artem Dergachev2fc19852016-08-18 12:29:41 +000044
Artem Dergachevda9e7182017-04-06 14:34:07 +000045 /// Reports only suspicious clones to the user along with informaton
46 /// that explain why they are suspicious.
47 void reportSuspiciousClones(
48 BugReporter &BR, AnalysisManager &Mgr,
49 std::vector<CloneDetector::CloneGroup> &CloneGroups) const;
Artem Dergachevba816322016-07-26 18:13:12 +000050};
51} // end anonymous namespace
52
53void CloneChecker::checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
54 BugReporter &BR) const {
55 // Every statement that should be included in the search for clones needs to
56 // be passed to the CloneDetector.
Artem Dergachev96034ca2016-07-26 19:05:22 +000057 Detector.analyzeCodeBody(D);
Artem Dergachevba816322016-07-26 18:13:12 +000058}
59
60void CloneChecker::checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
61 AnalysisManager &Mgr,
62 BugReporter &BR) const {
63 // At this point, every statement in the translation unit has been analyzed by
64 // the CloneDetector. The only thing left to do is to report the found clones.
65
66 int MinComplexity = Mgr.getAnalyzerOptions().getOptionAsInteger(
67 "MinimumCloneComplexity", 10, this);
Artem Dergachevba816322016-07-26 18:13:12 +000068 assert(MinComplexity >= 0);
69
Artem Dergachev2fc19852016-08-18 12:29:41 +000070 bool ReportSuspiciousClones = Mgr.getAnalyzerOptions().getBooleanOption(
71 "ReportSuspiciousClones", true, this);
72
73 bool ReportNormalClones = Mgr.getAnalyzerOptions().getBooleanOption(
74 "ReportNormalClones", true, this);
75
Artem Dergachevda9e7182017-04-06 14:34:07 +000076 // Let the CloneDetector create a list of clones from all the analyzed
77 // statements. We don't filter for matching variable patterns at this point
78 // because reportSuspiciousClones() wants to search them for errors.
79 std::vector<CloneDetector::CloneGroup> AllCloneGroups;
Artem Dergachevf8b4fc32017-04-05 14:17:36 +000080
Artem Dergachevda9e7182017-04-06 14:34:07 +000081 Detector.findClones(AllCloneGroups, RecursiveCloneTypeIIConstraint(),
82 MinComplexityConstraint(MinComplexity),
83 MinGroupSizeConstraint(2), OnlyLargestCloneConstraint());
84
85 if (ReportSuspiciousClones)
86 reportSuspiciousClones(BR, Mgr, AllCloneGroups);
87
88 // We are done for this translation unit unless we also need to report normal
89 // clones.
90 if (!ReportNormalClones)
91 return;
92
93 // Now that the suspicious clone detector has checked for pattern errors,
94 // we also filter all clones who don't have matching patterns
95 CloneDetector::constrainClones(AllCloneGroups,
96 MatchingVariablePatternConstraint(),
97 MinGroupSizeConstraint(2));
98
99 reportClones(BR, Mgr, AllCloneGroups);
Artem Dergachev2fc19852016-08-18 12:29:41 +0000100}
101
Artem Dergachev4eca0de2016-10-08 10:54:30 +0000102static PathDiagnosticLocation makeLocation(const StmtSequence &S,
103 AnalysisManager &Mgr) {
104 ASTContext &ACtx = Mgr.getASTContext();
105 return PathDiagnosticLocation::createBegin(
106 S.front(), ACtx.getSourceManager(),
107 Mgr.getAnalysisDeclContext(ACtx.getTranslationUnitDecl()));
108}
109
Artem Dergachevda9e7182017-04-06 14:34:07 +0000110void CloneChecker::reportClones(
111 BugReporter &BR, AnalysisManager &Mgr,
112 std::vector<CloneDetector::CloneGroup> &CloneGroups) const {
Artem Dergachevba816322016-07-26 18:13:12 +0000113
Artem Dergachev4eca0de2016-10-08 10:54:30 +0000114 if (!BT_Exact)
115 BT_Exact.reset(new BugType(this, "Exact code clone", "Code clone"));
Artem Dergachevba816322016-07-26 18:13:12 +0000116
Artem Dergachevda9e7182017-04-06 14:34:07 +0000117 for (const CloneDetector::CloneGroup &Group : CloneGroups) {
Artem Dergachevba816322016-07-26 18:13:12 +0000118 // We group the clones by printing the first as a warning and all others
119 // as a note.
Artem Dergachevda9e7182017-04-06 14:34:07 +0000120 auto R = llvm::make_unique<BugReport>(*BT_Exact, "Duplicate code detected",
121 makeLocation(Group.front(), Mgr));
122 R->addRange(Group.front().getSourceRange());
Artem Dergachev4eca0de2016-10-08 10:54:30 +0000123
Artem Dergachevda9e7182017-04-06 14:34:07 +0000124 for (unsigned i = 1; i < Group.size(); ++i)
125 R->addNote("Similar code here", makeLocation(Group[i], Mgr),
126 Group[i].getSourceRange());
Artem Dergachev4eca0de2016-10-08 10:54:30 +0000127 BR.emitReport(std::move(R));
Artem Dergachevba816322016-07-26 18:13:12 +0000128 }
129}
130
Artem Dergachevda9e7182017-04-06 14:34:07 +0000131void CloneChecker::reportSuspiciousClones(
132 BugReporter &BR, AnalysisManager &Mgr,
133 std::vector<CloneDetector::CloneGroup> &CloneGroups) const {
134 std::vector<VariablePattern::SuspiciousClonePair> Pairs;
Artem Dergachev2fc19852016-08-18 12:29:41 +0000135
Artem Dergachevda9e7182017-04-06 14:34:07 +0000136 for (const CloneDetector::CloneGroup &Group : CloneGroups) {
137 for (unsigned i = 0; i < Group.size(); ++i) {
138 VariablePattern PatternA(Group[i]);
139
140 for (unsigned j = i + 1; j < Group.size(); ++j) {
141 VariablePattern PatternB(Group[j]);
142
143 VariablePattern::SuspiciousClonePair ClonePair;
144 // For now, we only report clones which break the variable pattern just
145 // once because multiple differences in a pattern are an indicator that
146 // those differences are maybe intended (e.g. because it's actually a
147 // different algorithm).
148 // FIXME: In very big clones even multiple variables can be unintended,
149 // so replacing this number with a percentage could better handle such
150 // cases. On the other hand it could increase the false-positive rate
151 // for all clones if the percentage is too high.
152 if (PatternA.countPatternDifferences(PatternB, &ClonePair) == 1) {
153 Pairs.push_back(ClonePair);
154 break;
155 }
156 }
157 }
158 }
Artem Dergachev2fc19852016-08-18 12:29:41 +0000159
Artem Dergachev4eca0de2016-10-08 10:54:30 +0000160 if (!BT_Suspicious)
161 BT_Suspicious.reset(
162 new BugType(this, "Suspicious code clone", "Code clone"));
Artem Dergachev2fc19852016-08-18 12:29:41 +0000163
Artem Dergachev4eca0de2016-10-08 10:54:30 +0000164 ASTContext &ACtx = BR.getContext();
165 SourceManager &SM = ACtx.getSourceManager();
166 AnalysisDeclContext *ADC =
167 Mgr.getAnalysisDeclContext(ACtx.getTranslationUnitDecl());
Artem Dergachev2fc19852016-08-18 12:29:41 +0000168
Artem Dergachevda9e7182017-04-06 14:34:07 +0000169 for (VariablePattern::SuspiciousClonePair &Pair : Pairs) {
Artem Dergachev4eca0de2016-10-08 10:54:30 +0000170 // FIXME: We are ignoring the suggestions currently, because they are
171 // only 50% accurate (even if the second suggestion is unavailable),
172 // which may confuse the user.
173 // Think how to perform more accurate suggestions?
Artem Dergachev2fc19852016-08-18 12:29:41 +0000174
Artem Dergachev4eca0de2016-10-08 10:54:30 +0000175 auto R = llvm::make_unique<BugReport>(
176 *BT_Suspicious,
177 "Potential copy-paste error; did you really mean to use '" +
178 Pair.FirstCloneInfo.Variable->getNameAsString() + "' here?",
179 PathDiagnosticLocation::createBegin(Pair.FirstCloneInfo.Mention, SM,
180 ADC));
181 R->addRange(Pair.FirstCloneInfo.Mention->getSourceRange());
Artem Dergachev2fc19852016-08-18 12:29:41 +0000182
Artem Dergachev4eca0de2016-10-08 10:54:30 +0000183 R->addNote("Similar code using '" +
184 Pair.SecondCloneInfo.Variable->getNameAsString() + "' here",
185 PathDiagnosticLocation::createBegin(Pair.SecondCloneInfo.Mention,
186 SM, ADC),
187 Pair.SecondCloneInfo.Mention->getSourceRange());
188
189 BR.emitReport(std::move(R));
Artem Dergachev2fc19852016-08-18 12:29:41 +0000190 }
191}
192
Artem Dergachevba816322016-07-26 18:13:12 +0000193//===----------------------------------------------------------------------===//
194// Register CloneChecker
195//===----------------------------------------------------------------------===//
196
197void ento::registerCloneChecker(CheckerManager &Mgr) {
198 Mgr.registerChecker<CloneChecker>();
199}