Last active
September 18, 2021 00:53
-
-
Save vsapsai/f9d3603dde95eebd23248da4d7b4f5ec to your computer and use it in GitHub Desktop.
Synthesize test cases for `Sema::addMethodTGlobalList` performance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Generates a test case with a chain of framework, each of those having | |
# multiple classes with `init` method. The dependency chain looks like | |
# | |
# repro.m -> SynthesizedN -> SynthesizedN-1 -> ... -> Synthesized0 -> Base | |
import argparse | |
import os | |
# To reuse in multiple places. | |
def framework_name(index): | |
return f"Synthesized{index}" | |
def write_framework(name, location, header, modulemap): | |
framework_path = os.path.join(location, f"{name}.framework") | |
os.mkdir(framework_path) | |
os.mkdir(os.path.join(framework_path, "Headers")) | |
with open(os.path.join(framework_path, "Headers", f"{name}.h"), "wt") as f: | |
f.write(header) | |
os.mkdir(os.path.join(framework_path, "Modules")) | |
with open(os.path.join(framework_path, "Modules", "module.modulemap"), "wt") as f: | |
f.write(modulemap) | |
def synthesize_common_code(location): | |
modulemap = """ | |
framework module Base { | |
header "Base.h" | |
export * | |
} | |
""" | |
header = """ | |
@interface NSObject | |
- (id)init; | |
@end | |
""" | |
write_framework("Base", location, header, modulemap) | |
def synthesize_framework(index, location, classes_count): | |
name = framework_name(index) | |
modulemap = """ | |
framework module {name} {{ | |
header "{name}.h" | |
export * | |
}} | |
""".format(name=name) | |
previous_import = "" | |
if index > 0: | |
prev_name = framework_name(index - 1) | |
previous_import = f"#import <{prev_name}/{prev_name}.h>" | |
classes = [""" | |
@interface {name}_{i} : NSObject | |
- (id)init; | |
@end | |
""".format(name=name, i=i) for i in range(classes_count)] | |
header = """ | |
#import <Base/Base.h> | |
{previous_import} | |
{all_classes} | |
""".format(previous_import=previous_import, all_classes="\n\n".join(classes)) | |
write_framework(name, location, header, modulemap) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--frameworks-count", type=int, default=10, | |
help="number of frameworks to synthesize") | |
parser.add_argument("--classes-per-framework", type=int, default=5, | |
help="number of classes to create in each framework") | |
parser.add_argument("--output-directory", required=True, | |
help="file path to store the generated files") | |
args = parser.parse_args() | |
output_dir = os.path.abspath(args.output_directory) | |
frameworks_dir = os.path.join(output_dir, "Frameworks") | |
os.mkdir(frameworks_dir) | |
# Write Base.framework. | |
synthesize_common_code(frameworks_dir) | |
# Write synthesized frameworks. | |
for index in range(args.frameworks_count): | |
synthesize_framework(index, frameworks_dir, args.classes_per_framework) | |
# Write a file triggering the imports. | |
last_framework_name = framework_name(args.frameworks_count - 1) | |
repro = """ | |
#import <{name}/{name}.h> | |
""".format(name=last_framework_name) | |
with open(os.path.join(output_dir, "repro.m"), "wt") as f: | |
f.write(repro) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment