Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 484326389
  • Loading branch information
protobuf-github-bot authored and haberman committed Oct 27, 2022
1 parent 240986d commit afaf93b
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/google/protobuf/arena_align.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace protobuf {
namespace internal {

struct ArenaAlignDefault {
static constexpr size_t align = 8; // NOLINT
PROTOBUF_EXPORT static constexpr size_t align = 8; // NOLINT

static constexpr bool IsAligned(size_t n) { return (n & (align - 1)) == 0; }

Expand Down
12 changes: 6 additions & 6 deletions src/google/protobuf/arenastring.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ class TaggedStringPtr {
// Bit flags qualifying string properties. We can use 2 bits as
// ptr_ is guaranteed and enforced to be aligned on 4 byte boundaries.
enum Flags {
kArenaBit = 0x1, // ptr is arena allocated
kMutableBit = 0x2, // ptr contents are fully mutable
kMask = 0x3 // Bit mask
kArenaBit = 0x1, // ptr is arena allocated
kMutableBit = 0x2, // ptr contents are fully mutable
kMask = 0x3 // Bit mask
};

// Composed logical types
Expand Down Expand Up @@ -168,7 +168,7 @@ class TaggedStringPtr {

// If the current string is a heap-allocated mutable value, returns a pointer
// to it. Returns nullptr otherwise.
inline std::string *GetIfAllocated() const {
inline std::string* GetIfAllocated() const {
auto allocated = as_int() ^ kAllocated;
if (allocated & kMask) return nullptr;

Expand Down Expand Up @@ -423,8 +423,8 @@ inline void ArenaStringPtr::SetBytes(absl::string_view value, Arena* arena) {
}

template <>
PROTOBUF_EXPORT
void ArenaStringPtr::Set(const std::string& value, Arena* arena);
PROTOBUF_EXPORT void ArenaStringPtr::Set(const std::string& value,
Arena* arena);

template <>
inline void ArenaStringPtr::SetBytes(const std::string& value, Arena* arena) {
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ cc_library(
":code_generator",
":importer",
"//src/google/protobuf:protobuf_nowkt",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
],
Expand Down
3 changes: 2 additions & 1 deletion src/google/protobuf/compiler/command_line_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "google/protobuf/compiler/command_line_interface.h"

#include "absl/container/btree_map.h"
#include "absl/container/flat_hash_map.h"

#include "google/protobuf/stubs/platform_macros.h"
Expand Down Expand Up @@ -427,7 +428,7 @@ class CommandLineInterface::GeneratorContextImpl : public GeneratorContext {
// The files_ field maps from path keys to file content values. It's a map
// instead of an unordered_map so that files are written in order (good when
// writing zips).
absl::flat_hash_map<std::string, std::string> files_;
absl::btree_map<std::string, std::string> files_;
const std::vector<const FileDescriptor*>& parsed_files_;
bool had_error_;
};
Expand Down
5 changes: 2 additions & 3 deletions src/google/protobuf/compiler/csharp/csharp_map_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ void MapFieldGenerator::GenerateMembers(io::Printer* printer) {
}

void MapFieldGenerator::GenerateMergingCode(io::Printer* printer) {
printer->Print(
variables_,
"$name$_.MergeFrom(other.$name$_);\n");
printer->Print(variables_,
"$name$_.MergeFrom(other.$name$_);\n");
}

void MapFieldGenerator::GenerateParsingCode(io::Printer* printer) {
Expand Down
4 changes: 4 additions & 0 deletions src/google/protobuf/compiler/objectivec/enum_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ void RepeatedEnumFieldGenerator::FinishInitialization() {
variables_["storage_type"] + "|\n";
}

// NOTE: RepeatedEnumFieldGenerator::DetermineForwardDeclarations isn't needed
// because `GPBEnumArray` isn't generic (like `NSArray` would be for messages)
// and thus doesn't reference the type in the header.

} // namespace objectivec
} // namespace compiler
} // namespace protobuf
Expand Down
5 changes: 5 additions & 0 deletions src/google/protobuf/compiler/objectivec/map_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ void MapFieldGenerator::DetermineForwardDeclarations(
std::set<std::string>* fwd_decls, bool include_external_types) const {
RepeatedFieldGenerator::DetermineForwardDeclarations(fwd_decls,
include_external_types);
// NOTE: Maps with values of enums don't have to worry about adding the
// forward declaration because `GPB*EnumDictionary` isn't generic to the
// specific enum (like say `NSDictionary<String, MyMessage>`) and thus doesn't
// reference the type in the header.

const FieldDescriptor* value_descriptor =
descriptor_->message_type()->map_value();
// Within a file there is no requirement on the order of the messages, so
Expand Down
15 changes: 14 additions & 1 deletion src/google/protobuf/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
#include "google/protobuf/port.h"
#include "absl/base/call_once.h"
#include "absl/base/casts.h"
#include "absl/functional/function_ref.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/generated_message_reflection.h"
Expand Down Expand Up @@ -181,7 +182,10 @@ class CelMapReflectionFriend; // field_backed_map_impl.cc

namespace internal {
class MapFieldPrinterHelper; // text_format.cc
}
void PerformAbslStringify(
const Message& message,
absl::FunctionRef<void(absl::string_view)> append); // text_format.cc
} // namespace internal
namespace util {
class MessageDifferencer;
}
Expand Down Expand Up @@ -330,6 +334,15 @@ class PROTOBUF_EXPORT Message : public MessageLite {
// Convenience function useful in GDB. Prints DebugString() to stdout.
void PrintDebugString() const;

// Implementation of the `AbslStringify` interface. This adds something
// similar to either `ShortDebugString()` or `DebugString()` to the sink.
// Do not rely on exact format.
template <typename Sink>
friend void AbslStringify(Sink& sink, const google::protobuf::Message& message) {
internal::PerformAbslStringify(
message, [&](absl::string_view content) { sink.Append(content); });
}

// Reflection-based methods ----------------------------------------
// These methods are pure-virtual in MessageLite, but Message provides
// reflection-based default implementations.
Expand Down
17 changes: 17 additions & 0 deletions src/google/protobuf/text_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ std::string Message::Utf8DebugString() const {

void Message::PrintDebugString() const { printf("%s", DebugString().c_str()); }

namespace internal {

void PerformAbslStringify(const Message& message,
absl::FunctionRef<void(absl::string_view)> append) {
// TODO(b/249835002): consider using the single line version for short
TextFormat::Printer printer;
printer.SetExpandAny(true);
printer.SetInsertSilentMarker(true);
printer.SetRedactDebugString(true);
printer.SetRandomizeDebugString(true);
std::string result;
printer.PrintToString(message, &result);
append(result);
}

} // namespace internal


// ===========================================================================
// Implementation of the parse information tree class.
Expand Down
3 changes: 3 additions & 0 deletions src/google/protobuf/text_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ class PROTOBUF_EXPORT TextFormat {
friend std::string Message::DebugString() const;
friend std::string Message::ShortDebugString() const;
friend std::string Message::Utf8DebugString() const;
friend void internal::PerformAbslStringify(
const Message& message,
absl::FunctionRef<void(absl::string_view)> append);

// Sets whether silent markers will be inserted.
void SetInsertSilentMarker(bool v) { insert_silent_marker_ = v; }
Expand Down
4 changes: 4 additions & 0 deletions src/google/protobuf/text_format_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "google/protobuf/stubs/logging.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/test_util.h"
Expand All @@ -76,6 +77,8 @@ namespace protobuf {
namespace text_format_unittest {

using ::google::protobuf::internal::kDebugStringSilentMarker;
using ::testing::AllOf;
using ::testing::HasSubstr;

// A basic string with different escapable characters for testing.
const std::string kEscapeTestString =
Expand Down Expand Up @@ -158,6 +161,7 @@ TEST_F(TextFormatTest, ShortDebugString) {
"optional_foreign_message { }"));
}


TEST_F(TextFormatTest, ShortPrimitiveRepeateds) {
proto_.set_optional_int32(123);
proto_.add_repeated_int32(456);
Expand Down

0 comments on commit afaf93b

Please sign in to comment.