简体   繁体   English

禁止来自CPD的C / C ++代码警告

[英]Suppress warnings from CPD for C/C++ code

We are using PMD Copy Paste Detector (CPD) to analyze our C and C++ code. 我们正在使用PMD复制粘贴检测器(CPD)分析我们的C和C ++代码。 However, there are a few parts of the code that are very similar, but with a good reason and we would like to suppress the warnings for these parts. 但是,代码中有一些非常相似的部分,但是有充分的理由,我们希望取消这些部分的警告。

The documentation of PMD CPD only mentions something about annotations, but this will not work for our these languages. PMD CPD文档仅提及有关注释的内容,但这不适用于我们的这些语言。

How can I still ignore warnings for specific parts? 如何仍然忽略特定零件的警告?

Is there a comment to do so perhaps? 是否有评论可以这样做?

[UPDATE] I'm using the following Groovy script to run 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

You can define your custom markers for excluding certain blocks from analysis through the --skip-blocks-pattern option. 您可以通过--skip-blocks-pattern选项定义自定义标记,以从分析中排除某些块。

--skip-blocks-pattern Pattern to find the blocks to skip. --skip-blocks-pattern用于查找要跳过的块的模式 Start and End pattern separated by |. 起始和结束模式以|分隔。 Default is #if 0|#endif . 默认值为#if 0|#endif

For example the following will ignore blocks between /* SUPPRESS CPD START */ and /* SUPPRESS CPD END */ comments (the comment must occupy a separate line): 例如,以下内容将忽略/* 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 */'

Note however, that this will cause the tool perform copy-paste-detection inside code delimited by #if 0 / #endif . 但是请注意,这将导致该工具在以#if 0 / #endif分隔的代码内部执行复制粘贴检测。

After searching through the code of PMD on GitHub, I think I can safely say that this is NOT supported at this point in time (current version being PMD 5.5.0). 在GitHub上搜索PMD的代码后,我想可以确定地说,目前支持此功能(当前版本为PMD 5.5.0)。

A search for CPD-START in their repository, does not show any results within the pmd-cpp directory (see the search results on GitHub ). 在其存储库中搜索CPD-START不会在pmd-cpp目录中显示任何结果(请参阅GitHub上的搜索结果 )。

I know this is a ~3 years old question, but for completeness, CPD started supporting this in PMD 5.6.0 (April 2017) in Java, and since 6.3.0 (April 2018) it has been extended to many other languages such as C/C++. 我知道这是一个已有3年历史的问题,但是为了完整起见,CPD在Java的PMD 5.6.0(2017年4月)中开始支持此问题,并且自6.3.0(2018年4月)以来已扩展到许多其他语言,例如C / C ++。 Nowadays, almost all CPD supported languages allow for comment-based suppressions. 如今,几乎所有CPD支持的语言都允许基于注释的禁止。

The complete (current) docs for comment-based suppression are available at https://pmd.github.io/pmd-6.13.0/pmd_userdocs_cpd.html#suppression 有关基于注释的抑制的完整(当前)文档可在https://pmd.github.io/pmd-6.13.0/pmd_userdocs_cpd.html#suppression获得。

It's worth noting, if a file has a // CPD-OFF comment, but no matching // CPD-ON , everything will be ignored until the end of file. 值得注意的是,如果文件具有// CPD-OFF注释,但没有匹配的// CPD-ON ,则所有操作都将被忽略,直到文件结尾。

I don't have any help for CPD. 我对CPD没有任何帮助。 In general, I know about such tools; 总的来说,我知道这种工具。 I don't understand the bit about "warnings". 我不太了解“警告”。

Our CloneDR tool finds exact and near-miss duplicate code . 我们的CloneDR工具可找到准确且几乎未命中的重复代码 IMHO, it finds better clones than CPD, because it uses the language syntax/ structure as a guide. 恕我直言,它发现比CPD更好的克隆,因为它使用语言语法/结构作为指南。 [This fact is backed up by a research report done by a third party that you can find at the site]. [这一事实得到了您在站点上找到的第三方研究报告的支持]。 And it does not issue "warnings". 并且它不会发出“警告”。

If there is code that it thinks is involved in a clone, the tool will generate an output report page for the clones involved. 如果它认为是参与克隆代码,该工具会产生涉及克隆的输出报告。 But that isn't a warning. 但这不是警告。 There is no way to suppress the reporting behavior. 无法抑制报告行为。 Obviously, if you have seen such a clone and decide it is not interesting, you can mark one of the clone entries with a comment stating that it is an uninteresting clone; 显然,如果您看到这样的克隆并确定它不有趣,则可以在注释中标记一个克隆条目,说明它是一个无趣的克隆; that comment will show up in the clone report. 该评论将显示在克隆报告中。 (Such) comments have no impact whatsover on what clones are detected by CloneDR, so adding them does not change the computed answer. (这样)注释对CloneDR检测到的克隆没有影响,因此添加它们不会更改计算出的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM