Skip to content

Commit

Permalink
tools: add make format-cpp to run clang-format on C++ diffs
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
joyeecheung authored and targos committed Aug 4, 2018
1 parent d2ad9a2 commit f0c871b
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
111 changes: 111 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
- Regex: '^<.*\.h>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 8
UseTab: Never
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
!test/fixtures/**/.*
!tools/node_modules/**/.*
!tools/doc/node_modules/**/.*
!.clang-format
!.editorconfig
!.eslintignore
!.eslintrc.js
Expand Down
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,33 @@ LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \
# and the actual filename is generated so it won't match header guards
ADDON_DOC_LINT_FLAGS=-whitespace/ending_newline,-build/header_guard

format-cpp-build:
cd tools/clang-format && $(call available-node,$(run-npm-install))

format-cpp-clean:
$(RM) -r tools/clang-format/node_modules

CLANG_FORMAT_START ?= HEAD
.PHONY: format-cpp
# 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
format-cpp: ## Format C++ diff from $CLANG_FORMAT_START to current changes
ifneq ("","$(wildcard tools/clang-format/node_modules/)")
@echo "Formatting C++ diff from $(CLANG_FORMAT_START).."
@$(PYTHON) tools/clang-format/node_modules/.bin/git-clang-format \
--binary=tools/clang-format/node_modules/.bin/clang-format \
--style=file \
$(CLANG_FORMAT_START) -- \
$(LINT_CPP_FILES)
else
@echo "clang-format is not installed."
@echo "To install (requires internet access) run: $ make format-cpp-build"
endif

.PHONY: lint-cpp
# Lints the C++ code with cpplint.py and check-imports.py.
lint-cpp: tools/.cpplintstamp
Expand Down
9 changes: 9 additions & 0 deletions tools/clang-format/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "node-core-clang-format",
"version": "1.0.0",
"description": "Formatting C++ files for Node.js core",
"license": "MIT",
"dependencies": {
"clang-format": "1.2.3"
}
}

0 comments on commit f0c871b

Please sign in to comment.