Implement target allowlists in addition to package allowlists for disallowed copts policies. Implement the design changes specified in design_banned_warnings.md and design_modular_copts_policies.md: - Update CcDisallowedCoptsInfo to provide package_allowlist, target_allowlist, and policy_label. - Update cc_copts_policy to accept package_allowlist and target_allowlist. - Update tools/cpp/policy/banned_warnings/BUILD to use package_allowlist. - Update third_party/crosstool/v18/stable/crosstool_configuration.bzl and third_party/apple_crosstool/crosstool.bzl to forward package_allowlist, target_allowlist, and policy_label. - Update cc_helper.bzl to exempt targets matching entries in package_allowlist and target_allowlist, and emit "disallowed by '<policy_label>'" in violation messages. - Add and update unit tests in tools/cpp/policy/ and third_party/bazel_rules/rules_cc/tests/cc/common/. Startblock: - BTRGuardian allows unknown commit Startblock: * // Put other blockers before this line to avoid churning. * has lgtm * is approved * and then * all comments are resolved * and then *[] allows unknown commit PiperOrigin-RevId: 1002924230 Change-Id: I35d1aefd0e0ecc47c81fa45c62481d534ca95a3f
diff --git a/cc/common/cc_helper.bzl b/cc/common/cc_helper.bzl index a8f7619..ad009a7 100644 --- a/cc/common/cc_helper.bzl +++ b/cc/common/cc_helper.bzl
@@ -947,9 +947,6 @@ if not hasattr(info, "flags") or not info.flags: continue - allowlist_target_label = getattr(info, "allowlist_target_label", None) - error_message = getattr(info, "error_message", None) - # Pair each set of flags with a description of where it came from. flag_sources = [("attribute '{}'".format(attr_name), copts)] if getattr(info, "verify_command_line", False) and command_line_copts: @@ -968,22 +965,33 @@ if not matches: continue - # Check the allowlist only after a match: lookups scan "//pkg/..." - # entries linearly, and most targets use no disallowed flags. - allowlist = getattr(info, "allowlist", None) - if allowlist != None and allowlist.contains(ctx.label): + # Check the allowlists only after a match: package allowlist lookups + # scan "//pkg/..." entries linearly, and most targets use no disallowed + # flags. + package_allowlist = getattr(info, "package_allowlist", getattr(info, "allowlist", None)) + if package_allowlist != None and package_allowlist.contains(ctx.label): continue + target_allowlist = getattr(info, "target_allowlist", None) + + if target_allowlist != None and ( + str(ctx.label) in target_allowlist or + str(ctx.label).lstrip("@") in target_allowlist + ): + continue + + policy_label = getattr(info, "policy_label", getattr(info, "allowlist_target_label", None)) + error_message = getattr(info, "error_message", None) for flag, source in matches: - violations.append((flag, allowlist_target_label, error_message, source)) + violations.append((flag, policy_label, error_message, source)) if violations: formatted_violations = [] - for flag, allowlist_target_label, error_message, source in violations: - allowlist_clause = ( - " (target is not in the allowlist '{}')".format( - allowlist_target_label, - ) if allowlist_target_label else "" + for flag, policy_label, error_message, source in violations: + policy_clause = ( + " disallowed by '{}'".format( + policy_label, + ) if policy_label else "" ) error_clause = ( ": {}".format(error_message) if error_message else "" @@ -992,7 +1000,7 @@ "- Flag '{}' in {}{}{}".format( flag, source, - allowlist_clause, + policy_clause, error_clause, ), )
diff --git a/tests/cc/common/disallowed_copts_test.bzl b/tests/cc/common/disallowed_copts_test.bzl index 6620454..6384acb 100644 --- a/tests/cc/common/disallowed_copts_test.bzl +++ b/tests/cc/common/disallowed_copts_test.bzl
@@ -6,7 +6,7 @@ load("@rules_testing//lib:util.bzl", "util") load("//cc:cc_binary.bzl", "cc_binary") load("//cc:cc_library.bzl", "cc_library") -load("//tests/cc/testutil:cc_analysis_test.bzl", "MOCK_ALLOWLIST_TOOLCHAINS", "MOCK_EMPTY_ALLOWLIST_TOOLCHAINS", "cc_analysis_test") +load("//tests/cc/testutil:cc_analysis_test.bzl", "MOCK_ALLOWLIST_TOOLCHAINS", "MOCK_EMPTY_ALLOWLIST_TOOLCHAINS", "MOCK_TARGET_ALLOWLIST_TOOLCHAINS", "cc_analysis_test") def _test_pass_impl(_env, _target): pass @@ -19,7 +19,9 @@ def _test_fail_empty_toolchain_allowlist_impl(env, target): env.expect.that_target(target).failures().contains_predicate( matching.contains( - "- Flag '-Wno-error' in attribute 'copts' (target is not in the allowlist '//custom:toolchain_allowlist'): Custom warning policy guidance.", + "- Flag '-Wno-error' in attribute 'copts' disallowed by '{}': Custom warning policy guidance.".format( + Label("//tests/cc/testutil/toolchains:k8-compiler_config_with_empty_allowlist"), + ), ), ) @@ -111,6 +113,22 @@ **kwargs ) +def _test_disallowed_copt_in_toolchain_target_allowlist_passes(name, **kwargs): + util.helper_target( + cc_library, + name = name + "/test", + copts = ["-Wno-error"], + ) + cc_analysis_test( + name = name, + impl = _test_pass_impl, + target = name + "/test", + config_settings = { + "//command_line_option:extra_toolchains": ",".join(MOCK_TARGET_ALLOWLIST_TOOLCHAINS), + }, + **kwargs + ) + def disallowed_copts_tests(name): test_suite( name = name, @@ -118,6 +136,7 @@ _test_disallowed_copt_fails, _test_disallowed_copt_in_toolchain_allowlist_passes, _test_disallowed_copt_cc_binary_in_toolchain_allowlist_passes, + _test_disallowed_copt_in_toolchain_target_allowlist_passes, _test_disallowed_copt_with_empty_toolchain_allowlist_fails, _test_valid_warning_passes, _test_no_copts_target_passes,
diff --git a/tests/cc/testutil/cc_analysis_test.bzl b/tests/cc/testutil/cc_analysis_test.bzl index 5557975..53fb856 100644 --- a/tests/cc/testutil/cc_analysis_test.bzl +++ b/tests/cc/testutil/cc_analysis_test.bzl
@@ -16,6 +16,10 @@ "//tests/cc/testutil/toolchains:cc-toolchain-k8-with-empty-allowlist", ] +MOCK_TARGET_ALLOWLIST_TOOLCHAINS = [ + "//tests/cc/testutil/toolchains:cc-toolchain-k8-with-target-allowlist", +] + def cc_analysis_test(name, with_features = None, test_features = [], with_action_configs = [], **kwargs): """Runs an analysis_test with the a mock C++ toolchain.
diff --git a/tests/cc/testutil/toolchains/BUILD b/tests/cc/testutil/toolchains/BUILD index 3c750cd..4894971 100644 --- a/tests/cc/testutil/toolchains/BUILD +++ b/tests/cc/testutil/toolchains/BUILD
@@ -449,7 +449,6 @@ abi_version = "local", action_configs = ["c++-header-analysis"], allowlist_provider = ":mock_allowlist", - allowlist_target_label = "//custom:toolchain_allowlist", artifact_name_patterns = {}, builtin_sysroot = "/usr/grte/v1", cc_target_os = "", @@ -497,7 +496,6 @@ abi_version = "local", action_configs = ["c++-header-analysis"], allowlist_provider = ":empty_mock_allowlist", - allowlist_target_label = "//custom:toolchain_allowlist", artifact_name_patterns = {}, builtin_sysroot = "/usr/grte/v1", cc_target_os = "", @@ -539,3 +537,52 @@ toolchain = ":cc-compiler-k8-with-empty-allowlist", toolchain_type = "//cc:toolchain_type", ) + +cc_toolchain_config( + name = "k8-compiler_config_with_target_allowlist", + abi_libc_version = "local", + abi_version = "local", + action_configs = ["c++-header-analysis"], + artifact_name_patterns = {}, + builtin_sysroot = "/usr/grte/v1", + cc_target_os = "", + compiler = "compiler", + cpu = "k8", + cxx_builtin_include_directories = [], + feature_names = [], + host_system_name = "local", + make_variables = {}, + target_allowlist = [ + "//tests/cc/common:test_disallowed_copt_in_toolchain_target_allowlist_passes/test", + ], + target_libc = "local", + target_system_name = "local", + tool_paths = {}, + toolchain_identifier = "mock-toolchain-k8", +) + +cc_toolchain( + name = "cc-compiler-k8-with-target-allowlist", + all_files = ":every-file", + ar_files = "ar-k8", + as_files = "as-k8", + compiler_files = "compile-k8", + compiler_files_without_includes = "compile-without-includes-k8", + coverage_files = "coverage-file", + dwp_files = "dwp-k8", + licenses = ["unencumbered"], + linker_files = "link-k8", + module_map = "crosstool.cppmap", + objcopy_files = "objcopy-k8", + output_licenses = ["unencumbered"], + strip_files = ":every-file", + supports_header_parsing = 1, + toolchain_config = ":k8-compiler_config_with_target_allowlist", + toolchain_identifier = "mock-toolchain-k8", +) + +toolchain( + name = "cc-toolchain-k8-with-target-allowlist", + toolchain = ":cc-compiler-k8-with-target-allowlist", + toolchain_type = "//cc:toolchain_type", +)
diff --git a/tests/cc/testutil/toolchains/cc_toolchain_config.bzl b/tests/cc/testutil/toolchains/cc_toolchain_config.bzl index 3ed8bbb..a923347 100644 --- a/tests/cc/testutil/toolchains/cc_toolchain_config.bzl +++ b/tests/cc/testutil/toolchains/cc_toolchain_config.bzl
@@ -2106,11 +2106,12 @@ extra_args = {} if bazel_features.cc.cc_common_is_in_rules_cc: allowlist = ctx.attr.allowlist_provider[PackageSpecificationInfo] if ctx.attr.allowlist_provider != None else None - allowlist_target_label = ctx.attr.allowlist_target_label or (str(ctx.attr.allowlist_provider.label) if ctx.attr.allowlist_provider else None) extra_args["disallowed_copts_infos"] = [struct( flags = ctx.attr.disallowed_copts, + package_allowlist = allowlist, allowlist = allowlist, - allowlist_target_label = allowlist_target_label, + target_allowlist = ctx.attr.target_allowlist if ctx.attr.target_allowlist else None, + policy_label = str(ctx.label), error_message = ctx.attr.error_message or None, )] @@ -2156,7 +2157,7 @@ "artifact_name_patterns": attr.string_list_dict(), "disallowed_copts": attr.string_list(default = ["-w", "-Wno-error"]), "allowlist_provider": attr.label(providers = [PackageSpecificationInfo]), - "allowlist_target_label": attr.string(), + "target_allowlist": attr.string_list(), "error_message": attr.string(), "cc_target_os": attr.string(), "builtin_sysroot": attr.string(default = "/usr/grte/v1"),