Skip to content

Commit d8c1390

Browse files
committed
Sample clang-format enforcement on kernel_log_monitor
- Formats kernel_log_monitor/main.cc - Example format_test enforcing the file stays formatted correctly - Creates a symlink to the root .clang-tidy file within the bazel workspace Assisted-by: Jetski <jetski@google.com> Bug: b/512215781
1 parent 0e96f74 commit d8c1390

5 files changed

Lines changed: 36 additions & 12 deletions

File tree

base/cvd/.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../.clang-format

base/cvd/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ package(
1212
default_visibility = ["//:android_cuttlefish"],
1313
)
1414

15-
exports_files([".clang-tidy"])
15+
exports_files([
16+
".clang-format",
17+
".clang-tidy",
18+
])
1619

1720
genrule(
1821
name = "build_compilation_mode_txt",

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

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

34
package(
45
default_visibility = ["//:android_cuttlefish"],
@@ -28,6 +29,13 @@ cf_cc_binary(
2829
],
2930
)
3031

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+
3139
cf_cc_library(
3240
name = "kernel_log_monitor_utils",
3341
srcs = [

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
#include <string>
2424
#include <vector>
2525

26-
#include "absl/strings/str_split.h"
27-
#include <gflags/gflags.h>
28-
#include <json/value.h>
2926
#include "absl/log/check.h"
3027
#include "absl/log/log.h"
28+
#include "absl/strings/str_split.h"
29+
#include "gflags/gflags.h"
30+
#include "json/value.h"
3131

3232
#include "cuttlefish/common/libs/fs/shared_fd.h"
3333
#include "cuttlefish/common/libs/fs/shared_select.h"
@@ -42,17 +42,18 @@ DEFINE_int32(log_pipe_fd, -1,
4242
"A file descriptor representing a (UNIX) socket from which to "
4343
"read the logs. If -1 is given the socket is created according to "
4444
"the instance configuration");
45-
DEFINE_string(subscriber_fds, "",
46-
"A comma separated list of file descriptors (most likely pipes) to"
47-
" send kernel log events to.");
45+
DEFINE_string(
46+
subscriber_fds, "",
47+
"A comma separated list of file descriptors (most likely pipes) to"
48+
" send kernel log events to.");
4849

4950
namespace cuttlefish::monitor {
5051
namespace {
5152

5253
std::vector<SharedFD> SubscribersFromCmdline() {
5354
// Validate the parameter
5455
std::string fd_list = FLAGS_subscriber_fds;
55-
for (auto c: fd_list) {
56+
for (auto c : fd_list) {
5657
if (c != ',' && (c < '0' || c > '9')) {
5758
LOG(ERROR) << "Invalid file descriptor list: " << fd_list;
5859
std::exit(1);
@@ -61,7 +62,7 @@ std::vector<SharedFD> SubscribersFromCmdline() {
6162

6263
std::vector<std::string> fds = absl::StrSplit(FLAGS_subscriber_fds, ',');
6364
std::vector<SharedFD> shared_fds;
64-
for (auto& fd_str: fds) {
65+
for (auto& fd_str : fds) {
6566
auto fd = std::stoi(fd_str);
6667
auto shared_fd = SharedFD::Dup(fd);
6768
close(fd);
@@ -84,8 +85,7 @@ int KernelLogMonitorMain(int argc, char** argv) {
8485
auto subscriber_fds = SubscribersFromCmdline();
8586

8687
// Disable default handling of SIGPIPE
87-
struct sigaction new_action {
88-
}, old_action{};
88+
struct sigaction new_action{}, old_action{};
8989
new_action.sa_handler = SIG_IGN;
9090
sigaction(SIGPIPE, &new_action, &old_action);
9191

@@ -105,7 +105,7 @@ int KernelLogMonitorMain(int argc, char** argv) {
105105

106106
KernelLogServer klog{pipe, instance.PerInstanceLogPath(kLogNameKernel)};
107107

108-
for (auto subscriber_fd: subscriber_fds) {
108+
for (auto subscriber_fd : subscriber_fds) {
109109
if (subscriber_fd->IsOpen()) {
110110
klog.SubscribeToEvents([subscriber_fd](Json::Value message) {
111111
if (!WriteEvent(subscriber_fd, message)) {

base/cvd/tools/format/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
2+
3+
package(
4+
default_visibility = ["//:android_cuttlefish"],
5+
)
6+
7+
native_binary(
8+
name = "clang_format",
9+
src = "@llvm_toolchain//:clang-format",
10+
out = "clang_format",
11+
data = ["//:.clang-format"],
12+
)

0 commit comments

Comments
 (0)