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