xiangzhai updated this revision to Diff 97059. xiangzhai added a comment. Hi Artem,
Thanks for your review! > Because LLVM's Illinois license is rather permissive than copyleft, we try to > avoid stuff copied from GPL/LGPL software (such as Qt or K3B) in our > codebase. The code doesn't seem to have been copy-pasted from a copyleft > project, but I guess it's better to rename the file to avoid referencing to > K3B. Sorry for my mistake! I have rename the testcase to moc_autogenerated.cpp and autogenerated_automoc.cpp. I am the maintainer of K3B, if you find any bug when burning ISO please report bug to me :) > Other LLVM contributors are free to edit this file, and i doubt it was > autogenerated; i believe these comments should be removed. Sorry for my fault! /* This file is autogenerated, do not edit*/ it is autogenerated by MOC, I removed it! And I use a single line LLVM regex instead of std::string functions, please review my patch, thanks a lot! Regards, Leslie Zhai Repository: rL LLVM https://reviews.llvm.org/D31320 Files: include/clang/Analysis/CloneDetection.h lib/Analysis/CloneDetection.cpp lib/StaticAnalyzer/Checkers/CloneChecker.cpp test/Analysis/copypaste/autogenerated_automoc.cpp test/Analysis/copypaste/moc_autogenerated.cpp
Index: test/Analysis/copypaste/autogenerated_automoc.cpp =================================================================== --- test/Analysis/copypaste/autogenerated_automoc.cpp +++ test/Analysis/copypaste/autogenerated_automoc.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=alpha.clone.CloneChecker -verify %s + +// Because files that have `_automoc.' in their names are most likely autogenerated, +// we suppress copy-paste warnings here. + +// expected-no-diagnostics + +void f1() { + int *p1 = new int[1]; + int *p2 = new int[1]; + if (p1) { + delete [] p1; + p1 = nullptr; + } + if (p2) { + delete [] p1; // no-warning + p2 = nullptr; + } +} Index: test/Analysis/copypaste/moc_autogenerated.cpp =================================================================== --- test/Analysis/copypaste/moc_autogenerated.cpp +++ test/Analysis/copypaste/moc_autogenerated.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=alpha.clone.CloneChecker -verify %s + +// Because files that have `moc_' in their names are most likely autogenerated, +// we suppress copy-paste warnings here. + +// expected-no-diagnostics + +void f1() { + int *p1 = new int[1]; + int *p2 = new int[1]; + if (p1) { + delete [] p1; + p1 = nullptr; + } + if (p2) { + delete [] p1; // no-warning + p2 = nullptr; + } +} 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<CloneDetector::CloneGroup> AllCloneGroups; - Detector.findClones(AllCloneGroups, RecursiveCloneTypeIIConstraint(), + Detector.findClones(AllCloneGroups, AutoGeneratedCloneConstraint(), + RecursiveCloneTypeIIConstraint(), MinComplexityConstraint(MinComplexity), MinGroupSizeConstraint(2), OnlyLargestCloneConstraint()); Index: lib/Analysis/CloneDetection.cpp =================================================================== --- lib/Analysis/CloneDetection.cpp +++ lib/Analysis/CloneDetection.cpp @@ -21,6 +21,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/MD5.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Regex.h" using namespace clang; @@ -366,6 +367,29 @@ } } +bool AutoGeneratedCloneConstraint::isAutoGenerated(const CloneDetector::CloneGroup &Group) { + if (Group.empty()) + return false; + + for (const StmtSequence &S : Group) { + const Decl *D = S.getContainingDecl(); + const SourceManager &SM = D->getASTContext().getSourceManager(); + std::string Filename = std::string(SM.getFilename(D->getLocation())); + // Get Basename + const size_t LastSlash = Filename.find_last_of("\\/"); + if (LastSlash != std::string::npos) + Filename.erase(0, LastSlash + 1); + const size_t LastDot = Filename.rfind('.'); + if (LastDot != std::string::npos) + Filename.erase(LastDot); + llvm::Regex R(StringRef("^(moc_|.*_automoc$)")); + if (R.match(StringRef(Filename))) + return true; + } + + return false; +} + static size_t createHash(llvm::MD5 &Hash) { size_t HashCode; Index: include/clang/Analysis/CloneDetection.h =================================================================== --- include/clang/Analysis/CloneDetection.h +++ include/clang/Analysis/CloneDetection.h @@ -319,6 +319,17 @@ void constrain(std::vector<CloneDetector::CloneGroup> &Result); }; +struct AutoGeneratedCloneConstraint { + bool isAutoGenerated(const CloneDetector::CloneGroup &Group); + + void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups) { + CloneConstraint::filterGroups( + CloneGroups, [this](const CloneDetector::CloneGroup &Group) { + return isAutoGenerated(Group); + }); + } +}; + /// Analyzes the pattern of the referenced variables in a statement. class VariablePattern {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits