诊断解析器添加
add-diagnostic-parser
在为“问题(Problems)面板”添加编译器或工具输出解析器时使用。涉及 src/diagnostics/<name>.ts、src/diagnostics/build.ts、package.json(cmake.enabledOutputParsers)和 package.nls.json。触发词:"add parser"、"new diagnostic parser"、"parse compiler output"。
- License 许可
- MIT
- 最近更新
- 2026-11-11
- Stars
- ★ 1.7k
- Forks
- 559
01 前言 Front Matter
- name
- add-diagnostic-parser
- description
- Use when adding a compiler or tool output parser for the Problems panel. Touches src/diagnostics/<name>.ts, src/diagnostics/build.ts, package.json (cmake.enabledOutputParsers), and package.nls.json. Triggers: "add parser", "new diagnostic parser", "parse compiler output".
02 资源文件 81
- 脚本scripts/default/__init__.py1 B
- 脚本scripts/default/base.py1.8 KB
- 脚本scripts/default/code_complexity.py2.8 KB
- 脚本scripts/default/code_duplication.py2.1 KB
- 脚本scripts/default/env_config.py1.9 KB
- 脚本scripts/default/helpers_usage.py1.9 KB
- 脚本scripts/default/import_hygiene.py2.2 KB
- 脚本scripts/default/kill_switch.py1.2 KB
- 脚本scripts/default/logging_rule.py1.6 KB
- 脚本scripts/default/manual_io.py1.1 KB
- 脚本scripts/default/memory_efficiency.py4.3 KB
- 脚本scripts/default/path_safety.py4.9 KB
- 脚本scripts/default/pythonic_standards.py3.1 KB
- 脚本scripts/default/security_rule.py2.5 KB
- 脚本scripts/default/security/__init__.py2.5 KB
- 脚本scripts/default/security/crypto_risk.py10.4 KB
- 脚本scripts/default/security/deserialization_risk.py4.0 KB
- 脚本scripts/default/security/info_disclosure_risk.py6.9 KB
- 脚本scripts/default/security/injections_risk.py11.2 KB
- 脚本scripts/default/security/network_risk.py5.0 KB
- 脚本scripts/default/security/resource_risks.py9.8 KB
- 脚本scripts/default/silent_exceptions.py1.2 KB
- 脚本scripts/default/type_safety.py12.2 KB
- 脚本scripts/linter.py21.2 KB
- 脚本scripts/rest_api/api_versioning.py7.8 KB
- 脚本scripts/rest_api/auth.py4.9 KB
- 脚本scripts/rest_api/breaking_changes.py8.6 KB
- 脚本scripts/rest_api/caching.py4.2 KB
- 脚本scripts/rest_api/cors.py8.1 KB
- 脚本scripts/rest_api/endpoint_naming.py9.5 KB
- 脚本scripts/rest_api/error_handling.py6.0 KB
- 脚本scripts/rest_api/http_method.py6.8 KB
- 脚本scripts/rest_api/idempotency.py4.6 KB
- 脚本scripts/rest_api/input_validation.py4.8 KB
- 脚本scripts/rest_api/n1_query.py4.1 KB
- 脚本scripts/rest_api/pagination.py4.1 KB
- 脚本scripts/rest_api/rate_limiting.py4.6 KB
- 脚本scripts/rest_api/request_logging.py6.6 KB
- 脚本scripts/rest_api/response_filtering.py4.9 KB
- 脚本scripts/rest_api/retry_logic.py4.8 KB
- 脚本scripts/rest_api/sensitive_data.py5.0 KB
- 脚本scripts/rest_api/status_code.py15.0 KB
- 脚本scripts/rest_api/tests/api_versioning/cases.py5.2 KB
- 脚本scripts/rest_api/tests/api_versioning/runner.py1.1 KB
- 脚本scripts/rest_api/tests/auth/cases.py7.0 KB
- 脚本scripts/rest_api/tests/auth/runner.py889 B
- 脚本scripts/rest_api/tests/breaking_changes/cases.py10.6 KB
- 脚本scripts/rest_api/tests/breaking_changes/runner.py1.0 KB
- 脚本scripts/rest_api/tests/caching/cases.py7.7 KB
- 脚本scripts/rest_api/tests/caching/runner.py886 B
- 脚本scripts/rest_api/tests/cors/cases.py8.6 KB
- 脚本scripts/rest_api/tests/cors/runner.py1.1 KB
- 脚本scripts/rest_api/tests/endpoint_naming/cases.py38.8 KB
- 脚本scripts/rest_api/tests/endpoint_naming/runner.py1.2 KB
- 脚本scripts/rest_api/tests/error_handling/cases.py8.1 KB
- 脚本scripts/rest_api/tests/error_handling/runner.py1.2 KB
- 脚本scripts/rest_api/tests/http_method/cases.py25.0 KB
- 脚本scripts/rest_api/tests/http_method/runner.py1.5 KB
- 脚本scripts/rest_api/tests/idempotency/cases.py5.1 KB
- 脚本scripts/rest_api/tests/idempotency/runner.py1.2 KB
- 脚本scripts/rest_api/tests/input_validation/cases.py7.8 KB
- 脚本scripts/rest_api/tests/input_validation/runner.py895 B
- 脚本scripts/rest_api/tests/n1_query/cases.py6.2 KB
- 脚本scripts/rest_api/tests/n1_query/runner.py887 B
- 脚本scripts/rest_api/tests/pagination/cases.py6.6 KB
- 脚本scripts/rest_api/tests/pagination/runner.py907 B
- 脚本scripts/rest_api/tests/rate_limiting/cases.py5.6 KB
- 脚本scripts/rest_api/tests/rate_limiting/runner.py886 B
- 脚本scripts/rest_api/tests/request_logging/cases.py9.3 KB
- 脚本scripts/rest_api/tests/request_logging/runner.py1.1 KB
- 脚本scripts/rest_api/tests/response_filtering/cases.py5.0 KB
- 脚本scripts/rest_api/tests/response_filtering/runner.py1.2 KB
- 脚本scripts/rest_api/tests/retry_logic/cases.py4.2 KB
- 脚本scripts/rest_api/tests/retry_logic/runner.py880 B
- 脚本scripts/rest_api/tests/sensitive_data/cases.py5.2 KB
- 脚本scripts/rest_api/tests/sensitive_data/runner.py1.1 KB
- 脚本scripts/rest_api/tests/status_code/cases.py22.3 KB
- 脚本scripts/rest_api/tests/status_code/runner.py1.1 KB
- 脚本scripts/rest_api/tests/timeout/cases.py4.8 KB
- 脚本scripts/rest_api/tests/timeout/runner.py886 B
- 脚本scripts/rest_api/timeout.py5.4 KB
03 SKILL.md 正文
Adding a New Diagnostic / Output Parser
Recipe for adding a new compiler or tool output parser to CMake Tools. A parser extracts diagnostics (errors, warnings, notes) from build output and surfaces them in the VS Code Problems panel.
Overview
Adding a parser touches 4 files (plus tests and changelog):
| File | What to do |
|---|---|
src/diagnostics/<name>.ts |
Create the parser class |
src/diagnostics/build.ts |
Register the parser in Compilers |
package.json |
Add to cmake.enabledOutputParsers enum |
package.nls.json |
Update description if user-visible text changes |
Step 1: Create the parser file
Create src/diagnostics/<name>.ts. Every parser extends RawDiagnosticParser (defined in src/diagnostics/util.ts) and exports a class named Parser.
Base class contract
// src/diagnostics/util.ts (simplified)
export abstract class RawDiagnosticParser {
get diagnostics(): readonly RawDiagnostic[] { /* accumulated results */ }
/** Called by the build consumer for every line of output. */
handleLine(line: string): boolean {
const result = this.doHandleLine(line);
if (result === FeedLineResult.Ok) return true; // line consumed, no new diagnostic
if (result === FeedLineResult.NotMine) return false; // line not recognized
// Otherwise result is a RawDiagnostic — it gets stored automatically
this._diagnostics.push(result);
return true;
}
/** Implement this. Return a RawDiagnostic to emit, or a FeedLineResult. */
protected abstract doHandleLine(line: string): RawDiagnostic | FeedLineResult;
}
Minimal single-regex parser (like MSVC)
/**
* Module for parsing <ToolName> diagnostics
*/ /** */
import * as vscode from 'vscode';
import { oneLess, RawDiagnosticParser, FeedLineResult } from '@cmt/diagnostics/util';
// Regex that captures: file, line, severity, code (optional), message
export const REGEX = /^(.+):(\d+):\s+(error|warning|info)\s+(\w+):\s+(.*)$/;
export class Parser extends RawDiagnosticParser {
doHandleLine(line: string) {
const mat = REGEX.exec(line);
if (!mat) {
return FeedLineResult.NotMine;
}
const [full, file, lineStr, severity, code, message] = mat;
const lineno = oneLess(lineStr); // convert 1-based to 0-based
return {
full,
file,
location: new vscode.Range(lineno, 0, lineno, 999),
severity,
message,
code,
related: []
};
}
}
Multi-line / stateful parser (like IAR)
For tools whose diagnostics span multiple lines, use internal state:
import { RawDiagnosticParser, RawDiagnostic, FeedLineResult, oneLess } from '@cmt/diagnostics/util';
enum ParserState { init, pending_message }
export class Parser extends RawDiagnosticParser {
private state = ParserState.init;
private pending: RawDiagnostic | null = null;
doHandleLine(line: string): RawDiagnostic | FeedLineResult {
switch (this.state) {
case ParserState.init: {
const mat = FIRST_LINE_REGEX.exec(line);
if (!mat) return FeedLineResult.NotMine;
this.pending = { /* ... build partial diagnostic ... */ };
this.state = ParserState.pending_message;
return FeedLineResult.Ok; // consumed, but not complete yet
}
case ParserState.pending_message: {
if (/* line completes the diagnostic */) {
const diag = this.pending!;
this.reset();
return diag; // emits the diagnostic
}
return FeedLineResult.Ok; // still accumulating
}
}
return FeedLineResult.NotMine;
}
}
Key types
// src/diagnostics/util.ts
interface RawDiagnostic {
full: string; // the full matched line(s)
file: string; // source file path
location: vscode.Range; // 0-based range (use oneLess() for 1-based input)
severity: string; // 'error' | 'warning' | 'note' | 'info' | 'fatal error' | 'remark'
message: string;
code?: string; // optional diagnostic code (e.g. 'C4996', 'LNK2019')
related: RawRelated[]; // related diagnostics (notes, template backtraces)
}
enum FeedLineResult {
Ok, // line was consumed (but no new diagnostic produced yet)
NotMine, // line not recognized by this parser
}
diagnosticSeverity() in util.ts maps the severity string to vscode.DiagnosticSeverity. Recognized values: 'error', 'fatal error', 'catastrophic error', 'warning', 'note', 'info', 'remark'.
Step 2: Register in src/diagnostics/build.ts
Import the new parser module and add an instance to the Compilers class. The property name becomes the parser identifier used in cmake.enabledOutputParsers.
// At the top — add import
import * as myparser from '@cmt/diagnostics/myparser';
// Inside the Compilers class — add property
export class Compilers {
[compiler: string]: RawDiagnosticParser;
gcc = new gcc.Parser();
gnuld = new gnu_ld.Parser();
ghs = new ghs.Parser();
diab = new diab.Parser();
msvc = new mvsc.Parser();
iar = new iar.Parser();
iwyu = new iwyu.Parser();
myparser = new myparser.Parser(); // ← new parser
}
The CompileOutputConsumer.error() method iterates over all Compilers properties in declaration order, calling parser.handleLine(line). The first parser to return true claims the line — order matters if your parser's regex could match lines from another compiler.
The resolveDiagnostics() method filters diagnostics by config.enableOutputParsers — only parsers whose name appears in that array will have their diagnostics shown in the Problems panel.
Step 3: Update package.json
Add the parser name to the cmake.enabledOutputParsers enum. Decide whether it should be default-enabled or opt-in.
// package.json → contributes.configuration → cmake.enabledOutputParsers
"cmake.enabledOutputParsers": {
"type": "array",
"items": {
"type": "string",
"enum": [
"cmake",
"gcc",
"gnuld",
"msvc",
"ghs",
"diab",
"iar",
"iwyu",
"myparser" // ← add to enum
]
},
"default": [
"cmake",
"gcc",
"gnuld",
"msvc",
"ghs",
"diab"
// only add here if default-enabled
]
}
Step 4: Update package.nls.json
If any user-visible setting descriptions changed, update the localization key in package.nls.json. The cmake.enabledOutputParsers description uses the key cmake-tools.configuration.cmake.enabledOutputParsers.description.
Step 5: Write tests
Add tests in test/unit-tests/diagnostics.test.ts. The test pattern:
test('Parse <tool> error', () => {
const build_consumer = new diags.CompileOutputConsumer(
new ConfigurationReader({} as ExtensionConfigurationSettings)
);
// Feed real compiler output lines
build_consumer.error('<tool-specific output line>');
// Verify diagnostics
const parser = build_consumer.compilers.myparser;
expect(parser.diagnostics).to.have.length(1);
expect(parser.diagnostics[0].severity).to.eq('error');
expect(parser.diagnostics[0].file).to.eq('expected/file.cpp');
});
Step 6: CHANGELOG.md
Add an entry under the current version:
Features:
- Add `myparser` diagnostic parser for <ToolName> compiler output. [PR #XXXX](https://github.com/microsoft/vscode-cmake-tools/pull/XXXX)
Currently registered parsers
Property in Compilers |
Module file | Default-enabled |
|---|---|---|
gcc |
src/diagnostics/gcc.ts |
✅ |
gnuld |
src/diagnostics/gnu-ld.ts |
✅ |
ghs |
src/diagnostics/ghs.ts |
✅ |
diab |
src/diagnostics/diab.ts |
✅ |
msvc |
src/diagnostics/msvc.ts |
✅ |
iar |
src/diagnostics/iar.ts |
❌ opt-in |
iwyu |
src/diagnostics/iwyu.ts |
❌ opt-in |
The cmake parser (in src/diagnostics/cmake.ts) is separate — it handles CMake's own configure/generate output via CMakeOutputConsumer, not build compiler output. It is listed in the enabledOutputParsers default array but is not part of the Compilers class.
Default-enabled vs opt-in
- Default-enabled parsers (
cmake,gcc,gnuld,msvc,ghs,diab) are included in the"default"array inpackage.json. Users get them automatically. - Opt-in parsers (
iar,iwyu) are in the"enum"but not in"default". Users must add them to theircmake.enabledOutputParserssetting. - Use opt-in for parsers with aggressive regexes that might false-positive on common output, or for niche toolchains.
Verification checklist
- [ ] Parser class extends
RawDiagnosticParserand exports asParser - [ ]
doHandleLine()returnsFeedLineResult.NotMinefor unrecognized lines - [ ]
doHandleLine()returns aRawDiagnostic(notFeedLineResult.Ok) when a diagnostic is complete - [ ] Line/column numbers converted from 1-based to 0-based with
oneLess() - [ ] Registered in
Compilersclass insrc/diagnostics/build.ts - [ ] Added to
cmake.enabledOutputParsersenum inpackage.json - [ ] Default array updated (or deliberately left opt-in)
- [ ] Unit tests in
test/unit-tests/diagnostics.test.tspass - [ ]
yarn compilesucceeds - [ ]
CHANGELOG.mdentry added
See also: .github/copilot-instructions.md for project-wide conventions.
04 同仓库更多 Skills
PR 验证项目搭建
validation-project
为测试某个特定的 vscode-cmake-tools pull request 创建验证项目。当被要求创建验证项目、测试 PR、验证 pull request 或搭建 PR 验证环境时使用此技能。该技能会创建一个结构化的项目目录,其中包含用于系统验证 PR 变更的文档、元数据和检查清单。
CMake Presets 与 Kits 双模式兼容
presets-vs-kits
在编写或审查必须同时兼容 CMake Presets 和 Kits/Variants 模式的代码时使用。涵盖配置、构建、测试、环境和生成器路径。触发词:“presets vs kits”、“useCMakePresets”、“works in both modes”。
PR 贡献合规检查
pr-readiness
验证提交到 microsoft/vscode-cmake-tools 的拉取请求(PR)是否符合贡献要求。在准备、审查或定稿 PR 时使用,用于检查标题是否具有描述性、描述是否有意义、CHANGELOG 条目格式是否规范、代码正确性、回归风险、是否遵循现有模式以及是否需要更新文档。
后端单元测试编写
backend-tests
用于编写 test/unit-tests/backend/*.test.ts 下的单元测试,这些测试在 Node 中运行,使用 Mocha+Chai 且不依赖 VS Code API。适用于纯逻辑:解析器、辅助函数、环境合并。触发词:"backend test"、"unit test"、"mocha test without vscode"。
cmake 配置项添加
add-setting
在添加新的 cmake.* 配置设置时使用。涉及 package.json(contributes.configuration)、package.nls.json、src/config.ts(接口 + getter)、docs/cmake-settings.md 和 CHANGELOG.md。触发词:"add setting"、"new setting"、"add configuration"。
新增 CMake Tools 命令
add-command
用于向 CMake Tools 添加新的 cmake.* 命令。涉及 package.json(contributes.commands)、package.nls.json、src/extension.ts(funs 数组)和 CHANGELOG.md。触发词:"add command"、"register command"、"new command palette entry"。