diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000000..4aad29c328a --- /dev/null +++ b/.clang-format @@ -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: '^' + 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 diff --git a/.gitignore b/.gitignore index 1d901b3a699..255a4c5a4bc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ !test/fixtures/**/.* !tools/node_modules/**/.* !tools/doc/node_modules/**/.* +!.clang-format !.editorconfig !.eslintignore !.eslintrc.js diff --git a/Makefile b/Makefile index b228cbbf7a0..fc4a094656a 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/tools/clang-format/package.json b/tools/clang-format/package.json new file mode 100644 index 00000000000..8432296ed6f --- /dev/null +++ b/tools/clang-format/package.json @@ -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" + } +}