Index: include/clang/Analysis/CloneDetection.h =================================================================== --- include/clang/Analysis/CloneDetection.h +++ include/clang/Analysis/CloneDetection.h @@ -28,6 +28,7 @@ class VarDecl; class ASTContext; class CompoundStmt; +class FileEntry; /// \brief Identifies a list of statements. /// @@ -278,6 +279,13 @@ void findSuspiciousClones(std::vector &Result, unsigned MinGroupComplexity); + /// \brief Get the FileEntry for Decl. + /// \param Decl. + /// \param SourceManager. + /// \return FileEntry. + const clang::FileEntry *getFileEntryForDecl(const clang::Decl *decl, + clang::SourceManager *sourceManager); + private: /// Stores all encountered StmtSequences alongside their CloneSignature. std::vector> Sequences; Index: lib/Analysis/CloneDetection.cpp =================================================================== --- lib/Analysis/CloneDetection.cpp +++ lib/Analysis/CloneDetection.cpp @@ -602,8 +602,24 @@ }; } // end anonymous namespace +const clang::FileEntry *CloneDetector::getFileEntryForDecl(const clang::Decl *decl, + clang::SourceManager * sourceManager) { + if (!decl || !sourceManager) + return nullptr; + clang::SourceLocation sLoc = decl->getLocation(); + clang::FileID fileID = sourceManager->getFileID(sLoc); + return sourceManager->getFileEntryForID(fileID); +} + void CloneDetector::analyzeCodeBody(const Decl *D) { assert(D); + assert(D->getASTContext()); + const clang::FileEntry *fileEntry = getFileEntryForDecl(D, + &D->getASTContext().getSourceManager()); + // Filter Qt's Meta-Object Compiler (moc) http://doc.qt.io/qt-5/moc.html + // The false positive is too high for moc_XXX.cpp during scan-build + if (fileEntry && std::string(fileEntry->getName()).find("moc_") == 0) + return; assert(D->hasBody()); CloneSignatureGenerator Generator(*this, D->getASTContext()); Generator.consumeCodeBody(D->getBody());