73 Commits

Author SHA1 Message Date
Robert Edmonds
8d334a7204 Work around GOOGLE_* changes in protobuf >= 22.0
According to the protobuf migration guide as of June 2023 [0], protobuf
22.0 (aka 4.22.0) took a dependency on something called "abseil" and as
a result the "stubs" have been removed. This apparently caused all the
uses of GOOGLE_* identifiers in protoc-c to fail when building against
newer versions of protobuf.

This commit introduces compatibility definitions when building against
protobuf >= 4.22.0 so that protobuf-c can build against older and newer
versions of protobuf.

[0] https://web.archive.org/web/20230611151200/https://protobuf.dev/support/migration/#abseil
2023-07-02 19:42:19 -04:00
Robert Edmonds
7b90330bff Use GOOGLE_LOG(FATAL) instead of GOOGLE_LOG(DFATAL)
Looking at where these identifiers are ultimately defined [0], it looks
like "DFATAL" means either "ERROR" or "FATAL" depending on whether
NDEBUG is defined. However, looking at the actual code sites in protoc-c
where DFATAL is used, it's not clear why we couldn't just use FATAL
unconditionally.

This is aimed at supporting newer versions of protobuf where the DFATAL
identifier apparently no longer exists.

[0] https://github.com/protocolbuffers/protobuf/blob/v21.12/src/google/protobuf/stubs/logging.h#L61-L65
2023-07-02 19:35:43 -04:00
Robert Edmonds
ad48868dfe protoc-c: Remove GOOGLE_DISALLOW_EVIL_CONSTRUCTORS macro invocations
protobuf has removed the definition of this macro as of commit
1595417dd3859bbff7d3d61ad0b6e39044d47489, so the invocation of this
macro in protobuf-c breaks the build when building agaist the protobuf
22.x or 23.x series.

Simply removing the macro invocations seems to be safe and doesn't break
the build on Debian's protobuf 3.21.12 nor Debian's protobuf 3.6.1.3.
2023-07-02 17:48:18 -04:00
Robert Edmonds
5ae0762b85
Merge branch 'next' into options 2021-04-06 19:57:36 -04:00
Ilya Lipnitskiy
7df109917a
protoc-c: add c_package option
This option allows to override the top-level 'package' setting, or even
remove the package prefix altogether (if set to "").

Fixes #322
2021-03-21 21:11:51 -07:00
Ilya Lipnitskiy
4f39ca6e0c
protoc-c: don't use FullName helpers for non-full names
FullName* methods are designed to split a full name based on '.' in the
name. name() does not have '.' separators, so call CamelToLower and
CamelToUpper in place of FullNameToLower and FullNameToUpper.
2021-03-21 21:11:50 -07:00
Ilya Lipnitskiy
1b4e45e445
protoc-c: remove dead code 2021-03-21 21:11:50 -07:00
Ilya Lipnitskiy
f486c8ed66
protoc-c: add use_oneof_field_name option
Allows for generic oneof offset lookups based on the name of the oneof,
instead of having to know the field name. All oneof fields should have
the same offset, since they are members of a union.

Fixes #204
2021-03-21 21:11:50 -07:00
Ilya Lipnitskiy
dfb6e300fa
protoc-c: add const_strings option
Keep default as false to preserve backwards compatibility.

Fixes #239
2021-03-21 21:11:50 -07:00
Ilya Lipnitskiy
cebe3d03f9
protoc-c: add string as bytes option
Allows treating proto strings as ProtobufCBinaryData to work around
limitations such as NULL characters in strings, which are allowed in
protobuf, but not allowed in 'char *' types.

Fixes #203
2021-03-21 21:11:50 -07:00
Ilya Lipnitskiy
6962695641
protoc-c: add option to customize base field name
Fixes #287
2021-03-21 21:11:50 -07:00
Ilya Lipnitskiy
baa860ff97
protoc-c: add options for function generation
Add options controller helper function generation. Preserve existing
behavior of not generating pack/unpack functions for sub-messages if
option is not explicitly set.

