Skip to content

Commit 87636dc

Browse files
authored
Add clang-format-diff hook (#2057)
This adds a commit hook to Travis CI that errors out if incoming PRs' diffs are not clang-formatted. Turns out clang-format is also capable of formatting JavaScript, but we haven't agreed on a style for JS yet, this PR disables JavaScript formatting for now. This also adds clang-format exempt header/footer to a generated source file.
1 parent 34dd4c7 commit 87636dc

5 files changed

Lines changed: 28 additions & 0 deletions

File tree

.clang-format

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
---
2+
Language: Cpp
13
BasedOnStyle: LLVM
24
PointerAlignment: Left
35
IndentCaseLabels: true
@@ -6,3 +8,7 @@ ConstructorInitializerIndentWidth: 2
68
SpaceAfterTemplateKeyword: false
79
BinPackArguments: false
810
BinPackParameters: false
11+
---
12+
Language: JavaScript
13+
DisableFormat: true
14+
---

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ jobs:
5959
script:
6060
- set -o errexit
6161
- flake8
62+
- ./clang-format-diff.sh
6263
# ensure generated parser is up to date
6364
- ./scripts/gen-s-parser.py | diff src/gen-s-parser.inc -
6465
- BUILD_SUBDIR=${BUILD_SUBDIR:-.}

clang-format-diff.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
MERGE_BASE=$(git merge-base master HEAD)
3+
FORMAT_MSG=$(git clang-format $MERGE_BASE -q --diff -- src/)
4+
if [ -n "$FORMAT_MSG" -a "$FORMAT_MSG" != "no modified files to format" ]
5+
then
6+
echo "Run git clang-format before committing!"
7+
echo
8+
# Run git clang-format once again to show the error
9+
git clang-format $MERGE_BASE -q --diff -- src/
10+
exit 1
11+
fi

scripts/gen-s-parser.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@ def emit(node, idx=0):
533533

534534
def print_header():
535535
print("// DO NOT EDIT! This file generated by scripts/gen-s-parser.py\n")
536+
print("// clang-format off\n")
537+
538+
539+
def print_footer():
540+
print("\n// clang-format on")
536541

537542

538543
def generate_with_guard(generator, guard):
@@ -549,6 +554,7 @@ def main():
549554
sys.exit(1)
550555
print_header()
551556
generate_with_guard(instruction_parser, "INSTRUCTION_PARSER")
557+
print_footer()
552558

553559

554560
if __name__ == "__main__":

src/gen-s-parser.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// DO NOT EDIT! This file generated by scripts/gen-s-parser.py
22

3+
// clang-format off
4+
35
#ifdef INSTRUCTION_PARSER
46
#undef INSTRUCTION_PARSER
57
char op[27] = {'\0'};
@@ -2260,3 +2262,5 @@ switch (op[0]) {
22602262
parse_error:
22612263
throw ParseException(std::string(op));
22622264
#endif // INSTRUCTION_PARSER
2265+
2266+
// clang-format on

0 commit comments

Comments
 (0)