PMD 복사 붙여 넣기 감지기 (CPD)를 사용하여 C 및 C++ 코드를 분석합니다. 그러나 매우 유사한 몇 가지 코드가 있지만 좋은 이유가 있으며 이러한 부분에 대한 경고를 표시하지 않으려합니다.C/C++ 코드의 CPD에서 경고 표시 안 함
documentation of PMD CPD에는 특수 효과에 대해서만 언급되어 있지만이 언어에서는 작동하지 않습니다.
어떻게하면 특정 부품에 대한 경고를 무시할 수 있습니까?
아마도 그렇게 할 코멘트가 있습니까?
[업데이트] 내가 CPD 실행하려면 다음 그루비 스크립트를 사용하고 : GitHub의에 PMD의 코드를 검색 한 후
@GrabResolver(name = 'jcenter', root = 'https://jcenter.bintray.com/')
@Grab('net.sourceforge.pmd:pmd-core:5.4.+')
@Grab('net.sourceforge.pmd:pmd-cpp:5.4.+')
import net.sourceforge.pmd.cpd.CPD
import net.sourceforge.pmd.cpd.CPDConfiguration
import java.util.regex.Pattern
def tokens = 60
def scanDirs = ['./path/to/scan', './scan/this/too']
def ignores = [
'./ignore/this/path',
'./this/must/be/ignored/too'
].collect({ it.replace('/', File.separator) })
def rootDir = new File('.')
def outputDir = new File('./reports/analysis/')
def filename_date_format = 'yyyyMMdd'
def encoding = System.getProperty('file.encoding')
def language_converter = new CPDConfiguration.LanguageConverter()
def config = new CPDConfiguration()
config.language = new CPDConfiguration.LanguageConverter().convert('c')
config.minimumTileSize = tokens
config.renderer = config.getRendererFromString 'xml', 'UTF-8'
config.skipBlocksPattern = '//DUPSTOP|//DUPSTART'
config.skipLexicalErrors = true
def cpd = new CPD(config)
scanDirs.each { path ->
def dir = new File(path);
dir.eachFileRecurse(groovy.io.FileType.FILES) {
// Ignore file?
def doIgnore = false
ignores.each { ignore ->
if(it.path.startsWith(ignore)) {
doIgnore = true
}
}
if(doIgnore) {
return
}
// Other checks
def lowerCaseName = it.name.toLowerCase()
if(lowerCaseName.endsWith('.c') || lowerCaseName.endsWith('.cpp') || lowerCaseName.endsWith('.h')) {
cpd.add it
}
}
}
cpd.go();
def duplicationFound = cpd.matches.hasNext()
def now = new Date().format(filename_date_format)
def outputFile = new File(outputDir.canonicalFile, "cpd_report_${now}.xml")
println "Saving report to ${outputFile.absolutePath}"
def absoluteRootDir = rootDir.canonicalPath
if(absoluteRootDir[-1] != File.separator) {
absoluteRootDir += File.separator
}
outputFile.parentFile.mkdirs()
def xmlOutput = config.renderer.render(cpd.matches);
if(duplicationFound) {
def filePattern = "(<file\\s+line=\"\\d+\"\\s+path=\")${Pattern.quote(absoluteRootDir)}([^\"]+\"\\s*/>)"
xmlOutput = xmlOutput.replaceAll(filePattern, '$1$2')
} else {
println 'No duplication found.'
}
outputFile.write xmlOutput
불행합니다. 그러나 PMD *는 결국 Java 용으로 설계되었습니다. 어쩌면 Clang 정적 분석기 (또는 다른 도구)가 더 나은 사용자 정의를 제공 할 수 있습니까? – StoryTeller
나는 Clang의 코드 복사/복사 붙여 넣기 감지기에 대해 알지 못하므로이 방법이 아닌 것으로 알고 있습니다. –