25 using namespace clang;
30 :
public Checker<check::ASTCodeBody, check::EndOfTranslationUnit> {
32 mutable std::unique_ptr<BugType> BT_Exact, BT_Suspicious;
43 std::vector<CloneDetector::CloneGroup> &CloneGroups)
const;
47 void reportSuspiciousClones(
49 std::vector<CloneDetector::CloneGroup> &CloneGroups)
const;
57 Detector.analyzeCodeBody(D);
67 "MinimumCloneComplexity", 50,
this);
68 assert(MinComplexity >= 0);
71 "ReportSuspiciousClones",
true,
this);
74 "ReportNormalClones",
true,
this);
77 "IgnoredFilesPattern",
"",
this);
82 std::vector<CloneDetector::CloneGroup> AllCloneGroups;
90 if (ReportSuspiciousClones)
91 reportSuspiciousClones(BR, Mgr, AllCloneGroups);
95 if (!ReportNormalClones)
104 reportClones(BR, Mgr, AllCloneGroups);
115 void CloneChecker::reportClones(
117 std::vector<CloneDetector::CloneGroup> &CloneGroups)
const {
120 BT_Exact.reset(
new BugType(
this,
"Exact code clone",
"Code clone"));
125 auto R = llvm::make_unique<BugReport>(*BT_Exact,
"Duplicate code detected",
127 R->addRange(Group.front().getSourceRange());
129 for (
unsigned i = 1; i < Group.size(); ++i)
130 R->addNote(
"Similar code here",
makeLocation(Group[i], Mgr),
136 void CloneChecker::reportSuspiciousClones(
138 std::vector<CloneDetector::CloneGroup> &CloneGroups)
const {
139 std::vector<VariablePattern::SuspiciousClonePair> Pairs;
142 for (
unsigned i = 0; i < Group.size(); ++i) {
145 for (
unsigned j = i + 1; j < Group.size(); ++j) {
158 Pairs.push_back(ClonePair);
167 new BugType(
this,
"Suspicious code clone",
"Code clone"));
180 auto R = llvm::make_unique<BugReport>(
182 "Potential copy-paste error; did you really mean to use '" +
183 Pair.FirstCloneInfo.Variable->getNameAsString() +
"' here?",
186 R->addRange(Pair.FirstCloneInfo.Mention->getSourceRange());
188 R->addNote(
"Similar code using '" +
189 Pair.SecondCloneInfo.Variable->getNameAsString() +
"' here",
192 Pair.SecondCloneInfo.Mention->getSourceRange());
Analyzes the pattern of the referenced variables in a statement.
Decl - This represents one declaration (or definition), e.g.
const Stmt * front() const
Returns the first statement in this sequence.
This file defines classes for searching and analyzing source code clones.
Identifies a list of statements.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
AnalysisDeclContext contains the context data for the function or method under analysis.
Ensures that all clones reference variables in the same pattern.
unsigned countPatternDifferences(const VariablePattern &Other, VariablePattern::SuspiciousClonePair *FirstMismatch=nullptr)
Counts the differences between this pattern and the given one.
Ensures that all clone groups contain at least the given amount of clones.
static void constrainClones(std::vector< CloneGroup > &CloneGroups, T C)
Constrains the given list of clone groups with the given constraint.
StringRef getOptionAsString(StringRef Name, StringRef DefaultVal, const ento::CheckerBase *C=nullptr, bool SearchInParents=false)
Query an option's string value.
ASTContext & getContext()
Defines the Diagnostic-related interfaces.
Searches for similar subtrees in the AST.
ASTContext & getASTContext() override
Describes two clones that reference their variables in a different pattern which could indicate a pro...
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
This constraint moves clones into clone groups of type II via hashing.
AnalyzerOptions & getAnalyzerOptions() override
This constraint moves clones into clone groups of type II by comparing them.
Ensures that every clone has at least the given complexity.
bool getBooleanOption(StringRef Name, bool DefaultVal, const ento::CheckerBase *C=nullptr, bool SearchInParents=false)
Interprets an option's string value as a boolean.
BugReporter is a utility class for generating PathDiagnostics for analysis.
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
CHECKER * registerChecker()
Used to register checkers.
void emitReport(std::unique_ptr< BugReport > R)
Add the given report to the set of reports tracked by BugReporter.
Dataflow Directional Tag Classes.
static PathDiagnosticLocation makeLocation(const StmtSequence &S, AnalysisManager &Mgr)
SourceManager & getSourceManager()
TranslationUnitDecl * getTranslationUnitDecl() const
TranslationUnitDecl - The top declaration context.
int getOptionAsInteger(StringRef Name, int DefaultVal, const ento::CheckerBase *C=nullptr, bool SearchInParents=false)
Interprets an option's string value as an integer value.
This class handles loading and caching of source files into memory.
Ensures that no clone group fully contains another clone group.