Sanitize filenames before putting them in #ifndef.
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Thu, 7 Feb 2013 15:40:17 +0000 (17:40 +0200)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Thu, 7 Feb 2013 15:40:17 +0000 (17:40 +0200)
Update issue 50
Status: FixedInGit

generator/nanopb_generator.py
tests/Makefile
tests/funny-proto+name.proto [new file with mode: 0644]

index 9e85a11..d35a425 100644 (file)
@@ -452,6 +452,16 @@ def sort_dependencies(messages):
         if msgname in message_by_name:
             yield message_by_name[msgname]
 
+def make_identifier(headername):
+    '''Make #ifndef identifier that contains uppercase A-Z and digits 0-9'''
+    result = ""
+    for c in headername.upper():
+        if c.isalnum():
+            result += c
+        else:
+            result += '_'
+    return result
+
 def generate_header(dependencies, headername, enums, messages):
     '''Generate content for a header file.
     Generates strings, which should be concatenated and stored to file.
@@ -460,7 +470,7 @@ def generate_header(dependencies, headername, enums, messages):
     yield '/* Automatically generated nanopb header */\n'
     yield '/* Generated by %s at %s. */\n\n' % (nanopb_version, time.asctime())
     
-    symbol = headername.replace('.', '_').upper()
+    symbol = make_identifier(headername)
     yield '#ifndef _PB_%s_\n' % symbol
     yield '#define _PB_%s_\n' % symbol
     yield '#include <pb.h>\n\n'
index 38f10ea..7385fe3 100644 (file)
@@ -6,7 +6,7 @@ TESTS=  decode_unittests encode_unittests \
        test_decode1 test_decode2 test_decode3 test_decode3_buf \
        test_encode1 test_encode2 test_encode3 test_encode3_buf \
        test_decode_callbacks test_encode_callbacks \
-       test_missing_fields test_no_messages \
+       test_missing_fields test_no_messages test_funny_name \
        test_multiple_files test_cxxcompile test_options \
        bc_encode bc_decode
 
@@ -73,6 +73,7 @@ test_missing_fields: test_missing_fields.o pb_encode.o pb_decode.o missing_field
 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
+test_funny_name: funny-proto+name.pb.h funny-proto+name.pb.o
 bc_encode: bc_alltypes.pb.o pb_encode.o bc_encode.o
 bc_decode: bc_alltypes.pb.o pb_decode.o bc_decode.o
 
diff --git a/tests/funny-proto+name.proto b/tests/funny-proto+name.proto
new file mode 100644 (file)
index 0000000..e69de29