【问题标题】:Configuring static analysis or linters for C++ with Bazel使用 Bazel 为 C++ 配置静态分析或 linter
【发布时间】:2020-05-22 06:26:23
【问题描述】:

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

但是,我不确定应该如何使用 Bazel 设置这些工具。一些人四处探索发现了自定义外观的解决方案,例如Drake(参见cpplint.bzl)或apollo,但需要编写一堆自定义构建工具链逻辑才能使这些工作看起来很奇怪。有没有合适的方法来设置这些?

【问题讨论】:

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


    【解决方案1】:

    我还没有一个很好的答案,我仍然很惊讶它是这本手册,但我至少有 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 .
    

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 2014-05-30
      • 1970-01-01
      • 2022-12-14
      • 2021-07-30
      • 1970-01-01
      • 2020-12-05
      • 2020-08-05
      • 2020-09-26
      相关资源
      最近更新 更多