#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/Analysis/CloneDetection.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
using namespace clang;
using namespace ento;
namespace {
class CloneChecker
: public Checker<check::ASTCodeBody, check::EndOfTranslationUnit> {
public:
int MinComplexity;
bool ReportNormalClones = false;
StringRef IgnoredFilesPattern;
private:
mutable CloneDetector Detector;
mutable std::unique_ptr<BugType> BT_Exact, BT_Suspicious;
public:
void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
BugReporter &BR) const;
void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
AnalysisManager &Mgr, BugReporter &BR) const;
void reportClones(BugReporter &BR, AnalysisManager &Mgr,
std::vector<CloneDetector::CloneGroup> &CloneGroups) const;
void reportSuspiciousClones(
BugReporter &BR, AnalysisManager &Mgr,
std::vector<CloneDetector::CloneGroup> &CloneGroups) const;
};
}
void CloneChecker::checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
BugReporter &BR) const {
Detector.analyzeCodeBody(D);
}
void CloneChecker::checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
AnalysisManager &Mgr,
BugReporter &BR) const {
std::vector<CloneDetector::CloneGroup> AllCloneGroups;
Detector.findClones(
AllCloneGroups, FilenamePatternConstraint(IgnoredFilesPattern),
RecursiveCloneTypeIIHashConstraint(), MinGroupSizeConstraint(2),
MinComplexityConstraint(MinComplexity),
RecursiveCloneTypeIIVerifyConstraint(), OnlyLargestCloneConstraint());
reportSuspiciousClones(BR, Mgr, AllCloneGroups);
if (!ReportNormalClones)
return;
CloneDetector::constrainClones(AllCloneGroups,
MatchingVariablePatternConstraint(),
MinGroupSizeConstraint(2));
reportClones(BR, Mgr, AllCloneGroups);
}
static PathDiagnosticLocation makeLocation(const StmtSequence &S,
AnalysisManager &Mgr) {
ASTContext &ACtx = Mgr.getASTContext();
return PathDiagnosticLocation::createBegin(
S.front(), ACtx.getSourceManager(),
Mgr.getAnalysisDeclContext(ACtx.getTranslationUnitDecl()));
}
void CloneChecker::reportClones(
BugReporter &BR, AnalysisManager &Mgr,
std::vector<CloneDetector::CloneGroup> &CloneGroups) const {
if (!BT_Exact)
BT_Exact.reset(new BugType(this, "Exact code clone", "Code clone"));
for (const CloneDetector::CloneGroup &Group : CloneGroups) {
auto R = std::make_unique<BasicBugReport>(
*BT_Exact, "Duplicate code detected", makeLocation(Group.front(), Mgr));
R->addRange(Group.front().getSourceRange());
for (unsigned i = 1; i < Group.size(); ++i)
R->addNote("Similar code here", makeLocation(Group[i], Mgr),
Group[i].getSourceRange());
BR.emitReport(std::move(R));
}
}
void CloneChecker::reportSuspiciousClones(
BugReporter &BR, AnalysisManager &Mgr,
std::vector<CloneDetector::CloneGroup> &CloneGroups) const {
std::vector<VariablePattern::SuspiciousClonePair> Pairs;
for (const CloneDetector::CloneGroup &Group : CloneGroups) {
for (unsigned i = 0; i < Group.size(); ++i) {
VariablePattern PatternA(Group[i]);
for (unsigned j = i + 1; j < Group.size(); ++j) {
VariablePattern PatternB(Group[j]);
VariablePattern::SuspiciousClonePair ClonePair;
if (PatternA.countPatternDifferences(PatternB, &ClonePair) == 1) {
Pairs.push_back(ClonePair);
break;
}
}
}
}
if (!BT_Suspicious)
BT_Suspicious.reset(
new BugType(this, "Suspicious code clone", "Code clone"));
ASTContext &ACtx = BR.getContext();
SourceManager &SM = ACtx.getSourceManager();
AnalysisDeclContext *ADC =
Mgr.getAnalysisDeclContext(ACtx.getTranslationUnitDecl());
for (VariablePattern::SuspiciousClonePair &Pair : Pairs) {
auto R = std::make_unique<BasicBugReport>(
*BT_Suspicious,
"Potential copy-paste error; did you really mean to use '" +
Pair.FirstCloneInfo.Variable->getNameAsString() + "' here?",
PathDiagnosticLocation::createBegin(Pair.FirstCloneInfo.Mention, SM,
ADC));
R->addRange(Pair.FirstCloneInfo.Mention->getSourceRange());
R->addNote("Similar code using '" +
Pair.SecondCloneInfo.Variable->getNameAsString() + "' here",
PathDiagnosticLocation::createBegin(Pair.SecondCloneInfo.Mention,
SM, ADC),
Pair.SecondCloneInfo.Mention->getSourceRange());
BR.emitReport(std::move(R));
}
}
void ento::registerCloneChecker(CheckerManager &Mgr) {
auto *Checker = Mgr.registerChecker<CloneChecker>();
Checker->MinComplexity = Mgr.getAnalyzerOptions().getCheckerIntegerOption(
Checker, "MinimumCloneComplexity");
if (Checker->MinComplexity < 0)
Mgr.reportInvalidCheckerOptionValue(
Checker, "MinimumCloneComplexity", "a non-negative value");
Checker->ReportNormalClones = Mgr.getAnalyzerOptions().getCheckerBooleanOption(
Checker, "ReportNormalClones");
Checker->IgnoredFilesPattern = Mgr.getAnalyzerOptions()
.getCheckerStringOption(Checker, "IgnoredFilesPattern");
}
bool ento::shouldRegisterCloneChecker(const CheckerManager &mgr) {
return true;
}