diff --git a/migrate_cpp/migrate_cpp.py b/migrate_cpp/migrate_cpp.py index b76132e6201d..1746b8134517 100644 --- a/migrate_cpp/migrate_cpp.py +++ b/migrate_cpp/migrate_cpp.py @@ -14,68 +14,95 @@ import sys _CLANG_TIDY = "../external/bootstrap_clang_toolchain/bin/clang-tidy" _CPP_REFACTORING = "./cpp_refactoring/cpp_refactoring" -_CPP_EXTS = {".h", ".c", ".cc", ".cpp", ".cxx"} +_H_EXTS = {".h", ".hpp"} +_CPP_EXTS = {".c", ".cc", ".cpp", ".cxx"} -def _data_file(relative_path): - """Returns the path to a data file.""" - return os.path.join(os.path.dirname(sys.argv[0]), relative_path) - - -def _parse_args(args=None): - """Parses command-line arguments and flags.""" - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument( - "dir", - type=str, - help="A directory containing C++ files to migrate to Carbon.", - ) - parsed_args = parser.parse_args(args=args) - return parsed_args - - -def _gather_files(parsed_args): - """Returns the list of C++ files to convert.""" - all_files = glob.glob( - os.path.join(parsed_args.dir, "**/*.*"), recursive=True - ) - cpp_files = [f for f in all_files if os.path.splitext(f)[1] in _CPP_EXTS] - if not cpp_files: - sys.exit( - "%r doesn't contain any C++ files to convert." % parsed_args.dir +class _Workflow(object): + def __init__(self): + """Parses command-line arguments and flags.""" + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "dir", + type=str, + help="A directory containing C++ files to migrate to Carbon.", ) - return sorted(cpp_files) + parsed_args = parser.parse_args() + self._parsed_args = parsed_args + self._data_dir = os.path.dirname(sys.argv[0]) -def _clang_tidy(parsed_args, cpp_files): - """Runs clang-tidy to fix C++ files in a directory.""" - print("Running clang-tidy...") - clang_tidy = _data_file(_CLANG_TIDY) - with open(_data_file("clang_tidy.yaml")) as f: - config = f.read() - subprocess.run([clang_tidy, "--fix", "--config", config] + cpp_files) + # Validate arguments. + if not os.path.isdir(parsed_args.dir): + sys.exit("%r must point to a directory." % parsed_args.dir) + def run(self): + """Runs the migration workflow.""" + self._gather_files() + self._clang_tidy() + self._cpp_refactoring() + self._rename_files() + self._print_header("Done!") -def _cpp_refactoring(parsed_args, cpp_files): - """Runs cpp_refactoring to migrate C++ files towards Carbon syntax.""" - print("Running cpp_refactoring...") - cpp_refactoring = _data_file(_CPP_REFACTORING) - subprocess.run([cpp_refactoring] + cpp_files) + def _data_file(self, relative_path): + """Returns the path to a data file.""" + return os.path.join(self._data_dir, relative_path) + @staticmethod + def _print_header(header): + print("*" * 79) + print("* %-75s *" % header) + print("*" * 79) -def _main(): - """Main program execution.""" - parsed_args = _parse_args() + def _gather_files(self): + """Returns the list of C++ files to convert.""" + self._print_header("Gathering C++ files...") + all_files = glob.glob( + os.path.join(self._parsed_args.dir, "**/*.*"), recursive=True + ) + exts = _CPP_EXTS.union(_H_EXTS) + cpp_files = [f for f in all_files if os.path.splitext(f)[1] in exts] + if not cpp_files: + sys.exit( + "%r doesn't contain any C++ files to convert." + % self._parsed_args.dir + ) + self._cpp_files = sorted(cpp_files) + print("%d files found." % len(self._cpp_files)) - # Validate arguments. - if not os.path.isdir(parsed_args.dir): - sys.exit("%r must point to a directory." % parsed_args.dir) + def _clang_tidy(self): + """Runs clang-tidy to fix C++ files in a directory.""" + self._print_header("Running clang-tidy...") + clang_tidy = self._data_file(_CLANG_TIDY) + with open(self._data_file("clang_tidy.yaml")) as f: + config = f.read() + subprocess.run( + [clang_tidy, "--fix", "--config", config] + self._cpp_files + ) - cpp_files = _gather_files(parsed_args) - _clang_tidy(parsed_args, cpp_files) - _cpp_refactoring(parsed_args, cpp_files) - print("Done!") + def _cpp_refactoring(self): + """Runs cpp_refactoring to migrate C++ files towards Carbon syntax.""" + self._print_header("Running cpp_refactoring...") + cpp_refactoring = self._data_file(_CPP_REFACTORING) + subprocess.run([cpp_refactoring] + self._cpp_files) + + def _rename_files(self): + """Renames C++ files to the destination Carbon filenames.""" + api_renames = 0 + impl_renames = 0 + for f in self._cpp_files: + parts = os.path.splitext(f) + if parts[1] in _H_EXTS: + os.rename(f, parts[0] + ".carbon") + api_renames += 1 + else: + os.rename(f, parts[0] + ".impl.carbon") + impl_renames += 1 + print( + "Renaming resulted in %d API files and %d impl files." + % (api_renames, impl_renames) + ) if __name__ == "__main__": - _main() + _Workflow().run() diff --git a/third_party/examples/woff2/carbon/compile_flags.txt b/third_party/examples/woff2/carbon/compile_flags.txt index 6758a4d987f7..d8e8f9d86419 100644 --- a/third_party/examples/woff2/carbon/compile_flags.txt +++ b/third_party/examples/woff2/carbon/compile_flags.txt @@ -53,4 +53,4 @@ -D__TIME__="redacted" -Wno-unused-variable -Wno-unused-const-variable --Wno-sign-compare \ No newline at end of file +-Wno-sign-compare diff --git a/third_party/examples/woff2/carbon/include/woff2/decode.h b/third_party/examples/woff2/carbon/include/woff2/decode.carbon similarity index 100% rename from third_party/examples/woff2/carbon/include/woff2/decode.h rename to third_party/examples/woff2/carbon/include/woff2/decode.carbon diff --git a/third_party/examples/woff2/carbon/include/woff2/encode.h b/third_party/examples/woff2/carbon/include/woff2/encode.carbon similarity index 100% rename from third_party/examples/woff2/carbon/include/woff2/encode.h rename to third_party/examples/woff2/carbon/include/woff2/encode.carbon diff --git a/third_party/examples/woff2/carbon/include/woff2/output.h b/third_party/examples/woff2/carbon/include/woff2/output.carbon similarity index 100% rename from third_party/examples/woff2/carbon/include/woff2/output.h rename to third_party/examples/woff2/carbon/include/woff2/output.carbon diff --git a/third_party/examples/woff2/carbon/src/buffer.h b/third_party/examples/woff2/carbon/src/buffer.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/buffer.h rename to third_party/examples/woff2/carbon/src/buffer.carbon diff --git a/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer.cc b/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer.cc rename to third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer_new_entry.cc b/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer_new_entry.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer_new_entry.cc rename to third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer_new_entry.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/file.h b/third_party/examples/woff2/carbon/src/file.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/file.h rename to third_party/examples/woff2/carbon/src/file.carbon diff --git a/third_party/examples/woff2/carbon/src/font.h b/third_party/examples/woff2/carbon/src/font.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/font.h rename to third_party/examples/woff2/carbon/src/font.carbon diff --git a/third_party/examples/woff2/carbon/src/font.cc b/third_party/examples/woff2/carbon/src/font.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/font.cc rename to third_party/examples/woff2/carbon/src/font.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/glyph.h b/third_party/examples/woff2/carbon/src/glyph.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/glyph.h rename to third_party/examples/woff2/carbon/src/glyph.carbon diff --git a/third_party/examples/woff2/carbon/src/glyph.cc b/third_party/examples/woff2/carbon/src/glyph.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/glyph.cc rename to third_party/examples/woff2/carbon/src/glyph.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/normalize.h b/third_party/examples/woff2/carbon/src/normalize.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/normalize.h rename to third_party/examples/woff2/carbon/src/normalize.carbon diff --git a/third_party/examples/woff2/carbon/src/normalize.cc b/third_party/examples/woff2/carbon/src/normalize.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/normalize.cc rename to third_party/examples/woff2/carbon/src/normalize.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/port.h b/third_party/examples/woff2/carbon/src/port.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/port.h rename to third_party/examples/woff2/carbon/src/port.carbon diff --git a/third_party/examples/woff2/carbon/src/round.h b/third_party/examples/woff2/carbon/src/round.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/round.h rename to third_party/examples/woff2/carbon/src/round.carbon diff --git a/third_party/examples/woff2/carbon/src/store_bytes.h b/third_party/examples/woff2/carbon/src/store_bytes.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/store_bytes.h rename to third_party/examples/woff2/carbon/src/store_bytes.carbon diff --git a/third_party/examples/woff2/carbon/src/table_tags.h b/third_party/examples/woff2/carbon/src/table_tags.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/table_tags.h rename to third_party/examples/woff2/carbon/src/table_tags.carbon diff --git a/third_party/examples/woff2/carbon/src/table_tags.cc b/third_party/examples/woff2/carbon/src/table_tags.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/table_tags.cc rename to third_party/examples/woff2/carbon/src/table_tags.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/transform.h b/third_party/examples/woff2/carbon/src/transform.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/transform.h rename to third_party/examples/woff2/carbon/src/transform.carbon diff --git a/third_party/examples/woff2/carbon/src/transform.cc b/third_party/examples/woff2/carbon/src/transform.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/transform.cc rename to third_party/examples/woff2/carbon/src/transform.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/variable_length.h b/third_party/examples/woff2/carbon/src/variable_length.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/variable_length.h rename to third_party/examples/woff2/carbon/src/variable_length.carbon diff --git a/third_party/examples/woff2/carbon/src/variable_length.cc b/third_party/examples/woff2/carbon/src/variable_length.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/variable_length.cc rename to third_party/examples/woff2/carbon/src/variable_length.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/woff2_common.h b/third_party/examples/woff2/carbon/src/woff2_common.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/woff2_common.h rename to third_party/examples/woff2/carbon/src/woff2_common.carbon diff --git a/third_party/examples/woff2/carbon/src/woff2_common.cc b/third_party/examples/woff2/carbon/src/woff2_common.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/woff2_common.cc rename to third_party/examples/woff2/carbon/src/woff2_common.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/woff2_compress.cc b/third_party/examples/woff2/carbon/src/woff2_compress.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/woff2_compress.cc rename to third_party/examples/woff2/carbon/src/woff2_compress.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/woff2_dec.cc b/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/woff2_dec.cc rename to third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/woff2_decompress.cc b/third_party/examples/woff2/carbon/src/woff2_decompress.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/woff2_decompress.cc rename to third_party/examples/woff2/carbon/src/woff2_decompress.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/woff2_enc.cc b/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/woff2_enc.cc rename to third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/woff2_info.cc b/third_party/examples/woff2/carbon/src/woff2_info.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/woff2_info.cc rename to third_party/examples/woff2/carbon/src/woff2_info.impl.carbon diff --git a/third_party/examples/woff2/carbon/src/woff2_out.cc b/third_party/examples/woff2/carbon/src/woff2_out.impl.carbon similarity index 100% rename from third_party/examples/woff2/carbon/src/woff2_out.cc rename to third_party/examples/woff2/carbon/src/woff2_out.impl.carbon