Fixes #240
Fixes #442
2021-03-21 21:11:49 -07:00
Ilya Lipnitskiy
883b1acc1b
protoc-c: add no_generate option
Allows suppression of .pb-c.{c,h} file generation on a .proto file
basis.

protobuf-c.proto: Use the new option in itself, because the options are
never used by protobuf-c runtime, only by the protoc-gen-c compiler.

t/test-full.proto: import protobuf-c.proto to test that
protobuf-c.pb-c.h dependency does not get included in test-full.pb-c.h.
2021-03-21 21:11:49 -07:00
Ilya Lipnitskiy
0e060260f0
protoc-c: add custom options support 2021-03-21 20:28:33 -07:00
Ilya Lipnitskiy
2a278e0756
protoc-c: fix shared lib build on windows
Fixes #458

Signed-off-by: Ilya Lipnitskiy <ilya.lipnitskiy@gmail.com>
2021-03-15 18:01:47 -07:00
Robert Edmonds
94532c7a63
Merge pull request #443 from acozzette/std-string
Updated the generator to fully qualify std::string
2020-10-16 20:03:05 -04:00
Adam Cozzette
3d2ed0d410 Updated the generator to fully qualify std::string
The protobuf common.h header currently has a "using std::string;"
statement that pulls std::string into the google::protobuf namespace:
ce66f6047d/src/google/protobuf/stubs/common.h (L195)

I plan to delete that line soon. To keep protobuf-c working, this commit
updates the generator to fully qualify std::string.
2020-10-16 16:45:21 -07:00
Robert Edmonds
d18a21d5ba
Merge pull request #430 from protobuf-c/ilya-underscore
protoc-c: Remove leading underscores from structs
2020-06-20 15:56:46 -04:00
Ilya Lipnitskiy
cd671dbc3d protobuf-c.c, protoc-c: Remove leading underscores
Fixes #188.

