Skip to content

Commit 1087274

Browse files
Add an RAII ifndef helper for C++ codegen.
This can be used in headers to automatically generate ifdef guards. PiperOrigin-RevId: 618204633
1 parent eef5564 commit 1087274

File tree

5 files changed

+283
-0
lines changed

5 files changed

+283
-0
lines changed

pkg/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ cc_dist_library(
174174
"//src/google/protobuf:arena_align",
175175
"//src/google/protobuf:cmake_wkt_cc_proto",
176176
"//src/google/protobuf/compiler:importer",
177+
"//src/google/protobuf/io/cpp_utils:ifndef_guard",
177178
"//src/google/protobuf/json",
178179
"//src/google/protobuf/util:delimited_message_util",
179180
"//src/google/protobuf/util:differencer",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Utilities for generating C++ code
2+
3+
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
4+
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
5+
load("//build_defs:cpp_opts.bzl", "COPTS")
6+
7+
package(
8+
default_visibility = ["//visibility:public"],
9+
)
10+
11+
cc_library(
12+
name = "ifndef_guard",
13+
srcs = ["ifndef_guard.cc"],
14+
hdrs = ["ifndef_guard.h"],
15+
copts = COPTS,
16+
strip_include_prefix = "/src",
17+
deps = [
18+
"//src/google/protobuf/io:printer",
19+
"@com_google_absl//absl/functional:any_invocable",
20+
"@com_google_absl//absl/log:die_if_null",
21+
"@com_google_absl//absl/strings",
22+
"@com_google_absl//absl/strings:string_view",
23+
],
24+
)
25+
26+
cc_test(
27+
name = "ifndef_guard_unittest",
28+
srcs = ["ifndef_guard_unittest.cc"],
29+
deps = [
30+
":ifndef_guard",
31+
"//src/google/protobuf/io",
32+
"//src/google/protobuf/io:printer",
33+
"@com_google_absl//absl/log:absl_check",
34+
"@com_google_absl//absl/strings:string_view",
35+
"@com_google_absl//absl/types:optional",
36+
"@com_google_googletest//:gtest",
37+
"@com_google_googletest//:gtest_main",
38+
],
39+
)
40+
41+
################################################################################
42+
# Distribution packaging
43+
################################################################################
44+
45+
pkg_files(
46+
name = "dist_files",
47+
srcs = glob(["**/*"]),
48+
strip_prefix = strip_prefix.from_root(""),
49+
visibility = ["//src:__pkg__"],
50+
)
51+
52+
filegroup(
53+
name = "test_srcs",
54+
srcs = glob([
55+
"*unittest.cc",
56+
]),
57+
visibility = ["//pkg:__pkg__"],
58+
)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2024 Google LLC. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
#include "google/protobuf/io/cpp_utils/ifndef_guard.h"
9+
10+
#include <string>
11+
12+
#include "absl/functional/any_invocable.h"
13+
#include "absl/log/die_if_null.h"
14+
#include "absl/strings/ascii.h"
15+
#include "absl/strings/str_cat.h"
16+
#include "absl/strings/str_replace.h"
17+
#include "absl/strings/string_view.h"
18+
#include "absl/strings/substitute.h"
19+
#include "google/protobuf/io/printer.h"
20+
21+
namespace google {
22+
namespace protobuf {
23+
namespace io {
24+
namespace cpp {
25+
26+
namespace {
27+
28+
std::string MakeIfdefGuardIdentifier(const absl::string_view header_path) {
29+
return absl::StrCat(absl::AsciiStrToUpper(absl::StrReplaceAll(header_path,
30+
{
31+
{"/", "_"},
32+
{".", "_"},
33+
})),
34+
"_");
35+
}
36+
37+
} // namespace
38+
39+
IfdefGuardPrinter::IfdefGuardPrinter(google::protobuf::io::Printer* const p,
40+
const absl::string_view filename)
41+
: IfdefGuardPrinter(p, filename, MakeIfdefGuardIdentifier) {}
42+
43+
IfdefGuardPrinter::IfdefGuardPrinter(
44+
google::protobuf::io::Printer* const p, const absl::string_view filename,
45+
absl::AnyInvocable<std::string(absl::string_view)> make_ifdef_identifier)
46+
: p_(ABSL_DIE_IF_NULL(p)),
47+
ifdef_identifier_(make_ifdef_identifier(filename)) {
48+
// We can't use variable substitution, because we don't know what delimiter
49+
// to use.
50+
p->Print(absl::Substitute(
51+
R"(#ifndef $0
52+
#define $0
53+
54+
)",
55+
ifdef_identifier_));
56+
}
57+
58+
IfdefGuardPrinter::~IfdefGuardPrinter() {
59+
// We can't use variable substitution, because we don't know what delimiter
60+
// to use.
61+
p_->Print(absl::Substitute(
62+
R"(
63+
#endif // $0
64+
)",
65+
ifdef_identifier_));
66+
}
67+
68+
} // namespace cpp
69+
} // namespace io
70+
} // namespace protobuf
71+
} // namespace google
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2024 Google Inc. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
// An RAII type for printing an ifdef guard.
9+
//
10+
// This can be used to ensure that appropriate ifdef guards are applied in
11+
// a generated header file.
12+
//
13+
// Example:
14+
// {
15+
// Printer printer(output_stream.get(), '$');
16+
// const IfdefGuardPrinter ifdef_guard(&printer, output_path);
17+
// // #ifdef guard will be emitted here
18+
// ...
19+
// // #endif will be emitted here
20+
// }
21+
//
22+
// By default, the filename will be converted to a macro by substituting '/' and
23+
// '.' characters with '_'. If a different transformation is required, an
24+
// optional transformation function can be provided.
25+
26+
#ifndef GOOGLE_PROTOBUF_IO_CPP_UTILS_IFNDEF_GUARD_H__
27+
#define GOOGLE_PROTOBUF_IO_CPP_UTILS_IFNDEF_GUARD_H__
28+
29+
#include <string>
30+
31+
#include "absl/functional/any_invocable.h"
32+
#include "absl/strings/string_view.h"
33+
#include "google/protobuf/io/printer.h"
34+
35+
namespace google {
36+
namespace protobuf {
37+
namespace io {
38+
namespace cpp {
39+
40+
class IfdefGuardPrinter final {
41+
public:
42+
explicit IfdefGuardPrinter(google::protobuf::io::Printer* p,
43+
absl::string_view filename);
44+
45+
explicit IfdefGuardPrinter(
46+
google::protobuf::io::Printer* p, absl::string_view filename,
47+
absl::AnyInvocable<std::string(absl::string_view)> make_ifdef_identifier);
48+
49+
~IfdefGuardPrinter();
50+
51+
private:
52+
google::protobuf::io::Printer* const p_;
53+
const std::string ifdef_identifier_;
54+
};
55+
56+
} // namespace cpp
57+
} // namespace io
58+
} // namespace protobuf
59+
} // namespace google
60+
61+
#endif // GOOGLE_PROTOBUF_IO_CPP_UTILS_IFNDEF_GUARD_H__
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "google/protobuf/io/cpp_utils/ifndef_guard.h"
2+
3+
#include <string>
4+
5+
#include <gtest/gtest.h>
6+
#include "absl/log/absl_check.h"
7+
#include "absl/strings/string_view.h"
8+
#include "absl/types/optional.h"
9+
#include "google/protobuf/io/printer.h"
10+
#include "google/protobuf/io/zero_copy_stream.h"
11+
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
12+
13+
namespace google {
14+
namespace protobuf {
15+
namespace io {
16+
namespace cpp {
17+
18+
namespace {
19+
20+
class IfnDefGuardTest : public testing::Test {
21+
protected:
22+
ZeroCopyOutputStream* output() {
23+
ABSL_CHECK(stream_.has_value());
24+
return &*stream_;
25+
}
26+
absl::string_view written() {
27+
stream_.reset();
28+
return out_;
29+
}
30+
31+
std::string out_;
32+
absl::optional<StringOutputStream> stream_{&out_};
33+
};
34+
35+
TEST_F(IfnDefGuardTest, Basic) {
36+
{
37+
Printer printer(output(), '$');
38+
39+
const IfdefGuardPrinter ifdef_guard(&printer, "A/B/E/alpha");
40+
41+
EXPECT_FALSE(printer.failed());
42+
}
43+
44+
EXPECT_EQ(written(),
45+
"#ifndef A_B_E_ALPHA_\n"
46+
"#define A_B_E_ALPHA_\n"
47+
"\n"
48+
"\n"
49+
"#endif // A_B_E_ALPHA_\n");
50+
}
51+
52+
TEST_F(IfnDefGuardTest, DifferentDelim) {
53+
{
54+
Printer printer(output(), '\0');
55+
56+
const IfdefGuardPrinter ifdef_guard(&printer, "A/B/E/alpha");
57+
58+
EXPECT_FALSE(printer.failed());
59+
}
60+
61+
EXPECT_EQ(written(),
62+
"#ifndef A_B_E_ALPHA_\n"
63+
"#define A_B_E_ALPHA_\n"
64+
"\n"
65+
"\n"
66+
"#endif // A_B_E_ALPHA_\n");
67+
}
68+
69+
TEST_F(IfnDefGuardTest, DifferentSubstitutionFunction) {
70+
{
71+
Printer printer(output(), '$');
72+
73+
const IfdefGuardPrinter ifdef_guard(
74+
&printer, "A/B/E/alpha", [](absl::string_view) { return "FOO_BAR_"; });
75+
76+
EXPECT_FALSE(printer.failed());
77+
}
78+
79+
EXPECT_EQ(written(),
80+
"#ifndef FOO_BAR_\n"
81+
"#define FOO_BAR_\n"
82+
"\n"
83+
"\n"
84+
"#endif // FOO_BAR_\n");
85+
}
86+
87+
} // namespace
88+
} // namespace cpp
89+
} // namespace io
90+
91+
} // namespace protobuf
92+
} // namespace google

0 commit comments

Comments
 (0)