Joshua Haberman | e59d2c8 | 2021-04-05 10:47:53 -0700 | [diff] [blame] | 1 | /* |
Joshua Haberman | 823eb09 | 2021-04-05 12:26:41 -0700 | [diff] [blame] | 2 | * Copyright (c) 2009-2021, Google LLC |
Joshua Haberman | e59d2c8 | 2021-04-05 10:47:53 -0700 | [diff] [blame] | 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 | * |
Joshua Haberman | 1c955f3 | 2022-01-12 07:19:28 -0800 | [diff] [blame] | 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 |
Joshua Haberman | e59d2c8 | 2021-04-05 10:47:53 -0700 | [diff] [blame] | 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 | */ |
Joshua Haberman | 7a54a5f | 2020-12-18 19:39:18 -0800 | [diff] [blame] | 27 | |
| 28 | #ifndef UPBC_COMMON_H |
| 29 | #define UPBC_COMMON_H |
| 30 | |
| 31 | #include <vector> |
| 32 | |
Joshua Haberman | 58c1dbc | 2021-10-13 23:03:47 -0700 | [diff] [blame] | 33 | #include "absl/strings/str_replace.h" |
Joshua Haberman | 7a54a5f | 2020-12-18 19:39:18 -0800 | [diff] [blame] | 34 | #include "absl/strings/substitute.h" |
Joshua Haberman | e41a2d7 | 2023-01-11 21:25:34 -0800 | [diff] [blame] | 35 | #include "upb/reflection/def.hpp" |
Joshua Haberman | 7a54a5f | 2020-12-18 19:39:18 -0800 | [diff] [blame] | 36 | |
| 37 | namespace upbc { |
| 38 | |
| 39 | class Output { |
| 40 | public: |
Joshua Haberman | 7a54a5f | 2020-12-18 19:39:18 -0800 | [diff] [blame] | 41 | template <class... Arg> |
| 42 | void operator()(absl::string_view format, const Arg&... arg) { |
| 43 | Write(absl::Substitute(format, arg...)); |
| 44 | } |
| 45 | |
Joshua Haberman | e41a2d7 | 2023-01-11 21:25:34 -0800 | [diff] [blame] | 46 | absl::string_view output() const { return output_; } |
| 47 | |
Joshua Haberman | 7a54a5f | 2020-12-18 19:39:18 -0800 | [diff] [blame] | 48 | private: |
| 49 | void Write(absl::string_view data) { |
Joshua Haberman | 58c1dbc | 2021-10-13 23:03:47 -0700 | [diff] [blame] | 50 | std::string stripped; |
| 51 | if (absl::StartsWith(data, "\n ")) { |
| 52 | size_t indent = data.substr(1).find_first_not_of(' '); |
| 53 | if (indent != absl::string_view::npos) { |
| 54 | // Remove indentation from all lines. |
| 55 | auto line_prefix = data.substr(0, indent + 1); |
| 56 | // The final line has an extra newline and is indented two less, eg. |
| 57 | // R"cc( |
| 58 | // UPB_INLINE $0 $1_$2(const $1 *msg) { |
| 59 | // return $1_has_$2(msg) ? *UPB_PTR_AT(msg, $3, $0) : $4; |
| 60 | // } |
| 61 | // )cc", |
| 62 | std::string last_line_prefix = std::string(line_prefix); |
| 63 | last_line_prefix.resize(last_line_prefix.size() - 2); |
| 64 | data.remove_prefix(line_prefix.size()); |
| 65 | stripped = absl::StrReplaceAll( |
| 66 | data, {{line_prefix, "\n"}, {last_line_prefix, "\n"}}); |
| 67 | data = stripped; |
| 68 | } |
| 69 | } |
Joshua Haberman | e41a2d7 | 2023-01-11 21:25:34 -0800 | [diff] [blame] | 70 | absl::StrAppend(&output_, data); |
Joshua Haberman | 7a54a5f | 2020-12-18 19:39:18 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Joshua Haberman | e41a2d7 | 2023-01-11 21:25:34 -0800 | [diff] [blame] | 73 | std::string output_; |
Joshua Haberman | 7a54a5f | 2020-12-18 19:39:18 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | std::string StripExtension(absl::string_view fname); |
| 77 | std::string ToCIdent(absl::string_view str); |
| 78 | std::string ToPreproc(absl::string_view str); |
Joshua Haberman | e41a2d7 | 2023-01-11 21:25:34 -0800 | [diff] [blame] | 79 | void EmitFileWarning(absl::string_view name, Output& output); |
| 80 | std::string MessageName(upb::MessageDefPtr descriptor); |
| 81 | std::string FileLayoutName(upb::FileDefPtr file); |
| 82 | std::string HeaderFilename(upb::FileDefPtr file); |
Joshua Haberman | 7a54a5f | 2020-12-18 19:39:18 -0800 | [diff] [blame] | 83 | |
Joshua Haberman | e41a2d7 | 2023-01-11 21:25:34 -0800 | [diff] [blame] | 84 | std::string MessageInit(absl::string_view full_name); |
| 85 | std::string EnumInit(upb::EnumDefPtr descriptor); |
Protobuf Team Bot | 46e306b | 2022-06-30 10:23:47 -0700 | [diff] [blame] | 86 | |
Joshua Haberman | 7a54a5f | 2020-12-18 19:39:18 -0800 | [diff] [blame] | 87 | } // namespace upbc |
| 88 | |
Joshua Haberman | 1c955f3 | 2022-01-12 07:19:28 -0800 | [diff] [blame] | 89 | #endif // UPBC_COMMON_H |