跳转到主内容

在 C++ 代码中使用clang-tidy

clang-tidy 是自动检查 C/C++/Objective-C 代码的样式冲突、编程 错误和最佳实践的工具。

Electron的clang-tidy集成是作为一个linter脚本提供的,可以通过npm run lint:clang-tidy来运行。 当 clang-tidy 检查您的磁盘文件时,您需要构建 Electron,以便它知道使用了哪些编译器标志。 脚本--output-dir需要一个必填选项,该选项告诉脚本从哪个构建目录获取编译信息。 一个典型的用法是: npm run lint:clang-tidy --out-dir ../out/Testing

没有提供文件名的话,工具将检查所有的C/C++/Objective-C文件。 您可以提供要检查的文件列表,在 选项后通过文件名: npm 运行行:clang-tidy --out-dir 。 /out/测试shell/browser/api/electron_api_app.cc

While clang-tidy has a long list of possible checks, in Electron only a few are enabled by default. At the moment Electron doesn't have a .clang-tidy config, so clang-tidy will find the one from Chromium at src/.clang-tidy and use the checks which Chromium has enabled. You can change which checks are run by using the --checks= option. This is passed straight through to clang-tidy, so see its documentation for full details. Wildcards can be used, and checks can be disabled by prefixing a -. By default any checks listed are added to those in .clang-tidy, so if you'd like to limit the checks to specific ones you should first exclude all checks then add back what you want, like --checks=-*,performance*.

Running clang-tidy is rather slow - internally it compiles each file and then runs the checks so it will always be some factor slower than compilation. While you can use parallel runs to speed it up using the --jobs|-j option, clang-tidy also uses a lot of memory during its checks, so it can easily run into out-of-memory errors. As such the default number of jobs is one.