Skip to content

Commit

Permalink
[CI] Use clang-tidy in CI (#15563)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and pull[bot] committed Jul 11, 2023
1 parent 6f93245 commit 1907282
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
Checks: '-*,bugprone-use-after-move'
Checks: 'bugprone-*,-bugprone-not-null-terminated-result,-bugprone-suspicious-memory-comparison,-bugprone-argument-comment,-bugprone-unused-return-value,-bugprone-branch-clone,-bugprone-easily-swappable-parameters,-bugprone-reserved-identifier,-bugprone-macro-parentheses,-bugprone-forward-declaration-namespace,-bugprone-forwarding-reference-overload,-bugprone-undelegated-constructor,-bugprone-sizeof-expression,-bugprone-implicit-widening-of-multiplication-result,-bugprone-too-small-loop-variable,-bugprone-narrowing-conversions,-bugprone-misplaced-widening-cast,-bugprone-suspicious-include,-bugprone-signed-char-misuse,-bugprone-copy-constructor-init,-clang-analyzer-core.CallAndMessage,-clang-analyzer-core.UndefinedBinaryOperatorResult,-clang-analyzer-core.NullDereference,-clang-analyzer-optin.cplusplus.UninitializedObject,-clang-analyzer-core.uninitialized.Branch,-clang-analyzer-optin.performance,-clang-analyzer-deadcode.DeadStores,-clang-analyzer-cplusplus.Move,-clang-analyzer-optin.cplusplus.VirtualCall,-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-nullability.NullablePassedToNonnull,-clang-analyzer-optin.performance.Padding,-clang-analyzer-security.insecureAPI.bzero,-clang-analyzer-unix.cstring.NullArg,-clang-analyzer-security.insecureAPI.rand,-clang-analyzer-core.NonNullParamChecker,-clang-analyzer-nullability.NullPassedToNonnull,-clang-analyzer-unix.Malloc,-clang-analyzer-valist.Unterminated,-clang-analyzer-cplusplus.NewDeleteLeaks,-clang-diagnostic-implicit-int-conversion'
WarningsAsErrors: '*'
HeaderFilterRegex: ''
HeaderFilterRegex: '(src|examples|zzz_generated|credentials)'
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ jobs:
case $BUILD_TYPE in
"fake") GN_ARGS='chip_device_platform="fake"';;
"gcc_release") GN_ARGS='is_debug=false';;
"clang") GN_ARGS='is_clang=true';;
"clang") GN_ARGS='is_clang=true pw_command_launcher="`pwd`/../scripts/helpers/clang-tidy-launcher.py"';;
"mbedtls") GN_ARGS='chip_crypto="mbedtls"';;
esac
Expand Down Expand Up @@ -317,7 +317,7 @@ jobs:
run: |
for BUILD_TYPE in clang python_lib; do
case $BUILD_TYPE in
"clang") GN_ARGS='is_clang=true target_os="all" is_asan=true';;
"clang") GN_ARGS='is_clang=true target_os="all" is_asan=true pw_command_launcher="`pwd`/../scripts/helpers/clang-tidy-launcher.py"';;
"python_lib") GN_ARGS='enable_rtti=true enable_pylib=true';;
esac
scripts/build/gn_gen.sh --args="$GN_ARGS"
Expand Down
83 changes: 55 additions & 28 deletions scripts/helpers/clang-tidy-launcher.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,73 @@
#!/usr/bin/env python
#!/usr/bin/env -S python3 -B

# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import subprocess
import sys


def main():
if len(sys.argv) < 2:
return 1
def consumeArgument(args):
if len(args) == 0:
return None
return args.pop(0)


cc = sys.argv[1]
if not cc.startswith("clang"):
def maybeRunClangTidy(args):
# If the command is not a clang command, do no try to run clang-tidy on it
cc = consumeArgument(args)
if cc is None or not cc.startswith("clang"):
return 0

command = ["clang-tidy"]
clang_args = []
clang_tidy_srcs = []
clang_tidy_args = []

i = 2
while i < len(sys.argv):
if sys.argv[i] == "-c":
# Source file
i += 1
if i >= len(sys.argv):
print("Got -c without argument", file=sys.stderr)
# If warnings comes from third_party, ignore it as there is little we can do.
ignored_list = ['third_party/mbedtls', 'third_party/lwip']

arg = consumeArgument(args)
while arg:
if arg == "-c":
sourceFile = consumeArgument(args)
if sourceFile is None:
return 1
command.append(sys.argv[i])
elif sys.argv[i] == "-o":
# Ignore output
i += 1
if i >= len(sys.argv):
print("Got -o without argument", file=sys.stderr)

for name in ignored_list:
if name in sourceFile:
return 0

clang_tidy_srcs.append(sourceFile)

elif arg == "-o":
objectFile = consumeArgument(args)
if objectFile is None:
return 1
else:
clang_args.append(sys.argv[i])
clang_tidy_args.append(arg)

arg = consumeArgument(args)

i += 1
command = ["clang-tidy"] + clang_tidy_srcs + ["--"] + clang_tidy_args
return subprocess.run(command).returncode

tidy_result = subprocess.run(command + ["--"] + clang_args)
if tidy_result.returncode != 0:
return tidy_result.returncode

clang_result = subprocess.run(sys.argv[1:])
return clang_result.returncode
def main():
returnCode = maybeRunClangTidy(sys.argv[1:])
if returnCode != 0:
return returnCode

return subprocess.run(sys.argv[1:]).returncode


if __name__ == "__main__":
Expand Down

0 comments on commit 1907282

Please sign in to comment.