Make NamespaceOpener track source locations

PiperOrigin-RevId: 554504627
pull/13398/head
Matt Kulukundis 1 year ago committed by Copybara-Service
parent 7656a6107d
commit 3271478450
  1. 9
      src/google/protobuf/compiler/cpp/helpers.cc
  2. 36
      src/google/protobuf/compiler/cpp/helpers.h

@ -1190,7 +1190,8 @@ bool IsWellKnownMessage(const FileDescriptor* file) {
return well_known_files->find(file->name()) != well_known_files->end(); return well_known_files->find(file->name()) != well_known_files->end();
} }
void NamespaceOpener::ChangeTo(absl::string_view name) { void NamespaceOpener::ChangeTo(absl::string_view name,
io::Printer::SourceLocation loc) {
std::vector<std::string> new_stack = std::vector<std::string> new_stack =
absl::StrSplit(name, "::", absl::SkipEmpty()); absl::StrSplit(name, "::", absl::SkipEmpty());
size_t len = std::min(name_stack_.size(), new_stack.size()); size_t len = std::min(name_stack_.size(), new_stack.size());
@ -1205,12 +1206,14 @@ void NamespaceOpener::ChangeTo(absl::string_view name) {
for (size_t i = name_stack_.size(); i > common_idx; i--) { for (size_t i = name_stack_.size(); i > common_idx; i--) {
p_->Emit({{"ns", name_stack_[i - 1]}}, R"( p_->Emit({{"ns", name_stack_[i - 1]}}, R"(
} // namespace $ns$ } // namespace $ns$
)"); )",
loc);
} }
for (size_t i = common_idx; i < new_stack.size(); ++i) { for (size_t i = common_idx; i < new_stack.size(); ++i) {
p_->Emit({{"ns", new_stack[i]}}, R"( p_->Emit({{"ns", new_stack[i]}}, R"(
namespace $ns$ { namespace $ns$ {
)"); )",
loc);
} }
name_stack_ = std::move(new_stack); name_stack_ = std::move(new_stack);

@ -979,21 +979,37 @@ void PrintFieldComment(const Formatter& format, const T* field,
class PROTOC_EXPORT NamespaceOpener { class PROTOC_EXPORT NamespaceOpener {
public: public:
explicit NamespaceOpener(io::Printer* p) : p_(p) {} explicit NamespaceOpener(
explicit NamespaceOpener(const Formatter& format) : p_(format.printer()) {} io::Printer* p,
NamespaceOpener(absl::string_view name, const Formatter& format) io::Printer::SourceLocation loc = io::Printer::SourceLocation::current())
: NamespaceOpener(format) { : p_(p), loc_(loc) {}
ChangeTo(name);
explicit NamespaceOpener(
const Formatter& format,
io::Printer::SourceLocation loc = io::Printer::SourceLocation::current())
: NamespaceOpener(format.printer(), loc) {}
NamespaceOpener(
absl::string_view name, const Formatter& format,
io::Printer::SourceLocation loc = io::Printer::SourceLocation::current())
: NamespaceOpener(name, format.printer(), loc) {}
NamespaceOpener(
absl::string_view name, io::Printer* p,
io::Printer::SourceLocation loc = io::Printer::SourceLocation::current())
: NamespaceOpener(p, loc) {
ChangeTo(name, loc);
} }
NamespaceOpener(absl::string_view name, io::Printer* p) : NamespaceOpener(p) {
ChangeTo(name);
}
~NamespaceOpener() { ChangeTo(""); }
void ChangeTo(absl::string_view name); ~NamespaceOpener() { ChangeTo("", loc_); }
void ChangeTo(
absl::string_view name,
io::Printer::SourceLocation loc = io::Printer::SourceLocation::current());
private: private:
io::Printer* p_; io::Printer* p_;
io::Printer::SourceLocation loc_;
std::vector<std::string> name_stack_; std::vector<std::string> name_stack_;
}; };

Loading…
Cancel
Save