Implements suggested fix from the issue:
The struct tag namespace is different from the type namespace. Use the
same symbol names (i.e. without leading underscores) in every namespace.
2020-05-13 15:30:20 -07:00
Ilya Lipnitskiy
93afe2d52d c_message.cc: Resolve name conflict (Fixes #389) 2020-05-12 12:21:48 -07:00
Ilya Lipnitskiy
69c39824cf protoc-c/c_field.cc: Fix proto3 field packing #330
Fixes #330 and makes scalar repeated fields packed by default in proto3.

https://developers.google.com/protocol-buffers/docs/encoding#packed:
In proto3, repeated fields of scalar numeric types are packed by
default.
2019-05-17 13:29:06 -07:00
Robert Edmonds
83c59e705f Convert uses of protobuf's scoped_ptr.h to C++11 std::unique_ptr
Upstream protobuf removed scoped_ptr.h, which provided the scoped_ptr
that we depended on, in commit 67952fab2c766ac5eacc15bb78e5af4039a3d398
(67952fab2c).

This commit converts uses of scoped_ptr type to idiomatic C++11
std::unique_ptr, similar to the changes in the protobuf commit.

This has been tested on protobuf 3.0.0 (the version currently in Debian)
as well as the latest protobuf 3.6.1 release.
2018-08-13 21:26:00 -04:00
Robert Edmonds
26a836423c
Merge pull request #309 from iveqy/add_std
Add std:: to some types
2018-08-13 20:57:46 -04:00
Fredrik Gustafsson
034e603d2a Add std:: to some types
This is required for compilation to succeed on debian jessie with g++
6.3.0.
2018-03-11 08:57:46 +01:00
Robert Edmonds
4302266b45 Restore protobuf-2.x compatibility
The change in commit 712154b912de824741381c0bb26c2fbed54515a3 ("Bump
minimum required header version for proto3 syntax") uses functionality
only exposed by protobuf-3.x, breaking the build when compiling against
protobuf-2.x.

Since we still want to support building against protobuf-2.x, this
commit makes the proto3 syntax check in the file generator dependent on
building against protobuf-3.x.
2017-08-07 12:31:55 -04:00
Robert Edmonds
712154b912 Bump minimum required header version for proto3 syntax
The changes in #274 (proto3: make strings default to "" instead of NULL)
add a new symbol to the library and add an 'extern' declaration to the
protobuf-c header file.

Since the compiler may generate files that depend on that new
'protobuf_c_empty_string' declaration in protobuf-c.h, we need to bump
the min_header_version value that is written into generated header
files. But since the 'protobuf_c_empty_string' symbol can only be
referenced when generating proto3 syntax files, we only need to use the
stricter min_header_version value when processing a proto3 file.
2017-07-29 20:16:22 -04:00
Robert Edmonds
582cad8bff Fix namespace errors when compiled with latest protobuf
The protobuf project removed "using namespace std" namespace pollution
from their stubs/common.h header file. This caused build failures for us
since we relied on their namespace pollution.

This commit updates protobuf-c to convert:

 'map' → 'std::map'
 'set' → 'std::set'
 'back_insert_iterator' → 'std::back_insert_iterator'
2017-07-29 18:52:28 -04:00
Paolo Borelli
2a46af4278 proto3: make strings default to "" instead of NULL
The spec talks about "empty string" and other languages like C#
return a zero length string and not null.
This is useful because when moving from proto2's "required string"
to a proto3's plain string we will be guaranteed that we
never get a null pointer.
The tradeoff is adding a special case to the library but avoiding
a lot of null checks in the calling code.
The current code is already special casing "" in pack_string.
2017-06-12 09:32:39 +02:00
Robert Edmonds
c48d932003 Merge pull request #228 from pbor/wip/proto3
proto3 support
2017-02-25 14:20:04 -08:00
Robert Edmonds
638c54d4db GenerateStructDefinition(): Reset vars["opt_comma"] when processing multiple oneofs
This commit fixes #251, which causes incorrect code to be generated when
there are multiple oneofs in the same message.

In #221, we added code to force int-sizing for oneof enums, but we only
set vars["opt_comma"] initially, before entering the loop over the
message's oneofs. This caused commas to be omitted when generating the
enums for subsequent oneofs after the first oneof.

This commit resets vars["opt_comma"] every time through the loop that
generates the enum declarations for the message's oneofs.
2017-02-25 15:52:39 -05:00
Robert Edmonds
956ea86712 GenerateStructDefinition(): Append oneof name when forcing enums to be int size
This commit fixes a problem identified in #251. When generating the
PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE directive at the end of the enum
values generated for a oneof, we inadvertently only included the
protobuf class name in the call to the directive. This breaks if there
is more than one oneof per protobuf class, which causes duplicate enum
names to be generated.

This issue is fixed by also appending the oneof's name to the protobuf
class name when generating the PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE
directive, which should cause unique enum names to be generated.

This changes the enum name that is generated, but these names are
implementation details since they are sentinel values that only exist to
force the compiler to size the enum type correctly.

(The test case for #220 is updated since that test relies on the
oneof implementation details in the code generator.)
2017-02-25 15:52:39 -05:00
Richard Kettlewell
c23065e4b1 Allowing <type>__free_unpacked(NULL, ...)
This makes __free_unpacked() consistent with free(), and simplifies
callers by allowing them to indiscriminately free message objects
without regard to whether they have been allocated or not.
2017-02-25 19:59:17 +00:00
Paolo Borelli
8194f4d91a proto3 support
This is a first cut at adding proto3 support.

As far as I understand protobuf-c already has pretty much everything
needed once it is built using a new version of protobuf itself.
The only missing thing is that in proto3 all fields are optional and
having to manually set has_foo is inconvenient.

This patch special cases the proto3 syntax files so that structs for the
bytes, enum and primitive fields do not emit the has_ field.

It also adds PROTOBUF_C_LABEL_NONE to the label enum that is used for
proto3 fields. When a fields has this label, the quantifier is not
consulted and instead the field is packed/unpacked depending on
whether it has a value different from NULL/0.
2016-10-30 10:06:52 +01:00
Thomas Köckerbauer
f73cc06d1e make init_value const 2016-09-07 15:18:41 +02:00
Ilya Lipnitskiy
bb6553c397 protoc-c/c_message.cc: Force int size on oneof enums
Fixes #220. Patch by Dave Benson from the mailing list thread.
2016-06-17 00:27:34 -07:00
Robert Edmonds
171ccd222b Merge remote-tracking branch 'pbor/wip/plugin' into next 2016-04-25 21:40:37 -04:00
Ilya Lipnitskiy
258eb7d4dc protoc-c/c_enum_field.cc: Fix default enum values
From https://developers.google.com/protocol-buffers/docs/proto#optional:
If the default value is not specified for an optional element, a
type-specific default value is used instead: for strings, the default
value is the empty string. For bools, the default value is false. For
numeric types, the default value is zero. For enums, the default value
is the first value listed in the enum's type definition. This means care
must be taken when adding a value to the beginning of an enum value
list.

Prior to this change, protoc-c set the default enum value to 0, whether
or not 0 was the first value listed in the enum's type definition (or if
it even was listed at all).
2016-04-22 11:12:15 -07:00
Paolo Borelli
af2120aa55 Turn the compiler into a protoc plugin
Using the old "protoc-c" command directly is still supported
through a symlink.
2016-02-17 10:58:43 +01:00
Paolo Borelli
af613f932a Fix union initialization
Using {} as an initializer fails on MSVC

Signed-off-by: Paolo Borelli <pborelli@gnome.org>
2016-02-02 14:45:29 -05:00
Ilya Lipnitskiy
0e2d3c09e2 Rename LITE_RUNTIME to CODE_SIZE 2015-10-30 15:54:00 -07:00
Ilya Lipnitskiy
603e431864 protoc-c: Support the optimize_for = LITE_RUNTIME file option
Adds support for the LITE_RUNTIME optimization option to the protobuf-c
compiler. Enabling this option would generate lighter weight message,
enum, and service descriptors that contain NO strings. As a result,
calls to lookup descriptors via the *_get_{field,value,method}_by_name
API will return NULL.

Default compiler behavior (when optimize_for is not specified or is not
set to LITE_RUNTIME) is unchanged.
2015-03-15 20:01:03 -07:00
Andrey Myznikov
812ecf9326 Fix incorrect 'Short version of service name' generation for ProtobufCServiceDescriptor 2015-03-14 16:00:28 -04:00
Robert Edmonds
cbe7b84a14 Merge remote-tracking branch 'github/protoc_c_comments' into next 2015-03-13 12:42:51 -04:00
Ilya Lipnitskiy
b64f6c1f68 protoc-c/c_enum.cc: Make enum_values_{by_name,by_number} structs static 2015-02-14 19:21:29 -08:00
Ilya Lipnitskiy
8f42a69f86 protoc-c/c_helpers.cc: Enhance comment string parsing
Certain protobuf comments could generate invalid C comments
and inadvertently close the comment block. This commit removes '/'
signs in such comments.

One example of a .proto file containing such comments is a commonly
included descriptor.proto from the protobuf library.
2015-01-30 20:11:34 -08:00
Ilya Lipnitskiy
35ec2e2f9c protoc-c: Print comments for generated enum, message, and field definitions 2014-12-16 18:12:54 -08:00
Ilya Lipnitskiy
9db84a3b90 protoc-c: Implement oneofs 2014-11-19 23:07:23 -08:00
Robert Edmonds
8655ca076d PROTOBUF_C_{NO_,}DEPRECATED -> PROTOBUF_C__{NO_,}DEPRECATED
these identifiers aren't intended for use by client code. add a double
underscore to indicate this.
2014-06-06 14:55:11 -04:00
Robert Edmonds
af5120e678 PROTOBUF_C_{BEGIN,END}_DECLS -> PROTOBUF_C__{BEGIN,END}_DECLS
these identifiers aren't intended for use by client code. add a double
underscore to indicate this.
2014-06-06 14:50:14 -04:00
Andrei Nigmatulin
94e7cde263 cosmetics 2014-06-06 14:48:59 -04:00