Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 1 | //===--- 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 Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 21 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | using namespace ento; |
| 27 | |
| 28 | namespace { |
| 29 | class CloneChecker |
| 30 | : public Checker<check::ASTCodeBody, check::EndOfTranslationUnit> { |
Artem Dergachev | 96034ca | 2016-07-26 19:05:22 +0000 | [diff] [blame] | 31 | mutable CloneDetector Detector; |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 32 | mutable std::unique_ptr<BugType> BT_Exact, BT_Suspicious; |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 33 | |
| 34 | public: |
| 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 Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 40 | |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 41 | /// \brief Reports all clones to the user. |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 42 | void reportClones(BugReporter &BR, AnalysisManager &Mgr, |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 43 | int MinComplexity) const; |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 44 | |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 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; |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 49 | }; |
| 50 | } // end anonymous namespace |
| 51 | |
| 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. |
Artem Dergachev | 96034ca | 2016-07-26 19:05:22 +0000 | [diff] [blame] | 56 | Detector.analyzeCodeBody(D); |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void CloneChecker::checkEndOfTranslationUnit(const TranslationUnitDecl *TU, |
| 60 | AnalysisManager &Mgr, |
| 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. |
| 64 | |
| 65 | int MinComplexity = Mgr.getAnalyzerOptions().getOptionAsInteger( |
| 66 | "MinimumCloneComplexity", 10, this); |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 67 | assert(MinComplexity >= 0); |
| 68 | |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 69 | bool ReportSuspiciousClones = Mgr.getAnalyzerOptions().getBooleanOption( |
| 70 | "ReportSuspiciousClones", true, this); |
| 71 | |
| 72 | bool ReportNormalClones = Mgr.getAnalyzerOptions().getBooleanOption( |
| 73 | "ReportNormalClones", true, this); |
| 74 | |
Artem Dergachev | f8b4fc3 | 2017-04-05 14:17:36 +0000 | [diff] [blame] | 75 | if (ReportSuspiciousClones) |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 76 | reportSuspiciousClones(BR, Mgr, MinComplexity); |
Artem Dergachev | f8b4fc3 | 2017-04-05 14:17:36 +0000 | [diff] [blame] | 77 | |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 78 | if (ReportNormalClones) |
| 79 | reportClones(BR, Mgr, MinComplexity); |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 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())); |
| 88 | } |
| 89 | |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 90 | void CloneChecker::reportClones(BugReporter &BR, AnalysisManager &Mgr, |
| 91 | int MinComplexity) const { |
| 92 | |
| 93 | std::vector<CloneDetector::CloneGroup> CloneGroups; |
| 94 | Detector.findClones(CloneGroups, MinComplexity); |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 95 | |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 96 | if (!BT_Exact) |
| 97 | BT_Exact.reset(new BugType(this, "Exact code clone", "Code clone")); |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 98 | |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 99 | for (CloneDetector::CloneGroup &Group : CloneGroups) { |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 100 | // We group the clones by printing the first as a warning and all others |
| 101 | // as a note. |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 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()); |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 106 | |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 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()); |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 111 | BR.emitReport(std::move(R)); |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 115 | void CloneChecker::reportSuspiciousClones(BugReporter &BR, |
| 116 | AnalysisManager &Mgr, |
| 117 | int MinComplexity) const { |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 118 | |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 119 | std::vector<CloneDetector::SuspiciousClonePair> Clones; |
| 120 | Detector.findSuspiciousClones(Clones, MinComplexity); |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 121 | |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 122 | if (!BT_Suspicious) |
| 123 | BT_Suspicious.reset( |
| 124 | new BugType(this, "Suspicious code clone", "Code clone")); |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 125 | |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 126 | ASTContext &ACtx = BR.getContext(); |
| 127 | SourceManager &SM = ACtx.getSourceManager(); |
| 128 | AnalysisDeclContext *ADC = |
| 129 | Mgr.getAnalysisDeclContext(ACtx.getTranslationUnitDecl()); |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 130 | |
Artem Dergachev | c4aee48 | 2017-04-05 15:06:17 +0000 | [diff] [blame] | 131 | for (CloneDetector::SuspiciousClonePair &Pair : Clones) { |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 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? |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 136 | |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 137 | auto R = llvm::make_unique<BugReport>( |
| 138 | *BT_Suspicious, |
| 139 | "Potential copy-paste error; did you really mean to use '" + |
| 140 | Pair.FirstCloneInfo.Variable->getNameAsString() + "' here?", |
| 141 | PathDiagnosticLocation::createBegin(Pair.FirstCloneInfo.Mention, SM, |
| 142 | ADC)); |
| 143 | R->addRange(Pair.FirstCloneInfo.Mention->getSourceRange()); |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 144 | |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 145 | R->addNote("Similar code using '" + |
| 146 | Pair.SecondCloneInfo.Variable->getNameAsString() + "' here", |
| 147 | PathDiagnosticLocation::createBegin(Pair.SecondCloneInfo.Mention, |
| 148 | SM, ADC), |
| 149 | Pair.SecondCloneInfo.Mention->getSourceRange()); |
| 150 | |
| 151 | BR.emitReport(std::move(R)); |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 155 | //===----------------------------------------------------------------------===// |
| 156 | // Register CloneChecker |
| 157 | //===----------------------------------------------------------------------===// |
| 158 | |
| 159 | void ento::registerCloneChecker(CheckerManager &Mgr) { |
| 160 | Mgr.registerChecker<CloneChecker>(); |
| 161 | } |