如何为 vscode 中的 cppcheck 任务定义问题匹配器?
How to define a problem matcher for cppcheck task in vscode?
我正在为 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
但是没有出现在"problems"栏中。
我自己正在寻找解决方案,但只能找到这个悬而未决的问题。所以我继续对它进行了一些修改,最终让它工作了。
问题似乎是 Cppcheck 的旧版本(我使用的是 1.82 版)尚不支持打印列。看起来支持 wasn't added until Cppcheck 1.84. However, the default $gcc
problem matcher 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
}
}
}
]
}
我遇到了类似的问题,但是我不得不使用旧的 gcc 编译器。我详细阐述了@Ditti 的解决方案,因为该编译器打印了一些带有列号的 warnings/errors 而一些没有。我还将问题匹配器放在任务文件的顶部,以便它适用于所有任务。
我也使用 line
而不是 location
。使用 location
时,问题窗格中的 column
未正确使用。也许 location
会提供行和列,因此 column
仍未使用。但是当分别指定 line
和 column
时,它现在适用于我下面的两个测试用例。
{
// 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 样式的完整路径,因为驱动器号后面有冒号。所以我把第一组改成了惰性匹配
我正在为 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
但是没有出现在"problems"栏中。
我自己正在寻找解决方案,但只能找到这个悬而未决的问题。所以我继续对它进行了一些修改,最终让它工作了。
问题似乎是 Cppcheck 的旧版本(我使用的是 1.82 版)尚不支持打印列。看起来支持 wasn't added until Cppcheck 1.84. However, the default $gcc
problem matcher 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
}
}
}
]
}
我遇到了类似的问题,但是我不得不使用旧的 gcc 编译器。我详细阐述了@Ditti 的解决方案,因为该编译器打印了一些带有列号的 warnings/errors 而一些没有。我还将问题匹配器放在任务文件的顶部,以便它适用于所有任务。
我也使用 line
而不是 location
。使用 location
时,问题窗格中的 column
未正确使用。也许 location
会提供行和列,因此 column
仍未使用。但是当分别指定 line
和 column
时,它现在适用于我下面的两个测试用例。
{
// 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 样式的完整路径,因为驱动器号后面有冒号。所以我把第一组改成了惰性匹配