Skip to content

Commit

Permalink
[style] add clang-tidy format
Browse files Browse the repository at this point in the history
  • Loading branch information
wgtdkp committed Apr 3, 2024
1 parent 7c936cb commit e870c84
Show file tree
Hide file tree
Showing 59 changed files with 887 additions and 389 deletions.
24 changes: 24 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
Checks: >
-*,
bugprone-argument-comment,
bugprone-too-small-loop-variable,
google-explicit-constructor,
google-readability-casting,
misc-include-cleaner,
misc-unused-using-decls,
modernize-loop-convert,
modernize-use-bool-literals,
modernize-use-equals-default,
modernize-use-equals-delete,
modernize-use-nullptr,
readability-avoid-const-params-in-decls,
readability-else-after-return,
readability-inconsistent-declaration-parameter-name,
readability-make-member-function-const,
readability-redundant-control-flow,
readability-redundant-member-init,
readability-simplify-boolean-expr,
readability-static-accessed-through-instance
WarningsAsErrors: '*'
HeaderFilterRegex: '(include|src|tests).*(?<!third_party.*repo)'
6 changes: 6 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Diagnostics:
# for Include Cleaner, see https://clangd.llvm.org/design/include-cleaner
UnusedIncludes: Strict
MissingIncludes: Strict
If:
PathExclude: third_party/.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ jobs:
- name: Bootstrap
run: |
sudo apt update
sudo apt --no-install-recommends install -y clang-format-14
python3 -m pip install yapf==0.29.0
script/bootstrap.sh
- name: Check
run: |
script/make-pretty check
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ cmake-build-*
#tools
.vscode
.idea
.cache
CMakeLists.txt.user

#log
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()

if (CMAKE_BUILD_TYPE STREQUAL Debug)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Output compile commands for clang-tidy" FORCE)
endif()

if(NOT "${CMAKE_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:taskAffinity=""
android:theme="@style/Theme.OpenThreadCommissioner">
<activity
android:name=".MainActivity"
Expand Down
6 changes: 3 additions & 3 deletions include/commissioner/commissioner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
#ifndef OT_COMM_COMMISSIONER_HPP_
#define OT_COMM_COMMISSIONER_HPP_

#include <cstdint>
#include <functional>
#include <memory>
#include <string>

#include <stddef.h>
#include <stdint.h>
#include <vector>

#include <commissioner/defines.hpp>
#include <commissioner/error.hpp>
Expand Down
15 changes: 3 additions & 12 deletions include/commissioner/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
#ifndef OT_COMM_ERROR_HPP_
#define OT_COMM_ERROR_HPP_

#include <memory>
#include <ostream>
#include <string>
#include <utility>

#include <commissioner/defines.hpp>

Expand Down Expand Up @@ -227,18 +227,9 @@ class OT_COMM_MUST_USE_RESULT Error
std::string mMessage;
};

inline Error::Error(const Error &aError)
: mCode(aError.mCode)
, mMessage(aError.mMessage)
{
}
inline Error::Error(const Error &aError) = default;

inline Error &Error::operator=(const Error &aError)
{
mCode = aError.mCode;
mMessage = aError.mMessage;
return *this;
}
inline Error &Error::operator=(const Error &aError) = default;

inline Error::Error(Error &&aError) noexcept
: mCode(std::move(aError.mCode))
Expand Down
20 changes: 11 additions & 9 deletions include/commissioner/network_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
#ifndef OT_COMM_NETWORK_DATA_HPP_
#define OT_COMM_NETWORK_DATA_HPP_

#include <cstdint>
#include <string>

#include <stdint.h>
#include <vector>

#include <commissioner/defines.hpp>
#include <commissioner/error.hpp>
Expand Down Expand Up @@ -69,10 +69,10 @@ struct XpanId

bool operator==(const XpanId &aOther) const;

bool operator!=(const uint64_t aOther) const;
bool operator<(const XpanId aOther) const;
bool operator!=(const XpanId &aOther) const;
bool operator<(const XpanId &aOther) const;

operator std::string() const;
explicit operator std::string() const;

