7

我们正在使用 PMD 复制粘贴检测器 (CPD) 来分析我们的 C 和 C++ 代码。但是,有一些代码部分非常相似,但有充分的理由,我们想禁止这些部分的警告。

PMD CPD的文档只提到了一些关于注释的内容,但这不适用于我们的这些语言。

我怎样才能仍然忽略特定部分的警告?

有没有评论可以这样做?

[更新]我正在使用以下 Groovy 脚本来运行 CPD:

@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
4

4 回答 4

3

您可以通过该--skip-blocks-pattern选项定义自定义标记以从分析中排除某些块。

--skip-blocks-pattern模式查找要跳过的块。以 | 分隔的开始和结束模式。默认为#if 0|#endif

例如以下将忽略/* SUPPRESS CPD START *//* SUPPRESS CPD END */注释之间的块(注释必须占据单独的行):

$ ./run.sh cpd --minimum-tokens 100 --files /path/to/c/source --language cpp ----skip-blocks-pattern '/* SUPPRESS CPD START */|/* SUPPRESS CPD END */'

但是请注意,这将导致该工具在#if 0/分隔的代码内执行复制粘贴检测#endif

于 2016-07-04T08:33:53.140 回答
2

在 GitHub 上搜索 PMD 的代码后,我想我可以肯定地说目前支持此功能(当前版本为 PMD 5.5.0)。

在他们的存储库中搜索CPD-START,不会在pmd-cpp目录中显示任何结果(请参阅GitHub 上的搜索结果)。

于 2016-07-04T08:15:55.270 回答
1

我知道这是一个大约 3 年的问题,但为了完整起见,CPD 在 Java 的 PMD 5.6.0(2017 年 4 月)中开始支持这个问题,并且自 6.3.0(2018 年 4 月)以来,它已扩展到许多其他语言,例如C/C++。如今,几乎所有 CPD 支持的语言都允许基于评论的抑制。

基于评论的抑制的完整(当前)文档可在https://pmd.github.io/pmd-6.13.0/pmd_userdocs_cpd.html#suppression获得

值得注意的是,如果文件有// CPD-OFF注释,但没有匹配// CPD-ON的,所有内容都将被忽略,直到文件结束。

于 2019-04-17T03:39:55.317 回答
-1

我对 CPD 没有任何帮助。一般来说,我知道这些工具;我不明白有关“警告”的内容。

我们的CloneDR 工具可找到准确且几乎未命中的重复代码。恕我直言,它找到了比 CPD 更好的克隆,因为它使用语言语法/结构作为指导。[这一事实得到第三方研究报告的支持,您可以在网站上找到该报告]。而且它不会发出“警告”。

如果存在认为克隆中涉及的代码,该工具将为所涉及的克隆生成一个输出报告页面。但这不是警告。没有办法抑制举报行为。显然,如果您看过这样的克隆并认为它不感兴趣,您可以用注释标记其中一个克隆条目,说明它是无趣的克隆;该评论将显示在克隆报告中。(此类)评论对 CloneDR 检测到的克隆没有任何影响,因此添加它们不会更改计算的答案。

于 2016-07-05T08:27:02.413 回答