Introduce format checker (#21)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 22 Mar 2018 00:23:08 +0000 (09:23 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 22 Mar 2018 00:23:08 +0000 (09:23 +0900)
This commit introduces C++ format checker to make it easy to maintain style
consistency. Users may invoke this format checker by running 'nncc format'.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
.clang-format [new file with mode: 0644]
scripts/command/format [new file with mode: 0644]

diff --git a/.clang-format b/.clang-format
new file mode 100644 (file)
index 0000000..84181d3
--- /dev/null
@@ -0,0 +1,89 @@
+---
+Language:        Cpp
+AccessModifierOffset: -2
+AlignAfterOpenBracket: Align
+AlignEscapedNewlinesLeft: true
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignOperands:   true
+AlignTrailingComments: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass:      true
+  AfterControlStatement: true
+  AfterEnum:       false
+  AfterFunction:   true
+  AfterNamespace:  false
+  AfterObjCDeclaration: false
+  AfterStruct:     true
+  AfterUnion:      true
+  BeforeCatch:     true
+  BeforeElse:      true
+  IndentBraces:    false
+BreakBeforeBraces: Allman
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializersBeforeComma: false
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: true
+ColumnLimit:     100
+CommentPragmas:  '^ IWYU pragma:'
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DerivePointerAlignment: false
+DisableFormat:   false
+ExperimentalAutoDetectBinPacking: false
+IncludeCategories:
+  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
+    Priority:        2
+  - Regex:           '^(<|"(gtest|isl|json)/)'
+    Priority:        3
+  - Regex:           '.*'
+    Priority:        1
+IndentCaseLabels: false
+IndentWidth:     2
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: true
+MacroBlockBegin: ''
+MacroBlockEnd:   ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBlockIndentWidth: 2
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakString: 1000
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 60
+PointerAlignment: Right
+ReflowComments:  true
+SortIncludes:    false
+SpaceAfterCStyleCast: false
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeParens: ControlStatements
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles:  false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard:        Cpp11
+TabWidth:        4
+UseTab:          Never
diff --git a/scripts/command/format b/scripts/command/format
new file mode 100644 (file)
index 0000000..fb5cd4d
--- /dev/null
@@ -0,0 +1,47 @@
+#!/bin/bash
+
+CLANG_FORMAT_CANDIDATES=()
+CLANG_FORMAT_CANDIDATES+=("clang-format")
+CLANG_FORMAT_CANDIDATES+=("clang-format-3.9")
+
+for CLANG_FORMAT_CANDIDATE in ${CLANG_FORMAT_CANDIDATES[@]}; do
+  if [[ -n $(which ${CLANG_FORMAT_CANDIDATE}) ]]; then
+    CLANG_FORMAT="${CLANG_FORMAT_CANDIDATE}"
+  fi
+done
+
+if [[ -z ${CLANG_FORMAT}  ]]; then
+  echo "[ERROR] clang-format is unavailable"
+  echo
+  echo "Please install clang-format before running format check"
+  exit 255
+fi
+
+if [[ -n $(git -C "${NNCC_PROJECT_PATH}" diff) ]]; then
+  echo "[ERROR] Commit all the changes before running format check"
+  exit 255
+fi
+
+DIRECTORIES_TO_BE_TESTED=()
+
+for DIRECTORY_TO_BE_TESTED in $(find "${NNCC_PROJECT_PATH}" -name '.FORMATCHECKED' -exec dirname {} \;); do
+  DIRECTORIES_TO_BE_TESTED+=("$DIRECTORY_TO_BE_TESTED")
+done
+
+CANDIDATE_FILES=$(find "${DIRECTORIES_TO_BE_TESTED[@]}" -iname '*.h' -o -iname '*.cpp')
+TRACKED_FILES=$(git ls-files $CANDIDATE_FILES)
+
+if [[ -z "$TRACKED_FILES" ]]; then
+  "${CLANG_FORMAT}" -i $TRACKED_FILES
+fi
+
+DIFF=$(git -C "${NNCC_PROJECT_PATH}" diff)
+
+if [[ -n "${DIFF}" ]]; then
+  echo "${DIFF}" > "${NNCC_PROJECT_PATH}/format.patch"
+
+  echo "[FAILED] Please find format.patch for details"
+  exit 255
+fi
+
+echo "[PASSED]"