clang_util.py 1.07 KB
Newer Older
1 2 3 4 5
# Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights
# reserved. Use of this source code is governed by a BSD-style license that
# can be found in the LICENSE file

from exec_util import exec_cmd
6
import os
7 8
import sys

9 10 11 12
# Script directory.
script_dir = os.path.dirname(__file__)
root_dir = os.path.join(script_dir, os.pardir)

13 14 15 16 17 18
if sys.platform == 'win32':
  # Force use of the clang-format version bundled with depot_tools.
  clang_format_exe = 'clang-format.bat'
else:
  clang_format_exe = 'clang-format'

19 20 21 22 23 24 25
def clang_format(file_name, file_contents):
  # -assume-filename is necessary to find the .clang-format file and determine
  # the language when specifying contents via stdin.
  result = exec_cmd("%s -assume-filename=%s" % (clang_format_exe, file_name), \
                    root_dir, file_contents)
  if result['err'] != '':
    print "clang-format error: %s" % result['err']
26 27 28 29 30 31 32
  if result['out'] != '':
    output = result['out']
    if sys.platform == 'win32':
      # Convert to Unix line endings.
      output = output.replace("\r", "")
    return output
  return None