migrate_cpp.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """Migrates C++ code to Carbon."""
  2. __copyright__ = """
  3. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  4. Exceptions. See /LICENSE for license information.
  5. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. """
  7. import argparse
  8. import glob
  9. import os
  10. import subprocess
  11. import sys
  12. _CLANG_TIDY = "../external/bootstrap_clang_toolchain/bin/clang-tidy"
  13. _CPP_REFACTORING = "./cpp_refactoring/cpp_refactoring"
  14. _H_EXTS = {".h", ".hpp"}
  15. _CPP_EXTS = {".c", ".cc", ".cpp", ".cxx"}
  16. class _Workflow(object):
  17. def __init__(self):
  18. """Parses command-line arguments and flags."""
  19. parser = argparse.ArgumentParser(description=__doc__)
  20. parser.add_argument(
  21. "dir",
  22. type=str,
  23. help="A directory containing C++ files to migrate to Carbon.",
  24. )
  25. parsed_args = parser.parse_args()
  26. self._parsed_args = parsed_args
  27. self._data_dir = os.path.dirname(sys.argv[0])
  28. # Validate arguments.
  29. if not os.path.isdir(parsed_args.dir):
  30. sys.exit("%r must point to a directory." % parsed_args.dir)
  31. def run(self):
  32. """Runs the migration workflow."""
  33. self._gather_files()
  34. self._clang_tidy()
  35. self._cpp_refactoring()
  36. self._rename_files()
  37. self._print_header("Done!")
  38. def _data_file(self, relative_path):
  39. """Returns the path to a data file."""
  40. return os.path.join(self._data_dir, relative_path)
  41. @staticmethod
  42. def _print_header(header):
  43. print("*" * 79)
  44. print("* %-75s *" % header)
  45. print("*" * 79)
  46. def _gather_files(self):
  47. """Returns the list of C++ files to convert."""
  48. self._print_header("Gathering C++ files...")
  49. all_files = glob.glob(
  50. os.path.join(self._parsed_args.dir, "**/*.*"), recursive=True
  51. )
  52. exts = _CPP_EXTS.union(_H_EXTS)
  53. cpp_files = [f for f in all_files if os.path.splitext(f)[1] in exts]
  54. if not cpp_files:
  55. sys.exit(
  56. "%r doesn't contain any C++ files to convert."
  57. % self._parsed_args.dir
  58. )
  59. self._cpp_files = sorted(cpp_files)
  60. print("%d files found." % len(self._cpp_files))
  61. def _clang_tidy(self):
  62. """Runs clang-tidy to fix C++ files in a directory."""
  63. self._print_header("Running clang-tidy...")
  64. clang_tidy = self._data_file(_CLANG_TIDY)
  65. with open(self._data_file("clang_tidy.yaml")) as f:
  66. config = f.read()
  67. subprocess.run(
  68. [clang_tidy, "--fix", "--config", config] + self._cpp_files
  69. )
  70. def _cpp_refactoring(self):
  71. """Runs cpp_refactoring to migrate C++ files towards Carbon syntax."""
  72. self._print_header("Running cpp_refactoring...")
  73. cpp_refactoring = self._data_file(_CPP_REFACTORING)
  74. subprocess.run([cpp_refactoring] + self._cpp_files)
  75. def _rename_files(self):
  76. """Renames C++ files to the destination Carbon filenames."""
  77. api_renames = 0
  78. impl_renames = 0
  79. for f in self._cpp_files:
  80. parts = os.path.splitext(f)
  81. if parts[1] in _H_EXTS:
  82. os.rename(f, parts[0] + ".carbon")
  83. api_renames += 1
  84. else:
  85. os.rename(f, parts[0] + ".impl.carbon")
  86. impl_renames += 1
  87. print(
  88. "Renaming resulted in %d API files and %d impl files."
  89. % (api_renames, impl_renames)
  90. )
  91. if __name__ == "__main__":
  92. _Workflow().run()