diff --git a/src/google/protobuf/compiler/cpp/namespace_printer.cc b/src/google/protobuf/compiler/cpp/namespace_printer.cc index 3531d7c737..4d170fbf05 100644 --- a/src/google/protobuf/compiler/cpp/namespace_printer.cc +++ b/src/google/protobuf/compiler/cpp/namespace_printer.cc @@ -26,8 +26,9 @@ NamespacePrinter::NamespacePrinter( NamespacePrinter::~NamespacePrinter() { // Close the namespace. - for (const std::string& ns : namespace_components_) { - p_->Print(absl::Substitute("} // namespace $0\n", ns)); + for (auto it = namespace_components_.rbegin(); + it != namespace_components_.rend(); ++it) { + p_->Print(absl::Substitute("} // namespace $0\n", *it)); } } diff --git a/src/google/protobuf/compiler/cpp/namespace_printer_unittest.cc b/src/google/protobuf/compiler/cpp/namespace_printer_unittest.cc index 3e7fab9b79..f18333135f 100644 --- a/src/google/protobuf/compiler/cpp/namespace_printer_unittest.cc +++ b/src/google/protobuf/compiler/cpp/namespace_printer_unittest.cc @@ -46,9 +46,9 @@ TEST_F(NamespacePrinterTest, Basic) { "namespace B {\n" "namespace E {\n" "\n" - "} // namespace A\n" + "} // namespace E\n" "} // namespace B\n" - "} // namespace E\n"); + "} // namespace A\n"); } TEST_F(NamespacePrinterTest, DifferentDelim) { @@ -65,9 +65,9 @@ TEST_F(NamespacePrinterTest, DifferentDelim) { "namespace B {\n" "namespace E {\n" "\n" - "} // namespace A\n" + "} // namespace E\n" "} // namespace B\n" - "} // namespace E\n"); + "} // namespace A\n"); } } // namespace