blob: ee1c6943f28923764478b9e17848ed833cd23089 [file] [log] [blame]
Artem Dergachevba816322016-07-26 18:13:12 +00001//===--- CloneDetection.cpp - Finds code clones in an AST -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// This file implements classes for searching and anlyzing source code clones.
11///
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/CloneDetection.h"
15
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/RecursiveASTVisitor.h"
18#include "clang/AST/Stmt.h"
Artem Dergachev78692ea2016-08-02 12:21:09 +000019#include "clang/AST/StmtVisitor.h"
Artem Dergachevba816322016-07-26 18:13:12 +000020#include "llvm/ADT/StringRef.h"
21
22using namespace clang;
23
24StmtSequence::StmtSequence(const CompoundStmt *Stmt, ASTContext &Context,
25 unsigned StartIndex, unsigned EndIndex)
26 : S(Stmt), Context(&Context), StartIndex(StartIndex), EndIndex(EndIndex) {
27 assert(Stmt && "Stmt must not be a nullptr");
28 assert(StartIndex < EndIndex && "Given array should not be empty");
29 assert(EndIndex <= Stmt->size() && "Given array too big for this Stmt");
30}
31
32StmtSequence::StmtSequence(const Stmt *Stmt, ASTContext &Context)
33 : S(Stmt), Context(&Context), StartIndex(0), EndIndex(0) {}
34
35StmtSequence::StmtSequence()
36 : S(nullptr), Context(nullptr), StartIndex(0), EndIndex(0) {}
37
38bool StmtSequence::contains(const StmtSequence &Other) const {
39 // If both sequences reside in different translation units, they can never
40 // contain each other.
41 if (Context != Other.Context)
42 return false;
43
44 const SourceManager &SM = Context->getSourceManager();
45
46 // Otherwise check if the start and end locations of the current sequence
47 // surround the other sequence.
48 bool StartIsInBounds =
49 SM.isBeforeInTranslationUnit(getStartLoc(), Other.getStartLoc()) ||
50 getStartLoc() == Other.getStartLoc();
51 if (!StartIsInBounds)
52 return false;
53
54 bool EndIsInBounds =
55 SM.isBeforeInTranslationUnit(Other.getEndLoc(), getEndLoc()) ||
56 Other.getEndLoc() == getEndLoc();
57 return EndIsInBounds;
58}
59
60StmtSequence::iterator StmtSequence::begin() const {
61 if (!holdsSequence()) {
62 return &S;
63 }
64 auto CS = cast<CompoundStmt>(S);
65 return CS->body_begin() + StartIndex;
66}
67
68StmtSequence::iterator StmtSequence::end() const {
69 if (!holdsSequence()) {
Vassil Vassilev5721e0f2016-08-09 10:00:23 +000070 return reinterpret_cast<StmtSequence::iterator>(&S) + 1;
Artem Dergachevba816322016-07-26 18:13:12 +000071 }
72 auto CS = cast<CompoundStmt>(S);
73 return CS->body_begin() + EndIndex;
74}
75
76SourceLocation StmtSequence::getStartLoc() const {
77 return front()->getLocStart();
78}
79
80SourceLocation StmtSequence::getEndLoc() const { return back()->getLocEnd(); }
81
82namespace {
Artem Dergachev7a0088b2016-08-04 19:37:00 +000083
84/// \brief Analyzes the pattern of the referenced variables in a statement.
85class VariablePattern {
86
87 /// \brief Describes an occurence of a variable reference in a statement.
88 struct VariableOccurence {
89 /// The index of the associated VarDecl in the Variables vector.
90 size_t KindID;
Artem Dergachev2fc19852016-08-18 12:29:41 +000091 /// The source range in the code where the variable was referenced.
92 SourceRange Range;
Artem Dergachev7a0088b2016-08-04 19:37:00 +000093
Artem Dergachev2fc19852016-08-18 12:29:41 +000094 VariableOccurence(size_t KindID, SourceRange Range)
95 : KindID(KindID), Range(Range) {}
Artem Dergachev7a0088b2016-08-04 19:37:00 +000096 };
97
98 /// All occurences of referenced variables in the order of appearance.
99 std::vector<VariableOccurence> Occurences;
100 /// List of referenced variables in the order of appearance.
101 /// Every item in this list is unique.
102 std::vector<const VarDecl *> Variables;
103
104 /// \brief Adds a new variable referenced to this pattern.
105 /// \param VarDecl The declaration of the variable that is referenced.
Artem Dergachev2fc19852016-08-18 12:29:41 +0000106 /// \param Range The SourceRange where this variable is referenced.
107 void addVariableOccurence(const VarDecl *VarDecl, SourceRange Range) {
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000108 // First check if we already reference this variable
109 for (size_t KindIndex = 0; KindIndex < Variables.size(); ++KindIndex) {
110 if (Variables[KindIndex] == VarDecl) {
111 // If yes, add a new occurence that points to the existing entry in
112 // the Variables vector.
Artem Dergachev2fc19852016-08-18 12:29:41 +0000113 Occurences.emplace_back(KindIndex, Range);
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000114 return;
115 }
116 }
117 // If this variable wasn't already referenced, add it to the list of
118 // referenced variables and add a occurence that points to this new entry.
Artem Dergachev2fc19852016-08-18 12:29:41 +0000119 Occurences.emplace_back(Variables.size(), Range);
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000120 Variables.push_back(VarDecl);
121 }
122
123 /// \brief Adds each referenced variable from the given statement.
124 void addVariables(const Stmt *S) {
125 // Sometimes we get a nullptr (such as from IfStmts which often have nullptr
126 // children). We skip such statements as they don't reference any
127 // variables.
128 if (!S)
129 return;
130
131 // Check if S is a reference to a variable. If yes, add it to the pattern.
132 if (auto D = dyn_cast<DeclRefExpr>(S)) {
133 if (auto VD = dyn_cast<VarDecl>(D->getDecl()->getCanonicalDecl()))
Artem Dergachev2fc19852016-08-18 12:29:41 +0000134 addVariableOccurence(VD, D->getSourceRange());
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000135 }
136
137 // Recursively check all children of the given statement.
138 for (const Stmt *Child : S->children()) {
139 addVariables(Child);
140 }
141 }
142
143public:
144 /// \brief Creates an VariablePattern object with information about the given
145 /// StmtSequence.
146 VariablePattern(const StmtSequence &Sequence) {
147 for (const Stmt *S : Sequence)
148 addVariables(S);
149 }
150
Artem Dergachev2fc19852016-08-18 12:29:41 +0000151 /// \brief Counts the differences between this pattern and the given one.
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000152 /// \param Other The given VariablePattern to compare with.
Artem Dergachev2fc19852016-08-18 12:29:41 +0000153 /// \param FirstMismatch Output parameter that will be filled with information
154 /// about the first difference between the two patterns. This parameter
155 /// can be a nullptr, in which case it will be ignored.
156 /// \return Returns the number of differences between the pattern this object
157 /// is following and the given VariablePattern.
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000158 ///
Artem Dergachev2fc19852016-08-18 12:29:41 +0000159 /// For example, the following statements all have the same pattern and this
160 /// function would return zero:
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000161 ///
162 /// if (a < b) return a; return b;
163 /// if (x < y) return x; return y;
164 /// if (u2 < u1) return u2; return u1;
165 ///
Artem Dergachev2fc19852016-08-18 12:29:41 +0000166 /// But the following statement has a different pattern (note the changed
167 /// variables in the return statements) and would have two differences when
168 /// compared with one of the statements above.
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000169 ///
170 /// if (a < b) return b; return a;
171 ///
172 /// This function should only be called if the related statements of the given
173 /// pattern and the statements of this objects are clones of each other.
Artem Dergachev2fc19852016-08-18 12:29:41 +0000174 unsigned countPatternDifferences(
175 const VariablePattern &Other,
176 CloneDetector::SuspiciousClonePair *FirstMismatch = nullptr) {
177 unsigned NumberOfDifferences = 0;
178
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000179 assert(Other.Occurences.size() == Occurences.size());
180 for (unsigned i = 0; i < Occurences.size(); ++i) {
Artem Dergachev2fc19852016-08-18 12:29:41 +0000181 auto ThisOccurence = Occurences[i];
182 auto OtherOccurence = Other.Occurences[i];
183 if (ThisOccurence.KindID == OtherOccurence.KindID)
184 continue;
185
186 ++NumberOfDifferences;
187
188 // If FirstMismatch is not a nullptr, we need to store information about
189 // the first difference between the two patterns.
190 if (FirstMismatch == nullptr)
191 continue;
192
193 // Only proceed if we just found the first difference as we only store
194 // information about the first difference.
195 if (NumberOfDifferences != 1)
196 continue;
197
198 const VarDecl *FirstSuggestion = nullptr;
199 // If there is a variable available in the list of referenced variables
200 // which wouldn't break the pattern if it is used in place of the
201 // current variable, we provide this variable as the suggested fix.
202 if (OtherOccurence.KindID < Variables.size())
203 FirstSuggestion = Variables[OtherOccurence.KindID];
204
205 // Store information about the first clone.
206 FirstMismatch->FirstCloneInfo =
207 CloneDetector::SuspiciousClonePair::SuspiciousCloneInfo(
208 Variables[ThisOccurence.KindID], ThisOccurence.Range,
209 FirstSuggestion);
210
211 // Same as above but with the other clone. We do this for both clones as
212 // we don't know which clone is the one containing the unintended
213 // pattern error.
214 const VarDecl *SecondSuggestion = nullptr;
215 if (ThisOccurence.KindID < Other.Variables.size())
216 SecondSuggestion = Other.Variables[ThisOccurence.KindID];
217
218 // Store information about the second clone.
219 FirstMismatch->SecondCloneInfo =
220 CloneDetector::SuspiciousClonePair::SuspiciousCloneInfo(
221 Variables[ThisOccurence.KindID], OtherOccurence.Range,
222 SecondSuggestion);
223
224 // SuspiciousClonePair guarantees that the first clone always has a
225 // suggested variable associated with it. As we know that one of the two
226 // clones in the pair always has suggestion, we swap the two clones
227 // in case the first clone has no suggested variable which means that
228 // the second clone has a suggested variable and should be first.
229 if (!FirstMismatch->FirstCloneInfo.Suggestion)
230 std::swap(FirstMismatch->FirstCloneInfo,
231 FirstMismatch->SecondCloneInfo);
232
233 // This ensures that we always have at least one suggestion in a pair.
234 assert(FirstMismatch->FirstCloneInfo.Suggestion);
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000235 }
Artem Dergachev2fc19852016-08-18 12:29:41 +0000236
237 return NumberOfDifferences;
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000238 }
239};
240}
241
242namespace {
Artem Dergachev78692ea2016-08-02 12:21:09 +0000243/// \brief Collects the data of a single Stmt.
244///
245/// This class defines what a code clone is: If it collects for two statements
246/// the same data, then those two statements are considered to be clones of each
247/// other.
248class StmtDataCollector : public ConstStmtVisitor<StmtDataCollector> {
249
250 ASTContext &Context;
251 std::vector<CloneDetector::DataPiece> &CollectedData;
252
253public:
254 /// \brief Collects data of the given Stmt.
255 /// \param S The given statement.
256 /// \param Context The ASTContext of S.
257 /// \param D The given data vector to which all collected data is appended.
258 StmtDataCollector(const Stmt *S, ASTContext &Context,
259 std::vector<CloneDetector::DataPiece> &D)
260 : Context(Context), CollectedData(D) {
261 Visit(S);
262 }
263
264 // Below are utility methods for appending different data to the vector.
265
266 void addData(CloneDetector::DataPiece Integer) {
267 CollectedData.push_back(Integer);
268 }
269
270 // FIXME: The functions below add long strings to the data vector which are
271 // probably not good for performance. Replace the strings with pointer values
272 // or a some other unique integer.
273
274 void addData(llvm::StringRef Str) {
275 if (Str.empty())
276 return;
277
278 const size_t OldSize = CollectedData.size();
279
280 const size_t PieceSize = sizeof(CloneDetector::DataPiece);
281 // Calculate how many vector units we need to accomodate all string bytes.
282 size_t RoundedUpPieceNumber = (Str.size() + PieceSize - 1) / PieceSize;
283 // Allocate space for the string in the data vector.
284 CollectedData.resize(CollectedData.size() + RoundedUpPieceNumber);
285
286 // Copy the string to the allocated space at the end of the vector.
287 std::memcpy(CollectedData.data() + OldSize, Str.data(), Str.size());
288 }
289
290 void addData(const QualType &QT) { addData(QT.getAsString()); }
291
292// The functions below collect the class specific data of each Stmt subclass.
293
294// Utility macro for defining a visit method for a given class. This method
295// calls back to the ConstStmtVisitor to visit all parent classes.
296#define DEF_ADD_DATA(CLASS, CODE) \
297 void Visit##CLASS(const CLASS *S) { \
298 CODE; \
299 ConstStmtVisitor<StmtDataCollector>::Visit##CLASS(S); \
300 }
301
302 DEF_ADD_DATA(Stmt, { addData(S->getStmtClass()); })
303 DEF_ADD_DATA(Expr, { addData(S->getType()); })
304
305 //--- Builtin functionality ----------------------------------------------//
306 DEF_ADD_DATA(ArrayTypeTraitExpr, { addData(S->getTrait()); })
307 DEF_ADD_DATA(ExpressionTraitExpr, { addData(S->getTrait()); })
308 DEF_ADD_DATA(PredefinedExpr, { addData(S->getIdentType()); })
309 DEF_ADD_DATA(TypeTraitExpr, {
310 addData(S->getTrait());
311 for (unsigned i = 0; i < S->getNumArgs(); ++i)
312 addData(S->getArg(i)->getType());
313 })
314
315 //--- Calls --------------------------------------------------------------//
Artem Dergachevcad15142016-08-10 16:25:16 +0000316 DEF_ADD_DATA(CallExpr, {
317 // Function pointers don't have a callee and we just skip hashing it.
Artem Dergachev51838882016-08-20 09:57:21 +0000318 if (const FunctionDecl *D = S->getDirectCallee()) {
319 // If the function is a template instantiation, we also need to handle
320 // the template arguments as they are no included in the qualified name.
321 if (D->isTemplateInstantiation()) {
322 auto Args = D->getTemplateSpecializationArgs();
323 std::string ArgString;
324
325 // Print all template arguments into ArgString
326 llvm::raw_string_ostream OS(ArgString);
327 for (unsigned i = 0; i < Args->size(); ++i) {
328 Args->get(i).print(Context.getLangOpts(), OS);
329 // Add a padding character so that 'foo<X, XX>()' != 'foo<XX, X>()'.
330 OS << '\n';
331 }
332 OS.flush();
333
334 addData(ArgString);
335 }
336 addData(D->getQualifiedNameAsString());
337 }
Artem Dergachevcad15142016-08-10 16:25:16 +0000338 })
Artem Dergachev78692ea2016-08-02 12:21:09 +0000339
340 //--- Exceptions ---------------------------------------------------------//
341 DEF_ADD_DATA(CXXCatchStmt, { addData(S->getCaughtType()); })
342
343 //--- C++ OOP Stmts ------------------------------------------------------//
344 DEF_ADD_DATA(CXXDeleteExpr, {
345 addData(S->isArrayFormAsWritten());
346 addData(S->isGlobalDelete());
347 })
348
349 //--- Casts --------------------------------------------------------------//
350 DEF_ADD_DATA(ObjCBridgedCastExpr, { addData(S->getBridgeKind()); })
351
352 //--- Miscellaneous Exprs ------------------------------------------------//
353 DEF_ADD_DATA(BinaryOperator, { addData(S->getOpcode()); })
354 DEF_ADD_DATA(UnaryOperator, { addData(S->getOpcode()); })
355
356 //--- Control flow -------------------------------------------------------//
357 DEF_ADD_DATA(GotoStmt, { addData(S->getLabel()->getName()); })
358 DEF_ADD_DATA(IndirectGotoStmt, {
359 if (S->getConstantTarget())
360 addData(S->getConstantTarget()->getName());
361 })
362 DEF_ADD_DATA(LabelStmt, { addData(S->getDecl()->getName()); })
363 DEF_ADD_DATA(MSDependentExistsStmt, { addData(S->isIfExists()); })
364 DEF_ADD_DATA(AddrLabelExpr, { addData(S->getLabel()->getName()); })
365
366 //--- Objective-C --------------------------------------------------------//
367 DEF_ADD_DATA(ObjCIndirectCopyRestoreExpr, { addData(S->shouldCopy()); })
368 DEF_ADD_DATA(ObjCPropertyRefExpr, {
369 addData(S->isSuperReceiver());
370 addData(S->isImplicitProperty());
371 })
372 DEF_ADD_DATA(ObjCAtCatchStmt, { addData(S->hasEllipsis()); })
373
374 //--- Miscellaneous Stmts ------------------------------------------------//
375 DEF_ADD_DATA(CXXFoldExpr, {
376 addData(S->isRightFold());
377 addData(S->getOperator());
378 })
379 DEF_ADD_DATA(GenericSelectionExpr, {
380 for (unsigned i = 0; i < S->getNumAssocs(); ++i) {
381 addData(S->getAssocType(i));
382 }
383 })
384 DEF_ADD_DATA(LambdaExpr, {
385 for (const LambdaCapture &C : S->captures()) {
386 addData(C.isPackExpansion());
387 addData(C.getCaptureKind());
388 if (C.capturesVariable())
389 addData(C.getCapturedVar()->getType());
390 }
391 addData(S->isGenericLambda());
392 addData(S->isMutable());
393 })
394 DEF_ADD_DATA(DeclStmt, {
395 auto numDecls = std::distance(S->decl_begin(), S->decl_end());
396 addData(static_cast<CloneDetector::DataPiece>(numDecls));
397 for (const Decl *D : S->decls()) {
398 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
399 addData(VD->getType());
400 }
401 }
402 })
403 DEF_ADD_DATA(AsmStmt, {
404 addData(S->isSimple());
405 addData(S->isVolatile());
406 addData(S->generateAsmString(Context));
407 for (unsigned i = 0; i < S->getNumInputs(); ++i) {
408 addData(S->getInputConstraint(i));
409 }
410 for (unsigned i = 0; i < S->getNumOutputs(); ++i) {
411 addData(S->getOutputConstraint(i));
412 }
413 for (unsigned i = 0; i < S->getNumClobbers(); ++i) {
414 addData(S->getClobber(i));
415 }
416 })
417 DEF_ADD_DATA(AttributedStmt, {
418 for (const Attr *A : S->getAttrs()) {
419 addData(std::string(A->getSpelling()));
420 }
421 })
422};
423} // end anonymous namespace
424
425namespace {
Artem Dergachevba816322016-07-26 18:13:12 +0000426/// Generates CloneSignatures for a set of statements and stores the results in
427/// a CloneDetector object.
428class CloneSignatureGenerator {
429
430 CloneDetector &CD;
431 ASTContext &Context;
432
433 /// \brief Generates CloneSignatures for all statements in the given statement
434 /// tree and stores them in the CloneDetector.
435 ///
436 /// \param S The root of the given statement tree.
437 /// \return The CloneSignature of the root statement.
438 CloneDetector::CloneSignature generateSignatures(const Stmt *S) {
439 // Create an empty signature that will be filled in this method.
440 CloneDetector::CloneSignature Signature;
441
Artem Dergachev78692ea2016-08-02 12:21:09 +0000442 // Collect all relevant data from S and put it into the empty signature.
443 StmtDataCollector(S, Context, Signature.Data);
Artem Dergachevba816322016-07-26 18:13:12 +0000444
445 // Storage for the signatures of the direct child statements. This is only
446 // needed if the current statement is a CompoundStmt.
447 std::vector<CloneDetector::CloneSignature> ChildSignatures;
448 const CompoundStmt *CS = dyn_cast<const CompoundStmt>(S);
449
450 // The signature of a statement includes the signatures of its children.
451 // Therefore we create the signatures for every child and add them to the
452 // current signature.
453 for (const Stmt *Child : S->children()) {
454 // Some statements like 'if' can have nullptr children that we will skip.
455 if (!Child)
456 continue;
457
458 // Recursive call to create the signature of the child statement. This
459 // will also create and store all clone groups in this child statement.
460 auto ChildSignature = generateSignatures(Child);
461
462 // Add the collected data to the signature of the current statement.
463 Signature.add(ChildSignature);
464
465 // If the current statement is a CompoundStatement, we need to store the
466 // signature for the generation of the sub-sequences.
467 if (CS)
468 ChildSignatures.push_back(ChildSignature);
469 }
470
471 // If the current statement is a CompoundStmt, we also need to create the
472 // clone groups from the sub-sequences inside the children.
473 if (CS)
474 handleSubSequences(CS, ChildSignatures);
475
476 // Save the signature for the current statement in the CloneDetector object.
477 CD.add(StmtSequence(S, Context), Signature);
478
479 return Signature;
480 }
481
482 /// \brief Adds all possible sub-sequences in the child array of the given
483 /// CompoundStmt to the CloneDetector.
484 /// \param CS The given CompoundStmt.
485 /// \param ChildSignatures A list of calculated signatures for each child in
486 /// the given CompoundStmt.
487 void handleSubSequences(
488 const CompoundStmt *CS,
489 const std::vector<CloneDetector::CloneSignature> &ChildSignatures) {
490
491 // FIXME: This function has quadratic runtime right now. Check if skipping
492 // this function for too long CompoundStmts is an option.
493
494 // The length of the sub-sequence. We don't need to handle sequences with
495 // the length 1 as they are already handled in CollectData().
496 for (unsigned Length = 2; Length <= CS->size(); ++Length) {
497 // The start index in the body of the CompoundStmt. We increase the
498 // position until the end of the sub-sequence reaches the end of the
499 // CompoundStmt body.
500 for (unsigned Pos = 0; Pos <= CS->size() - Length; ++Pos) {
501 // Create an empty signature and add the signatures of all selected
502 // child statements to it.
503 CloneDetector::CloneSignature SubSignature;
504
505 for (unsigned i = Pos; i < Pos + Length; ++i) {
506 SubSignature.add(ChildSignatures[i]);
507 }
508
509 // Save the signature together with the information about what children
510 // sequence we selected.
511 CD.add(StmtSequence(CS, Context, Pos, Pos + Length), SubSignature);
512 }
513 }
514 }
515
516public:
517 explicit CloneSignatureGenerator(CloneDetector &CD, ASTContext &Context)
518 : CD(CD), Context(Context) {}
519
520 /// \brief Generates signatures for all statements in the given function body.
521 void consumeCodeBody(const Stmt *S) { generateSignatures(S); }
522};
523} // end anonymous namespace
524
525void CloneDetector::analyzeCodeBody(const Decl *D) {
526 assert(D);
527 assert(D->hasBody());
528 CloneSignatureGenerator Generator(*this, D->getASTContext());
529 Generator.consumeCodeBody(D->getBody());
530}
531
532void CloneDetector::add(const StmtSequence &S,
533 const CloneSignature &Signature) {
534 // StringMap only works with StringRefs, so we create one for our data vector.
535 auto &Data = Signature.Data;
536 StringRef DataRef = StringRef(reinterpret_cast<const char *>(Data.data()),
537 Data.size() * sizeof(unsigned));
538
539 // Search with the help of the signature if we already have encountered a
540 // clone of the given StmtSequence.
541 auto I = CloneGroupIndexes.find(DataRef);
542 if (I == CloneGroupIndexes.end()) {
543 // We haven't found an existing clone group, so we create a new clone group
544 // for this StmtSequence and store the index of it in our search map.
545 CloneGroupIndexes[DataRef] = CloneGroups.size();
546 CloneGroups.emplace_back(S, Signature.Complexity);
547 return;
548 }
549
550 // We have found an existing clone group and can expand it with the given
551 // StmtSequence.
552 CloneGroups[I->getValue()].Sequences.push_back(S);
553}
554
555namespace {
556/// \brief Returns true if and only if \p Stmt contains at least one other
557/// sequence in the \p Group.
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000558bool containsAnyInGroup(StmtSequence &Stmt, CloneDetector::CloneGroup &Group) {
Artem Dergachevba816322016-07-26 18:13:12 +0000559 for (StmtSequence &GroupStmt : Group.Sequences) {
560 if (Stmt.contains(GroupStmt))
561 return true;
562 }
563 return false;
564}
565
566/// \brief Returns true if and only if all sequences in \p OtherGroup are
567/// contained by a sequence in \p Group.
568bool containsGroup(CloneDetector::CloneGroup &Group,
569 CloneDetector::CloneGroup &OtherGroup) {
570 // We have less sequences in the current group than we have in the other,
571 // so we will never fulfill the requirement for returning true. This is only
572 // possible because we know that a sequence in Group can contain at most
573 // one sequence in OtherGroup.
574 if (Group.Sequences.size() < OtherGroup.Sequences.size())
575 return false;
576
577 for (StmtSequence &Stmt : Group.Sequences) {
578 if (!containsAnyInGroup(Stmt, OtherGroup))
579 return false;
580 }
581 return true;
582}
583} // end anonymous namespace
584
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000585/// \brief Finds all actual clone groups in a single group of presumed clones.
586/// \param Result Output parameter to which all found groups are added. Every
587/// clone in a group that was added this way follows the same
588/// variable pattern as the other clones in its group.
589/// \param Group A group of clones. The clones are allowed to have a different
590/// variable pattern.
591static void createCloneGroups(std::vector<CloneDetector::CloneGroup> &Result,
592 const CloneDetector::CloneGroup &Group) {
593 // We remove the Sequences one by one, so a list is more appropriate.
594 std::list<StmtSequence> UnassignedSequences(Group.Sequences.begin(),
595 Group.Sequences.end());
596
597 // Search for clones as long as there could be clones in UnassignedSequences.
598 while (UnassignedSequences.size() > 1) {
599
600 // Pick the first Sequence as a protoype for a new clone group.
601 StmtSequence Prototype = UnassignedSequences.front();
602 UnassignedSequences.pop_front();
603
604 CloneDetector::CloneGroup FilteredGroup(Prototype, Group.Complexity);
605
606 // Analyze the variable pattern of the prototype. Every other StmtSequence
607 // needs to have the same pattern to get into the new clone group.
608 VariablePattern PrototypeFeatures(Prototype);
609
610 // Search all remaining StmtSequences for an identical variable pattern
611 // and assign them to our new clone group.
612 auto I = UnassignedSequences.begin(), E = UnassignedSequences.end();
613 while (I != E) {
Artem Dergachev2fc19852016-08-18 12:29:41 +0000614
615 if (VariablePattern(*I).countPatternDifferences(PrototypeFeatures) == 0) {
Artem Dergachev7a0088b2016-08-04 19:37:00 +0000616 FilteredGroup.Sequences.push_back(*I);
617 I = UnassignedSequences.erase(I);
618 continue;
619 }
620 ++I;
621 }
622
623 // Add a valid clone group to the list of found clone groups.
624 if (!FilteredGroup.isValid())
625 continue;
626
627 Result.push_back(FilteredGroup);
628 }
629}
630
Artem Dergachevba816322016-07-26 18:13:12 +0000631void CloneDetector::findClones(std::vector<CloneGroup> &Result,
Artem Dergachev2fc19852016-08-18 12:29:41 +0000632 unsigned MinGroupComplexity,
633 bool CheckPatterns) {
Artem Dergachevba816322016-07-26 18:13:12 +0000634 // Add every valid clone group that fulfills the complexity requirement.
635 for (const CloneGroup &Group : CloneGroups) {
636 if (Group.isValid() && Group.Complexity >= MinGroupComplexity) {
Artem Dergachev2fc19852016-08-18 12:29:41 +0000637 if (CheckPatterns)
638 createCloneGroups(Result, Group);
639 else
640 Result.push_back(Group);
Artem Dergachevba816322016-07-26 18:13:12 +0000641 }
642 }
643
644 std::vector<unsigned> IndexesToRemove;
645
646 // Compare every group in the result with the rest. If one groups contains
647 // another group, we only need to return the bigger group.
648 // Note: This doesn't scale well, so if possible avoid calling any heavy
649 // function from this loop to minimize the performance impact.
650 for (unsigned i = 0; i < Result.size(); ++i) {
651 for (unsigned j = 0; j < Result.size(); ++j) {
652 // Don't compare a group with itself.
653 if (i == j)
654 continue;
655
656 if (containsGroup(Result[j], Result[i])) {
657 IndexesToRemove.push_back(i);
658 break;
659 }
660 }
661 }
662
663 // Erasing a list of indexes from the vector should be done with decreasing
664 // indexes. As IndexesToRemove is constructed with increasing values, we just
665 // reverse iterate over it to get the desired order.
666 for (auto I = IndexesToRemove.rbegin(); I != IndexesToRemove.rend(); ++I) {
667 Result.erase(Result.begin() + *I);
668 }
669}
Artem Dergachev2fc19852016-08-18 12:29:41 +0000670
671void CloneDetector::findSuspiciousClones(
672 std::vector<CloneDetector::SuspiciousClonePair> &Result,
673 unsigned MinGroupComplexity) {
674 std::vector<CloneGroup> Clones;
675 // Reuse the normal search for clones but specify that the clone groups don't
676 // need to have a common referenced variable pattern so that we can manually
677 // search for the kind of pattern errors this function is supposed to find.
678 findClones(Clones, MinGroupComplexity, false);
679
680 for (const CloneGroup &Group : Clones) {
681 for (unsigned i = 0; i < Group.Sequences.size(); ++i) {
682 VariablePattern PatternA(Group.Sequences[i]);
683
684 for (unsigned j = i + 1; j < Group.Sequences.size(); ++j) {
685 VariablePattern PatternB(Group.Sequences[j]);
686
687 CloneDetector::SuspiciousClonePair ClonePair;
688 // For now, we only report clones which break the variable pattern just
689 // once because multiple differences in a pattern are an indicator that
690 // those differences are maybe intended (e.g. because it's actually
691 // a different algorithm).
692 // TODO: In very big clones even multiple variables can be unintended,
693 // so replacing this number with a percentage could better handle such
694 // cases. On the other hand it could increase the false-positive rate
695 // for all clones if the percentage is too high.
696 if (PatternA.countPatternDifferences(PatternB, &ClonePair) == 1) {
697 Result.push_back(ClonePair);
698 break;
699 }
700 }
701 }
702 }
703}