82 lines · python
1#!/usr/bin/env python32"""A script to generate FileCheck statements for Fortran runtime funcs.3 4This script can be used to update5flang/test/Transforms/verify-known-runtime-functions.fir6whenever new recognized Fortran runtime functions are added7into flang/Optimizer/Transforms/RuntimeFunctions.inc8or any of the recognized functions changes its signature.9"""10 11# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.12# See https://llvm.org/LICENSE.txt for license information.13# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception14 15import argparse16import os17import re18import sys19 20ADVERT_BEGIN = "// NOTE: Assertions have been autogenerated by "21ADVERT_END = """22// The script allows updating Flang LIT test23// flang/test/Transforms/verify-known-runtime-functions.fir,24// which is intended to verify signatures of Fortran runtime25// functions recognized in flang/Optimizer/Transforms/RuntimeFunctions.inc26// table. If new function is added into the table or27// an existing function changes its signature,28// the SetRuntimeCallAttributesPass may need to be updated29// to properly handle it. Once the pass is verified to work,30// one can update this test using the following output:31// echo "module {}" | fir-opt --gen-runtime-calls-for-test | \\32// generate-checks-for-runtime-funcs.py33"""34 35CHECK_RE_STR = "func.func.*@_Fortran.*"36CHECK_RE = re.compile(CHECK_RE_STR)37 38CHECK_NOT_STR = "// CHECK-NOT: func.func"39CHECK_STR = "// CHECK:"40CHECK_NEXT_STR = "// CHECK-NEXT:"41 42 43def main():44 parser = argparse.ArgumentParser(45 description=__doc__, formatter_class=argparse.RawTextHelpFormatter46 )47 parser.add_argument(48 "input", nargs="?", type=argparse.FileType("r"), default=sys.stdin49 )50 args = parser.parse_args()51 input_lines = [l.rstrip() for l in args.input]52 args.input.close()53 54 repo_path = os.path.join(os.path.dirname(__file__), "..", "..", "..")55 script_name = os.path.relpath(__file__, repo_path)56 autogenerated_note = ADVERT_BEGIN + script_name + "\n" + ADVERT_END57 58 output = sys.stdout59 output.write(autogenerated_note + "\n")60 61 output_lines = []62 output_lines.append(CHECK_NOT_STR)63 check_prefix = CHECK_STR64 for input_line in input_lines:65 if not input_line:66 continue67 68 m = CHECK_RE.match(input_line.lstrip())69 if m:70 output_lines.append(check_prefix + " " + input_line.lstrip())71 check_prefix = CHECK_NEXT_STR72 73 output_lines.append(CHECK_NOT_STR)74 for line in output_lines:75 output.write(line + "\n")76 77 output.close()78 79 80if __name__ == "__main__":81 main()82