зеркало из https://github.com/dotnet/llilc.git
Merge pull request #268 from adiaaida/ccFormatFull
Add formatall option to ccformat and prepare script for running in the lab
This commit is contained in:
Коммит
629c139489
|
@ -5,6 +5,9 @@ import argparse
|
|||
import os
|
||||
import subprocess
|
||||
import platform
|
||||
import StringIO
|
||||
import string
|
||||
import difflib
|
||||
|
||||
def expandPath(path):
|
||||
return os.path.abspath(os.path.expanduser(path))
|
||||
|
@ -110,9 +113,65 @@ def runTidy(args):
|
|||
|
||||
def runFormat(args):
|
||||
formatFix = "-i" if args.fix else ""
|
||||
subprocess.call(" ".join(["git", "diff", args.base, "-U0", "2>" + os.devnull,
|
||||
"|", args.clang_format_diff, "-p1", formatFix]), shell=True)
|
||||
return 0
|
||||
returncode = 0
|
||||
llilcSrc = expandPath(args.llilc_source)
|
||||
|
||||
if args.formatall:
|
||||
llilcSrc = expandPath(args.llilc_source)
|
||||
for dirname,subdir,files in os.walk(llilcSrc):
|
||||
if ".git" in dirname \
|
||||
or dirname == os.path.join(llilcSrc, "include", "clr"):
|
||||
continue
|
||||
for filename in files:
|
||||
if filename.endswith(".c") or filename.endswith(".cpp") or \
|
||||
filename.endswith(".h"):
|
||||
filepath=os.path.join(dirname, filename)
|
||||
proc = subprocess.Popen(" ".join([args.clang_format, filepath,
|
||||
formatFix, "2>" + os.devnull]), shell=True, stdout=subprocess.PIPE)
|
||||
|
||||
output,error = proc.communicate()
|
||||
|
||||
# Compute the diff if not fixing
|
||||
if not args.fix:
|
||||
with open(filepath) as f:
|
||||
code = f.read().splitlines()
|
||||
formatted_code = StringIO.StringIO(output).read().splitlines()
|
||||
diff = difflib.unified_diff(code, formatted_code,
|
||||
filepath, filepath,
|
||||
'(before formatting)', '(after formatting)')
|
||||
diff_string = string.join(diff, '\n')
|
||||
if len(diff_string) > 0:
|
||||
# If there was a diff, print out the file name.
|
||||
print(filepath)
|
||||
if args.print_diffs:
|
||||
sys.stdout.write(diff_string)
|
||||
returncode = -1
|
||||
else:
|
||||
noindex = ""
|
||||
|
||||
# base and no-index are mutually exclusive
|
||||
base = "" if args.noindex else args.base
|
||||
|
||||
if args.noindex:
|
||||
noindex = "--no-index"
|
||||
if args.left == "" or args.right == "":
|
||||
print("User must specify paths to repositories to be diffed with --no-index")
|
||||
print("usage: ccformat.py --no-index --left <path to left> --right <path to right>")
|
||||
return -2
|
||||
|
||||
proc = subprocess.Popen(" ".join(["git", "diff", base, "-U0", noindex,
|
||||
args.left, args.right, "2>" + os.devnull, "|", args.clang_format_diff,
|
||||
"-p1", formatFix]), shell=True, stdout=subprocess.PIPE)
|
||||
|
||||
output,error = proc.communicate()
|
||||
if output != "":
|
||||
if args.print_diffs:
|
||||
sys.stdout.write(output)
|
||||
returncode = -1
|
||||
|
||||
if returncode == -1:
|
||||
print "There were formatting errors. Rerun with --fix"
|
||||
return returncode
|
||||
|
||||
def main(argv):
|
||||
llvmBuild = os.environ.get("LLVMBUILD")
|
||||
|
@ -125,6 +184,8 @@ def main(argv):
|
|||
parser.add_argument("--clang-format-diff", metavar="PATH",
|
||||
default="clang-format-diff.py",
|
||||
help="path to clang-format-diff tool")
|
||||
parser.add_argument("--clang-format", metavar="PATH",
|
||||
default="clang-format", help="path to clang-format binary")
|
||||
parser.add_argument("--compile-commands", metavar="PATH",
|
||||
help="path to a directory containing a JSON compile commands " \
|
||||
"database used to determine clang-tidy options")
|
||||
|
@ -155,8 +216,19 @@ def main(argv):
|
|||
parser.add_argument("--checks", default="llvm*,misc*,microsoft*,"\
|
||||
"-llvm-header-guard,-llvm-include-order",
|
||||
help="clang-tidy checks to run")
|
||||
parser.add_argument("--base", metavar="BRANCH", default="origin",
|
||||
group = parser.add_mutually_exclusive_group()
|
||||
group.add_argument("--base", metavar="BRANCH", default="origin",
|
||||
help="Base for obtaining diffs")
|
||||
group.add_argument("--noindex", action="store_true", default=False,
|
||||
help="Run git diff with --no-index to compare two paths")
|
||||
parser.add_argument("--formatall", action="store_true", default=False,
|
||||
help="Run clang-format on all files")
|
||||
parser.add_argument("--left", default="",
|
||||
help="Path to compare against, used with --noindex")
|
||||
parser.add_argument("--right", default="",
|
||||
help="Path to be compared, used with --noindex")
|
||||
parser.add_argument("--print-diffs", action="store_true", default=False,
|
||||
help="Print formatting diffs if there are diffs")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
returncode=0
|
||||
|
@ -166,9 +238,10 @@ def main(argv):
|
|||
return returncode
|
||||
|
||||
if not args.noformat:
|
||||
runFormat(args)
|
||||
returncode = runFormat(args)
|
||||
|
||||
return returncode
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
returncode = main(sys.argv[1:])
|
||||
sys.exit(returncode)
|
||||
|
|
Загрузка…
Ссылка в новой задаче