/**
* Decodes hexadecimal string.
Expand Down Expand Up @@ -257,12 +257,14 @@ struct PanId
static constexpr uint64_t kEmptyPanId = 0;

uint16_t mValue;
PanId(uint16_t aValue);
explicit PanId(uint16_t aValue);
PanId(const PanId &aOther) = default;
PanId();

PanId &operator=(uint16_t aValue);
operator uint16_t() const;
operator std::string() const;
PanId &operator=(const PanId &aValue) = default;
PanId &operator=(uint16_t aValue);
explicit operator uint16_t() const;
explicit operator std::string() const;

Error FromHex(const std::string &aInput);
};
Expand Down
10 changes: 7 additions & 3 deletions script/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ if [ "$(uname)" = "Linux" ]; then
lcov \
jsonlint

sudo apt-get --no-install-recommends install -y clang-format-14 || echo 'WARNING: could not install clang-format-14, which is useful if you plan to contribute C/C++ code to the OpenThread project.'
sudo apt-get --no-install-recommends install -y clang-format-14 clang-tidy-14 || echo 'WARNING: could not install clang-format-14 and clang-tidy-14, which is useful if you plan to contribute C/C++ code to the OpenThread project.'
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.'

## Install newest CMake
Expand All @@ -118,8 +118,12 @@ elif [ "$(uname)" = "Darwin" ]; then
swig@4 \
lcov && true

brew install llvm@14 && sudo ln -s "$(brew --prefix llvm@14)/bin/clang-format" /usr/local/bin/clang-format-14 \
|| echo 'WARNING: could not install clang-format-14, which is useful if you plan to contribute C/C++ code to the OpenThread project.'
brew install llvm@14 && \
sudo ln -s "$(brew --prefix llvm@14)/bin/clang-format" /usr/local/bin/clang-format-14 && \
sudo ln -s "$(brew --prefix llvm@14)/bin/clang-tidy" /usr/local/bin/clang-tidy-14 && \
sudo ln -s "$(brew --prefix llvm@14)/bin/clang-apply-replacements" /usr/local/bin/clang-apply-replacements-14 && \
sudo ln -s "$(brew --prefix llvm@14)/bin/run-clang-tidy" /usr/local/bin/run-clang-tidy-14 || \
echo 'WARNING: could not install clang-format-14, which is useful if you plan to contribute C/C++ code to the OpenThread project.'

## Install latest cmake
match_version "$(cmake --version | grep -E -o '[0-9].*')" "${MIN_CMAKE_VERSION}" || {
Expand Down
86 changes: 86 additions & 0 deletions script/clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash
#
# Copyright (c) 2020, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

CLANG_TIDY_VERSION="LLVM version 14.0"
CLANG_APPLY_REPLACEMENTS_VERSION="clang-apply-replacements version 14.0"

die()
{
echo " *** ERROR: $*"
exit 1
}

# Search for clang-tidy-14
if command -v clang-tidy-14 >/dev/null; then
clang_tidy=$(command -v clang-tidy-14)
elif command -v clang-tidy >/dev/null; then
clang_tidy=$(command -v clang-tidy)
case "$($clang_tidy --version)" in
*"$CLANG_TIDY_VERSION"*) ;;

*)
die "$($clang_tidy --version); $CLANG_TIDY_VERSION required"
;;
esac
else
die "clang-tidy 14.0 required"
fi

# Search for clang-apply-replacements-14
if command -v clang-apply-replacements-14 >/dev/null; then
clang_apply_replacements=$(command -v clang-apply-replacements-14)
elif command -v clang-apply-replacements >/dev/null; then
clang_apply_replacements=$(command -v clang-apply-replacements)
case "$($clang_apply_replacements --version)" in
"$CLANG_APPLY_REPLACEMENTS_VERSION"*) ;;

*)
die "$($clang_apply_replacements --version); $CLANG_APPLY_REPLACEMENTS_VERSION required"
;;
esac
else
die "clang-apply-replacements 14.0 required"
fi

# Search for run-clang-tidy-14.py
if command -v run-clang-tidy-14.py >/dev/null; then
run_clang_tidy=$(command -v run-clang-tidy-14.py)
elif command -v run-clang-tidy-14 >/dev/null; then
run_clang_tidy=$(command -v run-clang-tidy-14)
elif command -v run-clang-tidy.py >/dev/null; then
run_clang_tidy=$(command -v run-clang-tidy.py)
elif command -v run-clang-tidy >/dev/null; then
run_clang_tidy=$(command -v run-clang-tidy)
else
die "run-clang-tidy.py 14.0 required"
fi

$run_clang_tidy -clang-tidy-binary "$clang_tidy" -clang-apply-replacements-binary "$clang_apply_replacements" "$@" || die

exit 0
34 changes: 34 additions & 0 deletions script/make-pretty
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# Format c/c++ only:
#
# script/make-pretty clang
# script/make-pretty clang-tidy
#
# Format markdown only:
#
Expand All @@ -49,18 +50,22 @@
# Check only:
#
# script/make-pretty check clang
# script/make-pretty check clang-tidy
# script/make-pretty check markdown
# script/make-pretty check python
#

set -euo pipefail

readonly OT_COMM_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd )

readonly OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN)
readonly OT_EXCLUDE_DIRS=(third_party)

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=("${OT_COMM_DIR}/include" "${OT_COMM_DIR}/src" "${OT_COMM_DIR}/tests")

do_clang_format()
{
Expand All @@ -82,6 +87,30 @@ do_clang_check()
| 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 \
&& cmake ../.. \
&& ../../script/clang-tidy -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 \
&& cmake ../.. \
&& ../../script/clang-tidy -j"$OT_BUILD_JOBS" "${OT_CLANG_TIDY_FIX_DIRS[@]}")
}

do_markdown_format()
{
echo -e '======================'
Expand Down Expand Up @@ -126,10 +155,13 @@ do_check()
{
if [ $# == 0 ]; then
do_clang_check
do_clang_tidy_check
do_markdown_check
do_python_check
elif [ "$1" == 'clang' ]; then
do_clang_check
elif [ "$1" == 'clang-tidy' ]; then
do_clang_tidy_check
elif [ "$1" == 'markdown' ]; then
do_markdown_check
elif [ "$1" == 'python' ]; then
Expand All @@ -149,6 +181,8 @@ main()
do_python_format
elif [ "$1" == 'clang' ]; then
do_clang_format
elif [ "$1" == 'clang-tidy' ]; then
do_clang_tidy_fix
elif [ "$1" == 'markdown' ]; then
do_markdown_format
elif [ "$1" == 'python' ]; then
Expand Down
Loading

0 comments on commit e870c84

Please sign in to comment.