| 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: |
| 42 |
Output(google::protobuf::io::ZeroCopyOutputStream* stream) |
| 43 |
: stream_(stream) {} |
| 44 |
~Output() { stream_->BackUp((int)size_); } |
| 45 |
|
| 46 |
template <class... Arg> |
| 47 |
void operator()(absl::string_view format, const Arg&... arg) { |
| 48 |
Write(absl::Substitute(format, arg...)); |
| 49 |
} |
| 50 |
|
| 51 |
private: |
| 52 |
void Write(absl::string_view data) { |
| 53 |
std::string stripped; |
| 54 |
if (absl::StartsWith(data, "\n ")) { |
| 55 |
size_t indent = data.substr(1).find_first_not_of(' '); |
| 56 |
if (indent != absl::string_view::npos) { |
| 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 |
|
| 96 |
google::protobuf::io::ZeroCopyOutputStream* stream_; |
| 97 |
char* ptr_ = nullptr; |
| 98 |
size_t size_ = 0; |
| 99 |
}; |
| 100 |
|
| 101 |
std::string StripExtension(absl::string_view fname); |
| 102 |
std::string ToCIdent(absl::string_view str); |
| 103 |
std::string ToPreproc(absl::string_view str); |
| 104 |
void EmitFileWarning(const google::protobuf::FileDescriptor* file, |
| 105 |
Output& output); |
| 106 |
std::string MessageName(const google::protobuf::Descriptor* descriptor); |
| 107 |
std::string FileLayoutName(const google::protobuf::FileDescriptor* file); |
| 108 |
std::string HeaderFilename(const google::protobuf::FileDescriptor* file); |
| 109 |
|
| 110 |
} // namespace upbc |
| 111 |
|
| 112 |
#endif // UPBC_COMMON_H |