Fix C++ codegen namespace printer to print closing namespaces in reverse order.

This will make the comment correctly attribute the actual namespace being closed.

PiperOrigin-RevId: 679548777
pull/18504/head
Protobuf Team Bot 2 months ago committed by Copybara-Service
parent 0ea9685deb
commit 3bf9c400ec
  1. 5
      src/google/protobuf/compiler/cpp/namespace_printer.cc
  2. 8
      src/google/protobuf/compiler/cpp/namespace_printer_unittest.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));
}
}

@ -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

Loading…
Cancel
Save