diff --git a/python/mozbuild/mozbuild/code_analysis/mach_commands.py b/python/mozbuild/mozbuild/code_analysis/mach_commands.py --- a/python/mozbuild/mozbuild/code_analysis/mach_commands.py +++ b/python/mozbuild/mozbuild/code_analysis/mach_commands.py @@ -2225,20 +2225,28 @@ "--outgoing", default=False, action="store_true", help="Run clang-format on outgoing files from mercurial repository.", ) + @CommandArgument( + "--standard-io", + default=False, + action="store_true", + help="Run clang-format on contents on stdin and print fixed file to stdout.", + ) def clang_format( self, assume_filename, path, commit, output_path=None, output_format="diff", verbose=False, outgoing=False, + standard_io=False, ): + # Run clang-format or clang-format-diff on the local changes # or files/directories if path is None and outgoing: repo = get_repository_object(self.topsrcdir) path = repo.get_outgoing_files() @@ -2278,10 +2286,14 @@ if rc != 0: return rc if not self._is_version_eligible(): return 1 + + # Expect file on `stdin` and output formatted file to `stdout`. + if standard_io: + return self._run_clang_format_stdio(self._clang_format_path) if path is None: return self._run_clang_format_diff( self._clang_format_diff, self._clang_format_path, commit, output ) @@ -2823,10 +2835,18 @@ if os.path.isfile(f) and f.endswith(extensions): path_list.append(f) return path_list + def _run_clang_format_stdio(self, clang_format): + # Run clang-format on the contents of `stdin`, and print the + # file contents after formatting on `stdout`. + args = [clang_format] + process = subprocess.Popen(args, stdin=sys.stdin, stdout=sys.stdout) + return process.returncode + + def _run_clang_format_in_console(self, clang_format, paths, assume_filename): path_list = self._generate_path_list(assume_filename, False) if path_list == []: return 0