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 0d20a21
Show file tree
Hide file tree
Showing 37 changed files with 296 additions and 145 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)'
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
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
39 changes: 39 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,27 @@
# 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")

OT_CLANG_TIDY_BUILD_OPTS=(
'-DCMAKE_EXPORT_COMPILE_COMMANDS=ON'
)
readonly OT_CLANG_TIDY_BUILD_OPTS

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

do_markdown_format()
{
echo -e '======================'
Expand Down Expand Up @@ -126,10 +160,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 +186,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
6 changes: 3 additions & 3 deletions src/app/cli/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1202,12 +1202,12 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr)
}
else
{
for (auto iter = json.begin(); iter != json.end(); ++iter)
for (const auto &jsonObject : json)
{
BorderAgent ba;
try
{
BorderAgentFromJson(ba, *iter);
BorderAgentFromJson(ba, jsonObject);
} catch (std::exception &e)
{
ExitNow(value = ERROR_BAD_FORMAT("incorrect border agent JSON format: {}", e.what()));
Expand Down Expand Up @@ -1440,7 +1440,7 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr)
auto mdnsCancelEventQueue = [](evutil_socket_t aSocket, short aWhat, void *aArg) {
(void)aSocket;
(void)aWhat;
event_base *ev_base = (event_base *)aArg;
event_base *ev_base = static_cast<event_base *>(aArg);
event_base_loopbreak(ev_base);
};
// Attach cancel pipe to event loop. When bytes written to the
Expand Down
2 changes: 1 addition & 1 deletion src/app/cli/interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class Interpreter
*
* @see @ref Interpreter::MultiNetCommandContext
*/
Error ReParseMultiNetworkSyntax(const Expression &aExpr, Expression &aRretExpr);
Error ReParseMultiNetworkSyntax(const Expression &aExpr, Expression &aRetExpr);
/**
* Updates on-screen visualization of the current network
* selection.
Expand Down
1 change: 0 additions & 1 deletion src/app/cli/interpreter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ using namespace ot::commissioner::persistent_storage;

using testing::_;
using testing::DoAll;
using testing::Mock;
using testing::Return;
using testing::ReturnRef;
using testing::StrEq;
Expand Down
21 changes: 13 additions & 8 deletions src/app/cli/job_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void JobManager::CleanupJobs()
{
for (auto job : mJobPool)
{
ASSERT(job == NULL || job->IsStopped());
ASSERT(job == nullptr || job->IsStopped());
delete job;
}
mJobPool.clear();
Expand Down Expand Up @@ -122,12 +122,16 @@ Error JobManager::CreateJob(CommissionerAppPtr &aCommissioner, const Interpreter

Error JobManager::PrepareJobs(const Interpreter::Expression &aExpr, const XpanIdArray &aNids, bool aGroupAlias)
{
Error error;

if (utils::ToLower(aExpr[0]) == "start")
return PrepareStartJobs(aExpr, aNids, aGroupAlias);
{
ExitNow(error = PrepareStartJobs(aExpr, aNids, aGroupAlias));
}
else if (utils::ToLower(aExpr[0]) == "stop")
return PrepareStopJobs(aExpr, aNids, aGroupAlias);

Error error;
{
ExitNow(error = PrepareStopJobs(aExpr, aNids, aGroupAlias));
}

for (const auto &nid : aNids)
{
Expand Down Expand Up @@ -169,6 +173,7 @@ Error JobManager::PrepareJobs(const Interpreter::Expression &aExpr, const XpanId

SuccessOrExit(error = CreateJob(entry->second, jobExpr, nid));
}

exit:
return error;
}
Expand Down Expand Up @@ -603,7 +608,7 @@ void JobManager::RunJobs()
{
for (auto job : mJobPool)
{
ASSERT(job != NULL);
ASSERT(job != nullptr);
job->Run();
}
WaitForJobs();
Expand All @@ -616,7 +621,7 @@ void JobManager::CancelCommand()

for (auto job : mJobPool)
{
ASSERT(job != NULL);
ASSERT(job != nullptr);
job->Cancel();
}
WaitForJobs();
Expand All @@ -639,7 +644,7 @@ void JobManager::WaitForJobs()
{
for (auto job : mJobPool)
{
ASSERT(job != NULL);
ASSERT(job != nullptr);
job->Wait();
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/app/cli/job_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ using namespace ot::commissioner;
using testing::_;
using testing::DoAll;
using testing::Invoke;
using testing::Mock;
using testing::Return;
using testing::WithArg;
using testing::WithArgs;
Expand All @@ -63,10 +62,7 @@ class JobManagerTestSuite : public testing::Test
struct TestContext
{
TestContext()
: mPS{}
, mRegistry{}
, mConf{}
, mInterpreter{}
: mConf{}
, mJobManager{mInterpreter}
, mDefaultCommissioner{new CommissionerAppMock()}
{
Expand All @@ -86,10 +82,7 @@ class JobManagerTestSuite : public testing::Test
CommissionerAppStaticExpecter mCommissionerAppStaticExpecter;
};

JobManagerTestSuite()
: Test()
{
}
JobManagerTestSuite() = default;
virtual ~JobManagerTestSuite() = default;

void SetInitialExpectations(TestContext &aContext)
Expand Down
2 changes: 1 addition & 1 deletion src/app/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int main(int argc, const char *argv[])

while (parseParams)
{
ch = getopt_long(argc, (char *const *)argv, "hvc:r:", gCommissionerCliOptions, nullptr);
ch = getopt_long(argc, const_cast<char *const *>(argv), "hvc:r:", gCommissionerCliOptions, nullptr);
switch (ch)
{
case 'h':
Expand Down
Loading

0 comments on commit 0d20a21

Please sign in to comment.