Skip to content

Commit ce34b32

Browse files
committed
Automate clang_format validation in cf_cc_* macros
- Add clang_format_enabled attribute to cf_cc_binary, cf_cc_library, and cf_cc_test. - Enable clang_format_enabled = True on kernel_log_monitor and kernel_log_monitor_utils targets. - Remove redundant manual format_test target in kernel_log_monitor/BUILD.bazel. - Format kernel_log_server.cc, kernel_log_server.h, utils.cc, and utils.h. Assisted-by: Jetski <jetski@google.com> Bug: b/512215781
1 parent 3241c95 commit ce34b32

6 files changed

Lines changed: 44 additions & 21 deletions

File tree

base/cvd/cuttlefish/bazel/rules.bzl

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,31 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@aspect_rules_lint//format:defs.bzl", "format_test")
16+
load("@cc_compatibility_proxy//:proxy.bzl", "cc_binary", "cc_library", "cc_test")
1517
load("//:build_variables.bzl", BUILD_VAR_COPTS = "COPTS")
1618
load("//tools/lint:linters.bzl", "clang_tidy_test")
17-
load("@cc_compatibility_proxy//:proxy.bzl", "cc_binary", "cc_library", "cc_test")
1819

1920
visibility(["//..."])
2021

2122
COPTS = BUILD_VAR_COPTS
2223

