Name sanitizing for entity names for Protocol Buffers entities.
* If entity names conflicts with ObjectiveC reserved words or some method name (e.g., hash, description), it is renamed.
diff --git a/prefixize.py b/prefixize.py
index 28a5769..affb73f 100644
--- a/prefixize.py
+++ b/prefixize.py
@@ -17,9 +17,17 @@
class MyVisitor(m.Visitor):
content=""
offset=0
+ doNameSanitization=False
statementsChanged=0
prefix=""
+ reserved = ['auto','else','long','switch','break','enum','register','typedef','case','extern','return',
+ 'union','char','float','short','unsigned','const','for','signed','void','continue','goto',
+ 'sizeof','volatile','default','if','static','while','do','int','struct','_Packed','double',
+ 'protocol','interface','implementation','NSObject','NSInteger','NSNumber','CGFloat','property',
+ 'nonatomic', 'retain','strong', 'weak', 'unsafe_unretained', 'readwrite' 'readonly',
+ 'hash', 'description', 'id']
+
def prefixize(self, lu, oldId):
'''
Simple frefixization - with constant prefix to all identifiers (flat).
@@ -53,6 +61,37 @@
self.offset += newCodeLen - oldCodeLen
self.statementsChanged+=1
+ def isNameInvalid(self, name):
+ '''
+ Returns true if name conflicts with objectiveC. It cannot be from the list of a reserved words
+ or starts with init or new.
+ :param name:
+ :return:
+ '''
+ return name in self.reserved or name.startswith('init') or name.startswith('new')
+
+ def sanitizeName(self, obj):
+ '''
+ Replaces entity name if it is considered conflicting.
+ :param obj:
+ :return:
+ '''
+ if not self.doNameSanitization:
+ return
+
+ if isinstance(obj, m.Name):
+ n = str(obj.value)
+ if self.isNameInvalid(n):
+ if self.verbose>1:
+ print "!!Invalid name: %s, %s" % (n, obj)
+ self.replace(obj.value, 'x'+n)
+
+ elif isinstance(obj, m.LU):
+ return
+
+ else:
+ return
+
def __init__(self):
super(MyVisitor, self).__init__()
@@ -71,8 +110,17 @@
'''Ignore'''
return True
+ def visit_LU(self, obj):
+ return True
+
+ def visit_default(self, obj):
+ return True
+
def visit_FieldDirective(self, obj):
'''Ignore, Field directive, e.g., default value.'''
+ n = str(obj.name)
+ if n == 'default':
+ self.sanitizeName(obj.value)
return True
def visit_FieldType(self, obj):
@@ -81,18 +129,21 @@
def visit_FieldDefinition(self, obj):
'''New field defined in a message, check type, if is name, prefixize.'''
- if self.verbose > 3:
- print "\tField: name=%s, lex=%s" % (obj.name, obj.lexspan)
+ if self.verbose > 4:
+ print "\tField: name=%s, lex=%s parent=%s" % (obj.name, obj.lexspan, obj.parent!=None)
if isinstance(obj.ftype, m.Name):
self.prefixize(obj.ftype, obj.ftype.value)
+ self.sanitizeName(obj.ftype)
+ self.sanitizeName(obj.name)
return True
def visit_EnumFieldDefinition(self, obj):
- if self.verbose > 3:
+ if self.verbose > 4:
print "\tEnumField: name=%s, %s" % (obj.name, obj)
+ self.sanitizeName(obj.name)
return True
def visit_EnumDefinition(self, obj):
@@ -109,6 +160,7 @@
print "Message, [%s] lex=%s body=|%s|\n" % (obj.name, obj.lexspan, obj.body)
self.prefixize(obj.name, str(obj.name.value))
+ self.sanitizeName(obj.name)
return True
def visit_MessageExtension(self, obj):
@@ -117,12 +169,15 @@
print "MessageEXT, [%s] body=%s\n\n" % (obj.name, obj.body)
self.prefixize(obj.name, obj.name.value)
+ self.sanitizeName(obj.name)
return True
def visit_MethodDefinition(self, obj):
+ self.sanitizeName(obj.name)
return True
def visit_ServiceDefinition(self, obj):
+ self.sanitizeName(obj.name)
return True
def visit_ExtensionsDirective(self, obj):
@@ -134,6 +189,9 @@
def visit_Name(self, obj):
return True
+ def visit_DotName(self, obj):
+ return True
+
def visit_Proto(self, obj):
return True
@@ -145,6 +203,7 @@
parser.add_argument('-o','--outdir', help='Output directory', required=False, default="", dest='outdir')
parser.add_argument('-e','--echo', help='Writes output to the standard output', required=False, default=False)
parser.add_argument('-v','--verbose', help='Writes output to the standard output', required=False, default=0, type=int)
+ parser.add_argument('-s','--sanitize', help='If set, performs entity name sanitization - renames conflicting names', required=False, default=0, type=int)
parser.add_argument('file')
args = parser.parse_args()
@@ -158,6 +217,8 @@
v = MyVisitor()
v.offset = 0
v.prefix = args.prefix
+ v.verbose = args.verbose
+ v.doNameSanitization = args.sanitize > 0
with open(args.file, 'r') as content_file:
v.content = content_file.read()