Skip to content

Commit

Permalink
Fix C++ codegen namespace printer to print closing namespaces in reve…
Browse files Browse the repository at this point in the history
…rse order.

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

PiperOrigin-RevId: 679548777
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 27, 2024
1 parent 0ea9685 commit 3bf9c40
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/google/protobuf/compiler/cpp/namespace_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit 3bf9c40

Please sign in to comment.