29 :
public Checker<check::ASTCodeBody, check::EndOfTranslationUnit> {
33 bool ReportNormalClones =
false;
34 StringRef IgnoredFilesPattern;
38 mutable std::unique_ptr<BugType> BT_Exact, BT_Suspicious;
49 std::vector<CloneDetector::CloneGroup> &CloneGroups)
const;
53 void reportSuspiciousClones(
55 std::vector<CloneDetector::CloneGroup> &CloneGroups)
const;
63 Detector.analyzeCodeBody(D);
75 std::vector<CloneDetector::CloneGroup> AllCloneGroups;
83 reportSuspiciousClones(BR, Mgr, AllCloneGroups);
87 if (!ReportNormalClones)
96 reportClones(BR, Mgr, AllCloneGroups);
107void CloneChecker::reportClones(
109 std::vector<CloneDetector::CloneGroup> &CloneGroups)
const {
112 BT_Exact.reset(
new BugType(
this,
"Exact code clone",
"Code clone"));
117 auto R = std::make_unique<BasicBugReport>(
119 R->addRange(
Group.front().getSourceRange());
121 for (
unsigned i = 1; i <
Group.size(); ++i)
122 R->addNote(
"Similar code here",
makeLocation(Group[i], Mgr),
128void CloneChecker::reportSuspiciousClones(
130 std::vector<CloneDetector::CloneGroup> &CloneGroups)
const {
131 std::vector<VariablePattern::SuspiciousClonePair> Pairs;
134 for (
unsigned i = 0; i <
Group.size(); ++i) {
137 for (
unsigned j = i + 1; j <
Group.size(); ++j) {
149 if (PatternA.countPatternDifferences(PatternB, &ClonePair) == 1) {
150 Pairs.push_back(ClonePair);
159 new BugType(
this,
"Suspicious code clone",
"Code clone"));
172 auto R = std::make_unique<BasicBugReport>(
174 "Potential copy-paste error; did you really mean to use '" +
175 Pair.FirstCloneInfo.Variable->getNameAsString() +
"' here?",
178 R->addRange(Pair.FirstCloneInfo.Mention->getSourceRange());
180 R->addNote(
"Similar code using '" +
181 Pair.SecondCloneInfo.Variable->getNameAsString() +
"' here",
184 Pair.SecondCloneInfo.Mention->getSourceRange());
198 Checker,
"MinimumCloneComplexity");
200 if (
Checker->MinComplexity < 0)
202 Checker,
"MinimumCloneComplexity",
"a non-negative value");
205 Checker,
"ReportNormalClones");
Defines the Diagnostic-related interfaces.
static PathDiagnosticLocation makeLocation(const StmtSequence &S, AnalysisManager &Mgr)
This file defines classes for searching and analyzing source code clones.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
SourceManager & getSourceManager()
TranslationUnitDecl * getTranslationUnitDecl() const
AnalysisDeclContext contains the context data for the function, method or block under analysis.
bool getCheckerBooleanOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Interprets an option's string value as a boolean.
int getCheckerIntegerOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Interprets an option's string value as an integer value.
StringRef getCheckerStringOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Query an option's string value.
Searches for similar subtrees in the AST.
static void constrainClones(std::vector< CloneGroup > &CloneGroups, T C)
Constrains the given list of clone groups with the given constraint.
Decl - This represents one declaration (or definition), e.g.
Ensures that every clone has at least the given complexity.
Ensures that all clone groups contain at least the given amount of clones.
This constraint moves clones into clone groups of type II via hashing.
This constraint moves clones into clone groups of type II by comparing them.
This class handles loading and caching of source files into memory.
Identifies a list of statements.
The top declaration context.
Analyzes the pattern of the referenced variables in a statement.
ASTContext & getASTContext() override
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
BugReporter is a utility class for generating PathDiagnostics for analysis.
ASTContext & getContext()
virtual void emitReport(std::unique_ptr< BugReport > R)
Add the given report to the set of reports tracked by BugReporter.
const AnalyzerOptions & getAnalyzerOptions() const
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
void reportInvalidCheckerOptionValue(const CheckerBase *C, StringRef OptionName, StringRef ExpectedValueDesc) const
Emits an error through a DiagnosticsEngine about an invalid user supplied checker option value.
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
Ensures that all clones reference variables in the same pattern.
Ensures that no clone group fully contains another clone group.
Describes two clones that reference their variables in a different pattern which could indicate a pro...