From 5a04a5078e42c04364d5e10ff7d763bb5224f0d0 Mon Sep 17 00:00:00 2001 From: Nikita Nikolaenko <6870920@gmail.com> Date: Mon, 18 Jul 2022 12:17:03 +0200 Subject: [PATCH] clangd and clang-tidy initial support (#466) * Add rules for extracting bazel compile commands * Add .clang-tidy configuration file --- .clang-tidy | 17 +++++++++++++++++ .clangd | 7 +++++++ .gitignore | 12 ++++++++++-- BUILD.bazel | 46 ++++++++++++++++++++++++++++++++++++++++++++++ WORKSPACE.bazel | 20 ++++++++++++++++++++ 5 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 .clang-tidy create mode 100644 .clangd diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 00000000..625745d0 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,17 @@ +# Provides better readability. +UseColor: true + +# NOTE: The checks were taken from +# https://github.com/3rdparty/eventuals/pull/210 +# TODO: Discuss if we want to add some extra checks. +# Available checks can be found here: +# https://clang.llvm.org/extra/clang-tidy/checks/list.html +Checks: > + bugprone-*, + clang-analyzer-cplusplus*, + concurrency-*, + cppcoreguidelines-*, + google-*, + modernize-*, + performance-*, + readability-*, diff --git a/.clangd b/.clangd new file mode 100644 index 00000000..de494cd5 --- /dev/null +++ b/.clangd @@ -0,0 +1,7 @@ +# If there are too many errors, clangd, by default, does not show them, but instead +# emits the error [clang: fatal_too_many_errors]. +# It's not helpful at all, so the following option disables this behaviour +# and forces clangd to continue running. +# https://github.com/clangd/coc-clangd/issues/255 +CompileFlags: + Add: -ferror-limit=0 diff --git a/.gitignore b/.gitignore index fd7bc170..631960ad 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,17 @@ .vscode .DS_Store user.bazelrc -.dazel_run -bazel-* *~ # Runtime file for Dazel. .dazel_run + +### Added by Hedron's Bazel Compile Commands Extractor: https://github.com/hedronvision/bazel-compile-commands-extractor +# The external link: Differs on Windows vs macOS/Linux, so we can't check it in. The pattern needs to not have a trailing / because it's a symlink on macOS/Linux. +/external +# Bazel output symlinks: Same reasoning as /external. You need the * because people can change the name of the directory your repository is cloned into, changing the bazel- symlink. +/bazel-* +# Compiled output -> don't check in +/compile_commands.json +# Directory where clangd puts its indexing work +/.cache/ diff --git a/BUILD.bazel b/BUILD.bazel index ad1b5128..4f213f5c 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,3 +1,7 @@ +load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands") + +######################################################################## + # TODO(alexmc): Remove this alias. alias( name = "eventuals", @@ -13,3 +17,45 @@ alias( deprecation = "Prefer //eventuals/grpc", visibility = ["//visibility:public"], ) + +######################################################################## + +# Hedron's Compile Commands Extractor for Bazel. +# Follow the link to learn how to set it up for your code editor: +# https://github.com/hedronvision/bazel-compile-commands-extractor +# NOTE: This rule is crucial for clangd and clang-tidy to work. +# Its output is used by clangd and clang-tidy to understand the build process. +# TODO(folming): find a cleaner way to make it automatic depending on the platform. +# select() doesn't work, produces the error: "unhashable type: 'select'". +# TODO(folming): move the rules to a separate bazel file. + +refresh_compile_commands( + name = "refresh_cc_windows", + # Add additional targets in here if needed. + # The value for keys is meant for additional bazel build arguments, e.g. + # "//test:eventuals": "--compilation_mode=dbg". + targets = { + "//test:eventuals": "", + }, +) + +refresh_compile_commands( + name = "refresh_cc_default", + # Add additional targets in here if needed. + # The value for keys is meant for additional bazel build arguments, e.g. + # "//test:eventuals": "--compilation_mode=dbg". + targets = { + "//test/grpc:grpc": "", + "//test:eventuals": "", + }, +) + +alias( + name = "refresh_cc", # refresh_compile_commands + actual = select({ + "@bazel_tools//src/conditions:windows": "refresh_cc_windows", + "//conditions:default": "refresh_cc_default", + }), +) + +######################################################################## diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 764a7319..81cd3f0b 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -19,3 +19,23 @@ load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps") grpc_extra_deps() ######################################################################## + +load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") + +# Hedron's Compile Commands Extractor for Bazel. +# Latest version available on 2022/07/18. +# Follow the link to learn how to set it up for your code editor: +# https://github.com/hedronvision/bazel-compile-commands-extractor +# TODO(folming): move the rules to a separate bazel file. +git_repository( + name = "hedron_compile_commands", + commit = "05610f52a2ea3cda6ac27133b96f71c36358adf9", + remote = "https://github.com/hedronvision/bazel-compile-commands-extractor", + shallow_since = "1657677319 -0700", +) + +load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup") + +hedron_compile_commands_setup() + +########################################################################