使用 Bazel 配置 C++ 的静态分析或 linter

dim*_*414 5 c++ clang-static-analyzer clang-format bazel

我正在自学 C++,并与 Bazel 一起开发一个小项目。我想确保我正在编写安全的代码并遵循合理的最佳实践,但我不确定如何去做。我知道一些静态分析工具,例如tsan和其他分析器clang-tidycpplint

但是我不确定应该如何使用 Bazel 设置这些工具。有些人已经找到了看起来像自定义的解决方案,例如Drake(请参阅 参考资料cpplint.bzl)或apollo,但是需要编写一堆自定义构建工具链逻辑才能使这些解决方案发挥作用似乎很奇怪。有没有正确的方法来设置这些?

dim*_*414 0

我还没有一个很好的答案,我仍然很惊讶这是本手册,但我至少已经得到cpplint.pyclang-tidy正在工作,如下所示:

cplint

我将此目标添加到我的src/BUILD文件中(您需要--filter根据需要进行配置):

py_test(
    name = "cpplint",
    srcs = ["@google_styleguide//:cpplint/cpplint.py"],
    data = [":all_cpp_files"],
    # Not sure if there's a better way to express the paths to the src/ dir
    # Drake uses the location oparator, but needs to jump through hoops to get the list of targets
    args = ["--root=src", "--linelength=100", "--counting=detailed",
            "--filter=..."] +
        ["src/%s" % f for f in glob(["**/*.cc", "**/*.h"])],
    main = "cpplint.py",
    python_version = "PY2",
)
Run Code Online (Sandbox Code Playgroud)

工作空间@google_styleguide定义如下:

new_git_repository(
    name = "google_styleguide",
    remote = "https://github.com/google/styleguide",
    commit = "26470f9ccb354ff2f6d098f831271a1833701b28",
    build_file = "@//:google_styleguide.BUILD",
)
Run Code Online (Sandbox Code Playgroud)

铿锵整齐

我创建了一个自定义 shell 脚本,大致如下所示(您需要调整checks要启用/禁用的脚本):

py_test(
    name = "cpplint",
    srcs = ["@google_styleguide//:cpplint/cpplint.py"],
    data = [":all_cpp_files"],
    # Not sure if there's a better way to express the paths to the src/ dir
    # Drake uses the location oparator, but needs to jump through hoops to get the list of targets
    args = ["--root=src", "--linelength=100", "--counting=detailed",
            "--filter=..."] +
        ["src/%s" % f for f in glob(["**/*.cc", "**/*.h"])],
    main = "cpplint.py",
    python_version = "PY2",
)
Run Code Online (Sandbox Code Playgroud)