Commit e3bf3a35 authored by David 'Digit' Turner's avatar David 'Digit' Turner Committed by Commit Bot

android: crazy_linker: Minor polish on tests.

Tiny refactor of the C source generating Python functions.
No bugfix / feature at all. But the result of calling
'updated_auto_generated_sources.sh' doesn't need to be
fixed by 'git cl format' anymore.

- Prevent trailing whitespace on commented lines produced
  by CSourceForComments().

- More consistent output from CSourceForIntegerHexArray()
  and CSourceForConstCharArray() by using a common
  CSourceForArrayData() function that takes a lambda to
  format individual values.

R=pasko@chromium.org, agrieve@chromium.org
BUG=NONE

Change-Id: Ifc9c5c175a3432acb5ce3d7918259c4ee294dd04
Reviewed-on: https://chromium-review.googlesource.com/c/1297423Reviewed-by: default avatarDavid Turner <digit@chromium.org>
Reviewed-by: default avatarEgor Pasko <pasko@chromium.org>
Commit-Queue: David Turner <digit@chromium.org>
Cr-Commit-Position: refs/heads/master@{#602331}
parent 2d0226c9
...@@ -13,7 +13,7 @@ namespace crazy { ...@@ -13,7 +13,7 @@ namespace crazy {
namespace testing { namespace testing {
// SysV ELF hash table: num_buckets=4 num_chain=16 // SysV ELF hash table: num_buckets=4 num_chain=16
// //
// idx symbol hash bucket chain // idx symbol hash bucket chain
// 0 <STN_UNDEF> // 0 <STN_UNDEF>
// 1 isnan 0070a47e 2 5 // 1 isnan 0070a47e 2 5
...@@ -31,19 +31,19 @@ namespace testing { ...@@ -31,19 +31,19 @@ namespace testing {
// 13 gettyen 0dcbbfde 2 14 // 13 gettyen 0dcbbfde 2 14
// 14 uselib 07c9c2f2 2 0 // 14 uselib 07c9c2f2 2 0
// 15 cfsetispeed 0b63b274 0 0 // 15 cfsetispeed 0b63b274 0 0
// //
// Buckets: 2, 8, 1, 3 // Buckets: 2, 8, 1, 3
// //
static const char kTestElfStringTable[145] = { static const char kTestElfStringTable[145] = {
'\0','i','s','n','a','n','\0','f','r','e','e','l','o','c','a','l','\0', '\0','i','s','n','a','n','\0','f','r','e','e','l','o','c','a','l','\0','h',
'h','c','r','e','a','t','e','_','\0','g','e','t','o','p','t','_','l','o', 'c','r','e','a','t','e','_','\0','g','e','t','o','p','t','_','l','o','n',
'n','g','_','o','n','l','\0','e','n','d','r','p','c','e','n','\0','p', 'g','_','o','n','l','\0','e','n','d','r','p','c','e','n','\0','p','t','h',
't','h','r','e','a','d','_','m','u','t','e','x','_','l','o','c','k','\0', 'r','e','a','d','_','m','u','t','e','x','_','l','o','c','k','\0','i','s',
'i','s','i','n','f','\0','s','e','t','r','l','i','m','i','\0','g','e', 'i','n','f','\0','s','e','t','r','l','i','m','i','\0','g','e','t','s','p',
't','s','p','e','n','\0','u','m','o','u','n','\0','s','t','r','s','i', 'e','n','\0','u','m','o','u','n','\0','s','t','r','s','i','g','n','a','\0',
'g','n','a','\0','l','i','s','t','x','a','t','t','\0','g','e','t','t', 'l','i','s','t','x','a','t','t','\0','g','e','t','t','y','e','n','\0','u',
'y','e','n','\0','u','s','e','l','i','b','\0','c','f','s','e','t','i', 's','e','l','i','b','\0','c','f','s','e','t','i','s','p','e','e','d','\0',
's','p','e','e','d','\0','\0'}; '\0'};
// Auto-generated macro used to list all symbols // Auto-generated macro used to list all symbols
// XX must be a macro that takes the following parameters: // XX must be a macro that takes the following parameters:
......
...@@ -13,9 +13,9 @@ namespace crazy { ...@@ -13,9 +13,9 @@ namespace crazy {
namespace testing { namespace testing {
// GNU hash table: num_buckets=4 bloom_size=2 bloom_shift=5 // GNU hash table: num_buckets=4 bloom_size=2 bloom_shift=5
// //
// idx symbol hash bucket bloom32 bloom64 chain // idx symbol hash bucket bloom32 bloom64 chain
// //
// 0 ST_UNDEF // 0 ST_UNDEF
// 1 cfsetispeed 830acc54 0 0:20:02 1:20:34 830acc54 // 1 cfsetispeed 830acc54 0 0:20:02 1:20:34 830acc54
// 2 strsigna 90f1e4b0 0 1:16:05 0:48:37 90f1e4b0 // 2 strsigna 90f1e4b0 0 1:16:05 0:48:37 90f1e4b0
...@@ -32,33 +32,33 @@ namespace testing { ...@@ -32,33 +32,33 @@ namespace testing {
// 13 getspen f07b2a7b 3 1:27:19 1:59:19 f07b2a7a // 13 getspen f07b2a7b 3 1:27:19 1:59:19 f07b2a7a
// 14 pthread_mutex_lock 4f152227 3 1:07:17 0:39:17 4f152226 // 14 pthread_mutex_lock 4f152227 3 1:07:17 0:39:17 4f152226
// 15 getopt_long_onl 57b1584f 3 0:15:02 1:15:02 57b1584f // 15 getopt_long_onl 57b1584f 3 0:15:02 1:15:02 57b1584f
// //
// Buckets: 1, 5, 8, 13 // Buckets: 1, 5, 8, 13
// //
// Bloom filter (32 bits): // Bloom filter (32 bits):
// bit# 24 16 8 0 // bit# 24 16 8 0
// .x....xx ...x.x.. xx...... .....x.x // .x....xx ...x.x.. xx...... .....x.x
// xxx.x... ....xxxx .x..x.x. x.x.xx.. // xxx.x... ....xxxx .x..x.x. x.x.xx..
// //
// also as: 0x4314c005 0xe80f4aac // also as: 0x4314c005 0xe80f4aac
// //
// Bloom filter (64 bits): // Bloom filter (64 bits):
// bit# 56 48 40 32 24 16 8 0 // bit# 56 48 40 32 24 16 8 0
// .......x .......x .x....x. x.x..... ..x...x. ...x..x. ........ .......x // .......x .......x .x....x. x.x..... ..x...x. ...x..x. ........ .......x
// .x..x... .....x.. ....x.x. .....x.. xx..x... ...xxx.. xx...... ....xx.x // .x..x... .....x.. ....x.x. .....x.. xx..x... ...xxx.. xx...... ....xx.x
// //
// also as: 0x010142a022120001 0x48040a04c81cc00d // also as: 0x010142a022120001 0x48040a04c81cc00d
// //
static const char kTestGnuStringTable[145] = { static const char kTestGnuStringTable[145] = {
'\0','c','f','s','e','t','i','s','p','e','e','d','\0','s','t','r','s', '\0','c','f','s','e','t','i','s','p','e','e','d','\0','s','t','r','s','i',
'i','g','n','a','\0','h','c','r','e','a','t','e','_','\0','e','n','d', 'g','n','a','\0','h','c','r','e','a','t','e','_','\0','e','n','d','r','p',
'r','p','c','e','n','\0','u','s','e','l','i','b','\0','g','e','t','t', 'c','e','n','\0','u','s','e','l','i','b','\0','g','e','t','t','y','e','n',
'y','e','n','\0','u','m','o','u','n','\0','f','r','e','e','l','o','c', '\0','u','m','o','u','n','\0','f','r','e','e','l','o','c','a','l','\0','l',
'a','l','\0','l','i','s','t','x','a','t','t','\0','i','s','n','a','n', 'i','s','t','x','a','t','t','\0','i','s','n','a','n','\0','i','s','i','n',
'\0','i','s','i','n','f','\0','s','e','t','r','l','i','m','i','\0','g', 'f','\0','s','e','t','r','l','i','m','i','\0','g','e','t','s','p','e','n',
'e','t','s','p','e','n','\0','p','t','h','r','e','a','d','_','m','u','t', '\0','p','t','h','r','e','a','d','_','m','u','t','e','x','_','l','o','c',
'e','x','_','l','o','c','k','\0','g','e','t','o','p','t','_','l','o','n', 'k','\0','g','e','t','o','p','t','_','l','o','n','g','_','o','n','l','\0',
'g','_','o','n','l','\0','\0'}; '\0'};
// Auto-generated macro used to list all symbols // Auto-generated macro used to list all symbols
// XX must be a macro that takes the following parameters: // XX must be a macro that takes the following parameters:
......
...@@ -32,6 +32,43 @@ r''' ...@@ -32,6 +32,43 @@ r'''
#endif // {guard_macro_name}''' #endif // {guard_macro_name}'''
) )
def CSourceForArrayData(values, formatter, margin=4, width=80):
"""Turn an array of values into a C source array data definition.
Args:
values: Array of input values.
formatter: Formatting function, applied to each input value to get a
C-source description of the value.
margin: Left-side margin / indentation level.
width: Maximum line width.
Returns:
A string containing the data definition as a C source fragment.
"""
read_pos = 0
read_len = len(values)
write_pos = margin
line_start = ' ' * margin
# Account for the margin + one final comma.
max_width = width - margin - 1
out = ''
while read_pos < read_len:
out += line_start
write_pos = 0
comma = ''
while read_pos < read_len:
item = comma + formatter(values[read_pos])
if write_pos + len(item) > max_width:
break # Too long, break line before this item.
out += item
read_pos += 1
write_pos += len(item)
comma = ','
if read_pos == read_len:
break
out += ',\n'
return out
def CSourceForIntegerHexArray(values, num_bits, margin=4, width=80): def CSourceForIntegerHexArray(values, num_bits, margin=4, width=80):
"""Turn an array of integers into a C source array data definition. """Turn an array of integers into a C source array data definition.
...@@ -44,23 +81,22 @@ def CSourceForIntegerHexArray(values, num_bits, margin=4, width=80): ...@@ -44,23 +81,22 @@ def CSourceForIntegerHexArray(values, num_bits, margin=4, width=80):
A string containing the data definition as a C source fragment. A string containing the data definition as a C source fragment.
""" """
chars_per_word = num_bits / 4 chars_per_word = num_bits / 4
# Account for 0x prefix + one space + one comma format_str = ' 0x%%0%dx' % chars_per_word
max_items_per_line = (width - margin - 2) / (4 + chars_per_word) out = CSourceForArrayData(values, lambda x: format_str % x,
line_start = ' ' * (margin - 1) margin - 1, width)
slen = len(values) out += ',\n'
n = 0
format_str = ' 0x%%0%dx,' % chars_per_word
out = ''
while n < slen:
out += line_start
n2 = min(n + max_items_per_line, slen)
while n < n2:
out += format_str % values[n]
n += 1
out += '\n';
return out return out
def _FormatChar(ch):
"""Convert a character into its C source description."""
code = ord(ch)
if code < 32 or code > 127:
return "'\\%d'" % code
else:
return "'%s'" % ch
def CSourceForConstCharArray(chars, variable_name, margin=4, width=80): def CSourceForConstCharArray(chars, variable_name, margin=4, width=80):
"""Return C source fragment for static const char C array. """Return C source fragment for static const char C array.
...@@ -70,35 +106,8 @@ def CSourceForConstCharArray(chars, variable_name, margin=4, width=80): ...@@ -70,35 +106,8 @@ def CSourceForConstCharArray(chars, variable_name, margin=4, width=80):
Returns: Returns:
A new string holding a C source fragment for the array definition. A new string holding a C source fragment for the array definition.
""" """
slen = len(chars) out = 'static const char %s[%d] = {\n' % (variable_name, len(chars))
out = 'static const char %s[%d] = {\n' % (variable_name, slen) out += CSourceForArrayData(chars, _FormatChar, margin, width)
line_start = ' ' * margin
# Allow for margin + 4 spaces on the right.
max_width = width - margin - 4
rpos = 0
wpos = margin + 1
while rpos < slen:
out += line_start
wpos = 0
comma = ''
while rpos < slen and wpos < max_width:
ch = chars[rpos]
code = ord(ch)
if code < 32 or code > 127:
ch = "'\\%d'" % code
else:
ch = "'%s'" % ch
ch = comma + ch
if wpos + len(ch) > max_width: # Too long, break line before this char.
break
out += ch
rpos += 1
wpos += len(ch)
comma = ','
if rpos == slen:
break
out += ',\n'
out += '};\n' out += '};\n'
return out return out
...@@ -107,7 +116,12 @@ def CSourceForComments(lines): ...@@ -107,7 +116,12 @@ def CSourceForComments(lines):
"""Wrap the content of |lines| instead into C++ comments.""" """Wrap the content of |lines| instead into C++ comments."""
out = '' out = ''
for line in lines.split('\n'): for line in lines.split('\n'):
out += '// %s\n' % line line = line.rstrip()
if line:
out += '// %s\n' % line
else:
out += '//\n'
return out return out
...@@ -116,6 +130,7 @@ def CSourceBeginAutoGeneratedHeader(script_name, guard_macro_name): ...@@ -116,6 +130,7 @@ def CSourceBeginAutoGeneratedHeader(script_name, guard_macro_name):
script_name=script_name, script_name=script_name,
guard_macro_name=guard_macro_name) guard_macro_name=guard_macro_name)
def CSourceEndAutoGeneratedHeader(script_name, guard_macro_name): def CSourceEndAutoGeneratedHeader(script_name, guard_macro_name):
return _AUTO_GENERATED_HEADER_END_TEMPLATE.format( return _AUTO_GENERATED_HEADER_END_TEMPLATE.format(
script_name=script_name, script_name=script_name,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment