【问题标题】:How to define a problem matcher for cppcheck task in vscode?如何在 vscode 中为 cppcheck 任务定义问题匹配器?
【发布时间】:2019-06-29 16:04:23
【问题描述】:

我正在为 vscode 设置一个“cppcheck”任务。它可以工作,但问题匹配器无法捕获问题。

我已经尝试过“$gcc”问题匹配器以及一些自定义配置。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cppcheck",
            "type": "shell",
            "command": "cppcheck --template=gcc --enable=style --project=${workspaceFolder}/build/compile_commands.json",
            "problemMatcher": "$gcc",
        }
    ]
}

或者这个:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cppcheck",
            "type": "shell",
            "command": "cppcheck --template=gcc --enable=warning src/jc_certreq.cpp",
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": "absolute",
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

例如终端显示这样的错误:

/home/user/workspace/project/myprogram.c:440: error: Memory leak: exts

但它没有出现在“问题”栏中。

【问题讨论】:

    标签: visual-studio-code cppcheck vscode-tasks


    【解决方案1】:

    我自己也在寻找解决方案,但只能找到这个悬而未决的问题。所以我继续进行了一些修改,最终让它工作了。

    问题似乎是旧版本的 Cppcheck(我使用的是 1.82 版)尚不支持打印列。看起来支持wasn't added until Cppcheck 1.84。但是,默认的$gcc 问题匹配器expects the column number to be present

    这是使用 gcc 模板时 Cppcheck 1.82 打印的消息示例:

    utilities/thread/condition.h:12: warning: The class 'Condition' has 'copy constructor' but lack of 'operator='.
    

    虽然 GCC 6.3 打印的错误如下所示:

    dump_tool.c:40:36: fatal error: generated/autoconf.h: No such file or directory
    

    它们几乎相同,只是缺少列号。只需从模式中删除它就可以解决我的问题。问题已匹配并显示在问题选项卡中。

    除此之外,我将severity 调整为warning,以便它们也出现在问题选项卡中,并且我将fileLocation 更改为relative,因为我正在使用相对路径。

    这是我用于项目的完整任务定义:

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            // ...
            {
                "label": "cppcheck",
                "group": "build",
                "type": "shell",
                "command": "cppcheck --template=gcc --enable=warning --std=c++03 --force .",
                "problemMatcher": {
                    "fileLocation": "relative",
                    "severity": "warning",
                    "pattern":{
                        "regexp": "^(.*):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "location": 2,
                        "severity": 3,
                        "message": 4
                    }
                }
            }
        ]
    }
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。Read this
    • 我继续修改我的答案以提供更多上下文和参考。感谢您的意见!
    【解决方案2】:

    我遇到了类似的问题,但我不得不使用旧的 gcc 编译器。我详细说明了@Ditti 的解决方案,因为该编译器会打印一些带有列号的警告/错误,而有些则没有。我还将问题匹配器放在我的任务文件的顶部,以便它适用于所有任务。

    我也使用line 而不是location。使用 location 时,column 未在问题窗格中正确使用。也许location 将服务于行和列,因此column 仍然未被使用。但是,当分别指定 linecolumn 时,它现在适用于我下面的两个测试用例。

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "problemMatcher":{
            "pattern":[
                {
                    "regexp": "^(.*?):(\\d+):(\\d*):?\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            ]
        },
        "tasks": [
            {
           //...
            }
        ]
    }
    

    我的测试用例是:

    File.c:416:10: warning: #warning test
    File.c:423: error: [12874] conflicting types for 'test'
    C:\Path\to\file.c:423: error: [12874] conflicting types for 'test'
    C:\Path\to\file.c:416:10 error: [12874] conflicting types for 'test'
    

    编辑:我被告知我的原始解决方案不适用于 Windows 样式的完整路径,因为驱动器号后面的冒号。所以我把第一组改成惰性匹配。

    【讨论】:

      猜你喜欢
      • 2020-11-17
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多