Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/.github/workflows/check-code-style.yml

raw
1 # This workflow is intended to check if PR conforms with coding standards used
2 # in the project.
3 #
4 # Documentation for GitHub Actions:
5 # [workflow-syntax]: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions
6 # [context-and-expression-syntax]: https://docs.github.com/en/free-pro-team@latest/actions/reference/context-and-expression-syntax-for-github-actions
7 # [workflow-commands]: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions
8
9 name: Check code style
10
11 on:
12 pull_request:
13 branches:
14 - main
15 - llvm_release_*
16 paths-ignore: # no need to check formatting for:
17 - 'docs/**' # documentation
18 - 'test/**' # tests
19 - '**.md' # README
20 - '**.txt' # CMakeLists.txt
21 - '**/check-**-build.yml' # Other workflows
22
23 env:
24 # We need compile command database in order to perform clang-tidy check. So,
25 # in order to perform configure step we need to setup llvm-dev package. This
26 # env variable used to specify desired version of it
27 LLVM_VERSION: 16
28
29 jobs:
30 clang-format-and-tidy:
31 name: clang-format & clang-tidy
32 runs-on: ubuntu-22.04
33 steps:
34 - name: Checkout sources
35 uses: actions/checkout@v2
36 with:
37 # In order to gather diff from PR we need to fetch not only the latest
38 # commit. Depth of 2 is enough, because GitHub Actions supply us with
39 # merge commit as {{ github.sha }}, i.e. the second commit is a merge
40 # base between target branch and PR
41 fetch-depth: 2
42
43 - name: Gather list of changes
44 id: gather-list-of-changes
45 run: |
46 git diff -U0 --no-color ${{ github.sha }}^ -- include lib \
47 ':(exclude)include/LLVMSPIRVExtensions.inc' \
48 ':(exclude)lib/SPIRV/libSPIRV/SPIRVErrorEnum.h' \
49 ':(exclude)lib/SPIRV/libSPIRV/SPIRVOpCodeEnum.h' \
50 ':(exclude)lib/SPIRV/libSPIRV/SPIRVOpCodeEnumInternal.h' \
51 > diff-to-inspect.txt
52 if [ -s diff-to-inspect.txt ]; then
53 # Here we set an output of our step, which is used later to either
54 # perform or skip further steps, i.e. there is no sense to install
55 # clang-format if PR hasn't changed .cpp files at all
56 # See [workflow-commands] for reference
57 echo '::set-output name=HAS_CHANGES::true'
58 fi
59
60 - name: Install dependencies
61 if: ${{ steps.gather-list-of-changes.outputs.HAS_CHANGES }}
62 run: |
63 # clang-tidy requires compile command database in order to be properly
64 # launched, so, we need to setup llvm package to perform cmake
65 # configuration step to generate that database
66 curl -L "https://apt.llvm.org/llvm-snapshot.gpg.key" | sudo apt-key add -
67 echo "deb https://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main" | sudo tee -a /etc/apt/sources.list
68 sudo apt-get update
69 sudo apt-get install -yqq \
70 clang-format-${{ env.LLVM_VERSION }} clang-tidy-${{ env.LLVM_VERSION }} \
71 llvm-${{ env.LLVM_VERSION }}-dev \
72 mlir-${{ env.LLVM_VERSION }}-tools \
73
74 - name: Generate compile command database
75 if: ${{ steps.gather-list-of-changes.outputs.HAS_CHANGES }}
76 run: |
77 mkdir build && cd build
78 cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" ${{ github.workspace }}
79
80 - name: Run clang-format
81 if: ${{ steps.gather-list-of-changes.outputs.HAS_CHANGES }}
82 id: run-clang-format
83 run: |
84 cat diff-to-inspect.txt | /usr/share/clang/clang-format-${{ env.LLVM_VERSION }}/clang-format-diff.py -p1 > clang-format.patch
85 if [ -s clang-format.patch ]; then
86 echo "clang-format found incorrectly formatted code:"
87 cat clang-format.patch;
88 exit 1;
89 else
90 rm clang-format.patch # to avoid uploading empty file
91 fi
92
93 - name: Run clang-tidy
94 # By some reason, GitHub Actions automatically include "success()"
95 # expression into an "if" statement if it doesn't contain any of job
96 # status check functions. This is why this and following steps has
97 # "always()" and "failure()" in "if" conditions.
98 # See "Job status check functions" in [context-and-expression-syntax]
99 if: ${{ always() && steps.gather-list-of-changes.outputs.HAS_CHANGES }}
100 id: run-clang-tidy
101 run: |
102 cat diff-to-inspect.txt | /usr/lib/llvm-${{ env.LLVM_VERSION }}/share/clang/clang-tidy-diff.py \
103 -p1 -clang-tidy-binary clang-tidy-${{ env.LLVM_VERSION }} -quiet \
104 -path ${{ github.workspace}}/build > clang-tidy.log 2>/dev/null
105 # By some reason, clang-tidy log contains tons of extra empty lines,
106 # that confuse the check below
107 sed -i '/^$/d' clang-tidy.log
108 if [ -s clang-tidy.log ]; then
109 if ! grep -q "No relevant changes found." clang-tidy.log; then
110 # Emit annotations
111 while read -r line; do
112 type="error"
113 if [[ $line == *"warning:"* ]]; then
114 type="warning"
115 elif [[ $line == *"error:"* ]]; then
116 type="error"
117 else
118 continue
119 fi
120
121 absolute_path=$(echo $line | grep -Po "^[\w\d-./]+(?=:)")
122 relative_path=${absolute_path##"${{ github.workspace }}"}
123
124 line_number=$(echo $line | grep -Po "(?<=:)\d+(?=:\d)")
125
126 message=$(echo $line | grep -Po "(?<=${type}: ).*$")
127
128 # see [workflow-commands] for documentation
129 echo "::${type} file=${relative_path},line=${line_number}::${message}"
130 done < clang-tidy.log
131
132 echo "clang-tidy found incorrectly written code:"
133 cat clang-tidy.log
134 exit 1
135 else
136 rm clang-tidy.log # to avoid uploading empty file
137 fi
138 fi
139
140 - name: Upload patch with clang-format fixes
141 uses: actions/upload-artifact@v4
142 if: ${{ failure() && steps.run-clang-format.outcome == 'failure' }}
143 with:
144 name: clang-format.patch
145 path: clang-format.patch
146 if-no-files-found: ignore
147
148 - name: Upload clang-tidy log
149 uses: actions/upload-artifact@v4
150 if: ${{ failure() && steps.run-clang-tidy.outcome == 'failure' }}
151 with:
152 name: clang-tidy.log
153 path: clang-tidy.log
154 if-no-files-found: ignore