Skip to content

Commit 23aada2

Browse files
Add [[deprecated]] attribute when generating enums and classes.
When the user adds `option deprecated = true` for an `enum` or `message`, we should add the `[[deprecated]]` attribute to the generated C++ `enum` or `class`. PiperOrigin-RevId: 670583253
1 parent c935f72 commit 23aada2

File tree

9 files changed

+92
-43
lines changed

9 files changed

+92
-43
lines changed

ci/Linux.bazelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import common.bazelrc
22

33
build --cxxopt=-std=c++14 --host_cxxopt=-std=c++14
4-
build --copt="-Werror" --copt="-Wno-sign-compare" --copt="-Wno-sign-conversion" --copt="-Wno-error=sign-conversion"
4+
build --copt="-Werror" --copt="-Wno-sign-compare" --copt="-Wno-sign-conversion" --copt="-Wno-error=sign-conversion" --copt="-Wno-deprecated-declarations"

ci/macOS.bazelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import common.bazelrc
22

33
build --cxxopt=-std=c++14 --host_cxxopt=-std=c++14
4-
build --copt="-Werror" --copt="-Wno-sign-compare" --copt="-Wno-sign-conversion" --copt="-Wno-error=sign-conversion"
4+
build --copt="-Werror" --copt="-Wno-sign-compare" --copt="-Wno-sign-conversion" --copt="-Wno-error=sign-conversion" --copt="-Wno-deprecated-declarations"
55
common --repo_env=BAZEL_NO_APPLE_CPP_TOOLCHAIN=1
66
common --xcode_version_config=@com_google_protobuf//.github:host_xcodes

src/google/protobuf/compiler/cpp/enum.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ absl::flat_hash_map<absl::string_view, std::string> EnumVars(
4444
const EnumValueDescriptor* min, const EnumValueDescriptor* max) {
4545
auto classname = ClassName(enum_, false);
4646
return {
47+
{"DEPRECATED", enum_->options().deprecated() ? "[[deprecated]]" : ""},
4748
{"Enum", std::string(enum_->name())},
4849
{"Enum_", ResolveKeyword(enum_->name())},
4950
{"Msg_Enum", classname},
@@ -162,7 +163,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
162163
}},
163164
},
164165
R"cc(
165-
enum $Msg_Enum_annotated$ : int {
166+
enum $DEPRECATED $$Msg_Enum_annotated$ : int {
166167
$values$,
167168
$open_enum_sentinels$,
168169
};

src/google/protobuf/compiler/cpp/message.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ std::vector<Sub> ClassVars(const Descriptor* desc, Options opts) {
501501
{"Msg", ClassName(desc, false)},
502502
{"pkg::Msg", QualifiedClassName(desc, opts)},
503503
{"pkg.Msg", desc->full_name()},
504+
{"deprecated", desc->options().deprecated() ? "[[deprecated]]" : ""},
504505

505506
// Old-style names, to be removed once all usages are gone in this and
506507
// other files.
@@ -2060,7 +2061,8 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
20602061
)cc");
20612062
}}},
20622063
R"cc(
2063-
class $dllexport_decl $$classname$ final : public $superclass$
2064+
class $dllexport_decl $$deprecated $$classname$ final
2065+
: public $superclass$
20642066
/* @@protoc_insertion_point(class_definition:$full_name$) */ {
20652067
public:
20662068
inline $classname$() : $classname$(nullptr) {}

src/google/protobuf/compiler/java/java_features.pb.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/compiler/plugin.pb.h

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/cpp_features.pb.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)