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)

The macro will be empty unless you define PROTOBUF_TEMPORARY_NULL_ANNOTATIONS. We will switch that on once all annotations are in. The annotations will also be off in open source until we are ready.

PiperOrigin-RevId: 711805580
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 3, 2025
1 parent eef246e commit 45ba25b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/google/protobuf/compiler/cpp/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ absl::flat_hash_map<absl::string_view, std::string> CommonVars(
{"hrule_thick", kThickSeparator},
{"hrule_thin", kThinSeparator},

{"nullable", "PROTOBUF_NULLABLE"},
{"nonnull", "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
8 changes: 8 additions & 0 deletions src/google/protobuf/port_def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,14 @@ namespace internal {
#define PROTOBUF_DEPRECATE_AND_INLINE() [[deprecated]]
#endif

#ifdef PROTOBUF_NONNULL
#error PROTOBUF_NONNULL was previously defined
#endif
#ifndef PROTOBUF_NONNULL
#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 45ba25b

Please sign in to comment.