2008-08-22 20:38:05 +00:00
|
|
|
// Protocol Buffers - Google's data interchange format
|
2013-11-18 20:25:34 -05:00
|
|
|
// Copyright 2008 Google Inc. All rights reserved.
|
2008-08-22 20:38:05 +00:00
|
|
|
// http://code.google.com/p/protobuf/
|
|
|
|
//
|
2013-11-18 20:25:34 -05:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
2008-08-22 20:38:05 +00:00
|
|
|
//
|
2013-11-18 20:25:34 -05:00
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
2008-08-22 20:38:05 +00:00
|
|
|
//
|
2013-11-18 20:25:34 -05:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// Author: kenton@google.com (Kenton Varda)
|
|
|
|
// Based on original Protocol Buffers design by
|
|
|
|
// Sanjay Ghemawat, Jeff Dean, and others.
|
|
|
|
|
2013-11-18 20:25:34 -05:00
|
|
|
// Copyright (c) 2008-2013, Dave Benson. All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// Modified to implement C code by Dave Benson.
|
2013-11-18 20:25:34 -05:00
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
#ifndef GOOGLE_PROTOBUF_COMPILER_C_HELPERS_H__
|
|
|
|
#define GOOGLE_PROTOBUF_COMPILER_C_HELPERS_H__
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2011-03-10 19:12:35 +00:00
|
|
|
#include <sstream>
|
2008-08-22 20:38:05 +00:00
|
|
|
#include <google/protobuf/descriptor.h>
|
2021-03-19 18:54:16 -07:00
|
|
|
#include <protobuf-c/protobuf-c.pb.h>
|
2008-08-22 20:38:05 +00:00
|
|
|
#include <google/protobuf/io/printer.h>
|
|
|
|
|
2023-07-02 22:57:57 -04:00
|
|
|
#if GOOGLE_PROTOBUF_VERSION >= 4023000
|
|
|
|
# include <google/protobuf/descriptor_legacy.h>
|
|
|
|
#endif
|
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
namespace google {
|
|
|
|
namespace protobuf {
|
|
|
|
namespace compiler {
|
|
|
|
namespace c {
|
|
|
|
|
|
|
|
// --- Borrowed from stubs. ---
|
2020-10-16 16:35:43 -07:00
|
|
|
template <typename T> std::string SimpleItoa(T n) {
|
2011-03-10 19:12:35 +00:00
|
|
|
std::stringstream stream;
|
|
|
|
stream << n;
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string SimpleFtoa(float f);
|
|
|
|
std::string SimpleDtoa(double f);
|
|
|
|
void SplitStringUsing(const std::string &str, const char *delim, std::vector<std::string> *out);
|
|
|
|
std::string CEscape(const std::string& src);
|
|
|
|
std::string StringReplace(const std::string& s, const std::string& oldsub, const std::string& newsub, bool replace_all);
|
|
|
|
inline bool HasSuffixString(const std::string& str, const std::string& suffix) { return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; }
|
|
|
|
inline std::string StripSuffixString(const std::string& str, const std::string& suffix) { if (HasSuffixString(str, suffix)) { return str.substr(0, str.size() - suffix.size()); } else { return str; } }
|
2008-08-22 20:38:05 +00:00
|
|
|
char* FastHexToBuffer(int i, char* buffer);
|
|
|
|
|
|
|
|
|
|
|
|
// Get the (unqualified) name that should be used for this field in C code.
|
|
|
|
// The name is coerced to lower-case to emulate proto1 behavior. People
|
|
|
|
// should be using lowercase-with-underscores style for proto field names
|
|
|
|
// anyway, so normally this just returns field->name().
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string FieldName(const FieldDescriptor* field);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
2010-08-12 19:38:03 +00:00
|
|
|
// Get macro string for deprecated field
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string FieldDeprecated(const FieldDescriptor* field);
|
2010-08-12 19:38:03 +00:00
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
// Returns the scope where the field was defined (for extensions, this is
|
|
|
|
// different from the message type to which the field applies).
|
|
|
|
inline const Descriptor* FieldScope(const FieldDescriptor* field) {
|
|
|
|
return field->is_extension() ?
|
|
|
|
field->extension_scope() : field->containing_type();
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert a CamelCase class name into an all uppercase affair
|
|
|
|
// with underscores separating words, e.g. MyClass becomes MY_CLASS.
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string CamelToUpper(const std::string &class_name);
|
|
|
|
std::string CamelToLower(const std::string &class_name);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// lowercased, underscored name to camel case
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string ToCamel(const std::string &name);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// lowercase the string
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string ToLower(const std::string &class_name);
|
|
|
|
std::string ToUpper(const std::string &class_name);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// full_name() to lowercase with underscores
|
2021-03-21 19:46:21 -07:00
|
|
|
std::string FullNameToLower(const std::string &full_name, const FileDescriptor *file);
|
|
|
|
std::string FullNameToUpper(const std::string &full_name, const FileDescriptor *file);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// full_name() to c-typename (with underscores for packages, otherwise camel case)
|
2021-03-21 19:46:21 -07:00
|
|
|
std::string FullNameToC(const std::string &class_name, const FileDescriptor *file);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
2014-12-16 18:12:54 -08:00
|
|
|
// Splits, indents, formats, and prints comment lines
|
2020-10-16 16:35:43 -07:00
|
|
|
void PrintComment (io::Printer* printer, std::string comment);
|
2014-12-16 18:12:54 -08:00
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
// make a string of spaces as long as input
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string ConvertToSpaces(const std::string &input);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// Strips ".proto" or ".protodevel" from the end of a filename.
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string StripProto(const std::string& filename);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// Get the C++ type name for a primitive type (e.g. "double", "::google::protobuf::int32", etc.).
|
|
|
|
// Note: non-built-in type names will be qualified, meaning they will start
|
|
|
|
// with a ::. If you are using the type as a template parameter, you will
|
|
|
|
// need to insure there is a space between the < and the ::, because the
|
|
|
|
// ridiculous C++ standard defines "<:" to be a synonym for "[".
|
|
|
|
const char* PrimitiveTypeName(FieldDescriptor::CppType type);
|
|
|
|
|
|
|
|
// Get the declared type name in CamelCase format, as is used e.g. for the
|
|
|
|
// methods of WireFormat. For example, TYPE_INT32 becomes "Int32".
|
|
|
|
const char* DeclaredTypeMethodName(FieldDescriptor::Type type);
|
|
|
|
|
|
|
|
// Convert a file name into a valid identifier.
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string FilenameIdentifier(const std::string& filename);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// Return the name of the BuildDescriptors() function for a given file.
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string GlobalBuildDescriptorsName(const std::string& filename);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
// return 'required', 'optional', or 'repeated'
|
2020-10-16 16:35:43 -07:00
|
|
|
std::string GetLabelName(FieldDescriptor::Label label);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
// write IntRanges entries for a bunch of sorted values.
|
|
|
|
// returns the number of ranges there are to bsearch.
|
2020-10-16 16:35:43 -07:00
|
|
|
unsigned WriteIntRanges(io::Printer* printer, int n_values, const int *values, const std::string &name);
|
2008-08-22 20:38:05 +00:00
|
|
|
|
2008-08-27 02:14:42 +00:00
|
|
|
struct NameIndex
|
|
|
|
{
|
|
|
|
unsigned index;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
int compare_name_indices_by_name(const void*, const void*);
|
|
|
|
|
2016-07-24 17:21:20 +02:00
|
|
|
// Return the syntax version of the file containing the field.
|
|
|
|
// This wrapper is needed to be able to compile against protobuf2.
|
|
|
|
inline int FieldSyntax(const FieldDescriptor* field) {
|
2023-07-08 23:14:23 -04:00
|
|
|
#if GOOGLE_PROTOBUF_VERSION >= 4023000
|
2023-07-02 22:57:57 -04:00
|
|
|
return FileDescriptorLegacy(field->file()).syntax() == FileDescriptorLegacy::SYNTAX_PROTO3 ? 3 : 2;
|
2016-07-24 17:21:20 +02:00
|
|
|
#else
|
2023-07-08 23:14:23 -04:00
|
|
|
return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 ? 3 : 2;
|
2016-07-24 17:21:20 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-07-02 19:42:19 -04:00
|
|
|
// Work around changes in protobuf >= 22.x without breaking compilation against
|
|
|
|
// older protobuf versions.
|
|
|
|
#if GOOGLE_PROTOBUF_VERSION >= 4022000
|
|
|
|
# define GOOGLE_ARRAYSIZE ABSL_ARRAYSIZE
|
|
|
|
# define GOOGLE_CHECK_EQ ABSL_CHECK_EQ
|
|
|
|
# define GOOGLE_CHECK_EQ ABSL_CHECK_EQ
|
|
|
|
# define GOOGLE_DCHECK_GE ABSL_DCHECK_GE
|
|
|
|
# define GOOGLE_LOG ABSL_LOG
|
|
|
|
#endif
|
|
|
|
|
2008-08-22 20:38:05 +00:00
|
|
|
} // namespace c
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace protobuf
|
|
|
|
|
|
|
|
} // namespace google
|
|
|
|
#endif // GOOGLE_PROTOBUF_COMPILER_C_HELPERS_H__
|