Skip to content

Commit

Permalink
add clang-format action
Browse files Browse the repository at this point in the history
Signed-off-by: peter5232 <[email protected]>
  • Loading branch information
peter5232 committed Sep 11, 2023
1 parent 6f5c85b commit 482ab36
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 143 deletions.
149 changes: 7 additions & 142 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,147 +1,12 @@
---
Language: Cpp
Language: Cpp
BasedOnStyle: Google
AccessModifierOffset: -3
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: true
AlignConsecutiveAssignments: false
AlignConsecutiveBitFields: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
- Regex: '.*'
Priority: 1
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Cpp11
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 4
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
---
Language: Proto
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 120
...
30 changes: 30 additions & 0 deletions .github/workflows/clang-format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Code Formatter

# run on pull requests to develop
on:
pull_request

jobs:
format:
name: Clang Formatter
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
# fetch everything to be able to compare with any ref
fetch-depth: 0

- name: Check
env:
LANG: "C.UTF-8"
LC_ALL: "C.UTF-8"
LANGUAGE: "C.UTF-8"
run: |
sudo apt-get install -y clang-format-14 python3 git
diff_output_file=$(mktemp)
git --no-pager diff -U0 --diff-filter=ACMRT ${{github.event.pull_request.base.sha}}...${{ github.event.pull_request.head.sha }} | clang-format-diff-14 -p1 2>&1 | tee $diff_output_file
if [ -s $diff_output_file ]; then
exit 1
else
exit 0
fi
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tag?= "curvebs:unknown"
case?= "*"
os?= "debian11"
ci?=0

commit_id="HEAD^"
define help_msg
## list
Usage:
Expand Down Expand Up @@ -88,3 +88,5 @@ test:

docker:
@bash util/docker.sh --os=$(os) --ci=$(ci)
format:
@bash util/format.sh $(commit_id)
15 changes: 15 additions & 0 deletions developers_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ Curve Ci use ```cpplint``` check what your changed.
$ cpplint --filter=-build/c++11 --quiet --recursive your_path
```

- use clang-format-diff to check submitted code
```
$ make format
# Or
$ make format commit_id=$(commit_id) # commit_id is a sha of a commit, default HEAD^
```
> Clang-format understands also special comments that switch formatting in a delimited range. The code between a comment `// clang-format off` or `/* clang-format off */` up to a comment `// clang-format on` or `/* clang-format on */` will not be formatted.
> ```cpp
> int formatted_code;
> // clang-format off
> void unformatted_code ;
> // clang-format on
> void formatted_code_again;
> ```

For PR we have the following requirements:

- The Curve coding standard strictly follows the [Google C++ Open Source Project Coding Guide](https://google.github.io/styleguide/cppguide.html), but we use 4 spaces to indent, Clang-format will more helpful for you. Of course, CI will check what your changed.
Expand Down
14 changes: 14 additions & 0 deletions developers_guide_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ Curve CI 使用```cpplint```检查更改的代码,
```bash
$ cpplint --filter=-build/c++11 --quiet --recursive your_path
```
- 本地执行clang-format-diff对提交部分代码进行检查:
```
$ make format
# or
$ make format commit_id=$(commit_id) # commit_id为某一个提交的sha,默认commit_id为HEAD^
```
> 如果需要对部分代码禁用格式检查,可以在开始处添加 `// clang-format off``/* clang-format off */` 注释,结尾处添加 `// clang-format on``/* clang-format on */` 注释
> ```cpp
> int formatted_code;
> // clang-format off
> void unformatted_code ;
> // clang-format on
> void formatted_code_again;
> ```

对于 PR 我们有如下要求:

Expand Down
2 changes: 2 additions & 0 deletions util/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
git --no-pager diff -U0 --diff-filter=ACMRT $1 HEAD | clang-format-diff-14 -p1 -i -style file

0 comments on commit 482ab36

Please sign in to comment.