c++ - 使用Bazel为C++配置静态分析或linters

标签 c++ bazel clang-format clang-static-analyzer

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

但是我不确定如何使用Bazel设置这些工具。一些闲逛的人发现了定制外观的解决方案,例如Drake(请参阅 cpplint.bzl )或apollo,但似乎很奇怪,需要编写一堆定制构建工具链逻辑才能使它们起作用。是否有适当的方法进行设置?

最佳答案

我还没有一个很好的答案,我仍然很惊讶这是本手册,但是我至少可以使cpplint.pyclang-tidy正常工作,就像这样:
cpplint
我将此目标添加到了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",
)
@google_styleguide工作空间的定义如下:
new_git_repository(
    name = "google_styleguide",
    remote = "https://github.com/google/styleguide",
    commit = "26470f9ccb354ff2f6d098f831271a1833701b28",
    build_file = "@//:google_styleguide.BUILD",
)
整洁的
我创建了一个大致如下所示的自定义 shell 脚本(您需要调整要启用/禁用的checks):
execution_root=$(bazel info execution_root) || exit

clang-tidy \
  -warnings-as-errors=* \
  -header-filter=src/.* \
  -checks="${checks}" \
  "src/"*.cc \
  -- \
    -I "${execution_root}/external/googletest/googletest/include/" \
    -I "${execution_root}/external/absl/" \
    -I "${execution_root}/external/gsl/include/" \
    -I .

关于c++ - 使用Bazel为C++配置静态分析或linters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60091054/

相关文章:

c++ - 我的语法在这里正确吗? C++

c++ - Rabbitmq 与 Apache QPID

windows - 使用 GPU 支持为 TensorFlow 创建 pip 包会产生 0 字节 simple_console_for_windows.zip

objective-c - clang-format 是否识别新的 NS_CLOSED_ENUM Objective-C 宏?

android - 将 FILE* 转换为 ifstream C++、Android NDK

c++ - 整数位移

ios - 为什么我的 Xcode 插件(如 clang 格式)在更新到新版本的 Xcode 后不再工作?

xcode - 如何获取 Xcode 8 的 clang 格式?

typescript - 通过 bazel 执行 Jasmine 测试时未找到规范

bazel - 有没有办法查看正在运行的 Bazel 操作的助记符?