Index: include/clang/Analysis/CloneDetection.h =================================================================== --- include/clang/Analysis/CloneDetection.h +++ include/clang/Analysis/CloneDetection.h @@ -318,6 +318,10 @@ void constrain(std::vector &Result); }; +struct AutoGeneratedCloneConstraint { + void constrain(std::vector &Result); +}; + /// Analyzes the pattern of the referenced variables in a statement. class VariablePattern { Index: lib/Analysis/CloneDetection.cpp =================================================================== --- lib/Analysis/CloneDetection.cpp +++ lib/Analysis/CloneDetection.cpp @@ -366,6 +366,39 @@ } } +void AutoGeneratedCloneConstraint::constrain( + std::vector &Result) { + std::vector IndexesToRemove; + unsigned i = 0; + + for (CloneDetector::CloneGroup &Group : Result) { + if (Group.empty()) { + i++; + continue; + } + + for (const StmtSequence &S : Group) { + const Decl *D = S.getContainingDecl(); + SourceLocation sLoc = D->getLocation(); + const SourceManager &SM = D->getASTContext().getSourceManager(); + FileID fileID = SM.getFileID(sLoc); + const FileEntry *fileEntry = SM.getFileEntryForID(fileID); + if (fileEntry && fileEntry->isValid()) { + if (std::string(fileEntry->getName()).find("moc_") == 0) { + IndexesToRemove.push_back(i); + break; + } + } + } + + i++; + } + + for (auto I = IndexesToRemove.rbegin(); I != IndexesToRemove.rend(); ++I) { + Result.erase(Result.begin() + *I); + } +} + static size_t createHash(llvm::MD5 &Hash) { size_t HashCode; Index: lib/StaticAnalyzer/Checkers/CloneChecker.cpp =================================================================== --- lib/StaticAnalyzer/Checkers/CloneChecker.cpp +++ lib/StaticAnalyzer/Checkers/CloneChecker.cpp @@ -78,7 +78,8 @@ // because reportSuspiciousClones() wants to search them for errors. std::vector AllCloneGroups; - Detector.findClones(AllCloneGroups, RecursiveCloneTypeIIConstraint(), + Detector.findClones(AllCloneGroups, AutoGeneratedCloneConstraint(), + RecursiveCloneTypeIIConstraint(), MinComplexityConstraint(MinComplexity), MinGroupSizeConstraint(2), OnlyLargestCloneConstraint()); @@ -93,6 +94,7 @@ // Now that the suspicious clone detector has checked for pattern errors, // we also filter all clones who don't have matching patterns CloneDetector::constrainClones(AllCloneGroups, + AutoGeneratedCloneConstraint(), MatchingVariablePatternConstraint(), MinGroupSizeConstraint(2)); Index: unittests/Analysis/CloneDetectionTest.cpp =================================================================== --- unittests/Analysis/CloneDetectionTest.cpp +++ unittests/Analysis/CloneDetectionTest.cpp @@ -72,7 +72,8 @@ Detector.findClones(CloneGroups, NoBarFunctionConstraint(), RecursiveCloneTypeIIConstraint(), MinComplexityConstraint(2), MinGroupSizeConstraint(2), - OnlyLargestCloneConstraint()); + OnlyLargestCloneConstraint(), + AutoGeneratedCloneConstraint()); ASSERT_EQ(CloneGroups.size(), 1u); ASSERT_EQ(CloneGroups.front().size(), 2u); @@ -89,7 +90,8 @@ Detector.findClones(CloneGroups, RecursiveCloneTypeIIConstraint(), MinComplexityConstraint(2), MinGroupSizeConstraint(2), - OnlyLargestCloneConstraint()); + OnlyLargestCloneConstraint(), + AutoGeneratedCloneConstraint()); ASSERT_EQ(CloneGroups.size(), 1u); ASSERT_EQ(CloneGroups.front().size(), 4u);