erenon / bazel_clang_tidy

Run clang-tidy on Bazel C++ targets directly, efficiently, with caching enabled
MIT License
96 stars 51 forks source link

how can i not check system-headers #22

Open boluo250 opened 2 years ago

boluo250 commented 2 years ago

always report `error: invalid argument '-std=c++17' not allowed with 'C' [clang-diagnostic-error] /root/.cache/bazel/_bazel_root/c0cc7bb48e68379f59a785b3d4b8baaf/execroot/main/bazel-out/k8-fastbuild/bin/external/com_github_google_glog/_virtual_includes/glog/glog/logging.h:42:10: error: 'iosfwd' file not found [clang-diagnostic-error]

include `

@erenon

erenon commented 2 years ago

which source file is being processed when this error happens? (the source file that includes logging.h)

boluo250 commented 2 years ago

i build this folder image

boluo250 commented 2 years ago

in this folder several .cpp and .h include <glog/logging.h> @erenon

erenon commented 2 years ago

Why does bazel try to compile cpp files as C? How does your toolchain definition look like? What does aquery say (compile flags)?

boluo250 commented 1 year ago

Why does bazel try to compile cpp files as C? How does your toolchain definition look like? What does aquery say (compile flags)? i try to compile a demo. i test out the third party "glog" print this error.how can i not check third party? image

boluo250 commented 1 year ago

[https://github.com/google/glog/issues/839](url) @erenon hi,I have raised this issue

boluo250 commented 1 year ago

hi,@erenon this is my Minimized example,show error clang-version:15 bazel-version:bazel 4.2.1 os:ubuntu20.04

WORKSPACE

# //:WORKSPACE
load(
    "@bazel_tools//tools/build_defs/repo:git.bzl",
    "git_repository",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
git_repository(
       name = "bazel_clang_tidy",
       commit = "fe56d1f6f6d7c7f0d27b66c15935f8319918c542",
       remote = "https://github.com/boluo250/bazel_clang_tidy.git",
)

BUILD

filegroup(
       name = "clang_tidy_config",
       data = [".clang-tidy"],
       visibility = ["//visibility:public"],
)
`
**.bazelrc**
`# Required for bazel_clang_tidy to operate as expected
build:clang-tidy --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect
build:clang-tidy --output_groups=report
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_config=//:clang_tidy_config

.clang-tidy

UseColor: true

Checks: '-*,
  abseil-*,
  -abseil-no-namespace'

HeaderFilterRegex: '*'
WarningsAsErrors: "*"
CheckOptions:
  - key:             readability-identifier-naming.IgnoreMainLikeFunctions
    value:           1
  - key:             readability-identifier-naming.IgnoreFailedSplit
    value:           0
  - { key: HeaderFileExtensions,          value: "x" }

es_cpp/BUILD

cc_binary(
    name = "main-es1",
    srcs = [ "testaaa.h"],
    visibility = ["//visibility:public"],
)

es_cpp/testaaa.h #include <iostream> // NOLINT [build/c++17]

bazel build //es_cpp:main-es1 --config clang-tidy print

root@44fa34cc27e3:~/test/2clang_tidy_test# bazel build //es_cpp:main-es1 --config clang-tidy
INFO: Analyzed target //es_cpp:main-es1 (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /root/test/2clang_tidy_test/es_cpp/BUILD:2:10: Run clang-tidy on es_cpp/testaaa.h failed: (Exit 1): clang_tidy failed: error executing command bazel-out/k8-fastbuild/bin/external/bazel_clang_tidy/clang_tidy/clang_tidy bazel-out/k8-fastbuild/bin/es_cpp/bazel_clang_tidy_es_cpp/testaaa.h.main-es1.clang-tidy.yaml --export-fixes ... (remaining 26 argument(s) skipped)
2 errors generated.
Error while processing /root/.cache/bazel/_bazel_root/c12c70d71c83a34d9ced4530ee069878/execroot/__main__/es_cpp/testaaa.h.
Found compiler error(s).
error: invalid argument '-std=c++0x' not allowed with 'C' [clang-diagnostic-error]
/root/.cache/bazel/_bazel_root/c12c70d71c83a34d9ced4530ee069878/execroot/__main__/es_cpp/testaaa.h:1:10: error: 'iostream' file not found [clang-diagnostic-error]
#include <iostream> // NOLINT [build/c++17]
         ^~~~~~~~~~
Aspect @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect of //es_cpp:main-es1 failed to build
Use --verbose_failures to see the command lines of failed build steps.

INFO: Elapsed time: 0.217s, Critical Path: 0.05s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully
HappyCerberus commented 1 year ago

I run into the same problem. It's actually an issue with clang treating .h files as C.

To fix it, change the run_clang_tidy.sh:

clang-tidy --extra-arg-before=-xc++ "$@"