tools: add `make format-cpp` to run clang-format on C++ diffs · nodejs/node@f0c871b · GitHub
Skip to content

Commit f0c871b

Browse files
joyeecheungtargos
authored andcommitted
tools: add make format-cpp to run clang-format on C++ diffs
This patch adds a `make format-cpp` shortcut to the Makefile that runs clang-format on the C++ diffs, and a `make format-cpp-build` to install clang-format from npm. To format staged changes: ``` $ make format-cpp ``` To format HEAD~1...HEAD (latest commit): ``` $ CLANG_FORMAT_START=`git rev-parse HEAD~1` make format-cpp ``` To format diff between master and current branch head (master...HEAD): ``` $ CLANG_FORMAT_START=master make format-cpp ``` Most of the .clang-format file comes from running ``` $ clang-format --dump-config --style=Google ``` with clang-format built with llvm/trunk 328768 (npm version 1.2.3) The clang-format version is fixed because different version of clang-format may format the files differently. PR-URL: #21997 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent d2ad9a2 commit f0c871b

4 files changed

Lines changed: 148 additions & 0 deletions

File tree

.clang-format

Lines changed: 111 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
!test/fixtures/**/.*
55
!tools/node_modules/**/.*
66
!tools/doc/node_modules/**/.*
7+
!.clang-format
78
!.editorconfig
89
!.eslintignore
910
!.eslintrc.js

Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,33 @@ LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \
11691169
# and the actual filename is generated so it won't match header guards
11701170
ADDON_DOC_LINT_FLAGS=-whitespace/ending_newline,-build/header_guard
11711171

1172+
format-cpp-build:
1173+
cd tools/clang-format && $(call available-node,$(run-npm-install))
1174+
1175+
format-cpp-clean:
1176+
$(RM) -r tools/clang-format/node_modules
1177+
1178+
CLANG_FORMAT_START ?= HEAD
1179+
.PHONY: format-cpp
1180+
# To format staged changes:
1181+
# $ make format-cpp
1182+
# To format HEAD~1...HEAD (latest commit):
1183+
# $ CLANG_FORMAT_START=`git rev-parse HEAD~1` make format-cpp
1184+
# To format diff between master and current branch head (master...HEAD):
1185+
# $ CLANG_FORMAT_START=master make format-cpp
1186+
format-cpp: ## Format C++ diff from $CLANG_FORMAT_START to current changes
1187+
ifneq ("","$(wildcard tools/clang-format/node_modules/)")
1188+
@echo "Formatting C++ diff from $(CLANG_FORMAT_START).."
1189+
@$(PYTHON) tools/clang-format/node_modules/.bin/git-clang-format \
1190+
--binary=tools/clang-format/node_modules/.bin/clang-format \
1191+
--style=file \
1192+
$(CLANG_FORMAT_START) -- \
1193+
$(LINT_CPP_FILES)
1194+
else
1195+
@echo "clang-format is not installed."
1196+
@echo "To install (requires internet access) run: $ make format-cpp-build"
1197+
endif
1198+
11721199
.PHONY: lint-cpp
11731200
# Lints the C++ code with cpplint.py and check-imports.py.
11741201
lint-cpp: tools/.cpplintstamp

tools/clang-format/package.json

Lines changed: 9 additions & 0 deletions

0 commit comments

Comments
 (0)