upb/upbc/common.h
Go to the documentation of this file.
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 
28 #ifndef UPBC_COMMON_H
29 #define UPBC_COMMON_H
30 
31 #include <vector>
32 
33 #include "absl/strings/str_replace.h"
34 #include "absl/strings/substitute.h"
35 #include "google/protobuf/descriptor.h"
36 #include "google/protobuf/io/zero_copy_stream.h"
37 
38 namespace upbc {
39 
40 class Output {
41  public:
43  : stream_(stream) {}
44  ~Output() { stream_->BackUp((int)size_); }
45 
46  template <class... Arg>
49  }
50 
51  private:
53  std::string stripped;
54  if (absl::StartsWith(data, "\n ")) {
55  size_t indent = data.substr(1).find_first_not_of(' ');
57  // Remove indentation from all lines.
58  auto line_prefix = data.substr(0, indent + 1);
59  // The final line has an extra newline and is indented two less, eg.
60  // R"cc(
61  // UPB_INLINE $0 $1_$2(const $1 *msg) {
62  // return $1_has_$2(msg) ? *UPB_PTR_AT(msg, $3, $0) : $4;
63  // }
64  // )cc",
65  std::string last_line_prefix = std::string(line_prefix);
66  last_line_prefix.resize(last_line_prefix.size() - 2);
67  data.remove_prefix(line_prefix.size());
68  stripped = absl::StrReplaceAll(
69  data, {{line_prefix, "\n"}, {last_line_prefix, "\n"}});
70  data = stripped;
71  }
72  }
73  while (!data.empty()) {
74  RefreshOutput();
75  size_t to_write = std::min(data.size(), size_);
76  memcpy(ptr_, data.data(), to_write);
77  data.remove_prefix(to_write);
78  ptr_ += to_write;
79  size_ -= to_write;
80  }
81  }
82 
83  void RefreshOutput() {
84  while (size_ == 0) {
85  void* ptr;
86  int size;
87  if (!stream_->Next(&ptr, &size)) {
88  fprintf(stderr, "upbc: Failed to write to to output\n");
89  abort();
90  }
91  ptr_ = static_cast<char*>(ptr);
92  size_ = size;
93  }
94  }
95 
97  char* ptr_ = nullptr;
98  size_t size_ = 0;
99 };
100 
105  Output& output);
109 
110 } // namespace upbc
111 
112 #endif // UPBC_COMMON_H
xds_interop_client.str
str
Definition: xds_interop_client.py:487
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
upbc::EmitFileWarning
void EmitFileWarning(const protobuf::FileDescriptor *file, Output &output)
Definition: upb/upbc/common.cc:53
http2_test_server.format
format
Definition: http2_test_server.py:118
upbc::Output::stream_
google::protobuf::io::ZeroCopyOutputStream * stream_
Definition: upb/upbc/common.h:96
absl::StartsWith
bool StartsWith(absl::string_view text, absl::string_view prefix) noexcept
Definition: third_party/abseil-cpp/absl/strings/match.h:58
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
upbc
Definition: upb/upbc/common.cc:30
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
python_utils.upload_rbe_results.indent
indent
Definition: upload_rbe_results.py:183
upbc::StripExtension
std::string StripExtension(absl::string_view fname)
Definition: upb/upbc/common.cc:37
Arg
Arg(64) -> Arg(128) ->Arg(256) ->Arg(512) ->Arg(1024) ->Arg(1536) ->Arg(2048) ->Arg(3072) ->Arg(4096) ->Arg(5120) ->Arg(6144) ->Arg(7168)
upbc::HeaderFilename
std::string HeaderFilename(const google::protobuf::FileDescriptor *file)
Definition: upb/upbc/common.cc:73
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
upbc::Output::~Output
~Output()
Definition: upb/upbc/common.h:44
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
arg
Definition: cmdline.cc:40
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
upbc::Output::RefreshOutput
void RefreshOutput()
Definition: upb/upbc/common.h:83
min
#define min(a, b)
Definition: qsort.h:83
upbc::Output::ptr_
char * ptr_
Definition: upb/upbc/common.h:97
upbc::ToPreproc
std::string ToPreproc(absl::string_view str)
Definition: upb/upbc/common.cc:49
upbc::FileLayoutName
std::string FileLayoutName(const google::protobuf::FileDescriptor *file)
Definition: upb/upbc/common.cc:69
upbc::Output::Output
Output(google::protobuf::io::ZeroCopyOutputStream *stream)
Definition: upb/upbc/common.h:42
google::protobuf::io::ZeroCopyOutputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h:183
upbc::Output::operator()
void operator()(absl::string_view format, const Arg &... arg)
Definition: upb/upbc/common.h:47
absl::StrReplaceAll
std::string StrReplaceAll(absl::string_view s, strings_internal::FixedMapping replacements)
Definition: abseil-cpp/absl/strings/str_replace.cc:71
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
upbc::MessageName
std::string MessageName(const protobuf::Descriptor *descriptor)
Definition: upb/upbc/common.cc:65
absl::Substitute
ABSL_MUST_USE_RESULT std::string Substitute(absl::string_view format)
Definition: abseil-cpp/absl/strings/substitute.h:506
upbc::Output::size_
size_t size_
Definition: upb/upbc/common.h:98
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
google::protobuf::io::ZeroCopyOutputStream::Next
virtual bool Next(void **data, int *size)=0
absl::string_view::npos
static constexpr size_type npos
Definition: abseil-cpp/absl/strings/string_view.h:182
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
upbc::Output::Write
void Write(absl::string_view data)
Definition: upb/upbc/common.h:52
upbc::Output
Definition: upb/upbc/common.h:40
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
upbc::ToCIdent
std::string ToCIdent(absl::string_view str)
Definition: upb/upbc/common.cc:45
google::protobuf::io::ZeroCopyOutputStream::BackUp
virtual void BackUp(int count)=0
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:59