add clang-tidy to github actions (#27755)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/27755

This gives us nice annotations. See
https://github.com/suo/pytorch/pull/22/files for an approximation of
what it will look like (ignore the warnings on the lint.yml file).

I deleted the old azure pipelines one since making the code work for
both was annoying, and unlike flake8 this one does not affect master

Test Plan: Imported from OSS

Differential Revision: D17888974

Pulled By: suo

fbshipit-source-id: d8928a1451b6ef500dc1889284cab2845ecdeeea
This commit is contained in:
Michael Suo 2019-10-11 17:00:14 -07:00 committed by Facebook Github Bot
parent 3d2c90131a
commit 640b486339
5 changed files with 94 additions and 68 deletions

View File

@ -28,7 +28,6 @@ Checks: '
,performance-*
,-performance-noexcept-move-constructor
'
WarningsAsErrors: '*'
HeaderFilterRegex: 'torch/csrc/.*'
AnalyzeTemporaryDtors: false
CheckOptions:

View File

@ -46,3 +46,89 @@ jobs:
regex: '^(?<filename>.*?):(?<lineNumber>\d+):(?<columnNumber>\d+): (?<errorCode>\w\d+) (?<errorDesc>.*)'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
clang-tidy:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Setup Python
uses: actions/setup-python@v1
with:
python-version: 3.7.4
architecture: x64
- name: Checkout PyTorch
uses: actions/checkout@master
- name: Checkout PR tip
run: |
set -eux
git checkout ${GITHUB_HEAD_REF}
echo ::set-output name=commit_sha::$(git rev-parse ${GITHUB_HEAD_REF})
id: get_pr_tip
- name: Install dependencies
run: |
set -eux
# Install dependencies
pip install pyyaml
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-8 main"
sudo apt-get update
sudo apt-get install -y clang-tidy-8
sudo update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-8 1000
- name: Run clang-tidy
run: |
set -eux
BASE_BRANCH=master
if [[ $PR_TARGET ]]; then
git remote add upstream https://github.com/pytorch/pytorch
git fetch upstream "$PR_TARGET"
BASE_BRANCH="upstream/$PR_TARGET"
fi
if [[ ! -d build ]]; then
git submodule update --init --recursive
mkdir build
pushd build
# We really only need compile_commands.json, so no need to build!
time cmake ..
popd
# Generate ATen files.
time python aten/src/ATen/gen.py \
-s aten/src/ATen \
-d build/aten/src/ATen \
aten/src/ATen/Declarations.cwrap \
aten/src/THNN/generic/THNN.h \
aten/src/THCUNN/generic/THCUNN.h \
aten/src/ATen/nn.yaml \
aten/src/ATen/native/native_functions.yaml
# Generate PyTorch files.
time python tools/setup_helpers/generate_code.py \
--declarations-path build/aten/src/ATen/Declarations.yaml \
--nn-path aten/src
fi
# Run Clang-Tidy
# The negative filters below are to exclude files that include onnx_pb.h or
# caffe2_pb.h, otherwise we'd have to build protos as part of this CI job.
python tools/clang_tidy.py \
--paths torch/csrc/ \
--diff "$BASE_BRANCH" \
-g"-torch/csrc/jit/export.cpp" \
-g"-torch/csrc/jit/import.cpp" \
-g"-torch/csrc/jit/netdef_converter.cpp" \
"$@" > ${GITHUB_WORKSPACE}/clang-tidy-output.txt
cat ${GITHUB_WORKSPACE}/clang-tidy-output.txt
env:
PR_TARGET: ${{ github.base_ref }}
- name: Add annotations
uses: suo/add-annotations-github-action@master
with:
check_name: 'clang-tidy'
linter_output_path: 'clang-tidy-output.txt'
commit_sha: ${{ steps.get_pr_tip.outputs.commit_sha }}
regex: '^(?<filename>.*?):(?<lineNumber>\d+):(?<columnNumber>\d+): (?<errorDesc>.*?) (?<errorCode>\[.*\])'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@ -96,21 +96,3 @@ jobs:
versionSpec: "3.6"
- script: sudo apt-get install -y doxygen && pip install -r requirements.txt
- script: cd docs/cpp/source && ./check-doxygen.sh
- job: clang_tidy
displayName: "clang tidy"
pool:
vmImage: 'Ubuntu 16.04'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: "3.6"
- script: |
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-8 main"
sudo apt-get update
sudo apt-get install -y clang-tidy-8
sudo update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-8 1000
- script: git branch master origin/master
- script: pip install pyyaml
- script: tools/run-clang-tidy-in-ci.sh

View File

@ -18,6 +18,7 @@ import argparse
import collections
import fnmatch
import json
import os
import os.path
import re
import shlex
@ -172,7 +173,7 @@ def run_clang_tidy(options, line_filters, files):
with open(options.config_file) as config:
# Here we convert the YAML config file to a JSON blob.
command += ["-config", json.dumps(yaml.load(config))]
command += ["-config", json.dumps(yaml.load(config, Loader=yaml.FullLoader))]
command += options.extra_args
if line_filters:
@ -293,8 +294,13 @@ def main():
if options.diff:
line_filters = [get_changed_lines(options.diff, f) for f in files]
print(run_clang_tidy(options, line_filters, files), file=sys.stderr)
pwd = os.getcwd() + "/"
clang_tidy_output = run_clang_tidy(options, line_filters, files)
formatted_output = []
for line in clang_tidy_output.splitlines():
if line.startswith(pwd):
print(line[len(pwd):])
if __name__ == "__main__":
main()

View File

@ -1,47 +0,0 @@
#!/bin/bash
set -ex
BASE_BRANCH=master
if [[ $SYSTEM_PULLREQUEST_TARGETBRANCH ]]; then
git remote add upstream https://github.com/pytorch/pytorch
git fetch upstream "$SYSTEM_PULLREQUEST_TARGETBRANCH"
BASE_BRANCH="upstream/$SYSTEM_PULLREQUEST_TARGETBRANCH"
fi
if [[ ! -d build ]]; then
git submodule update --init --recursive
mkdir build
pushd build
# We really only need compile_commands.json, so no need to build!
time cmake ..
popd
# Generate ATen files.
time python aten/src/ATen/gen.py \
-s aten/src/ATen \
-d build/aten/src/ATen \
aten/src/ATen/Declarations.cwrap \
aten/src/THNN/generic/THNN.h \
aten/src/THCUNN/generic/THCUNN.h \
aten/src/ATen/nn.yaml \
aten/src/ATen/native/native_functions.yaml
# Generate PyTorch files.
time python tools/setup_helpers/generate_code.py \
--declarations-path build/aten/src/ATen/Declarations.yaml \
--nn-path aten/src
fi
# Run Clang-Tidy
# The negative filters below are to exclude files that include onnx_pb.h or
# caffe2_pb.h, otherwise we'd have to build protos as part of this CI job.
time python tools/clang_tidy.py \
--verbose \
--paths torch/csrc/ \
--diff "$BASE_BRANCH" \
-g"-torch/csrc/jit/export.cpp" \
-g"-torch/csrc/jit/import.cpp" \
-g"-torch/csrc/jit/netdef_converter.cpp" \
"$@"