Skip to content

Instantly share code, notes, and snippets.

@tlively
Created February 6, 2024 02:02
Show Gist options
  • Save tlively/4e22311736661849e641d02e521a0748 to your computer and use it in GitHub Desktop.
Save tlively/4e22311736661849e641d02e521a0748 to your computer and use it in GitHub Desktop.
Update identifiers in wat tests
#! /usr/bin/python3
"""Ensure all identifiers match the supported format.
"""
import argparse
import glob
import os
import subprocess
import sys
import re
NAME_RE = re.compile(r"\$[^\s\"\(\);,]+")
def warn(msg):
print(f'WARNING: {msg}', file=sys.stderr)
def idchar(c):
if ord('a') <= ord(c) <= ord('z'):
return True
if ord('A') <= ord(c) <= ord('Z'):
return True
if ord('0') <= ord(c) <= ord('9'):
return True
if c in {'!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '/', ':', '<',
'=', '>', '?', '@', '\\', '^', '_', '`', '|', '~'}:
return True
return False
def replace(match):
original = match[0]
for c in original:
if not idchar(c):
return '$"' + original[1:] + '"'
return original
def port_test(args, test):
if not test.endswith('.wast') and not test.endswith('.wat'):
warn(f'Skipping {test} because only .wat and .wast files are supported')
return
forbidden = {
'test/lld/em_js_O0.wat',
'test/lit/passes/separate-data-segments.wast',
'test/lit/wasm-split/basic.wast',
'test/lit/wasm-split/merge-profiles.wast',
'test/lit/wasm-split/verbose.wast',
}
for f in forbidden:
if test.endswith(f):
return
print("porting", test)
with open(test) as file:
text = file.read()
updated = re.sub(NAME_RE, replace, text)
if args.dry_run:
print(updated)
else:
with open(test, 'w') as file:
file.write(updated)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--binaryen-bin', dest='binaryen_bin', default='bin',
help=('Specifies the path to the Binaryen executables in the CMake build'
' directory. Default: bin/ of current directory (i.e. assume an'
' in-tree build).'))
parser.add_argument('--dry', dest='dry_run', action='store_true',
help=('Print the updated tests instead of updating'
' files in-place.'))
parser.add_argument('tests', nargs='+', help='The test files to port')
args = parser.parse_args()
args.binaryen_bin = os.path.abspath(args.binaryen_bin)
for pattern in args.tests:
for test in glob.glob(pattern, recursive=True):
port_test(args, test)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment