From a8de2fefb61d8e1d1de091a322488ef567d85b8e Mon Sep 17 00:00:00 2001 From: Jian Sun Date: Sat, 20 Jul 2024 00:07:05 -0600 Subject: [PATCH] add clang format check --- .clang-format | 41 +++++++++++++++++++++ .github/workflows/clang_format.yml | 58 ++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 .clang-format create mode 100644 .github/workflows/clang_format.yml diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..aa73990 --- /dev/null +++ b/.clang-format @@ -0,0 +1,41 @@ + +--- +BasedOnStyle: Google +AlignAfterOpenBracket: 'AlwaysBreak' +AllowAllConstructorInitializersOnNextLine: 'false' +AllowAllParametersOfDeclarationOnNextLine: 'false' +AlignConsecutiveMacros: 'true' +AllowShortCaseLabelsOnASingleLine: 'true' +AllowShortFunctionsOnASingleLine: 'None' +AllowShortIfStatementsOnASingleLine: 'Never' +AllowShortLoopsOnASingleLine: 'false' +BreakBeforeBraces: Allman +BinPackArguments: 'false' +BinPackParameters: 'false' +Cpp11BracedListStyle: 'false' +ColumnLimit: 125 +IndentWidth: 2 +IndentPPDirectives: BeforeHash +NamespaceIndentation: All +PackConstructorInitializers: 'Never' +SpaceAfterTemplateKeyword: 'false' +SpaceBeforeCtorInitializerColon: 'true' +SpaceBeforeInheritanceColon: 'true' +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: 'true' +SpaceInEmptyBlock: true +Standard: 'Latest' +IncludeBlocks: Regroup +IncludeCategories: + # CUDA, cuBLAS, LLVM, OpenMP headers + - Regex: '<(cuda|cublas|llvm|omp)[[:alnum:].(-|_)*]+>' + Priority: 2 + # External libraries headers + - Regex: '<[[:alnum:].(-|_)*]+\/' + Priority: 3 + # System headers + - Regex: '<[[:alnum:].(-|_)*]+>' + Priority: 4 + # Main, local, private headers + - Regex: '".*' + Priority: 1 diff --git a/.github/workflows/clang_format.yml b/.github/workflows/clang_format.yml new file mode 100644 index 0000000..d61003b --- /dev/null +++ b/.github/workflows/clang_format.yml @@ -0,0 +1,58 @@ +name: Clang-Format + +on: + push: + branches: + - main + +jobs: + format: + name: Run Clang-Format + runs-on: ubuntu-latest + + steps: + - name: Install Clang-Format + run: sudo apt-get update && sudo apt-get install clang-format && clang-format --version + + - name: Check out code, run clang format, push changes + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Format code + run: | + find include -type f \( -name '*.hpp' -o -name '*.h' -o -name '*.cuh' -o -name '*.inl' \) -exec clang-format -i --style=file --verbose {} + + find src -type f \( -name '*.cu' -o -name '*.hpp' -o -name '*.h' -o -name '*.cpp' \) -exec clang-format -i --style=file --verbose {} + + find test -type f \( -name '*.hpp' -o -name '*.h' -o -name '*.cpp' -o -name '*.cuh' -o -name '*.cu' \) ! -path 'test/tutorial/*' -exec clang-format -i --style=file --verbose {} + + + - name: Check for changes + id: check-changes + run: | + git diff --exit-code + continue-on-error: true + + - name: Commit and push changes + # a failue of this step means changes were detected + if: steps.check-changes.outcome != 'success' + run: | + git config --global user.name "GitHub Actions" + git config --global user.email "actions@github.com" + git commit -am "Auto-format code using Clang-Format" || echo "No changes to commit" + + - name: Push changes to main-formatting branch + # a failue of this step means changes were detected + if: steps.check-changes.outcome != 'success' + run: | + git push origin HEAD:main-formatting + + - name: Create Pull Request + # a failue of this step means changes were detected + if: steps.check-changes.outcome != 'success' + uses: peter-evans/create-pull-request@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: "Auto-format code using Clang-Format" + title: "Auto-format code changes" + body: "This is an automated pull request to apply code formatting using Clang-Format." + branch: "main-formatting" \ No newline at end of file