1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
//===--- CloneDetection.cpp - Finds code clones in an AST -------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// This file implements classes for searching and anlyzing source code clones.
///
//===----------------------------------------------------------------------===//
#include "clang/Analysis/CloneDetection.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/Stmt.h"
#include "llvm/ADT/StringRef.h"
using namespace clang;
StmtSequence::StmtSequence(const CompoundStmt *Stmt, ASTContext &Context,
unsigned StartIndex, unsigned EndIndex)
: S(Stmt), Context(&Context), StartIndex(StartIndex), EndIndex(EndIndex) {
assert(Stmt && "Stmt must not be a nullptr");
assert(StartIndex < EndIndex && "Given array should not be empty");
assert(EndIndex <= Stmt->size() && "Given array too big for this Stmt");
}
StmtSequence::StmtSequence(const Stmt *Stmt, ASTContext &Context)
: S(Stmt), Context(&Context), StartIndex(0), EndIndex(0) {}
StmtSequence::StmtSequence()
: S(nullptr), Context(nullptr), StartIndex(0), EndIndex(0) {}
bool StmtSequence::contains(const StmtSequence &Other) const {
// If both sequences reside in different translation units, they can never
// contain each other.
if (Context != Other.Context)
return false;
const SourceManager &SM = Context->getSourceManager();
// Otherwise check if the start and end locations of the current sequence
// surround the other sequence.
bool StartIsInBounds =
SM.isBeforeInTranslationUnit(getStartLoc(), Other.getStartLoc()) ||
getStartLoc() == Other.getStartLoc();
if (!StartIsInBounds)
return false;
bool EndIsInBounds =
SM.isBeforeInTranslationUnit(Other.getEndLoc(), getEndLoc()) ||
Other.getEndLoc() == getEndLoc();
return EndIsInBounds;
}
StmtSequence::iterator StmtSequence::begin() const {
if (!holdsSequence()) {
return &S;
}
auto CS = cast<CompoundStmt>(S);
return CS->body_begin() + StartIndex;
}
StmtSequence::iterator StmtSequence::end() const {
if (!holdsSequence()) {
return &S + 1;
}
auto CS = cast<CompoundStmt>(S);
return CS->body_begin() + EndIndex;
}
SourceLocation StmtSequence::getStartLoc() const {
return front()->getLocStart();
}
SourceLocation StmtSequence::getEndLoc() const { return back()->getLocEnd(); }
namespace {
/// Generates CloneSignatures for a set of statements and stores the results in
/// a CloneDetector object.
class CloneSignatureGenerator {
CloneDetector &CD;
ASTContext &Context;
/// \brief Generates CloneSignatures for all statements in the given statement
/// tree and stores them in the CloneDetector.
///
/// \param S The root of the given statement tree.
/// \return The CloneSignature of the root statement.
CloneDetector::CloneSignature generateSignatures(const Stmt *S) {
// Create an empty signature that will be filled in this method.
CloneDetector::CloneSignature Signature;
// The only relevant data for now is the class of the statement.
// TODO: Collect statement class specific data.
Signature.Data.push_back(S->getStmtClass());
// Storage for the signatures of the direct child statements. This is only
// needed if the current statement is a CompoundStmt.
std::vector<CloneDetector::CloneSignature> ChildSignatures;
const CompoundStmt *CS = dyn_cast<const CompoundStmt>(S);
// The signature of a statement includes the signatures of its children.
// Therefore we create the signatures for every child and add them to the
// current signature.
for (const Stmt *Child : S->children()) {
// Some statements like 'if' can have nullptr children that we will skip.
if (!Child)
continue;
// Recursive call to create the signature of the child statement. This
// will also create and store all clone groups in this child statement.
auto ChildSignature = generateSignatures(Child);
// Add the collected data to the signature of the current statement.
Signature.add(ChildSignature);
// If the current statement is a CompoundStatement, we need to store the
// signature for the generation of the sub-sequences.
if (CS)
ChildSignatures.push_back(ChildSignature);
}
// If the current statement is a CompoundStmt, we also need to create the
// clone groups from the sub-sequences inside the children.
if (CS)
handleSubSequences(CS, ChildSignatures);
// Save the signature for the current statement in the CloneDetector object.
CD.add(StmtSequence(S, Context), Signature);
return Signature;
}
/// \brief Adds all possible sub-sequences in the child array of the given
/// CompoundStmt to the CloneDetector.
/// \param CS The given CompoundStmt.
/// \param ChildSignatures A list of calculated signatures for each child in
/// the given CompoundStmt.
void handleSubSequences(
const CompoundStmt *CS,
const std::vector<CloneDetector::CloneSignature> &ChildSignatures) {
// FIXME: This function has quadratic runtime right now. Check if skipping
// this function for too long CompoundStmts is an option.
// The length of the sub-sequence. We don't need to handle sequences with
// the length 1 as they are already handled in CollectData().
for (unsigned Length = 2; Length <= CS->size(); ++Length) {
// The start index in the body of the CompoundStmt. We increase the
// position until the end of the sub-sequence reaches the end of the
// CompoundStmt body.
for (unsigned Pos = 0; Pos <= CS->size() - Length; ++Pos) {
// Create an empty signature and add the signatures of all selected
// child statements to it.
CloneDetector::CloneSignature SubSignature;
for (unsigned i = Pos; i < Pos + Length; ++i) {
SubSignature.add(ChildSignatures[i]);
}
// Save the signature together with the information about what children
// sequence we selected.
CD.add(StmtSequence(CS, Context, Pos, Pos + Length), SubSignature);
}
}
}
public:
explicit CloneSignatureGenerator(CloneDetector &CD, ASTContext &Context)
: CD(CD), Context(Context) {}
/// \brief Generates signatures for all statements in the given function body.
void consumeCodeBody(const Stmt *S) { generateSignatures(S); }
};
} // end anonymous namespace
void CloneDetector::analyzeCodeBody(const Decl *D) {
assert(D);
assert(D->hasBody());
CloneSignatureGenerator Generator(*this, D->getASTContext());
Generator.consumeCodeBody(D->getBody());
}
void CloneDetector::add(const StmtSequence &S,
const CloneSignature &Signature) {
// StringMap only works with StringRefs, so we create one for our data vector.
auto &Data = Signature.Data;
StringRef DataRef = StringRef(reinterpret_cast<const char *>(Data.data()),
Data.size() * sizeof(unsigned));
// Search with the help of the signature if we already have encountered a
// clone of the given StmtSequence.
auto I = CloneGroupIndexes.find(DataRef);
if (I == CloneGroupIndexes.end()) {
// We haven't found an existing clone group, so we create a new clone group
// for this StmtSequence and store the index of it in our search map.
CloneGroupIndexes[DataRef] = CloneGroups.size();
CloneGroups.emplace_back(S, Signature.Complexity);
return;
}
// We have found an existing clone group and can expand it with the given
// StmtSequence.
CloneGroups[I->getValue()].Sequences.push_back(S);
}
namespace {
/// \brief Returns true if and only if \p Stmt contains at least one other
/// sequence in the \p Group.
bool containsAnyInGroup(StmtSequence &Stmt,
CloneDetector::CloneGroup &Group) {
for (StmtSequence &GroupStmt : Group.Sequences) {
if (Stmt.contains(GroupStmt))
return true;
}
return false;
}
/// \brief Returns true if and only if all sequences in \p OtherGroup are
/// contained by a sequence in \p Group.
bool containsGroup(CloneDetector::CloneGroup &Group,
CloneDetector::CloneGroup &OtherGroup) {
// We have less sequences in the current group than we have in the other,
// so we will never fulfill the requirement for returning true. This is only
// possible because we know that a sequence in Group can contain at most
// one sequence in OtherGroup.
if (Group.Sequences.size() < OtherGroup.Sequences.size())
return false;
for (StmtSequence &Stmt : Group.Sequences) {
if (!containsAnyInGroup(Stmt, OtherGroup))
return false;
}
return true;
}
} // end anonymous namespace
void CloneDetector::findClones(std::vector<CloneGroup> &Result,
unsigned MinGroupComplexity) {
// Add every valid clone group that fulfills the complexity requirement.
for (const CloneGroup &Group : CloneGroups) {
if (Group.isValid() && Group.Complexity >= MinGroupComplexity) {
Result.push_back(Group);
}
}
std::vector<unsigned> IndexesToRemove;
// Compare every group in the result with the rest. If one groups contains
// another group, we only need to return the bigger group.
// Note: This doesn't scale well, so if possible avoid calling any heavy
// function from this loop to minimize the performance impact.
for (unsigned i = 0; i < Result.size(); ++i) {
for (unsigned j = 0; j < Result.size(); ++j) {
// Don't compare a group with itself.
if (i == j)
continue;
if (containsGroup(Result[j], Result[i])) {
IndexesToRemove.push_back(i);
break;
}
}
}
// Erasing a list of indexes from the vector should be done with decreasing
// indexes. As IndexesToRemove is constructed with increasing values, we just
// reverse iterate over it to get the desired order.
for (auto I = IndexesToRemove.rbegin(); I != IndexesToRemove.rend(); ++I) {
Result.erase(Result.begin() + *I);
}
}
|