23-
def _cf_cc_binary_implementation(name, clang_tidy_enabled, copts, **kwargs):
24+
def _cf_cc_binary_implementation(name, clang_format_enabled, clang_tidy_enabled, copts, **kwargs):
2425
if not clang_tidy_enabled and not kwargs["deprecation"]:
2526
kwargs["deprecation"] = "Not covered by clang-tidy"
2627
cc_binary(
2728
name = name,
2829
copts = (copts or []) + COPTS,
2930
**kwargs,
3031
)
32+
if clang_format_enabled:
33+
format_test(
34+
name = name + "_format_test",
35+
cc = "//tools/format:clang_format",
36+
disable_git_attribute_checks = True,
37+
srcs = (kwargs.get("srcs") or []) + (kwargs.get("hdrs") or []),
38+
visibility = ["//visibility:private"],
39+
)
3140
if clang_tidy_enabled:
3241
clang_tidy_test(
3342
name = name + "_clang_tidy",
@@ -39,20 +48,29 @@ def _cf_cc_binary_implementation(name, clang_tidy_enabled, copts, **kwargs):
3948
cf_cc_binary = macro(
4049
inherit_attrs = cc_binary,
4150
attrs = {
51+
"clang_format_enabled": attr.bool(configurable = False, default = False, doc = "Decide if a corresponding format_test target is generated"),
4252
"clang_tidy_enabled": attr.bool(configurable = False, default = True, doc = "Decide if a corresponding clang_tidy_test target is generated"),
4353
"copts": attr.string_list(configurable = False, default = []),
4454
},
4555
implementation = _cf_cc_binary_implementation,
4656
)
4757

48-
def _cf_cc_library_implementation(name, clang_tidy_enabled, copts, **kwargs):
58+
def _cf_cc_library_implementation(name, clang_format_enabled, clang_tidy_enabled, copts, **kwargs):
4959
if not clang_tidy_enabled and not kwargs["deprecation"]:
5060
kwargs["deprecation"] = "Not covered by clang-tidy"
5161
cc_library(
5262
name = name,
5363
copts = (copts or []) + COPTS,
5464
**kwargs,
5565
)
66+
if clang_format_enabled:
67+
format_test(
68+
name = name + "_format_test",
69+
cc = "//tools/format:clang_format",
70+
disable_git_attribute_checks = True,
71+
srcs = (kwargs.get("srcs") or []) + (kwargs.get("hdrs") or []),
72+
visibility = ["//visibility:private"],
73+
)
5674
if clang_tidy_enabled:
5775
clang_tidy_test(
5876
name = name + "_clang_tidy",
@@ -64,13 +82,14 @@ def _cf_cc_library_implementation(name, clang_tidy_enabled, copts, **kwargs):
6482
cf_cc_library = macro(
6583
inherit_attrs = cc_library,
6684
attrs = {
85+
"clang_format_enabled": attr.bool(configurable = False, default = False, doc = "Decide if a corresponding format_test target is generated"),
6786
"clang_tidy_enabled": attr.bool(configurable = False, default = True, doc = "Decide if a corresponding clang_tidy_test target is generated"),
6887
"copts": attr.string_list(configurable = False, default = []),
6988
},
7089
implementation = _cf_cc_library_implementation,
7190
)
7291

73-
def _cf_cc_test_implementation(name, clang_tidy_enabled, copts, deps, **kwargs):
92+
def _cf_cc_test_implementation(name, clang_format_enabled, clang_tidy_enabled, copts, deps, **kwargs):
7493
if not clang_tidy_enabled and not kwargs["deprecation"]:
7594
kwargs["deprecation"] = "Not covered by clang-tidy"
7695
cc_test(
@@ -82,6 +101,14 @@ def _cf_cc_test_implementation(name, clang_tidy_enabled, copts, deps, **kwargs):
82101
],
83102
**kwargs,
84103
)
104+
if clang_format_enabled:
105+
format_test(
106+
name = name + "_format_test",
107+
cc = "//tools/format:clang_format",
108+
disable_git_attribute_checks = True,
109+
srcs = (kwargs.get("srcs") or []) + (kwargs.get("hdrs") or []),
110+
visibility = ["//visibility:private"],
111+
)
85112
if clang_tidy_enabled:
86113
clang_tidy_test(
87114
name = name + "_clang_tidy",
@@ -93,6 +120,7 @@ def _cf_cc_test_implementation(name, clang_tidy_enabled, copts, deps, **kwargs):
93120
cf_cc_test = macro(
94121
inherit_attrs = cc_test,
95122
attrs = {
123+
"clang_format_enabled": attr.bool(configurable = False, default = False, doc = "Decide if a corresponding format_test target is generated"),
96124
"clang_tidy_enabled": attr.bool(configurable = False, default = True, doc = "Decide if a corresponding clang_tidy_test target is generated"),
97125
"copts": attr.string_list(configurable = False, default = []),
98126
"deps": attr.label_list(configurable = False),

base/cvd/cuttlefish/host/commands/kernel_log_monitor/BUILD.bazel

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("//cuttlefish/bazel:rules.bzl", "cf_cc_binary", "cf_cc_library")
2-
load("@aspect_rules_lint//format:defs.bzl", "format_test")
32

43
package(
54
default_visibility = ["//:android_cuttlefish"],
@@ -12,6 +11,7 @@ cf_cc_binary(
1211
srcs = [
1312
"main.cc",
1413
],
14+
clang_format_enabled = True,
1515
deps = [
1616
":kernel_log_monitor_utils",
1717
"//cuttlefish/common/libs/fs",
@@ -29,13 +29,6 @@ cf_cc_binary(
2929
],
3030
)
3131

32-
format_test(
33-
name = "kernel_log_monitor_format_test",
34-
cc = "//tools/format:clang_format",
35-
disable_git_attribute_checks = True,
36-
srcs = ["main.cc"],
37-
)
38-
3932
cf_cc_library(
4033
name = "kernel_log_monitor_utils",
4134
srcs = [
@@ -46,6 +39,7 @@ cf_cc_library(
4639
"kernel_log_server.h",
4740
"utils.h",
4841
],
42+
clang_format_enabled = True,
4943
deps = [
5044
"//cuttlefish/common/libs/fs",
5145
"//cuttlefish/common/libs/utils:json",

base/cvd/cuttlefish/host/commands/kernel_log_monitor/kernel_log_server.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
#include <utility>
2626
#include <vector>
2727

28+
#include "absl/log/log.h"
2829
#include "absl/strings/ascii.h"
2930
#include "absl/strings/str_split.h"
30-
#include "absl/log/log.h"
3131

3232
#include "cuttlefish/common/libs/fs/shared_fd.h"
3333
#include "cuttlefish/common/libs/fs/shared_select.h"
34-
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
3534
#include "cuttlefish/host/libs/config/config_constants.h"
35+
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
3636

3737
namespace cuttlefish::monitor {
3838
namespace {
@@ -87,8 +87,8 @@ void ProcessSubscriptions(Json::Value message,
8787
if (action == SubscriptionAction::ContinueSubscription) {
8888
++idx;
8989
} else {
90-
// Cancel the subscription by swapping it with the last active subscription
91-
// and decreasing the active subscription count
90+
// Cancel the subscription by swapping it with the last active
91+
// subscription and decreasing the active subscription count
9292
--active_subscription_count;
9393
std::swap((*subscribers)[idx], (*subscribers)[active_subscription_count]);
9494
}
@@ -135,7 +135,7 @@ bool KernelLogServer::HandleIncomingMessage() {
135135
}
136136

137137
// Detect VIRTUAL_DEVICE_BOOT_*
138-
for (ssize_t i=0; i<ret; i++) {
138+
for (ssize_t i = 0; i < ret; i++) {
139139
if ('\n' == buf[i]) {
140140
for (auto& [match, prefix] : kInformationalPatterns) {
141141
auto pos = line_.find(match);

base/cvd/cuttlefish/host/commands/kernel_log_monitor/kernel_log_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <string>
2222
#include <vector>
2323

24-
#include <json/json.h>
24+
#include "json/json.h"
2525

2626
#include "cuttlefish/common/libs/fs/shared_fd.h"
2727
#include "cuttlefish/common/libs/fs/shared_select.h"

base/cvd/cuttlefish/host/commands/kernel_log_monitor/utils.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
#include <optional>
2323
#include <string>
2424

25-
#include <json/value.h>
26-
#include <json/writer.h>
2725
#include "absl/log/log.h"
26+
#include "json/value.h"
27+
#include "json/writer.h"
2828

2929
#include "cuttlefish/common/libs/fs/shared_buf.h"
3030
#include "cuttlefish/common/libs/fs/shared_fd.h"

base/cvd/cuttlefish/host/commands/kernel_log_monitor/utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616
#pragma once
1717

18-
#include <json/json.h>
1918
#include <optional>
2019

20+
#include "json/json.h"
21+
2122
#include "cuttlefish/common/libs/fs/shared_fd.h"
2223
#include "cuttlefish/host/commands/kernel_log_monitor/kernel_log_server.h"
2324
#include "cuttlefish/result/result.h"

0 commit comments

Comments
 (0)