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_pattern(description, pattern):
27  return {
28      'category': 'C/C++',
29      'severity': Severity.TIDY,
30      'description': 'clang-tidy ' + description,
31      'patterns': [r'.*: .+\[' + pattern + r'\]$']
32  }
33
34
35def simple_tidy_warn_pattern(description):
36  return tidy_warn_pattern(description, description)
37
38
39def group_tidy_warn_pattern(description):
40  return tidy_warn_pattern(description, description + r'-.+')
41
42
43def analyzer_high(description, patterns):
44  return {
45      'category': 'C/C++',
46      'severity': Severity.HIGH,
47      'description': description,
48      'patterns': patterns
49  }
50
51
52def analyzer_high_check(check):
53  return analyzer_high(check, [r'.*: .+\[' + check + r'\]$'])
54
55
56def analyzer_group_high(check):
57  return analyzer_high(check, [r'.*: .+\[' + check + r'.+\]$'])
58
59
60def analyzer_warn(description, patterns):
61  return {
62      'category': 'C/C++',
63      'severity': Severity.ANALYZER,
64      'description': description,
65      'patterns': patterns
66  }
67
68
69def analyzer_warn_check(check):
70  return analyzer_warn(check, [r'.*: .+\[' + check + r'\]$'])
71
72
73def analyzer_group_check(check):
74  return analyzer_warn(check, [r'.*: .+\[' + check + r'.+\]$'])
75
76
77warn_patterns = [
78    # pylint does not recognize g-inconsistent-quotes
79    # pylint:disable=line-too-long,bad-option-value,g-inconsistent-quotes
80    group_tidy_warn_pattern('android'),
81    simple_tidy_warn_pattern('abseil-string-find-startswith'),
82    simple_tidy_warn_pattern('bugprone-argument-comment'),
83    simple_tidy_warn_pattern('bugprone-branch-clone'),
84    simple_tidy_warn_pattern('bugprone-copy-constructor-init'),
85    simple_tidy_warn_pattern('bugprone-fold-init-type'),
86    simple_tidy_warn_pattern('bugprone-forward-declaration-namespace'),
87    simple_tidy_warn_pattern('bugprone-forwarding-reference-overload'),
88    simple_tidy_warn_pattern('bugprone-inaccurate-erase'),
89    simple_tidy_warn_pattern('bugprone-incorrect-roundings'),
90    simple_tidy_warn_pattern('bugprone-integer-division'),
91    simple_tidy_warn_pattern('bugprone-lambda-function-name'),
92    simple_tidy_warn_pattern('bugprone-macro-parentheses'),
93    simple_tidy_warn_pattern('bugprone-misplaced-widening-cast'),
94    simple_tidy_warn_pattern('bugprone-move-forwarding-reference'),
95    simple_tidy_warn_pattern('bugprone-parent-virtual-call'),
96    simple_tidy_warn_pattern('bugprone-posix-return'),
97    simple_tidy_warn_pattern('bugprone-sizeof-container'),
98    simple_tidy_warn_pattern('bugprone-sizeof-expression'),
99    simple_tidy_warn_pattern('bugprone-string-constructor'),
100    simple_tidy_warn_pattern('bugprone-string-integer-assignment'),
101    simple_tidy_warn_pattern('bugprone-suspicious-enum-usage'),
102    simple_tidy_warn_pattern('bugprone-suspicious-missing-comma'),
103    simple_tidy_warn_pattern('bugprone-suspicious-string-compare'),
104    simple_tidy_warn_pattern('bugprone-suspicious-semicolon'),
105    simple_tidy_warn_pattern('bugprone-terminating-continue'),
106    simple_tidy_warn_pattern('bugprone-too-small-loop-variable'),
107    simple_tidy_warn_pattern('bugprone-undefined-memory-manipulation'),
108    simple_tidy_warn_pattern('bugprone-unhandled-self-assignment'),
109    simple_tidy_warn_pattern('bugprone-unused-raii'),
110    simple_tidy_warn_pattern('bugprone-unused-return-value'),
111    simple_tidy_warn_pattern('bugprone-use-after-move'),
112    group_tidy_warn_pattern('bugprone'),
113    simple_tidy_warn_pattern('cert-dcl16-c'),
114    simple_tidy_warn_pattern('cert-dcl21-cpp'),
115    simple_tidy_warn_pattern('cert-dcl50-cpp'),
116    simple_tidy_warn_pattern('cert-dcl54-cpp'),
117    simple_tidy_warn_pattern('cert-dcl59-cpp'),
118    simple_tidy_warn_pattern('cert-env33-c'),
119    simple_tidy_warn_pattern('cert-err34-c'),
120    simple_tidy_warn_pattern('cert-err52-cpp'),
121    simple_tidy_warn_pattern('cert-msc30-c'),
122    simple_tidy_warn_pattern('cert-msc50-cpp'),
123    simple_tidy_warn_pattern('cert-oop54-cpp'),
124    group_tidy_warn_pattern('cert'),
125    group_tidy_warn_pattern('clang-diagnostic'),
126    group_tidy_warn_pattern('cppcoreguidelines'),
127    group_tidy_warn_pattern('llvm'),
128    simple_tidy_warn_pattern('google-default-arguments'),
129    simple_tidy_warn_pattern('google-runtime-int'),
130    simple_tidy_warn_pattern('google-runtime-operator'),
131    simple_tidy_warn_pattern('google-runtime-references'),
132    group_tidy_warn_pattern('google-build'),
133    group_tidy_warn_pattern('google-explicit'),
134    group_tidy_warn_pattern('google-redability'),
135    group_tidy_warn_pattern('google-global'),
136    group_tidy_warn_pattern('google-redability'),
137    group_tidy_warn_pattern('google-redability'),
138    group_tidy_warn_pattern('google'),
139    simple_tidy_warn_pattern('hicpp-explicit-conversions'),
140    simple_tidy_warn_pattern('hicpp-function-size'),
141    simple_tidy_warn_pattern('hicpp-invalid-access-moved'),
142    simple_tidy_warn_pattern('hicpp-member-init'),
143    simple_tidy_warn_pattern('hicpp-delete-operators'),
144    simple_tidy_warn_pattern('hicpp-special-member-functions'),
145    simple_tidy_warn_pattern('hicpp-use-equals-default'),
146    simple_tidy_warn_pattern('hicpp-use-equals-delete'),
147    simple_tidy_warn_pattern('hicpp-no-assembler'),
148    simple_tidy_warn_pattern('hicpp-noexcept-move'),
149    simple_tidy_warn_pattern('hicpp-use-override'),
150    group_tidy_warn_pattern('hicpp'),
151    group_tidy_warn_pattern('modernize'),
152    group_tidy_warn_pattern('misc'),
153    simple_tidy_warn_pattern('performance-faster-string-find'),
154    simple_tidy_warn_pattern('performance-for-range-copy'),
155    simple_tidy_warn_pattern('performance-implicit-cast-in-loop'),
156    simple_tidy_warn_pattern('performance-inefficient-string-concatenation'),
157    simple_tidy_warn_pattern('performance-type-promotion-in-math-fn'),
158    simple_tidy_warn_pattern('performance-unnecessary-copy-initialization'),
159    simple_tidy_warn_pattern('performance-unnecessary-value-param'),
160    simple_tidy_warn_pattern('portability-simd-intrinsics'),
161    group_tidy_warn_pattern('performance'),
162    group_tidy_warn_pattern('readability'),
163    simple_tidy_warn_pattern('abseil-string-find-startwith'),
164    simple_tidy_warn_pattern('abseil-faster-strsplit-delimiter'),
165    simple_tidy_warn_pattern('abseil-no-namespace'),
166    simple_tidy_warn_pattern('abseil-no-internal-dependencies'),
167    group_tidy_warn_pattern('abseil'),
168    simple_tidy_warn_pattern('portability-simd-intrinsics'),
169    group_tidy_warn_pattern('portability'),
170
171    # warnings from clang-tidy's clang-analyzer checks
172    analyzer_high('clang-analyzer-core, null pointer',
173                  [r".*: warning: .+ pointer is null .*\[clang-analyzer-core"]),
174    analyzer_high('clang-analyzer-core, uninitialized value',
175                  [r".*: warning: .+ uninitialized (value|data) .*\[clang-analyzer-core"]),
176    analyzer_warn('clang-analyzer-optin.performance.Padding',
177                  [r".*: warning: Excessive padding in '.*'"]),
178    # analyzer_warn('clang-analyzer Unreachable code',
179    #               [r".*: warning: This statement is never executed.*UnreachableCode"]),
180    analyzer_warn('clang-analyzer Size of malloc may overflow',
181                  [r".*: warning: .* size of .* may overflow .*MallocOverflow"]),
182    analyzer_warn('clang-analyzer sozeof() on a pointer type',
183                  [r".*: warning: .*calls sizeof.* on a pointer type.*SizeofPtr"]),
184    analyzer_warn('clang-analyzer Pointer arithmetic on non-array variables',
185                  [r".*: warning: Pointer arithmetic on non-array variables .*PointerArithm"]),
186    analyzer_warn('clang-analyzer Subtraction of pointers of different memory chunks',
187                  [r".*: warning: Subtraction of two pointers .*PointerSub"]),
188    analyzer_warn('clang-analyzer Access out-of-bound array element',
189                  [r".*: warning: Access out-of-bound array element .*ArrayBound"]),
190    analyzer_warn('clang-analyzer Out of bound memory access',
191                  [r".*: warning: Out of bound memory access .*ArrayBoundV2"]),
192    analyzer_warn('clang-analyzer Possible lock order reversal',
193                  [r".*: warning: .* Possible lock order reversal.*PthreadLock"]),
194    analyzer_warn('clang-analyzer call path problems',
195                  [r".*: warning: Call Path : .+"]),
196    analyzer_warn_check('clang-analyzer-core.CallAndMessage'),
197    analyzer_high_check('clang-analyzer-core.NonNullParamChecker'),
198    analyzer_high_check('clang-analyzer-core.NullDereference'),
199    analyzer_warn_check('clang-analyzer-core.UndefinedBinaryOperatorResult'),
200    analyzer_warn_check('clang-analyzer-core.DivideZero'),
201    analyzer_warn_check('clang-analyzer-core.VLASize'),
202    analyzer_warn_check('clang-analyzer-core.uninitialized.ArraySubscript'),
203    analyzer_warn_check('clang-analyzer-core.uninitialized.Assign'),
204    analyzer_warn_check('clang-analyzer-core.uninitialized.UndefReturn'),
205    analyzer_warn_check('clang-analyzer-cplusplus.Move'),
206    analyzer_warn_check('clang-analyzer-deadcode.DeadStores'),
207    analyzer_warn_check('clang-analyzer-optin.cplusplus.UninitializedObject'),
208    analyzer_warn_check('clang-analyzer-optin.cplusplus.VirtualCall'),
209    analyzer_warn_check('clang-analyzer-portability.UnixAPI'),
210    analyzer_warn_check('clang-analyzer-unix.cstring.NullArg'),
211    analyzer_high_check('clang-analyzer-unix.MallocSizeof'),
212    analyzer_warn_check('clang-analyzer-valist.Uninitialized'),
213    analyzer_warn_check('clang-analyzer-valist.Unterminated'),
214    analyzer_group_check('clang-analyzer-core.uninitialized'),
215    analyzer_group_check('clang-analyzer-deadcode'),
216    analyzer_warn_check('clang-analyzer-security.insecureAPI.strcpy'),
217    analyzer_group_high('clang-analyzer-security.insecureAPI'),
218    analyzer_group_high('clang-analyzer-security'),
219    analyzer_high_check('clang-analyzer-unix.Malloc'),
220    analyzer_high_check('clang-analyzer-cplusplus.NewDeleteLeaks'),
221    analyzer_high_check('clang-analyzer-cplusplus.NewDelete'),
222    analyzer_group_check('clang-analyzer-unix'),
223    analyzer_group_check('clang-analyzer'),  # catch all
224]
225
226
227compile_patterns(warn_patterns)
228