erg@chromium.org | d528f8b | 2012-05-11 17:31:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2 | # |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 3 | # Copyright (c) 2009 Google Inc. All rights reserved. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4 | # |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 8 | # |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 18 | # |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 30 | |
agable | f39c333 | 2016-09-26 09:35:42 -0700 | [diff] [blame] | 31 | # pylint: skip-file |
| 32 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 33 | """Does google-lint on c++ files. |
| 34 | |
| 35 | The goal of this script is to identify places in the code that *may* |
| 36 | be in non-compliance with google style. It does not attempt to fix |
| 37 | up these problems -- the point is to educate. It does also not |
| 38 | attempt to find all problems, or to ensure that everything it does |
| 39 | find is legitimately a problem. |
| 40 | |
| 41 | In particular, we can get very confused by /* and // inside strings! |
| 42 | We do a small hack, which is to ignore //'s with "'s after them on the |
| 43 | same line, but it is far from perfect (in either direction). |
| 44 | """ |
| 45 | |
| 46 | import codecs |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 47 | import copy |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 48 | import getopt |
| 49 | import math # for log |
| 50 | import os |
| 51 | import re |
| 52 | import sre_compile |
| 53 | import string |
| 54 | import sys |
| 55 | import unicodedata |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 56 | import sysconfig |
| 57 | |
| 58 | try: |
| 59 | xrange # Python 2 |
| 60 | except NameError: |
| 61 | xrange = range # Python 3 |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | _USAGE = """ |
| 65 | Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...] |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 66 | [--counting=total|toplevel|detailed] [--root=subdir] |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 67 | [--linelength=digits] [--headers=x,y,...] |
| 68 | [--quiet] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 69 | <file> [file] ... |
| 70 | |
| 71 | The style guidelines this tries to follow are those in |
Alexandr Ilin | ff294c3 | 2017-04-27 15:57:40 +0200 | [diff] [blame] | 72 | https://google.github.io/styleguide/cppguide.html |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 73 | |
| 74 | Every problem is given a confidence score from 1-5, with 5 meaning we are |
| 75 | certain of the problem, and 1 meaning it could be a legitimate construct. |
| 76 | This will miss some errors, and is not a substitute for a code review. |
| 77 | |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 78 | To suppress false-positive errors of a certain category, add a |
| 79 | 'NOLINT(category)' comment to the line. NOLINT or NOLINT(*) |
| 80 | suppresses errors of all categories on that line. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 81 | |
| 82 | The files passed in will be linted; at least one file must be provided. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 83 | Default linted extensions are .cc, .cpp, .cu, .cuh and .h. Change the |
| 84 | extensions with the --extensions flag. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 85 | |
| 86 | Flags: |
| 87 | |
| 88 | output=vs7 |
| 89 | By default, the output is formatted to ease emacs parsing. Visual Studio |
| 90 | compatible output (vs7) may also be used. Other formats are unsupported. |
| 91 | |
| 92 | verbose=# |
| 93 | Specify a number 0-5 to restrict errors to certain verbosity levels. |
| 94 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 95 | quiet |
| 96 | Don't print anything if no errors are found. |
| 97 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 98 | filter=-x,+y,... |
| 99 | Specify a comma-separated list of category-filters to apply: only |
| 100 | error messages whose category names pass the filters will be printed. |
| 101 | (Category names are printed with the message and look like |
| 102 | "[whitespace/indent]".) Filters are evaluated left to right. |
| 103 | "-FOO" and "FOO" means "do not print categories that start with FOO". |
| 104 | "+FOO" means "do print categories that start with FOO". |
| 105 | |
| 106 | Examples: --filter=-whitespace,+whitespace/braces |
| 107 | --filter=whitespace,runtime/printf,+runtime/printf_format |
| 108 | --filter=-,+build/include_what_you_use |
| 109 | |
| 110 | To see a list of all the categories used in cpplint, pass no arg: |
| 111 | --filter= |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 112 | |
| 113 | counting=total|toplevel|detailed |
| 114 | The total number of errors found is always printed. If |
| 115 | 'toplevel' is provided, then the count of errors in each of |
| 116 | the top-level categories like 'build' and 'whitespace' will |
| 117 | also be printed. If 'detailed' is provided, then a count |
| 118 | is provided for each category like 'build/class'. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 119 | |
| 120 | root=subdir |
| 121 | The root directory used for deriving header guard CPP variable. |
| 122 | By default, the header guard CPP variable is calculated as the relative |
| 123 | path to the directory that contains .git, .hg, or .svn. When this flag |
| 124 | is specified, the relative path is calculated from the specified |
| 125 | directory. If the specified directory does not exist, this flag is |
| 126 | ignored. |
| 127 | |
| 128 | Examples: |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 129 | Assuming that top/src/.git exists (and cwd=top/src), the header guard |
| 130 | CPP variables for top/src/chrome/browser/ui/browser.h are: |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 131 | |
| 132 | No flag => CHROME_BROWSER_UI_BROWSER_H_ |
| 133 | --root=chrome => BROWSER_UI_BROWSER_H_ |
| 134 | --root=chrome/browser => UI_BROWSER_H_ |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 135 | --root=.. => SRC_CHROME_BROWSER_UI_BROWSER_H_ |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 136 | |
| 137 | linelength=digits |
| 138 | This is the allowed line length for the project. The default value is |
| 139 | 80 characters. |
| 140 | |
| 141 | Examples: |
| 142 | --linelength=120 |
| 143 | |
| 144 | extensions=extension,extension,... |
| 145 | The allowed file extensions that cpplint will check |
| 146 | |
| 147 | Examples: |
| 148 | --extensions=hpp,cpp |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 149 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 150 | headers=x,y,... |
| 151 | The header extensions that cpplint will treat as .h in checks. Values are |
| 152 | automatically added to --extensions list. |
| 153 | |
| 154 | Examples: |
| 155 | --headers=hpp,hxx |
| 156 | --headers=hpp |
| 157 | |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 158 | cpplint.py supports per-directory configurations specified in CPPLINT.cfg |
| 159 | files. CPPLINT.cfg file can contain a number of key=value pairs. |
| 160 | Currently the following options are supported: |
| 161 | |
| 162 | set noparent |
| 163 | filter=+filter1,-filter2,... |
| 164 | exclude_files=regex |
avakulenko@google.com | 68a4fa6 | 2014-08-25 16:26:18 +0000 | [diff] [blame] | 165 | linelength=80 |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 166 | root=subdir |
| 167 | headers=x,y,... |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 168 | |
| 169 | "set noparent" option prevents cpplint from traversing directory tree |
| 170 | upwards looking for more .cfg files in parent directories. This option |
| 171 | is usually placed in the top-level project directory. |
| 172 | |
| 173 | The "filter" option is similar in function to --filter flag. It specifies |
| 174 | message filters in addition to the |_DEFAULT_FILTERS| and those specified |
| 175 | through --filter command-line flag. |
| 176 | |
| 177 | "exclude_files" allows to specify a regular expression to be matched against |
| 178 | a file name. If the expression matches, the file is skipped and not run |
| 179 | through liner. |
| 180 | |
avakulenko@google.com | 68a4fa6 | 2014-08-25 16:26:18 +0000 | [diff] [blame] | 181 | "linelength" allows to specify the allowed line length for the project. |
| 182 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 183 | The "root" option is similar in function to the --root flag (see example |
| 184 | above). Paths are relative to the directory of the CPPLINT.cfg. |
| 185 | |
| 186 | The "headers" option is similar in function to the --headers flag |
| 187 | (see example above). |
| 188 | |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 189 | CPPLINT.cfg has an effect on files in the same directory and all |
| 190 | sub-directories, unless overridden by a nested configuration file. |
| 191 | |
| 192 | Example file: |
| 193 | filter=-build/include_order,+build/include_alpha |
| 194 | exclude_files=.*\.cc |
| 195 | |
| 196 | The above example disables build/include_order warning and enables |
| 197 | build/include_alpha as well as excludes all .cc from being |
| 198 | processed by linter, in the current directory (where the .cfg |
| 199 | file is located) and all sub-directories. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 200 | """ |
| 201 | |
| 202 | # We categorize each error message we print. Here are the categories. |
| 203 | # We want an explicit list so we can list them all in cpplint --filter=. |
| 204 | # If you add a new error message with a new category, add it to the list |
| 205 | # here! cpplint_unittest.py should tell you if you forget to do this. |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 206 | _ERROR_CATEGORIES = [ |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 207 | 'build/class', |
| 208 | 'build/c++11', |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 209 | 'build/c++14', |
| 210 | 'build/c++tr1', |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 211 | 'build/deprecated', |
| 212 | 'build/endif_comment', |
| 213 | 'build/explicit_make_pair', |
| 214 | 'build/forward_decl', |
| 215 | 'build/header_guard', |
| 216 | 'build/include', |
| 217 | 'build/include_alpha', |
| 218 | 'build/include_order', |
| 219 | 'build/include_what_you_use', |
| 220 | 'build/namespaces', |
| 221 | 'build/printf_format', |
| 222 | 'build/storage_class', |
| 223 | 'legal/copyright', |
| 224 | 'readability/alt_tokens', |
| 225 | 'readability/braces', |
| 226 | 'readability/casting', |
| 227 | 'readability/check', |
| 228 | 'readability/constructors', |
| 229 | 'readability/fn_size', |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 230 | 'readability/inheritance', |
| 231 | 'readability/multiline_comment', |
| 232 | 'readability/multiline_string', |
| 233 | 'readability/namespace', |
| 234 | 'readability/nolint', |
| 235 | 'readability/nul', |
| 236 | 'readability/strings', |
| 237 | 'readability/todo', |
| 238 | 'readability/utf8', |
| 239 | 'runtime/arrays', |
| 240 | 'runtime/casting', |
| 241 | 'runtime/explicit', |
| 242 | 'runtime/int', |
| 243 | 'runtime/init', |
| 244 | 'runtime/invalid_increment', |
| 245 | 'runtime/member_string_references', |
| 246 | 'runtime/memset', |
| 247 | 'runtime/indentation_namespace', |
| 248 | 'runtime/operator', |
| 249 | 'runtime/printf', |
| 250 | 'runtime/printf_format', |
| 251 | 'runtime/references', |
| 252 | 'runtime/string', |
| 253 | 'runtime/threadsafe_fn', |
| 254 | 'runtime/vlog', |
| 255 | 'whitespace/blank_line', |
| 256 | 'whitespace/braces', |
| 257 | 'whitespace/comma', |
| 258 | 'whitespace/comments', |
| 259 | 'whitespace/empty_conditional_body', |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 260 | 'whitespace/empty_if_body', |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 261 | 'whitespace/empty_loop_body', |
| 262 | 'whitespace/end_of_line', |
| 263 | 'whitespace/ending_newline', |
| 264 | 'whitespace/forcolon', |
| 265 | 'whitespace/indent', |
| 266 | 'whitespace/line_length', |
| 267 | 'whitespace/newline', |
| 268 | 'whitespace/operators', |
| 269 | 'whitespace/parens', |
| 270 | 'whitespace/semicolon', |
| 271 | 'whitespace/tab', |
| 272 | 'whitespace/todo', |
| 273 | ] |
| 274 | |
| 275 | # These error categories are no longer enforced by cpplint, but for backwards- |
| 276 | # compatibility they may still appear in NOLINT comments. |
| 277 | _LEGACY_ERROR_CATEGORIES = [ |
| 278 | 'readability/streams', |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 279 | 'readability/function', |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 280 | ] |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 281 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 282 | # The default state of the category filter. This is overridden by the --filter= |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 283 | # flag. By default all errors are on, so only add here categories that should be |
| 284 | # off by default (i.e., categories that must be enabled by the --filter= flags). |
| 285 | # All entries here should start with a '-' or '+', as in the --filter= flag. |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 286 | _DEFAULT_FILTERS = ['-build/include_alpha'] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 287 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 288 | # The default list of categories suppressed for C (not C++) files. |
| 289 | _DEFAULT_C_SUPPRESSED_CATEGORIES = [ |
| 290 | 'readability/casting', |
| 291 | ] |
| 292 | |
| 293 | # The default list of categories suppressed for Linux Kernel files. |
| 294 | _DEFAULT_KERNEL_SUPPRESSED_CATEGORIES = [ |
| 295 | 'whitespace/tab', |
| 296 | ] |
| 297 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 298 | # We used to check for high-bit characters, but after much discussion we |
| 299 | # decided those were OK, as long as they were in UTF-8 and didn't represent |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 300 | # hard-coded international strings, which belong in a separate i18n file. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 301 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 302 | # C++ headers |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 303 | _CPP_HEADERS = frozenset([ |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 304 | # Legacy |
| 305 | 'algobase.h', |
| 306 | 'algo.h', |
| 307 | 'alloc.h', |
| 308 | 'builtinbuf.h', |
| 309 | 'bvector.h', |
| 310 | 'complex.h', |
| 311 | 'defalloc.h', |
| 312 | 'deque.h', |
| 313 | 'editbuf.h', |
| 314 | 'fstream.h', |
| 315 | 'function.h', |
| 316 | 'hash_map', |
| 317 | 'hash_map.h', |
| 318 | 'hash_set', |
| 319 | 'hash_set.h', |
| 320 | 'hashtable.h', |
| 321 | 'heap.h', |
| 322 | 'indstream.h', |
| 323 | 'iomanip.h', |
| 324 | 'iostream.h', |
| 325 | 'istream.h', |
| 326 | 'iterator.h', |
| 327 | 'list.h', |
| 328 | 'map.h', |
| 329 | 'multimap.h', |
| 330 | 'multiset.h', |
| 331 | 'ostream.h', |
| 332 | 'pair.h', |
| 333 | 'parsestream.h', |
| 334 | 'pfstream.h', |
| 335 | 'procbuf.h', |
| 336 | 'pthread_alloc', |
| 337 | 'pthread_alloc.h', |
| 338 | 'rope', |
| 339 | 'rope.h', |
| 340 | 'ropeimpl.h', |
| 341 | 'set.h', |
| 342 | 'slist', |
| 343 | 'slist.h', |
| 344 | 'stack.h', |
| 345 | 'stdiostream.h', |
| 346 | 'stl_alloc.h', |
| 347 | 'stl_relops.h', |
| 348 | 'streambuf.h', |
| 349 | 'stream.h', |
| 350 | 'strfile.h', |
| 351 | 'strstream.h', |
| 352 | 'tempbuf.h', |
| 353 | 'tree.h', |
| 354 | 'type_traits.h', |
| 355 | 'vector.h', |
| 356 | # 17.6.1.2 C++ library headers |
| 357 | 'algorithm', |
| 358 | 'array', |
| 359 | 'atomic', |
| 360 | 'bitset', |
| 361 | 'chrono', |
| 362 | 'codecvt', |
| 363 | 'complex', |
| 364 | 'condition_variable', |
| 365 | 'deque', |
| 366 | 'exception', |
| 367 | 'forward_list', |
| 368 | 'fstream', |
| 369 | 'functional', |
| 370 | 'future', |
| 371 | 'initializer_list', |
| 372 | 'iomanip', |
| 373 | 'ios', |
| 374 | 'iosfwd', |
| 375 | 'iostream', |
| 376 | 'istream', |
| 377 | 'iterator', |
| 378 | 'limits', |
| 379 | 'list', |
| 380 | 'locale', |
| 381 | 'map', |
| 382 | 'memory', |
| 383 | 'mutex', |
| 384 | 'new', |
| 385 | 'numeric', |
| 386 | 'ostream', |
| 387 | 'queue', |
| 388 | 'random', |
| 389 | 'ratio', |
| 390 | 'regex', |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 391 | 'scoped_allocator', |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 392 | 'set', |
| 393 | 'sstream', |
| 394 | 'stack', |
| 395 | 'stdexcept', |
| 396 | 'streambuf', |
| 397 | 'string', |
| 398 | 'strstream', |
| 399 | 'system_error', |
| 400 | 'thread', |
| 401 | 'tuple', |
| 402 | 'typeindex', |
| 403 | 'typeinfo', |
| 404 | 'type_traits', |
| 405 | 'unordered_map', |
| 406 | 'unordered_set', |
| 407 | 'utility', |
| 408 | 'valarray', |
| 409 | 'vector', |
| 410 | # 17.6.1.2 C++ headers for C library facilities |
| 411 | 'cassert', |
| 412 | 'ccomplex', |
| 413 | 'cctype', |
| 414 | 'cerrno', |
| 415 | 'cfenv', |
| 416 | 'cfloat', |
| 417 | 'cinttypes', |
| 418 | 'ciso646', |
| 419 | 'climits', |
| 420 | 'clocale', |
| 421 | 'cmath', |
| 422 | 'csetjmp', |
| 423 | 'csignal', |
| 424 | 'cstdalign', |
| 425 | 'cstdarg', |
| 426 | 'cstdbool', |
| 427 | 'cstddef', |
| 428 | 'cstdint', |
| 429 | 'cstdio', |
| 430 | 'cstdlib', |
| 431 | 'cstring', |
| 432 | 'ctgmath', |
| 433 | 'ctime', |
| 434 | 'cuchar', |
| 435 | 'cwchar', |
| 436 | 'cwctype', |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 437 | ]) |
| 438 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 439 | # Type names |
| 440 | _TYPES = re.compile( |
| 441 | r'^(?:' |
| 442 | # [dcl.type.simple] |
| 443 | r'(char(16_t|32_t)?)|wchar_t|' |
| 444 | r'bool|short|int|long|signed|unsigned|float|double|' |
| 445 | # [support.types] |
| 446 | r'(ptrdiff_t|size_t|max_align_t|nullptr_t)|' |
| 447 | # [cstdint.syn] |
| 448 | r'(u?int(_fast|_least)?(8|16|32|64)_t)|' |
| 449 | r'(u?int(max|ptr)_t)|' |
| 450 | r')$') |
| 451 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 452 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 453 | # These headers are excluded from [build/include] and [build/include_order] |
| 454 | # checks: |
| 455 | # - Anything not following google file name conventions (containing an |
| 456 | # uppercase character, such as Python.h or nsStringAPI.h, for example). |
| 457 | # - Lua headers. |
| 458 | _THIRD_PARTY_HEADERS_PATTERN = re.compile( |
| 459 | r'^(?:[^/]*[A-Z][^/]*\.h|lua\.h|lauxlib\.h|lualib\.h)$') |
| 460 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 461 | # Pattern for matching FileInfo.BaseName() against test file name |
| 462 | _TEST_FILE_SUFFIX = r'(_test|_unittest|_regtest)$' |
| 463 | |
| 464 | # Pattern that matches only complete whitespace, possibly across multiple lines. |
| 465 | _EMPTY_CONDITIONAL_BODY_PATTERN = re.compile(r'^\s*$', re.DOTALL) |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 466 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 467 | # Assertion macros. These are defined in base/logging.h and |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 468 | # testing/base/public/gunit.h. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 469 | _CHECK_MACROS = [ |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 470 | 'DCHECK', 'CHECK', |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 471 | 'EXPECT_TRUE', 'ASSERT_TRUE', |
| 472 | 'EXPECT_FALSE', 'ASSERT_FALSE', |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 473 | ] |
| 474 | |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 475 | # Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 476 | _CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS]) |
| 477 | |
| 478 | for op, replacement in [('==', 'EQ'), ('!=', 'NE'), |
| 479 | ('>=', 'GE'), ('>', 'GT'), |
| 480 | ('<=', 'LE'), ('<', 'LT')]: |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 481 | _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 482 | _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement |
| 483 | _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement |
| 484 | _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 485 | |
| 486 | for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'), |
| 487 | ('>=', 'LT'), ('>', 'LE'), |
| 488 | ('<=', 'GT'), ('<', 'GE')]: |
| 489 | _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement |
| 490 | _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 491 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 492 | # Alternative tokens and their replacements. For full list, see section 2.5 |
| 493 | # Alternative tokens [lex.digraph] in the C++ standard. |
| 494 | # |
| 495 | # Digraphs (such as '%:') are not included here since it's a mess to |
| 496 | # match those on a word boundary. |
| 497 | _ALT_TOKEN_REPLACEMENT = { |
| 498 | 'and': '&&', |
| 499 | 'bitor': '|', |
| 500 | 'or': '||', |
| 501 | 'xor': '^', |
| 502 | 'compl': '~', |
| 503 | 'bitand': '&', |
| 504 | 'and_eq': '&=', |
| 505 | 'or_eq': '|=', |
| 506 | 'xor_eq': '^=', |
| 507 | 'not': '!', |
| 508 | 'not_eq': '!=' |
| 509 | } |
| 510 | |
| 511 | # Compile regular expression that matches all the above keywords. The "[ =()]" |
| 512 | # bit is meant to avoid matching these keywords outside of boolean expressions. |
| 513 | # |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 514 | # False positives include C-style multi-line comments and multi-line strings |
| 515 | # but those have always been troublesome for cpplint. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 516 | _ALT_TOKEN_REPLACEMENT_PATTERN = re.compile( |
| 517 | r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)') |
| 518 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 519 | |
| 520 | # These constants define types of headers for use with |
| 521 | # _IncludeState.CheckNextIncludeOrder(). |
| 522 | _C_SYS_HEADER = 1 |
| 523 | _CPP_SYS_HEADER = 2 |
| 524 | _LIKELY_MY_HEADER = 3 |
| 525 | _POSSIBLE_MY_HEADER = 4 |
| 526 | _OTHER_HEADER = 5 |
| 527 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 528 | # These constants define the current inline assembly state |
| 529 | _NO_ASM = 0 # Outside of inline assembly block |
| 530 | _INSIDE_ASM = 1 # Inside inline assembly block |
| 531 | _END_ASM = 2 # Last line of inline assembly block |
| 532 | _BLOCK_ASM = 3 # The whole block is an inline assembly block |
| 533 | |
| 534 | # Match start of assembly blocks |
| 535 | _MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)' |
| 536 | r'(?:\s+(volatile|__volatile__))?' |
| 537 | r'\s*[{(]') |
| 538 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 539 | # Match strings that indicate we're working on a C (not C++) file. |
| 540 | _SEARCH_C_FILE = re.compile(r'\b(?:LINT_C_FILE|' |
| 541 | r'vim?:\s*.*(\s*|:)filetype=c(\s*|:|$))') |
| 542 | |
| 543 | # Match string that indicates we're working on a Linux Kernel file. |
| 544 | _SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 545 | |
| 546 | _regexp_compile_cache = {} |
| 547 | |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 548 | # {str, set(int)}: a map from error categories to sets of linenumbers |
| 549 | # on which those errors are expected and should be suppressed. |
| 550 | _error_suppressions = {} |
| 551 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 552 | # The root directory used for deriving header guard CPP variable. |
| 553 | # This is set by --root flag. |
| 554 | _root = None |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 555 | _root_debug = False |
sdefresne | 263e928 | 2016-07-19 02:14:22 -0700 | [diff] [blame] | 556 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 557 | # The allowed line length of files. |
| 558 | # This is set by --linelength flag. |
| 559 | _line_length = 80 |
| 560 | |
| 561 | # The allowed extensions for file names |
| 562 | # This is set by --extensions flag. |
| 563 | _valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh']) |
| 564 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 565 | # Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc. |
| 566 | # This is set by --headers flag. |
| 567 | _hpp_headers = set(['h']) |
| 568 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 569 | # {str, bool}: a map from error categories to booleans which indicate if the |
| 570 | # category should be suppressed for every line. |
| 571 | _global_error_suppressions = {} |
| 572 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 573 | def ProcessHppHeadersOption(val): |
| 574 | global _hpp_headers |
| 575 | try: |
| 576 | _hpp_headers = set(val.split(',')) |
| 577 | # Automatically append to extensions list so it does not have to be set 2 times |
| 578 | _valid_extensions.update(_hpp_headers) |
| 579 | except ValueError: |
| 580 | PrintUsage('Header extensions must be comma separated list.') |
| 581 | |
| 582 | def IsHeaderExtension(file_extension): |
| 583 | return file_extension in _hpp_headers |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 584 | |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 585 | def ParseNolintSuppressions(filename, raw_line, linenum, error): |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 586 | """Updates the global list of line error-suppressions. |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 587 | |
| 588 | Parses any NOLINT comments on the current line, updating the global |
| 589 | error_suppressions store. Reports an error if the NOLINT comment |
| 590 | was malformed. |
| 591 | |
| 592 | Args: |
| 593 | filename: str, the name of the input file. |
| 594 | raw_line: str, the line of input text, with comments. |
| 595 | linenum: int, the number of the current line. |
| 596 | error: function, an error handler. |
| 597 | """ |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 598 | matched = Search(r'\bNOLINT(NEXTLINE)?\b(\([^)]+\))?', raw_line) |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 599 | if matched: |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 600 | if matched.group(1): |
| 601 | suppressed_line = linenum + 1 |
| 602 | else: |
| 603 | suppressed_line = linenum |
| 604 | category = matched.group(2) |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 605 | if category in (None, '(*)'): # => "suppress all" |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 606 | _error_suppressions.setdefault(None, set()).add(suppressed_line) |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 607 | else: |
| 608 | if category.startswith('(') and category.endswith(')'): |
| 609 | category = category[1:-1] |
| 610 | if category in _ERROR_CATEGORIES: |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 611 | _error_suppressions.setdefault(category, set()).add(suppressed_line) |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 612 | elif category not in _LEGACY_ERROR_CATEGORIES: |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 613 | error(filename, linenum, 'readability/nolint', 5, |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 614 | 'Unknown NOLINT error category: %s' % category) |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 615 | |
| 616 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 617 | def ProcessGlobalSuppresions(lines): |
| 618 | """Updates the list of global error suppressions. |
| 619 | |
| 620 | Parses any lint directives in the file that have global effect. |
| 621 | |
| 622 | Args: |
| 623 | lines: An array of strings, each representing a line of the file, with the |
| 624 | last element being empty if the file is terminated with a newline. |
| 625 | """ |
| 626 | for line in lines: |
| 627 | if _SEARCH_C_FILE.search(line): |
| 628 | for category in _DEFAULT_C_SUPPRESSED_CATEGORIES: |
| 629 | _global_error_suppressions[category] = True |
| 630 | if _SEARCH_KERNEL_FILE.search(line): |
| 631 | for category in _DEFAULT_KERNEL_SUPPRESSED_CATEGORIES: |
| 632 | _global_error_suppressions[category] = True |
| 633 | |
| 634 | |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 635 | def ResetNolintSuppressions(): |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 636 | """Resets the set of NOLINT suppressions to empty.""" |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 637 | _error_suppressions.clear() |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 638 | _global_error_suppressions.clear() |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 639 | |
| 640 | |
| 641 | def IsErrorSuppressedByNolint(category, linenum): |
| 642 | """Returns true if the specified error category is suppressed on this line. |
| 643 | |
| 644 | Consults the global error_suppressions map populated by |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 645 | ParseNolintSuppressions/ProcessGlobalSuppresions/ResetNolintSuppressions. |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 646 | |
| 647 | Args: |
| 648 | category: str, the category of the error. |
| 649 | linenum: int, the current line number. |
| 650 | Returns: |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 651 | bool, True iff the error should be suppressed due to a NOLINT comment or |
| 652 | global suppression. |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 653 | """ |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 654 | return (_global_error_suppressions.get(category, False) or |
| 655 | linenum in _error_suppressions.get(category, set()) or |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 656 | linenum in _error_suppressions.get(None, set())) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 657 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 658 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 659 | def Match(pattern, s): |
| 660 | """Matches the string with the pattern, caching the compiled regexp.""" |
| 661 | # The regexp compilation caching is inlined in both Match and Search for |
| 662 | # performance reasons; factoring it out into a separate function turns out |
| 663 | # to be noticeably expensive. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 664 | if pattern not in _regexp_compile_cache: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 665 | _regexp_compile_cache[pattern] = sre_compile.compile(pattern) |
| 666 | return _regexp_compile_cache[pattern].match(s) |
| 667 | |
| 668 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 669 | def ReplaceAll(pattern, rep, s): |
| 670 | """Replaces instances of pattern in a string with a replacement. |
| 671 | |
| 672 | The compiled regex is kept in a cache shared by Match and Search. |
| 673 | |
| 674 | Args: |
| 675 | pattern: regex pattern |
| 676 | rep: replacement text |
| 677 | s: search string |
| 678 | |
| 679 | Returns: |
| 680 | string with replacements made (or original string if no replacements) |
| 681 | """ |
| 682 | if pattern not in _regexp_compile_cache: |
| 683 | _regexp_compile_cache[pattern] = sre_compile.compile(pattern) |
| 684 | return _regexp_compile_cache[pattern].sub(rep, s) |
| 685 | |
| 686 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 687 | def Search(pattern, s): |
| 688 | """Searches the string for the pattern, caching the compiled regexp.""" |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 689 | if pattern not in _regexp_compile_cache: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 690 | _regexp_compile_cache[pattern] = sre_compile.compile(pattern) |
| 691 | return _regexp_compile_cache[pattern].search(s) |
| 692 | |
| 693 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 694 | def _IsSourceExtension(s): |
| 695 | """File extension (excluding dot) matches a source file extension.""" |
| 696 | return s in ('c', 'cc', 'cpp', 'cxx') |
| 697 | |
| 698 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 699 | class _IncludeState(object): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 700 | """Tracks line numbers for includes, and the order in which includes appear. |
| 701 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 702 | include_list contains list of lists of (header, line number) pairs. |
| 703 | It's a lists of lists rather than just one flat list to make it |
| 704 | easier to update across preprocessor boundaries. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 705 | |
| 706 | Call CheckNextIncludeOrder() once for each header in the file, passing |
| 707 | in the type constants defined above. Calls in an illegal order will |
| 708 | raise an _IncludeError with an appropriate error message. |
| 709 | |
| 710 | """ |
| 711 | # self._section will move monotonically through this set. If it ever |
| 712 | # needs to move backwards, CheckNextIncludeOrder will raise an error. |
| 713 | _INITIAL_SECTION = 0 |
| 714 | _MY_H_SECTION = 1 |
| 715 | _C_SECTION = 2 |
| 716 | _CPP_SECTION = 3 |
| 717 | _OTHER_H_SECTION = 4 |
| 718 | |
| 719 | _TYPE_NAMES = { |
| 720 | _C_SYS_HEADER: 'C system header', |
| 721 | _CPP_SYS_HEADER: 'C++ system header', |
| 722 | _LIKELY_MY_HEADER: 'header this file implements', |
| 723 | _POSSIBLE_MY_HEADER: 'header this file may implement', |
| 724 | _OTHER_HEADER: 'other header', |
| 725 | } |
| 726 | _SECTION_NAMES = { |
| 727 | _INITIAL_SECTION: "... nothing. (This can't be an error.)", |
| 728 | _MY_H_SECTION: 'a header this file implements', |
| 729 | _C_SECTION: 'C system header', |
| 730 | _CPP_SECTION: 'C++ system header', |
| 731 | _OTHER_H_SECTION: 'other header', |
| 732 | } |
| 733 | |
| 734 | def __init__(self): |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 735 | self.include_list = [[]] |
| 736 | self.ResetSection('') |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 737 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 738 | def FindHeader(self, header): |
| 739 | """Check if a header has already been included. |
| 740 | |
| 741 | Args: |
| 742 | header: header to check. |
| 743 | Returns: |
| 744 | Line number of previous occurrence, or -1 if the header has not |
| 745 | been seen before. |
| 746 | """ |
| 747 | for section_list in self.include_list: |
| 748 | for f in section_list: |
| 749 | if f[0] == header: |
| 750 | return f[1] |
| 751 | return -1 |
| 752 | |
| 753 | def ResetSection(self, directive): |
| 754 | """Reset section checking for preprocessor directive. |
| 755 | |
| 756 | Args: |
| 757 | directive: preprocessor directive (e.g. "if", "else"). |
| 758 | """ |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 759 | # The name of the current section. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 760 | self._section = self._INITIAL_SECTION |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 761 | # The path of last found header. |
| 762 | self._last_header = '' |
| 763 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 764 | # Update list of includes. Note that we never pop from the |
| 765 | # include list. |
| 766 | if directive in ('if', 'ifdef', 'ifndef'): |
| 767 | self.include_list.append([]) |
| 768 | elif directive in ('else', 'elif'): |
| 769 | self.include_list[-1] = [] |
| 770 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 771 | def SetLastHeader(self, header_path): |
| 772 | self._last_header = header_path |
| 773 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 774 | def CanonicalizeAlphabeticalOrder(self, header_path): |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 775 | """Returns a path canonicalized for alphabetical comparison. |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 776 | |
| 777 | - replaces "-" with "_" so they both cmp the same. |
| 778 | - removes '-inl' since we don't require them to be after the main header. |
| 779 | - lowercase everything, just in case. |
| 780 | |
| 781 | Args: |
| 782 | header_path: Path to be canonicalized. |
| 783 | |
| 784 | Returns: |
| 785 | Canonicalized path. |
| 786 | """ |
| 787 | return header_path.replace('-inl.h', '.h').replace('-', '_').lower() |
| 788 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 789 | def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path): |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 790 | """Check if a header is in alphabetical order with the previous header. |
| 791 | |
| 792 | Args: |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 793 | clean_lines: A CleansedLines instance containing the file. |
| 794 | linenum: The number of the line to check. |
| 795 | header_path: Canonicalized header to be checked. |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 796 | |
| 797 | Returns: |
| 798 | Returns true if the header is in alphabetical order. |
| 799 | """ |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 800 | # If previous section is different from current section, _last_header will |
| 801 | # be reset to empty string, so it's always less than current header. |
| 802 | # |
| 803 | # If previous line was a blank line, assume that the headers are |
| 804 | # intentionally sorted the way they are. |
| 805 | if (self._last_header > header_path and |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 806 | Match(r'^\s*#\s*include\b', clean_lines.elided[linenum - 1])): |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 807 | return False |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 808 | return True |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 809 | |
| 810 | def CheckNextIncludeOrder(self, header_type): |
| 811 | """Returns a non-empty error message if the next header is out of order. |
| 812 | |
| 813 | This function also updates the internal state to be ready to check |
| 814 | the next include. |
| 815 | |
| 816 | Args: |
| 817 | header_type: One of the _XXX_HEADER constants defined above. |
| 818 | |
| 819 | Returns: |
| 820 | The empty string if the header is in the right order, or an |
| 821 | error message describing what's wrong. |
| 822 | |
| 823 | """ |
| 824 | error_message = ('Found %s after %s' % |
| 825 | (self._TYPE_NAMES[header_type], |
| 826 | self._SECTION_NAMES[self._section])) |
| 827 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 828 | last_section = self._section |
| 829 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 830 | if header_type == _C_SYS_HEADER: |
| 831 | if self._section <= self._C_SECTION: |
| 832 | self._section = self._C_SECTION |
| 833 | else: |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 834 | self._last_header = '' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 835 | return error_message |
| 836 | elif header_type == _CPP_SYS_HEADER: |
| 837 | if self._section <= self._CPP_SECTION: |
| 838 | self._section = self._CPP_SECTION |
| 839 | else: |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 840 | self._last_header = '' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 841 | return error_message |
| 842 | elif header_type == _LIKELY_MY_HEADER: |
| 843 | if self._section <= self._MY_H_SECTION: |
| 844 | self._section = self._MY_H_SECTION |
| 845 | else: |
| 846 | self._section = self._OTHER_H_SECTION |
| 847 | elif header_type == _POSSIBLE_MY_HEADER: |
| 848 | if self._section <= self._MY_H_SECTION: |
| 849 | self._section = self._MY_H_SECTION |
| 850 | else: |
| 851 | # This will always be the fallback because we're not sure |
| 852 | # enough that the header is associated with this file. |
| 853 | self._section = self._OTHER_H_SECTION |
| 854 | else: |
| 855 | assert header_type == _OTHER_HEADER |
| 856 | self._section = self._OTHER_H_SECTION |
| 857 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 858 | if last_section != self._section: |
| 859 | self._last_header = '' |
| 860 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 861 | return '' |
| 862 | |
| 863 | |
| 864 | class _CppLintState(object): |
| 865 | """Maintains module-wide state..""" |
| 866 | |
| 867 | def __init__(self): |
| 868 | self.verbose_level = 1 # global setting. |
| 869 | self.error_count = 0 # global count of reported errors |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 870 | # filters to apply when emitting error messages |
| 871 | self.filters = _DEFAULT_FILTERS[:] |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 872 | # backup of filter list. Used to restore the state after each file. |
| 873 | self._filters_backup = self.filters[:] |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 874 | self.counting = 'total' # In what way are we counting errors? |
| 875 | self.errors_by_category = {} # string to int dict storing error counts |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 876 | self.quiet = False # Suppress non-error messagess? |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 877 | |
| 878 | # output format: |
| 879 | # "emacs" - format that emacs can parse (default) |
| 880 | # "vs7" - format that Microsoft Visual Studio 7 can parse |
| 881 | self.output_format = 'emacs' |
| 882 | |
| 883 | def SetOutputFormat(self, output_format): |
| 884 | """Sets the output format for errors.""" |
| 885 | self.output_format = output_format |
| 886 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 887 | def SetQuiet(self, quiet): |
| 888 | """Sets the module's quiet settings, and returns the previous setting.""" |
| 889 | last_quiet = self.quiet |
| 890 | self.quiet = quiet |
| 891 | return last_quiet |
| 892 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 893 | def SetVerboseLevel(self, level): |
| 894 | """Sets the module's verbosity, and returns the previous setting.""" |
| 895 | last_verbose_level = self.verbose_level |
| 896 | self.verbose_level = level |
| 897 | return last_verbose_level |
| 898 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 899 | def SetCountingStyle(self, counting_style): |
| 900 | """Sets the module's counting options.""" |
| 901 | self.counting = counting_style |
| 902 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 903 | def SetFilters(self, filters): |
| 904 | """Sets the error-message filters. |
| 905 | |
| 906 | These filters are applied when deciding whether to emit a given |
| 907 | error message. |
| 908 | |
| 909 | Args: |
| 910 | filters: A string of comma-separated filters (eg "+whitespace/indent"). |
| 911 | Each filter should start with + or -; else we die. |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 912 | |
| 913 | Raises: |
| 914 | ValueError: The comma-separated filters did not all start with '+' or '-'. |
| 915 | E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter" |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 916 | """ |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 917 | # Default filters always have less priority than the flag ones. |
| 918 | self.filters = _DEFAULT_FILTERS[:] |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 919 | self.AddFilters(filters) |
| 920 | |
| 921 | def AddFilters(self, filters): |
| 922 | """ Adds more filters to the existing list of error-message filters. """ |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 923 | for filt in filters.split(','): |
| 924 | clean_filt = filt.strip() |
| 925 | if clean_filt: |
| 926 | self.filters.append(clean_filt) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 927 | for filt in self.filters: |
| 928 | if not (filt.startswith('+') or filt.startswith('-')): |
| 929 | raise ValueError('Every filter in --filters must start with + or -' |
| 930 | ' (%s does not)' % filt) |
| 931 | |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 932 | def BackupFilters(self): |
| 933 | """ Saves the current filter list to backup storage.""" |
| 934 | self._filters_backup = self.filters[:] |
| 935 | |
| 936 | def RestoreFilters(self): |
| 937 | """ Restores filters previously backed up.""" |
| 938 | self.filters = self._filters_backup[:] |
| 939 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 940 | def ResetErrorCounts(self): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 941 | """Sets the module's error statistic back to zero.""" |
| 942 | self.error_count = 0 |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 943 | self.errors_by_category = {} |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 944 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 945 | def IncrementErrorCount(self, category): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 946 | """Bumps the module's error statistic.""" |
| 947 | self.error_count += 1 |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 948 | if self.counting in ('toplevel', 'detailed'): |
| 949 | if self.counting != 'detailed': |
| 950 | category = category.split('/')[0] |
| 951 | if category not in self.errors_by_category: |
| 952 | self.errors_by_category[category] = 0 |
| 953 | self.errors_by_category[category] += 1 |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 954 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 955 | def PrintErrorCounts(self): |
| 956 | """Print a summary of errors by category, and the total.""" |
| 957 | for category, count in self.errors_by_category.iteritems(): |
| 958 | sys.stderr.write('Category \'%s\' errors found: %d\n' % |
| 959 | (category, count)) |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 960 | sys.stdout.write('Total errors found: %d\n' % self.error_count) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 961 | |
| 962 | _cpplint_state = _CppLintState() |
| 963 | |
| 964 | |
| 965 | def _OutputFormat(): |
| 966 | """Gets the module's output format.""" |
| 967 | return _cpplint_state.output_format |
| 968 | |
| 969 | |
| 970 | def _SetOutputFormat(output_format): |
| 971 | """Sets the module's output format.""" |
| 972 | _cpplint_state.SetOutputFormat(output_format) |
| 973 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 974 | def _Quiet(): |
| 975 | """Return's the module's quiet setting.""" |
| 976 | return _cpplint_state.quiet |
| 977 | |
| 978 | def _SetQuiet(quiet): |
| 979 | """Set the module's quiet status, and return previous setting.""" |
| 980 | return _cpplint_state.SetQuiet(quiet) |
| 981 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 982 | |
| 983 | def _VerboseLevel(): |
| 984 | """Returns the module's verbosity setting.""" |
| 985 | return _cpplint_state.verbose_level |
| 986 | |
| 987 | |
| 988 | def _SetVerboseLevel(level): |
| 989 | """Sets the module's verbosity, and returns the previous setting.""" |
| 990 | return _cpplint_state.SetVerboseLevel(level) |
| 991 | |
| 992 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 993 | def _SetCountingStyle(level): |
| 994 | """Sets the module's counting options.""" |
| 995 | _cpplint_state.SetCountingStyle(level) |
| 996 | |
| 997 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 998 | def _Filters(): |
| 999 | """Returns the module's list of output filters, as a list.""" |
| 1000 | return _cpplint_state.filters |
| 1001 | |
| 1002 | |
| 1003 | def _SetFilters(filters): |
| 1004 | """Sets the module's error-message filters. |
| 1005 | |
| 1006 | These filters are applied when deciding whether to emit a given |
| 1007 | error message. |
| 1008 | |
| 1009 | Args: |
| 1010 | filters: A string of comma-separated filters (eg "whitespace/indent"). |
| 1011 | Each filter should start with + or -; else we die. |
| 1012 | """ |
| 1013 | _cpplint_state.SetFilters(filters) |
| 1014 | |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 1015 | def _AddFilters(filters): |
| 1016 | """Adds more filter overrides. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 1017 | |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 1018 | Unlike _SetFilters, this function does not reset the current list of filters |
| 1019 | available. |
| 1020 | |
| 1021 | Args: |
| 1022 | filters: A string of comma-separated filters (eg "whitespace/indent"). |
| 1023 | Each filter should start with + or -; else we die. |
| 1024 | """ |
| 1025 | _cpplint_state.AddFilters(filters) |
| 1026 | |
| 1027 | def _BackupFilters(): |
| 1028 | """ Saves the current filter list to backup storage.""" |
| 1029 | _cpplint_state.BackupFilters() |
| 1030 | |
| 1031 | def _RestoreFilters(): |
| 1032 | """ Restores filters previously backed up.""" |
| 1033 | _cpplint_state.RestoreFilters() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1034 | |
| 1035 | class _FunctionState(object): |
| 1036 | """Tracks current function name and the number of lines in its body.""" |
| 1037 | |
| 1038 | _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc. |
| 1039 | _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER. |
| 1040 | |
| 1041 | def __init__(self): |
| 1042 | self.in_a_function = False |
| 1043 | self.lines_in_function = 0 |
| 1044 | self.current_function = '' |
| 1045 | |
| 1046 | def Begin(self, function_name): |
| 1047 | """Start analyzing function body. |
| 1048 | |
| 1049 | Args: |
| 1050 | function_name: The name of the function being tracked. |
| 1051 | """ |
| 1052 | self.in_a_function = True |
| 1053 | self.lines_in_function = 0 |
| 1054 | self.current_function = function_name |
| 1055 | |
| 1056 | def Count(self): |
| 1057 | """Count line in current function body.""" |
| 1058 | if self.in_a_function: |
| 1059 | self.lines_in_function += 1 |
| 1060 | |
| 1061 | def Check(self, error, filename, linenum): |
| 1062 | """Report if too many lines in function body. |
| 1063 | |
| 1064 | Args: |
| 1065 | error: The function to call with any errors found. |
| 1066 | filename: The name of the current file. |
| 1067 | linenum: The number of the line to check. |
| 1068 | """ |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 1069 | if not self.in_a_function: |
| 1070 | return |
| 1071 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1072 | if Match(r'T(EST|est)', self.current_function): |
| 1073 | base_trigger = self._TEST_TRIGGER |
| 1074 | else: |
| 1075 | base_trigger = self._NORMAL_TRIGGER |
| 1076 | trigger = base_trigger * 2**_VerboseLevel() |
| 1077 | |
| 1078 | if self.lines_in_function > trigger: |
| 1079 | error_level = int(math.log(self.lines_in_function / base_trigger, 2)) |
| 1080 | # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... |
| 1081 | if error_level > 5: |
| 1082 | error_level = 5 |
| 1083 | error(filename, linenum, 'readability/fn_size', error_level, |
| 1084 | 'Small and focused functions are preferred:' |
| 1085 | ' %s has %d non-comment lines' |
| 1086 | ' (error triggered by exceeding %d lines).' % ( |
| 1087 | self.current_function, self.lines_in_function, trigger)) |
| 1088 | |
| 1089 | def End(self): |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 1090 | """Stop analyzing function body.""" |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1091 | self.in_a_function = False |
| 1092 | |
| 1093 | |
| 1094 | class _IncludeError(Exception): |
| 1095 | """Indicates a problem with the include order in a file.""" |
| 1096 | pass |
| 1097 | |
| 1098 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 1099 | class FileInfo(object): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1100 | """Provides utility functions for filenames. |
| 1101 | |
| 1102 | FileInfo provides easy access to the components of a file's path |
| 1103 | relative to the project root. |
| 1104 | """ |
| 1105 | |
| 1106 | def __init__(self, filename): |
| 1107 | self._filename = filename |
| 1108 | |
| 1109 | def FullName(self): |
| 1110 | """Make Windows paths like Unix.""" |
| 1111 | return os.path.abspath(self._filename).replace('\\', '/') |
| 1112 | |
| 1113 | def RepositoryName(self): |
| 1114 | """FullName after removing the local path to the repository. |
| 1115 | |
| 1116 | If we have a real absolute path name here we can try to do something smart: |
| 1117 | detecting the root of the checkout and truncating /path/to/checkout from |
| 1118 | the name so that we get header guards that don't include things like |
| 1119 | "C:\Documents and Settings\..." or "/home/username/..." in them and thus |
| 1120 | people on different computers who have checked the source out to different |
| 1121 | locations won't see bogus errors. |
| 1122 | """ |
| 1123 | fullname = self.FullName() |
| 1124 | |
| 1125 | if os.path.exists(fullname): |
| 1126 | project_dir = os.path.dirname(fullname) |
| 1127 | |
| 1128 | if os.path.exists(os.path.join(project_dir, ".svn")): |
| 1129 | # If there's a .svn file in the current directory, we recursively look |
| 1130 | # up the directory tree for the top of the SVN checkout |
| 1131 | root_dir = project_dir |
| 1132 | one_up_dir = os.path.dirname(root_dir) |
| 1133 | while os.path.exists(os.path.join(one_up_dir, ".svn")): |
| 1134 | root_dir = os.path.dirname(root_dir) |
| 1135 | one_up_dir = os.path.dirname(one_up_dir) |
| 1136 | |
| 1137 | prefix = os.path.commonprefix([root_dir, project_dir]) |
| 1138 | return fullname[len(prefix) + 1:] |
| 1139 | |
erg@chromium.org | 7956a87 | 2011-11-30 01:44:03 +0000 | [diff] [blame] | 1140 | # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by |
| 1141 | # searching up from the current path. |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 1142 | root_dir = current_dir = os.path.dirname(fullname) |
| 1143 | while current_dir != os.path.dirname(current_dir): |
| 1144 | if (os.path.exists(os.path.join(current_dir, ".git")) or |
| 1145 | os.path.exists(os.path.join(current_dir, ".hg")) or |
| 1146 | os.path.exists(os.path.join(current_dir, ".svn"))): |
| 1147 | root_dir = current_dir |
| 1148 | current_dir = os.path.dirname(current_dir) |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 1149 | |
| 1150 | if (os.path.exists(os.path.join(root_dir, ".git")) or |
erg@chromium.org | 7956a87 | 2011-11-30 01:44:03 +0000 | [diff] [blame] | 1151 | os.path.exists(os.path.join(root_dir, ".hg")) or |
| 1152 | os.path.exists(os.path.join(root_dir, ".svn"))): |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 1153 | prefix = os.path.commonprefix([root_dir, project_dir]) |
| 1154 | return fullname[len(prefix) + 1:] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1155 | |
| 1156 | # Don't know what to do; header guard warnings may be wrong... |
| 1157 | return fullname |
| 1158 | |
| 1159 | def Split(self): |
| 1160 | """Splits the file into the directory, basename, and extension. |
| 1161 | |
| 1162 | For 'chrome/browser/browser.cc', Split() would |
| 1163 | return ('chrome/browser', 'browser', '.cc') |
| 1164 | |
| 1165 | Returns: |
| 1166 | A tuple of (directory, basename, extension). |
| 1167 | """ |
| 1168 | |
| 1169 | googlename = self.RepositoryName() |
| 1170 | project, rest = os.path.split(googlename) |
| 1171 | return (project,) + os.path.splitext(rest) |
| 1172 | |
| 1173 | def BaseName(self): |
| 1174 | """File base name - text after the final slash, before the final period.""" |
| 1175 | return self.Split()[1] |
| 1176 | |
| 1177 | def Extension(self): |
| 1178 | """File extension - text following the final period.""" |
| 1179 | return self.Split()[2] |
| 1180 | |
| 1181 | def NoExtension(self): |
| 1182 | """File has no source file extension.""" |
| 1183 | return '/'.join(self.Split()[0:2]) |
| 1184 | |
| 1185 | def IsSource(self): |
| 1186 | """File has a source file extension.""" |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 1187 | return _IsSourceExtension(self.Extension()[1:]) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1188 | |
| 1189 | |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 1190 | def _ShouldPrintError(category, confidence, linenum): |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 1191 | """If confidence >= verbose, category passes filter and is not suppressed.""" |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 1192 | |
| 1193 | # There are three ways we might decide not to print an error message: |
| 1194 | # a "NOLINT(category)" comment appears in the source, |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1195 | # the verbosity level isn't high enough, or the filters filter it out. |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 1196 | if IsErrorSuppressedByNolint(category, linenum): |
| 1197 | return False |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1198 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1199 | if confidence < _cpplint_state.verbose_level: |
| 1200 | return False |
| 1201 | |
| 1202 | is_filtered = False |
| 1203 | for one_filter in _Filters(): |
| 1204 | if one_filter.startswith('-'): |
| 1205 | if category.startswith(one_filter[1:]): |
| 1206 | is_filtered = True |
| 1207 | elif one_filter.startswith('+'): |
| 1208 | if category.startswith(one_filter[1:]): |
| 1209 | is_filtered = False |
| 1210 | else: |
| 1211 | assert False # should have been checked for in SetFilter. |
| 1212 | if is_filtered: |
| 1213 | return False |
| 1214 | |
| 1215 | return True |
| 1216 | |
| 1217 | |
| 1218 | def Error(filename, linenum, category, confidence, message): |
| 1219 | """Logs the fact we've found a lint error. |
| 1220 | |
| 1221 | We log where the error was found, and also our confidence in the error, |
| 1222 | that is, how certain we are this is a legitimate style regression, and |
| 1223 | not a misidentification or a use that's sometimes justified. |
| 1224 | |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 1225 | False positives can be suppressed by the use of |
| 1226 | "cpplint(category)" comments on the offending line. These are |
| 1227 | parsed into _error_suppressions. |
| 1228 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1229 | Args: |
| 1230 | filename: The name of the file containing the error. |
| 1231 | linenum: The number of the line containing the error. |
| 1232 | category: A string used to describe the "category" this bug |
| 1233 | falls under: "whitespace", say, or "runtime". Categories |
| 1234 | may have a hierarchy separated by slashes: "whitespace/indent". |
| 1235 | confidence: A number from 1-5 representing a confidence score for |
| 1236 | the error, with 5 meaning that we are certain of the problem, |
| 1237 | and 1 meaning that it could be a legitimate construct. |
| 1238 | message: The error message. |
| 1239 | """ |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 1240 | if _ShouldPrintError(category, confidence, linenum): |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 1241 | _cpplint_state.IncrementErrorCount(category) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1242 | if _cpplint_state.output_format == 'vs7': |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 1243 | sys.stderr.write('%s(%s): error cpplint: [%s] %s [%d]\n' % ( |
| 1244 | filename, linenum, category, message, confidence)) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1245 | elif _cpplint_state.output_format == 'eclipse': |
| 1246 | sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % ( |
| 1247 | filename, linenum, message, category, confidence)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1248 | else: |
| 1249 | sys.stderr.write('%s:%s: %s [%s] [%d]\n' % ( |
| 1250 | filename, linenum, message, category, confidence)) |
| 1251 | |
| 1252 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1253 | # Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1254 | _RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile( |
| 1255 | r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)') |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1256 | # Match a single C style comment on the same line. |
| 1257 | _RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/' |
| 1258 | # Matches multi-line C style comments. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1259 | # This RE is a little bit more complicated than one might expect, because we |
| 1260 | # have to take care of space removals tools so we can handle comments inside |
| 1261 | # statements better. |
| 1262 | # The current rule is: We only clear spaces from both sides when we're at the |
| 1263 | # end of the line. Otherwise, we try to remove spaces from the right side, |
| 1264 | # if this doesn't work we try on left side but only if there's a non-character |
| 1265 | # on the right. |
| 1266 | _RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile( |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1267 | r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' + |
| 1268 | _RE_PATTERN_C_COMMENTS + r'\s+|' + |
| 1269 | r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' + |
| 1270 | _RE_PATTERN_C_COMMENTS + r')') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1271 | |
| 1272 | |
| 1273 | def IsCppString(line): |
| 1274 | """Does line terminate so, that the next symbol is in string constant. |
| 1275 | |
| 1276 | This function does not consider single-line nor multi-line comments. |
| 1277 | |
| 1278 | Args: |
| 1279 | line: is a partial line of code starting from the 0..n. |
| 1280 | |
| 1281 | Returns: |
| 1282 | True, if next character appended to 'line' is inside a |
| 1283 | string constant. |
| 1284 | """ |
| 1285 | |
| 1286 | line = line.replace(r'\\', 'XX') # after this, \\" does not match to \" |
| 1287 | return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1 |
| 1288 | |
| 1289 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1290 | def CleanseRawStrings(raw_lines): |
| 1291 | """Removes C++11 raw strings from lines. |
| 1292 | |
| 1293 | Before: |
| 1294 | static const char kData[] = R"( |
| 1295 | multi-line string |
| 1296 | )"; |
| 1297 | |
| 1298 | After: |
| 1299 | static const char kData[] = "" |
| 1300 | (replaced by blank line) |
| 1301 | ""; |
| 1302 | |
| 1303 | Args: |
| 1304 | raw_lines: list of raw lines. |
| 1305 | |
| 1306 | Returns: |
| 1307 | list of lines with C++11 raw strings replaced by empty strings. |
| 1308 | """ |
| 1309 | |
| 1310 | delimiter = None |
| 1311 | lines_without_raw_strings = [] |
| 1312 | for line in raw_lines: |
| 1313 | if delimiter: |
| 1314 | # Inside a raw string, look for the end |
| 1315 | end = line.find(delimiter) |
| 1316 | if end >= 0: |
| 1317 | # Found the end of the string, match leading space for this |
| 1318 | # line and resume copying the original lines, and also insert |
| 1319 | # a "" on the last line. |
| 1320 | leading_space = Match(r'^(\s*)\S', line) |
| 1321 | line = leading_space.group(1) + '""' + line[end + len(delimiter):] |
| 1322 | delimiter = None |
| 1323 | else: |
| 1324 | # Haven't found the end yet, append a blank line. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1325 | line = '""' |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1326 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1327 | # Look for beginning of a raw string, and replace them with |
| 1328 | # empty strings. This is done in a loop to handle multiple raw |
| 1329 | # strings on the same line. |
| 1330 | while delimiter is None: |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1331 | # Look for beginning of a raw string. |
| 1332 | # See 2.14.15 [lex.string] for syntax. |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 1333 | # |
| 1334 | # Once we have matched a raw string, we check the prefix of the |
| 1335 | # line to make sure that the line is not part of a single line |
| 1336 | # comment. It's done this way because we remove raw strings |
| 1337 | # before removing comments as opposed to removing comments |
| 1338 | # before removing raw strings. This is because there are some |
| 1339 | # cpplint checks that requires the comments to be preserved, but |
| 1340 | # we don't want to check comments that are inside raw strings. |
| 1341 | matched = Match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line) |
| 1342 | if (matched and |
| 1343 | not Match(r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//', |
| 1344 | matched.group(1))): |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1345 | delimiter = ')' + matched.group(2) + '"' |
| 1346 | |
| 1347 | end = matched.group(3).find(delimiter) |
| 1348 | if end >= 0: |
| 1349 | # Raw string ended on same line |
| 1350 | line = (matched.group(1) + '""' + |
| 1351 | matched.group(3)[end + len(delimiter):]) |
| 1352 | delimiter = None |
| 1353 | else: |
| 1354 | # Start of a multi-line raw string |
| 1355 | line = matched.group(1) + '""' |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1356 | else: |
| 1357 | break |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1358 | |
| 1359 | lines_without_raw_strings.append(line) |
| 1360 | |
| 1361 | # TODO(unknown): if delimiter is not None here, we might want to |
| 1362 | # emit a warning for unterminated string. |
| 1363 | return lines_without_raw_strings |
| 1364 | |
| 1365 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1366 | def FindNextMultiLineCommentStart(lines, lineix): |
| 1367 | """Find the beginning marker for a multiline comment.""" |
| 1368 | while lineix < len(lines): |
| 1369 | if lines[lineix].strip().startswith('/*'): |
| 1370 | # Only return this marker if the comment goes beyond this line |
| 1371 | if lines[lineix].strip().find('*/', 2) < 0: |
| 1372 | return lineix |
| 1373 | lineix += 1 |
| 1374 | return len(lines) |
| 1375 | |
| 1376 | |
| 1377 | def FindNextMultiLineCommentEnd(lines, lineix): |
| 1378 | """We are inside a comment, find the end marker.""" |
| 1379 | while lineix < len(lines): |
| 1380 | if lines[lineix].strip().endswith('*/'): |
| 1381 | return lineix |
| 1382 | lineix += 1 |
| 1383 | return len(lines) |
| 1384 | |
| 1385 | |
| 1386 | def RemoveMultiLineCommentsFromRange(lines, begin, end): |
| 1387 | """Clears a range of lines for multi-line comments.""" |
| 1388 | # Having // dummy comments makes the lines non-empty, so we will not get |
| 1389 | # unnecessary blank line warnings later in the code. |
| 1390 | for i in range(begin, end): |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1391 | lines[i] = '/**/' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1392 | |
| 1393 | |
| 1394 | def RemoveMultiLineComments(filename, lines, error): |
| 1395 | """Removes multiline (c-style) comments from lines.""" |
| 1396 | lineix = 0 |
| 1397 | while lineix < len(lines): |
| 1398 | lineix_begin = FindNextMultiLineCommentStart(lines, lineix) |
| 1399 | if lineix_begin >= len(lines): |
| 1400 | return |
| 1401 | lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin) |
| 1402 | if lineix_end >= len(lines): |
| 1403 | error(filename, lineix_begin + 1, 'readability/multiline_comment', 5, |
| 1404 | 'Could not find end of multi-line comment') |
| 1405 | return |
| 1406 | RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1) |
| 1407 | lineix = lineix_end + 1 |
| 1408 | |
| 1409 | |
| 1410 | def CleanseComments(line): |
| 1411 | """Removes //-comments and single-line C-style /* */ comments. |
| 1412 | |
| 1413 | Args: |
| 1414 | line: A line of C++ source. |
| 1415 | |
| 1416 | Returns: |
| 1417 | The line with single-line comments removed. |
| 1418 | """ |
| 1419 | commentpos = line.find('//') |
| 1420 | if commentpos != -1 and not IsCppString(line[:commentpos]): |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 1421 | line = line[:commentpos].rstrip() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1422 | # get rid of /* ... */ |
| 1423 | return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line) |
| 1424 | |
| 1425 | |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 1426 | class CleansedLines(object): |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1427 | """Holds 4 copies of all lines with different preprocessing applied to them. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1428 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1429 | 1) elided member contains lines without strings and comments. |
| 1430 | 2) lines member contains lines without comments. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1431 | 3) raw_lines member contains all the lines without processing. |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1432 | 4) lines_without_raw_strings member is same as raw_lines, but with C++11 raw |
| 1433 | strings removed. |
| 1434 | All these members are of <type 'list'>, and of the same length. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1435 | """ |
| 1436 | |
| 1437 | def __init__(self, lines): |
| 1438 | self.elided = [] |
| 1439 | self.lines = [] |
| 1440 | self.raw_lines = lines |
| 1441 | self.num_lines = len(lines) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1442 | self.lines_without_raw_strings = CleanseRawStrings(lines) |
| 1443 | for linenum in range(len(self.lines_without_raw_strings)): |
| 1444 | self.lines.append(CleanseComments( |
| 1445 | self.lines_without_raw_strings[linenum])) |
| 1446 | elided = self._CollapseStrings(self.lines_without_raw_strings[linenum]) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1447 | self.elided.append(CleanseComments(elided)) |
| 1448 | |
| 1449 | def NumLines(self): |
| 1450 | """Returns the number of lines represented.""" |
| 1451 | return self.num_lines |
| 1452 | |
| 1453 | @staticmethod |
| 1454 | def _CollapseStrings(elided): |
| 1455 | """Collapses strings and chars on a line to simple "" or '' blocks. |
| 1456 | |
| 1457 | We nix strings first so we're not fooled by text like '"http://"' |
| 1458 | |
| 1459 | Args: |
| 1460 | elided: The line being processed. |
| 1461 | |
| 1462 | Returns: |
| 1463 | The line with collapsed strings. |
| 1464 | """ |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1465 | if _RE_PATTERN_INCLUDE.match(elided): |
| 1466 | return elided |
| 1467 | |
| 1468 | # Remove escaped characters first to make quote/single quote collapsing |
| 1469 | # basic. Things that look like escaped characters shouldn't occur |
| 1470 | # outside of strings and chars. |
| 1471 | elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided) |
| 1472 | |
| 1473 | # Replace quoted strings and digit separators. Both single quotes |
| 1474 | # and double quotes are processed in the same loop, otherwise |
| 1475 | # nested quotes wouldn't work. |
| 1476 | collapsed = '' |
| 1477 | while True: |
| 1478 | # Find the first quote character |
| 1479 | match = Match(r'^([^\'"]*)([\'"])(.*)$', elided) |
| 1480 | if not match: |
| 1481 | collapsed += elided |
| 1482 | break |
| 1483 | head, quote, tail = match.groups() |
| 1484 | |
| 1485 | if quote == '"': |
| 1486 | # Collapse double quoted strings |
| 1487 | second_quote = tail.find('"') |
| 1488 | if second_quote >= 0: |
| 1489 | collapsed += head + '""' |
| 1490 | elided = tail[second_quote + 1:] |
| 1491 | else: |
| 1492 | # Unmatched double quote, don't bother processing the rest |
| 1493 | # of the line since this is probably a multiline string. |
| 1494 | collapsed += elided |
| 1495 | break |
| 1496 | else: |
| 1497 | # Found single quote, check nearby text to eliminate digit separators. |
| 1498 | # |
| 1499 | # There is no special handling for floating point here, because |
| 1500 | # the integer/fractional/exponent parts would all be parsed |
| 1501 | # correctly as long as there are digits on both sides of the |
| 1502 | # separator. So we are fine as long as we don't see something |
| 1503 | # like "0.'3" (gcc 4.9.0 will not allow this literal). |
| 1504 | if Search(r'\b(?:0[bBxX]?|[1-9])[0-9a-fA-F]*$', head): |
| 1505 | match_literal = Match(r'^((?:\'?[0-9a-zA-Z_])*)(.*)$', "'" + tail) |
| 1506 | collapsed += head + match_literal.group(1).replace("'", '') |
| 1507 | elided = match_literal.group(2) |
| 1508 | else: |
| 1509 | second_quote = tail.find('\'') |
| 1510 | if second_quote >= 0: |
| 1511 | collapsed += head + "''" |
| 1512 | elided = tail[second_quote + 1:] |
| 1513 | else: |
| 1514 | # Unmatched single quote |
| 1515 | collapsed += elided |
| 1516 | break |
| 1517 | |
| 1518 | return collapsed |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1519 | |
| 1520 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1521 | def FindEndOfExpressionInLine(line, startpos, stack): |
| 1522 | """Find the position just after the end of current parenthesized expression. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1523 | |
| 1524 | Args: |
| 1525 | line: a CleansedLines line. |
| 1526 | startpos: start searching at this position. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1527 | stack: nesting stack at startpos. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1528 | |
| 1529 | Returns: |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1530 | On finding matching end: (index just after matching end, None) |
| 1531 | On finding an unclosed expression: (-1, None) |
| 1532 | Otherwise: (-1, new stack at end of this line) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1533 | """ |
| 1534 | for i in xrange(startpos, len(line)): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1535 | char = line[i] |
| 1536 | if char in '([{': |
| 1537 | # Found start of parenthesized expression, push to expression stack |
| 1538 | stack.append(char) |
| 1539 | elif char == '<': |
| 1540 | # Found potential start of template argument list |
| 1541 | if i > 0 and line[i - 1] == '<': |
| 1542 | # Left shift operator |
| 1543 | if stack and stack[-1] == '<': |
| 1544 | stack.pop() |
| 1545 | if not stack: |
| 1546 | return (-1, None) |
| 1547 | elif i > 0 and Search(r'\boperator\s*$', line[0:i]): |
| 1548 | # operator<, don't add to stack |
| 1549 | continue |
| 1550 | else: |
| 1551 | # Tentative start of template argument list |
| 1552 | stack.append('<') |
| 1553 | elif char in ')]}': |
| 1554 | # Found end of parenthesized expression. |
| 1555 | # |
| 1556 | # If we are currently expecting a matching '>', the pending '<' |
| 1557 | # must have been an operator. Remove them from expression stack. |
| 1558 | while stack and stack[-1] == '<': |
| 1559 | stack.pop() |
| 1560 | if not stack: |
| 1561 | return (-1, None) |
| 1562 | if ((stack[-1] == '(' and char == ')') or |
| 1563 | (stack[-1] == '[' and char == ']') or |
| 1564 | (stack[-1] == '{' and char == '}')): |
| 1565 | stack.pop() |
| 1566 | if not stack: |
| 1567 | return (i + 1, None) |
| 1568 | else: |
| 1569 | # Mismatched parentheses |
| 1570 | return (-1, None) |
| 1571 | elif char == '>': |
| 1572 | # Found potential end of template argument list. |
| 1573 | |
| 1574 | # Ignore "->" and operator functions |
| 1575 | if (i > 0 and |
| 1576 | (line[i - 1] == '-' or Search(r'\boperator\s*$', line[0:i - 1]))): |
| 1577 | continue |
| 1578 | |
| 1579 | # Pop the stack if there is a matching '<'. Otherwise, ignore |
| 1580 | # this '>' since it must be an operator. |
| 1581 | if stack: |
| 1582 | if stack[-1] == '<': |
| 1583 | stack.pop() |
| 1584 | if not stack: |
| 1585 | return (i + 1, None) |
| 1586 | elif char == ';': |
| 1587 | # Found something that look like end of statements. If we are currently |
| 1588 | # expecting a '>', the matching '<' must have been an operator, since |
| 1589 | # template argument list should not contain statements. |
| 1590 | while stack and stack[-1] == '<': |
| 1591 | stack.pop() |
| 1592 | if not stack: |
| 1593 | return (-1, None) |
| 1594 | |
| 1595 | # Did not find end of expression or unbalanced parentheses on this line |
| 1596 | return (-1, stack) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1597 | |
| 1598 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1599 | def CloseExpression(clean_lines, linenum, pos): |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1600 | """If input points to ( or { or [ or <, finds the position that closes it. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1601 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1602 | If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1603 | linenum/pos that correspond to the closing of the expression. |
| 1604 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1605 | TODO(unknown): cpplint spends a fair bit of time matching parentheses. |
| 1606 | Ideally we would want to index all opening and closing parentheses once |
| 1607 | and have CloseExpression be just a simple lookup, but due to preprocessor |
| 1608 | tricks, this is not so easy. |
| 1609 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1610 | Args: |
| 1611 | clean_lines: A CleansedLines instance containing the file. |
| 1612 | linenum: The number of the line to check. |
| 1613 | pos: A position on the line. |
| 1614 | |
| 1615 | Returns: |
| 1616 | A tuple (line, linenum, pos) pointer *past* the closing brace, or |
| 1617 | (line, len(lines), -1) if we never find a close. Note we ignore |
| 1618 | strings and comments when matching; and the line we return is the |
| 1619 | 'cleansed' line at linenum. |
| 1620 | """ |
| 1621 | |
| 1622 | line = clean_lines.elided[linenum] |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1623 | if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1624 | return (line, clean_lines.NumLines(), -1) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1625 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1626 | # Check first line |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1627 | (end_pos, stack) = FindEndOfExpressionInLine(line, pos, []) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1628 | if end_pos > -1: |
| 1629 | return (line, linenum, end_pos) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1630 | |
| 1631 | # Continue scanning forward |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1632 | while stack and linenum < clean_lines.NumLines() - 1: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1633 | linenum += 1 |
| 1634 | line = clean_lines.elided[linenum] |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1635 | (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1636 | if end_pos > -1: |
| 1637 | return (line, linenum, end_pos) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1638 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1639 | # Did not find end of expression before end of file, give up |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1640 | return (line, clean_lines.NumLines(), -1) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1641 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1642 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1643 | def FindStartOfExpressionInLine(line, endpos, stack): |
| 1644 | """Find position at the matching start of current expression. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1645 | |
| 1646 | This is almost the reverse of FindEndOfExpressionInLine, but note |
| 1647 | that the input position and returned position differs by 1. |
| 1648 | |
| 1649 | Args: |
| 1650 | line: a CleansedLines line. |
| 1651 | endpos: start searching at this position. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1652 | stack: nesting stack at endpos. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1653 | |
| 1654 | Returns: |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1655 | On finding matching start: (index at matching start, None) |
| 1656 | On finding an unclosed expression: (-1, None) |
| 1657 | Otherwise: (-1, new stack at beginning of this line) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1658 | """ |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1659 | i = endpos |
| 1660 | while i >= 0: |
| 1661 | char = line[i] |
| 1662 | if char in ')]}': |
| 1663 | # Found end of expression, push to expression stack |
| 1664 | stack.append(char) |
| 1665 | elif char == '>': |
| 1666 | # Found potential end of template argument list. |
| 1667 | # |
| 1668 | # Ignore it if it's a "->" or ">=" or "operator>" |
| 1669 | if (i > 0 and |
| 1670 | (line[i - 1] == '-' or |
| 1671 | Match(r'\s>=\s', line[i - 1:]) or |
| 1672 | Search(r'\boperator\s*$', line[0:i]))): |
| 1673 | i -= 1 |
| 1674 | else: |
| 1675 | stack.append('>') |
| 1676 | elif char == '<': |
| 1677 | # Found potential start of template argument list |
| 1678 | if i > 0 and line[i - 1] == '<': |
| 1679 | # Left shift operator |
| 1680 | i -= 1 |
| 1681 | else: |
| 1682 | # If there is a matching '>', we can pop the expression stack. |
| 1683 | # Otherwise, ignore this '<' since it must be an operator. |
| 1684 | if stack and stack[-1] == '>': |
| 1685 | stack.pop() |
| 1686 | if not stack: |
| 1687 | return (i, None) |
| 1688 | elif char in '([{': |
| 1689 | # Found start of expression. |
| 1690 | # |
| 1691 | # If there are any unmatched '>' on the stack, they must be |
| 1692 | # operators. Remove those. |
| 1693 | while stack and stack[-1] == '>': |
| 1694 | stack.pop() |
| 1695 | if not stack: |
| 1696 | return (-1, None) |
| 1697 | if ((char == '(' and stack[-1] == ')') or |
| 1698 | (char == '[' and stack[-1] == ']') or |
| 1699 | (char == '{' and stack[-1] == '}')): |
| 1700 | stack.pop() |
| 1701 | if not stack: |
| 1702 | return (i, None) |
| 1703 | else: |
| 1704 | # Mismatched parentheses |
| 1705 | return (-1, None) |
| 1706 | elif char == ';': |
| 1707 | # Found something that look like end of statements. If we are currently |
| 1708 | # expecting a '<', the matching '>' must have been an operator, since |
| 1709 | # template argument list should not contain statements. |
| 1710 | while stack and stack[-1] == '>': |
| 1711 | stack.pop() |
| 1712 | if not stack: |
| 1713 | return (-1, None) |
| 1714 | |
| 1715 | i -= 1 |
| 1716 | |
| 1717 | return (-1, stack) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1718 | |
| 1719 | |
| 1720 | def ReverseCloseExpression(clean_lines, linenum, pos): |
| 1721 | """If input points to ) or } or ] or >, finds the position that opens it. |
| 1722 | |
| 1723 | If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the |
| 1724 | linenum/pos that correspond to the opening of the expression. |
| 1725 | |
| 1726 | Args: |
| 1727 | clean_lines: A CleansedLines instance containing the file. |
| 1728 | linenum: The number of the line to check. |
| 1729 | pos: A position on the line. |
| 1730 | |
| 1731 | Returns: |
| 1732 | A tuple (line, linenum, pos) pointer *at* the opening brace, or |
| 1733 | (line, 0, -1) if we never find the matching opening brace. Note |
| 1734 | we ignore strings and comments when matching; and the line we |
| 1735 | return is the 'cleansed' line at linenum. |
| 1736 | """ |
| 1737 | line = clean_lines.elided[linenum] |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1738 | if line[pos] not in ')}]>': |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1739 | return (line, 0, -1) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1740 | |
| 1741 | # Check last line |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1742 | (start_pos, stack) = FindStartOfExpressionInLine(line, pos, []) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1743 | if start_pos > -1: |
| 1744 | return (line, linenum, start_pos) |
| 1745 | |
| 1746 | # Continue scanning backward |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1747 | while stack and linenum > 0: |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1748 | linenum -= 1 |
| 1749 | line = clean_lines.elided[linenum] |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1750 | (start_pos, stack) = FindStartOfExpressionInLine(line, len(line) - 1, stack) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1751 | if start_pos > -1: |
| 1752 | return (line, linenum, start_pos) |
| 1753 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1754 | # Did not find start of expression before beginning of file, give up |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 1755 | return (line, 0, -1) |
| 1756 | |
| 1757 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1758 | def CheckForCopyright(filename, lines, error): |
| 1759 | """Logs an error if no Copyright message appears at the top of the file.""" |
| 1760 | |
| 1761 | # We'll say it should occur by line 10. Don't forget there's a |
| 1762 | # dummy line at the front. |
| 1763 | for line in xrange(1, min(len(lines), 11)): |
| 1764 | if re.search(r'Copyright', lines[line], re.I): break |
| 1765 | else: # means no copyright line was found |
| 1766 | error(filename, 0, 'legal/copyright', 5, |
| 1767 | 'No copyright message found. ' |
| 1768 | 'You should have a line: "Copyright [year] <Copyright Owner>"') |
| 1769 | |
| 1770 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1771 | def GetIndentLevel(line): |
| 1772 | """Return the number of leading spaces in line. |
| 1773 | |
| 1774 | Args: |
| 1775 | line: A string to check. |
| 1776 | |
| 1777 | Returns: |
| 1778 | An integer count of leading spaces, possibly zero. |
| 1779 | """ |
| 1780 | indent = Match(r'^( *)\S', line) |
| 1781 | if indent: |
| 1782 | return len(indent.group(1)) |
| 1783 | else: |
| 1784 | return 0 |
| 1785 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 1786 | def PathSplitToList(path): |
| 1787 | """Returns the path split into a list by the separator. |
| 1788 | |
| 1789 | Args: |
| 1790 | path: An absolute or relative path (e.g. '/a/b/c/' or '../a') |
| 1791 | |
| 1792 | Returns: |
| 1793 | A list of path components (e.g. ['a', 'b', 'c]). |
| 1794 | """ |
| 1795 | lst = [] |
| 1796 | while True: |
| 1797 | (head, tail) = os.path.split(path) |
| 1798 | if head == path: # absolute paths end |
| 1799 | lst.append(head) |
| 1800 | break |
| 1801 | if tail == path: # relative paths end |
| 1802 | lst.append(tail) |
| 1803 | break |
| 1804 | |
| 1805 | path = head |
| 1806 | lst.append(tail) |
| 1807 | |
| 1808 | lst.reverse() |
| 1809 | return lst |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 1810 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1811 | def GetHeaderGuardCPPVariable(filename): |
| 1812 | """Returns the CPP variable that should be used as a header guard. |
| 1813 | |
| 1814 | Args: |
| 1815 | filename: The name of a C++ header file. |
| 1816 | |
| 1817 | Returns: |
| 1818 | The CPP variable that should be used as a header guard in the |
| 1819 | named file. |
| 1820 | |
| 1821 | """ |
| 1822 | |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 1823 | # Restores original filename in case that cpplint is invoked from Emacs's |
| 1824 | # flymake. |
| 1825 | filename = re.sub(r'_flymake\.h$', '.h', filename) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1826 | filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename) |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1827 | # Replace 'c++' with 'cpp'. |
| 1828 | filename = filename.replace('C++', 'cpp').replace('c++', 'cpp') |
skym@chromium.org | 3990c41 | 2016-02-05 20:55:12 +0000 | [diff] [blame] | 1829 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1830 | fileinfo = FileInfo(filename) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 1831 | file_path_from_root = fileinfo.RepositoryName() |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 1832 | |
| 1833 | def FixupPathFromRoot(): |
| 1834 | if _root_debug: |
| 1835 | sys.stderr.write("\n_root fixup, _root = '%s', repository name = '%s'\n" |
| 1836 | %(_root, fileinfo.RepositoryName())) |
| 1837 | |
| 1838 | # Process the file path with the --root flag if it was set. |
| 1839 | if not _root: |
| 1840 | if _root_debug: |
| 1841 | sys.stderr.write("_root unspecified\n") |
| 1842 | return file_path_from_root |
| 1843 | |
| 1844 | def StripListPrefix(lst, prefix): |
| 1845 | # f(['x', 'y'], ['w, z']) -> None (not a valid prefix) |
| 1846 | if lst[:len(prefix)] != prefix: |
| 1847 | return None |
| 1848 | # f(['a, 'b', 'c', 'd'], ['a', 'b']) -> ['c', 'd'] |
| 1849 | return lst[(len(prefix)):] |
| 1850 | |
| 1851 | # root behavior: |
| 1852 | # --root=subdir , lstrips subdir from the header guard |
| 1853 | maybe_path = StripListPrefix(PathSplitToList(file_path_from_root), |
| 1854 | PathSplitToList(_root)) |
| 1855 | |
| 1856 | if _root_debug: |
| 1857 | sys.stderr.write(("_root lstrip (maybe_path=%s, file_path_from_root=%s," + |
| 1858 | " _root=%s)\n") %(maybe_path, file_path_from_root, _root)) |
| 1859 | |
| 1860 | if maybe_path: |
| 1861 | return os.path.join(*maybe_path) |
| 1862 | |
| 1863 | # --root=.. , will prepend the outer directory to the header guard |
| 1864 | full_path = fileinfo.FullName() |
| 1865 | root_abspath = os.path.abspath(_root) |
| 1866 | |
| 1867 | maybe_path = StripListPrefix(PathSplitToList(full_path), |
| 1868 | PathSplitToList(root_abspath)) |
| 1869 | |
| 1870 | if _root_debug: |
| 1871 | sys.stderr.write(("_root prepend (maybe_path=%s, full_path=%s, " + |
| 1872 | "root_abspath=%s)\n") %(maybe_path, full_path, root_abspath)) |
| 1873 | |
| 1874 | if maybe_path: |
| 1875 | return os.path.join(*maybe_path) |
| 1876 | |
| 1877 | if _root_debug: |
| 1878 | sys.stderr.write("_root ignore, returning %s\n" %(file_path_from_root)) |
| 1879 | |
| 1880 | # --root=FAKE_DIR is ignored |
| 1881 | return file_path_from_root |
| 1882 | |
| 1883 | file_path_from_root = FixupPathFromRoot() |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1884 | return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1885 | |
| 1886 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1887 | def CheckForHeaderGuard(filename, clean_lines, error): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1888 | """Checks that the file contains a header guard. |
| 1889 | |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 1890 | Logs an error if no #ifndef header guard is present. For other |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1891 | headers, checks that the full pathname is used. |
| 1892 | |
| 1893 | Args: |
| 1894 | filename: The name of the C++ header file. |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1895 | clean_lines: A CleansedLines instance containing the file. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1896 | error: The function to call with any errors found. |
| 1897 | """ |
| 1898 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 1899 | # Don't check for header guards if there are error suppression |
| 1900 | # comments somewhere in this file. |
| 1901 | # |
| 1902 | # Because this is silencing a warning for a nonexistent line, we |
| 1903 | # only support the very specific NOLINT(build/header_guard) syntax, |
| 1904 | # and not the general NOLINT or NOLINT(*) syntax. |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1905 | raw_lines = clean_lines.lines_without_raw_strings |
| 1906 | for i in raw_lines: |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 1907 | if Search(r'//\s*NOLINT\(build/header_guard\)', i): |
| 1908 | return |
| 1909 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1910 | cppvar = GetHeaderGuardCPPVariable(filename) |
| 1911 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1912 | ifndef = '' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1913 | ifndef_linenum = 0 |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1914 | define = '' |
| 1915 | endif = '' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1916 | endif_linenum = 0 |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1917 | for linenum, line in enumerate(raw_lines): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1918 | linesplit = line.split() |
| 1919 | if len(linesplit) >= 2: |
| 1920 | # find the first occurrence of #ifndef and #define, save arg |
| 1921 | if not ifndef and linesplit[0] == '#ifndef': |
| 1922 | # set ifndef to the header guard presented on the #ifndef line. |
| 1923 | ifndef = linesplit[1] |
| 1924 | ifndef_linenum = linenum |
| 1925 | if not define and linesplit[0] == '#define': |
| 1926 | define = linesplit[1] |
| 1927 | # find the last occurrence of #endif, save entire line |
| 1928 | if line.startswith('#endif'): |
| 1929 | endif = line |
| 1930 | endif_linenum = linenum |
| 1931 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1932 | if not ifndef or not define or ifndef != define: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1933 | error(filename, 0, 'build/header_guard', 5, |
| 1934 | 'No #ifndef header guard found, suggested CPP variable is: %s' % |
| 1935 | cppvar) |
| 1936 | return |
| 1937 | |
| 1938 | # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__ |
| 1939 | # for backward compatibility. |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 1940 | if ifndef != cppvar: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1941 | error_level = 0 |
| 1942 | if ifndef != cppvar + '_': |
| 1943 | error_level = 5 |
| 1944 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1945 | ParseNolintSuppressions(filename, raw_lines[ifndef_linenum], ifndef_linenum, |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 1946 | error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1947 | error(filename, ifndef_linenum, 'build/header_guard', error_level, |
| 1948 | '#ifndef header guard has wrong style, please use: %s' % cppvar) |
| 1949 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1950 | # Check for "//" comments on endif line. |
| 1951 | ParseNolintSuppressions(filename, raw_lines[endif_linenum], endif_linenum, |
| 1952 | error) |
| 1953 | match = Match(r'#endif\s*//\s*' + cppvar + r'(_)?\b', endif) |
| 1954 | if match: |
| 1955 | if match.group(1) == '_': |
| 1956 | # Issue low severity warning for deprecated double trailing underscore |
| 1957 | error(filename, endif_linenum, 'build/header_guard', 0, |
| 1958 | '#endif line should be "#endif // %s"' % cppvar) |
erg@chromium.org | c452fea | 2012-01-26 21:10:45 +0000 | [diff] [blame] | 1959 | return |
| 1960 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1961 | # Didn't find the corresponding "//" comment. If this file does not |
| 1962 | # contain any "//" comments at all, it could be that the compiler |
| 1963 | # only wants "/**/" comments, look for those instead. |
| 1964 | no_single_line_comments = True |
| 1965 | for i in xrange(1, len(raw_lines) - 1): |
| 1966 | line = raw_lines[i] |
| 1967 | if Match(r'^(?:(?:\'(?:\.|[^\'])*\')|(?:"(?:\.|[^"])*")|[^\'"])*//', line): |
| 1968 | no_single_line_comments = False |
| 1969 | break |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1970 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1971 | if no_single_line_comments: |
| 1972 | match = Match(r'#endif\s*/\*\s*' + cppvar + r'(_)?\s*\*/', endif) |
| 1973 | if match: |
| 1974 | if match.group(1) == '_': |
| 1975 | # Low severity warning for double trailing underscore |
| 1976 | error(filename, endif_linenum, 'build/header_guard', 0, |
| 1977 | '#endif line should be "#endif /* %s */"' % cppvar) |
| 1978 | return |
| 1979 | |
| 1980 | # Didn't find anything |
| 1981 | error(filename, endif_linenum, 'build/header_guard', 5, |
| 1982 | '#endif line should be "#endif // %s"' % cppvar) |
| 1983 | |
| 1984 | |
| 1985 | def CheckHeaderFileIncluded(filename, include_state, error): |
| 1986 | """Logs an error if a .cc file does not include its header.""" |
| 1987 | |
| 1988 | # Do not check test files |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 1989 | fileinfo = FileInfo(filename) |
| 1990 | if Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()): |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1991 | return |
| 1992 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 1993 | headerfile = filename[0:len(filename) - len(fileinfo.Extension())] + '.h' |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 1994 | if not os.path.exists(headerfile): |
| 1995 | return |
| 1996 | headername = FileInfo(headerfile).RepositoryName() |
| 1997 | first_include = 0 |
| 1998 | for section_list in include_state.include_list: |
| 1999 | for f in section_list: |
| 2000 | if headername in f[0] or f[0] in headername: |
| 2001 | return |
| 2002 | if not first_include: |
| 2003 | first_include = f[1] |
| 2004 | |
| 2005 | error(filename, first_include, 'build/include', 5, |
| 2006 | '%s should include its header file %s' % (fileinfo.RepositoryName(), |
| 2007 | headername)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2008 | |
| 2009 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2010 | def CheckForBadCharacters(filename, lines, error): |
| 2011 | """Logs an error for each line containing bad characters. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2012 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2013 | Two kinds of bad characters: |
| 2014 | |
| 2015 | 1. Unicode replacement characters: These indicate that either the file |
| 2016 | contained invalid UTF-8 (likely) or Unicode replacement characters (which |
| 2017 | it shouldn't). Note that it's possible for this to throw off line |
| 2018 | numbering if the invalid UTF-8 occurred adjacent to a newline. |
| 2019 | |
| 2020 | 2. NUL bytes. These are problematic for some tools. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2021 | |
| 2022 | Args: |
| 2023 | filename: The name of the current file. |
| 2024 | lines: An array of strings, each representing a line of the file. |
| 2025 | error: The function to call with any errors found. |
| 2026 | """ |
| 2027 | for linenum, line in enumerate(lines): |
| 2028 | if u'\ufffd' in line: |
| 2029 | error(filename, linenum, 'readability/utf8', 5, |
| 2030 | 'Line contains invalid UTF-8 (or Unicode replacement character).') |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2031 | if '\0' in line: |
| 2032 | error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2033 | |
| 2034 | |
| 2035 | def CheckForNewlineAtEOF(filename, lines, error): |
| 2036 | """Logs an error if there is no newline char at the end of the file. |
| 2037 | |
| 2038 | Args: |
| 2039 | filename: The name of the current file. |
| 2040 | lines: An array of strings, each representing a line of the file. |
| 2041 | error: The function to call with any errors found. |
| 2042 | """ |
| 2043 | |
| 2044 | # The array lines() was created by adding two newlines to the |
| 2045 | # original file (go figure), then splitting on \n. |
| 2046 | # To verify that the file ends in \n, we just have to make sure the |
| 2047 | # last-but-two element of lines() exists and is empty. |
| 2048 | if len(lines) < 3 or lines[-2]: |
| 2049 | error(filename, len(lines) - 2, 'whitespace/ending_newline', 5, |
| 2050 | 'Could not find a newline character at the end of the file.') |
| 2051 | |
| 2052 | |
| 2053 | def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error): |
| 2054 | """Logs an error if we see /* ... */ or "..." that extend past one line. |
| 2055 | |
| 2056 | /* ... */ comments are legit inside macros, for one line. |
| 2057 | Otherwise, we prefer // comments, so it's ok to warn about the |
| 2058 | other. Likewise, it's ok for strings to extend across multiple |
| 2059 | lines, as long as a line continuation character (backslash) |
| 2060 | terminates each line. Although not currently prohibited by the C++ |
| 2061 | style guide, it's ugly and unnecessary. We don't do well with either |
| 2062 | in this lint program, so we warn about both. |
| 2063 | |
| 2064 | Args: |
| 2065 | filename: The name of the current file. |
| 2066 | clean_lines: A CleansedLines instance containing the file. |
| 2067 | linenum: The number of the line to check. |
| 2068 | error: The function to call with any errors found. |
| 2069 | """ |
| 2070 | line = clean_lines.elided[linenum] |
| 2071 | |
| 2072 | # Remove all \\ (escaped backslashes) from the line. They are OK, and the |
| 2073 | # second (escaped) slash may trigger later \" detection erroneously. |
| 2074 | line = line.replace('\\\\', '') |
| 2075 | |
| 2076 | if line.count('/*') > line.count('*/'): |
| 2077 | error(filename, linenum, 'readability/multiline_comment', 5, |
| 2078 | 'Complex multi-line /*...*/-style comment found. ' |
| 2079 | 'Lint may give bogus warnings. ' |
| 2080 | 'Consider replacing these with //-style comments, ' |
| 2081 | 'with #if 0...#endif, ' |
| 2082 | 'or with more clearly structured multi-line comments.') |
| 2083 | |
| 2084 | if (line.count('"') - line.count('\\"')) % 2: |
| 2085 | error(filename, linenum, 'readability/multiline_string', 5, |
| 2086 | 'Multi-line string ("...") found. This lint script doesn\'t ' |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2087 | 'do well with such strings, and may give bogus warnings. ' |
| 2088 | 'Use C++11 raw strings or concatenation instead.') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2089 | |
| 2090 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2091 | # (non-threadsafe name, thread-safe alternative, validation pattern) |
| 2092 | # |
| 2093 | # The validation pattern is used to eliminate false positives such as: |
| 2094 | # _rand(); // false positive due to substring match. |
| 2095 | # ->rand(); // some member function rand(). |
| 2096 | # ACMRandom rand(seed); // some variable named rand. |
| 2097 | # ISAACRandom rand(); // another variable named rand. |
| 2098 | # |
| 2099 | # Basically we require the return value of these functions to be used |
| 2100 | # in some expression context on the same line by matching on some |
| 2101 | # operator before the function name. This eliminates constructors and |
| 2102 | # member function calls. |
| 2103 | _UNSAFE_FUNC_PREFIX = r'(?:[-+*/=%^&|(<]\s*|>\s+)' |
| 2104 | _THREADING_LIST = ( |
| 2105 | ('asctime(', 'asctime_r(', _UNSAFE_FUNC_PREFIX + r'asctime\([^)]+\)'), |
| 2106 | ('ctime(', 'ctime_r(', _UNSAFE_FUNC_PREFIX + r'ctime\([^)]+\)'), |
| 2107 | ('getgrgid(', 'getgrgid_r(', _UNSAFE_FUNC_PREFIX + r'getgrgid\([^)]+\)'), |
| 2108 | ('getgrnam(', 'getgrnam_r(', _UNSAFE_FUNC_PREFIX + r'getgrnam\([^)]+\)'), |
| 2109 | ('getlogin(', 'getlogin_r(', _UNSAFE_FUNC_PREFIX + r'getlogin\(\)'), |
| 2110 | ('getpwnam(', 'getpwnam_r(', _UNSAFE_FUNC_PREFIX + r'getpwnam\([^)]+\)'), |
| 2111 | ('getpwuid(', 'getpwuid_r(', _UNSAFE_FUNC_PREFIX + r'getpwuid\([^)]+\)'), |
| 2112 | ('gmtime(', 'gmtime_r(', _UNSAFE_FUNC_PREFIX + r'gmtime\([^)]+\)'), |
| 2113 | ('localtime(', 'localtime_r(', _UNSAFE_FUNC_PREFIX + r'localtime\([^)]+\)'), |
| 2114 | ('rand(', 'rand_r(', _UNSAFE_FUNC_PREFIX + r'rand\(\)'), |
| 2115 | ('strtok(', 'strtok_r(', |
| 2116 | _UNSAFE_FUNC_PREFIX + r'strtok\([^)]+\)'), |
| 2117 | ('ttyname(', 'ttyname_r(', _UNSAFE_FUNC_PREFIX + r'ttyname\([^)]+\)'), |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2118 | ) |
| 2119 | |
| 2120 | |
| 2121 | def CheckPosixThreading(filename, clean_lines, linenum, error): |
| 2122 | """Checks for calls to thread-unsafe functions. |
| 2123 | |
| 2124 | Much code has been originally written without consideration of |
| 2125 | multi-threading. Also, engineers are relying on their old experience; |
| 2126 | they have learned posix before threading extensions were added. These |
| 2127 | tests guide the engineers to use thread-safe functions (when using |
| 2128 | posix directly). |
| 2129 | |
| 2130 | Args: |
| 2131 | filename: The name of the current file. |
| 2132 | clean_lines: A CleansedLines instance containing the file. |
| 2133 | linenum: The number of the line to check. |
| 2134 | error: The function to call with any errors found. |
| 2135 | """ |
| 2136 | line = clean_lines.elided[linenum] |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2137 | for single_thread_func, multithread_safe_func, pattern in _THREADING_LIST: |
| 2138 | # Additional pattern matching check to confirm that this is the |
| 2139 | # function we are looking for |
| 2140 | if Search(pattern, line): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2141 | error(filename, linenum, 'runtime/threadsafe_fn', 2, |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2142 | 'Consider using ' + multithread_safe_func + |
| 2143 | '...) instead of ' + single_thread_func + |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2144 | '...) for improved thread safety.') |
| 2145 | |
| 2146 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2147 | def CheckVlogArguments(filename, clean_lines, linenum, error): |
| 2148 | """Checks that VLOG() is only used for defining a logging level. |
| 2149 | |
| 2150 | For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and |
| 2151 | VLOG(FATAL) are not. |
| 2152 | |
| 2153 | Args: |
| 2154 | filename: The name of the current file. |
| 2155 | clean_lines: A CleansedLines instance containing the file. |
| 2156 | linenum: The number of the line to check. |
| 2157 | error: The function to call with any errors found. |
| 2158 | """ |
| 2159 | line = clean_lines.elided[linenum] |
| 2160 | if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line): |
| 2161 | error(filename, linenum, 'runtime/vlog', 5, |
| 2162 | 'VLOG() should be used with numeric verbosity level. ' |
| 2163 | 'Use LOG() if you want symbolic severity levels.') |
| 2164 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 2165 | # Matches invalid increment: *count++, which moves pointer instead of |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 2166 | # incrementing a value. |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 2167 | _RE_PATTERN_INVALID_INCREMENT = re.compile( |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 2168 | r'^\s*\*\w+(\+\+|--);') |
| 2169 | |
| 2170 | |
| 2171 | def CheckInvalidIncrement(filename, clean_lines, linenum, error): |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 2172 | """Checks for invalid increment *count++. |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 2173 | |
| 2174 | For example following function: |
| 2175 | void increment_counter(int* count) { |
| 2176 | *count++; |
| 2177 | } |
| 2178 | is invalid, because it effectively does count++, moving pointer, and should |
| 2179 | be replaced with ++*count, (*count)++ or *count += 1. |
| 2180 | |
| 2181 | Args: |
| 2182 | filename: The name of the current file. |
| 2183 | clean_lines: A CleansedLines instance containing the file. |
| 2184 | linenum: The number of the line to check. |
| 2185 | error: The function to call with any errors found. |
| 2186 | """ |
| 2187 | line = clean_lines.elided[linenum] |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 2188 | if _RE_PATTERN_INVALID_INCREMENT.match(line): |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 2189 | error(filename, linenum, 'runtime/invalid_increment', 5, |
| 2190 | 'Changing pointer instead of value (or unused value of operator*).') |
| 2191 | |
| 2192 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 2193 | def IsMacroDefinition(clean_lines, linenum): |
| 2194 | if Search(r'^#define', clean_lines[linenum]): |
| 2195 | return True |
| 2196 | |
| 2197 | if linenum > 0 and Search(r'\\$', clean_lines[linenum - 1]): |
| 2198 | return True |
| 2199 | |
| 2200 | return False |
| 2201 | |
| 2202 | |
| 2203 | def IsForwardClassDeclaration(clean_lines, linenum): |
| 2204 | return Match(r'^\s*(\btemplate\b)*.*class\s+\w+;\s*$', clean_lines[linenum]) |
| 2205 | |
| 2206 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2207 | class _BlockInfo(object): |
| 2208 | """Stores information about a generic block of code.""" |
| 2209 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2210 | def __init__(self, linenum, seen_open_brace): |
| 2211 | self.starting_linenum = linenum |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2212 | self.seen_open_brace = seen_open_brace |
| 2213 | self.open_parentheses = 0 |
| 2214 | self.inline_asm = _NO_ASM |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 2215 | self.check_namespace_indentation = False |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2216 | |
| 2217 | def CheckBegin(self, filename, clean_lines, linenum, error): |
| 2218 | """Run checks that applies to text up to the opening brace. |
| 2219 | |
| 2220 | This is mostly for checking the text after the class identifier |
| 2221 | and the "{", usually where the base class is specified. For other |
| 2222 | blocks, there isn't much to check, so we always pass. |
| 2223 | |
| 2224 | Args: |
| 2225 | filename: The name of the current file. |
| 2226 | clean_lines: A CleansedLines instance containing the file. |
| 2227 | linenum: The number of the line to check. |
| 2228 | error: The function to call with any errors found. |
| 2229 | """ |
| 2230 | pass |
| 2231 | |
| 2232 | def CheckEnd(self, filename, clean_lines, linenum, error): |
| 2233 | """Run checks that applies to text after the closing brace. |
| 2234 | |
| 2235 | This is mostly used for checking end of namespace comments. |
| 2236 | |
| 2237 | Args: |
| 2238 | filename: The name of the current file. |
| 2239 | clean_lines: A CleansedLines instance containing the file. |
| 2240 | linenum: The number of the line to check. |
| 2241 | error: The function to call with any errors found. |
| 2242 | """ |
| 2243 | pass |
| 2244 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2245 | def IsBlockInfo(self): |
| 2246 | """Returns true if this block is a _BlockInfo. |
| 2247 | |
| 2248 | This is convenient for verifying that an object is an instance of |
| 2249 | a _BlockInfo, but not an instance of any of the derived classes. |
| 2250 | |
| 2251 | Returns: |
| 2252 | True for this class, False for derived classes. |
| 2253 | """ |
| 2254 | return self.__class__ == _BlockInfo |
| 2255 | |
| 2256 | |
| 2257 | class _ExternCInfo(_BlockInfo): |
| 2258 | """Stores information about an 'extern "C"' block.""" |
| 2259 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2260 | def __init__(self, linenum): |
| 2261 | _BlockInfo.__init__(self, linenum, True) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2262 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2263 | |
| 2264 | class _ClassInfo(_BlockInfo): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2265 | """Stores information about a class.""" |
| 2266 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2267 | def __init__(self, name, class_or_struct, clean_lines, linenum): |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2268 | _BlockInfo.__init__(self, linenum, False) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2269 | self.name = name |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2270 | self.is_derived = False |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 2271 | self.check_namespace_indentation = True |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2272 | if class_or_struct == 'struct': |
| 2273 | self.access = 'public' |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2274 | self.is_struct = True |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2275 | else: |
| 2276 | self.access = 'private' |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2277 | self.is_struct = False |
| 2278 | |
| 2279 | # Remember initial indentation level for this class. Using raw_lines here |
| 2280 | # instead of elided to account for leading comments. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2281 | self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum]) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2282 | |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 2283 | # Try to find the end of the class. This will be confused by things like: |
| 2284 | # class A { |
| 2285 | # } *x = { ... |
| 2286 | # |
| 2287 | # But it's still good enough for CheckSectionSpacing. |
| 2288 | self.last_line = 0 |
| 2289 | depth = 0 |
| 2290 | for i in range(linenum, clean_lines.NumLines()): |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2291 | line = clean_lines.elided[i] |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 2292 | depth += line.count('{') - line.count('}') |
| 2293 | if not depth: |
| 2294 | self.last_line = i |
| 2295 | break |
| 2296 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2297 | def CheckBegin(self, filename, clean_lines, linenum, error): |
| 2298 | # Look for a bare ':' |
| 2299 | if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]): |
| 2300 | self.is_derived = True |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2301 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2302 | def CheckEnd(self, filename, clean_lines, linenum, error): |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 2303 | # If there is a DISALLOW macro, it should appear near the end of |
| 2304 | # the class. |
| 2305 | seen_last_thing_in_class = False |
| 2306 | for i in xrange(linenum - 1, self.starting_linenum, -1): |
| 2307 | match = Search( |
| 2308 | r'\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(' + |
| 2309 | self.name + r'\)', |
| 2310 | clean_lines.elided[i]) |
| 2311 | if match: |
| 2312 | if seen_last_thing_in_class: |
| 2313 | error(filename, i, 'readability/constructors', 3, |
| 2314 | match.group(1) + ' should be the last thing in the class') |
| 2315 | break |
| 2316 | |
| 2317 | if not Match(r'^\s*$', clean_lines.elided[i]): |
| 2318 | seen_last_thing_in_class = True |
| 2319 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2320 | # Check that closing brace is aligned with beginning of the class. |
| 2321 | # Only do this if the closing brace is indented by only whitespaces. |
| 2322 | # This means we will not check single-line class definitions. |
| 2323 | indent = Match(r'^( *)\}', clean_lines.elided[linenum]) |
| 2324 | if indent and len(indent.group(1)) != self.class_indent: |
| 2325 | if self.is_struct: |
| 2326 | parent = 'struct ' + self.name |
| 2327 | else: |
| 2328 | parent = 'class ' + self.name |
| 2329 | error(filename, linenum, 'whitespace/indent', 3, |
| 2330 | 'Closing brace should be aligned with beginning of %s' % parent) |
| 2331 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2332 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2333 | class _NamespaceInfo(_BlockInfo): |
| 2334 | """Stores information about a namespace.""" |
| 2335 | |
| 2336 | def __init__(self, name, linenum): |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2337 | _BlockInfo.__init__(self, linenum, False) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2338 | self.name = name or '' |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 2339 | self.check_namespace_indentation = True |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2340 | |
| 2341 | def CheckEnd(self, filename, clean_lines, linenum, error): |
| 2342 | """Check end of namespace comments.""" |
| 2343 | line = clean_lines.raw_lines[linenum] |
| 2344 | |
| 2345 | # Check how many lines is enclosed in this namespace. Don't issue |
| 2346 | # warning for missing namespace comments if there aren't enough |
| 2347 | # lines. However, do apply checks if there is already an end of |
| 2348 | # namespace comment and it's incorrect. |
| 2349 | # |
| 2350 | # TODO(unknown): We always want to check end of namespace comments |
| 2351 | # if a namespace is large, but sometimes we also want to apply the |
| 2352 | # check if a short namespace contained nontrivial things (something |
| 2353 | # other than forward declarations). There is currently no logic on |
| 2354 | # deciding what these nontrivial things are, so this check is |
| 2355 | # triggered by namespace size only, which works most of the time. |
| 2356 | if (linenum - self.starting_linenum < 10 |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2357 | and not Match(r'^\s*};*\s*(//|/\*).*\bnamespace\b', line)): |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2358 | return |
| 2359 | |
| 2360 | # Look for matching comment at end of namespace. |
| 2361 | # |
| 2362 | # Note that we accept C style "/* */" comments for terminating |
| 2363 | # namespaces, so that code that terminate namespaces inside |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2364 | # preprocessor macros can be cpplint clean. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2365 | # |
| 2366 | # We also accept stuff like "// end of namespace <name>." with the |
| 2367 | # period at the end. |
| 2368 | # |
| 2369 | # Besides these, we don't accept anything else, otherwise we might |
| 2370 | # get false negatives when existing comment is a substring of the |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2371 | # expected namespace. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2372 | if self.name: |
| 2373 | # Named namespace |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2374 | if not Match((r'^\s*};*\s*(//|/\*).*\bnamespace\s+' + |
| 2375 | re.escape(self.name) + r'[\*/\.\\\s]*$'), |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2376 | line): |
| 2377 | error(filename, linenum, 'readability/namespace', 5, |
| 2378 | 'Namespace should be terminated with "// namespace %s"' % |
| 2379 | self.name) |
| 2380 | else: |
| 2381 | # Anonymous namespace |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2382 | if not Match(r'^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2383 | # If "// namespace anonymous" or "// anonymous namespace (more text)", |
| 2384 | # mention "// anonymous namespace" as an acceptable form |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2385 | if Match(r'^\s*}.*\b(namespace anonymous|anonymous namespace)\b', line): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2386 | error(filename, linenum, 'readability/namespace', 5, |
| 2387 | 'Anonymous namespace should be terminated with "// namespace"' |
| 2388 | ' or "// anonymous namespace"') |
| 2389 | else: |
| 2390 | error(filename, linenum, 'readability/namespace', 5, |
| 2391 | 'Anonymous namespace should be terminated with "// namespace"') |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2392 | |
| 2393 | |
| 2394 | class _PreprocessorInfo(object): |
| 2395 | """Stores checkpoints of nesting stacks when #if/#else is seen.""" |
| 2396 | |
| 2397 | def __init__(self, stack_before_if): |
| 2398 | # The entire nesting stack before #if |
| 2399 | self.stack_before_if = stack_before_if |
| 2400 | |
| 2401 | # The entire nesting stack up to #else |
| 2402 | self.stack_before_else = [] |
| 2403 | |
| 2404 | # Whether we have already seen #else or #elif |
| 2405 | self.seen_else = False |
| 2406 | |
| 2407 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2408 | class NestingState(object): |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2409 | """Holds states related to parsing braces.""" |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2410 | |
| 2411 | def __init__(self): |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2412 | # Stack for tracking all braces. An object is pushed whenever we |
| 2413 | # see a "{", and popped when we see a "}". Only 3 types of |
| 2414 | # objects are possible: |
| 2415 | # - _ClassInfo: a class or struct. |
| 2416 | # - _NamespaceInfo: a namespace. |
| 2417 | # - _BlockInfo: some other type of block. |
| 2418 | self.stack = [] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2419 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2420 | # Top of the previous stack before each Update(). |
| 2421 | # |
| 2422 | # Because the nesting_stack is updated at the end of each line, we |
| 2423 | # had to do some convoluted checks to find out what is the current |
| 2424 | # scope at the beginning of the line. This check is simplified by |
| 2425 | # saving the previous top of nesting stack. |
| 2426 | # |
| 2427 | # We could save the full stack, but we only need the top. Copying |
| 2428 | # the full nesting stack would slow down cpplint by ~10%. |
| 2429 | self.previous_stack_top = [] |
| 2430 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2431 | # Stack of _PreprocessorInfo objects. |
| 2432 | self.pp_stack = [] |
| 2433 | |
| 2434 | def SeenOpenBrace(self): |
| 2435 | """Check if we have seen the opening brace for the innermost block. |
| 2436 | |
| 2437 | Returns: |
| 2438 | True if we have seen the opening brace, False if the innermost |
| 2439 | block is still expecting an opening brace. |
| 2440 | """ |
| 2441 | return (not self.stack) or self.stack[-1].seen_open_brace |
| 2442 | |
| 2443 | def InNamespaceBody(self): |
| 2444 | """Check if we are currently one level inside a namespace body. |
| 2445 | |
| 2446 | Returns: |
| 2447 | True if top of the stack is a namespace block, False otherwise. |
| 2448 | """ |
| 2449 | return self.stack and isinstance(self.stack[-1], _NamespaceInfo) |
| 2450 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2451 | def InExternC(self): |
| 2452 | """Check if we are currently one level inside an 'extern "C"' block. |
| 2453 | |
| 2454 | Returns: |
| 2455 | True if top of the stack is an extern block, False otherwise. |
| 2456 | """ |
| 2457 | return self.stack and isinstance(self.stack[-1], _ExternCInfo) |
| 2458 | |
| 2459 | def InClassDeclaration(self): |
| 2460 | """Check if we are currently one level inside a class or struct declaration. |
| 2461 | |
| 2462 | Returns: |
| 2463 | True if top of the stack is a class/struct, False otherwise. |
| 2464 | """ |
| 2465 | return self.stack and isinstance(self.stack[-1], _ClassInfo) |
| 2466 | |
| 2467 | def InAsmBlock(self): |
| 2468 | """Check if we are currently one level inside an inline ASM block. |
| 2469 | |
| 2470 | Returns: |
| 2471 | True if the top of the stack is a block containing inline ASM. |
| 2472 | """ |
| 2473 | return self.stack and self.stack[-1].inline_asm != _NO_ASM |
| 2474 | |
| 2475 | def InTemplateArgumentList(self, clean_lines, linenum, pos): |
| 2476 | """Check if current position is inside template argument list. |
| 2477 | |
| 2478 | Args: |
| 2479 | clean_lines: A CleansedLines instance containing the file. |
| 2480 | linenum: The number of the line to check. |
| 2481 | pos: position just after the suspected template argument. |
| 2482 | Returns: |
| 2483 | True if (linenum, pos) is inside template arguments. |
| 2484 | """ |
| 2485 | while linenum < clean_lines.NumLines(): |
| 2486 | # Find the earliest character that might indicate a template argument |
| 2487 | line = clean_lines.elided[linenum] |
| 2488 | match = Match(r'^[^{};=\[\]\.<>]*(.)', line[pos:]) |
| 2489 | if not match: |
| 2490 | linenum += 1 |
| 2491 | pos = 0 |
| 2492 | continue |
| 2493 | token = match.group(1) |
| 2494 | pos += len(match.group(0)) |
| 2495 | |
| 2496 | # These things do not look like template argument list: |
| 2497 | # class Suspect { |
| 2498 | # class Suspect x; } |
| 2499 | if token in ('{', '}', ';'): return False |
| 2500 | |
| 2501 | # These things look like template argument list: |
| 2502 | # template <class Suspect> |
| 2503 | # template <class Suspect = default_value> |
| 2504 | # template <class Suspect[]> |
| 2505 | # template <class Suspect...> |
| 2506 | if token in ('>', '=', '[', ']', '.'): return True |
| 2507 | |
| 2508 | # Check if token is an unmatched '<'. |
| 2509 | # If not, move on to the next character. |
| 2510 | if token != '<': |
| 2511 | pos += 1 |
| 2512 | if pos >= len(line): |
| 2513 | linenum += 1 |
| 2514 | pos = 0 |
| 2515 | continue |
| 2516 | |
| 2517 | # We can't be sure if we just find a single '<', and need to |
| 2518 | # find the matching '>'. |
| 2519 | (_, end_line, end_pos) = CloseExpression(clean_lines, linenum, pos - 1) |
| 2520 | if end_pos < 0: |
| 2521 | # Not sure if template argument list or syntax error in file |
| 2522 | return False |
| 2523 | linenum = end_line |
| 2524 | pos = end_pos |
| 2525 | return False |
| 2526 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2527 | def UpdatePreprocessor(self, line): |
| 2528 | """Update preprocessor stack. |
| 2529 | |
| 2530 | We need to handle preprocessors due to classes like this: |
| 2531 | #ifdef SWIG |
| 2532 | struct ResultDetailsPageElementExtensionPoint { |
| 2533 | #else |
| 2534 | struct ResultDetailsPageElementExtensionPoint : public Extension { |
| 2535 | #endif |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2536 | |
| 2537 | We make the following assumptions (good enough for most files): |
| 2538 | - Preprocessor condition evaluates to true from #if up to first |
| 2539 | #else/#elif/#endif. |
| 2540 | |
| 2541 | - Preprocessor condition evaluates to false from #else/#elif up |
| 2542 | to #endif. We still perform lint checks on these lines, but |
| 2543 | these do not affect nesting stack. |
| 2544 | |
| 2545 | Args: |
| 2546 | line: current line to check. |
| 2547 | """ |
| 2548 | if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line): |
| 2549 | # Beginning of #if block, save the nesting stack here. The saved |
| 2550 | # stack will allow us to restore the parsing state in the #else case. |
| 2551 | self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack))) |
| 2552 | elif Match(r'^\s*#\s*(else|elif)\b', line): |
| 2553 | # Beginning of #else block |
| 2554 | if self.pp_stack: |
| 2555 | if not self.pp_stack[-1].seen_else: |
| 2556 | # This is the first #else or #elif block. Remember the |
| 2557 | # whole nesting stack up to this point. This is what we |
| 2558 | # keep after the #endif. |
| 2559 | self.pp_stack[-1].seen_else = True |
| 2560 | self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack) |
| 2561 | |
| 2562 | # Restore the stack to how it was before the #if |
| 2563 | self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if) |
| 2564 | else: |
| 2565 | # TODO(unknown): unexpected #else, issue warning? |
| 2566 | pass |
| 2567 | elif Match(r'^\s*#\s*endif\b', line): |
| 2568 | # End of #if or #else blocks. |
| 2569 | if self.pp_stack: |
| 2570 | # If we saw an #else, we will need to restore the nesting |
| 2571 | # stack to its former state before the #else, otherwise we |
| 2572 | # will just continue from where we left off. |
| 2573 | if self.pp_stack[-1].seen_else: |
| 2574 | # Here we can just use a shallow copy since we are the last |
| 2575 | # reference to it. |
| 2576 | self.stack = self.pp_stack[-1].stack_before_else |
| 2577 | # Drop the corresponding #if |
| 2578 | self.pp_stack.pop() |
| 2579 | else: |
| 2580 | # TODO(unknown): unexpected #endif, issue warning? |
| 2581 | pass |
| 2582 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2583 | # TODO(unknown): Update() is too long, but we will refactor later. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2584 | def Update(self, filename, clean_lines, linenum, error): |
| 2585 | """Update nesting state with current line. |
| 2586 | |
| 2587 | Args: |
| 2588 | filename: The name of the current file. |
| 2589 | clean_lines: A CleansedLines instance containing the file. |
| 2590 | linenum: The number of the line to check. |
| 2591 | error: The function to call with any errors found. |
| 2592 | """ |
| 2593 | line = clean_lines.elided[linenum] |
| 2594 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2595 | # Remember top of the previous nesting stack. |
| 2596 | # |
| 2597 | # The stack is always pushed/popped and not modified in place, so |
| 2598 | # we can just do a shallow copy instead of copy.deepcopy. Using |
| 2599 | # deepcopy would slow down cpplint by ~28%. |
| 2600 | if self.stack: |
| 2601 | self.previous_stack_top = self.stack[-1] |
| 2602 | else: |
| 2603 | self.previous_stack_top = None |
| 2604 | |
| 2605 | # Update pp_stack |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2606 | self.UpdatePreprocessor(line) |
| 2607 | |
| 2608 | # Count parentheses. This is to avoid adding struct arguments to |
| 2609 | # the nesting stack. |
| 2610 | if self.stack: |
| 2611 | inner_block = self.stack[-1] |
| 2612 | depth_change = line.count('(') - line.count(')') |
| 2613 | inner_block.open_parentheses += depth_change |
| 2614 | |
| 2615 | # Also check if we are starting or ending an inline assembly block. |
| 2616 | if inner_block.inline_asm in (_NO_ASM, _END_ASM): |
| 2617 | if (depth_change != 0 and |
| 2618 | inner_block.open_parentheses == 1 and |
| 2619 | _MATCH_ASM.match(line)): |
| 2620 | # Enter assembly block |
| 2621 | inner_block.inline_asm = _INSIDE_ASM |
| 2622 | else: |
| 2623 | # Not entering assembly block. If previous line was _END_ASM, |
| 2624 | # we will now shift to _NO_ASM state. |
| 2625 | inner_block.inline_asm = _NO_ASM |
| 2626 | elif (inner_block.inline_asm == _INSIDE_ASM and |
| 2627 | inner_block.open_parentheses == 0): |
| 2628 | # Exit assembly block |
| 2629 | inner_block.inline_asm = _END_ASM |
| 2630 | |
| 2631 | # Consume namespace declaration at the beginning of the line. Do |
| 2632 | # this in a loop so that we catch same line declarations like this: |
| 2633 | # namespace proto2 { namespace bridge { class MessageSet; } } |
| 2634 | while True: |
| 2635 | # Match start of namespace. The "\b\s*" below catches namespace |
| 2636 | # declarations even if it weren't followed by a whitespace, this |
| 2637 | # is so that we don't confuse our namespace checker. The |
| 2638 | # missing spaces will be flagged by CheckSpacing. |
| 2639 | namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line) |
| 2640 | if not namespace_decl_match: |
| 2641 | break |
| 2642 | |
| 2643 | new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum) |
| 2644 | self.stack.append(new_namespace) |
| 2645 | |
| 2646 | line = namespace_decl_match.group(2) |
| 2647 | if line.find('{') != -1: |
| 2648 | new_namespace.seen_open_brace = True |
| 2649 | line = line[line.find('{') + 1:] |
| 2650 | |
| 2651 | # Look for a class declaration in whatever is left of the line |
| 2652 | # after parsing namespaces. The regexp accounts for decorated classes |
| 2653 | # such as in: |
| 2654 | # class LOCKABLE API Object { |
| 2655 | # }; |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2656 | class_decl_match = Match( |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2657 | r'^(\s*(?:template\s*<[\w\s<>,:]*>\s*)?' |
| 2658 | r'(class|struct)\s+(?:[A-Z_]+\s+)*(\w+(?:::\w+)*))' |
| 2659 | r'(.*)$', line) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2660 | if (class_decl_match and |
| 2661 | (not self.stack or self.stack[-1].open_parentheses == 0)): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2662 | # We do not want to accept classes that are actually template arguments: |
| 2663 | # template <class Ignore1, |
| 2664 | # class Ignore2 = Default<Args>, |
| 2665 | # template <Args> class Ignore3> |
| 2666 | # void Function() {}; |
| 2667 | # |
| 2668 | # To avoid template argument cases, we scan forward and look for |
| 2669 | # an unmatched '>'. If we see one, assume we are inside a |
| 2670 | # template argument list. |
| 2671 | end_declaration = len(class_decl_match.group(1)) |
| 2672 | if not self.InTemplateArgumentList(clean_lines, linenum, end_declaration): |
| 2673 | self.stack.append(_ClassInfo( |
| 2674 | class_decl_match.group(3), class_decl_match.group(2), |
| 2675 | clean_lines, linenum)) |
| 2676 | line = class_decl_match.group(4) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2677 | |
| 2678 | # If we have not yet seen the opening brace for the innermost block, |
| 2679 | # run checks here. |
| 2680 | if not self.SeenOpenBrace(): |
| 2681 | self.stack[-1].CheckBegin(filename, clean_lines, linenum, error) |
| 2682 | |
| 2683 | # Update access control if we are inside a class/struct |
| 2684 | if self.stack and isinstance(self.stack[-1], _ClassInfo): |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2685 | classinfo = self.stack[-1] |
| 2686 | access_match = Match( |
| 2687 | r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?' |
| 2688 | r':(?:[^:]|$)', |
| 2689 | line) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2690 | if access_match: |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2691 | classinfo.access = access_match.group(2) |
| 2692 | |
| 2693 | # Check that access keywords are indented +1 space. Skip this |
| 2694 | # check if the keywords are not preceded by whitespaces. |
| 2695 | indent = access_match.group(1) |
| 2696 | if (len(indent) != classinfo.class_indent + 1 and |
| 2697 | Match(r'^\s*$', indent)): |
| 2698 | if classinfo.is_struct: |
| 2699 | parent = 'struct ' + classinfo.name |
| 2700 | else: |
| 2701 | parent = 'class ' + classinfo.name |
| 2702 | slots = '' |
| 2703 | if access_match.group(3): |
| 2704 | slots = access_match.group(3) |
| 2705 | error(filename, linenum, 'whitespace/indent', 3, |
| 2706 | '%s%s: should be indented +1 space inside %s' % ( |
| 2707 | access_match.group(2), slots, parent)) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2708 | |
| 2709 | # Consume braces or semicolons from what's left of the line |
| 2710 | while True: |
| 2711 | # Match first brace, semicolon, or closed parenthesis. |
| 2712 | matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line) |
| 2713 | if not matched: |
| 2714 | break |
| 2715 | |
| 2716 | token = matched.group(1) |
| 2717 | if token == '{': |
| 2718 | # If namespace or class hasn't seen a opening brace yet, mark |
| 2719 | # namespace/class head as complete. Push a new block onto the |
| 2720 | # stack otherwise. |
| 2721 | if not self.SeenOpenBrace(): |
| 2722 | self.stack[-1].seen_open_brace = True |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2723 | elif Match(r'^extern\s*"[^"]*"\s*\{', line): |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2724 | self.stack.append(_ExternCInfo(linenum)) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2725 | else: |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2726 | self.stack.append(_BlockInfo(linenum, True)) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2727 | if _MATCH_ASM.match(line): |
| 2728 | self.stack[-1].inline_asm = _BLOCK_ASM |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2729 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2730 | elif token == ';' or token == ')': |
| 2731 | # If we haven't seen an opening brace yet, but we already saw |
| 2732 | # a semicolon, this is probably a forward declaration. Pop |
| 2733 | # the stack for these. |
| 2734 | # |
| 2735 | # Similarly, if we haven't seen an opening brace yet, but we |
| 2736 | # already saw a closing parenthesis, then these are probably |
| 2737 | # function arguments with extra "class" or "struct" keywords. |
| 2738 | # Also pop these stack for these. |
| 2739 | if not self.SeenOpenBrace(): |
| 2740 | self.stack.pop() |
| 2741 | else: # token == '}' |
| 2742 | # Perform end of block checks and pop the stack. |
| 2743 | if self.stack: |
| 2744 | self.stack[-1].CheckEnd(filename, clean_lines, linenum, error) |
| 2745 | self.stack.pop() |
| 2746 | line = matched.group(2) |
| 2747 | |
| 2748 | def InnermostClass(self): |
| 2749 | """Get class info on the top of the stack. |
| 2750 | |
| 2751 | Returns: |
| 2752 | A _ClassInfo object if we are inside a class, or None otherwise. |
| 2753 | """ |
| 2754 | for i in range(len(self.stack), 0, -1): |
| 2755 | classinfo = self.stack[i - 1] |
| 2756 | if isinstance(classinfo, _ClassInfo): |
| 2757 | return classinfo |
| 2758 | return None |
| 2759 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2760 | def CheckCompletedBlocks(self, filename, error): |
| 2761 | """Checks that all classes and namespaces have been completely parsed. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2762 | |
| 2763 | Call this when all lines in a file have been processed. |
| 2764 | Args: |
| 2765 | filename: The name of the current file. |
| 2766 | error: The function to call with any errors found. |
| 2767 | """ |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2768 | # Note: This test can result in false positives if #ifdef constructs |
| 2769 | # get in the way of brace matching. See the testBuildClass test in |
| 2770 | # cpplint_unittest.py for an example of this. |
| 2771 | for obj in self.stack: |
| 2772 | if isinstance(obj, _ClassInfo): |
| 2773 | error(filename, obj.starting_linenum, 'build/class', 5, |
| 2774 | 'Failed to find complete declaration of class %s' % |
| 2775 | obj.name) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2776 | elif isinstance(obj, _NamespaceInfo): |
| 2777 | error(filename, obj.starting_linenum, 'build/namespaces', 5, |
| 2778 | 'Failed to find complete declaration of namespace %s' % |
| 2779 | obj.name) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2780 | |
| 2781 | |
| 2782 | def CheckForNonStandardConstructs(filename, clean_lines, linenum, |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2783 | nesting_state, error): |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2784 | r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2785 | |
| 2786 | Complain about several constructs which gcc-2 accepts, but which are |
| 2787 | not standard C++. Warning about these in lint is one way to ease the |
| 2788 | transition to new compilers. |
| 2789 | - put storage class first (e.g. "static const" instead of "const static"). |
| 2790 | - "%lld" instead of %qd" in printf-type functions. |
| 2791 | - "%1$d" is non-standard in printf-type functions. |
| 2792 | - "\%" is an undefined character escape sequence. |
| 2793 | - text after #endif is not allowed. |
| 2794 | - invalid inner-style forward declaration. |
| 2795 | - >? and <? operators, and their >?= and <?= cousins. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2796 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 2797 | Additionally, check for constructor/destructor style violations and reference |
| 2798 | members, as it is very convenient to do so while checking for |
| 2799 | gcc-2 compliance. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2800 | |
| 2801 | Args: |
| 2802 | filename: The name of the current file. |
| 2803 | clean_lines: A CleansedLines instance containing the file. |
| 2804 | linenum: The number of the line to check. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2805 | nesting_state: A NestingState instance which maintains information about |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2806 | the current stack of nested blocks being parsed. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2807 | error: A callable to which errors are reported, which takes 4 arguments: |
| 2808 | filename, line number, error level, and message |
| 2809 | """ |
| 2810 | |
| 2811 | # Remove comments from the line, but leave in strings for now. |
| 2812 | line = clean_lines.lines[linenum] |
| 2813 | |
| 2814 | if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line): |
| 2815 | error(filename, linenum, 'runtime/printf_format', 3, |
| 2816 | '%q in format strings is deprecated. Use %ll instead.') |
| 2817 | |
| 2818 | if Search(r'printf\s*\(.*".*%\d+\$', line): |
| 2819 | error(filename, linenum, 'runtime/printf_format', 2, |
| 2820 | '%N$ formats are unconventional. Try rewriting to avoid them.') |
| 2821 | |
| 2822 | # Remove escaped backslashes before looking for undefined escapes. |
| 2823 | line = line.replace('\\\\', '') |
| 2824 | |
| 2825 | if Search(r'("|\').*\\(%|\[|\(|{)', line): |
| 2826 | error(filename, linenum, 'build/printf_format', 3, |
| 2827 | '%, [, (, and { are undefined character escapes. Unescape them.') |
| 2828 | |
| 2829 | # For the rest, work with both comments and strings removed. |
| 2830 | line = clean_lines.elided[linenum] |
| 2831 | |
| 2832 | if Search(r'\b(const|volatile|void|char|short|int|long' |
| 2833 | r'|float|double|signed|unsigned' |
| 2834 | r'|schar|u?int8|u?int16|u?int32|u?int64)' |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2835 | r'\s+(register|static|extern|typedef)\b', |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2836 | line): |
| 2837 | error(filename, linenum, 'build/storage_class', 5, |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2838 | 'Storage-class specifier (static, extern, typedef, etc) should be ' |
| 2839 | 'at the beginning of the declaration.') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2840 | |
| 2841 | if Match(r'\s*#\s*endif\s*[^/\s]+', line): |
| 2842 | error(filename, linenum, 'build/endif_comment', 5, |
| 2843 | 'Uncommented text after #endif is non-standard. Use a comment.') |
| 2844 | |
| 2845 | if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line): |
| 2846 | error(filename, linenum, 'build/forward_decl', 5, |
| 2847 | 'Inner-style forward declarations are invalid. Remove this line.') |
| 2848 | |
| 2849 | if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?', |
| 2850 | line): |
| 2851 | error(filename, linenum, 'build/deprecated', 3, |
| 2852 | '>? and <? (max and min) operators are non-standard and deprecated.') |
| 2853 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 2854 | if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line): |
| 2855 | # TODO(unknown): Could it be expanded safely to arbitrary references, |
| 2856 | # without triggering too many false positives? The first |
| 2857 | # attempt triggered 5 warnings for mostly benign code in the regtest, hence |
| 2858 | # the restriction. |
| 2859 | # Here's the original regexp, for the reference: |
| 2860 | # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?' |
| 2861 | # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;' |
| 2862 | error(filename, linenum, 'runtime/member_string_references', 2, |
| 2863 | 'const string& members are dangerous. It is much better to use ' |
| 2864 | 'alternatives, such as pointers or simple constants.') |
| 2865 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 2866 | # Everything else in this function operates on class declarations. |
| 2867 | # Return early if the top of the nesting stack is not a class, or if |
| 2868 | # the class head is not completed yet. |
| 2869 | classinfo = nesting_state.InnermostClass() |
| 2870 | if not classinfo or not classinfo.seen_open_brace: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2871 | return |
| 2872 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2873 | # The class may have been declared with namespace or classname qualifiers. |
| 2874 | # The constructor and destructor will not have those qualifiers. |
| 2875 | base_classname = classinfo.name.split('::')[-1] |
| 2876 | |
| 2877 | # Look for single-argument constructors that aren't marked explicit. |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2878 | # Technically a valid construct, but against style. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 2879 | explicit_constructor_match = Match( |
danakj | d7f5675 | 2017-02-22 11:45:06 -0500 | [diff] [blame] | 2880 | r'\s+(?:(?:inline|constexpr)\s+)*(explicit\s+)?' |
| 2881 | r'(?:(?:inline|constexpr)\s+)*%s\s*' |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 2882 | r'\(((?:[^()]|\([^()]*\))*)\)' |
| 2883 | % re.escape(base_classname), |
| 2884 | line) |
| 2885 | |
| 2886 | if explicit_constructor_match: |
| 2887 | is_marked_explicit = explicit_constructor_match.group(1) |
| 2888 | |
| 2889 | if not explicit_constructor_match.group(2): |
| 2890 | constructor_args = [] |
| 2891 | else: |
| 2892 | constructor_args = explicit_constructor_match.group(2).split(',') |
| 2893 | |
| 2894 | # collapse arguments so that commas in template parameter lists and function |
| 2895 | # argument parameter lists don't split arguments in two |
| 2896 | i = 0 |
| 2897 | while i < len(constructor_args): |
| 2898 | constructor_arg = constructor_args[i] |
| 2899 | while (constructor_arg.count('<') > constructor_arg.count('>') or |
| 2900 | constructor_arg.count('(') > constructor_arg.count(')')): |
| 2901 | constructor_arg += ',' + constructor_args[i + 1] |
| 2902 | del constructor_args[i + 1] |
| 2903 | constructor_args[i] = constructor_arg |
| 2904 | i += 1 |
| 2905 | |
| 2906 | defaulted_args = [arg for arg in constructor_args if '=' in arg] |
| 2907 | noarg_constructor = (not constructor_args or # empty arg list |
| 2908 | # 'void' arg specifier |
| 2909 | (len(constructor_args) == 1 and |
| 2910 | constructor_args[0].strip() == 'void')) |
| 2911 | onearg_constructor = ((len(constructor_args) == 1 and # exactly one arg |
| 2912 | not noarg_constructor) or |
| 2913 | # all but at most one arg defaulted |
| 2914 | (len(constructor_args) >= 1 and |
| 2915 | not noarg_constructor and |
| 2916 | len(defaulted_args) >= len(constructor_args) - 1)) |
| 2917 | initializer_list_constructor = bool( |
| 2918 | onearg_constructor and |
| 2919 | Search(r'\bstd\s*::\s*initializer_list\b', constructor_args[0])) |
| 2920 | copy_constructor = bool( |
| 2921 | onearg_constructor and |
| 2922 | Match(r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&' |
| 2923 | % re.escape(base_classname), constructor_args[0].strip())) |
| 2924 | |
| 2925 | if (not is_marked_explicit and |
| 2926 | onearg_constructor and |
| 2927 | not initializer_list_constructor and |
| 2928 | not copy_constructor): |
| 2929 | if defaulted_args: |
| 2930 | error(filename, linenum, 'runtime/explicit', 5, |
| 2931 | 'Constructors callable with one argument ' |
| 2932 | 'should be marked explicit.') |
| 2933 | else: |
| 2934 | error(filename, linenum, 'runtime/explicit', 5, |
| 2935 | 'Single-parameter constructors should be marked explicit.') |
| 2936 | elif is_marked_explicit and not onearg_constructor: |
| 2937 | if noarg_constructor: |
| 2938 | error(filename, linenum, 'runtime/explicit', 5, |
| 2939 | 'Zero-parameter constructors should not be marked explicit.') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2940 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2941 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2942 | def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2943 | """Checks for the correctness of various spacing around function calls. |
| 2944 | |
| 2945 | Args: |
| 2946 | filename: The name of the current file. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2947 | clean_lines: A CleansedLines instance containing the file. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2948 | linenum: The number of the line to check. |
| 2949 | error: The function to call with any errors found. |
| 2950 | """ |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2951 | line = clean_lines.elided[linenum] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2952 | |
| 2953 | # Since function calls often occur inside if/for/while/switch |
| 2954 | # expressions - which have their own, more liberal conventions - we |
| 2955 | # first see if we should be looking inside such an expression for a |
| 2956 | # function call, to which we can apply more strict standards. |
| 2957 | fncall = line # if there's no control flow construct, look at whole line |
| 2958 | for pattern in (r'\bif\s*\((.*)\)\s*{', |
| 2959 | r'\bfor\s*\((.*)\)\s*{', |
| 2960 | r'\bwhile\s*\((.*)\)\s*[{;]', |
| 2961 | r'\bswitch\s*\((.*)\)\s*{'): |
| 2962 | match = Search(pattern, line) |
| 2963 | if match: |
| 2964 | fncall = match.group(1) # look inside the parens for function calls |
| 2965 | break |
| 2966 | |
| 2967 | # Except in if/for/while/switch, there should never be space |
| 2968 | # immediately inside parens (eg "f( 3, 4 )"). We make an exception |
| 2969 | # for nested parens ( (a+b) + c ). Likewise, there should never be |
| 2970 | # a space before a ( when it's a function argument. I assume it's a |
| 2971 | # function argument when the char before the whitespace is legal in |
| 2972 | # a function name (alnum + _) and we're not starting a macro. Also ignore |
| 2973 | # pointers and references to arrays and functions coz they're too tricky: |
| 2974 | # we use a very simple way to recognize these: |
| 2975 | # " (something)(maybe-something)" or |
| 2976 | # " (something)(maybe-something," or |
| 2977 | # " (something)[something]" |
| 2978 | # Note that we assume the contents of [] to be short enough that |
| 2979 | # they'll never need to wrap. |
| 2980 | if ( # Ignore control structures. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 2981 | not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b', |
| 2982 | fncall) and |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2983 | # Ignore pointers/references to functions. |
| 2984 | not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and |
| 2985 | # Ignore pointers/references to arrays. |
| 2986 | not Search(r' \([^)]+\)\[[^\]]+\]', fncall)): |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 2987 | if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2988 | error(filename, linenum, 'whitespace/parens', 4, |
| 2989 | 'Extra space after ( in function call') |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 2990 | elif Search(r'\(\s+(?!(\s*\\)|\()', fncall): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 2991 | error(filename, linenum, 'whitespace/parens', 2, |
| 2992 | 'Extra space after (') |
| 2993 | if (Search(r'\w\s+\(', fncall) and |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 2994 | not Search(r'_{0,2}asm_{0,2}\s+_{0,2}volatile_{0,2}\s+\(', fncall) and |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2995 | not Search(r'#\s*define|typedef|using\s+\w+\s*=', fncall) and |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 2996 | not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall) and |
| 2997 | not Search(r'\bcase\s+\(', fncall)): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 2998 | # TODO(unknown): Space after an operator function seem to be a common |
| 2999 | # error, silence those for now by restricting them to highest verbosity. |
| 3000 | if Search(r'\boperator_*\b', line): |
| 3001 | error(filename, linenum, 'whitespace/parens', 0, |
| 3002 | 'Extra space before ( in function call') |
| 3003 | else: |
| 3004 | error(filename, linenum, 'whitespace/parens', 4, |
| 3005 | 'Extra space before ( in function call') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3006 | # If the ) is followed only by a newline or a { + newline, assume it's |
| 3007 | # part of a control statement (if/while/etc), and don't complain |
| 3008 | if Search(r'[^)]\s+\)\s*[^{\s]', fncall): |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3009 | # If the closing parenthesis is preceded by only whitespaces, |
| 3010 | # try to give a more descriptive error message. |
| 3011 | if Search(r'^\s+\)', fncall): |
| 3012 | error(filename, linenum, 'whitespace/parens', 2, |
| 3013 | 'Closing ) should be moved to the previous line') |
| 3014 | else: |
| 3015 | error(filename, linenum, 'whitespace/parens', 2, |
| 3016 | 'Extra space before )') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3017 | |
| 3018 | |
| 3019 | def IsBlankLine(line): |
| 3020 | """Returns true if the given line is blank. |
| 3021 | |
| 3022 | We consider a line to be blank if the line is empty or consists of |
| 3023 | only white spaces. |
| 3024 | |
| 3025 | Args: |
| 3026 | line: A line of a string. |
| 3027 | |
| 3028 | Returns: |
| 3029 | True, if the given line is blank. |
| 3030 | """ |
| 3031 | return not line or line.isspace() |
| 3032 | |
| 3033 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 3034 | def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line, |
| 3035 | error): |
| 3036 | is_namespace_indent_item = ( |
| 3037 | len(nesting_state.stack) > 1 and |
| 3038 | nesting_state.stack[-1].check_namespace_indentation and |
| 3039 | isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and |
| 3040 | nesting_state.previous_stack_top == nesting_state.stack[-2]) |
| 3041 | |
| 3042 | if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item, |
| 3043 | clean_lines.elided, line): |
| 3044 | CheckItemIndentationInNamespace(filename, clean_lines.elided, |
| 3045 | line, error) |
| 3046 | |
| 3047 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3048 | def CheckForFunctionLengths(filename, clean_lines, linenum, |
| 3049 | function_state, error): |
| 3050 | """Reports for long function bodies. |
| 3051 | |
| 3052 | For an overview why this is done, see: |
Alexandr Ilin | ff294c3 | 2017-04-27 15:57:40 +0200 | [diff] [blame] | 3053 | https://google.github.io/styleguide/cppguide.html#Write_Short_Functions |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3054 | |
| 3055 | Uses a simplistic algorithm assuming other style guidelines |
| 3056 | (especially spacing) are followed. |
| 3057 | Only checks unindented functions, so class members are unchecked. |
| 3058 | Trivial bodies are unchecked, so constructors with huge initializer lists |
| 3059 | may be missed. |
| 3060 | Blank/comment lines are not counted so as to avoid encouraging the removal |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3061 | of vertical space and comments just to get through a lint check. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3062 | NOLINT *on the last line of a function* disables this check. |
| 3063 | |
| 3064 | Args: |
| 3065 | filename: The name of the current file. |
| 3066 | clean_lines: A CleansedLines instance containing the file. |
| 3067 | linenum: The number of the line to check. |
| 3068 | function_state: Current function name and lines in body so far. |
| 3069 | error: The function to call with any errors found. |
| 3070 | """ |
| 3071 | lines = clean_lines.lines |
| 3072 | line = lines[linenum] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3073 | joined_line = '' |
| 3074 | |
| 3075 | starting_func = False |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 3076 | regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ... |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3077 | match_result = Match(regexp, line) |
| 3078 | if match_result: |
| 3079 | # If the name is all caps and underscores, figure it's a macro and |
| 3080 | # ignore it, unless it's TEST or TEST_F. |
| 3081 | function_name = match_result.group(1).split()[-1] |
| 3082 | if function_name == 'TEST' or function_name == 'TEST_F' or ( |
| 3083 | not Match(r'[A-Z_]+$', function_name)): |
| 3084 | starting_func = True |
| 3085 | |
| 3086 | if starting_func: |
| 3087 | body_found = False |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 3088 | for start_linenum in xrange(linenum, clean_lines.NumLines()): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3089 | start_line = lines[start_linenum] |
| 3090 | joined_line += ' ' + start_line.lstrip() |
| 3091 | if Search(r'(;|})', start_line): # Declarations and trivial functions |
| 3092 | body_found = True |
| 3093 | break # ... ignore |
| 3094 | elif Search(r'{', start_line): |
| 3095 | body_found = True |
| 3096 | function = Search(r'((\w|:)*)\(', line).group(1) |
| 3097 | if Match(r'TEST', function): # Handle TEST... macros |
| 3098 | parameter_regexp = Search(r'(\(.*\))', joined_line) |
| 3099 | if parameter_regexp: # Ignore bad syntax |
| 3100 | function += parameter_regexp.group(1) |
| 3101 | else: |
| 3102 | function += '()' |
| 3103 | function_state.Begin(function) |
| 3104 | break |
| 3105 | if not body_found: |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 3106 | # No body for the function (or evidence of a non-function) was found. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3107 | error(filename, linenum, 'readability/fn_size', 5, |
| 3108 | 'Lint failed to find start of function body.') |
| 3109 | elif Match(r'^\}\s*$', line): # function end |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 3110 | function_state.Check(error, filename, linenum) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3111 | function_state.End() |
| 3112 | elif not Match(r'^\s*$', line): |
| 3113 | function_state.Count() # Count non-blank/non-comment lines. |
| 3114 | |
| 3115 | |
| 3116 | _RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?') |
| 3117 | |
| 3118 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3119 | def CheckComment(line, filename, linenum, next_line_start, error): |
| 3120 | """Checks for common mistakes in comments. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3121 | |
| 3122 | Args: |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3123 | line: The line in question. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3124 | filename: The name of the current file. |
| 3125 | linenum: The number of the line to check. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3126 | next_line_start: The first non-whitespace column of the next line. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3127 | error: The function to call with any errors found. |
| 3128 | """ |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3129 | commentpos = line.find('//') |
| 3130 | if commentpos != -1: |
| 3131 | # Check if the // may be in quotes. If so, ignore it |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3132 | if re.sub(r'\\.', '', line[0:commentpos]).count('"') % 2 == 0: |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3133 | # Allow one space for new scopes, two spaces otherwise: |
| 3134 | if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and |
| 3135 | ((commentpos >= 1 and |
| 3136 | line[commentpos-1] not in string.whitespace) or |
| 3137 | (commentpos >= 2 and |
| 3138 | line[commentpos-2] not in string.whitespace))): |
| 3139 | error(filename, linenum, 'whitespace/comments', 2, |
| 3140 | 'At least two spaces is best between code and comments') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3141 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3142 | # Checks for common mistakes in TODO comments. |
| 3143 | comment = line[commentpos:] |
| 3144 | match = _RE_PATTERN_TODO.match(comment) |
| 3145 | if match: |
| 3146 | # One whitespace is correct; zero whitespace is handled elsewhere. |
| 3147 | leading_whitespace = match.group(1) |
| 3148 | if len(leading_whitespace) > 1: |
| 3149 | error(filename, linenum, 'whitespace/todo', 2, |
| 3150 | 'Too many spaces before TODO') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3151 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3152 | username = match.group(2) |
| 3153 | if not username: |
| 3154 | error(filename, linenum, 'readability/todo', 2, |
| 3155 | 'Missing username in TODO; it should look like ' |
| 3156 | '"// TODO(my_username): Stuff."') |
| 3157 | |
| 3158 | middle_whitespace = match.group(3) |
| 3159 | # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison |
| 3160 | if middle_whitespace != ' ' and middle_whitespace != '': |
| 3161 | error(filename, linenum, 'whitespace/todo', 2, |
| 3162 | 'TODO(my_username) should be followed by a space') |
| 3163 | |
| 3164 | # If the comment contains an alphanumeric character, there |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 3165 | # should be a space somewhere between it and the // unless |
| 3166 | # it's a /// or //! Doxygen comment. |
| 3167 | if (Match(r'//[^ ]*\w', comment) and |
| 3168 | not Match(r'(///|//\!)(\s+|$)', comment)): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3169 | error(filename, linenum, 'whitespace/comments', 4, |
| 3170 | 'Should have a space between // and comment') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3171 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 3172 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3173 | def CheckSpacing(filename, clean_lines, linenum, nesting_state, error): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3174 | """Checks for the correctness of various spacing issues in the code. |
| 3175 | |
| 3176 | Things we check for: spaces around operators, spaces after |
| 3177 | if/for/while/switch, no spaces around parens in function calls, two |
| 3178 | spaces between code and comment, don't start a block with a blank |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3179 | line, don't end a function with a blank line, don't add a blank line |
| 3180 | after public/protected/private, don't have too many blank lines in a row. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3181 | |
| 3182 | Args: |
| 3183 | filename: The name of the current file. |
| 3184 | clean_lines: A CleansedLines instance containing the file. |
| 3185 | linenum: The number of the line to check. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3186 | nesting_state: A NestingState instance which maintains information about |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3187 | the current stack of nested blocks being parsed. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3188 | error: The function to call with any errors found. |
| 3189 | """ |
| 3190 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3191 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 3192 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 3193 | # raw strings, |
| 3194 | raw = clean_lines.lines_without_raw_strings |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3195 | line = raw[linenum] |
| 3196 | |
| 3197 | # Before nixing comments, check if the line is blank for no good |
| 3198 | # reason. This includes the first line after a block is opened, and |
| 3199 | # blank lines at the end of a function (ie, right before a line like '}' |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3200 | # |
| 3201 | # Skip all the blank line checks if we are immediately inside a |
| 3202 | # namespace body. In other words, don't issue blank line warnings |
| 3203 | # for this block: |
| 3204 | # namespace { |
| 3205 | # |
| 3206 | # } |
| 3207 | # |
| 3208 | # A warning about missing end of namespace comments will be issued instead. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3209 | # |
| 3210 | # Also skip blank line checks for 'extern "C"' blocks, which are formatted |
| 3211 | # like namespaces. |
| 3212 | if (IsBlankLine(line) and |
| 3213 | not nesting_state.InNamespaceBody() and |
| 3214 | not nesting_state.InExternC()): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3215 | elided = clean_lines.elided |
| 3216 | prev_line = elided[linenum - 1] |
| 3217 | prevbrace = prev_line.rfind('{') |
| 3218 | # TODO(unknown): Don't complain if line before blank line, and line after, |
| 3219 | # both start with alnums and are indented the same amount. |
| 3220 | # This ignores whitespace at the start of a namespace block |
| 3221 | # because those are not usually indented. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3222 | if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3223 | # OK, we have a blank line at the start of a code block. Before we |
| 3224 | # complain, we check if it is an exception to the rule: The previous |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3225 | # non-empty line has the parameters of a function header that are indented |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3226 | # 4 spaces (because they did not fit in a 80 column line when placed on |
| 3227 | # the same line as the function name). We also check for the case where |
| 3228 | # the previous line is indented 6 spaces, which may happen when the |
| 3229 | # initializers of a constructor do not fit into a 80 column line. |
| 3230 | exception = False |
| 3231 | if Match(r' {6}\w', prev_line): # Initializer list? |
| 3232 | # We are looking for the opening column of initializer list, which |
| 3233 | # should be indented 4 spaces to cause 6 space indentation afterwards. |
| 3234 | search_position = linenum-2 |
| 3235 | while (search_position >= 0 |
| 3236 | and Match(r' {6}\w', elided[search_position])): |
| 3237 | search_position -= 1 |
| 3238 | exception = (search_position >= 0 |
| 3239 | and elided[search_position][:5] == ' :') |
| 3240 | else: |
| 3241 | # Search for the function arguments or an initializer list. We use a |
| 3242 | # simple heuristic here: If the line is indented 4 spaces; and we have a |
| 3243 | # closing paren, without the opening paren, followed by an opening brace |
| 3244 | # or colon (for initializer lists) we assume that it is the last line of |
| 3245 | # a function header. If we have a colon indented 4 spaces, it is an |
| 3246 | # initializer list. |
| 3247 | exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)', |
| 3248 | prev_line) |
| 3249 | or Match(r' {4}:', prev_line)) |
| 3250 | |
| 3251 | if not exception: |
| 3252 | error(filename, linenum, 'whitespace/blank_line', 2, |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3253 | 'Redundant blank line at the start of a code block ' |
| 3254 | 'should be deleted.') |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3255 | # Ignore blank lines at the end of a block in a long if-else |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3256 | # chain, like this: |
| 3257 | # if (condition1) { |
| 3258 | # // Something followed by a blank line |
| 3259 | # |
| 3260 | # } else if (condition2) { |
| 3261 | # // Something else |
| 3262 | # } |
| 3263 | if linenum + 1 < clean_lines.NumLines(): |
| 3264 | next_line = raw[linenum + 1] |
| 3265 | if (next_line |
| 3266 | and Match(r'\s*}', next_line) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3267 | and next_line.find('} else ') == -1): |
| 3268 | error(filename, linenum, 'whitespace/blank_line', 3, |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3269 | 'Redundant blank line at the end of a code block ' |
| 3270 | 'should be deleted.') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3271 | |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3272 | matched = Match(r'\s*(public|protected|private):', prev_line) |
| 3273 | if matched: |
| 3274 | error(filename, linenum, 'whitespace/blank_line', 3, |
| 3275 | 'Do not leave a blank line after "%s:"' % matched.group(1)) |
| 3276 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3277 | # Next, check comments |
| 3278 | next_line_start = 0 |
| 3279 | if linenum + 1 < clean_lines.NumLines(): |
| 3280 | next_line = raw[linenum + 1] |
| 3281 | next_line_start = len(next_line) - len(next_line.lstrip()) |
| 3282 | CheckComment(line, filename, linenum, next_line_start, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3283 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3284 | # get rid of comments and strings |
| 3285 | line = clean_lines.elided[linenum] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3286 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3287 | # You shouldn't have spaces before your brackets, except maybe after |
| 3288 | # 'delete []' or 'return []() {};' |
| 3289 | if Search(r'\w\s+\[', line) and not Search(r'(?:delete|return)\s+\[', line): |
| 3290 | error(filename, linenum, 'whitespace/braces', 5, |
| 3291 | 'Extra space before [') |
| 3292 | |
| 3293 | # In range-based for, we wanted spaces before and after the colon, but |
| 3294 | # not around "::" tokens that might appear. |
| 3295 | if (Search(r'for *\(.*[^:]:[^: ]', line) or |
| 3296 | Search(r'for *\(.*[^: ]:[^:]', line)): |
| 3297 | error(filename, linenum, 'whitespace/forcolon', 2, |
| 3298 | 'Missing space around colon in range-based for loop') |
| 3299 | |
| 3300 | |
| 3301 | def CheckOperatorSpacing(filename, clean_lines, linenum, error): |
| 3302 | """Checks for horizontal spacing around operators. |
| 3303 | |
| 3304 | Args: |
| 3305 | filename: The name of the current file. |
| 3306 | clean_lines: A CleansedLines instance containing the file. |
| 3307 | linenum: The number of the line to check. |
| 3308 | error: The function to call with any errors found. |
| 3309 | """ |
| 3310 | line = clean_lines.elided[linenum] |
| 3311 | |
| 3312 | # Don't try to do spacing checks for operator methods. Do this by |
| 3313 | # replacing the troublesome characters with something else, |
| 3314 | # preserving column position for all other characters. |
| 3315 | # |
| 3316 | # The replacement is done repeatedly to avoid false positives from |
| 3317 | # operators that call operators. |
| 3318 | while True: |
| 3319 | match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line) |
| 3320 | if match: |
| 3321 | line = match.group(1) + ('_' * len(match.group(2))) + match.group(3) |
| 3322 | else: |
| 3323 | break |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3324 | |
| 3325 | # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )". |
| 3326 | # Otherwise not. Note we only check for non-spaces on *both* sides; |
| 3327 | # sometimes people put non-spaces on one side when aligning ='s among |
| 3328 | # many lines (not that this is behavior that I approve of...) |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 3329 | if ((Search(r'[\w.]=', line) or |
| 3330 | Search(r'=[\w.]', line)) |
| 3331 | and not Search(r'\b(if|while|for) ', line) |
| 3332 | # Operators taken from [lex.operators] in C++11 standard. |
| 3333 | and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line) |
| 3334 | and not Search(r'operator=', line)): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3335 | error(filename, linenum, 'whitespace/operators', 4, |
| 3336 | 'Missing spaces around =') |
| 3337 | |
| 3338 | # It's ok not to have spaces around binary operators like + - * /, but if |
| 3339 | # there's too little whitespace, we get concerned. It's hard to tell, |
| 3340 | # though, so we punt on this one for now. TODO. |
| 3341 | |
| 3342 | # You should always have whitespace around binary operators. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3343 | # |
| 3344 | # Check <= and >= first to avoid false positives with < and >, then |
| 3345 | # check non-include lines for spacing around < and >. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3346 | # |
| 3347 | # If the operator is followed by a comma, assume it's be used in a |
| 3348 | # macro context and don't do any checks. This avoids false |
| 3349 | # positives. |
| 3350 | # |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3351 | # Note that && is not included here. This is because there are too |
| 3352 | # many false positives due to RValue references. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3353 | match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3354 | if match: |
| 3355 | error(filename, linenum, 'whitespace/operators', 3, |
| 3356 | 'Missing spaces around %s' % match.group(1)) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3357 | elif not Match(r'#.*include', line): |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3358 | # Look for < that is not surrounded by spaces. This is only |
| 3359 | # triggered if both sides are missing spaces, even though |
| 3360 | # technically should should flag if at least one side is missing a |
| 3361 | # space. This is done to avoid some false positives with shifts. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3362 | match = Match(r'^(.*[^\s<])<[^\s=<,]', line) |
| 3363 | if match: |
| 3364 | (_, _, end_pos) = CloseExpression( |
| 3365 | clean_lines, linenum, len(match.group(1))) |
| 3366 | if end_pos <= -1: |
| 3367 | error(filename, linenum, 'whitespace/operators', 3, |
| 3368 | 'Missing spaces around <') |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3369 | |
| 3370 | # Look for > that is not surrounded by spaces. Similar to the |
| 3371 | # above, we only trigger if both sides are missing spaces to avoid |
| 3372 | # false positives with shifts. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3373 | match = Match(r'^(.*[^-\s>])>[^\s=>,]', line) |
| 3374 | if match: |
| 3375 | (_, _, start_pos) = ReverseCloseExpression( |
| 3376 | clean_lines, linenum, len(match.group(1))) |
| 3377 | if start_pos <= -1: |
| 3378 | error(filename, linenum, 'whitespace/operators', 3, |
| 3379 | 'Missing spaces around >') |
| 3380 | |
| 3381 | # We allow no-spaces around << when used like this: 10<<20, but |
| 3382 | # not otherwise (particularly, not when used as streams) |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 3383 | # |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3384 | # We also allow operators following an opening parenthesis, since |
| 3385 | # those tend to be macros that deal with operators. |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3386 | match = Search(r'(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])', line) |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 3387 | if (match and not (match.group(1).isdigit() and match.group(2).isdigit()) and |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3388 | not (match.group(1) == 'operator' and match.group(2) == ';')): |
| 3389 | error(filename, linenum, 'whitespace/operators', 3, |
| 3390 | 'Missing spaces around <<') |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3391 | |
| 3392 | # We allow no-spaces around >> for almost anything. This is because |
| 3393 | # C++11 allows ">>" to close nested templates, which accounts for |
| 3394 | # most cases when ">>" is not followed by a space. |
| 3395 | # |
| 3396 | # We still warn on ">>" followed by alpha character, because that is |
| 3397 | # likely due to ">>" being used for right shifts, e.g.: |
| 3398 | # value >> alpha |
| 3399 | # |
| 3400 | # When ">>" is used to close templates, the alphanumeric letter that |
| 3401 | # follows would be part of an identifier, and there should still be |
| 3402 | # a space separating the template type and the identifier. |
| 3403 | # type<type<type>> alpha |
| 3404 | match = Search(r'>>[a-zA-Z_]', line) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3405 | if match: |
| 3406 | error(filename, linenum, 'whitespace/operators', 3, |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3407 | 'Missing spaces around >>') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3408 | |
| 3409 | # There shouldn't be space around unary operators |
| 3410 | match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line) |
| 3411 | if match: |
| 3412 | error(filename, linenum, 'whitespace/operators', 4, |
| 3413 | 'Extra space for operator %s' % match.group(1)) |
| 3414 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3415 | |
| 3416 | def CheckParenthesisSpacing(filename, clean_lines, linenum, error): |
| 3417 | """Checks for horizontal spacing around parentheses. |
| 3418 | |
| 3419 | Args: |
| 3420 | filename: The name of the current file. |
| 3421 | clean_lines: A CleansedLines instance containing the file. |
| 3422 | linenum: The number of the line to check. |
| 3423 | error: The function to call with any errors found. |
| 3424 | """ |
| 3425 | line = clean_lines.elided[linenum] |
| 3426 | |
| 3427 | # No spaces after an if, while, switch, or for |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3428 | match = Search(r' (if\(|for\(|while\(|switch\()', line) |
| 3429 | if match: |
| 3430 | error(filename, linenum, 'whitespace/parens', 5, |
| 3431 | 'Missing space before ( in %s' % match.group(1)) |
| 3432 | |
| 3433 | # For if/for/while/switch, the left and right parens should be |
| 3434 | # consistent about how many spaces are inside the parens, and |
| 3435 | # there should either be zero or one spaces inside the parens. |
| 3436 | # We don't want: "if ( foo)" or "if ( foo )". |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 3437 | # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3438 | match = Search(r'\b(if|for|while|switch)\s*' |
| 3439 | r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$', |
| 3440 | line) |
| 3441 | if match: |
| 3442 | if len(match.group(2)) != len(match.group(4)): |
| 3443 | if not (match.group(3) == ';' and |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 3444 | len(match.group(2)) == 1 + len(match.group(4)) or |
| 3445 | not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3446 | error(filename, linenum, 'whitespace/parens', 5, |
| 3447 | 'Mismatching spaces inside () in %s' % match.group(1)) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3448 | if len(match.group(2)) not in [0, 1]: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3449 | error(filename, linenum, 'whitespace/parens', 5, |
| 3450 | 'Should have zero or one spaces inside ( and ) in %s' % |
| 3451 | match.group(1)) |
| 3452 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3453 | |
| 3454 | def CheckCommaSpacing(filename, clean_lines, linenum, error): |
| 3455 | """Checks for horizontal spacing near commas and semicolons. |
| 3456 | |
| 3457 | Args: |
| 3458 | filename: The name of the current file. |
| 3459 | clean_lines: A CleansedLines instance containing the file. |
| 3460 | linenum: The number of the line to check. |
| 3461 | error: The function to call with any errors found. |
| 3462 | """ |
| 3463 | raw = clean_lines.lines_without_raw_strings |
| 3464 | line = clean_lines.elided[linenum] |
| 3465 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3466 | # You should always have a space after a comma (either as fn arg or operator) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3467 | # |
| 3468 | # This does not apply when the non-space character following the |
| 3469 | # comma is another comma, since the only time when that happens is |
| 3470 | # for empty macro arguments. |
| 3471 | # |
| 3472 | # We run this check in two passes: first pass on elided lines to |
| 3473 | # verify that lines contain missing whitespaces, second pass on raw |
| 3474 | # lines to confirm that those missing whitespaces are not due to |
| 3475 | # elided comments. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 3476 | if (Search(r',[^,\s]', ReplaceAll(r'\boperator\s*,\s*\(', 'F(', line)) and |
| 3477 | Search(r',[^,\s]', raw[linenum])): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3478 | error(filename, linenum, 'whitespace/comma', 3, |
| 3479 | 'Missing space after ,') |
| 3480 | |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3481 | # You should always have a space after a semicolon |
| 3482 | # except for few corner cases |
| 3483 | # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more |
| 3484 | # space after ; |
| 3485 | if Search(r';[^\s};\\)/]', line): |
| 3486 | error(filename, linenum, 'whitespace/semicolon', 3, |
| 3487 | 'Missing space after ;') |
| 3488 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3489 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3490 | def _IsType(clean_lines, nesting_state, expr): |
| 3491 | """Check if expression looks like a type name, returns true if so. |
| 3492 | |
| 3493 | Args: |
| 3494 | clean_lines: A CleansedLines instance containing the file. |
| 3495 | nesting_state: A NestingState instance which maintains information about |
| 3496 | the current stack of nested blocks being parsed. |
| 3497 | expr: The expression to check. |
| 3498 | Returns: |
| 3499 | True, if token looks like a type. |
| 3500 | """ |
| 3501 | # Keep only the last token in the expression |
| 3502 | last_word = Match(r'^.*(\b\S+)$', expr) |
| 3503 | if last_word: |
| 3504 | token = last_word.group(1) |
| 3505 | else: |
| 3506 | token = expr |
| 3507 | |
| 3508 | # Match native types and stdint types |
| 3509 | if _TYPES.match(token): |
| 3510 | return True |
| 3511 | |
| 3512 | # Try a bit harder to match templated types. Walk up the nesting |
| 3513 | # stack until we find something that resembles a typename |
| 3514 | # declaration for what we are looking for. |
| 3515 | typename_pattern = (r'\b(?:typename|class|struct)\s+' + re.escape(token) + |
| 3516 | r'\b') |
| 3517 | block_index = len(nesting_state.stack) - 1 |
| 3518 | while block_index >= 0: |
| 3519 | if isinstance(nesting_state.stack[block_index], _NamespaceInfo): |
| 3520 | return False |
| 3521 | |
| 3522 | # Found where the opening brace is. We want to scan from this |
| 3523 | # line up to the beginning of the function, minus a few lines. |
| 3524 | # template <typename Type1, // stop scanning here |
| 3525 | # ...> |
| 3526 | # class C |
| 3527 | # : public ... { // start scanning here |
| 3528 | last_line = nesting_state.stack[block_index].starting_linenum |
| 3529 | |
| 3530 | next_block_start = 0 |
| 3531 | if block_index > 0: |
| 3532 | next_block_start = nesting_state.stack[block_index - 1].starting_linenum |
| 3533 | first_line = last_line |
| 3534 | while first_line >= next_block_start: |
| 3535 | if clean_lines.elided[first_line].find('template') >= 0: |
| 3536 | break |
| 3537 | first_line -= 1 |
| 3538 | if first_line < next_block_start: |
| 3539 | # Didn't find any "template" keyword before reaching the next block, |
| 3540 | # there are probably no template things to check for this block |
| 3541 | block_index -= 1 |
| 3542 | continue |
| 3543 | |
| 3544 | # Look for typename in the specified range |
| 3545 | for i in xrange(first_line, last_line + 1, 1): |
| 3546 | if Search(typename_pattern, clean_lines.elided[i]): |
| 3547 | return True |
| 3548 | block_index -= 1 |
| 3549 | |
| 3550 | return False |
| 3551 | |
| 3552 | |
| 3553 | def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3554 | """Checks for horizontal spacing near commas. |
| 3555 | |
| 3556 | Args: |
| 3557 | filename: The name of the current file. |
| 3558 | clean_lines: A CleansedLines instance containing the file. |
| 3559 | linenum: The number of the line to check. |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3560 | nesting_state: A NestingState instance which maintains information about |
| 3561 | the current stack of nested blocks being parsed. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3562 | error: The function to call with any errors found. |
| 3563 | """ |
| 3564 | line = clean_lines.elided[linenum] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3565 | |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3566 | # Except after an opening paren, or after another opening brace (in case of |
| 3567 | # an initializer list, for instance), you should have spaces before your |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3568 | # braces when they are delimiting blocks, classes, namespaces etc. |
| 3569 | # And since you should never have braces at the beginning of a line, |
| 3570 | # this is an easy test. Except that braces used for initialization don't |
| 3571 | # follow the same rule; we often don't want spaces before those. |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 3572 | match = Match(r'^(.*[^ ({>]){', line) |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3573 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3574 | if match: |
| 3575 | # Try a bit harder to check for brace initialization. This |
| 3576 | # happens in one of the following forms: |
| 3577 | # Constructor() : initializer_list_{} { ... } |
| 3578 | # Constructor{}.MemberFunction() |
| 3579 | # Type variable{}; |
| 3580 | # FunctionCall(type{}, ...); |
| 3581 | # LastArgument(..., type{}); |
| 3582 | # LOG(INFO) << type{} << " ..."; |
| 3583 | # map_of_type[{...}] = ...; |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3584 | # ternary = expr ? new type{} : nullptr; |
| 3585 | # OuterTemplate<InnerTemplateConstructor<Type>{}> |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3586 | # |
| 3587 | # We check for the character following the closing brace, and |
| 3588 | # silence the warning if it's one of those listed above, i.e. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3589 | # "{.;,)<>]:". |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3590 | # |
| 3591 | # To account for nested initializer list, we allow any number of |
| 3592 | # closing braces up to "{;,)<". We can't simply silence the |
| 3593 | # warning on first sight of closing brace, because that would |
| 3594 | # cause false negatives for things that are not initializer lists. |
| 3595 | # Silence this: But not this: |
| 3596 | # Outer{ if (...) { |
| 3597 | # Inner{...} if (...){ // Missing space before { |
| 3598 | # }; } |
| 3599 | # |
| 3600 | # There is a false negative with this approach if people inserted |
| 3601 | # spurious semicolons, e.g. "if (cond){};", but we will catch the |
| 3602 | # spurious semicolon with a separate check. |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3603 | leading_text = match.group(1) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3604 | (endline, endlinenum, endpos) = CloseExpression( |
| 3605 | clean_lines, linenum, len(match.group(1))) |
| 3606 | trailing_text = '' |
| 3607 | if endpos > -1: |
| 3608 | trailing_text = endline[endpos:] |
| 3609 | for offset in xrange(endlinenum + 1, |
| 3610 | min(endlinenum + 3, clean_lines.NumLines() - 1)): |
| 3611 | trailing_text += clean_lines.elided[offset] |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3612 | # We also suppress warnings for `uint64_t{expression}` etc., as the style |
| 3613 | # guide recommends brace initialization for integral types to avoid |
| 3614 | # overflow/truncation. |
| 3615 | if (not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text) |
| 3616 | and not _IsType(clean_lines, nesting_state, leading_text)): |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3617 | error(filename, linenum, 'whitespace/braces', 5, |
| 3618 | 'Missing space before {') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3619 | |
| 3620 | # Make sure '} else {' has spaces. |
| 3621 | if Search(r'}else', line): |
| 3622 | error(filename, linenum, 'whitespace/braces', 5, |
| 3623 | 'Missing space before else') |
| 3624 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3625 | # You shouldn't have a space before a semicolon at the end of the line. |
| 3626 | # There's a special case for "for" since the style guide allows space before |
| 3627 | # the semicolon there. |
| 3628 | if Search(r':\s*;\s*$', line): |
| 3629 | error(filename, linenum, 'whitespace/semicolon', 5, |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3630 | 'Semicolon defining empty statement. Use {} instead.') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3631 | elif Search(r'^\s*;\s*$', line): |
| 3632 | error(filename, linenum, 'whitespace/semicolon', 5, |
| 3633 | 'Line contains only semicolon. If this should be an empty statement, ' |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3634 | 'use {} instead.') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3635 | elif (Search(r'\s+;\s*$', line) and |
| 3636 | not Search(r'\bfor\b', line)): |
| 3637 | error(filename, linenum, 'whitespace/semicolon', 5, |
| 3638 | 'Extra space before last semicolon. If this should be an empty ' |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3639 | 'statement, use {} instead.') |
| 3640 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3641 | |
| 3642 | def IsDecltype(clean_lines, linenum, column): |
| 3643 | """Check if the token ending on (linenum, column) is decltype(). |
| 3644 | |
| 3645 | Args: |
| 3646 | clean_lines: A CleansedLines instance containing the file. |
| 3647 | linenum: the number of the line to check. |
| 3648 | column: end column of the token to check. |
| 3649 | Returns: |
| 3650 | True if this token is decltype() expression, False otherwise. |
| 3651 | """ |
| 3652 | (text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column) |
| 3653 | if start_col < 0: |
| 3654 | return False |
| 3655 | if Search(r'\bdecltype\s*$', text[0:start_col]): |
| 3656 | return True |
| 3657 | return False |
| 3658 | |
| 3659 | |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3660 | def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error): |
| 3661 | """Checks for additional blank line issues related to sections. |
| 3662 | |
| 3663 | Currently the only thing checked here is blank line before protected/private. |
| 3664 | |
| 3665 | Args: |
| 3666 | filename: The name of the current file. |
| 3667 | clean_lines: A CleansedLines instance containing the file. |
| 3668 | class_info: A _ClassInfo objects. |
| 3669 | linenum: The number of the line to check. |
| 3670 | error: The function to call with any errors found. |
| 3671 | """ |
| 3672 | # Skip checks if the class is small, where small means 25 lines or less. |
| 3673 | # 25 lines seems like a good cutoff since that's the usual height of |
| 3674 | # terminals, and any class that can't fit in one screen can't really |
| 3675 | # be considered "small". |
| 3676 | # |
| 3677 | # Also skip checks if we are on the first line. This accounts for |
| 3678 | # classes that look like |
| 3679 | # class Foo { public: ... }; |
| 3680 | # |
| 3681 | # If we didn't find the end of the class, last_line would be zero, |
| 3682 | # and the check will be skipped by the first condition. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3683 | if (class_info.last_line - class_info.starting_linenum <= 24 or |
| 3684 | linenum <= class_info.starting_linenum): |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3685 | return |
| 3686 | |
| 3687 | matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum]) |
| 3688 | if matched: |
| 3689 | # Issue warning if the line before public/protected/private was |
| 3690 | # not a blank line, but don't do this if the previous line contains |
| 3691 | # "class" or "struct". This can happen two ways: |
| 3692 | # - We are at the beginning of the class. |
| 3693 | # - We are forward-declaring an inner class that is semantically |
| 3694 | # private, but needed to be public for implementation reasons. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3695 | # Also ignores cases where the previous line ends with a backslash as can be |
| 3696 | # common when defining classes in C macros. |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3697 | prev_line = clean_lines.lines[linenum - 1] |
| 3698 | if (not IsBlankLine(prev_line) and |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3699 | not Search(r'\b(class|struct)\b', prev_line) and |
| 3700 | not Search(r'\\$', prev_line)): |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3701 | # Try a bit harder to find the beginning of the class. This is to |
| 3702 | # account for multi-line base-specifier lists, e.g.: |
| 3703 | # class Derived |
| 3704 | # : public Base { |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 3705 | end_class_head = class_info.starting_linenum |
| 3706 | for i in range(class_info.starting_linenum, linenum): |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 3707 | if Search(r'\{\s*$', clean_lines.lines[i]): |
| 3708 | end_class_head = i |
| 3709 | break |
| 3710 | if end_class_head < linenum - 1: |
| 3711 | error(filename, linenum, 'whitespace/blank_line', 3, |
| 3712 | '"%s:" should be preceded by a blank line' % matched.group(1)) |
| 3713 | |
| 3714 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3715 | def GetPreviousNonBlankLine(clean_lines, linenum): |
| 3716 | """Return the most recent non-blank line and its line number. |
| 3717 | |
| 3718 | Args: |
| 3719 | clean_lines: A CleansedLines instance containing the file contents. |
| 3720 | linenum: The number of the line to check. |
| 3721 | |
| 3722 | Returns: |
| 3723 | A tuple with two elements. The first element is the contents of the last |
| 3724 | non-blank line before the current line, or the empty string if this is the |
| 3725 | first non-blank line. The second is the line number of that line, or -1 |
| 3726 | if this is the first non-blank line. |
| 3727 | """ |
| 3728 | |
| 3729 | prevlinenum = linenum - 1 |
| 3730 | while prevlinenum >= 0: |
| 3731 | prevline = clean_lines.elided[prevlinenum] |
| 3732 | if not IsBlankLine(prevline): # if not a blank line... |
| 3733 | return (prevline, prevlinenum) |
| 3734 | prevlinenum -= 1 |
| 3735 | return ('', -1) |
| 3736 | |
| 3737 | |
| 3738 | def CheckBraces(filename, clean_lines, linenum, error): |
| 3739 | """Looks for misplaced braces (e.g. at the end of line). |
| 3740 | |
| 3741 | Args: |
| 3742 | filename: The name of the current file. |
| 3743 | clean_lines: A CleansedLines instance containing the file. |
| 3744 | linenum: The number of the line to check. |
| 3745 | error: The function to call with any errors found. |
| 3746 | """ |
| 3747 | |
| 3748 | line = clean_lines.elided[linenum] # get rid of comments and strings |
| 3749 | |
| 3750 | if Match(r'\s*{\s*$', line): |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3751 | # We allow an open brace to start a line in the case where someone is using |
| 3752 | # braces in a block to explicitly create a new scope, which is commonly used |
| 3753 | # to control the lifetime of stack-allocated variables. Braces are also |
| 3754 | # used for brace initializers inside function calls. We don't detect this |
| 3755 | # perfectly: we just don't complain if the last non-whitespace character on |
| 3756 | # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3757 | # previous line starts a preprocessor block. We also allow a brace on the |
| 3758 | # following line if it is part of an array initialization and would not fit |
| 3759 | # within the 80 character limit of the preceding line. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3760 | prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3761 | if (not Search(r'[,;:}{(]\s*$', prevline) and |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3762 | not Match(r'\s*#', prevline) and |
| 3763 | not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3764 | error(filename, linenum, 'whitespace/braces', 4, |
| 3765 | '{ should almost always be at the end of the previous line') |
| 3766 | |
| 3767 | # An else clause should be on the same line as the preceding closing brace. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3768 | if Match(r'\s*else\b\s*(?:if\b|\{|$)', line): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3769 | prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] |
| 3770 | if Match(r'\s*}\s*$', prevline): |
| 3771 | error(filename, linenum, 'whitespace/newline', 4, |
| 3772 | 'An else should appear on the same line as the preceding }') |
| 3773 | |
| 3774 | # If braces come on one side of an else, they should be on both. |
| 3775 | # However, we have to worry about "else if" that spans multiple lines! |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3776 | if Search(r'else if\s*\(', line): # could be multi-line if |
| 3777 | brace_on_left = bool(Search(r'}\s*else if\s*\(', line)) |
| 3778 | # find the ( after the if |
| 3779 | pos = line.find('else if') |
| 3780 | pos = line.find('(', pos) |
| 3781 | if pos > 0: |
| 3782 | (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos) |
| 3783 | brace_on_right = endline[endpos:].find('{') != -1 |
| 3784 | if brace_on_left != brace_on_right: # must be brace after if |
| 3785 | error(filename, linenum, 'readability/braces', 5, |
| 3786 | 'If an else has a brace on one side, it should have it on both') |
| 3787 | elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line): |
| 3788 | error(filename, linenum, 'readability/braces', 5, |
| 3789 | 'If an else has a brace on one side, it should have it on both') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3790 | |
| 3791 | # Likewise, an else should never have the else clause on the same line |
| 3792 | if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line): |
| 3793 | error(filename, linenum, 'whitespace/newline', 4, |
| 3794 | 'Else clause should never be on same line as else (use 2 lines)') |
| 3795 | |
| 3796 | # In the same way, a do/while should never be on one line |
| 3797 | if Match(r'\s*do [^\s{]', line): |
| 3798 | error(filename, linenum, 'whitespace/newline', 4, |
| 3799 | 'do/while clauses should not be on a single line') |
| 3800 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3801 | # Check single-line if/else bodies. The style guide says 'curly braces are not |
| 3802 | # required for single-line statements'. We additionally allow multi-line, |
| 3803 | # single statements, but we reject anything with more than one semicolon in |
| 3804 | # it. This means that the first semicolon after the if should be at the end of |
| 3805 | # its line, and the line after that should have an indent level equal to or |
| 3806 | # lower than the if. We also check for ambiguous if/else nesting without |
| 3807 | # braces. |
| 3808 | if_else_match = Search(r'\b(if\s*\(|else\b)', line) |
| 3809 | if if_else_match and not Match(r'\s*#', line): |
| 3810 | if_indent = GetIndentLevel(line) |
| 3811 | endline, endlinenum, endpos = line, linenum, if_else_match.end() |
| 3812 | if_match = Search(r'\bif\s*\(', line) |
| 3813 | if if_match: |
| 3814 | # This could be a multiline if condition, so find the end first. |
| 3815 | pos = if_match.end() - 1 |
| 3816 | (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, pos) |
| 3817 | # Check for an opening brace, either directly after the if or on the next |
| 3818 | # line. If found, this isn't a single-statement conditional. |
| 3819 | if (not Match(r'\s*{', endline[endpos:]) |
| 3820 | and not (Match(r'\s*$', endline[endpos:]) |
| 3821 | and endlinenum < (len(clean_lines.elided) - 1) |
| 3822 | and Match(r'\s*{', clean_lines.elided[endlinenum + 1]))): |
| 3823 | while (endlinenum < len(clean_lines.elided) |
| 3824 | and ';' not in clean_lines.elided[endlinenum][endpos:]): |
| 3825 | endlinenum += 1 |
| 3826 | endpos = 0 |
| 3827 | if endlinenum < len(clean_lines.elided): |
| 3828 | endline = clean_lines.elided[endlinenum] |
| 3829 | # We allow a mix of whitespace and closing braces (e.g. for one-liner |
| 3830 | # methods) and a single \ after the semicolon (for macros) |
| 3831 | endpos = endline.find(';') |
| 3832 | if not Match(r';[\s}]*(\\?)$', endline[endpos:]): |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 3833 | # Semicolon isn't the last character, there's something trailing. |
| 3834 | # Output a warning if the semicolon is not contained inside |
| 3835 | # a lambda expression. |
| 3836 | if not Match(r'^[^{};]*\[[^\[\]]*\][^{}]*\{[^{}]*\}\s*\)*[;,]\s*$', |
| 3837 | endline): |
| 3838 | error(filename, linenum, 'readability/braces', 4, |
| 3839 | 'If/else bodies with multiple statements require braces') |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3840 | elif endlinenum < len(clean_lines.elided) - 1: |
| 3841 | # Make sure the next line is dedented |
| 3842 | next_line = clean_lines.elided[endlinenum + 1] |
| 3843 | next_indent = GetIndentLevel(next_line) |
| 3844 | # With ambiguous nested if statements, this will error out on the |
| 3845 | # if that *doesn't* match the else, regardless of whether it's the |
| 3846 | # inner one or outer one. |
| 3847 | if (if_match and Match(r'\s*else\b', next_line) |
| 3848 | and next_indent != if_indent): |
| 3849 | error(filename, linenum, 'readability/braces', 4, |
| 3850 | 'Else clause should be indented at the same level as if. ' |
| 3851 | 'Ambiguous nested if/else chains require braces.') |
| 3852 | elif next_indent > if_indent: |
| 3853 | error(filename, linenum, 'readability/braces', 4, |
| 3854 | 'If/else bodies with multiple statements require braces') |
| 3855 | |
| 3856 | |
| 3857 | def CheckTrailingSemicolon(filename, clean_lines, linenum, error): |
| 3858 | """Looks for redundant trailing semicolon. |
| 3859 | |
| 3860 | Args: |
| 3861 | filename: The name of the current file. |
| 3862 | clean_lines: A CleansedLines instance containing the file. |
| 3863 | linenum: The number of the line to check. |
| 3864 | error: The function to call with any errors found. |
| 3865 | """ |
| 3866 | |
| 3867 | line = clean_lines.elided[linenum] |
| 3868 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3869 | # Block bodies should not be followed by a semicolon. Due to C++11 |
| 3870 | # brace initialization, there are more places where semicolons are |
| 3871 | # required than not, so we use a whitelist approach to check these |
| 3872 | # rather than a blacklist. These are the places where "};" should |
| 3873 | # be replaced by just "}": |
| 3874 | # 1. Some flavor of block following closing parenthesis: |
| 3875 | # for (;;) {}; |
| 3876 | # while (...) {}; |
| 3877 | # switch (...) {}; |
| 3878 | # Function(...) {}; |
| 3879 | # if (...) {}; |
| 3880 | # if (...) else if (...) {}; |
| 3881 | # |
| 3882 | # 2. else block: |
| 3883 | # if (...) else {}; |
| 3884 | # |
| 3885 | # 3. const member function: |
| 3886 | # Function(...) const {}; |
| 3887 | # |
| 3888 | # 4. Block following some statement: |
| 3889 | # x = 42; |
| 3890 | # {}; |
| 3891 | # |
| 3892 | # 5. Block at the beginning of a function: |
| 3893 | # Function(...) { |
| 3894 | # {}; |
| 3895 | # } |
| 3896 | # |
| 3897 | # Note that naively checking for the preceding "{" will also match |
| 3898 | # braces inside multi-dimensional arrays, but this is fine since |
| 3899 | # that expression will not contain semicolons. |
| 3900 | # |
| 3901 | # 6. Block following another block: |
| 3902 | # while (true) {} |
| 3903 | # {}; |
| 3904 | # |
| 3905 | # 7. End of namespaces: |
| 3906 | # namespace {}; |
| 3907 | # |
| 3908 | # These semicolons seems far more common than other kinds of |
| 3909 | # redundant semicolons, possibly due to people converting classes |
| 3910 | # to namespaces. For now we do not warn for this case. |
| 3911 | # |
| 3912 | # Try matching case 1 first. |
| 3913 | match = Match(r'^(.*\)\s*)\{', line) |
| 3914 | if match: |
| 3915 | # Matched closing parenthesis (case 1). Check the token before the |
| 3916 | # matching opening parenthesis, and don't warn if it looks like a |
| 3917 | # macro. This avoids these false positives: |
| 3918 | # - macro that defines a base class |
| 3919 | # - multi-line macro that defines a base class |
| 3920 | # - macro that defines the whole class-head |
| 3921 | # |
| 3922 | # But we still issue warnings for macros that we know are safe to |
| 3923 | # warn, specifically: |
| 3924 | # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P |
| 3925 | # - TYPED_TEST |
| 3926 | # - INTERFACE_DEF |
| 3927 | # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED: |
| 3928 | # |
| 3929 | # We implement a whitelist of safe macros instead of a blacklist of |
| 3930 | # unsafe macros, even though the latter appears less frequently in |
| 3931 | # google code and would have been easier to implement. This is because |
| 3932 | # the downside for getting the whitelist wrong means some extra |
| 3933 | # semicolons, while the downside for getting the blacklist wrong |
| 3934 | # would result in compile errors. |
| 3935 | # |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 3936 | # In addition to macros, we also don't want to warn on |
| 3937 | # - Compound literals |
| 3938 | # - Lambdas |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3939 | # - alignas specifier with anonymous structs |
| 3940 | # - decltype |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3941 | closing_brace_pos = match.group(1).rfind(')') |
| 3942 | opening_parenthesis = ReverseCloseExpression( |
| 3943 | clean_lines, linenum, closing_brace_pos) |
| 3944 | if opening_parenthesis[2] > -1: |
| 3945 | line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]] |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3946 | macro = Search(r'\b([A-Z_][A-Z0-9_]*)\s*$', line_prefix) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3947 | func = Match(r'^(.*\])\s*$', line_prefix) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3948 | if ((macro and |
| 3949 | macro.group(1) not in ( |
| 3950 | 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST', |
| 3951 | 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED', |
| 3952 | 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3953 | (func and not Search(r'\boperator\s*\[\s*\]', func.group(1))) or |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 3954 | Search(r'\b(?:struct|union)\s+alignas\s*$', line_prefix) or |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 3955 | Search(r'\bdecltype$', line_prefix) or |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3956 | Search(r'\s+=\s*$', line_prefix)): |
| 3957 | match = None |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 3958 | if (match and |
| 3959 | opening_parenthesis[1] > 1 and |
| 3960 | Search(r'\]\s*$', clean_lines.elided[opening_parenthesis[1] - 1])): |
| 3961 | # Multi-line lambda-expression |
| 3962 | match = None |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 3963 | |
| 3964 | else: |
| 3965 | # Try matching cases 2-3. |
| 3966 | match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line) |
| 3967 | if not match: |
| 3968 | # Try matching cases 4-6. These are always matched on separate lines. |
| 3969 | # |
| 3970 | # Note that we can't simply concatenate the previous line to the |
| 3971 | # current line and do a single match, otherwise we may output |
| 3972 | # duplicate warnings for the blank line case: |
| 3973 | # if (cond) { |
| 3974 | # // blank line |
| 3975 | # } |
| 3976 | prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] |
| 3977 | if prevline and Search(r'[;{}]\s*$', prevline): |
| 3978 | match = Match(r'^(\s*)\{', line) |
| 3979 | |
| 3980 | # Check matching closing brace |
| 3981 | if match: |
| 3982 | (endline, endlinenum, endpos) = CloseExpression( |
| 3983 | clean_lines, linenum, len(match.group(1))) |
| 3984 | if endpos > -1 and Match(r'^\s*;', endline[endpos:]): |
| 3985 | # Current {} pair is eligible for semicolon check, and we have found |
| 3986 | # the redundant semicolon, output warning here. |
| 3987 | # |
| 3988 | # Note: because we are scanning forward for opening braces, and |
| 3989 | # outputting warnings for the matching closing brace, if there are |
| 3990 | # nested blocks with trailing semicolons, we will get the error |
| 3991 | # messages in reversed order. |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 3992 | |
| 3993 | # We need to check the line forward for NOLINT |
| 3994 | raw_lines = clean_lines.raw_lines |
| 3995 | ParseNolintSuppressions(filename, raw_lines[endlinenum-1], endlinenum-1, |
| 3996 | error) |
| 3997 | ParseNolintSuppressions(filename, raw_lines[endlinenum], endlinenum, |
| 3998 | error) |
| 3999 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4000 | error(filename, endlinenum, 'readability/braces', 4, |
| 4001 | "You don't need a ; after a }") |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4002 | |
| 4003 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4004 | def CheckEmptyBlockBody(filename, clean_lines, linenum, error): |
| 4005 | """Look for empty loop/conditional body with only a single semicolon. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 4006 | |
| 4007 | Args: |
| 4008 | filename: The name of the current file. |
| 4009 | clean_lines: A CleansedLines instance containing the file. |
| 4010 | linenum: The number of the line to check. |
| 4011 | error: The function to call with any errors found. |
| 4012 | """ |
| 4013 | |
| 4014 | # Search for loop keywords at the beginning of the line. Because only |
| 4015 | # whitespaces are allowed before the keywords, this will also ignore most |
| 4016 | # do-while-loops, since those lines should start with closing brace. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4017 | # |
| 4018 | # We also check "if" blocks here, since an empty conditional block |
| 4019 | # is likely an error. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 4020 | line = clean_lines.elided[linenum] |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4021 | matched = Match(r'\s*(for|while|if)\s*\(', line) |
| 4022 | if matched: |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4023 | # Find the end of the conditional expression. |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 4024 | (end_line, end_linenum, end_pos) = CloseExpression( |
| 4025 | clean_lines, linenum, line.find('(')) |
| 4026 | |
| 4027 | # Output warning if what follows the condition expression is a semicolon. |
| 4028 | # No warning for all other cases, including whitespace or newline, since we |
| 4029 | # have a separate check for semicolons preceded by whitespace. |
| 4030 | if end_pos >= 0 and Match(r';', end_line[end_pos:]): |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4031 | if matched.group(1) == 'if': |
| 4032 | error(filename, end_linenum, 'whitespace/empty_conditional_body', 5, |
| 4033 | 'Empty conditional bodies should use {}') |
| 4034 | else: |
| 4035 | error(filename, end_linenum, 'whitespace/empty_loop_body', 5, |
| 4036 | 'Empty loop bodies should use {} or continue') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4037 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4038 | # Check for if statements that have completely empty bodies (no comments) |
| 4039 | # and no else clauses. |
| 4040 | if end_pos >= 0 and matched.group(1) == 'if': |
| 4041 | # Find the position of the opening { for the if statement. |
| 4042 | # Return without logging an error if it has no brackets. |
| 4043 | opening_linenum = end_linenum |
| 4044 | opening_line_fragment = end_line[end_pos:] |
| 4045 | # Loop until EOF or find anything that's not whitespace or opening {. |
| 4046 | while not Search(r'^\s*\{', opening_line_fragment): |
| 4047 | if Search(r'^(?!\s*$)', opening_line_fragment): |
| 4048 | # Conditional has no brackets. |
| 4049 | return |
| 4050 | opening_linenum += 1 |
| 4051 | if opening_linenum == len(clean_lines.elided): |
| 4052 | # Couldn't find conditional's opening { or any code before EOF. |
| 4053 | return |
| 4054 | opening_line_fragment = clean_lines.elided[opening_linenum] |
| 4055 | # Set opening_line (opening_line_fragment may not be entire opening line). |
| 4056 | opening_line = clean_lines.elided[opening_linenum] |
| 4057 | |
| 4058 | # Find the position of the closing }. |
| 4059 | opening_pos = opening_line_fragment.find('{') |
| 4060 | if opening_linenum == end_linenum: |
| 4061 | # We need to make opening_pos relative to the start of the entire line. |
| 4062 | opening_pos += end_pos |
| 4063 | (closing_line, closing_linenum, closing_pos) = CloseExpression( |
| 4064 | clean_lines, opening_linenum, opening_pos) |
| 4065 | if closing_pos < 0: |
| 4066 | return |
| 4067 | |
| 4068 | # Now construct the body of the conditional. This consists of the portion |
| 4069 | # of the opening line after the {, all lines until the closing line, |
| 4070 | # and the portion of the closing line before the }. |
| 4071 | if (clean_lines.raw_lines[opening_linenum] != |
| 4072 | CleanseComments(clean_lines.raw_lines[opening_linenum])): |
| 4073 | # Opening line ends with a comment, so conditional isn't empty. |
| 4074 | return |
| 4075 | if closing_linenum > opening_linenum: |
| 4076 | # Opening line after the {. Ignore comments here since we checked above. |
| 4077 | body = list(opening_line[opening_pos+1:]) |
| 4078 | # All lines until closing line, excluding closing line, with comments. |
| 4079 | body.extend(clean_lines.raw_lines[opening_linenum+1:closing_linenum]) |
| 4080 | # Closing line before the }. Won't (and can't) have comments. |
| 4081 | body.append(clean_lines.elided[closing_linenum][:closing_pos-1]) |
| 4082 | body = '\n'.join(body) |
| 4083 | else: |
| 4084 | # If statement has brackets and fits on a single line. |
| 4085 | body = opening_line[opening_pos+1:closing_pos-1] |
| 4086 | |
| 4087 | # Check if the body is empty |
| 4088 | if not _EMPTY_CONDITIONAL_BODY_PATTERN.search(body): |
| 4089 | return |
| 4090 | # The body is empty. Now make sure there's not an else clause. |
| 4091 | current_linenum = closing_linenum |
| 4092 | current_line_fragment = closing_line[closing_pos:] |
| 4093 | # Loop until EOF or find anything that's not whitespace or else clause. |
| 4094 | while Search(r'^\s*$|^(?=\s*else)', current_line_fragment): |
| 4095 | if Search(r'^(?=\s*else)', current_line_fragment): |
| 4096 | # Found an else clause, so don't log an error. |
| 4097 | return |
| 4098 | current_linenum += 1 |
| 4099 | if current_linenum == len(clean_lines.elided): |
| 4100 | break |
| 4101 | current_line_fragment = clean_lines.elided[current_linenum] |
| 4102 | |
| 4103 | # The body is empty and there's no else clause until EOF or other code. |
| 4104 | error(filename, end_linenum, 'whitespace/empty_if_body', 4, |
| 4105 | ('If statement had no body and no else clause')) |
| 4106 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4107 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4108 | def FindCheckMacro(line): |
| 4109 | """Find a replaceable CHECK-like macro. |
| 4110 | |
| 4111 | Args: |
| 4112 | line: line to search on. |
| 4113 | Returns: |
| 4114 | (macro name, start position), or (None, -1) if no replaceable |
| 4115 | macro is found. |
| 4116 | """ |
| 4117 | for macro in _CHECK_MACROS: |
| 4118 | i = line.find(macro) |
| 4119 | if i >= 0: |
| 4120 | # Find opening parenthesis. Do a regular expression match here |
| 4121 | # to make sure that we are matching the expected CHECK macro, as |
| 4122 | # opposed to some other macro that happens to contain the CHECK |
| 4123 | # substring. |
| 4124 | matched = Match(r'^(.*\b' + macro + r'\s*)\(', line) |
| 4125 | if not matched: |
| 4126 | continue |
| 4127 | return (macro, len(matched.group(1))) |
| 4128 | return (None, -1) |
| 4129 | |
| 4130 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4131 | def CheckCheck(filename, clean_lines, linenum, error): |
| 4132 | """Checks the use of CHECK and EXPECT macros. |
| 4133 | |
| 4134 | Args: |
| 4135 | filename: The name of the current file. |
| 4136 | clean_lines: A CleansedLines instance containing the file. |
| 4137 | linenum: The number of the line to check. |
| 4138 | error: The function to call with any errors found. |
| 4139 | """ |
| 4140 | |
| 4141 | # Decide the set of replacement macros that should be suggested |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4142 | lines = clean_lines.elided |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4143 | (check_macro, start_pos) = FindCheckMacro(lines[linenum]) |
| 4144 | if not check_macro: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4145 | return |
| 4146 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4147 | # Find end of the boolean expression by matching parentheses |
| 4148 | (last_line, end_line, end_pos) = CloseExpression( |
| 4149 | clean_lines, linenum, start_pos) |
| 4150 | if end_pos < 0: |
| 4151 | return |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4152 | |
| 4153 | # If the check macro is followed by something other than a |
| 4154 | # semicolon, assume users will log their own custom error messages |
| 4155 | # and don't suggest any replacements. |
| 4156 | if not Match(r'\s*;', last_line[end_pos:]): |
| 4157 | return |
| 4158 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4159 | if linenum == end_line: |
| 4160 | expression = lines[linenum][start_pos + 1:end_pos - 1] |
| 4161 | else: |
| 4162 | expression = lines[linenum][start_pos + 1:] |
| 4163 | for i in xrange(linenum + 1, end_line): |
| 4164 | expression += lines[i] |
| 4165 | expression += last_line[0:end_pos - 1] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4166 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4167 | # Parse expression so that we can take parentheses into account. |
| 4168 | # This avoids false positives for inputs like "CHECK((a < 4) == b)", |
| 4169 | # which is not replaceable by CHECK_LE. |
| 4170 | lhs = '' |
| 4171 | rhs = '' |
| 4172 | operator = None |
| 4173 | while expression: |
| 4174 | matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||' |
| 4175 | r'==|!=|>=|>|<=|<|\()(.*)$', expression) |
| 4176 | if matched: |
| 4177 | token = matched.group(1) |
| 4178 | if token == '(': |
| 4179 | # Parenthesized operand |
| 4180 | expression = matched.group(2) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4181 | (end, _) = FindEndOfExpressionInLine(expression, 0, ['(']) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4182 | if end < 0: |
| 4183 | return # Unmatched parenthesis |
| 4184 | lhs += '(' + expression[0:end] |
| 4185 | expression = expression[end:] |
| 4186 | elif token in ('&&', '||'): |
| 4187 | # Logical and/or operators. This means the expression |
| 4188 | # contains more than one term, for example: |
| 4189 | # CHECK(42 < a && a < b); |
| 4190 | # |
| 4191 | # These are not replaceable with CHECK_LE, so bail out early. |
| 4192 | return |
| 4193 | elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'): |
| 4194 | # Non-relational operator |
| 4195 | lhs += token |
| 4196 | expression = matched.group(2) |
| 4197 | else: |
| 4198 | # Relational operator |
| 4199 | operator = token |
| 4200 | rhs = matched.group(2) |
| 4201 | break |
| 4202 | else: |
| 4203 | # Unparenthesized operand. Instead of appending to lhs one character |
| 4204 | # at a time, we do another regular expression match to consume several |
| 4205 | # characters at once if possible. Trivial benchmark shows that this |
| 4206 | # is more efficient when the operands are longer than a single |
| 4207 | # character, which is generally the case. |
| 4208 | matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression) |
| 4209 | if not matched: |
| 4210 | matched = Match(r'^(\s*\S)(.*)$', expression) |
| 4211 | if not matched: |
| 4212 | break |
| 4213 | lhs += matched.group(1) |
| 4214 | expression = matched.group(2) |
| 4215 | |
| 4216 | # Only apply checks if we got all parts of the boolean expression |
| 4217 | if not (lhs and operator and rhs): |
| 4218 | return |
| 4219 | |
| 4220 | # Check that rhs do not contain logical operators. We already know |
| 4221 | # that lhs is fine since the loop above parses out && and ||. |
| 4222 | if rhs.find('&&') > -1 or rhs.find('||') > -1: |
| 4223 | return |
| 4224 | |
| 4225 | # At least one of the operands must be a constant literal. This is |
| 4226 | # to avoid suggesting replacements for unprintable things like |
| 4227 | # CHECK(variable != iterator) |
| 4228 | # |
| 4229 | # The following pattern matches decimal, hex integers, strings, and |
| 4230 | # characters (in that order). |
| 4231 | lhs = lhs.strip() |
| 4232 | rhs = rhs.strip() |
| 4233 | match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$' |
| 4234 | if Match(match_constant, lhs) or Match(match_constant, rhs): |
| 4235 | # Note: since we know both lhs and rhs, we can provide a more |
| 4236 | # descriptive error message like: |
| 4237 | # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42) |
| 4238 | # Instead of: |
| 4239 | # Consider using CHECK_EQ instead of CHECK(a == b) |
| 4240 | # |
| 4241 | # We are still keeping the less descriptive message because if lhs |
| 4242 | # or rhs gets long, the error message might become unreadable. |
| 4243 | error(filename, linenum, 'readability/check', 2, |
| 4244 | 'Consider using %s instead of %s(a %s b)' % ( |
| 4245 | _CHECK_REPLACEMENT[check_macro][operator], |
| 4246 | check_macro, operator)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4247 | |
| 4248 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 4249 | def CheckAltTokens(filename, clean_lines, linenum, error): |
| 4250 | """Check alternative keywords being used in boolean expressions. |
| 4251 | |
| 4252 | Args: |
| 4253 | filename: The name of the current file. |
| 4254 | clean_lines: A CleansedLines instance containing the file. |
| 4255 | linenum: The number of the line to check. |
| 4256 | error: The function to call with any errors found. |
| 4257 | """ |
| 4258 | line = clean_lines.elided[linenum] |
| 4259 | |
| 4260 | # Avoid preprocessor lines |
| 4261 | if Match(r'^\s*#', line): |
| 4262 | return |
| 4263 | |
| 4264 | # Last ditch effort to avoid multi-line comments. This will not help |
| 4265 | # if the comment started before the current line or ended after the |
| 4266 | # current line, but it catches most of the false positives. At least, |
| 4267 | # it provides a way to workaround this warning for people who use |
| 4268 | # multi-line comments in preprocessor macros. |
| 4269 | # |
| 4270 | # TODO(unknown): remove this once cpplint has better support for |
| 4271 | # multi-line comments. |
| 4272 | if line.find('/*') >= 0 or line.find('*/') >= 0: |
| 4273 | return |
| 4274 | |
| 4275 | for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line): |
| 4276 | error(filename, linenum, 'readability/alt_tokens', 2, |
| 4277 | 'Use operator %s instead of %s' % ( |
| 4278 | _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1))) |
| 4279 | |
| 4280 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4281 | def GetLineWidth(line): |
| 4282 | """Determines the width of the line in column positions. |
| 4283 | |
| 4284 | Args: |
| 4285 | line: A string, which may be a Unicode string. |
| 4286 | |
| 4287 | Returns: |
| 4288 | The width of the line in column positions, accounting for Unicode |
| 4289 | combining characters and wide characters. |
| 4290 | """ |
| 4291 | if isinstance(line, unicode): |
| 4292 | width = 0 |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4293 | for uc in unicodedata.normalize('NFC', line): |
| 4294 | if unicodedata.east_asian_width(uc) in ('W', 'F'): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4295 | width += 2 |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4296 | elif not unicodedata.combining(uc): |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 4297 | # Issue 337 |
| 4298 | # https://mail.python.org/pipermail/python-list/2012-August/628809.html |
| 4299 | if (sys.version_info.major, sys.version_info.minor) <= (3, 2): |
| 4300 | # https://github.com/python/cpython/blob/2.7/Include/unicodeobject.h#L81 |
| 4301 | is_wide_build = sysconfig.get_config_var("Py_UNICODE_SIZE") >= 4 |
| 4302 | # https://github.com/python/cpython/blob/2.7/Objects/unicodeobject.c#L564 |
| 4303 | is_low_surrogate = 0xDC00 <= ord(uc) <= 0xDFFF |
| 4304 | if not is_wide_build and is_low_surrogate: |
| 4305 | width -= 1 |
| 4306 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4307 | width += 1 |
| 4308 | return width |
| 4309 | else: |
| 4310 | return len(line) |
| 4311 | |
| 4312 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 4313 | def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4314 | error): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4315 | """Checks rules from the 'C++ style rules' section of cppguide.html. |
| 4316 | |
| 4317 | Most of these rules are hard to test (naming, comment style), but we |
| 4318 | do what we can. In particular we check for 2-space indents, line lengths, |
| 4319 | tab usage, spaces inside code, etc. |
| 4320 | |
| 4321 | Args: |
| 4322 | filename: The name of the current file. |
| 4323 | clean_lines: A CleansedLines instance containing the file. |
| 4324 | linenum: The number of the line to check. |
| 4325 | file_extension: The extension (without the dot) of the filename. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4326 | nesting_state: A NestingState instance which maintains information about |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 4327 | the current stack of nested blocks being parsed. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4328 | error: The function to call with any errors found. |
| 4329 | """ |
| 4330 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4331 | # Don't use "elided" lines here, otherwise we can't check commented lines. |
| 4332 | # Don't want to use "raw" either, because we don't want to check inside C++11 |
| 4333 | # raw strings, |
| 4334 | raw_lines = clean_lines.lines_without_raw_strings |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4335 | line = raw_lines[linenum] |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4336 | prev = raw_lines[linenum - 1] if linenum > 0 else '' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4337 | |
| 4338 | if line.find('\t') != -1: |
| 4339 | error(filename, linenum, 'whitespace/tab', 1, |
| 4340 | 'Tab found; better to use spaces') |
| 4341 | |
| 4342 | # One or three blank spaces at the beginning of the line is weird; it's |
| 4343 | # hard to reconcile that with 2-space indents. |
| 4344 | # NOTE: here are the conditions rob pike used for his tests. Mine aren't |
| 4345 | # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces |
| 4346 | # if(RLENGTH > 20) complain = 0; |
| 4347 | # if(match($0, " +(error|private|public|protected):")) complain = 0; |
| 4348 | # if(match(prev, "&& *$")) complain = 0; |
| 4349 | # if(match(prev, "\\|\\| *$")) complain = 0; |
| 4350 | # if(match(prev, "[\",=><] *$")) complain = 0; |
| 4351 | # if(match($0, " <<")) complain = 0; |
| 4352 | # if(match(prev, " +for \\(")) complain = 0; |
| 4353 | # if(prevodd && match(prevprev, " +for \\(")) complain = 0; |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4354 | scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$' |
| 4355 | classinfo = nesting_state.InnermostClass() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4356 | initial_spaces = 0 |
| 4357 | cleansed_line = clean_lines.elided[linenum] |
| 4358 | while initial_spaces < len(line) and line[initial_spaces] == ' ': |
| 4359 | initial_spaces += 1 |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4360 | # There are certain situations we allow one space, notably for |
| 4361 | # section labels, and also lines containing multi-line raw strings. |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4362 | # We also don't check for lines that look like continuation lines |
| 4363 | # (of lines ending in double quotes, commas, equals, or angle brackets) |
| 4364 | # because the rules for how to indent those are non-trivial. |
| 4365 | if (not Search(r'[",=><] *$', prev) and |
| 4366 | (initial_spaces == 1 or initial_spaces == 3) and |
| 4367 | not Match(scope_or_label_pattern, cleansed_line) and |
| 4368 | not (clean_lines.raw_lines[linenum] != line and |
| 4369 | Match(r'^\s*""', line))): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4370 | error(filename, linenum, 'whitespace/indent', 3, |
| 4371 | 'Weird number of spaces at line-start. ' |
| 4372 | 'Are you using a 2-space indent?') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4373 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4374 | if line and line[-1].isspace(): |
| 4375 | error(filename, linenum, 'whitespace/end_of_line', 4, |
| 4376 | 'Line ends in whitespace. Consider deleting these extra spaces.') |
| 4377 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4378 | # Check if the line is a header guard. |
| 4379 | is_header_guard = False |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 4380 | if IsHeaderExtension(file_extension): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4381 | cppvar = GetHeaderGuardCPPVariable(filename) |
| 4382 | if (line.startswith('#ifndef %s' % cppvar) or |
| 4383 | line.startswith('#define %s' % cppvar) or |
| 4384 | line.startswith('#endif // %s' % cppvar)): |
| 4385 | is_header_guard = True |
| 4386 | # #include lines and header guards can be long, since there's no clean way to |
| 4387 | # split them. |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 4388 | # |
| 4389 | # URLs can be long too. It's possible to split these, but it makes them |
| 4390 | # harder to cut&paste. |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4391 | # |
| 4392 | # The "$Id:...$" comment may also get very long without it being the |
| 4393 | # developers fault. |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 4394 | if (not line.startswith('#include') and not is_header_guard and |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4395 | not Match(r'^\s*//.*http(s?)://\S*$', line) and |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4396 | not Match(r'^\s*//\s*[^\s]*$', line) and |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4397 | not Match(r'^// \$Id:.*#[0-9]+ \$$', line)): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4398 | line_width = GetLineWidth(line) |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4399 | if line_width > _line_length: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4400 | error(filename, linenum, 'whitespace/line_length', 2, |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4401 | 'Lines should be <= %i characters long' % _line_length) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4402 | |
| 4403 | if (cleansed_line.count(';') > 1 and |
| 4404 | # for loops are allowed two ;'s (and may run over two lines). |
| 4405 | cleansed_line.find('for') == -1 and |
| 4406 | (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or |
| 4407 | GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and |
| 4408 | # It's ok to have many commands in a switch case that fits in 1 line |
| 4409 | not ((cleansed_line.find('case ') != -1 or |
| 4410 | cleansed_line.find('default:') != -1) and |
| 4411 | cleansed_line.find('break;') != -1)): |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 4412 | error(filename, linenum, 'whitespace/newline', 0, |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4413 | 'More than one command on the same line') |
| 4414 | |
| 4415 | # Some more style checks |
| 4416 | CheckBraces(filename, clean_lines, linenum, error) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4417 | CheckTrailingSemicolon(filename, clean_lines, linenum, error) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4418 | CheckEmptyBlockBody(filename, clean_lines, linenum, error) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 4419 | CheckSpacing(filename, clean_lines, linenum, nesting_state, error) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4420 | CheckOperatorSpacing(filename, clean_lines, linenum, error) |
| 4421 | CheckParenthesisSpacing(filename, clean_lines, linenum, error) |
| 4422 | CheckCommaSpacing(filename, clean_lines, linenum, error) |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4423 | CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4424 | CheckSpacingForFunctionCall(filename, clean_lines, linenum, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4425 | CheckCheck(filename, clean_lines, linenum, error) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 4426 | CheckAltTokens(filename, clean_lines, linenum, error) |
| 4427 | classinfo = nesting_state.InnermostClass() |
| 4428 | if classinfo: |
| 4429 | CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4430 | |
| 4431 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4432 | _RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$') |
| 4433 | # Matches the first component of a filename delimited by -s and _s. That is: |
| 4434 | # _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo' |
| 4435 | # _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo' |
| 4436 | # _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo' |
| 4437 | # _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo' |
| 4438 | _RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+') |
| 4439 | |
| 4440 | |
| 4441 | def _DropCommonSuffixes(filename): |
| 4442 | """Drops common suffixes like _test.cc or -inl.h from filename. |
| 4443 | |
| 4444 | For example: |
| 4445 | >>> _DropCommonSuffixes('foo/foo-inl.h') |
| 4446 | 'foo/foo' |
| 4447 | >>> _DropCommonSuffixes('foo/bar/foo.cc') |
| 4448 | 'foo/bar/foo' |
| 4449 | >>> _DropCommonSuffixes('foo/foo_internal.h') |
| 4450 | 'foo/foo' |
| 4451 | >>> _DropCommonSuffixes('foo/foo_unusualinternal.h') |
| 4452 | 'foo/foo_unusualinternal' |
| 4453 | |
| 4454 | Args: |
| 4455 | filename: The input filename. |
| 4456 | |
| 4457 | Returns: |
| 4458 | The filename with the common suffix removed. |
| 4459 | """ |
| 4460 | for suffix in ('test.cc', 'regtest.cc', 'unittest.cc', |
| 4461 | 'inl.h', 'impl.h', 'internal.h'): |
| 4462 | if (filename.endswith(suffix) and len(filename) > len(suffix) and |
| 4463 | filename[-len(suffix) - 1] in ('-', '_')): |
| 4464 | return filename[:-len(suffix) - 1] |
| 4465 | return os.path.splitext(filename)[0] |
| 4466 | |
| 4467 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4468 | def _ClassifyInclude(fileinfo, include, is_system): |
| 4469 | """Figures out what kind of header 'include' is. |
| 4470 | |
| 4471 | Args: |
| 4472 | fileinfo: The current file cpplint is running over. A FileInfo instance. |
| 4473 | include: The path to a #included file. |
| 4474 | is_system: True if the #include used <> rather than "". |
| 4475 | |
| 4476 | Returns: |
| 4477 | One of the _XXX_HEADER constants. |
| 4478 | |
| 4479 | For example: |
| 4480 | >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True) |
| 4481 | _C_SYS_HEADER |
| 4482 | >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True) |
| 4483 | _CPP_SYS_HEADER |
| 4484 | >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False) |
| 4485 | _LIKELY_MY_HEADER |
| 4486 | >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'), |
| 4487 | ... 'bar/foo_other_ext.h', False) |
| 4488 | _POSSIBLE_MY_HEADER |
| 4489 | >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False) |
| 4490 | _OTHER_HEADER |
| 4491 | """ |
| 4492 | # This is a list of all standard c++ header files, except |
| 4493 | # those already checked for above. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4494 | is_cpp_h = include in _CPP_HEADERS |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4495 | |
| 4496 | if is_system: |
| 4497 | if is_cpp_h: |
| 4498 | return _CPP_SYS_HEADER |
| 4499 | else: |
| 4500 | return _C_SYS_HEADER |
| 4501 | |
| 4502 | # If the target file and the include we're checking share a |
| 4503 | # basename when we drop common extensions, and the include |
| 4504 | # lives in . , then it's likely to be owned by the target file. |
| 4505 | target_dir, target_base = ( |
| 4506 | os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName()))) |
| 4507 | include_dir, include_base = os.path.split(_DropCommonSuffixes(include)) |
| 4508 | if target_base == include_base and ( |
| 4509 | include_dir == target_dir or |
| 4510 | include_dir == os.path.normpath(target_dir + '/../public')): |
| 4511 | return _LIKELY_MY_HEADER |
| 4512 | |
| 4513 | # If the target and include share some initial basename |
| 4514 | # component, it's possible the target is implementing the |
| 4515 | # include, so it's allowed to be first, but we'll never |
| 4516 | # complain if it's not there. |
| 4517 | target_first_component = _RE_FIRST_COMPONENT.match(target_base) |
| 4518 | include_first_component = _RE_FIRST_COMPONENT.match(include_base) |
| 4519 | if (target_first_component and include_first_component and |
| 4520 | target_first_component.group(0) == |
| 4521 | include_first_component.group(0)): |
| 4522 | return _POSSIBLE_MY_HEADER |
| 4523 | |
| 4524 | return _OTHER_HEADER |
| 4525 | |
| 4526 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4527 | |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 4528 | def CheckIncludeLine(filename, clean_lines, linenum, include_state, error): |
| 4529 | """Check rules that are applicable to #include lines. |
| 4530 | |
| 4531 | Strings on #include lines are NOT removed from elided line, to make |
| 4532 | certain tasks easier. However, to prevent false positives, checks |
| 4533 | applicable to #include lines in CheckLanguage must be put here. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4534 | |
| 4535 | Args: |
| 4536 | filename: The name of the current file. |
| 4537 | clean_lines: A CleansedLines instance containing the file. |
| 4538 | linenum: The number of the line to check. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4539 | include_state: An _IncludeState instance in which the headers are inserted. |
| 4540 | error: The function to call with any errors found. |
| 4541 | """ |
| 4542 | fileinfo = FileInfo(filename) |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 4543 | line = clean_lines.lines[linenum] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4544 | |
| 4545 | # "include" should use the new style "foo/bar.h" instead of just "bar.h" |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4546 | # Only do this check if the included header follows google naming |
| 4547 | # conventions. If not, assume that it's a 3rd party API that |
| 4548 | # requires special include conventions. |
| 4549 | # |
| 4550 | # We also make an exception for Lua headers, which follow google |
| 4551 | # naming convention but not the include convention. |
| 4552 | match = Match(r'#include\s*"([^/]+\.h)"', line) |
| 4553 | if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4554 | error(filename, linenum, 'build/include', 4, |
| 4555 | 'Include the directory when naming .h files') |
| 4556 | |
| 4557 | # we shouldn't include a file more than once. actually, there are a |
| 4558 | # handful of instances where doing so is okay, but in general it's |
| 4559 | # not. |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 4560 | match = _RE_PATTERN_INCLUDE.search(line) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4561 | if match: |
| 4562 | include = match.group(2) |
| 4563 | is_system = (match.group(1) == '<') |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4564 | duplicate_line = include_state.FindHeader(include) |
| 4565 | if duplicate_line >= 0: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4566 | error(filename, linenum, 'build/include', 4, |
| 4567 | '"%s" already included at %s:%s' % |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4568 | (include, filename, duplicate_line)) |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 4569 | elif (include.endswith('.cc') and |
| 4570 | os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)): |
| 4571 | error(filename, linenum, 'build/include', 4, |
| 4572 | 'Do not include .cc files from other packages') |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4573 | elif not _THIRD_PARTY_HEADERS_PATTERN.match(include): |
| 4574 | include_state.include_list[-1].append((include, linenum)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4575 | |
| 4576 | # We want to ensure that headers appear in the right order: |
| 4577 | # 1) for foo.cc, foo.h (preferred location) |
| 4578 | # 2) c system files |
| 4579 | # 3) cpp system files |
| 4580 | # 4) for foo.cc, foo.h (deprecated location) |
| 4581 | # 5) other google headers |
| 4582 | # |
| 4583 | # We classify each include statement as one of those 5 types |
| 4584 | # using a number of techniques. The include_state object keeps |
| 4585 | # track of the highest type seen, and complains if we see a |
| 4586 | # lower type after that. |
| 4587 | error_message = include_state.CheckNextIncludeOrder( |
| 4588 | _ClassifyInclude(fileinfo, include, is_system)) |
| 4589 | if error_message: |
| 4590 | error(filename, linenum, 'build/include_order', 4, |
| 4591 | '%s. Should be: %s.h, c system, c++ system, other.' % |
| 4592 | (error_message, fileinfo.BaseName())) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4593 | canonical_include = include_state.CanonicalizeAlphabeticalOrder(include) |
| 4594 | if not include_state.IsInAlphabeticalOrder( |
| 4595 | clean_lines, linenum, canonical_include): |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 4596 | error(filename, linenum, 'build/include_alpha', 4, |
| 4597 | 'Include "%s" not in alphabetical order' % include) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4598 | include_state.SetLastHeader(canonical_include) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4599 | |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 4600 | |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4601 | |
| 4602 | def _GetTextInside(text, start_pattern): |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4603 | r"""Retrieves all the text between matching open and close parentheses. |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4604 | |
| 4605 | Given a string of lines and a regular expression string, retrieve all the text |
| 4606 | following the expression and between opening punctuation symbols like |
| 4607 | (, [, or {, and the matching close-punctuation symbol. This properly nested |
| 4608 | occurrences of the punctuations, so for the text like |
| 4609 | printf(a(), b(c())); |
| 4610 | a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'. |
| 4611 | start_pattern must match string having an open punctuation symbol at the end. |
| 4612 | |
| 4613 | Args: |
| 4614 | text: The lines to extract text. Its comments and strings must be elided. |
| 4615 | It can be single line and can span multiple lines. |
| 4616 | start_pattern: The regexp string indicating where to start extracting |
| 4617 | the text. |
| 4618 | Returns: |
| 4619 | The extracted text. |
| 4620 | None if either the opening string or ending punctuation could not be found. |
| 4621 | """ |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4622 | # TODO(unknown): Audit cpplint.py to see what places could be profitably |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4623 | # rewritten to use _GetTextInside (and use inferior regexp matching today). |
| 4624 | |
| 4625 | # Give opening punctuations to get the matching close-punctuations. |
| 4626 | matching_punctuation = {'(': ')', '{': '}', '[': ']'} |
| 4627 | closing_punctuation = set(matching_punctuation.itervalues()) |
| 4628 | |
| 4629 | # Find the position to start extracting text. |
| 4630 | match = re.search(start_pattern, text, re.M) |
| 4631 | if not match: # start_pattern not found in text. |
| 4632 | return None |
| 4633 | start_position = match.end(0) |
| 4634 | |
| 4635 | assert start_position > 0, ( |
| 4636 | 'start_pattern must ends with an opening punctuation.') |
| 4637 | assert text[start_position - 1] in matching_punctuation, ( |
| 4638 | 'start_pattern must ends with an opening punctuation.') |
| 4639 | # Stack of closing punctuations we expect to have in text after position. |
| 4640 | punctuation_stack = [matching_punctuation[text[start_position - 1]]] |
| 4641 | position = start_position |
| 4642 | while punctuation_stack and position < len(text): |
| 4643 | if text[position] == punctuation_stack[-1]: |
| 4644 | punctuation_stack.pop() |
| 4645 | elif text[position] in closing_punctuation: |
| 4646 | # A closing punctuation without matching opening punctuations. |
| 4647 | return None |
| 4648 | elif text[position] in matching_punctuation: |
| 4649 | punctuation_stack.append(matching_punctuation[text[position]]) |
| 4650 | position += 1 |
| 4651 | if punctuation_stack: |
| 4652 | # Opening punctuations left without matching close-punctuations. |
| 4653 | return None |
| 4654 | # punctuations match. |
| 4655 | return text[start_position:position - 1] |
| 4656 | |
| 4657 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4658 | # Patterns for matching call-by-reference parameters. |
| 4659 | # |
| 4660 | # Supports nested templates up to 2 levels deep using this messy pattern: |
| 4661 | # < (?: < (?: < [^<>]* |
| 4662 | # > |
| 4663 | # | [^<>] )* |
| 4664 | # > |
| 4665 | # | [^<>] )* |
| 4666 | # > |
| 4667 | _RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]* |
| 4668 | _RE_PATTERN_TYPE = ( |
| 4669 | r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?' |
| 4670 | r'(?:\w|' |
| 4671 | r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|' |
| 4672 | r'::)+') |
| 4673 | # A call-by-reference parameter ends with '& identifier'. |
| 4674 | _RE_PATTERN_REF_PARAM = re.compile( |
| 4675 | r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*' |
| 4676 | r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]') |
| 4677 | # A call-by-const-reference parameter either ends with 'const& identifier' |
| 4678 | # or looks like 'const type& identifier' when 'type' is atomic. |
| 4679 | _RE_PATTERN_CONST_REF_PARAM = ( |
| 4680 | r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT + |
| 4681 | r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')') |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4682 | # Stream types. |
| 4683 | _RE_PATTERN_REF_STREAM_PARAM = ( |
| 4684 | r'(?:.*stream\s*&\s*' + _RE_PATTERN_IDENT + r')') |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4685 | |
| 4686 | |
| 4687 | def CheckLanguage(filename, clean_lines, linenum, file_extension, |
| 4688 | include_state, nesting_state, error): |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 4689 | """Checks rules from the 'C++ language rules' section of cppguide.html. |
| 4690 | |
| 4691 | Some of these rules are hard to test (function overloading, using |
| 4692 | uint32 inappropriately), but we do the best we can. |
| 4693 | |
| 4694 | Args: |
| 4695 | filename: The name of the current file. |
| 4696 | clean_lines: A CleansedLines instance containing the file. |
| 4697 | linenum: The number of the line to check. |
| 4698 | file_extension: The extension (without the dot) of the filename. |
| 4699 | include_state: An _IncludeState instance in which the headers are inserted. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4700 | nesting_state: A NestingState instance which maintains information about |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4701 | the current stack of nested blocks being parsed. |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 4702 | error: The function to call with any errors found. |
| 4703 | """ |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4704 | # If the line is empty or consists of entirely a comment, no need to |
| 4705 | # check it. |
| 4706 | line = clean_lines.elided[linenum] |
| 4707 | if not line: |
| 4708 | return |
| 4709 | |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 4710 | match = _RE_PATTERN_INCLUDE.search(line) |
| 4711 | if match: |
| 4712 | CheckIncludeLine(filename, clean_lines, linenum, include_state, error) |
| 4713 | return |
| 4714 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 4715 | # Reset include state across preprocessor directives. This is meant |
| 4716 | # to silence warnings for conditional includes. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4717 | match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line) |
| 4718 | if match: |
| 4719 | include_state.ResetSection(match.group(1)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4720 | |
| 4721 | # Make Windows paths like Unix. |
| 4722 | fullname = os.path.abspath(filename).replace('\\', '/') |
skym@chromium.org | 3990c41 | 2016-02-05 20:55:12 +0000 | [diff] [blame] | 4723 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4724 | # Perform other checks now that we are sure that this is not an include line |
| 4725 | CheckCasts(filename, clean_lines, linenum, error) |
| 4726 | CheckGlobalStatic(filename, clean_lines, linenum, error) |
| 4727 | CheckPrintf(filename, clean_lines, linenum, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4728 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 4729 | if IsHeaderExtension(file_extension): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4730 | # TODO(unknown): check that 1-arg constructors are explicit. |
| 4731 | # How to tell it's a constructor? |
| 4732 | # (handled in CheckForNonStandardConstructs for now) |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4733 | # TODO(unknown): check that classes declare or disable copy/assign |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4734 | # (level 1 error) |
| 4735 | pass |
| 4736 | |
| 4737 | # Check if people are using the verboten C basic types. The only exception |
| 4738 | # we regularly allow is "unsigned short port" for port. |
| 4739 | if Search(r'\bshort port\b', line): |
| 4740 | if not Search(r'\bunsigned short port\b', line): |
| 4741 | error(filename, linenum, 'runtime/int', 4, |
| 4742 | 'Use "unsigned short" for ports, not "short"') |
| 4743 | else: |
| 4744 | match = Search(r'\b(short|long(?! +double)|long long)\b', line) |
| 4745 | if match: |
| 4746 | error(filename, linenum, 'runtime/int', 4, |
| 4747 | 'Use int16/int64/etc, rather than the C type %s' % match.group(1)) |
| 4748 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 4749 | # Check if some verboten operator overloading is going on |
| 4750 | # TODO(unknown): catch out-of-line unary operator&: |
| 4751 | # class X {}; |
| 4752 | # int operator&(const X& x) { return 42; } // unary operator& |
| 4753 | # The trick is it's hard to tell apart from binary operator&: |
| 4754 | # class Y { int operator&(const Y& x) { return 23; } }; // binary operator& |
| 4755 | if Search(r'\boperator\s*&\s*\(\s*\)', line): |
| 4756 | error(filename, linenum, 'runtime/operator', 4, |
| 4757 | 'Unary operator& is dangerous. Do not use it.') |
| 4758 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4759 | # Check for suspicious usage of "if" like |
| 4760 | # } if (a == b) { |
| 4761 | if Search(r'\}\s*if\s*\(', line): |
| 4762 | error(filename, linenum, 'readability/braces', 4, |
| 4763 | 'Did you mean "else if"? If not, start a new line for "if".') |
| 4764 | |
| 4765 | # Check for potential format string bugs like printf(foo). |
| 4766 | # We constrain the pattern not to pick things like DocidForPrintf(foo). |
| 4767 | # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str()) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4768 | # TODO(unknown): Catch the following case. Need to change the calling |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4769 | # convention of the whole function to process multiple line to handle it. |
| 4770 | # printf( |
| 4771 | # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line); |
| 4772 | printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(') |
| 4773 | if printf_args: |
| 4774 | match = Match(r'([\w.\->()]+)$', printf_args) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 4775 | if match and match.group(1) != '__VA_ARGS__': |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4776 | function_name = re.search(r'\b((?:string)?printf)\s*\(', |
| 4777 | line, re.I).group(1) |
| 4778 | error(filename, linenum, 'runtime/printf', 4, |
| 4779 | 'Potential format string bug. Do %s("%%s", %s) instead.' |
| 4780 | % (function_name, match.group(1))) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4781 | |
| 4782 | # Check for potential memset bugs like memset(buf, sizeof(buf), 0). |
| 4783 | match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line) |
| 4784 | if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)): |
| 4785 | error(filename, linenum, 'runtime/memset', 4, |
| 4786 | 'Did you mean "memset(%s, 0, %s)"?' |
| 4787 | % (match.group(1), match.group(2))) |
| 4788 | |
| 4789 | if Search(r'\busing namespace\b', line): |
| 4790 | error(filename, linenum, 'build/namespaces', 5, |
| 4791 | 'Do not use namespace using-directives. ' |
| 4792 | 'Use using-declarations instead.') |
| 4793 | |
| 4794 | # Detect variable-length arrays. |
| 4795 | match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line) |
| 4796 | if (match and match.group(2) != 'return' and match.group(2) != 'delete' and |
| 4797 | match.group(3).find(']') == -1): |
| 4798 | # Split the size using space and arithmetic operators as delimiters. |
| 4799 | # If any of the resulting tokens are not compile time constants then |
| 4800 | # report the error. |
| 4801 | tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3)) |
| 4802 | is_const = True |
| 4803 | skip_next = False |
| 4804 | for tok in tokens: |
| 4805 | if skip_next: |
| 4806 | skip_next = False |
| 4807 | continue |
| 4808 | |
| 4809 | if Search(r'sizeof\(.+\)', tok): continue |
| 4810 | if Search(r'arraysize\(\w+\)', tok): continue |
| 4811 | |
| 4812 | tok = tok.lstrip('(') |
| 4813 | tok = tok.rstrip(')') |
| 4814 | if not tok: continue |
| 4815 | if Match(r'\d+', tok): continue |
| 4816 | if Match(r'0[xX][0-9a-fA-F]+', tok): continue |
| 4817 | if Match(r'k[A-Z0-9]\w*', tok): continue |
| 4818 | if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue |
| 4819 | if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue |
| 4820 | # A catch all for tricky sizeof cases, including 'sizeof expression', |
| 4821 | # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)' |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 4822 | # requires skipping the next token because we split on ' ' and '*'. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4823 | if tok.startswith('sizeof'): |
| 4824 | skip_next = True |
| 4825 | continue |
| 4826 | is_const = False |
| 4827 | break |
| 4828 | if not is_const: |
| 4829 | error(filename, linenum, 'runtime/arrays', 1, |
| 4830 | 'Do not use variable-length arrays. Use an appropriately named ' |
| 4831 | "('k' followed by CamelCase) compile-time constant for the size.") |
| 4832 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4833 | # Check for use of unnamed namespaces in header files. Registration |
| 4834 | # macros are typically OK, so we allow use of "namespace {" on lines |
| 4835 | # that end with backslashes. |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 4836 | if (IsHeaderExtension(file_extension) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4837 | and Search(r'\bnamespace\s*{', line) |
| 4838 | and line[-1] != '\\'): |
| 4839 | error(filename, linenum, 'build/namespaces', 4, |
| 4840 | 'Do not use unnamed namespaces in header files. See ' |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 4841 | 'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 4842 | ' for more information.') |
| 4843 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4844 | |
| 4845 | def CheckGlobalStatic(filename, clean_lines, linenum, error): |
| 4846 | """Check for unsafe global or static objects. |
| 4847 | |
| 4848 | Args: |
| 4849 | filename: The name of the current file. |
| 4850 | clean_lines: A CleansedLines instance containing the file. |
| 4851 | linenum: The number of the line to check. |
| 4852 | error: The function to call with any errors found. |
| 4853 | """ |
| 4854 | line = clean_lines.elided[linenum] |
| 4855 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4856 | # Match two lines at a time to support multiline declarations |
| 4857 | if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line): |
| 4858 | line += clean_lines.elided[linenum + 1].strip() |
| 4859 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4860 | # Check for people declaring static/global STL strings at the top level. |
| 4861 | # This is dangerous because the C++ language does not guarantee that |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4862 | # globals with constructors are initialized before the first access, and |
| 4863 | # also because globals can be destroyed when some threads are still running. |
| 4864 | # TODO(unknown): Generalize this to also find static unique_ptr instances. |
| 4865 | # TODO(unknown): File bugs for clang-tidy to find these. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4866 | match = Match( |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4867 | r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +' |
| 4868 | r'([a-zA-Z0-9_:]+)\b(.*)', |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4869 | line) |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4870 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4871 | # Remove false positives: |
| 4872 | # - String pointers (as opposed to values). |
| 4873 | # string *pointer |
| 4874 | # const string *pointer |
| 4875 | # string const *pointer |
| 4876 | # string *const pointer |
| 4877 | # |
| 4878 | # - Functions and template specializations. |
| 4879 | # string Function<Type>(... |
| 4880 | # string Class<Type>::Method(... |
| 4881 | # |
| 4882 | # - Operators. These are matched separately because operator names |
| 4883 | # cross non-word boundaries, and trying to match both operators |
| 4884 | # and functions at the same time would decrease accuracy of |
| 4885 | # matching identifiers. |
| 4886 | # string Class::operator*() |
| 4887 | if (match and |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4888 | not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4889 | not Search(r'\boperator\W', line) and |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4890 | not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))): |
| 4891 | if Search(r'\bconst\b', line): |
| 4892 | error(filename, linenum, 'runtime/string', 4, |
| 4893 | 'For a static/global string constant, use a C style string ' |
| 4894 | 'instead: "%schar%s %s[]".' % |
| 4895 | (match.group(1), match.group(2) or '', match.group(3))) |
| 4896 | else: |
| 4897 | error(filename, linenum, 'runtime/string', 4, |
| 4898 | 'Static/global string variables are not permitted.') |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4899 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 4900 | if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or |
| 4901 | Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4902 | error(filename, linenum, 'runtime/init', 4, |
| 4903 | 'You seem to be initializing a member variable with itself.') |
| 4904 | |
| 4905 | |
| 4906 | def CheckPrintf(filename, clean_lines, linenum, error): |
| 4907 | """Check for printf related issues. |
| 4908 | |
| 4909 | Args: |
| 4910 | filename: The name of the current file. |
| 4911 | clean_lines: A CleansedLines instance containing the file. |
| 4912 | linenum: The number of the line to check. |
| 4913 | error: The function to call with any errors found. |
| 4914 | """ |
| 4915 | line = clean_lines.elided[linenum] |
| 4916 | |
| 4917 | # When snprintf is used, the second argument shouldn't be a literal. |
| 4918 | match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line) |
| 4919 | if match and match.group(2) != '0': |
| 4920 | # If 2nd arg is zero, snprintf is used to calculate size. |
| 4921 | error(filename, linenum, 'runtime/printf', 3, |
| 4922 | 'If you can, use sizeof(%s) instead of %s as the 2nd arg ' |
| 4923 | 'to snprintf.' % (match.group(1), match.group(2))) |
| 4924 | |
| 4925 | # Check if some verboten C functions are being used. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4926 | if Search(r'\bsprintf\s*\(', line): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4927 | error(filename, linenum, 'runtime/printf', 5, |
| 4928 | 'Never use sprintf. Use snprintf instead.') |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4929 | match = Search(r'\b(strcpy|strcat)\s*\(', line) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4930 | if match: |
| 4931 | error(filename, linenum, 'runtime/printf', 4, |
| 4932 | 'Almost always, snprintf is better than %s' % match.group(1)) |
| 4933 | |
| 4934 | |
| 4935 | def IsDerivedFunction(clean_lines, linenum): |
| 4936 | """Check if current line contains an inherited function. |
| 4937 | |
| 4938 | Args: |
| 4939 | clean_lines: A CleansedLines instance containing the file. |
| 4940 | linenum: The number of the line to check. |
| 4941 | Returns: |
| 4942 | True if current line contains a function with "override" |
| 4943 | virt-specifier. |
| 4944 | """ |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 4945 | # Scan back a few lines for start of current function |
| 4946 | for i in xrange(linenum, max(-1, linenum - 10), -1): |
| 4947 | match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i]) |
| 4948 | if match: |
| 4949 | # Look for "override" after the matching closing parenthesis |
| 4950 | line, _, closing_paren = CloseExpression( |
| 4951 | clean_lines, i, len(match.group(1))) |
| 4952 | return (closing_paren >= 0 and |
| 4953 | Search(r'\boverride\b', line[closing_paren:])) |
| 4954 | return False |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4955 | |
| 4956 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 4957 | def IsOutOfLineMethodDefinition(clean_lines, linenum): |
| 4958 | """Check if current line contains an out-of-line method definition. |
| 4959 | |
| 4960 | Args: |
| 4961 | clean_lines: A CleansedLines instance containing the file. |
| 4962 | linenum: The number of the line to check. |
| 4963 | Returns: |
| 4964 | True if current line contains an out-of-line method definition. |
| 4965 | """ |
| 4966 | # Scan back a few lines for start of current function |
| 4967 | for i in xrange(linenum, max(-1, linenum - 10), -1): |
| 4968 | if Match(r'^([^()]*\w+)\(', clean_lines.elided[i]): |
| 4969 | return Match(r'^[^()]*\w+::\w+\(', clean_lines.elided[i]) is not None |
| 4970 | return False |
| 4971 | |
| 4972 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 4973 | def IsInitializerList(clean_lines, linenum): |
| 4974 | """Check if current line is inside constructor initializer list. |
| 4975 | |
| 4976 | Args: |
| 4977 | clean_lines: A CleansedLines instance containing the file. |
| 4978 | linenum: The number of the line to check. |
| 4979 | Returns: |
| 4980 | True if current line appears to be inside constructor initializer |
| 4981 | list, False otherwise. |
| 4982 | """ |
| 4983 | for i in xrange(linenum, 1, -1): |
| 4984 | line = clean_lines.elided[i] |
| 4985 | if i == linenum: |
| 4986 | remove_function_body = Match(r'^(.*)\{\s*$', line) |
| 4987 | if remove_function_body: |
| 4988 | line = remove_function_body.group(1) |
| 4989 | |
| 4990 | if Search(r'\s:\s*\w+[({]', line): |
| 4991 | # A lone colon tend to indicate the start of a constructor |
| 4992 | # initializer list. It could also be a ternary operator, which |
| 4993 | # also tend to appear in constructor initializer lists as |
| 4994 | # opposed to parameter lists. |
| 4995 | return True |
| 4996 | if Search(r'\}\s*,\s*$', line): |
| 4997 | # A closing brace followed by a comma is probably the end of a |
| 4998 | # brace-initialized member in constructor initializer list. |
| 4999 | return True |
| 5000 | if Search(r'[{};]\s*$', line): |
| 5001 | # Found one of the following: |
| 5002 | # - A closing brace or semicolon, probably the end of the previous |
| 5003 | # function. |
| 5004 | # - An opening brace, probably the start of current class or namespace. |
| 5005 | # |
| 5006 | # Current line is probably not inside an initializer list since |
| 5007 | # we saw one of those things without seeing the starting colon. |
| 5008 | return False |
| 5009 | |
| 5010 | # Got to the beginning of the file without seeing the start of |
| 5011 | # constructor initializer list. |
| 5012 | return False |
| 5013 | |
| 5014 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5015 | def CheckForNonConstReference(filename, clean_lines, linenum, |
| 5016 | nesting_state, error): |
| 5017 | """Check for non-const references. |
| 5018 | |
| 5019 | Separate from CheckLanguage since it scans backwards from current |
| 5020 | line, instead of scanning forward. |
| 5021 | |
| 5022 | Args: |
| 5023 | filename: The name of the current file. |
| 5024 | clean_lines: A CleansedLines instance containing the file. |
| 5025 | linenum: The number of the line to check. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5026 | nesting_state: A NestingState instance which maintains information about |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5027 | the current stack of nested blocks being parsed. |
| 5028 | error: The function to call with any errors found. |
| 5029 | """ |
| 5030 | # Do nothing if there is no '&' on current line. |
| 5031 | line = clean_lines.elided[linenum] |
| 5032 | if '&' not in line: |
| 5033 | return |
| 5034 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5035 | # If a function is inherited, current function doesn't have much of |
| 5036 | # a choice, so any non-const references should not be blamed on |
| 5037 | # derived function. |
| 5038 | if IsDerivedFunction(clean_lines, linenum): |
| 5039 | return |
| 5040 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5041 | # Don't warn on out-of-line method definitions, as we would warn on the |
| 5042 | # in-line declaration, if it isn't marked with 'override'. |
| 5043 | if IsOutOfLineMethodDefinition(clean_lines, linenum): |
| 5044 | return |
| 5045 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5046 | # Long type names may be broken across multiple lines, usually in one |
| 5047 | # of these forms: |
| 5048 | # LongType |
| 5049 | # ::LongTypeContinued &identifier |
| 5050 | # LongType:: |
| 5051 | # LongTypeContinued &identifier |
| 5052 | # LongType< |
| 5053 | # ...>::LongTypeContinued &identifier |
| 5054 | # |
| 5055 | # If we detected a type split across two lines, join the previous |
| 5056 | # line to current line so that we can match const references |
| 5057 | # accordingly. |
| 5058 | # |
| 5059 | # Note that this only scans back one line, since scanning back |
| 5060 | # arbitrary number of lines would be expensive. If you have a type |
| 5061 | # that spans more than 2 lines, please use a typedef. |
| 5062 | if linenum > 1: |
| 5063 | previous = None |
| 5064 | if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line): |
| 5065 | # previous_line\n + ::current_line |
| 5066 | previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$', |
| 5067 | clean_lines.elided[linenum - 1]) |
| 5068 | elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line): |
| 5069 | # previous_line::\n + current_line |
| 5070 | previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$', |
| 5071 | clean_lines.elided[linenum - 1]) |
| 5072 | if previous: |
| 5073 | line = previous.group(1) + line.lstrip() |
| 5074 | else: |
| 5075 | # Check for templated parameter that is split across multiple lines |
| 5076 | endpos = line.rfind('>') |
| 5077 | if endpos > -1: |
| 5078 | (_, startline, startpos) = ReverseCloseExpression( |
| 5079 | clean_lines, linenum, endpos) |
| 5080 | if startpos > -1 and startline < linenum: |
| 5081 | # Found the matching < on an earlier line, collect all |
| 5082 | # pieces up to current line. |
| 5083 | line = '' |
| 5084 | for i in xrange(startline, linenum + 1): |
| 5085 | line += clean_lines.elided[i].strip() |
| 5086 | |
| 5087 | # Check for non-const references in function parameters. A single '&' may |
| 5088 | # found in the following places: |
| 5089 | # inside expression: binary & for bitwise AND |
| 5090 | # inside expression: unary & for taking the address of something |
| 5091 | # inside declarators: reference parameter |
| 5092 | # We will exclude the first two cases by checking that we are not inside a |
| 5093 | # function body, including one that was just introduced by a trailing '{'. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5094 | # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare]. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5095 | if (nesting_state.previous_stack_top and |
| 5096 | not (isinstance(nesting_state.previous_stack_top, _ClassInfo) or |
| 5097 | isinstance(nesting_state.previous_stack_top, _NamespaceInfo))): |
| 5098 | # Not at toplevel, not within a class, and not within a namespace |
| 5099 | return |
| 5100 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5101 | # Avoid initializer lists. We only need to scan back from the |
| 5102 | # current line for something that starts with ':'. |
| 5103 | # |
| 5104 | # We don't need to check the current line, since the '&' would |
| 5105 | # appear inside the second set of parentheses on the current line as |
| 5106 | # opposed to the first set. |
| 5107 | if linenum > 0: |
| 5108 | for i in xrange(linenum - 1, max(0, linenum - 10), -1): |
| 5109 | previous_line = clean_lines.elided[i] |
| 5110 | if not Search(r'[),]\s*$', previous_line): |
| 5111 | break |
| 5112 | if Match(r'^\s*:\s+\S', previous_line): |
| 5113 | return |
| 5114 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5115 | # Avoid preprocessors |
| 5116 | if Search(r'\\\s*$', line): |
| 5117 | return |
| 5118 | |
| 5119 | # Avoid constructor initializer lists |
| 5120 | if IsInitializerList(clean_lines, linenum): |
| 5121 | return |
| 5122 | |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5123 | # We allow non-const references in a few standard places, like functions |
| 5124 | # called "swap()" or iostream operators like "<<" or ">>". Do not check |
| 5125 | # those function parameters. |
| 5126 | # |
| 5127 | # We also accept & in static_assert, which looks like a function but |
| 5128 | # it's actually a declaration expression. |
| 5129 | whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|' |
| 5130 | r'operator\s*[<>][<>]|' |
| 5131 | r'static_assert|COMPILE_ASSERT' |
| 5132 | r')\s*\(') |
| 5133 | if Search(whitelisted_functions, line): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5134 | return |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5135 | elif not Search(r'\S+\([^)]*$', line): |
| 5136 | # Don't see a whitelisted function on this line. Actually we |
| 5137 | # didn't see any function name on this line, so this is likely a |
| 5138 | # multi-line parameter list. Try a bit harder to catch this case. |
| 5139 | for i in xrange(2): |
| 5140 | if (linenum > i and |
| 5141 | Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5142 | return |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5143 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5144 | decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body |
| 5145 | for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls): |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 5146 | if (not Match(_RE_PATTERN_CONST_REF_PARAM, parameter) and |
| 5147 | not Match(_RE_PATTERN_REF_STREAM_PARAM, parameter)): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5148 | error(filename, linenum, 'runtime/references', 2, |
| 5149 | 'Is this a non-const reference? ' |
| 5150 | 'If so, make const or use a pointer: ' + |
| 5151 | ReplaceAll(' *<', '<', parameter)) |
| 5152 | |
| 5153 | |
| 5154 | def CheckCasts(filename, clean_lines, linenum, error): |
| 5155 | """Various cast related checks. |
| 5156 | |
| 5157 | Args: |
| 5158 | filename: The name of the current file. |
| 5159 | clean_lines: A CleansedLines instance containing the file. |
| 5160 | linenum: The number of the line to check. |
| 5161 | error: The function to call with any errors found. |
| 5162 | """ |
| 5163 | line = clean_lines.elided[linenum] |
| 5164 | |
| 5165 | # Check to see if they're using an conversion function cast. |
| 5166 | # I just try to capture the most common basic types, though there are more. |
| 5167 | # Parameterless conversion functions, such as bool(), are allowed as they are |
| 5168 | # probably a member operator declaration or default constructor. |
| 5169 | match = Search( |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 5170 | r'(\bnew\s+(?:const\s+)?|\S<\s*(?:const\s+)?)?\b' |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5171 | r'(int|float|double|bool|char|int32|uint32|int64|uint64)' |
| 5172 | r'(\([^)].*)', line) |
| 5173 | expecting_function = ExpectingFunctionArgs(clean_lines, linenum) |
| 5174 | if match and not expecting_function: |
| 5175 | matched_type = match.group(2) |
| 5176 | |
| 5177 | # matched_new_or_template is used to silence two false positives: |
| 5178 | # - New operators |
| 5179 | # - Template arguments with function types |
| 5180 | # |
| 5181 | # For template arguments, we match on types immediately following |
| 5182 | # an opening bracket without any spaces. This is a fast way to |
| 5183 | # silence the common case where the function type is the first |
| 5184 | # template argument. False negative with less-than comparison is |
| 5185 | # avoided because those operators are usually followed by a space. |
| 5186 | # |
| 5187 | # function<double(double)> // bracket + no space = false positive |
| 5188 | # value < double(42) // bracket + space = true positive |
| 5189 | matched_new_or_template = match.group(1) |
| 5190 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5191 | # Avoid arrays by looking for brackets that come after the closing |
| 5192 | # parenthesis. |
| 5193 | if Match(r'\([^()]+\)\s*\[', match.group(3)): |
| 5194 | return |
| 5195 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5196 | # Other things to ignore: |
| 5197 | # - Function pointers |
| 5198 | # - Casts to pointer types |
| 5199 | # - Placement new |
| 5200 | # - Alias declarations |
| 5201 | matched_funcptr = match.group(3) |
| 5202 | if (matched_new_or_template is None and |
| 5203 | not (matched_funcptr and |
| 5204 | (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(', |
| 5205 | matched_funcptr) or |
| 5206 | matched_funcptr.startswith('(*)'))) and |
| 5207 | not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and |
| 5208 | not Search(r'new\(\S+\)\s*' + matched_type, line)): |
| 5209 | error(filename, linenum, 'readability/casting', 4, |
| 5210 | 'Using deprecated casting style. ' |
| 5211 | 'Use static_cast<%s>(...) instead' % |
| 5212 | matched_type) |
| 5213 | |
| 5214 | if not expecting_function: |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5215 | CheckCStyleCast(filename, clean_lines, linenum, 'static_cast', |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5216 | r'\((int|float|double|bool|char|u?int(16|32|64))\)', error) |
| 5217 | |
| 5218 | # This doesn't catch all cases. Consider (const char * const)"hello". |
| 5219 | # |
| 5220 | # (char *) "foo" should always be a const_cast (reinterpret_cast won't |
| 5221 | # compile). |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5222 | if CheckCStyleCast(filename, clean_lines, linenum, 'const_cast', |
| 5223 | r'\((char\s?\*+\s?)\)\s*"', error): |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5224 | pass |
| 5225 | else: |
| 5226 | # Check pointer casts for other than string constants |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5227 | CheckCStyleCast(filename, clean_lines, linenum, 'reinterpret_cast', |
| 5228 | r'\((\w+\s?\*+\s?)\)', error) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5229 | |
| 5230 | # In addition, we look for people taking the address of a cast. This |
| 5231 | # is dangerous -- casts can assign to temporaries, so the pointer doesn't |
| 5232 | # point where you think. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5233 | # |
| 5234 | # Some non-identifier character is required before the '&' for the |
| 5235 | # expression to be recognized as a cast. These are casts: |
| 5236 | # expression = &static_cast<int*>(temporary()); |
| 5237 | # function(&(int*)(temporary())); |
| 5238 | # |
| 5239 | # This is not a cast: |
| 5240 | # reference_type&(int* function_param); |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5241 | match = Search( |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5242 | r'(?:[^\w]&\(([^)*][^)]*)\)[\w(])|' |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5243 | r'(?:[^\w]&(static|dynamic|down|reinterpret)_cast\b)', line) |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5244 | if match: |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5245 | # Try a better error message when the & is bound to something |
| 5246 | # dereferenced by the casted pointer, as opposed to the casted |
| 5247 | # pointer itself. |
| 5248 | parenthesis_error = False |
| 5249 | match = Match(r'^(.*&(?:static|dynamic|down|reinterpret)_cast\b)<', line) |
| 5250 | if match: |
| 5251 | _, y1, x1 = CloseExpression(clean_lines, linenum, len(match.group(1))) |
| 5252 | if x1 >= 0 and clean_lines.elided[y1][x1] == '(': |
| 5253 | _, y2, x2 = CloseExpression(clean_lines, y1, x1) |
| 5254 | if x2 >= 0: |
| 5255 | extended_line = clean_lines.elided[y2][x2:] |
| 5256 | if y2 < clean_lines.NumLines() - 1: |
| 5257 | extended_line += clean_lines.elided[y2 + 1] |
| 5258 | if Match(r'\s*(?:->|\[)', extended_line): |
| 5259 | parenthesis_error = True |
| 5260 | |
| 5261 | if parenthesis_error: |
| 5262 | error(filename, linenum, 'readability/casting', 4, |
| 5263 | ('Are you taking an address of something dereferenced ' |
| 5264 | 'from a cast? Wrapping the dereferenced expression in ' |
| 5265 | 'parentheses will make the binding more obvious')) |
| 5266 | else: |
| 5267 | error(filename, linenum, 'runtime/casting', 4, |
| 5268 | ('Are you taking an address of a cast? ' |
| 5269 | 'This is dangerous: could be a temp var. ' |
| 5270 | 'Take the address before doing the cast, rather than after')) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5271 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5272 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5273 | def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5274 | """Checks for a C-style cast by looking for the pattern. |
| 5275 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5276 | Args: |
| 5277 | filename: The name of the current file. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5278 | clean_lines: A CleansedLines instance containing the file. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5279 | linenum: The number of the line to check. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5280 | cast_type: The string for the C++ cast to recommend. This is either |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5281 | reinterpret_cast, static_cast, or const_cast, depending. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5282 | pattern: The regular expression used to find C-style casts. |
| 5283 | error: The function to call with any errors found. |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5284 | |
| 5285 | Returns: |
| 5286 | True if an error was emitted. |
| 5287 | False otherwise. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5288 | """ |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5289 | line = clean_lines.elided[linenum] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5290 | match = Search(pattern, line) |
| 5291 | if not match: |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5292 | return False |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5293 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5294 | # Exclude lines with keywords that tend to look like casts |
| 5295 | context = line[0:match.start(1) - 1] |
| 5296 | if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context): |
| 5297 | return False |
| 5298 | |
| 5299 | # Try expanding current context to see if we one level of |
| 5300 | # parentheses inside a macro. |
| 5301 | if linenum > 0: |
| 5302 | for i in xrange(linenum - 1, max(0, linenum - 5), -1): |
| 5303 | context = clean_lines.elided[i] + context |
| 5304 | if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context): |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5305 | return False |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5306 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 5307 | # operator++(int) and operator--(int) |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5308 | if context.endswith(' operator++') or context.endswith(' operator--'): |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 5309 | return False |
| 5310 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 5311 | # A single unnamed argument for a function tends to look like old style cast. |
| 5312 | # If we see those, don't issue warnings for deprecated casts. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5313 | remainder = line[match.end(0):] |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5314 | if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)', |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5315 | remainder): |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 5316 | return False |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5317 | |
| 5318 | # At this point, all that should be left is actual casts. |
| 5319 | error(filename, linenum, 'readability/casting', 4, |
| 5320 | 'Using C-style cast. Use %s<%s>(...) instead' % |
| 5321 | (cast_type, match.group(1))) |
| 5322 | |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5323 | return True |
| 5324 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5325 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5326 | def ExpectingFunctionArgs(clean_lines, linenum): |
| 5327 | """Checks whether where function type arguments are expected. |
| 5328 | |
| 5329 | Args: |
| 5330 | clean_lines: A CleansedLines instance containing the file. |
| 5331 | linenum: The number of the line to check. |
| 5332 | |
| 5333 | Returns: |
| 5334 | True if the line at 'linenum' is inside something that expects arguments |
| 5335 | of function types. |
| 5336 | """ |
| 5337 | line = clean_lines.elided[linenum] |
| 5338 | return (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or |
| 5339 | (linenum >= 2 and |
| 5340 | (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$', |
| 5341 | clean_lines.elided[linenum - 1]) or |
| 5342 | Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$', |
| 5343 | clean_lines.elided[linenum - 2]) or |
| 5344 | Search(r'\bstd::m?function\s*\<\s*$', |
| 5345 | clean_lines.elided[linenum - 1])))) |
| 5346 | |
| 5347 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5348 | _HEADERS_CONTAINING_TEMPLATES = ( |
| 5349 | ('<deque>', ('deque',)), |
| 5350 | ('<functional>', ('unary_function', 'binary_function', |
| 5351 | 'plus', 'minus', 'multiplies', 'divides', 'modulus', |
| 5352 | 'negate', |
| 5353 | 'equal_to', 'not_equal_to', 'greater', 'less', |
| 5354 | 'greater_equal', 'less_equal', |
| 5355 | 'logical_and', 'logical_or', 'logical_not', |
| 5356 | 'unary_negate', 'not1', 'binary_negate', 'not2', |
| 5357 | 'bind1st', 'bind2nd', |
| 5358 | 'pointer_to_unary_function', |
| 5359 | 'pointer_to_binary_function', |
| 5360 | 'ptr_fun', |
| 5361 | 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t', |
| 5362 | 'mem_fun_ref_t', |
| 5363 | 'const_mem_fun_t', 'const_mem_fun1_t', |
| 5364 | 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t', |
| 5365 | 'mem_fun_ref', |
| 5366 | )), |
| 5367 | ('<limits>', ('numeric_limits',)), |
| 5368 | ('<list>', ('list',)), |
| 5369 | ('<map>', ('map', 'multimap',)), |
lhchavez | 2d1b6da | 2016-07-13 10:40:01 -0700 | [diff] [blame] | 5370 | ('<memory>', ('allocator', 'make_shared', 'make_unique', 'shared_ptr', |
| 5371 | 'unique_ptr', 'weak_ptr')), |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5372 | ('<queue>', ('queue', 'priority_queue',)), |
| 5373 | ('<set>', ('set', 'multiset',)), |
| 5374 | ('<stack>', ('stack',)), |
| 5375 | ('<string>', ('char_traits', 'basic_string',)), |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5376 | ('<tuple>', ('tuple',)), |
lhchavez | 2d1b6da | 2016-07-13 10:40:01 -0700 | [diff] [blame] | 5377 | ('<unordered_map>', ('unordered_map', 'unordered_multimap')), |
| 5378 | ('<unordered_set>', ('unordered_set', 'unordered_multiset')), |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5379 | ('<utility>', ('pair',)), |
| 5380 | ('<vector>', ('vector',)), |
| 5381 | |
| 5382 | # gcc extensions. |
| 5383 | # Note: std::hash is their hash, ::hash is our hash |
| 5384 | ('<hash_map>', ('hash_map', 'hash_multimap',)), |
| 5385 | ('<hash_set>', ('hash_set', 'hash_multiset',)), |
| 5386 | ('<slist>', ('slist',)), |
| 5387 | ) |
| 5388 | |
skym@chromium.org | 3990c41 | 2016-02-05 20:55:12 +0000 | [diff] [blame] | 5389 | _HEADERS_MAYBE_TEMPLATES = ( |
| 5390 | ('<algorithm>', ('copy', 'max', 'min', 'min_element', 'sort', |
| 5391 | 'transform', |
| 5392 | )), |
lhchavez | 2d1b6da | 2016-07-13 10:40:01 -0700 | [diff] [blame] | 5393 | ('<utility>', ('forward', 'make_pair', 'move', 'swap')), |
skym@chromium.org | 3990c41 | 2016-02-05 20:55:12 +0000 | [diff] [blame] | 5394 | ) |
| 5395 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5396 | _RE_PATTERN_STRING = re.compile(r'\bstring\b') |
| 5397 | |
skym@chromium.org | 3990c41 | 2016-02-05 20:55:12 +0000 | [diff] [blame] | 5398 | _re_pattern_headers_maybe_templates = [] |
| 5399 | for _header, _templates in _HEADERS_MAYBE_TEMPLATES: |
| 5400 | for _template in _templates: |
| 5401 | # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or |
| 5402 | # type::max(). |
| 5403 | _re_pattern_headers_maybe_templates.append( |
| 5404 | (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'), |
| 5405 | _template, |
| 5406 | _header)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5407 | |
skym@chromium.org | 3990c41 | 2016-02-05 20:55:12 +0000 | [diff] [blame] | 5408 | # Other scripts may reach in and modify this pattern. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5409 | _re_pattern_templates = [] |
| 5410 | for _header, _templates in _HEADERS_CONTAINING_TEMPLATES: |
| 5411 | for _template in _templates: |
| 5412 | _re_pattern_templates.append( |
| 5413 | (re.compile(r'(\<|\b)' + _template + r'\s*\<'), |
| 5414 | _template + '<>', |
| 5415 | _header)) |
| 5416 | |
| 5417 | |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5418 | def FilesBelongToSameModule(filename_cc, filename_h): |
| 5419 | """Check if these two filenames belong to the same module. |
| 5420 | |
| 5421 | The concept of a 'module' here is a as follows: |
| 5422 | foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the |
| 5423 | same 'module' if they are in the same directory. |
| 5424 | some/path/public/xyzzy and some/path/internal/xyzzy are also considered |
| 5425 | to belong to the same module here. |
| 5426 | |
| 5427 | If the filename_cc contains a longer path than the filename_h, for example, |
| 5428 | '/absolute/path/to/base/sysinfo.cc', and this file would include |
| 5429 | 'base/sysinfo.h', this function also produces the prefix needed to open the |
| 5430 | header. This is used by the caller of this function to more robustly open the |
| 5431 | header file. We don't have access to the real include paths in this context, |
| 5432 | so we need this guesswork here. |
| 5433 | |
| 5434 | Known bugs: tools/base/bar.cc and base/bar.h belong to the same module |
| 5435 | according to this implementation. Because of this, this function gives |
| 5436 | some false positives. This should be sufficiently rare in practice. |
| 5437 | |
| 5438 | Args: |
| 5439 | filename_cc: is the path for the .cc file |
| 5440 | filename_h: is the path for the header path |
| 5441 | |
| 5442 | Returns: |
| 5443 | Tuple with a bool and a string: |
| 5444 | bool: True if filename_cc and filename_h belong to the same module. |
| 5445 | string: the additional prefix needed to open the header file. |
| 5446 | """ |
| 5447 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 5448 | fileinfo = FileInfo(filename_cc) |
| 5449 | if not fileinfo.IsSource(): |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5450 | return (False, '') |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 5451 | filename_cc = filename_cc[:-len(fileinfo.Extension())] |
| 5452 | matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()) |
| 5453 | if matched_test_suffix: |
| 5454 | filename_cc = filename_cc[:-len(matched_test_suffix.group(1))] |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5455 | filename_cc = filename_cc.replace('/public/', '/') |
| 5456 | filename_cc = filename_cc.replace('/internal/', '/') |
| 5457 | |
| 5458 | if not filename_h.endswith('.h'): |
| 5459 | return (False, '') |
| 5460 | filename_h = filename_h[:-len('.h')] |
| 5461 | if filename_h.endswith('-inl'): |
| 5462 | filename_h = filename_h[:-len('-inl')] |
| 5463 | filename_h = filename_h.replace('/public/', '/') |
| 5464 | filename_h = filename_h.replace('/internal/', '/') |
| 5465 | |
| 5466 | files_belong_to_same_module = filename_cc.endswith(filename_h) |
| 5467 | common_path = '' |
| 5468 | if files_belong_to_same_module: |
| 5469 | common_path = filename_cc[:-len(filename_h)] |
| 5470 | return files_belong_to_same_module, common_path |
| 5471 | |
| 5472 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5473 | def UpdateIncludeState(filename, include_dict, io=codecs): |
| 5474 | """Fill up the include_dict with new includes found from the file. |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5475 | |
| 5476 | Args: |
| 5477 | filename: the name of the header to read. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5478 | include_dict: a dictionary in which the headers are inserted. |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5479 | io: The io factory to use to read the file. Provided for testability. |
| 5480 | |
| 5481 | Returns: |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5482 | True if a header was successfully added. False otherwise. |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5483 | """ |
| 5484 | headerfile = None |
| 5485 | try: |
| 5486 | headerfile = io.open(filename, 'r', 'utf8', 'replace') |
| 5487 | except IOError: |
| 5488 | return False |
| 5489 | linenum = 0 |
| 5490 | for line in headerfile: |
| 5491 | linenum += 1 |
| 5492 | clean_line = CleanseComments(line) |
| 5493 | match = _RE_PATTERN_INCLUDE.search(clean_line) |
| 5494 | if match: |
| 5495 | include = match.group(2) |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5496 | include_dict.setdefault(include, linenum) |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5497 | return True |
| 5498 | |
| 5499 | |
| 5500 | def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error, |
| 5501 | io=codecs): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5502 | """Reports for missing stl includes. |
| 5503 | |
| 5504 | This function will output warnings to make sure you are including the headers |
| 5505 | necessary for the stl containers and functions that you use. We only give one |
| 5506 | reason to include a header. For example, if you use both equal_to<> and |
| 5507 | less<> in a .h file, only one (the latter in the file) of these will be |
| 5508 | reported as a reason to include the <functional>. |
| 5509 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5510 | Args: |
| 5511 | filename: The name of the current file. |
| 5512 | clean_lines: A CleansedLines instance containing the file. |
| 5513 | include_state: An _IncludeState instance. |
| 5514 | error: The function to call with any errors found. |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5515 | io: The IO factory to use to read the header file. Provided for unittest |
| 5516 | injection. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5517 | """ |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5518 | required = {} # A map of header name to linenumber and the template entity. |
| 5519 | # Example of required: { '<functional>': (1219, 'less<>') } |
| 5520 | |
| 5521 | for linenum in xrange(clean_lines.NumLines()): |
| 5522 | line = clean_lines.elided[linenum] |
| 5523 | if not line or line[0] == '#': |
| 5524 | continue |
| 5525 | |
| 5526 | # String is special -- it is a non-templatized type in STL. |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5527 | matched = _RE_PATTERN_STRING.search(line) |
| 5528 | if matched: |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 5529 | # Don't warn about strings in non-STL namespaces: |
| 5530 | # (We check only the first match per line; good enough.) |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5531 | prefix = line[:matched.start()] |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 5532 | if prefix.endswith('std::') or not prefix.endswith('::'): |
| 5533 | required['<string>'] = (linenum, 'string') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5534 | |
skym@chromium.org | 3990c41 | 2016-02-05 20:55:12 +0000 | [diff] [blame] | 5535 | for pattern, template, header in _re_pattern_headers_maybe_templates: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5536 | if pattern.search(line): |
| 5537 | required[header] = (linenum, template) |
| 5538 | |
| 5539 | # The following function is just a speed up, no semantics are changed. |
| 5540 | if not '<' in line: # Reduces the cpu time usage by skipping lines. |
| 5541 | continue |
| 5542 | |
| 5543 | for pattern, template, header in _re_pattern_templates: |
lhchavez | 9b2173c | 2016-07-13 10:20:07 -0700 | [diff] [blame] | 5544 | matched = pattern.search(line) |
| 5545 | if matched: |
| 5546 | # Don't warn about IWYU in non-STL namespaces: |
| 5547 | # (We check only the first match per line; good enough.) |
| 5548 | prefix = line[:matched.start()] |
| 5549 | if prefix.endswith('std::') or not prefix.endswith('::'): |
| 5550 | required[header] = (linenum, template) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5551 | |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5552 | # The policy is that if you #include something in foo.h you don't need to |
| 5553 | # include it again in foo.cc. Here, we will look at possible includes. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5554 | # Let's flatten the include_state include_list and copy it into a dictionary. |
| 5555 | include_dict = dict([item for sublist in include_state.include_list |
| 5556 | for item in sublist]) |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5557 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5558 | # Did we find the header for this file (if any) and successfully load it? |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5559 | header_found = False |
| 5560 | |
| 5561 | # Use the absolute path so that matching works properly. |
erg@chromium.org | 8f92756 | 2012-01-30 19:51:28 +0000 | [diff] [blame] | 5562 | abs_filename = FileInfo(filename).FullName() |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5563 | |
| 5564 | # For Emacs's flymake. |
| 5565 | # If cpplint is invoked from Emacs's flymake, a temporary file is generated |
| 5566 | # by flymake and that file name might end with '_flymake.cc'. In that case, |
| 5567 | # restore original file name here so that the corresponding header file can be |
| 5568 | # found. |
| 5569 | # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h' |
| 5570 | # instead of 'foo_flymake.h' |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 5571 | abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename) |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5572 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5573 | # include_dict is modified during iteration, so we iterate over a copy of |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5574 | # the keys. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5575 | header_keys = include_dict.keys() |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5576 | for header in header_keys: |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5577 | (same_module, common_path) = FilesBelongToSameModule(abs_filename, header) |
| 5578 | fullpath = common_path + header |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5579 | if same_module and UpdateIncludeState(fullpath, include_dict, io): |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5580 | header_found = True |
| 5581 | |
| 5582 | # If we can't find the header file for a .cc, assume it's because we don't |
| 5583 | # know where to look. In that case we'll give up as we're not sure they |
| 5584 | # didn't include it in the .h file. |
| 5585 | # TODO(unknown): Do a better job of finding .h files so we are confident that |
| 5586 | # not having the .h file means there isn't one. |
| 5587 | if filename.endswith('.cc') and not header_found: |
| 5588 | return |
| 5589 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5590 | # All the lines have been processed, report the errors found. |
| 5591 | for required_header_unstripped in required: |
| 5592 | template = required[required_header_unstripped][1] |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5593 | if required_header_unstripped.strip('<>"') not in include_dict: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5594 | error(filename, required[required_header_unstripped][0], |
| 5595 | 'build/include_what_you_use', 4, |
| 5596 | 'Add #include ' + required_header_unstripped + ' for ' + template) |
| 5597 | |
| 5598 | |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5599 | _RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<') |
| 5600 | |
| 5601 | |
| 5602 | def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error): |
| 5603 | """Check that make_pair's template arguments are deduced. |
| 5604 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5605 | G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5606 | specified explicitly, and such use isn't intended in any case. |
| 5607 | |
| 5608 | Args: |
| 5609 | filename: The name of the current file. |
| 5610 | clean_lines: A CleansedLines instance containing the file. |
| 5611 | linenum: The number of the line to check. |
| 5612 | error: The function to call with any errors found. |
| 5613 | """ |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5614 | line = clean_lines.elided[linenum] |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5615 | match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line) |
| 5616 | if match: |
| 5617 | error(filename, linenum, 'build/explicit_make_pair', |
| 5618 | 4, # 4 = high confidence |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 5619 | 'For C++11-compatibility, omit template arguments from make_pair' |
| 5620 | ' OR use pair directly OR if appropriate, construct a pair directly') |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5621 | |
| 5622 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5623 | def CheckRedundantVirtual(filename, clean_lines, linenum, error): |
| 5624 | """Check if line contains a redundant "virtual" function-specifier. |
| 5625 | |
| 5626 | Args: |
| 5627 | filename: The name of the current file. |
| 5628 | clean_lines: A CleansedLines instance containing the file. |
| 5629 | linenum: The number of the line to check. |
| 5630 | error: The function to call with any errors found. |
| 5631 | """ |
| 5632 | # Look for "virtual" on current line. |
| 5633 | line = clean_lines.elided[linenum] |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5634 | virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line) |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5635 | if not virtual: return |
| 5636 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5637 | # Ignore "virtual" keywords that are near access-specifiers. These |
| 5638 | # are only used in class base-specifier and do not apply to member |
| 5639 | # functions. |
| 5640 | if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or |
| 5641 | Match(r'^\s+(public|protected|private)\b', virtual.group(3))): |
| 5642 | return |
| 5643 | |
| 5644 | # Ignore the "virtual" keyword from virtual base classes. Usually |
| 5645 | # there is a column on the same line in these cases (virtual base |
| 5646 | # classes are rare in google3 because multiple inheritance is rare). |
| 5647 | if Match(r'^.*[^:]:[^:].*$', line): return |
| 5648 | |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5649 | # Look for the next opening parenthesis. This is the start of the |
| 5650 | # parameter list (possibly on the next line shortly after virtual). |
| 5651 | # TODO(unknown): doesn't work if there are virtual functions with |
| 5652 | # decltype() or other things that use parentheses, but csearch suggests |
| 5653 | # that this is rare. |
| 5654 | end_col = -1 |
| 5655 | end_line = -1 |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5656 | start_col = len(virtual.group(2)) |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5657 | for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())): |
| 5658 | line = clean_lines.elided[start_line][start_col:] |
| 5659 | parameter_list = Match(r'^([^(]*)\(', line) |
| 5660 | if parameter_list: |
| 5661 | # Match parentheses to find the end of the parameter list |
| 5662 | (_, end_line, end_col) = CloseExpression( |
| 5663 | clean_lines, start_line, start_col + len(parameter_list.group(1))) |
| 5664 | break |
| 5665 | start_col = 0 |
| 5666 | |
| 5667 | if end_col < 0: |
| 5668 | return # Couldn't find end of parameter list, give up |
| 5669 | |
| 5670 | # Look for "override" or "final" after the parameter list |
| 5671 | # (possibly on the next few lines). |
| 5672 | for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())): |
| 5673 | line = clean_lines.elided[i][end_col:] |
| 5674 | match = Search(r'\b(override|final)\b', line) |
| 5675 | if match: |
| 5676 | error(filename, linenum, 'readability/inheritance', 4, |
| 5677 | ('"virtual" is redundant since function is ' |
| 5678 | 'already declared as "%s"' % match.group(1))) |
| 5679 | |
| 5680 | # Set end_col to check whole lines after we are done with the |
| 5681 | # first line. |
| 5682 | end_col = 0 |
| 5683 | if Search(r'[^\w]\s*$', line): |
| 5684 | break |
| 5685 | |
| 5686 | |
| 5687 | def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error): |
| 5688 | """Check if line contains a redundant "override" or "final" virt-specifier. |
| 5689 | |
| 5690 | Args: |
| 5691 | filename: The name of the current file. |
| 5692 | clean_lines: A CleansedLines instance containing the file. |
| 5693 | linenum: The number of the line to check. |
| 5694 | error: The function to call with any errors found. |
| 5695 | """ |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5696 | # Look for closing parenthesis nearby. We need one to confirm where |
| 5697 | # the declarator ends and where the virt-specifier starts to avoid |
| 5698 | # false positives. |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5699 | line = clean_lines.elided[linenum] |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5700 | declarator_end = line.rfind(')') |
| 5701 | if declarator_end >= 0: |
| 5702 | fragment = line[declarator_end:] |
| 5703 | else: |
| 5704 | if linenum > 1 and clean_lines.elided[linenum - 1].rfind(')') >= 0: |
| 5705 | fragment = line |
| 5706 | else: |
| 5707 | return |
| 5708 | |
| 5709 | # Check that at most one of "override" or "final" is present, not both |
| 5710 | if Search(r'\boverride\b', fragment) and Search(r'\bfinal\b', fragment): |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5711 | error(filename, linenum, 'readability/inheritance', 4, |
| 5712 | ('"override" is redundant since function is ' |
| 5713 | 'already declared as "final"')) |
| 5714 | |
| 5715 | |
| 5716 | |
| 5717 | |
| 5718 | # Returns true if we are at a new block, and it is directly |
| 5719 | # inside of a namespace. |
| 5720 | def IsBlockInNameSpace(nesting_state, is_forward_declaration): |
| 5721 | """Checks that the new block is directly in a namespace. |
| 5722 | |
| 5723 | Args: |
| 5724 | nesting_state: The _NestingState object that contains info about our state. |
| 5725 | is_forward_declaration: If the class is a forward declared class. |
| 5726 | Returns: |
| 5727 | Whether or not the new block is directly in a namespace. |
| 5728 | """ |
| 5729 | if is_forward_declaration: |
| 5730 | if len(nesting_state.stack) >= 1 and ( |
| 5731 | isinstance(nesting_state.stack[-1], _NamespaceInfo)): |
| 5732 | return True |
| 5733 | else: |
| 5734 | return False |
| 5735 | |
| 5736 | return (len(nesting_state.stack) > 1 and |
| 5737 | nesting_state.stack[-1].check_namespace_indentation and |
| 5738 | isinstance(nesting_state.stack[-2], _NamespaceInfo)) |
| 5739 | |
| 5740 | |
| 5741 | def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item, |
| 5742 | raw_lines_no_comments, linenum): |
| 5743 | """This method determines if we should apply our namespace indentation check. |
| 5744 | |
| 5745 | Args: |
| 5746 | nesting_state: The current nesting state. |
| 5747 | is_namespace_indent_item: If we just put a new class on the stack, True. |
| 5748 | If the top of the stack is not a class, or we did not recently |
| 5749 | add the class, False. |
| 5750 | raw_lines_no_comments: The lines without the comments. |
| 5751 | linenum: The current line number we are processing. |
| 5752 | |
| 5753 | Returns: |
| 5754 | True if we should apply our namespace indentation check. Currently, it |
| 5755 | only works for classes and namespaces inside of a namespace. |
| 5756 | """ |
| 5757 | |
| 5758 | is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments, |
| 5759 | linenum) |
| 5760 | |
| 5761 | if not (is_namespace_indent_item or is_forward_declaration): |
| 5762 | return False |
| 5763 | |
| 5764 | # If we are in a macro, we do not want to check the namespace indentation. |
| 5765 | if IsMacroDefinition(raw_lines_no_comments, linenum): |
| 5766 | return False |
| 5767 | |
| 5768 | return IsBlockInNameSpace(nesting_state, is_forward_declaration) |
| 5769 | |
| 5770 | |
| 5771 | # Call this method if the line is directly inside of a namespace. |
| 5772 | # If the line above is blank (excluding comments) or the start of |
| 5773 | # an inner namespace, it cannot be indented. |
| 5774 | def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum, |
| 5775 | error): |
| 5776 | line = raw_lines_no_comments[linenum] |
| 5777 | if Match(r'^\s+', line): |
| 5778 | error(filename, linenum, 'runtime/indentation_namespace', 4, |
| 5779 | 'Do not indent within a namespace') |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5780 | |
| 5781 | |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 5782 | def ProcessLine(filename, file_extension, clean_lines, line, |
| 5783 | include_state, function_state, nesting_state, error, |
| 5784 | extra_check_functions=[]): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5785 | """Processes a single line in the file. |
| 5786 | |
| 5787 | Args: |
| 5788 | filename: Filename of the file that is being processed. |
| 5789 | file_extension: The extension (dot not included) of the file. |
| 5790 | clean_lines: An array of strings, each representing a line of the file, |
| 5791 | with comments stripped. |
| 5792 | line: Number of line being processed. |
| 5793 | include_state: An _IncludeState instance in which the headers are inserted. |
| 5794 | function_state: A _FunctionState instance which counts function lines, etc. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5795 | nesting_state: A NestingState instance which maintains information about |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 5796 | the current stack of nested blocks being parsed. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5797 | error: A callable to which errors are reported, which takes 4 arguments: |
| 5798 | filename, line number, error level, and message |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5799 | extra_check_functions: An array of additional check functions that will be |
| 5800 | run on each source line. Each function takes 4 |
| 5801 | arguments: filename, clean_lines, line, error |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5802 | """ |
| 5803 | raw_lines = clean_lines.raw_lines |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 5804 | ParseNolintSuppressions(filename, raw_lines[line], line, error) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 5805 | nesting_state.Update(filename, clean_lines, line, error) |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5806 | CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line, |
| 5807 | error) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5808 | if nesting_state.InAsmBlock(): return |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5809 | CheckForFunctionLengths(filename, clean_lines, line, function_state, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5810 | CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error) |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 5811 | CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5812 | CheckLanguage(filename, clean_lines, line, file_extension, include_state, |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5813 | nesting_state, error) |
| 5814 | CheckForNonConstReference(filename, clean_lines, line, nesting_state, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5815 | CheckForNonStandardConstructs(filename, clean_lines, line, |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 5816 | nesting_state, error) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5817 | CheckVlogArguments(filename, clean_lines, line, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5818 | CheckPosixThreading(filename, clean_lines, line, error) |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 5819 | CheckInvalidIncrement(filename, clean_lines, line, error) |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5820 | CheckMakePairUsesDeduction(filename, clean_lines, line, error) |
avakulenko@google.com | 5914675 | 2014-08-11 20:20:55 +0000 | [diff] [blame] | 5821 | CheckRedundantVirtual(filename, clean_lines, line, error) |
| 5822 | CheckRedundantOverrideOrFinal(filename, clean_lines, line, error) |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5823 | for check_fn in extra_check_functions: |
| 5824 | check_fn(filename, clean_lines, line, error) |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 5825 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5826 | def FlagCxx11Features(filename, clean_lines, linenum, error): |
| 5827 | """Flag those c++11 features that we only allow in certain places. |
| 5828 | |
| 5829 | Args: |
| 5830 | filename: The name of the current file. |
| 5831 | clean_lines: A CleansedLines instance containing the file. |
| 5832 | linenum: The number of the line to check. |
| 5833 | error: The function to call with any errors found. |
| 5834 | """ |
| 5835 | line = clean_lines.elided[linenum] |
| 5836 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5837 | include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line) |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 5838 | |
| 5839 | # Flag unapproved C++ TR1 headers. |
| 5840 | if include and include.group(1).startswith('tr1/'): |
| 5841 | error(filename, linenum, 'build/c++tr1', 5, |
| 5842 | ('C++ TR1 headers such as <%s> are unapproved.') % include.group(1)) |
| 5843 | |
| 5844 | # Flag unapproved C++11 headers. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5845 | if include and include.group(1) in ('cfenv', |
| 5846 | 'condition_variable', |
| 5847 | 'fenv.h', |
| 5848 | 'future', |
| 5849 | 'mutex', |
| 5850 | 'thread', |
| 5851 | 'chrono', |
| 5852 | 'ratio', |
| 5853 | 'regex', |
| 5854 | 'system_error', |
| 5855 | ): |
| 5856 | error(filename, linenum, 'build/c++11', 5, |
| 5857 | ('<%s> is an unapproved C++11 header.') % include.group(1)) |
| 5858 | |
| 5859 | # The only place where we need to worry about C++11 keywords and library |
| 5860 | # features in preprocessor directives is in macro definitions. |
| 5861 | if Match(r'\s*#', line) and not Match(r'\s*#\s*define\b', line): return |
| 5862 | |
| 5863 | # These are classes and free functions. The classes are always |
| 5864 | # mentioned as std::*, but we only catch the free functions if |
| 5865 | # they're not found by ADL. They're alphabetical by header. |
| 5866 | for top_name in ( |
| 5867 | # type_traits |
| 5868 | 'alignment_of', |
| 5869 | 'aligned_union', |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5870 | ): |
| 5871 | if Search(r'\bstd::%s\b' % top_name, line): |
| 5872 | error(filename, linenum, 'build/c++11', 5, |
| 5873 | ('std::%s is an unapproved C++11 class or function. Send c-style ' |
| 5874 | 'an example of where it would make your code more readable, and ' |
| 5875 | 'they may let you use it.') % top_name) |
| 5876 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5877 | |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 5878 | def FlagCxx14Features(filename, clean_lines, linenum, error): |
| 5879 | """Flag those C++14 features that we restrict. |
| 5880 | |
| 5881 | Args: |
| 5882 | filename: The name of the current file. |
| 5883 | clean_lines: A CleansedLines instance containing the file. |
| 5884 | linenum: The number of the line to check. |
| 5885 | error: The function to call with any errors found. |
| 5886 | """ |
| 5887 | line = clean_lines.elided[linenum] |
| 5888 | |
| 5889 | include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line) |
| 5890 | |
| 5891 | # Flag unapproved C++14 headers. |
| 5892 | if include and include.group(1) in ('scoped_allocator', 'shared_mutex'): |
| 5893 | error(filename, linenum, 'build/c++14', 5, |
| 5894 | ('<%s> is an unapproved C++14 header.') % include.group(1)) |
| 5895 | |
| 5896 | |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5897 | def ProcessFileData(filename, file_extension, lines, error, |
| 5898 | extra_check_functions=[]): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5899 | """Performs lint checks and reports any errors to the given error function. |
| 5900 | |
| 5901 | Args: |
| 5902 | filename: Filename of the file that is being processed. |
| 5903 | file_extension: The extension (dot not included) of the file. |
| 5904 | lines: An array of strings, each representing a line of the file, with the |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5905 | last element being empty if the file is terminated with a newline. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5906 | error: A callable to which errors are reported, which takes 4 arguments: |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5907 | filename, line number, error level, and message |
| 5908 | extra_check_functions: An array of additional check functions that will be |
| 5909 | run on each source line. Each function takes 4 |
| 5910 | arguments: filename, clean_lines, line, error |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5911 | """ |
| 5912 | lines = (['// marker so line numbers and indices both start at 1'] + lines + |
| 5913 | ['// marker so line numbers end in a known way']) |
| 5914 | |
| 5915 | include_state = _IncludeState() |
| 5916 | function_state = _FunctionState() |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5917 | nesting_state = NestingState() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5918 | |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 5919 | ResetNolintSuppressions() |
| 5920 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5921 | CheckForCopyright(filename, lines, error) |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 5922 | ProcessGlobalSuppresions(lines) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5923 | RemoveMultiLineComments(filename, lines, error) |
| 5924 | clean_lines = CleansedLines(lines) |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5925 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 5926 | if IsHeaderExtension(file_extension): |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5927 | CheckForHeaderGuard(filename, clean_lines, error) |
| 5928 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5929 | for line in xrange(clean_lines.NumLines()): |
| 5930 | ProcessLine(filename, file_extension, clean_lines, line, |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 5931 | include_state, function_state, nesting_state, error, |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 5932 | extra_check_functions) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 5933 | FlagCxx11Features(filename, clean_lines, line, error) |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5934 | nesting_state.CheckCompletedBlocks(filename, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5935 | |
| 5936 | CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error) |
skym@chromium.org | 3990c41 | 2016-02-05 20:55:12 +0000 | [diff] [blame] | 5937 | |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5938 | # Check that the .cc file has included its header if it exists. |
avakulenko@chromium.org | 764ce71 | 2016-05-06 23:03:41 +0000 | [diff] [blame] | 5939 | if _IsSourceExtension(file_extension): |
avakulenko@google.com | 255f2be | 2014-12-05 22:19:55 +0000 | [diff] [blame] | 5940 | CheckHeaderFileIncluded(filename, include_state, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5941 | |
| 5942 | # We check here rather than inside ProcessLine so that we see raw |
| 5943 | # lines rather than "cleaned" lines. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 5944 | CheckForBadCharacters(filename, lines, error) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 5945 | |
| 5946 | CheckForNewlineAtEOF(filename, lines, error) |
| 5947 | |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 5948 | def ProcessConfigOverrides(filename): |
| 5949 | """ Loads the configuration files and processes the config overrides. |
| 5950 | |
| 5951 | Args: |
| 5952 | filename: The name of the file being processed by the linter. |
| 5953 | |
| 5954 | Returns: |
| 5955 | False if the current |filename| should not be processed further. |
| 5956 | """ |
| 5957 | |
| 5958 | abs_filename = os.path.abspath(filename) |
| 5959 | cfg_filters = [] |
| 5960 | keep_looking = True |
| 5961 | while keep_looking: |
| 5962 | abs_path, base_name = os.path.split(abs_filename) |
| 5963 | if not base_name: |
| 5964 | break # Reached the root directory. |
| 5965 | |
| 5966 | cfg_file = os.path.join(abs_path, "CPPLINT.cfg") |
| 5967 | abs_filename = abs_path |
| 5968 | if not os.path.isfile(cfg_file): |
| 5969 | continue |
| 5970 | |
| 5971 | try: |
| 5972 | with open(cfg_file) as file_handle: |
| 5973 | for line in file_handle: |
| 5974 | line, _, _ = line.partition('#') # Remove comments. |
| 5975 | if not line.strip(): |
| 5976 | continue |
| 5977 | |
| 5978 | name, _, val = line.partition('=') |
| 5979 | name = name.strip() |
| 5980 | val = val.strip() |
| 5981 | if name == 'set noparent': |
| 5982 | keep_looking = False |
| 5983 | elif name == 'filter': |
| 5984 | cfg_filters.append(val) |
| 5985 | elif name == 'exclude_files': |
| 5986 | # When matching exclude_files pattern, use the base_name of |
| 5987 | # the current file name or the directory name we are processing. |
| 5988 | # For example, if we are checking for lint errors in /foo/bar/baz.cc |
| 5989 | # and we found the .cfg file at /foo/CPPLINT.cfg, then the config |
| 5990 | # file's "exclude_files" filter is meant to be checked against "bar" |
| 5991 | # and not "baz" nor "bar/baz.cc". |
| 5992 | if base_name: |
| 5993 | pattern = re.compile(val) |
| 5994 | if pattern.match(base_name): |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 5995 | if _cpplint_state.quiet: |
| 5996 | # Suppress "Ignoring file" warning when using --quiet. |
| 5997 | return False |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 5998 | sys.stderr.write('Ignoring "%s": file excluded by "%s". ' |
| 5999 | 'File path component "%s" matches ' |
| 6000 | 'pattern "%s"\n' % |
| 6001 | (filename, cfg_file, base_name, val)) |
| 6002 | return False |
avakulenko@google.com | 68a4fa6 | 2014-08-25 16:26:18 +0000 | [diff] [blame] | 6003 | elif name == 'linelength': |
| 6004 | global _line_length |
| 6005 | try: |
| 6006 | _line_length = int(val) |
| 6007 | except ValueError: |
| 6008 | sys.stderr.write('Line length must be numeric.') |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 6009 | elif name == 'root': |
| 6010 | global _root |
| 6011 | # root directories are specified relative to CPPLINT.cfg dir. |
| 6012 | _root = os.path.join(os.path.dirname(cfg_file), val) |
| 6013 | elif name == 'headers': |
| 6014 | ProcessHppHeadersOption(val) |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 6015 | else: |
| 6016 | sys.stderr.write( |
| 6017 | 'Invalid configuration option (%s) in file %s\n' % |
| 6018 | (name, cfg_file)) |
| 6019 | |
| 6020 | except IOError: |
| 6021 | sys.stderr.write( |
| 6022 | "Skipping config file '%s': Can't open for reading\n" % cfg_file) |
| 6023 | keep_looking = False |
| 6024 | |
| 6025 | # Apply all the accumulated filters in reverse order (top-level directory |
| 6026 | # config options having the least priority). |
| 6027 | for filter in reversed(cfg_filters): |
| 6028 | _AddFilters(filter) |
| 6029 | |
| 6030 | return True |
| 6031 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 6032 | |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 6033 | def ProcessFile(filename, vlevel, extra_check_functions=[]): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6034 | """Does google-lint on a single file. |
| 6035 | |
| 6036 | Args: |
| 6037 | filename: The name of the file to parse. |
| 6038 | |
| 6039 | vlevel: The level of errors to report. Every error of confidence |
| 6040 | >= verbose_level will be reported. 0 is a good default. |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 6041 | |
| 6042 | extra_check_functions: An array of additional check functions that will be |
| 6043 | run on each source line. Each function takes 4 |
| 6044 | arguments: filename, clean_lines, line, error |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6045 | """ |
| 6046 | |
| 6047 | _SetVerboseLevel(vlevel) |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 6048 | _BackupFilters() |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 6049 | old_errors = _cpplint_state.error_count |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 6050 | |
| 6051 | if not ProcessConfigOverrides(filename): |
| 6052 | _RestoreFilters() |
| 6053 | return |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6054 | |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 6055 | lf_lines = [] |
| 6056 | crlf_lines = [] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6057 | try: |
| 6058 | # Support the UNIX convention of using "-" for stdin. Note that |
| 6059 | # we are not opening the file with universal newline support |
| 6060 | # (which codecs doesn't support anyway), so the resulting lines do |
| 6061 | # contain trailing '\r' characters if we are reading a file that |
| 6062 | # has CRLF endings. |
| 6063 | # If after the split a trailing '\r' is present, it is removed |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 6064 | # below. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6065 | if filename == '-': |
| 6066 | lines = codecs.StreamReaderWriter(sys.stdin, |
| 6067 | codecs.getreader('utf8'), |
| 6068 | codecs.getwriter('utf8'), |
| 6069 | 'replace').read().split('\n') |
| 6070 | else: |
| 6071 | lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n') |
| 6072 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6073 | # Remove trailing '\r'. |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 6074 | # The -1 accounts for the extra trailing blank line we get from split() |
| 6075 | for linenum in range(len(lines) - 1): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6076 | if lines[linenum].endswith('\r'): |
| 6077 | lines[linenum] = lines[linenum].rstrip('\r') |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 6078 | crlf_lines.append(linenum + 1) |
| 6079 | else: |
| 6080 | lf_lines.append(linenum + 1) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6081 | |
| 6082 | except IOError: |
| 6083 | sys.stderr.write( |
| 6084 | "Skipping input '%s': Can't open for reading\n" % filename) |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 6085 | _RestoreFilters() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6086 | return |
| 6087 | |
| 6088 | # Note, if no dot is found, this will give the entire filename as the ext. |
| 6089 | file_extension = filename[filename.rfind('.') + 1:] |
| 6090 | |
| 6091 | # When reading from stdin, the extension is unknown, so no cpplint tests |
| 6092 | # should rely on the extension. |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 6093 | if filename != '-' and file_extension not in _valid_extensions: |
| 6094 | sys.stderr.write('Ignoring %s; not a valid file name ' |
| 6095 | '(%s)\n' % (filename, ', '.join(_valid_extensions))) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6096 | else: |
asvitkine@chromium.org | 8b8d8be | 2011-09-08 15:34:45 +0000 | [diff] [blame] | 6097 | ProcessFileData(filename, file_extension, lines, Error, |
| 6098 | extra_check_functions) |
avakulenko@google.com | d39bbb5 | 2014-06-04 22:55:20 +0000 | [diff] [blame] | 6099 | |
| 6100 | # If end-of-line sequences are a mix of LF and CR-LF, issue |
| 6101 | # warnings on the lines with CR. |
| 6102 | # |
| 6103 | # Don't issue any warnings if all lines are uniformly LF or CR-LF, |
| 6104 | # since critique can handle these just fine, and the style guide |
| 6105 | # doesn't dictate a particular end of line sequence. |
| 6106 | # |
| 6107 | # We can't depend on os.linesep to determine what the desired |
| 6108 | # end-of-line sequence should be, since that will return the |
| 6109 | # server-side end-of-line sequence. |
| 6110 | if lf_lines and crlf_lines: |
| 6111 | # Warn on every line with CR. An alternative approach might be to |
| 6112 | # check whether the file is mostly CRLF or just LF, and warn on the |
| 6113 | # minority, we bias toward LF here since most tools prefer LF. |
| 6114 | for linenum in crlf_lines: |
| 6115 | Error(filename, linenum, 'whitespace/newline', 1, |
| 6116 | 'Unexpected \\r (^M) found; better to use only \\n') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6117 | |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 6118 | # Suppress printing anything if --quiet was passed unless the error |
| 6119 | # count has increased after processing this file. |
| 6120 | if not _cpplint_state.quiet or old_errors != _cpplint_state.error_count: |
| 6121 | sys.stdout.write('Done processing %s\n' % filename) |
avakulenko@google.com | 1744993 | 2014-07-28 22:13:33 +0000 | [diff] [blame] | 6122 | _RestoreFilters() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6123 | |
| 6124 | |
| 6125 | def PrintUsage(message): |
| 6126 | """Prints a brief usage string and exits, optionally with an error message. |
| 6127 | |
| 6128 | Args: |
| 6129 | message: The optional error message. |
| 6130 | """ |
| 6131 | sys.stderr.write(_USAGE) |
| 6132 | if message: |
| 6133 | sys.exit('\nFATAL ERROR: ' + message) |
| 6134 | else: |
| 6135 | sys.exit(1) |
| 6136 | |
| 6137 | |
| 6138 | def PrintCategories(): |
| 6139 | """Prints a list of all the error-categories used by error messages. |
| 6140 | |
| 6141 | These are the categories used to filter messages via --filter. |
| 6142 | """ |
erg@google.com | 35589e6 | 2010-11-17 18:58:16 +0000 | [diff] [blame] | 6143 | sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6144 | sys.exit(0) |
| 6145 | |
| 6146 | |
| 6147 | def ParseArguments(args): |
| 6148 | """Parses the command line arguments. |
| 6149 | |
| 6150 | This may set the output format and verbosity level as side-effects. |
| 6151 | |
| 6152 | Args: |
| 6153 | args: The command line arguments: |
| 6154 | |
| 6155 | Returns: |
| 6156 | The list of filenames to lint. |
| 6157 | """ |
| 6158 | try: |
| 6159 | (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=', |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 6160 | 'counting=', |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 6161 | 'filter=', |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 6162 | 'root=', |
| 6163 | 'linelength=', |
sdefresne | 263e928 | 2016-07-19 02:14:22 -0700 | [diff] [blame] | 6164 | 'extensions=', |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 6165 | 'headers=', |
| 6166 | 'quiet']) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6167 | except getopt.GetoptError: |
| 6168 | PrintUsage('Invalid arguments.') |
| 6169 | |
| 6170 | verbosity = _VerboseLevel() |
| 6171 | output_format = _OutputFormat() |
| 6172 | filters = '' |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 6173 | quiet = _Quiet() |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 6174 | counting_style = '' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6175 | |
| 6176 | for (opt, val) in opts: |
| 6177 | if opt == '--help': |
| 6178 | PrintUsage(None) |
| 6179 | elif opt == '--output': |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 6180 | if val not in ('emacs', 'vs7', 'eclipse'): |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 6181 | PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6182 | output_format = val |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 6183 | elif opt == '--quiet': |
| 6184 | quiet = True |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6185 | elif opt == '--verbose': |
| 6186 | verbosity = int(val) |
| 6187 | elif opt == '--filter': |
| 6188 | filters = val |
erg@google.com | 6317a9c | 2009-06-25 00:28:19 +0000 | [diff] [blame] | 6189 | if not filters: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6190 | PrintCategories() |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 6191 | elif opt == '--counting': |
| 6192 | if val not in ('total', 'toplevel', 'detailed'): |
| 6193 | PrintUsage('Valid counting options are total, toplevel, and detailed') |
| 6194 | counting_style = val |
mazda@chromium.org | 3fffcec | 2013-06-07 01:04:53 +0000 | [diff] [blame] | 6195 | elif opt == '--root': |
| 6196 | global _root |
| 6197 | _root = val |
raphael.kubo.da.costa@intel.com | 331fbc4 | 2014-05-09 08:48:20 +0000 | [diff] [blame] | 6198 | elif opt == '--linelength': |
| 6199 | global _line_length |
| 6200 | try: |
| 6201 | _line_length = int(val) |
| 6202 | except ValueError: |
| 6203 | PrintUsage('Line length must be digits.') |
| 6204 | elif opt == '--extensions': |
| 6205 | global _valid_extensions |
| 6206 | try: |
| 6207 | _valid_extensions = set(val.split(',')) |
| 6208 | except ValueError: |
qyearsley | 12fa6ff | 2016-08-24 09:18:40 -0700 | [diff] [blame] | 6209 | PrintUsage('Extensions must be comma separated list.') |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 6210 | elif opt == '--headers': |
| 6211 | ProcessHppHeadersOption(val) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6212 | |
| 6213 | if not filenames: |
| 6214 | PrintUsage('No files were specified.') |
| 6215 | |
| 6216 | _SetOutputFormat(output_format) |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 6217 | _SetQuiet(quiet) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6218 | _SetVerboseLevel(verbosity) |
| 6219 | _SetFilters(filters) |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 6220 | _SetCountingStyle(counting_style) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6221 | |
| 6222 | return filenames |
| 6223 | |
| 6224 | |
| 6225 | def main(): |
| 6226 | filenames = ParseArguments(sys.argv[1:]) |
| 6227 | |
| 6228 | # Change stderr to write with replacement characters so we don't die |
| 6229 | # if we try to print something containing non-ASCII characters. |
| 6230 | sys.stderr = codecs.StreamReaderWriter(sys.stderr, |
| 6231 | codecs.getreader('utf8'), |
| 6232 | codecs.getwriter('utf8'), |
| 6233 | 'replace') |
| 6234 | |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 6235 | _cpplint_state.ResetErrorCounts() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6236 | for filename in filenames: |
| 6237 | ProcessFile(filename, _cpplint_state.verbose_level) |
Amin Hassani | f7e1e10 | 2018-06-21 20:13:30 +0000 | [diff] [blame] | 6238 | # If --quiet is passed, suppress printing error count unless there are errors. |
| 6239 | if not _cpplint_state.quiet or _cpplint_state.error_count > 0: |
| 6240 | _cpplint_state.PrintErrorCounts() |
erg@google.com | 26970fa | 2009-11-17 18:07:32 +0000 | [diff] [blame] | 6241 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 6242 | sys.exit(_cpplint_state.error_count > 0) |
| 6243 | |
| 6244 | |
| 6245 | if __name__ == '__main__': |
| 6246 | main() |