1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/Analysis/CloneDetection.h" |
14 | |
15 | #include "clang/AST/DataCollection.h" |
16 | #include "clang/AST/DeclTemplate.h" |
17 | #include "llvm/Support/MD5.h" |
18 | #include "llvm/Support/Path.h" |
19 | |
20 | using namespace clang; |
21 | |
22 | StmtSequence::StmtSequence(const CompoundStmt *Stmt, const Decl *D, |
23 | unsigned StartIndex, unsigned EndIndex) |
24 | : S(Stmt), D(D), StartIndex(StartIndex), EndIndex(EndIndex) { |
25 | (0) . __assert_fail ("Stmt && \"Stmt must not be a nullptr\"", "/home/seafit/code_projects/clang_source/clang/lib/Analysis/CloneDetection.cpp", 25, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Stmt && "Stmt must not be a nullptr"); |
26 | (0) . __assert_fail ("StartIndex < EndIndex && \"Given array should not be empty\"", "/home/seafit/code_projects/clang_source/clang/lib/Analysis/CloneDetection.cpp", 26, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(StartIndex < EndIndex && "Given array should not be empty"); |
27 | (0) . __assert_fail ("EndIndex <= Stmt->size() && \"Given array too big for this Stmt\"", "/home/seafit/code_projects/clang_source/clang/lib/Analysis/CloneDetection.cpp", 27, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(EndIndex <= Stmt->size() && "Given array too big for this Stmt"); |
28 | } |
29 | |
30 | StmtSequence::StmtSequence(const Stmt *Stmt, const Decl *D) |
31 | : S(Stmt), D(D), StartIndex(0), EndIndex(0) {} |
32 | |
33 | StmtSequence::StmtSequence() |
34 | : S(nullptr), D(nullptr), StartIndex(0), EndIndex(0) {} |
35 | |
36 | bool StmtSequence::contains(const StmtSequence &Other) const { |
37 | |
38 | |
39 | if (D != Other.D) |
40 | return false; |
41 | |
42 | const SourceManager &SM = getASTContext().getSourceManager(); |
43 | |
44 | |
45 | |
46 | bool StartIsInBounds = |
47 | SM.isBeforeInTranslationUnit(getBeginLoc(), Other.getBeginLoc()) || |
48 | getBeginLoc() == Other.getBeginLoc(); |
49 | if (!StartIsInBounds) |
50 | return false; |
51 | |
52 | bool EndIsInBounds = |
53 | SM.isBeforeInTranslationUnit(Other.getEndLoc(), getEndLoc()) || |
54 | Other.getEndLoc() == getEndLoc(); |
55 | return EndIsInBounds; |
56 | } |
57 | |
58 | StmtSequence::iterator StmtSequence::begin() const { |
59 | if (!holdsSequence()) { |
60 | return &S; |
61 | } |
62 | auto CS = cast<CompoundStmt>(S); |
63 | return CS->body_begin() + StartIndex; |
64 | } |
65 | |
66 | StmtSequence::iterator StmtSequence::end() const { |
67 | if (!holdsSequence()) { |
68 | return reinterpret_cast<StmtSequence::iterator>(&S) + 1; |
69 | } |
70 | auto CS = cast<CompoundStmt>(S); |
71 | return CS->body_begin() + EndIndex; |
72 | } |
73 | |
74 | ASTContext &StmtSequence::getASTContext() const { |
75 | assert(D); |
76 | return D->getASTContext(); |
77 | } |
78 | |
79 | SourceLocation StmtSequence::getBeginLoc() const { |
80 | return front()->getBeginLoc(); |
81 | } |
82 | |
83 | SourceLocation StmtSequence::getEndLoc() const { return back()->getEndLoc(); } |
84 | |
85 | SourceRange StmtSequence::getSourceRange() const { |
86 | return SourceRange(getBeginLoc(), getEndLoc()); |
87 | } |
88 | |
89 | void CloneDetector::analyzeCodeBody(const Decl *D) { |
90 | assert(D); |
91 | hasBody()", "/home/seafit/code_projects/clang_source/clang/lib/Analysis/CloneDetection.cpp", 91, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D->hasBody()); |
92 | |
93 | Sequences.push_back(StmtSequence(D->getBody(), D)); |
94 | } |
95 | |
96 | |
97 | |
98 | static bool containsAnyInGroup(StmtSequence &Seq, |
99 | CloneDetector::CloneGroup &Group) { |
100 | for (StmtSequence &GroupSeq : Group) { |
101 | if (Seq.contains(GroupSeq)) |
102 | return true; |
103 | } |
104 | return false; |
105 | } |
106 | |
107 | |
108 | |
109 | static bool containsGroup(CloneDetector::CloneGroup &Group, |
110 | CloneDetector::CloneGroup &OtherGroup) { |
111 | |
112 | |
113 | |
114 | |
115 | if (Group.size() < OtherGroup.size()) |
116 | return false; |
117 | |
118 | for (StmtSequence &Stmt : Group) { |
119 | if (!containsAnyInGroup(Stmt, OtherGroup)) |
120 | return false; |
121 | } |
122 | return true; |
123 | } |
124 | |
125 | void OnlyLargestCloneConstraint::constrain( |
126 | std::vector<CloneDetector::CloneGroup> &Result) { |
127 | std::vector<unsigned> IndexesToRemove; |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | for (unsigned i = 0; i < Result.size(); ++i) { |
134 | for (unsigned j = 0; j < Result.size(); ++j) { |
135 | |
136 | if (i == j) |
137 | continue; |
138 | |
139 | if (containsGroup(Result[j], Result[i])) { |
140 | IndexesToRemove.push_back(i); |
141 | break; |
142 | } |
143 | } |
144 | } |
145 | |
146 | |
147 | |
148 | |
149 | for (auto I = IndexesToRemove.rbegin(); I != IndexesToRemove.rend(); ++I) { |
150 | Result.erase(Result.begin() + *I); |
151 | } |
152 | } |
153 | |
154 | bool FilenamePatternConstraint::isAutoGenerated( |
155 | const CloneDetector::CloneGroup &Group) { |
156 | std::string Error; |
157 | if (IgnoredFilesPattern.empty() || Group.empty() || |
158 | !IgnoredFilesRegex->isValid(Error)) |
159 | return false; |
160 | |
161 | for (const StmtSequence &S : Group) { |
162 | const SourceManager &SM = S.getASTContext().getSourceManager(); |
163 | StringRef Filename = llvm::sys::path::filename( |
164 | SM.getFilename(S.getContainingDecl()->getLocation())); |
165 | if (IgnoredFilesRegex->match(Filename)) |
166 | return true; |
167 | } |
168 | |
169 | return false; |
170 | } |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | namespace { |
180 | template <class T> |
181 | class CloneTypeIIStmtDataCollector |
182 | : public ConstStmtVisitor<CloneTypeIIStmtDataCollector<T>> { |
183 | ASTContext &Context; |
184 | |
185 | T &DataConsumer; |
186 | |
187 | template <class Ty> void addData(const Ty &Data) { |
188 | data_collection::addDataToConsumer(DataConsumer, Data); |
189 | } |
190 | |
191 | public: |
192 | CloneTypeIIStmtDataCollector(const Stmt *S, ASTContext &Context, |
193 | T &DataConsumer) |
194 | : Context(Context), DataConsumer(DataConsumer) { |
195 | this->Visit(S); |
196 | } |
197 | |
198 | |
199 | |
200 | |
201 | #define DEF_ADD_DATA(CLASS, CODE) \ |
202 | template <class = void> void Visit##CLASS(const CLASS *S) { \ |
203 | CODE; \ |
204 | ConstStmtVisitor<CloneTypeIIStmtDataCollector<T>>::Visit##CLASS(S); \ |
205 | } |
206 | |
207 | #include "clang/AST/StmtDataCollectors.inc" |
208 | |
209 | |
210 | #define SKIP(CLASS) \ |
211 | void Visit##CLASS(const CLASS *S) { \ |
212 | ConstStmtVisitor<CloneTypeIIStmtDataCollector<T>>::Visit##CLASS(S); \ |
213 | } |
214 | SKIP(DeclRefExpr) |
215 | SKIP(MemberExpr) |
216 | SKIP(IntegerLiteral) |
217 | SKIP(FloatingLiteral) |
218 | SKIP(StringLiteral) |
219 | SKIP(CXXBoolLiteralExpr) |
220 | SKIP(CharacterLiteral) |
221 | #undef SKIP |
222 | }; |
223 | } |
224 | |
225 | static size_t createHash(llvm::MD5 &Hash) { |
226 | size_t HashCode; |
227 | |
228 | |
229 | llvm::MD5::MD5Result HashResult; |
230 | Hash.final(HashResult); |
231 | |
232 | |
233 | |
234 | std::memcpy(&HashCode, &HashResult, |
235 | std::min(sizeof(HashCode), sizeof(HashResult))); |
236 | |
237 | return HashCode; |
238 | } |
239 | |
240 | |
241 | |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | static size_t |
250 | saveHash(const Stmt *S, const Decl *D, |
251 | std::vector<std::pair<size_t, StmtSequence>> &StmtsByHash) { |
252 | llvm::MD5 Hash; |
253 | ASTContext &Context = D->getASTContext(); |
254 | |
255 | CloneTypeIIStmtDataCollector<llvm::MD5>(S, Context, Hash); |
256 | |
257 | auto CS = dyn_cast<CompoundStmt>(S); |
258 | SmallVector<size_t, 8> ChildHashes; |
259 | |
260 | for (const Stmt *Child : S->children()) { |
261 | if (Child == nullptr) { |
262 | ChildHashes.push_back(0); |
263 | continue; |
264 | } |
265 | size_t ChildHash = saveHash(Child, D, StmtsByHash); |
266 | Hash.update( |
267 | StringRef(reinterpret_cast<char *>(&ChildHash), sizeof(ChildHash))); |
268 | ChildHashes.push_back(ChildHash); |
269 | } |
270 | |
271 | if (CS) { |
272 | |
273 | |
274 | |
275 | for (unsigned Pos = 0; Pos < CS->size(); ++Pos) { |
276 | |
277 | |
278 | |
279 | llvm::MD5 Hash; |
280 | for (unsigned Length = 1; Length <= CS->size() - Pos; ++Length) { |
281 | |
282 | |
283 | size_t ChildHash = ChildHashes[Pos + Length - 1]; |
284 | Hash.update( |
285 | StringRef(reinterpret_cast<char *>(&ChildHash), sizeof(ChildHash))); |
286 | |
287 | |
288 | if (Length > 1) { |
289 | llvm::MD5 SubHash = Hash; |
290 | StmtsByHash.push_back(std::make_pair( |
291 | createHash(SubHash), StmtSequence(CS, D, Pos, Pos + Length))); |
292 | } |
293 | } |
294 | } |
295 | } |
296 | |
297 | size_t HashCode = createHash(Hash); |
298 | StmtsByHash.push_back(std::make_pair(HashCode, StmtSequence(S, D))); |
299 | return HashCode; |
300 | } |
301 | |
302 | namespace { |
303 | |
304 | |
305 | class FoldingSetNodeIDWrapper { |
306 | |
307 | llvm::FoldingSetNodeID &FS; |
308 | |
309 | public: |
310 | FoldingSetNodeIDWrapper(llvm::FoldingSetNodeID &FS) : FS(FS) {} |
311 | |
312 | void update(StringRef Str) { FS.AddString(Str); } |
313 | }; |
314 | } |
315 | |
316 | |
317 | |
318 | static void CollectStmtSequenceData(const StmtSequence &Sequence, |
319 | FoldingSetNodeIDWrapper &OutputData) { |
320 | for (const Stmt *S : Sequence) { |
321 | CloneTypeIIStmtDataCollector<FoldingSetNodeIDWrapper>( |
322 | S, Sequence.getASTContext(), OutputData); |
323 | |
324 | for (const Stmt *Child : S->children()) { |
325 | if (!Child) |
326 | continue; |
327 | |
328 | CollectStmtSequenceData(StmtSequence(Child, Sequence.getContainingDecl()), |
329 | OutputData); |
330 | } |
331 | } |
332 | } |
333 | |
334 | |
335 | static bool areSequencesClones(const StmtSequence &LHS, |
336 | const StmtSequence &RHS) { |
337 | |
338 | |
339 | |
340 | |
341 | llvm::FoldingSetNodeID DataLHS, DataRHS; |
342 | FoldingSetNodeIDWrapper LHSWrapper(DataLHS); |
343 | FoldingSetNodeIDWrapper RHSWrapper(DataRHS); |
344 | |
345 | CollectStmtSequenceData(LHS, LHSWrapper); |
346 | CollectStmtSequenceData(RHS, RHSWrapper); |
347 | |
348 | return DataLHS == DataRHS; |
349 | } |
350 | |
351 | void RecursiveCloneTypeIIHashConstraint::constrain( |
352 | std::vector<CloneDetector::CloneGroup> &Sequences) { |
353 | |
354 | std::vector<CloneDetector::CloneGroup> Result; |
355 | |
356 | for (CloneDetector::CloneGroup &Group : Sequences) { |
357 | |
358 | |
359 | if (Group.empty()) |
360 | continue; |
361 | |
362 | std::vector<std::pair<size_t, StmtSequence>> StmtsByHash; |
363 | |
364 | |
365 | for (const StmtSequence &S : Group) { |
366 | saveHash(S.front(), S.getContainingDecl(), StmtsByHash); |
367 | } |
368 | |
369 | |
370 | std::stable_sort(StmtsByHash.begin(), StmtsByHash.end(), |
371 | [](std::pair<size_t, StmtSequence> LHS, |
372 | std::pair<size_t, StmtSequence> RHS) { |
373 | return LHS.first < RHS.first; |
374 | }); |
375 | |
376 | |
377 | |
378 | |
379 | |
380 | for (unsigned i = 0; i < StmtsByHash.size() - 1; ++i) { |
381 | const auto Current = StmtsByHash[i]; |
382 | |
383 | |
384 | |
385 | |
386 | CloneDetector::CloneGroup NewGroup; |
387 | |
388 | size_t PrototypeHash = Current.first; |
389 | |
390 | for (; i < StmtsByHash.size(); ++i) { |
391 | |
392 | if (PrototypeHash != StmtsByHash[i].first) { |
393 | |
394 | |
395 | |
396 | |
397 | assert(i != 0); |
398 | --i; |
399 | break; |
400 | } |
401 | |
402 | |
403 | NewGroup.push_back(StmtsByHash[i].second); |
404 | } |
405 | |
406 | |
407 | |
408 | Result.push_back(NewGroup); |
409 | } |
410 | } |
411 | |
412 | Sequences = Result; |
413 | } |
414 | |
415 | void RecursiveCloneTypeIIVerifyConstraint::constrain( |
416 | std::vector<CloneDetector::CloneGroup> &Sequences) { |
417 | CloneConstraint::splitCloneGroups( |
418 | Sequences, [](const StmtSequence &A, const StmtSequence &B) { |
419 | return areSequencesClones(A, B); |
420 | }); |
421 | } |
422 | |
423 | size_t MinComplexityConstraint::calculateStmtComplexity( |
424 | const StmtSequence &Seq, std::size_t Limit, |
425 | const std::string &ParentMacroStack) { |
426 | if (Seq.empty()) |
427 | return 0; |
428 | |
429 | size_t Complexity = 1; |
430 | |
431 | ASTContext &Context = Seq.getASTContext(); |
432 | |
433 | |
434 | std::string MacroStack = |
435 | data_collection::getMacroStack(Seq.getBeginLoc(), Context); |
436 | |
437 | |
438 | |
439 | |
440 | |
441 | |
442 | |
443 | |
444 | |
445 | if (!ParentMacroStack.empty() && MacroStack == ParentMacroStack) { |
446 | Complexity = 0; |
447 | } |
448 | |
449 | |
450 | |
451 | if (Seq.holdsSequence()) { |
452 | for (const Stmt *S : Seq) { |
453 | Complexity += calculateStmtComplexity( |
454 | StmtSequence(S, Seq.getContainingDecl()), Limit, MacroStack); |
455 | if (Complexity >= Limit) |
456 | return Limit; |
457 | } |
458 | } else { |
459 | for (const Stmt *S : Seq.front()->children()) { |
460 | Complexity += calculateStmtComplexity( |
461 | StmtSequence(S, Seq.getContainingDecl()), Limit, MacroStack); |
462 | if (Complexity >= Limit) |
463 | return Limit; |
464 | } |
465 | } |
466 | return Complexity; |
467 | } |
468 | |
469 | void MatchingVariablePatternConstraint::constrain( |
470 | std::vector<CloneDetector::CloneGroup> &CloneGroups) { |
471 | CloneConstraint::splitCloneGroups( |
472 | CloneGroups, [](const StmtSequence &A, const StmtSequence &B) { |
473 | VariablePattern PatternA(A); |
474 | VariablePattern PatternB(B); |
475 | return PatternA.countPatternDifferences(PatternB) == 0; |
476 | }); |
477 | } |
478 | |
479 | void CloneConstraint::splitCloneGroups( |
480 | std::vector<CloneDetector::CloneGroup> &CloneGroups, |
481 | llvm::function_ref<bool(const StmtSequence &, const StmtSequence &)> |
482 | Compare) { |
483 | std::vector<CloneDetector::CloneGroup> Result; |
484 | for (auto &HashGroup : CloneGroups) { |
485 | |
486 | |
487 | std::vector<char> Indexes; |
488 | Indexes.resize(HashGroup.size()); |
489 | |
490 | for (unsigned i = 0; i < HashGroup.size(); ++i) { |
491 | |
492 | if (Indexes[i]) |
493 | continue; |
494 | |
495 | |
496 | |
497 | |
498 | |
499 | StmtSequence Prototype = HashGroup[i]; |
500 | CloneDetector::CloneGroup PotentialGroup = {Prototype}; |
501 | ++Indexes[i]; |
502 | |
503 | |
504 | for (unsigned j = i + 1; j < HashGroup.size(); ++j) { |
505 | |
506 | if (Indexes[j]) |
507 | continue; |
508 | |
509 | |
510 | const StmtSequence &Candidate = HashGroup[j]; |
511 | |
512 | if (!Compare(Prototype, Candidate)) |
513 | continue; |
514 | |
515 | PotentialGroup.push_back(Candidate); |
516 | |
517 | ++Indexes[j]; |
518 | } |
519 | |
520 | |
521 | |
522 | Result.push_back(PotentialGroup); |
523 | } |
524 | |
525 | assert(llvm::all_of(Indexes, [](char c) { return c == 1; })); |
526 | } |
527 | CloneGroups = Result; |
528 | } |
529 | |
530 | void VariablePattern::addVariableOccurence(const VarDecl *VarDecl, |
531 | const Stmt *Mention) { |
532 | |
533 | for (size_t KindIndex = 0; KindIndex < Variables.size(); ++KindIndex) { |
534 | if (Variables[KindIndex] == VarDecl) { |
535 | |
536 | |
537 | Occurences.emplace_back(KindIndex, Mention); |
538 | return; |
539 | } |
540 | } |
541 | |
542 | |
543 | Occurences.emplace_back(Variables.size(), Mention); |
544 | Variables.push_back(VarDecl); |
545 | } |
546 | |
547 | void VariablePattern::addVariables(const Stmt *S) { |
548 | |
549 | |
550 | |
551 | if (!S) |
552 | return; |
553 | |
554 | |
555 | if (auto D = dyn_cast<DeclRefExpr>(S)) { |
556 | if (auto VD = dyn_cast<VarDecl>(D->getDecl()->getCanonicalDecl())) |
557 | addVariableOccurence(VD, D); |
558 | } |
559 | |
560 | |
561 | for (const Stmt *Child : S->children()) { |
562 | addVariables(Child); |
563 | } |
564 | } |
565 | |
566 | unsigned VariablePattern::countPatternDifferences( |
567 | const VariablePattern &Other, |
568 | VariablePattern::SuspiciousClonePair *FirstMismatch) { |
569 | unsigned NumberOfDifferences = 0; |
570 | |
571 | assert(Other.Occurences.size() == Occurences.size()); |
572 | for (unsigned i = 0; i < Occurences.size(); ++i) { |
573 | auto ThisOccurence = Occurences[i]; |
574 | auto OtherOccurence = Other.Occurences[i]; |
575 | if (ThisOccurence.KindID == OtherOccurence.KindID) |
576 | continue; |
577 | |
578 | ++NumberOfDifferences; |
579 | |
580 | |
581 | |
582 | if (FirstMismatch == nullptr) |
583 | continue; |
584 | |
585 | |
586 | |
587 | if (NumberOfDifferences != 1) |
588 | continue; |
589 | |
590 | const VarDecl *FirstSuggestion = nullptr; |
591 | |
592 | |
593 | |
594 | if (OtherOccurence.KindID < Variables.size()) |
595 | FirstSuggestion = Variables[OtherOccurence.KindID]; |
596 | |
597 | |
598 | FirstMismatch->FirstCloneInfo = |
599 | VariablePattern::SuspiciousClonePair::SuspiciousCloneInfo( |
600 | Variables[ThisOccurence.KindID], ThisOccurence.Mention, |
601 | FirstSuggestion); |
602 | |
603 | |
604 | |
605 | |
606 | const VarDecl *SecondSuggestion = nullptr; |
607 | if (ThisOccurence.KindID < Other.Variables.size()) |
608 | SecondSuggestion = Other.Variables[ThisOccurence.KindID]; |
609 | |
610 | |
611 | FirstMismatch->SecondCloneInfo = |
612 | VariablePattern::SuspiciousClonePair::SuspiciousCloneInfo( |
613 | Other.Variables[OtherOccurence.KindID], OtherOccurence.Mention, |
614 | SecondSuggestion); |
615 | |
616 | |
617 | |
618 | |
619 | |
620 | |
621 | if (!FirstMismatch->FirstCloneInfo.Suggestion) |
622 | std::swap(FirstMismatch->FirstCloneInfo, FirstMismatch->SecondCloneInfo); |
623 | |
624 | |
625 | FirstCloneInfo.Suggestion", "/home/seafit/code_projects/clang_source/clang/lib/Analysis/CloneDetection.cpp", 625, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FirstMismatch->FirstCloneInfo.Suggestion); |
626 | } |
627 | |
628 | return NumberOfDifferences; |
629 | } |
630 | |