Skip to content

Commit fed6bae

Browse files
committed
Update format-cpp
1 parent 9b585ec commit fed6bae

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

.github/workflows/format.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ jobs:
7474
uses: actions/checkout@v2
7575

7676
- name: Run formatter
77-
run: ./bin/format --check ./pennylane_lightning/src
77+
run: ./bin/format --check --cfversion 12 ./pennylane_lightning/src

Makefile

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ help:
1919
@echo " test-cpp to run the C++ test suite"
2020
@echo " test-python to run the Python test suite"
2121
@echo " coverage to generate a coverage report"
22-
@echo " format [check=1] to apply C++ formatter; use with 'check=1' to check instead of modify (requires clang-format)"
22+
@echo " format [check=1] to apply C++ and Python formatter; use with 'check=1' to check instead of modify (requires black and clang-format)"
23+
@echo " format [version=?] to apply C++ and Python formatter; use with 'version={version}' to check or modify with clang-format-{version} instead of clang-format"
2324
@echo " check-tidy to build PennyLane-Lightning with ENABLE_CLANG_TIDY=ON (requires clang-tidy & CMake)"
2425

2526
.PHONY: install
@@ -109,9 +110,9 @@ format: format-cpp format-python
109110

110111
format-cpp:
111112
ifdef check
112-
./bin/format --check ./pennylane_lightning/src
113+
./bin/format --check --cfversion $(if $(version:-=),$(version),0) ./pennylane_lightning/src
113114
else
114-
./bin/format ./pennylane_lightning/src
115+
./bin/format --cfversion $(if $(version:-=),$(version),0) ./pennylane_lightning/src
115116
endif
116117

117118
format-python:

bin/format

+41-35
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,28 @@ IGNORE_PATTERNS = ["external"]
2020

2121
DEFAULT_CLANG_FORMAT_VERSION=12
2222

23-
BASE_ARGS = [f"-style={json.dumps(CLANG_FMT_STYLE_CFG)}"]
24-
23+
BASE_ARGS = f"-style={json.dumps(CLANG_FMT_STYLE_CFG)}"
2524

2625
def parse_version(version_string):
2726
version_rgx = "version (\d+)"
2827

2928
m = re.search(version_rgx, version_string)
3029
return int(m.group(1))
3130

32-
def clang_format_command():
33-
command = f"clang-format-{DEFAULT_CLANG_FORMAT_VERSION}"
34-
35-
if shutil.which(command) is None:
36-
print(f"{command} is not found. Find default clang-format instead.")
37-
command = "clang-format"
38-
if shutil.which(command) is None:
39-
print(f"Default clang-format is not found.")
40-
raise FileNotFoundError("clang-format is not installed or is not in PATH.")
41-
else:
42-
p = subprocess.run([command, "--version"], stdout=subprocess.PIPE,
43-
stderr=subprocess.STDOUT, universal_newlines=True)
44-
version = parse_version(p.stdout)
45-
46-
if version < DEFAULT_CLANG_FORMAT_VERSION:
47-
print(f"Using clang-format version {version}. \
48-
As this is lower than the version used for the CI, \
49-
the CI may fail even after formatting.")
50-
51-
return command
52-
31+
def check_bin(command):
32+
try:
33+
p = subprocess.run([command, "--version"],
34+
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
35+
version = parse_version(p.stdout)
36+
37+
if version < DEFAULT_CLANG_FORMAT_VERSION:
38+
print(f"Using clang-format version {version}. \
39+
As this is lower than the version used for the CI, \
40+
the CI may fail even after formatting.")
41+
except FileNotFoundError as exc:
42+
raise FileNotFoundError(
43+
f"{command} is not installed or is not in PATH."
44+
) from exc
5345

5446
def parse_args():
5547
parser = argparse.ArgumentParser(
@@ -71,14 +63,21 @@ def parse_args():
7163
action="store_true",
7264
help="print detailed information about format violations",
7365
)
74-
66+
parser.add_argument(
67+
"-f",
68+
"--cfversion",
69+
type=int,
70+
default=0,
71+
action="store",
72+
help="set a version number for clang-format",
73+
)
7574
return parser.parse_args()
7675

77-
def fmt(command, paths) -> int:
78-
files = get_cpp_files(paths)
79-
cmd = (command, *BASE_ARGS, "-i", *files)
76+
def fmt(args, command) -> int:
77+
files = get_cpp_files(args.paths)
78+
cmd = (command, BASE_ARGS, "-i", *files)
8079

81-
sys.stderr.write(f"Formatting {len(files)} files in {paths}.\n")
80+
sys.stderr.write(f"Formatting {len(files)} files in {args.paths}.\n")
8281

8382
ret = subprocess.run(cmd, capture_output=True, universal_newlines=True)
8483
if ret.returncode != 0:
@@ -88,11 +87,11 @@ def fmt(command, paths) -> int:
8887
return 0
8988

9089

91-
def check(command, paths, is_verbose) -> int:
92-
cmd = (command, *BASE_ARGS, "--dry-run", "-Werror")
90+
def check(args, command) -> int:
91+
cmd = (command, BASE_ARGS, "--dry-run", "-Werror")
9392

9493
needs_reformatted_ct = 0
95-
files = get_cpp_files(paths)
94+
files = get_cpp_files(args.paths)
9695

9796
for src_file in files:
9897
ret = subprocess.run(
@@ -101,7 +100,7 @@ def check(command, paths, is_verbose) -> int:
101100

102101
if ret.returncode != 0:
103102
sys.stderr.write(f"Error: {src_file} would be reformatted.\n")
104-
if is_verbose:
103+
if args.verbose:
105104
sys.stderr.write(ret.stderr)
106105

107106
needs_reformatted_ct += 1
@@ -112,12 +111,19 @@ def check(command, paths, is_verbose) -> int:
112111
return needs_reformatted_ct
113112

114113
if __name__ == "__main__":
115-
command = clang_format_command()
116114
args = parse_args()
117115

116+
cf_version = args.cfversion
117+
cf_cmd = "clang-format"
118+
119+
if cf_version:
120+
cf_cmd += f"-{cf_version}"
121+
122+
check_bin(cf_cmd)
123+
118124
if args.check:
119-
ret = check(command, args.paths, args.verbose)
125+
ret = check(args, cf_cmd)
120126
else:
121-
ret = fmt(command, args.paths)
127+
ret = fmt(args, cf_cmd)
122128

123129
sys.exit(int(ret > 0))

0 commit comments

Comments
 (0)