xref: /aosp_15_r20/external/ComputeLibrary/scripts/clang_tidy_rules.py (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust#!/usr/bin/env python3
2*c217d954SCole Faust# -*- coding: utf-8 -*-
3*c217d954SCole Faust#
4*c217d954SCole Faust# Copyright (c) 2017-2023 Arm Limited.
5*c217d954SCole Faust#
6*c217d954SCole Faust# SPDX-License-Identifier: MIT
7*c217d954SCole Faust#
8*c217d954SCole Faust# Permission is hereby granted, free of charge, to any person obtaining a copy
9*c217d954SCole Faust# of this software and associated documentation files (the "Software"), to
10*c217d954SCole Faust# deal in the Software without restriction, including without limitation the
11*c217d954SCole Faust# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12*c217d954SCole Faust# sell copies of the Software, and to permit persons to whom the Software is
13*c217d954SCole Faust# furnished to do so, subject to the following conditions:
14*c217d954SCole Faust#
15*c217d954SCole Faust# The above copyright notice and this permission notice shall be included in all
16*c217d954SCole Faust# copies or substantial portions of the Software.
17*c217d954SCole Faust#
18*c217d954SCole Faust# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*c217d954SCole Faust# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*c217d954SCole Faust# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21*c217d954SCole Faust# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22*c217d954SCole Faust# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23*c217d954SCole Faust# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24*c217d954SCole Faust# SOFTWARE.
25*c217d954SCole Faust
26*c217d954SCole Faustimport os
27*c217d954SCole Faustimport re
28*c217d954SCole Faustimport sys
29*c217d954SCole Faust
30*c217d954SCole Faustdef get_list_includes():
31*c217d954SCole Faust    return "src/cpu/kernels/assembly " \
32*c217d954SCole Faust           "src/core/NEON/kernels/assembly " \
33*c217d954SCole Faust           "src/core/NEON/kernels/convolution/winograd " \
34*c217d954SCole Faust           "include/linux include " \
35*c217d954SCole Faust           ". ".split()
36*c217d954SCole Faust
37*c217d954SCole Faustdef get_list_flags( filename, arch):
38*c217d954SCole Faust    assert arch in ["armv7", "aarch64"]
39*c217d954SCole Faust    flags = ["-std=c++14"]
40*c217d954SCole Faust    flags.append("-DARM_COMPUTE_CPP_SCHEDULER=1")
41*c217d954SCole Faust    flags.append("-DARM_COMPUTE_CL")
42*c217d954SCole Faust    flags.append("-DARM_COMPUTE_OPENCL_ENABLED")
43*c217d954SCole Faust    if arch == "aarch64":
44*c217d954SCole Faust        flags.append("-DARM_COMPUTE_AARCH64_V8_2")
45*c217d954SCole Faust    return flags
46*c217d954SCole Faust
47*c217d954SCole Faustdef filter_files( list_files ):
48*c217d954SCole Faust    to_check = []
49*c217d954SCole Faust    for f in list_files:
50*c217d954SCole Faust        if os.path.splitext(f)[1] != ".cpp":
51*c217d954SCole Faust            continue
52*c217d954SCole Faust        # Skip OMPScheduler as it causes problems in clang
53*c217d954SCole Faust        if (("OMPScheduler.cpp" in f) or
54*c217d954SCole Faust            ("CLTracePoint.cpp" in f) or
55*c217d954SCole Faust            ("NETracePoint.cpp" in f) or
56*c217d954SCole Faust            ("TracePoint.cpp" in f)):
57*c217d954SCole Faust            continue
58*c217d954SCole Faust        to_check.append(f)
59*c217d954SCole Faust    return to_check
60*c217d954SCole Faust
61*c217d954SCole Faustdef filter_clang_tidy_lines( lines ):
62*c217d954SCole Faust    out = []
63*c217d954SCole Faust    print_context=False
64*c217d954SCole Faust    for i in range(0, len(lines)):
65*c217d954SCole Faust        line = lines[i]
66*c217d954SCole Faust
67*c217d954SCole Faust        if "/arm_conv/" in line:
68*c217d954SCole Faust            continue
69*c217d954SCole Faust
70*c217d954SCole Faust        if "/arm_gemm/" in line:
71*c217d954SCole Faust            continue
72*c217d954SCole Faust
73*c217d954SCole Faust        if "/convolution/" in line:
74*c217d954SCole Faust            continue
75*c217d954SCole Faust
76*c217d954SCole Faust        if "/validate_examples/" in line:
77*c217d954SCole Faust            continue
78*c217d954SCole Faust
79*c217d954SCole Faust        if "error:" in line:
80*c217d954SCole Faust            if (("Version.cpp" in line and "arm_compute_version.embed" in line and "file not found" in line) or
81*c217d954SCole Faust                ("arm_fp16.h" in line) or
82*c217d954SCole Faust                ("omp.h" in line) or
83*c217d954SCole Faust                ("cast from pointer to smaller type 'uintptr_t' (aka 'unsigned int') loses information" in line) or
84*c217d954SCole Faust                ("cast from pointer to smaller type 'cl_context_properties' (aka 'int') loses information" in line) or
85*c217d954SCole Faust                ("cast from pointer to smaller type 'std::uintptr_t' (aka 'unsigned int') loses information" in line) or
86*c217d954SCole Faust                ("NEMath.inl" in line and "statement expression not allowed at file scope" in line) or
87*c217d954SCole Faust                ("Utils.h" in line and "no member named 'unmap' in 'arm_compute::Tensor'" in line) or
88*c217d954SCole Faust                ("Utils.h" in line and "no member named 'map' in 'arm_compute::Tensor'" in line) or
89*c217d954SCole Faust                ("CPUUtils.cpp" in line and "'asm/hwcap.h' file not found" in line) or
90*c217d954SCole Faust                ("CPUUtils.cpp" in line and "use of undeclared identifier 'HWCAP_SVE'" in line) or
91*c217d954SCole Faust               ("sve" in line) or
92*c217d954SCole Faust                ("'arm_compute_version.embed' file not found" in line) ):
93*c217d954SCole Faust                print_context=False
94*c217d954SCole Faust                continue
95*c217d954SCole Faust
96*c217d954SCole Faust            out.append(line)
97*c217d954SCole Faust            print_context=True
98*c217d954SCole Faust        elif "warning:" in line:
99*c217d954SCole Faust            if ("uninitialized record type: '__ret'" in line or
100*c217d954SCole Faust               "local variable '__bound_functor' is still referred to by the global variable '__once_callable'" in line or
101*c217d954SCole Faust               "assigning newly created 'gsl::owner<>'" in line or
102*c217d954SCole Faust               "calling legacy resource function without passing a 'gsl::owner<>'" in line or
103*c217d954SCole Faust               "deleting a pointer through a type that is not marked 'gsl::owner<>'" in line or
104*c217d954SCole Faust               (any(f in line for f in ["Error.cpp","Error.h"]) and "thrown exception type is not nothrow copy constructible" in line) or
105*c217d954SCole Faust               (any(f in line for f in ["Error.cpp","Error.h"]) and "uninitialized record type: 'args'" in line) or
106*c217d954SCole Faust               (any(f in line for f in ["Error.cpp","Error.h"]) and "do not call c-style vararg functions" in line) or
107*c217d954SCole Faust               (any(f in line for f in ["Error.cpp","Error.h"]) and "do not define a C-style variadic function" in line) or
108*c217d954SCole Faust               ("TensorAllocator.cpp" in line and "warning: pointer parameter 'ptr' can be pointer to const" in line) or
109*c217d954SCole Faust               ("TensorAllocator.cpp" in line and "warning: do not declare C-style arrays" in line) or
110*c217d954SCole Faust               ("RawTensor.cpp" in line and "warning: pointer parameter 'ptr' can be pointer to const" in line) or
111*c217d954SCole Faust               ("RawTensor.cpp" in line and "warning: do not declare C-style arrays" in line) or
112*c217d954SCole Faust               ("NEMinMaxLocationKernel.cpp" in line and "move constructors should be marked noexcept" in line) or
113*c217d954SCole Faust               ("NEMinMaxLocationKernel.cpp" in line and "move assignment operators should be marked noexcept" in line) or
114*c217d954SCole Faust               ("CLMinMaxLocationKernel.cpp" in line and "Forming reference to null pointer" in line) or
115*c217d954SCole Faust               ("PMUCounter.cpp" in line and "consider replacing 'long long' with 'int64'" in line) or
116*c217d954SCole Faust               ("Validation.cpp" in line and "parameter 'classified_labels' is unused" in line) or
117*c217d954SCole Faust               ("Validation.cpp" in line and "parameter 'expected_labels' is unused" in line) or
118*c217d954SCole Faust               ("Reference.cpp" in line and "parameter 'rois' is unused" in line) or
119*c217d954SCole Faust               ("Reference.cpp" in line and "parameter 'shapes' is unused" in line) or
120*c217d954SCole Faust               ("Reference.cpp" in line and re.search(r"parameter '[^']+' is unused", line)) or
121*c217d954SCole Faust               ("ReferenceCPP.cpp" in line and "parameter 'rois' is unused" in line) or
122*c217d954SCole Faust               ("ReferenceCPP.cpp" in line and "parameter 'srcs' is unused" in line) or
123*c217d954SCole Faust               ("ReferenceCPP.cpp" in line and re.search(r"parameter '[^']+' is unused", line)) or
124*c217d954SCole Faust               ("NEGEMMMatrixMultiplyKernel.cpp" in line and "do not use C-style cast to convert between unrelated types" in line) or
125*c217d954SCole Faust               ("NEPoolingLayerKernel.cpp" in line and "do not use C-style cast to convert between unrelated types" in line) or
126*c217d954SCole Faust               ("NESoftmaxLayerKernel.cpp" in line and "macro argument should be enclosed in parentheses" in line) or
127*c217d954SCole Faust               ("GraphUtils.cpp" in line and "consider replacing 'unsigned long' with 'uint32'" in line) or
128*c217d954SCole Faust               ("GraphUtils.cpp" in line and "consider replacing 'unsigned long' with 'uint64'" in line) or
129*c217d954SCole Faust               ("ConvolutionLayer.cpp" in line and "move assignment operators should be marked noexcept" in line) or
130*c217d954SCole Faust               ("ConvolutionLayer.cpp" in line and "move constructors should be marked noexcept" in line) or
131*c217d954SCole Faust               ("parameter 'memory_manager' is unused" in line) or
132*c217d954SCole Faust               ("parameter 'memory_manager' is copied for each invocation but only used as a const reference" in line) or
133*c217d954SCole Faust               ("DeconvolutionLayer.cpp" in line and "casting (double + 0.5) to integer leads to incorrect rounding; consider using lround" in line) or
134*c217d954SCole Faust               ("NEWinogradLayerKernel.cpp" in line and "use '= default' to define a trivial destructor" in line) or
135*c217d954SCole Faust               ("NEGEMMLowpMatrixMultiplyCore.cpp" in line and "constructor does not initialize these fields" in line) or
136*c217d954SCole Faust               ("NEGEMMLowpAssemblyMatrixMultiplyCore" in line and "constructor does not initialize these fields" in line) or
137*c217d954SCole Faust               ("CpuDepthwiseConv2dNativeKernel" in line and re.search(r"parameter '[^']+' is unused", line)) or
138*c217d954SCole Faust               ("CpuDepthwiseConv2dAssemblyDispatch" in line and re.search(r"parameter '[^']+' is unused", line)) or
139*c217d954SCole Faust               ("CpuDepthwiseConv2dAssemblyDispatch" in line and "modernize-use-equals-default" in line) or
140*c217d954SCole Faust               ("CPUUtils.cpp" in line and "consider replacing 'unsigned long' with 'uint64'" in line) or
141*c217d954SCole Faust               ("CPUUtils.cpp" in line and "parameter 'cpusv' is unused" in line) or
142*c217d954SCole Faust               ("CPUUtils.cpp" in line and "warning: uninitialized record type" in line) or
143*c217d954SCole Faust               ("Utils.h" in line and "warning: Use of zero-allocated memory" in line) or
144*c217d954SCole Faust               ("sve" in line) or
145*c217d954SCole Faust               ("CpuDepthwiseConv2dNativeKernel.cpp" in line and "misc-non-private-member-variables-in-classes" in line)): # This is to prevent false positive, should be reassessed with the newer clang-tidy
146*c217d954SCole Faust                print_context=False
147*c217d954SCole Faust                continue
148*c217d954SCole Faust
149*c217d954SCole Faust            if "do not use C-style cast to convert between unrelated types" in line:
150*c217d954SCole Faust                if i + 1 < len(lines) and "vgetq_lane_f16" in lines[i + 1]:
151*c217d954SCole Faust                    print_context=False
152*c217d954SCole Faust                    continue
153*c217d954SCole Faust
154*c217d954SCole Faust            if "use 'using' instead of 'typedef'" in line:
155*c217d954SCole Faust                if i + 1 < len(lines) and "BOOST_FIXTURE_TEST_SUITE" in lines[i + 1]:
156*c217d954SCole Faust                    print_context=False
157*c217d954SCole Faust                    continue
158*c217d954SCole Faust
159*c217d954SCole Faust            if "do not call c-style vararg functions" in line:
160*c217d954SCole Faust                if (i + 1 < len(lines) and
161*c217d954SCole Faust                   ("BOOST_TEST" in lines[i + 1] or
162*c217d954SCole Faust                    "BOOST_FAIL" in lines[i + 1] or
163*c217d954SCole Faust                    "BOOST_CHECK_THROW" in lines[i + 1] or
164*c217d954SCole Faust                    "ARM_COMPUTE_ERROR_VAR" in lines[i + 1] or
165*c217d954SCole Faust                    "ARM_COMPUTE_RETURN_ON" in lines[i + 1] or
166*c217d954SCole Faust                    "syscall" in lines[i + 1])):
167*c217d954SCole Faust                        print_context=False
168*c217d954SCole Faust                        continue
169*c217d954SCole Faust
170*c217d954SCole Faust            out.append(line)
171*c217d954SCole Faust            print_context=True
172*c217d954SCole Faust        elif (("CLMinMaxLocationKernel.cpp" in line and "'?' condition is false" in line) or
173*c217d954SCole Faust              ("CLMinMaxLocationKernel.cpp" in line and "Assuming the condition is false" in line) or
174*c217d954SCole Faust              ("CLMinMaxLocationKernel.cpp" in line and "Assuming pointer value is null" in line) or
175*c217d954SCole Faust              ("CLMinMaxLocationKernel.cpp" in line and "Forming reference to null pointer" in line)):
176*c217d954SCole Faust               print_context=False
177*c217d954SCole Faust               continue
178*c217d954SCole Faust        elif print_context:
179*c217d954SCole Faust            out.append(line)
180*c217d954SCole Faust
181*c217d954SCole Faust    return out
182*c217d954SCole Faust
183*c217d954SCole Faustif __name__ == "__main__":
184*c217d954SCole Faust    if len(sys.argv) != 2:
185*c217d954SCole Faust        print("usage: {} CLANG-TIDY_OUTPUT_FILE".format(sys.argv[0]))
186*c217d954SCole Faust        sys.exit(1)
187*c217d954SCole Faust
188*c217d954SCole Faust    errors = []
189*c217d954SCole Faust    with open(sys.argv[1], mode="r") as clang_tidy_file:
190*c217d954SCole Faust        lines = clang_tidy_file.readlines()
191*c217d954SCole Faust        errors = filter_clang_tidy_lines(lines)
192*c217d954SCole Faust        print("\n".join(errors))
193*c217d954SCole Faust
194*c217d954SCole Faust    sys.exit(0 if len(errors) == 0 else 1)
195