Bug 1188888 - Part 2 - Generate readable Telemetry string tables. r=froydnj

This commit is contained in:
Georg Fritzsche 2016-07-20 17:10:24 +02:00
parent bea67fe9f5
commit 863e08b5df

View File

@ -37,11 +37,18 @@ class StringTable:
def writeDefinition(self, f, name):
"""Writes the string table to a file as a C const char array.
This writes out the string table as one single C char array for memory
size reasons, separating the individual strings with '\0' characters.
This way we can index directly into the string array and avoid the additional
storage costs for the pointers to them (and potential extra relocations for those).
:param f: the output stream.
:param name: the name of the output array.
"""
entries = self.table.items()
entries.sort(key=lambda x:x[1])
# Avoid null-in-string warnings with GCC and potentially
# overlong string constants; write everything out the long way.
def explodeToCharArray(string):
@ -51,16 +58,19 @@ class StringTable:
else:
return "'%s'" % s
return ", ".join(map(toCChar, string))
f.write("const char %s[] = {\n" % name)
for (string, offset) in entries[:-1]:
for (string, offset) in entries:
if "*/" in string:
raise ValueError, "String in string table contains unexpected sequence '*/': %s" % string
e = explodeToCharArray(string)
if e:
f.write(" /* %5d */ %s, '\\0',\n"
% (offset, explodeToCharArray(string)))
f.write(" /* %5d - \"%s\" */ %s, '\\0',\n"
% (offset, string, explodeToCharArray(string)))
else:
f.write(" /* %5d */ '\\0',\n" % offset)
f.write(" /* %5d */ %s, '\\0' };\n\n"
% (entries[-1][1], explodeToCharArray(entries[-1][0])))
f.write(" /* %5d - \"%s\" */ '\\0',\n" % (offset, string))
f.write("};\n\n")
def static_assert(output, expression, message):
"""Writes a C++ compile-time assertion expression to a file.