add -Werror feature by default

Bug: 187088227
Test: b test
Change-Id: Ibb8d4e5cdb626470b9b3c7c72ef1f889a5f457f8
diff --git a/BUILD.bazel b/BUILD.bazel
index efb2cd5..fd78f0f 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -40,6 +40,7 @@
     _musl_crt = "musl_crt",
 )
 load(":cc_toolchain_clang_version_test.bzl", "cc_toolchain_clang_version_test_suite")
+load(":cc_toolchain_features_misc_test.bzl", "cc_toolchain_features_test_suite")
 load(
     ":cc_toolchain_features_cfi_test.bzl",
     "cc_toolchain_features_cfi_test_suite",
@@ -425,6 +426,10 @@
     name = "cc_toolchain_clang_version_tests",
 )
 
+cc_toolchain_features_test_suite(
+    name = "cc_toolchain_features_tests",
+)
+
 cc_toolchain_features_cfi_test_suite(
     name = "cc_toolchain_features_cfi_tests",
 )
diff --git a/cc_toolchain_features.bzl b/cc_toolchain_features.bzl
index bc2602a..aea50ea 100644
--- a/cc_toolchain_features.bzl
+++ b/cc_toolchain_features.bzl
@@ -457,6 +457,21 @@
         ],
     ))
 
+    features.append(feature(
+        name = "warnings_as_errors",
+        enabled = True,
+        flag_sets = [
+            flag_set(
+                actions = _actions.compile,
+                flag_groups = [
+                    flag_group(
+                        flags = ["-Werror"],
+                    ),
+                ],
+            ),
+        ],
+    ))
+
     # The user_compile_flags feature is used by Bazel to add --copt, --conlyopt,
     # and --cxxopt values. Any features added above this call will thus appear
     # earlier in the commandline than the user opts (so users could override
diff --git a/cc_toolchain_features_misc_test.bzl b/cc_toolchain_features_misc_test.bzl
new file mode 100644
index 0000000..5a9b0ed
--- /dev/null
+++ b/cc_toolchain_features_misc_test.bzl
@@ -0,0 +1,63 @@
+"""Copyright (C) 2022 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+"""
+
+load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
+load(
+    "//build/bazel/rules/test_common:flags.bzl",
+    "action_flags_absent_for_mnemonic_test",
+    "action_flags_present_for_mnemonic_nonexclusive_test",
+)
+
+def _test_warnings_as_errors_feature():
+    name = "warnings_as_errors_feature"
+    test_name = name + "_test"
+    native.cc_library(
+        name = name,
+        srcs = ["foo.c", "bar.cpp"],
+        tags = ["manual"],
+    )
+    action_flags_present_for_mnemonic_nonexclusive_test(
+        name = test_name,
+        target_under_test = name,
+        mnemonics = ["CppCompile"],
+        expected_flags = ["-Werror"],
+    )
+    return test_name
+
+def _test_warnings_as_errors_disabled_feature():
+    name = "no_warnings_as_errors_feature"
+    test_name = name + "_test"
+    native.cc_library(
+        name = name,
+        srcs = ["foo.c", "bar.cpp"],
+        features = ["-warnings_as_errors"],
+        tags = ["manual"],
+    )
+    action_flags_absent_for_mnemonic_test(
+        name = test_name,
+        target_under_test = name,
+        mnemonics = ["CppCompile"],
+        expected_absent_flags = ["-Werror"],
+    )
+    return test_name
+
+def cc_toolchain_features_test_suite(name):
+    native.test_suite(
+        name = name,
+        tests = [
+            _test_warnings_as_errors_feature(),
+            _test_warnings_as_errors_disabled_feature(),
+        ],
+    )