Merge branch 'mkenums' into 'main'

glib-mkenums: feature use of previous symbols in evaluation

See merge request GNOME/glib!3043
This commit is contained in:
Philip Withnall 2022-11-08 11:12:40 +00:00
commit 9a0c8a585d
2 changed files with 44 additions and 13 deletions

View File

@ -143,7 +143,7 @@ enumname_prefix = '' # prefix of $enumname
enumindex = 0 # Global enum counter
firstenum = 1 # Is this the first enumeration per file?
entries = [] # [ name, val ] for each entry
sandbox = None # sandbox for safe evaluation of expressions
c_namespace = {} # C symbols namespace.
output = '' # Filename to write result into
@ -247,15 +247,12 @@ def parse_entries(file, file_name):
if flags is None and value is not None and '<<' in value:
seenbitshift = 1
if seenprivate:
continue
if options is not None:
options = parse_trigraph(options)
if 'skip' not in options:
entries.append((name, value, options.get('nick')))
entries.append((name, value, seenprivate, options.get('nick')))
else:
entries.append((name, value))
entries.append((name, value, seenprivate))
else:
m = re.match(r'''\s*
/\*< (([^*]|\*(?!/))*) >\s*\*/
@ -489,7 +486,7 @@ if len(fhead) > 0:
write_output(prod)
def process_file(curfilename):
global entries, flags, seenbitshift, seenprivate, enum_prefix
global entries, flags, seenbitshift, seenprivate, enum_prefix, c_namespace
firstenum = True
try:
@ -580,7 +577,7 @@ def process_file(curfilename):
# Autogenerate a prefix
if enum_prefix is None:
for entry in entries:
if len(entry) < 3 or entry[2] is None:
if not entry[2] and (len(entry) < 4 or entry[3] is None):
name = entry[0]
if enum_prefix is not None:
enum_prefix = os.path.commonprefix([name, enum_prefix])
@ -601,10 +598,11 @@ def process_file(curfilename):
for e in entries:
name = e[0]
num = e[1]
if len(e) < 3 or e[2] is None:
private = e[2]
if len(e) < 4 or e[3] is None:
nick = re.sub(r'^' + enum_prefix, '', name)
nick = nick.replace('_', '-').lower()
e = (name, num, nick)
e = (name, num, private, nick)
fixed_entries.append(e)
entries = fixed_entries
@ -720,7 +718,7 @@ def process_file(curfilename):
next_num = 0
prod = replace_specials(prod)
for name, num, nick in entries:
for name, num, private, nick in entries:
tmp_prod = prod
if '\u0040valuenum\u0040' in prod:
@ -729,7 +727,7 @@ def process_file(curfilename):
if num is not None:
# use sandboxed evaluation as a reasonable
# approximation to C constant folding
inum = eval(num, {}, {})
inum = eval(num, {}, c_namespace)
# make sure it parsed to an integer
if not isinstance(inum, int):
@ -738,9 +736,13 @@ def process_file(curfilename):
else:
num = next_num
c_namespace[name] = num
tmp_prod = tmp_prod.replace('\u0040valuenum\u0040', str(num))
next_num = int(num) + 1
if private:
continue
tmp_prod = tmp_prod.replace('\u0040VALUENAME\u0040', name)
tmp_prod = tmp_prod.replace('\u0040valuenick\u0040', nick)
if flags:

View File

@ -649,7 +649,7 @@ comment: {standard_bottom_comment}
"ENUM",
"ENUM_VALUE_PUBLIC2",
"public2",
"0",
"1",
)
def test_available_in(self):
@ -730,6 +730,35 @@ comment: {standard_bottom_comment}
"4",
)
def test_enum_symbolic_expression(self):
"""Test use of symbol in value expression."""
h_contents = """
typedef enum {
/*< private >*/
ENUM_VALUE_PRIVATE = 5,
/*< public >*/
ENUM_VALUE_PUBLIC = ENUM_VALUE_PRIVATE + 2,
} TestSymbolicEnum;
"""
result = self.runMkenumsWithHeader(h_contents)
self.assertEqual("", result.err)
self.assertSingleEnum(
result,
"TestSymbolicEnum",
"test_symbolic_enum",
"TEST_SYMBOLIC_ENUM",
"SYMBOLIC_ENUM",
"TEST",
"",
"enum",
"Enum",
"ENUM",
"ENUM_VALUE_PUBLIC",
"public",
"7",
)
class TestRspMkenums(TestMkenums):
"""Run all tests again in @rspfile mode"""