Fix bug with .proto without messages (again), and add a test case for it.
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Thu, 5 Jul 2012 17:02:06 +0000 (20:02 +0300)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Thu, 5 Jul 2012 17:02:06 +0000 (20:02 +0300)
generator/nanopb_generator.py
tests/Makefile
tests/no_messages.proto [new file with mode: 0644]

index 1923cc1..b9018ae 100644 (file)
@@ -414,15 +414,16 @@ def generate_header(dependencies, headername, enums, messages):
     for msg in messages:
         yield msg.fields_declaration() + '\n'
     
-    count_required_fields = lambda m: len([f for f in msg.fields if f.htype == 'PB_HTYPE_REQUIRED'])
-    largest_msg = max(messages, key = count_required_fields)
-    largest_count = count_required_fields(largest_msg)
-    if largest_count > 64:
-        yield '\n/* Check that missing required fields will be properly detected */\n'
-        yield '#if PB_MAX_REQUIRED_FIELDS < %d\n' % largest_count
-        yield '#error Properly detecting missing required fields in %s requires \\\n' % largest_msg.name
-        yield '       setting PB_MAX_REQUIRED_FIELDS to %d or more.\n' % largest_count
-        yield '#endif\n'
+    if messages:
+        count_required_fields = lambda m: len([f for f in msg.fields if f.htype == 'PB_HTYPE_REQUIRED'])
+        largest_msg = max(messages, key = count_required_fields)
+        largest_count = count_required_fields(largest_msg)
+        if largest_count > 64:
+            yield '\n/* Check that missing required fields will be properly detected */\n'
+            yield '#if PB_MAX_REQUIRED_FIELDS < %d\n' % largest_count
+            yield '#error Properly detecting missing required fields in %s requires \\\n' % largest_msg.name
+            yield '       setting PB_MAX_REQUIRED_FIELDS to %d or more.\n' % largest_count
+            yield '#endif\n'
     
     worst = 0
     worst_field = ''
index 24e9ba6..8c063f3 100644 (file)
@@ -1,7 +1,7 @@
 CFLAGS=-ansi -Wall -Werror -I .. -g -O0 --coverage
 LDFLAGS=--coverage
 DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h callbacks.pb.h unittests.h unittestproto.pb.h alltypes.pb.h missing_fields.pb.h
-TESTS=test_decode1 test_encode1 decode_unittests encode_unittests
+TESTS=test_decode1 test_encode1 decode_unittests encode_unittests test_no_messages
 
 all: breakpoints $(TESTS) run_unittests
 
@@ -28,6 +28,7 @@ test_encode_callbacks: test_encode_callbacks.o pb_encode.o callbacks.pb.o
 test_missing_fields: test_missing_fields.o pb_encode.o pb_decode.o missing_fields.pb.o
 decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
 encode_unittests: encode_unittests.o pb_encode.o unittestproto.pb.o
+test_no_messages: no_messages.pb.h no_messages.pb.c no_messages.pb.o
 
 %.pb: %.proto
        protoc -I. -I../generator -I/usr/include -o$@ $<
diff --git a/tests/no_messages.proto b/tests/no_messages.proto
new file mode 100644 (file)
index 0000000..279216b
--- /dev/null
@@ -0,0 +1,7 @@
+/* Test that a file without any messages works. */
+
+enum Test {
+    First = 1;
+}
+
+