15 #ifndef LLVM_CLANG_AST_CLONEDETECTION_H 16 #define LLVM_CLANG_AST_CLONEDETECTION_H 19 #include "llvm/Support/Regex.h" 77 iterator
begin()
const;
101 return EndIndex - StartIndex;
137 return std::tie(S, StartIndex, EndIndex) ==
138 std::tie(Other.S, Other.StartIndex, Other.EndIndex);
142 return std::tie(S, StartIndex, EndIndex) !=
143 std::tie(Other.S, Other.StartIndex, Other.EndIndex);
175 void analyzeCodeBody(
const Decl *D);
185 template <
typename T>
187 C.constrain(CloneGroups);
195 template <
typename T1,
typename... Ts>
197 Ts... ConstraintList) {
198 constrainClones(CloneGroups, C);
199 constrainClones(CloneGroups, ConstraintList...);
207 template <
typename... Ts>
212 CloneGroup AllClones;
213 AllClones.reserve(Sequences.size());
214 for (
const auto &
C : Sequences) {
215 AllClones.push_back(
C);
218 Result.push_back(AllClones);
220 constrainClones(Result, ConstraintList...);
224 CloneGroup Sequences;
237 std::vector<CloneDetector::CloneGroup> &CloneGroups,
240 std::remove_if(CloneGroups.begin(), CloneGroups.end(), Filter),
250 static void splitCloneGroups(
251 std::vector<CloneDetector::CloneGroup> &CloneGroups,
265 void constrain(std::vector<CloneDetector::CloneGroup> &Sequences);
276 void constrain(std::vector<CloneDetector::CloneGroup> &Sequences);
285 unsigned MinComplexity;
289 : MinComplexity(MinComplexity) {}
296 const std::string &ParentMacroStack =
"");
298 void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups) {
302 return calculateStmtComplexity(A.front(), MinComplexity) <
312 unsigned MinGroupSize;
316 : MinGroupSize(MinGroupSize) {}
318 void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups) {
321 return A.size() < MinGroupSize;
328 void constrain(std::vector<CloneDetector::CloneGroup> &
Result);
336 : IgnoredFilesPattern(IgnoredFilesPattern) {
337 IgnoredFilesRegex = std::make_shared<llvm::Regex>(
"^(" +
338 IgnoredFilesPattern.str() +
"$)");
343 void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups) {
346 return isAutoGenerated(Group);
355 struct VariableOccurence {
361 VariableOccurence(
size_t KindID,
const Stmt *Mention)
362 : KindID(KindID), Mention(Mention) {}
366 std::vector<VariableOccurence> Occurences;
369 std::vector<const VarDecl *> Variables;
377 void addVariables(
const Stmt *S);
383 for (
const Stmt *S : Sequence)
402 const VarDecl *Suggestion)
403 : Variable(Variable), Mention(Mention), Suggestion(Suggestion) {}
435 unsigned countPatternDifferences(
442 void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups);
447 #endif // LLVM_CLANG_AST_CLONEDETECTION_H FilenamePatternConstraint(StringRef IgnoredFilesPattern)
SourceLocation getEndLoc() const
Returns the end sourcelocation of the last statement in this sequence.
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Stmt - This represents one statement.
ASTContext & getASTContext() const
Returns the related ASTContext for the stored Stmts.
SourceRange getSourceRange() const
Returns the source range of the whole sequence - from the beginning of the first statement to the end...
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.
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
bool operator!=(const StmtSequence &Other) const
bool operator==(const StmtSequence &Other) const
void constrain(std::vector< CloneDetector::CloneGroup > &CloneGroups)
VarDecl - An instance of this class is created to represent a variable declaration or definition...
StmtSequence()
Constructs an empty StmtSequence.
SuspiciousCloneInfo(const VarDecl *Variable, const Stmt *Mention, const VarDecl *Suggestion)
iterator begin() const
Returns an iterator pointing to the first statement in this sequence.
Identifies a list of statements.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
This class is a utility class that contains utility functions for building custom constraints...
Ensures that all clones reference variables in the same pattern.
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.
VariablePattern(const StmtSequence &Sequence)
Creates an VariablePattern object with information about the given StmtSequence.
void findClones(std::vector< CloneGroup > &Result, Ts... ConstraintList)
Searches for clones in all previously passed statements.
iterator end() const
Returns an iterator pointing behind the last statement in this sequence.
Searches for similar subtrees in the AST.
void constrain(std::vector< CloneDetector::CloneGroup > &CloneGroups)
bool empty() const
Returns true if and only if this StmtSequence contains no statements.
Describes two clones that reference their variables in a different pattern which could indicate a pro...
CompoundStmt - This represents a group of statements like { stmt stmt }.
llvm::SmallVector< StmtSequence, 8 > CloneGroup
A collection of StmtSequences that share an arbitrary property.
This constraint moves clones into clone groups of type II via hashing.
SuspiciousCloneInfo SecondCloneInfo
This other clone in the pair which can have a suggested variable.
const FunctionProtoType * T
const Decl * getContainingDecl() const
Returns the declaration that contains the stored Stmts.
This constraint moves clones into clone groups of type II by comparing them.
StringRef IgnoredFilesPattern
Ensures that every clone has at least the given complexity.
const VarDecl * Suggestion
The variable that should have been referenced to follow the pattern.
The result type of a method or function.
std::shared_ptr< llvm::Regex > IgnoredFilesRegex
Utility class holding the relevant information about a single clone in this pair. ...
const Stmt * Mention
Where the variable was referenced.
unsigned size() const
Returns the number of statements this object holds.
static void constrainClones(std::vector< CloneGroup > &CloneGroups, T1 C, Ts... ConstraintList)
Constrains the given list of clone groups with the given list of constraints.
MinComplexityConstraint(unsigned MinComplexity)
Encodes a location in the source.
const Stmt *const * iterator
bool holdsSequence() const
Returns true if this objects holds a list of statements.
void constrain(std::vector< CloneDetector::CloneGroup > &CloneGroups)
Dataflow Directional Tag Classes.
const Stmt * back() const
Returns the last statement in this sequence.
SourceLocation getStartLoc() const
Returns the start sourcelocation of the first statement in this sequence.
MinGroupSizeConstraint(unsigned MinGroupSize=2)
bool contains(const StmtSequence &Other) const
Returns true if and only if this sequence covers a source range that contains the source range of the...
static void filterGroups(std::vector< CloneDetector::CloneGroup > &CloneGroups, llvm::function_ref< bool(const CloneDetector::CloneGroup &)> Filter)
Removes all groups by using a filter function.
SuspiciousCloneInfo FirstCloneInfo
The first clone in the pair which always has a suggested variable.
A trivial tuple used to represent a source range.
const VarDecl * Variable
The variable which referencing in this clone was against the pattern.
Ensures that no clone group fully contains another clone group.