Fix STATIC_ASSERT macro when using multiple .proto files.
authorSteffen Siering <steffen siering gmail com>
Sun, 11 Nov 2012 22:48:21 +0000 (22:48 +0000)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Fri, 16 Nov 2012 07:24:39 +0000 (09:24 +0200)
The __COUNTER__ macro (used for generating unique names) is at least supported
by gcc, clang and Visual Studio. With this change test_compiles.c is
compilable, since no more typedefs are redefined.

Compilers/Preprocessors not supporting __COUNTER__ error's are still possible
which are hopfully handled by the usage of __LINE__ in most sittuations.

Added unit test for the problem.

generator/nanopb_generator.py
pb.h
tests/Makefile
tests/callbacks2.proto [new file with mode: 0644]
tests/test_compiles.c [new file with mode: 0644]

index 6aac1b8..2a3cab2 100644 (file)
@@ -493,7 +493,9 @@ def generate_header(dependencies, headername, enums, messages):
     worst = 0
     worst_field = ''
     checks = []
+    checks_msgnames = []
     for msg in messages:
+        checks_msgnames.append(msg.name)
         for field in msg.fields:
             status = field.largest_field_value()
             if isinstance(status, (str, unicode)):
@@ -511,7 +513,8 @@ def generate_header(dependencies, headername, enums, messages):
                 yield '#error Field descriptor for %s is too large. Define PB_FIELD_16BIT to fix this.\n' % worst_field
             else:
                 assertion = ' && '.join(str(c) + ' < 256' for c in checks)
-                yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_16BIT)\n' % assertion
+                msgs = '_'.join(str(n) for n in checks_msgnames)
+                yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_%s)\n'%(assertion,msgs)
             yield '#endif\n\n'
         
         if worst > 65535 or checks:
@@ -520,7 +523,8 @@ def generate_header(dependencies, headername, enums, messages):
                 yield '#error Field descriptor for %s is too large. Define PB_FIELD_32BIT to fix this.\n' % worst_field
             else:
                 assertion = ' && '.join(str(c) + ' < 65536' for c in checks)
-                yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_32BIT)\n' % assertion
+                msgs = '_'.join(str(n) for n in checks_msgnames)
+                yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_%s)\n'%(assertion,msgs)
             yield '#endif\n'
     
     yield '\n#ifdef __cplusplus\n'
diff --git a/pb.h b/pb.h
index dafdfd5..22b10d8 100644 (file)
--- a/pb.h
+++ b/pb.h
@@ -26,7 +26,9 @@
 
 /* Compile-time assertion, used for checking compatible compilation options. */
 #ifndef STATIC_ASSERT
-#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1];
+#define STATIC_ASSERT(COND,MSG) typedef char STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
+#define STATIC_ASSERT_MSG(MSG, LINE, COUNTER) STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
+#define STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) static_assertion_##MSG##LINE##COUNTER
 #endif
 
 /* Number of required fields to keep track of
index 1f2be71..f3a64d1 100644 (file)
@@ -1,7 +1,9 @@
 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
+DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h callbacks2.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 test_no_messages
+TESTS=test_decode1 test_encode1 decode_unittests encode_unittests test_no_messages test_compiles
+
 
 # More strict checks for the core part of nanopb
 CFLAGS_CORE=-pedantic -Wextra -Wcast-qual -Wlogical-op -Wconversion
@@ -35,6 +37,7 @@ test_decode3: test_decode3.o pb_decode.o alltypes.pb.o
 test_encode1: test_encode1.o pb_encode.o person.pb.o
 test_encode2: test_encode2.o pb_encode.o person.pb.o
 test_encode3: test_encode3.o pb_encode.o alltypes.pb.o
+test_compiles: test_compiles.o pb_encode.o callbacks2.pb.o callbacks.pb.o
 test_decode_callbacks: test_decode_callbacks.o pb_decode.o callbacks.pb.o
 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
@@ -55,7 +58,7 @@ coverage: run_unittests
        gcov pb_encode.gcda
        gcov pb_decode.gcda
 
-run_unittests: decode_unittests encode_unittests test_cxxcompile test_encode1 test_encode2 test_encode3 test_decode1 test_decode2 test_decode3 test_encode_callbacks test_decode_callbacks test_missing_fields test_options
+run_unittests: decode_unittests encode_unittests test_cxxcompile test_compiles test_encode1 test_encode2 test_encode3 test_decode1 test_decode2 test_decode3 test_encode_callbacks test_decode_callbacks test_missing_fields test_options
        rm -f *.gcda
        
        ./decode_unittests > /dev/null
diff --git a/tests/callbacks2.proto b/tests/callbacks2.proto
new file mode 100644 (file)
index 0000000..9a55e15
--- /dev/null
@@ -0,0 +1,9 @@
+// Test if including generated header file for this file + implicit include of
+// callbacks.pb.h still compiles. Used with test_compiles.c.
+import "callbacks.proto";
+
+message Callback2Message {
+    required TestMessage tstmsg = 1;
+    required SubMessage submsg = 2;
+}
+
diff --git a/tests/test_compiles.c b/tests/test_compiles.c
new file mode 100644 (file)
index 0000000..cb4e16d
--- /dev/null
@@ -0,0 +1,13 @@
+/*
+ * Tests if still compile if typedefs are redfefined in STATIC_ASSERTS when
+ * proto file includes another poto file
+ */
+
+#include <stdio.h>
+#include <pb_encode.h>
+#include "callbacks2.pb.h"
+
+int main()
+{
+       return 0;
+}