Skip to content

Commit dce5885

Browse files
authored
clang-tidy: Use correct compilation flags when compiling C (#70)
* clang-tidy: Use correct compilation flags when compiling C * Format
1 parent 60e8255 commit dce5885

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

clang_tidy/clang_tidy.bzl

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
22
load("//cc:defs.bzl", "BINARY", "LIBRARY")
33
load("//tools:get_cc_files.bzl", "get_cc_srcs")
4+
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
45

56
def _flatten(input_list):
67
return [item for sublist in input_list for item in sublist]
@@ -72,7 +73,7 @@ def _run_tidy(ctx, wrapper, exe, additional_deps, config, flags, compilation_con
7273
)
7374
return outfile
7475

75-
def _toolchain_flags(ctx):
76+
def _toolchain_action_flags(ctx, action_name):
7677
cc_toolchain = find_cpp_toolchain(ctx)
7778
feature_configuration = cc_common.configure_features(
7879
ctx = ctx,
@@ -83,13 +84,34 @@ def _toolchain_flags(ctx):
8384
cc_toolchain = cc_toolchain,
8485
user_compile_flags = ctx.fragments.cpp.cxxopts + ctx.fragments.cpp.copts,
8586
)
87+
8688
flags = cc_common.get_memory_inefficient_command_line(
8789
feature_configuration = feature_configuration,
88-
action_name = "c++-compile", # tools/build_defs/cc/action_names.bzl CPP_COMPILE_ACTION_NAME
90+
action_name = action_name,
8991
variables = compile_variables,
9092
)
93+
9194
return flags
9295

96+
def _toolchain_flags_c(ctx):
97+
return _toolchain_action_flags(ctx, ACTION_NAMES.c_compile)
98+
99+
def _toolchain_flags_cpp(ctx):
100+
return _toolchain_action_flags(ctx, ACTION_NAMES.cpp_compile)
101+
102+
def _is_c_compilation(srcs):
103+
for src in srcs:
104+
if src.extension == "c":
105+
return True
106+
107+
return False
108+
109+
def _toolchain_flags(ctx, srcs):
110+
if _is_c_compilation(srcs):
111+
return _toolchain_flags_c(ctx)
112+
113+
return _toolchain_flags_cpp(ctx)
114+
93115
def _safe_flags(flags):
94116
# Some flags might be used by GCC, but not understood by Clang.
95117
# Remove them here, to allow users to run clang-tidy, without having
@@ -126,18 +148,19 @@ def _clang_tidy_aspect_impl(target, ctx):
126148
if not LIBRARY in tags and not BINARY in tags:
127149
return []
128150

151+
srcs = get_cc_srcs(ctx)
152+
129153
wrapper = ctx.attr._clang_tidy_wrapper.files_to_run
130154
exe = ctx.attr._clang_tidy_executable
131155
additional_deps = ctx.attr._clang_tidy_additional_deps
132156
config = ctx.attr._clang_tidy_config.files.to_list()[0]
133-
toolchain_flags = _toolchain_flags(ctx)
157+
toolchain_flags = _toolchain_flags(ctx, srcs)
158+
134159
rule_flags = ctx.rule.attr.copts if hasattr(ctx.rule.attr, "copts") else []
135160
safe_flags = _safe_flags(toolchain_flags + rule_flags)
136161
final_flags = _replace_gendir(safe_flags, ctx)
137162
compilation_contexts = _get_compilation_contexts(target, ctx)
138163

139-
srcs = get_cc_srcs(ctx)
140-
141164
# We exclude headers because we shouldn't run clang-tidy directly with them.
142165
# Headers will be linted if included in a source file.
143166
unsupported_ext = ["inc", "h", "hpp"]

0 commit comments

Comments
 (0)