common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2009-2021, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef UPBC_COMMON_H
  28. #define UPBC_COMMON_H
  29. #include <vector>
  30. #include "absl/strings/str_replace.h"
  31. #include "absl/strings/substitute.h"
  32. #include "google/protobuf/descriptor.h"
  33. #include "google/protobuf/io/zero_copy_stream.h"
  34. namespace upbc {
  35. class Output {
  36. public:
  37. Output(google::protobuf::io::ZeroCopyOutputStream* stream)
  38. : stream_(stream) {}
  39. ~Output() { stream_->BackUp((int)size_); }
  40. template <class... Arg>
  41. void operator()(absl::string_view format, const Arg&... arg) {
  42. Write(absl::Substitute(format, arg...));
  43. }
  44. private:
  45. void Write(absl::string_view data) {
  46. std::string stripped;
  47. if (absl::StartsWith(data, "\n ")) {
  48. size_t indent = data.substr(1).find_first_not_of(' ');
  49. if (indent != absl::string_view::npos) {
  50. // Remove indentation from all lines.
  51. auto line_prefix = data.substr(0, indent + 1);
  52. // The final line has an extra newline and is indented two less, eg.
  53. // R"cc(
  54. // UPB_INLINE $0 $1_$2(const $1 *msg) {
  55. // return $1_has_$2(msg) ? *UPB_PTR_AT(msg, $3, $0) : $4;
  56. // }
  57. // )cc",
  58. std::string last_line_prefix = std::string(line_prefix);
  59. last_line_prefix.resize(last_line_prefix.size() - 2);
  60. data.remove_prefix(line_prefix.size());
  61. stripped = absl::StrReplaceAll(
  62. data, {{line_prefix, "\n"}, {last_line_prefix, "\n"}});
  63. data = stripped;
  64. }
  65. }
  66. while (!data.empty()) {
  67. RefreshOutput();
  68. size_t to_write = std::min(data.size(), size_);
  69. memcpy(ptr_, data.data(), to_write);
  70. data.remove_prefix(to_write);
  71. ptr_ += to_write;
  72. size_ -= to_write;
  73. }
  74. }
  75. void RefreshOutput() {
  76. while (size_ == 0) {
  77. void* ptr;
  78. int size;
  79. if (!stream_->Next(&ptr, &size)) {
  80. fprintf(stderr, "upbc: Failed to write to to output\n");
  81. abort();
  82. }
  83. ptr_ = static_cast<char*>(ptr);
  84. size_ = size;
  85. }
  86. }
  87. google::protobuf::io::ZeroCopyOutputStream* stream_;
  88. char* ptr_ = nullptr;
  89. size_t size_ = 0;
  90. };
  91. std::string StripExtension(absl::string_view fname);
  92. std::string ToCIdent(absl::string_view str);
  93. std::string ToPreproc(absl::string_view str);
  94. void EmitFileWarning(const google::protobuf::FileDescriptor* file,
  95. Output& output);
  96. std::string MessageName(const google::protobuf::Descriptor* descriptor);
  97. std::string FileLayoutName(const google::protobuf::FileDescriptor* file);
  98. std::string HeaderFilename(const google::protobuf::FileDescriptor* file);
  99. } // namespace upbc
  100. #endif // UPBC_COMMON_H