Skip to content

Commit

Permalink
Delete PROTOBUF_NAMESPACE_* macros.
Browse files Browse the repository at this point in the history
These are no longer needed thanks to how our OSS sync works.

PiperOrigin-RevId: 529413361
  • Loading branch information
fowles authored and copybara-github committed May 4, 2023
1 parent 2f78ca7 commit a37f628
Show file tree
Hide file tree
Showing 24 changed files with 3,855 additions and 3,863 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void SetCordVariables(
(*variables)["default_variable_field"] = MakeDefaultFieldName(descriptor);
(*variables)["default_variable"] =
descriptor->default_value_string().empty()
? ProtobufNamespace(options) +
"::internal::GetEmptyCordAlreadyInited()"
? absl::StrCat(ProtobufNamespace(options),
"::internal::GetEmptyCordAlreadyInited()")
: absl::StrCat(
QualifiedClassName(descriptor->containing_type(), options),
"::", MakeDefaultFieldName(descriptor));
Expand Down Expand Up @@ -289,8 +289,7 @@ void CordFieldGenerator::GenerateAggregateInitializer(

CordOneofFieldGenerator::CordOneofFieldGenerator(
const FieldDescriptor* descriptor, const Options& options)
: CordFieldGenerator(descriptor, options) {
}
: CordFieldGenerator(descriptor, options) {}

void CordOneofFieldGenerator::GeneratePrivateMembers(
io::Printer* printer) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ std::vector<Sub> Vars(const FieldDescriptor* field, const Options& opts,
}

return {
{"Map", absl::Substitute("::PROTOBUF_NAMESPACE_ID::Map<$0, $1>", key_type,
val_type)},
{"Map", absl::Substitute("::google::protobuf::Map<$0, $1>", key_type, val_type)},
{"Entry", ClassName(field->message_type(), false)},
{"Key", PrimitiveTypeName(opts, key->cpp_type())},
{"Val", val_type},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::vector<Sub> Vars(const FieldDescriptor* field, const Options& opts,
QualifiedDefaultInstanceName(field->message_type(), opts);
std::string default_ptr =
QualifiedDefaultInstancePtr(field->message_type(), opts);
absl::string_view base = "::PROTOBUF_NAMESPACE_ID::MessageLite";
absl::string_view base = "::google::protobuf::MessageLite";

return {
{"Submsg", type},
Expand All @@ -87,11 +87,9 @@ std::vector<Sub> Vars(const FieldDescriptor* field, const Options& opts,
{".weak", weak ? ".weak" : ""},
Sub("StrongRef",
!weak ? ""
: absl::Substitute(
" "
"::PROTOBUF_NAMESPACE_ID::internal::StrongReference("
"reinterpret_cast<const $0&>($1));\n",
type, default_ref))
: absl::Substitute("::google::protobuf::internal::StrongReference("
"reinterpret_cast<const $0&>($1));\n",
type, default_ref))
.WithSuffix(";"),
};
}
Expand Down
18 changes: 12 additions & 6 deletions src/google/protobuf/compiler/cpp/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "google/protobuf/compiler/cpp/file.h"

#include <functional>
#include <iostream>
#include <memory>
#include <string>
Expand Down Expand Up @@ -207,6 +208,15 @@ void FileGenerator::GenerateSharedHeaderCode(io::Printer* p) {
{"undefs", [&] { GenerateMacroUndefs(p); }},
{"global_state_decls",
[&] { GenerateGlobalStateFunctionDeclarations(p); }},
{"any_metadata",
[&] {
NamespaceOpener ns(ProtobufNamespace(options_), p);
p->Emit(R"cc(
namespace internal {
class AnyMetadata;
} // namespace internal
)cc");
}},
{"fwd_decls", [&] { GenerateForwardDeclarations(p); }},
{"proto2_ns_enums",
[&] { GenerateProto2NamespaceEnumSpecializations(p); }},
Expand Down Expand Up @@ -250,11 +260,7 @@ void FileGenerator::GenerateSharedHeaderCode(io::Printer* p) {
#define $dllexport_macro$$ dllexport_decl$
$undefs$
PROTOBUF_NAMESPACE_OPEN
namespace internal {
class AnyMetadata;
} // namespace internal
PROTOBUF_NAMESPACE_CLOSE
$any_metadata$;
$global_state_decls$;
$fwd_decls$
Expand Down Expand Up @@ -1230,7 +1236,7 @@ void FileGenerator::GenerateForwardDeclarations(io::Printer* p) {
decl.second.Print(p, options_);
}

ns.ChangeTo("PROTOBUF_NAMESPACE_ID");
ns.ChangeTo(ProtobufNamespace(options_));
for (const auto& decl : decls) {
decl.second.PrintTopLevelDecl(p, options_);
}
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/cpp/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ absl::flat_hash_map<absl::string_view, std::string> CommonVars(
const Options& options) {
bool is_oss = options.opensource_runtime;
return {
{"proto_ns", ProtobufNamespace(options)},
{"proto_ns", std::string(ProtobufNamespace(options))},
{"pb", absl::StrCat("::", ProtobufNamespace(options))},
{"pbi", absl::StrCat("::", ProtobufNamespace(options), "::internal")},

Expand Down
54 changes: 13 additions & 41 deletions src/google/protobuf/compiler/cpp/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ namespace google {
namespace protobuf {
namespace compiler {
namespace cpp {

namespace {

static const char kAnyMessageName[] = "Any";
static const char kAnyProtoFile[] = "google/protobuf/any.proto";
constexpr absl::string_view kAnyMessageName = "Any";
constexpr absl::string_view kAnyProtoFile = "google/protobuf/any.proto";

std::string DotsToColons(absl::string_view name) {
return absl::StrReplaceAll(name, {{".", "::"}});
Expand Down Expand Up @@ -257,18 +255,17 @@ void SetCommonMessageDataVariables(

absl::flat_hash_map<absl::string_view, std::string> UnknownFieldsVars(
const Descriptor* desc, const Options& opts) {
std::string proto_ns = ProtobufNamespace(opts);

std::string unknown_fields_type;
std::string default_instance;
if (UseUnknownFieldSet(desc->file(), opts)) {
unknown_fields_type = absl::StrCat("::", proto_ns, "::UnknownFieldSet");
unknown_fields_type =
absl::StrCat("::", ProtobufNamespace(opts), "::UnknownFieldSet");
default_instance = absl::StrCat(unknown_fields_type, "::default_instance");
} else {
unknown_fields_type =
PrimitiveTypeName(opts, FieldDescriptor::CPPTYPE_STRING);
default_instance =
absl::StrCat("::", proto_ns, "::internal::GetEmptyString");
default_instance = absl::StrCat("::", ProtobufNamespace(opts),
"::internal::GetEmptyString");
}

return {
Expand Down Expand Up @@ -457,18 +454,7 @@ std::string Namespace(absl::string_view package) {

std::string Namespace(const FileDescriptor* d) { return Namespace(d, {}); }
std::string Namespace(const FileDescriptor* d, const Options& options) {
std::string ns = Namespace(d->package());
if (IsWellKnownMessage(d) && options.opensource_runtime) {
// Written with string concatenation to prevent rewriting of
// ::google::protobuf.
constexpr absl::string_view prefix =
"::google::" // prevent clang-format reflowing
"protobuf";
absl::string_view new_ns(ns);
absl::ConsumePrefix(&new_ns, prefix);
return absl::StrCat("::PROTOBUF_NAMESPACE_ID", new_ns);
}
return ns;
return Namespace(d->package());
}

std::string Namespace(const Descriptor* d) { return Namespace(d, {}); }
Expand Down Expand Up @@ -1161,28 +1147,14 @@ void NamespaceOpener::ChangeTo(absl::string_view name) {
}

for (size_t i = name_stack_.size(); i > common_idx; i--) {
const auto& ns = name_stack_[i - 1];
if (ns == "PROTOBUF_NAMESPACE_ID") {
p_->Emit(R"cc(
PROTOBUF_NAMESPACE_CLOSE
)cc");
} else {
p_->Emit({{"ns", ns}}, R"(
} // namespace $ns$
)");
}
p_->Emit({{"ns", name_stack_[i - 1]}}, R"(
} // namespace $ns$
)");
}
for (size_t i = common_idx; i < new_stack.size(); ++i) {
const auto& ns = new_stack[i];
if (ns == "PROTOBUF_NAMESPACE_ID") {
p_->Emit(R"cc(
PROTOBUF_NAMESPACE_OPEN
)cc");
} else {
p_->Emit({{"ns", ns}}, R"(
namespace $ns$ {
)");
}
p_->Emit({{"ns", new_stack[i]}}, R"(
namespace $ns$ {
)");
}

name_stack_ = std::move(new_stack);
Expand Down
32 changes: 17 additions & 15 deletions src/google/protobuf/compiler/cpp/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,20 @@
#include <string>
#include <tuple>

#include "google/protobuf/compiler/scc.h"
#include "google/protobuf/compiler/code_generator.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/absl_check.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/cpp/names.h"
#include "google/protobuf/compiler/cpp/options.h"
#include "google/protobuf/compiler/scc.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/port.h"
#include "absl/strings/str_cat.h"
#include "google/protobuf/io/printer.h"
#include "google/protobuf/port.h"


// Must be included last.
#include "google/protobuf/port_def.inc"
Expand All @@ -64,23 +63,27 @@ namespace google {
namespace protobuf {
namespace compiler {
namespace cpp {

enum class ArenaDtorNeeds { kNone = 0, kOnDemand = 1, kRequired = 2 };

inline std::string ProtobufNamespace(const Options& /* options */) {
return "PROTOBUF_NAMESPACE_ID";
inline absl::string_view ProtobufNamespace(const Options& opts) {
// This won't be transformed by copybara, since copybara looks for google::protobuf::.
constexpr absl::string_view kGoogle3Ns = "proto2";
constexpr absl::string_view kOssNs = "google::protobuf";

return opts.opensource_runtime ? kOssNs : kGoogle3Ns;
}

inline std::string MacroPrefix(const Options& /* options */) {
return "GOOGLE_PROTOBUF";
inline std::string MacroPrefix(const Options& options) {
// Constants are different in the internal and external version.
return options.opensource_runtime ? "GOOGLE_PROTOBUF" : "GOOGLE_PROTOBUF";
}

inline std::string DeprecatedAttribute(const Options& /* options */,
inline std::string DeprecatedAttribute(const Options&,
const FieldDescriptor* d) {
return d->options().deprecated() ? "[[deprecated]] " : "";
}

inline std::string DeprecatedAttribute(const Options& /* options */,
inline std::string DeprecatedAttribute(const Options&,
const EnumValueDescriptor* d) {
return d->options().deprecated() ? "[[deprecated]] " : "";
}
Expand Down Expand Up @@ -393,8 +396,7 @@ bool ShouldSplit(const FieldDescriptor* field, const Options& options);
bool ShouldForceAllocationOnConstruction(const Descriptor* desc,
const Options& options);

inline bool IsFieldUsed(const FieldDescriptor* /* field */,
const Options& /* options */) {
inline bool IsFieldUsed(const FieldDescriptor* /* field */, const Options&) {
return true;
}

Expand Down
7 changes: 3 additions & 4 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
"#endif // !PROTOBUF_FORCE_COPY_IN_SWAP\n"
" InternalSwap(other);\n"
" } else {\n"
" ::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other);\n"
" $pbi$::GenericSwap(this, other);\n"
" }\n"
"}\n"
"void UnsafeArenaSwap($classname$* other) {\n"
Expand Down Expand Up @@ -2022,8 +2022,7 @@ void MessageGenerator::GenerateClassMethods(io::Printer* p) {
format(
"void $classname$::PrepareSplitMessageForWrite() {\n"
" if (IsSplitMessageDefault()) {\n"
" void* chunk = "
"::PROTOBUF_NAMESPACE_ID::internal::CreateSplitMessageGeneric("
" void* chunk = $pbi$::CreateSplitMessageGeneric("
"GetArenaForAllocation(), &$1$, sizeof(Impl_::Split), this, &$2$);\n"
" $split$ = reinterpret_cast<Impl_::Split*>(chunk);\n"
" }\n"
Expand Down Expand Up @@ -3139,7 +3138,7 @@ void MessageGenerator::GenerateSwap(io::Printer* p) {
});

format(
"::PROTOBUF_NAMESPACE_ID::internal::memswap<\n"
"$pbi$::memswap<\n"
" PROTOBUF_FIELD_OFFSET($classname$, $last$)\n"
" + sizeof($classname$::$last$)\n"
" - PROTOBUF_FIELD_OFFSET($classname$, $first$)>(\n"
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,5 @@ int ProtobufMain(int argc, char* argv[]) {
} // namespace google

int main(int argc, char* argv[]) {
return PROTOBUF_NAMESPACE_ID::compiler::ProtobufMain(argc, argv);
return google::protobuf::compiler::ProtobufMain(argc, argv);
}
Loading

0 comments on commit a37f628

Please sign in to comment.