1# python3
2# Copyright (C) 2019 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Warning patterns for clang-tidy."""
17
18# No need of doc strings for trivial small functions.
19# pylint:disable=missing-function-docstring
20
21# pylint:disable=relative-beyond-top-level
22from .cpp_warn_patterns import compile_patterns
23from .severity import Severity
24
25
26def tidy_warn(description, patterns):
27  return {
28      'category': 'C/C++',
29      'severity': Severity.TIDY,
30      'description': 'clang-tidy ' + description,
31      'patterns': patterns,
32  }
33
34
35def tidy_warn_pattern(description, pattern):
36  return tidy_warn(description, [r'.*: .+\[' + pattern + r'\]$'])
37
38
39def simple_tidy_warn_pattern(description):
40  return tidy_warn_pattern(description, description)
41
42
43def group_tidy_warn_pattern(description):
44  return tidy_warn_pattern(description, description + r'-.+')
45
46
47def analyzer_high(description, patterns):
48  return {
49      'category': 'C/C++',
50      'severity': Severity.HIGH,
51      'description': description,
52      'patterns': patterns
53  }
54
55
56def analyzer_high_check(check):
57  return analyzer_high(check, [r'.*: .+\[' + check + r'\]$'])
58
59
60def analyzer_group_high(check):
61  return analyzer_high(check, [r'.*: .+\[' + check + r'.+\]$'])
62
63
64def analyzer_warn(description, patterns):
65  return {
66      'category': 'C/C++',
67      'severity': Severity.ANALYZER,
68      'description': description,
69      'patterns': patterns
70  }
71
72
73def analyzer_warn_check(check):
74  return analyzer_warn(check, [r'.*: .+\[' + check + r'\]$'])
75
76
77def analyzer_group_check(check):
78  return analyzer_warn(check, [r'.*: .+\[' + check + r'.+\]$'])
79
80
81warn_patterns = [
82    # pylint does not recognize g-inconsistent-quotes
83    # pylint:disable=line-too-long,bad-option-value,g-inconsistent-quotes
84    group_tidy_warn_pattern('altera'),
85    group_tidy_warn_pattern('android'),
86    simple_tidy_warn_pattern('abseil-string-find-startswith'),
87    simple_tidy_warn_pattern('bugprone-argument-comment'),
88    simple_tidy_warn_pattern('bugprone-branch-clone'),
89    simple_tidy_warn_pattern('bugprone-copy-constructor-init'),
90    simple_tidy_warn_pattern('bugprone-fold-init-type'),
91    simple_tidy_warn_pattern('bugprone-forward-declaration-namespace'),
92    simple_tidy_warn_pattern('bugprone-forwarding-reference-overload'),
93    simple_tidy_warn_pattern('bugprone-inaccurate-erase'),
94    simple_tidy_warn_pattern('bugprone-incorrect-roundings'),
95    simple_tidy_warn_pattern('bugprone-integer-division'),
96    simple_tidy_warn_pattern('bugprone-lambda-function-name'),
97    simple_tidy_warn_pattern('bugprone-macro-parentheses'),
98    simple_tidy_warn_pattern('bugprone-misplaced-widening-cast'),
99    simple_tidy_warn_pattern('bugprone-move-forwarding-reference'),
100    simple_tidy_warn_pattern('bugprone-parent-virtual-call'),
101    simple_tidy_warn_pattern('bugprone-posix-return'),
102    simple_tidy_warn_pattern('bugprone-sizeof-container'),
103    simple_tidy_warn_pattern('bugprone-sizeof-expression'),
104    simple_tidy_warn_pattern('bugprone-string-constructor'),
105    simple_tidy_warn_pattern('bugprone-string-integer-assignment'),
106    simple_tidy_warn_pattern('bugprone-suspicious-enum-usage'),
107    simple_tidy_warn_pattern('bugprone-suspicious-missing-comma'),
108    simple_tidy_warn_pattern('bugprone-suspicious-string-compare'),
109    simple_tidy_warn_pattern('bugprone-suspicious-semicolon'),
110    simple_tidy_warn_pattern('bugprone-terminating-continue'),
111    simple_tidy_warn_pattern('bugprone-too-small-loop-variable'),
112    simple_tidy_warn_pattern('bugprone-undefined-memory-manipulation'),
113    simple_tidy_warn_pattern('bugprone-unhandled-self-assignment'),
114    simple_tidy_warn_pattern('bugprone-unused-raii'),
115    simple_tidy_warn_pattern('bugprone-unused-return-value'),
116    simple_tidy_warn_pattern('bugprone-use-after-move'),
117    group_tidy_warn_pattern('bugprone'),
118    simple_tidy_warn_pattern('cert-dcl16-c'),
119    simple_tidy_warn_pattern('cert-dcl21-cpp'),
120    simple_tidy_warn_pattern('cert-dcl50-cpp'),
121    simple_tidy_warn_pattern('cert-dcl54-cpp'),
122    simple_tidy_warn_pattern('cert-dcl59-cpp'),
123    simple_tidy_warn_pattern('cert-env33-c'),
124    simple_tidy_warn_pattern('cert-err34-c'),
125    simple_tidy_warn_pattern('cert-err52-cpp'),
126    simple_tidy_warn_pattern('cert-msc30-c'),
127    simple_tidy_warn_pattern('cert-msc50-cpp'),
128    simple_tidy_warn_pattern('cert-oop54-cpp'),
129    group_tidy_warn_pattern('cert'),
130    group_tidy_warn_pattern('clang-diagnostic'),
131    group_tidy_warn_pattern('concurrency'),
132    group_tidy_warn_pattern('cppcoreguidelines'),
133    group_tidy_warn_pattern('fuchsia'),
134    simple_tidy_warn_pattern('google-default-arguments'),
135    simple_tidy_warn_pattern('google-runtime-int'),
136    simple_tidy_warn_pattern('google-runtime-operator'),
137    simple_tidy_warn_pattern('google-runtime-references'),
138    group_tidy_warn_pattern('google-build'),
139    group_tidy_warn_pattern('google-explicit'),
140    group_tidy_warn_pattern('google-redability'),
141    group_tidy_warn_pattern('google-global'),
142    group_tidy_warn_pattern('google-redability'),
143    group_tidy_warn_pattern('google-redability'),
144    group_tidy_warn_pattern('google'),
145    simple_tidy_warn_pattern('hicpp-explicit-conversions'),
146    simple_tidy_warn_pattern('hicpp-function-size'),
147    simple_tidy_warn_pattern('hicpp-invalid-access-moved'),
148    simple_tidy_warn_pattern('hicpp-member-init'),
149    simple_tidy_warn_pattern('hicpp-delete-operators'),
150    simple_tidy_warn_pattern('hicpp-special-member-functions'),
151    simple_tidy_warn_pattern('hicpp-use-equals-default'),
152    simple_tidy_warn_pattern('hicpp-use-equals-delete'),
153    simple_tidy_warn_pattern('hicpp-no-assembler'),
154    simple_tidy_warn_pattern('hicpp-noexcept-move'),
155    simple_tidy_warn_pattern('hicpp-use-override'),
156    group_tidy_warn_pattern('hicpp'),
157    group_tidy_warn_pattern('llvm'),
158    group_tidy_warn_pattern('llvmlibc'),
159    group_tidy_warn_pattern('misc'),
160    group_tidy_warn_pattern('modernize'),
161    simple_tidy_warn_pattern('performance-faster-string-find'),
162    simple_tidy_warn_pattern('performance-for-range-copy'),
163    simple_tidy_warn_pattern('performance-implicit-cast-in-loop'),
164    simple_tidy_warn_pattern('performance-inefficient-string-concatenation'),
165    simple_tidy_warn_pattern('performance-type-promotion-in-math-fn'),
166    simple_tidy_warn_pattern('performance-unnecessary-copy-initialization'),
167    simple_tidy_warn_pattern('performance-unnecessary-value-param'),
168    simple_tidy_warn_pattern('portability-simd-intrinsics'),
169    group_tidy_warn_pattern('performance'),
170    group_tidy_warn_pattern('readability'),
171    simple_tidy_warn_pattern('abseil-string-find-startwith'),
172    simple_tidy_warn_pattern('abseil-faster-strsplit-delimiter'),
173    simple_tidy_warn_pattern('abseil-no-namespace'),
174    simple_tidy_warn_pattern('abseil-no-internal-dependencies'),
175    group_tidy_warn_pattern('abseil'),
176    simple_tidy_warn_pattern('portability-simd-intrinsics'),
177    group_tidy_warn_pattern('portability'),
178
179    tidy_warn('TIMEOUT', [r".*: warning: clang-tidy aborted "]),
180    tidy_warn('Long Runs', [r".*: warning: clang-tidy used "]),
181
182    # warnings from clang-tidy's clang-analyzer checks
183    analyzer_high('clang-analyzer-core, null pointer',
184                  [r".*: warning: .+ pointer is null .*\[clang-analyzer-core"]),
185    analyzer_high('clang-analyzer-core, uninitialized value',
186                  [r".*: warning: .+ uninitialized (value|data) .*\[clang-analyzer-core"]),
187    analyzer_warn('clang-analyzer-optin.performance.Padding',
188                  [r".*: warning: Excessive padding in '.*'"]),
189    # analyzer_warn('clang-analyzer Unreachable code',
190    #               [r".*: warning: This statement is never executed.*UnreachableCode"]),
191    analyzer_warn('clang-analyzer Size of malloc may overflow',
192                  [r".*: warning: .* size of .* may overflow .*MallocOverflow"]),
193    analyzer_warn('clang-analyzer sozeof() on a pointer type',
194                  [r".*: warning: .*calls sizeof.* on a pointer type.*SizeofPtr"]),
195    analyzer_warn('clang-analyzer Pointer arithmetic on non-array variables',
196                  [r".*: warning: Pointer arithmetic on non-array variables .*PointerArithm"]),
197    analyzer_warn('clang-analyzer Subtraction of pointers of different memory chunks',
198                  [r".*: warning: Subtraction of two pointers .*PointerSub"]),
199    analyzer_warn('clang-analyzer Access out-of-bound array element',
200                  [r".*: warning: Access out-of-bound array element .*ArrayBound"]),
201    analyzer_warn('clang-analyzer Out of bound memory access',
202                  [r".*: warning: Out of bound memory access .*ArrayBoundV2"]),
203    analyzer_warn('clang-analyzer Possible lock order reversal',
204                  [r".*: warning: .* Possible lock order reversal.*PthreadLock"]),
205    analyzer_warn('clang-analyzer call path problems',
206                  [r".*: warning: Call Path : .+"]),
207    analyzer_warn_check('clang-analyzer-core.CallAndMessage'),
208    analyzer_high_check('clang-analyzer-core.NonNullParamChecker'),
209    analyzer_high_check('clang-analyzer-core.NullDereference'),
210    analyzer_warn_check('clang-analyzer-core.UndefinedBinaryOperatorResult'),
211    analyzer_warn_check('clang-analyzer-core.DivideZero'),
212    analyzer_warn_check('clang-analyzer-core.VLASize'),
213    analyzer_warn_check('clang-analyzer-core.uninitialized.ArraySubscript'),
214    analyzer_warn_check('clang-analyzer-core.uninitialized.Assign'),
215    analyzer_warn_check('clang-analyzer-core.uninitialized.UndefReturn'),
216    analyzer_warn_check('clang-analyzer-cplusplus.Move'),
217    analyzer_warn_check('clang-analyzer-deadcode.DeadStores'),
218    analyzer_warn_check('clang-analyzer-optin.cplusplus.UninitializedObject'),
219    analyzer_warn_check('clang-analyzer-optin.cplusplus.VirtualCall'),
220    analyzer_warn_check('clang-analyzer-portability.UnixAPI'),
221    analyzer_warn_check('clang-analyzer-unix.cstring.NullArg'),
222    analyzer_high_check('clang-analyzer-unix.MallocSizeof'),
223    analyzer_warn_check('clang-analyzer-valist.Uninitialized'),
224    analyzer_warn_check('clang-analyzer-valist.Unterminated'),
225    analyzer_group_check('clang-analyzer-core.uninitialized'),
226    analyzer_group_check('clang-analyzer-deadcode'),
227    analyzer_warn_check('clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling'),
228    analyzer_warn_check('clang-analyzer-security.insecureAPI.bcmp'),
229    analyzer_warn_check('clang-analyzer-security.insecureAPI.bcopy'),
230    analyzer_warn_check('clang-analyzer-security.insecureAPI.bzero'),
231    analyzer_warn_check('clang-analyzer-security.insecureAPI.strcpy'),
232    analyzer_group_high('clang-analyzer-security.insecureAPI'),
233    analyzer_group_high('clang-analyzer-security'),
234    analyzer_high_check('clang-analyzer-unix.Malloc'),
235    analyzer_high_check('clang-analyzer-cplusplus.NewDeleteLeaks'),
236    analyzer_high_check('clang-analyzer-cplusplus.NewDelete'),
237    analyzer_group_check('clang-analyzer-unix'),
238    analyzer_group_check('clang-analyzer'),  # catch all
239]
240
241
242compile_patterns(warn_patterns)
243