Skip to content

Commit

Permalink
Initial part of adding nullability annotations to C++ generated code
Browse files Browse the repository at this point in the history
Add protobuf local macro similar to the very recently added absl version abseil/abseil-cpp@cd9dd42 The macro form allows Wnullability-completeness to check (vs absl's template form of annotation)

Introduce a flag+option that is default off. We will flip it on when ready and flip it on for one example file to see how it works as we add annotations.

The annotations will also be off in open source until we are ready.

PiperOrigin-RevId: 708322571
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 20, 2024
1 parent 05af18d commit c48d413
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/google/protobuf/compiler/cpp/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ std::string NumberedCcFileName(absl::string_view basename, int number) {
absl::flat_hash_map<absl::string_view, std::string> CommonVars(
const Options& options) {
bool is_oss = options.opensource_runtime;
bool annotate_nullability = options.annotate_nullability && !is_oss;
return {
{"proto_ns", std::string(ProtobufNamespace(options))},
{"pb", absl::StrCat("::", ProtobufNamespace(options))},
Expand All @@ -67,6 +68,9 @@ absl::flat_hash_map<absl::string_view, std::string> CommonVars(
{"hrule_thick", kThickSeparator},
{"hrule_thin", kThinSeparator},

{"nullable", annotate_nullability ? "PROTOBUF_NULLABLE" : ""},
{"nonnull", annotate_nullability ? "PROTOBUF_NONNULL" : ""},

// Warning: there is some clever naming/splitting here to avoid extract
// script rewrites. The names of these variables must not be things that
// the extract script will rewrite. That's why we use "CHK" (for example)
Expand Down Expand Up @@ -186,6 +190,16 @@ bool CppGenerator::Generate(const FileDescriptor* file,
file_options.force_eagerly_verified_lazy = true;
} else if (key == "experimental_strip_nonfunctional_codegen") {
file_options.strip_nonfunctional_codegen = true;
} else if (key == "annotate_nullability") {
int enable_annotate_nullability = 0;
if (absl::SimpleAtoi(value, &enable_annotate_nullability) &&
(enable_annotate_nullability == 0 ||
enable_annotate_nullability == 1)) {
file_options.annotate_nullability = enable_annotate_nullability != 0;
} else {
*error = absl::StrCat("Invalid value for ", key, ": ", value);
return false;
}
} else {
*error = absl::StrCat("Unknown generator option: ", key);
return false;
Expand Down
3 changes: 3 additions & 0 deletions src/google/protobuf/compiler/cpp/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ struct Options {
bool force_inline_string = false;
#endif // !PROTOBUF_STABLE_EXPERIMENTS
bool strip_nonfunctional_codegen = false;
// TODO: clean this up once the change is complete
// and rolled out.
bool annotate_nullability = false;
};

} // namespace cpp
Expand Down
12 changes: 12 additions & 0 deletions src/google/protobuf/port_def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,18 @@ namespace internal {
#define PROTOBUF_DEPRECATE_AND_INLINE() [[deprecated]]
#endif

#ifdef PROTOBUF_NONNULL
#error PROTOBUF_NONNULL was previously defined
#endif
#if defined(__clang__) && !defined(__OBJC__) && \
ABSL_HAVE_FEATURE(nullability_on_classes)
#define PROTOBUF_NONNULL _Nonnull
#define PROTOBUF_NULLABLE _Nullable
#else
#define PROTOBUF_NONNULL
#define PROTOBUF_NULLABLE
#endif

namespace google {
namespace protobuf {
namespace internal {
Expand Down
2 changes: 2 additions & 0 deletions src/google/protobuf/port_undef.inc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
#undef PROTOBUF_TC_PARAM_DECL
#undef PROTOBUF_DEBUG
#undef PROTOBUF_NO_THREADLOCAL
#undef PROTOBUF_NONNULL
#undef PROTOBUF_NULLABLE

#ifdef PROTOBUF_FUTURE_BREAKING_CHANGES
#undef PROTOBUF_FUTURE_BREAKING_CHANGES
Expand Down

0 comments on commit c48d413

Please sign in to comment.