Skip to content

Commit fcf0d09

Browse files
authored
Add a project .clang-format config file (#1053)
Scripts and workflows that run `clang-format` currently each add the option `--style=google` to the invocation of `clang-format`. To follow best practices, this PR adds a `.clang-format` configuration file at the top level of the project to set the style, and removes the flag from where it was added on the command line explicitly. The use of a config file has the following benefits: - It lets tools like IDEs discover the setting automatically. - It reduces the chances that developers will forget to add the flag when they run `clang-format` directly on the command line for quick checks, or write new scripts that use `clang-format`. - It communicates intentions more clearly to new contributors. Removing the flag from the scripts is not strictly necessary (everything will work the same if there is both a config file and the flag is passed to `clang-format`). However, if a config file is used, it's better for maintainability and consistency to also remove the flag from individual invocations.
1 parent 8fb9489 commit fcf0d09

3 files changed

Lines changed: 8 additions & 5 deletions

File tree

.clang-format

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Run manually to reformat a file:
2+
# clang-format -i --style=file <file>
3+
BasedOnStyle: Google

scripts/format_all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ python3 -m yapf --style=google --in-place --recursive --parallel \
1919
echo -e "Done! \nDoing notebook formatting..."
2020
python3 ./scripts/format_ipynb.py
2121
echo -e "Done! \nDoing C++ formatting..."
22-
find tensorflow_quantum/ -iname *.h -o -iname *.cc | xargs clang-format -i -style=google
22+
find tensorflow_quantum/ -iname *.h -o -iname *.cc | xargs clang-format -i
2323
echo "Done!"
2424
exit 0;

scripts/format_check.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/bin/bash
22
# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved.
3-
#
3+
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
66
# You may obtain a copy of the License at
7-
#
7+
#
88
# http://www.apache.org/licenses/LICENSE-2.0
9-
#
9+
#
1010
# Unless required by applicable law or agreed to in writing, software
1111
# distributed under the License is distributed on an "AS IS" BASIS,
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -100,7 +100,7 @@ if [ $? -ne 0 ]; then
100100
fi
101101

102102
echo "Checking C++ formatting...";
103-
formatting_outputs=$(find tensorflow_quantum/ -iname *.h -o -iname *.cc | xargs clang-format -style=google -output-replacements-xml);
103+
formatting_outputs=$(find tensorflow_quantum/ -iname *.h -o -iname *.cc | xargs clang-format -output-replacements-xml);
104104
CFORMATCHECK=0
105105
while read -r formatting_outputs; do
106106
if [ "$formatting_outputs" != "<?xml version='1.0'?>" ] && [ "$formatting_outputs" != "<replacements xml:space='preserve' incomplete_format='false'>" ] && [ "$formatting_outputs" != "</replacements>" ] && [ "$formatting_outputs" != "<replacement offset='4290' length='9'> </replacement>" ]; then

0 commit comments

Comments
 (0)