diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fbc818f01..1294802a6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -41,13 +41,14 @@ jobs: if: "github.ref != 'refs/heads/master'" pretty: - runs-on: ubuntu-18.04 + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 - name: Bootstrap run: | sudo rm /etc/apt/sources.list.d/* && sudo apt-get update - sudo apt-get --no-install-recommends install -y clang-format-10 shellcheck + sudo apt-get --no-install-recommends install -y clang-format-10 clang-tidy-10 shellcheck + sudo ln -s /usr/bin/run-clang-tidy-10.py /usr/local/bin/run-clang-tidy.py python3 -m pip install yapf==0.29.0 sudo snap install shfmt - name: Check @@ -99,7 +100,6 @@ jobs: - name: Bootstrap run: | sudo rm /etc/apt/sources.list.d/* && sudo apt-get update - sudo apt-get remove libllvm10 sudo apt-get --no-install-recommends install -y clang-tools-10 - name: Run run: | diff --git a/script/bootstrap b/script/bootstrap index 3a4bdb55f..88791a496 100755 --- a/script/bootstrap +++ b/script/bootstrap @@ -58,8 +58,10 @@ install_packages_apt() echo 'Installing pretty tools useful for code contributions...' - # add clang-format for pretty - sudo apt-get --no-install-recommends install -y clang-format-10 + # add clang-format and clang-tidy for pretty + sudo apt-get --no-install-recommends install -y clang-format-10 clang-tidy-10 + sudo ln -sf /usr/bin/clang-tidy-10 /usr/bin/clang-tidy + sudo ln -sf /usr/bin/clang-apply-replacements-10 /usr/bin/clang-apply-replacements # add yapf for pretty python3 -m pip install yapf==0.29.0 || echo 'WARNING: could not install yapf, which is useful if you plan to contribute python code to the OpenThread project.' diff --git a/script/make-pretty b/script/make-pretty index 6df008405..3fad69180 100755 --- a/script/make-pretty +++ b/script/make-pretty @@ -37,6 +37,8 @@ # Format c/c++ only: # # script/make-pretty clang +# script/make-pretty clang-format +# script/make-pretty clang-tidy # # Format markdown only: # @@ -53,6 +55,8 @@ # Check only: # # script/make-pretty check clang +# script/make-pretty check clang-format +# script/make-pretty check clang-tidy # script/make-pretty check markdown # script/make-pretty check python # script/make-pretty check shell @@ -67,31 +71,114 @@ readonly OT_CLANG_SOURCES=('*.c' '*.cc' '*.cpp' '*.h' '*.hpp') readonly OT_MARKDOWN_SOURCES=('*.md') readonly OT_PYTHON_SOURCES=('*.py') +readonly OT_CLANG_TIDY_FIX_DIRS=('examples' 'include' 'src' 'tests') +readonly OT_CLANG_TIDY_BUILD_OPTS=( + '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON' + '-DOT_APP_RCP=OFF' + '-DOT_MTD=OFF' + '-DOT_RCP=OFF' + '-DOT_PLATFORM=simulation' + '-DOT_BACKBONE_ROUTER=ON' + '-DOT_BORDER_AGENT=ON' + '-DOT_BORDER_ROUTER=ON' + '-DOT_CHANNEL_MANAGER=ON' + '-DOT_CHANNEL_MONITOR=ON' + '-DOT_CHILD_SUPERVISION=ON' + '-DOT_COAP=ON' + '-DOT_COAP_OBSERVE=ON' + '-DOT_COAPS=ON' + '-DOT_COMMISSIONER=ON' + '-DOT_CSL_RECEIVER=ON' + '-DOT_DHCP6_CLIENT=ON' + '-DOT_DHCP6_SERVER=ON' + '-DOT_DIAGNOSTIC=ON' + '-DOT_DNS_CLIENT=ON' + '-DOT_DUA=ON' + '-DOT_MLR=ON' + '-DOT_ECDSA=ON' + '-DOT_IP6_FRAGM=ON' + '-DOT_JAM_DETECTION=ON' + '-DOT_JOINER=ON' + '-DOT_LEGACY=ON' + '-DOT_LINK_RAW=ON' + '-DOT_LINK_METRICS=ON' + '-DOT_MAC_FILTER=ON' + '-DOT_MTD_NETDIAG=ON' + '-DOT_REFERENCE_DEVICE=ON' + '-DOT_SERVICE=ON' + '-DOT_SLAAC=ON' + '-DOT_SNTP_CLIENT=ON' + '-DOT_THREAD_VERSION=1.2' + '-DOT_COVERAGE=ON' + '-DOT_LOG_LEVEL_DYNAMIC=ON' + '-DOT_COMPILE_WARNING_AS_ERROR=ON' +) + +readonly OT_CLANG_TIDY_CHECKS="\ +-*,\ +readability-make-member-function-const,\ +" + +#performance-for-range-copy\ + do_clang_format() { - echo -e '=====================' - echo -e ' format c/c++' - echo -e '=====================' + echo -e '========================================' + echo -e ' format c/c++ (clang-format)' + echo -e '========================================' git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format -style=file -i -verbose } -do_clang_check() +do_clang_format_check() { - echo -e '=====================' - echo -e ' check c/c++' - echo -e '=====================' + echo -e '========================================' + echo -e ' check c/c++ (clang-format)' + echo -e '========================================' git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format-check } +do_clang_tidy_fix() +{ + echo -e '========================================' + echo -e ' format c/c++ (clang-tidy)' + echo -e '========================================' + + (mkdir -p ./build/cmake-tidy \ + && cd ./build/cmake-tidy \ + && THREAD_VERSION=1.2 cmake "${OT_CLANG_TIDY_BUILD_OPTS[@]}" ../.. \ + && run-clang-tidy.py -header-filter='.*' -checks="${OT_CLANG_TIDY_CHECKS}" -j"$OT_BUILD_JOBS" "${OT_CLANG_TIDY_FIX_DIRS[@]}" -fix) +} + +do_clang_tidy_check() +{ + echo -e '========================================' + echo -e ' check c/c++ (clang-tidy)' + echo -e '========================================' + + ( + mkdir -p ./build/cmake-tidy \ + && cd ./build/cmake-tidy \ + && THREAD_VERSION=1.2 cmake "${OT_CLANG_TIDY_BUILD_OPTS[@]}" ../.. \ + && run-clang-tidy.py -header-filter='.*' -checks="${OT_CLANG_TIDY_CHECKS}" -j"$OT_BUILD_JOBS" "${OT_CLANG_TIDY_FIX_DIRS[@]}" \ + | grep -v -E "third_party" >output.txt + if grep -q "warning: \|error: " output.txt; then + echo "You must pass the clang tidy checks before submitting a pull request" + echo "" + grep --color -E '^|warning: |error: ' output.txt + exit 1 + fi + ) +} + do_markdown_format() { - echo -e '======================' + echo -e '========================================' echo -e ' format markdown' - echo -e '======================' + echo -e '========================================' git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --write @@ -99,9 +186,9 @@ do_markdown_format() do_markdown_check() { - echo -e '======================' + echo -e '========================================' echo -e ' check markdown' - echo -e '======================' + echo -e '========================================' git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --check @@ -109,9 +196,9 @@ do_markdown_check() do_python_format() { - echo -e '======================' + echo -e '========================================' echo -e ' format python' - echo -e '======================' + echo -e '========================================' git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style '{based_on_style: google, column_limit: 119}' -ipr @@ -119,9 +206,9 @@ do_python_format() do_python_check() { - echo -e '=====================' + echo -e '========================================' echo -e ' check python' - echo -e '=====================' + echo -e '========================================' git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style '{based_on_style: google, column_limit: 119}' -dpr @@ -129,9 +216,9 @@ do_python_check() do_shell_format() { - echo -e '=====================' + echo -e '========================================' echo -e ' format shell' - echo -e '=====================' + echo -e '========================================' git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -w @@ -139,9 +226,9 @@ do_shell_format() do_shell_check() { - echo -e '=====================' + echo -e '========================================' echo -e ' check shell' - echo -e '=====================' + echo -e '========================================' git ls-files | xargs shfmt -f | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -d @@ -153,12 +240,18 @@ do_shell_check() do_check() { if [ $# == 0 ]; then - do_clang_check + do_clang_format_check + do_clang_tidy_check do_markdown_check do_python_check do_shell_check elif [ "$1" == 'clang' ]; then - do_clang_check + do_clang_format_check + do_clang_tidy_check + elif [ "$1" == 'clang-format' ]; then + do_clang_format_check + elif [ "$1" == 'clang-tidy' ]; then + do_clang_tidy_check elif [ "$1" == 'markdown' ]; then do_markdown_check elif [ "$1" == 'python' ]; then @@ -175,12 +268,18 @@ do_check() main() { if [ $# == 0 ]; then + do_clang_tidy_fix do_clang_format do_markdown_format do_python_format do_shell_format elif [ "$1" == 'clang' ]; then + do_clang_tidy_fix do_clang_format + elif [ "$1" == 'clang-format' ]; then + do_clang_format + elif [ "$1" == 'clang-tidy' ]; then + do_clang_tidy_fix elif [ "$1" == 'markdown' ]; then do_markdown_format elif [ "$1" == 'python' ]; then