Gate typeid(T).name() calls behind PROTOBUF_RTTI.

Closes #11742

PiperOrigin-RevId: 506705023
pull/11759/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 6816da50cf
commit 322df1b2ea
  1. 15
      src/google/protobuf/json/internal/untyped_message.cc
  2. 16
      src/google/protobuf/port.h

@ -41,9 +41,6 @@
#include <vector>
#include "google/protobuf/type.pb.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/message.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
@ -54,8 +51,8 @@
#include "absl/types/span.h"
#include "absl/types/variant.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/port.h"
#include "google/protobuf/util/type_resolver.h"
#include "google/protobuf/wire_format.h"
#include "google/protobuf/wire_format_lite.h"
#include "utf8_validity.h"
#include "google/protobuf/stubs/status_macros.h"
@ -536,10 +533,16 @@ absl::Status UntypedMessage::InsertField(const ResolverPool::Field& field,
} else if (auto* extant = absl::get_if<std::vector<T>>(&slot)) {
extant->push_back(std::move(value));
} else {
absl::optional<absl::string_view> name =
google::protobuf::internal::RttiTypeName<T>();
if (!name.has_value()) {
name = "<unknown>";
}
return absl::InvalidArgumentError(
absl::StrFormat("inconsistent types for field number %d: tried to "
"insert %s, but index was %d",
number, typeid(T).name(), slot.index()));
"insert '%s', but index was %d",
number, *name, slot.index()));
}
return absl::OkStatus();

@ -42,8 +42,14 @@
#include <string>
#include <type_traits>
#if PROTOBUF_RTTI
#include <typeinfo>
#endif
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
// must be last
#include "google/protobuf/port_def.inc"
@ -164,6 +170,16 @@ inline ToRef DownCast(From& f) {
return *static_cast<To*>(&f);
}
// Looks up the name of `T` via RTTI, if RTTI is available.
template <typename T>
inline absl::optional<absl::string_view> RttiTypeName() {
#if PROTOBUF_RTTI
return typeid(T).name();
#else
return absl::nullopt;
#endif
}
// Helpers for identifying our supported types.
template <typename T>
struct is_supported_integral_type

Loading…
Cancel
Save