14#ifndef LLVM_CLANG_ANALYSIS_CLONEDETECTION_H
15#define LLVM_CLANG_ANALYSIS_CLONEDETECTION_H
18#include "llvm/Support/Regex.h"
100 return EndIndex - StartIndex;
136 return std::tie(S, StartIndex, EndIndex) ==
141 return std::tie(S, StartIndex, EndIndex) !=
184 template <
typename T>
186 C.constrain(CloneGroups);
194 template <
typename T1,
typename... Ts>
196 Ts... ConstraintList) {
206 template <
typename... Ts>
211 Result.push_back(Sequences);
230 std::vector<CloneDetector::CloneGroup> &CloneGroups,
232 llvm::erase_if(CloneGroups, Filter);
242 std::vector<CloneDetector::CloneGroup> &CloneGroups,
256 void constrain(std::vector<CloneDetector::CloneGroup> &Sequences);
267 void constrain(std::vector<CloneDetector::CloneGroup> &Sequences);
276 unsigned MinComplexity;
280 : MinComplexity(MinComplexity) {}
287 const std::string &ParentMacroStack =
"");
289 void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups) {
303 unsigned MinGroupSize;
307 : MinGroupSize(MinGroupSize) {}
309 void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups) {
312 return A.size() < MinGroupSize;
334 void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups) {
346 struct VariableOccurence {
352 VariableOccurence(
size_t KindID,
const Stmt *Mention)
353 : KindID(KindID), Mention(Mention) {}
357 std::vector<VariableOccurence> Occurences;
360 std::vector<const VarDecl *> Variables;
368 void addVariables(
const Stmt *S);
374 for (
const Stmt *S : Sequence)
433 void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups);
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.
static void filterGroups(std::vector< CloneDetector::CloneGroup > &CloneGroups, llvm::function_ref< bool(const CloneDetector::CloneGroup &)> Filter)
Removes all groups by using a filter function.
static void splitCloneGroups(std::vector< CloneDetector::CloneGroup > &CloneGroups, llvm::function_ref< bool(const StmtSequence &, const StmtSequence &)> Compare)
Splits the given CloneGroups until the given Compare function returns true for all clones in a single...
Searches for similar subtrees in the AST.
void findClones(std::vector< CloneGroup > &Result, Ts... ConstraintList)
Searches for clones in all previously passed statements.
static void constrainClones(std::vector< CloneGroup > &CloneGroups, T C)
Constrains the given list of clone groups with the given constraint.
static void constrainClones(std::vector< CloneGroup > &CloneGroups, T1 C, Ts... ConstraintList)
Constrains the given list of clone groups with the given list of constraints.
void analyzeCodeBody(const Decl *D)
Generates and stores search data for all statements in the body of the given Decl.
llvm::SmallVector< StmtSequence, 8 > CloneGroup
A collection of StmtSequences that share an arbitrary property.
CompoundStmt - This represents a group of statements like { stmt stmt }.
Decl - This represents one declaration (or definition), e.g.
Ensures that every clone has at least the given complexity.
size_t calculateStmtComplexity(const StmtSequence &Seq, std::size_t Limit, const std::string &ParentMacroStack="")
Calculates the complexity of the given StmtSequence.
MinComplexityConstraint(unsigned MinComplexity)
void constrain(std::vector< CloneDetector::CloneGroup > &CloneGroups)
Ensures that all clone groups contain at least the given amount of clones.
void constrain(std::vector< CloneDetector::CloneGroup > &CloneGroups)
MinGroupSizeConstraint(unsigned MinGroupSize=2)
This constraint moves clones into clone groups of type II via hashing.
void constrain(std::vector< CloneDetector::CloneGroup > &Sequences)
This constraint moves clones into clone groups of type II by comparing them.
void constrain(std::vector< CloneDetector::CloneGroup > &Sequences)
Encodes a location in the source.
A trivial tuple used to represent a source range.
Identifies a list of statements.
bool operator==(const StmtSequence &Other) const
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...
iterator begin() const
Returns an iterator pointing to the first statement in this sequence.
const Stmt *const * iterator
const Decl * getContainingDecl() const
Returns the declaration that contains the stored Stmts.
StmtSequence()
Constructs an empty StmtSequence.
ASTContext & getASTContext() const
Returns the related ASTContext for the stored Stmts.
unsigned size() const
Returns the number of statements this object holds.
iterator end() const
Returns an iterator pointing behind the last statement in this sequence.
SourceLocation getEndLoc() const
Returns the end sourcelocation of the last statement in this sequence.
bool operator!=(const StmtSequence &Other) const
bool empty() const
Returns true if and only if this StmtSequence contains no statements.
const Stmt * front() const
Returns the first statement in this sequence.
bool holdsSequence() const
Returns true if this objects holds a list of statements.
SourceLocation getBeginLoc() const
Returns the start sourcelocation of the first statement in this sequence.
SourceRange getSourceRange() const
Returns the source range of the whole sequence - from the beginning of the first statement to the end...
const Stmt * back() const
Returns the last statement in this sequence.
Stmt - This represents one statement.
Represents a variable declaration or definition.
Analyzes the pattern of the referenced variables in a statement.
VariablePattern(const StmtSequence &Sequence)
Creates an VariablePattern object with information about the given StmtSequence.
unsigned countPatternDifferences(const VariablePattern &Other, VariablePattern::SuspiciousClonePair *FirstMismatch=nullptr)
Counts the differences between this pattern and the given one.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
The JSON file list parser is used to communicate input to InstallAPI.
@ Seq
'seq' clause, allowed on 'loop' and 'routine' directives.
@ Result
The result type of a method or function.
const FunctionProtoType * T
@ Other
Other implicit parameter.
std::shared_ptr< llvm::Regex > IgnoredFilesRegex
FilenamePatternConstraint(StringRef IgnoredFilesPattern)
bool isAutoGenerated(const CloneDetector::CloneGroup &Group)
void constrain(std::vector< CloneDetector::CloneGroup > &CloneGroups)
StringRef IgnoredFilesPattern
Ensures that all clones reference variables in the same pattern.
void constrain(std::vector< CloneDetector::CloneGroup > &CloneGroups)
Ensures that no clone group fully contains another clone group.
void constrain(std::vector< CloneDetector::CloneGroup > &Result)
Utility class holding the relevant information about a single clone in this pair.
const Stmt * Mention
Where the variable was referenced.
const VarDecl * Suggestion
The variable that should have been referenced to follow the pattern.
const VarDecl * Variable
The variable which referencing in this clone was against the pattern.
SuspiciousCloneInfo(const VarDecl *Variable, const Stmt *Mention, const VarDecl *Suggestion)
Describes two clones that reference their variables in a different pattern which could indicate a pro...
SuspiciousCloneInfo SecondCloneInfo
This other clone in the pair which can have a suggested variable.
SuspiciousCloneInfo FirstCloneInfo
The first clone in the pair which always has a suggested variable.