From cda217e319ee9109ff4f7c48f6a7743c8ec2573b Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Senior=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 22 Mar 2018 09:23:08 +0900 Subject: [PATCH] Introduce format checker (#21) 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 --- .clang-format | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/command/format | 47 ++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 .clang-format create mode 100644 scripts/command/format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..84181d3 --- /dev/null +++ b/.clang-format @@ -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 index 0000000..fb5cd4d --- /dev/null +++ b/scripts/command/format @@ -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]" -- 2.7.4