|
280 | 280 | 'build/include', |
281 | 281 | 'build/include_subdir', |
282 | 282 | 'build/include_alpha', |
| 283 | + 'build/include_inline', |
283 | 284 | 'build/include_order', |
284 | 285 | 'build/include_what_you_use', |
285 | 286 | 'build/namespaces_literals', |
|
294 | 295 | 'readability/constructors', |
295 | 296 | 'readability/fn_size', |
296 | 297 | 'readability/inheritance', |
| 298 | + 'readability/pointer_notation', |
297 | 299 | 'readability/multiline_comment', |
298 | 300 | 'readability/multiline_string', |
299 | 301 | 'readability/namespace', |
300 | 302 | 'readability/nolint', |
301 | 303 | 'readability/nul', |
| 304 | + 'readability/null_usage', |
302 | 305 | 'readability/strings', |
303 | 306 | 'readability/todo', |
304 | 307 | 'readability/utf8', |
|
318 | 321 | 'runtime/string', |
319 | 322 | 'runtime/threadsafe_fn', |
320 | 323 | 'runtime/vlog', |
| 324 | + 'runtime/v8_persistent', |
321 | 325 | 'whitespace/blank_line', |
322 | 326 | 'whitespace/braces', |
323 | 327 | 'whitespace/comma', |
|
737 | 741 | # Match string that indicates we're working on a Linux Kernel file. |
738 | 742 | _SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)') |
739 | 743 |
|
| 744 | +_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b') |
| 745 | + |
| 746 | +_V8_PERSISTENT_PATTERN = re.compile(r'\bv8::Persistent\b') |
| 747 | + |
| 748 | +_RIGHT_LEANING_POINTER_PATTERN = re.compile(r'[^=|(,\s><);&?:}]' |
| 749 | + r'(?<!(sizeof|return))' |
| 750 | + r'\s\*[a-zA-Z_][0-9a-zA-Z_]*') |
| 751 | + |
740 | 752 | _regexp_compile_cache = {} |
741 | 753 |
|
742 | 754 | # {str, set(int)}: a map from error categories to sets of linenumbers |
|
756 | 768 | # Files to exclude from linting. This is set by the --exclude flag. |
757 | 769 | _excludes = None |
758 | 770 |
|
759 | | -# Whether to supress PrintInfo messages |
| 771 | +# Whether to suppress PrintInfo messages |
760 | 772 | _quiet = False |
761 | 773 |
|
762 | 774 | # The allowed line length of files. |
@@ -965,10 +977,11 @@ class _IncludeState(object): |
965 | 977 | # needs to move backwards, CheckNextIncludeOrder will raise an error. |
966 | 978 | _INITIAL_SECTION = 0 |
967 | 979 | _MY_H_SECTION = 1 |
968 | | - _C_SECTION = 2 |
969 | | - _CPP_SECTION = 3 |
970 | | - _OTHER_SYS_SECTION = 4 |
971 | | - _OTHER_H_SECTION = 5 |
| 980 | + _OTHER_H_SECTION = 2 |
| 981 | + _OTHER_SYS_SECTION = 3 |
| 982 | + _C_SECTION = 4 |
| 983 | + _CPP_SECTION = 5 |
| 984 | + |
972 | 985 |
|
973 | 986 | _TYPE_NAMES = { |
974 | 987 | _C_SYS_HEADER: 'C system header', |
@@ -2386,6 +2399,21 @@ def CheckForBadCharacters(filename, lines, error): |
2386 | 2399 | error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.') |
2387 | 2400 |
|
2388 | 2401 |
|
| 2402 | +def CheckInlineHeader(filename, include_state, error): |
| 2403 | + """Logs an error if both a header and its inline variant are included.""" |
| 2404 | + |
| 2405 | + all_headers = dict(item for sublist in include_state.include_list |
| 2406 | + for item in sublist) |
| 2407 | + bad_headers = set('%s.h' % name[:-6] for name in all_headers.keys() |
| 2408 | + if name.endswith('-inl.h')) |
| 2409 | + bad_headers &= set(all_headers.keys()) |
| 2410 | + |
| 2411 | + for name in bad_headers: |
| 2412 | + err = '%s includes both %s and %s-inl.h' % (filename, name, name) |
| 2413 | + linenum = all_headers[name] |
| 2414 | + error(filename, linenum, 'build/include_inline', 5, err) |
| 2415 | + |
| 2416 | + |
2389 | 2417 | def CheckForNewlineAtEOF(filename, lines, error): |
2390 | 2418 | """Logs an error if there is no newline char at the end of the file. |
2391 | 2419 |
|
@@ -3409,7 +3437,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum, |
3409 | 3437 | """Reports for long function bodies. |
3410 | 3438 |
|
3411 | 3439 | For an overview why this is done, see: |
3412 | | - https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions |
| 3440 | + https://google.github.io/styleguide/cppguide.html#Write_Short_Functions |
3413 | 3441 |
|
3414 | 3442 | Uses a simplistic algorithm assuming other style guidelines |
3415 | 3443 | (especially spacing) are followed. |
@@ -4635,6 +4663,71 @@ def CheckAltTokens(filename, clean_lines, linenum, error): |
4635 | 4663 | 'Use operator %s instead of %s' % ( |
4636 | 4664 | _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1))) |
4637 | 4665 |
|
| 4666 | +def CheckNullTokens(filename, clean_lines, linenum, error): |
| 4667 | + """Check NULL usage. |
| 4668 | +
|
| 4669 | + Args: |
| 4670 | + filename: The name of the current file. |
| 4671 | + clean_lines: A CleansedLines instance containing the file. |
| 4672 | + linenum: The number of the line to check. |
| 4673 | + error: The function to call with any errors found. |
| 4674 | + """ |
| 4675 | + line = clean_lines.elided[linenum] |
| 4676 | + |
| 4677 | + # Avoid preprocessor lines |
| 4678 | + if Match(r'^\s*#', line): |
| 4679 | + return |
| 4680 | + |
| 4681 | + if line.find('/*') >= 0 or line.find('*/') >= 0: |
| 4682 | + return |
| 4683 | + |
| 4684 | + for match in _NULL_TOKEN_PATTERN.finditer(line): |
| 4685 | + error(filename, linenum, 'readability/null_usage', 2, |
| 4686 | + 'Use nullptr instead of NULL') |
| 4687 | + |
| 4688 | +def CheckV8PersistentTokens(filename, clean_lines, linenum, error): |
| 4689 | + """Check v8::Persistent usage. |
| 4690 | +
|
| 4691 | + Args: |
| 4692 | + filename: The name of the current file. |
| 4693 | + clean_lines: A CleansedLines instance containing the file. |
| 4694 | + linenum: The number of the line to check. |
| 4695 | + error: The function to call with any errors found. |
| 4696 | + """ |
| 4697 | + line = clean_lines.elided[linenum] |
| 4698 | + |
| 4699 | + # Avoid preprocessor lines |
| 4700 | + if Match(r'^\s*#', line): |
| 4701 | + return |
| 4702 | + |
| 4703 | + if line.find('/*') >= 0 or line.find('*/') >= 0: |
| 4704 | + return |
| 4705 | + |
| 4706 | + for match in _V8_PERSISTENT_PATTERN.finditer(line): |
| 4707 | + error(filename, linenum, 'runtime/v8_persistent', 2, |
| 4708 | + 'Use v8::Global instead of v8::Persistent') |
| 4709 | + |
| 4710 | +def CheckLeftLeaningPointer(filename, clean_lines, linenum, error): |
| 4711 | + """Check for left-leaning pointer placement. |
| 4712 | +
|
| 4713 | + Args: |
| 4714 | + filename: The name of the current file. |
| 4715 | + clean_lines: A CleansedLines instance containing the file. |
| 4716 | + linenum: The number of the line to check. |
| 4717 | + error: The function to call with any errors found. |
| 4718 | + """ |
| 4719 | + line = clean_lines.elided[linenum] |
| 4720 | + |
| 4721 | + # Avoid preprocessor lines |
| 4722 | + if Match(r'^\s*#', line): |
| 4723 | + return |
| 4724 | + |
| 4725 | + if '/*' in line or '*/' in line: |
| 4726 | + return |
| 4727 | + |
| 4728 | + for match in _RIGHT_LEANING_POINTER_PATTERN.finditer(line): |
| 4729 | + error(filename, linenum, 'readability/pointer_notation', 2, |
| 4730 | + 'Use left leaning pointer instead of right leaning') |
4638 | 4731 |
|
4639 | 4732 | def GetLineWidth(line): |
4640 | 4733 | """Determines the width of the line in column positions. |
@@ -4789,6 +4882,9 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, |
4789 | 4882 | CheckSpacingForFunctionCall(filename, clean_lines, linenum, error) |
4790 | 4883 | CheckCheck(filename, clean_lines, linenum, error) |
4791 | 4884 | CheckAltTokens(filename, clean_lines, linenum, error) |
| 4885 | + CheckNullTokens(filename, clean_lines, linenum, error) |
| 4886 | + CheckV8PersistentTokens(filename, clean_lines, linenum, error) |
| 4887 | + CheckLeftLeaningPointer(filename, clean_lines, linenum, error) |
4792 | 4888 | classinfo = nesting_state.InnermostClass() |
4793 | 4889 | if classinfo: |
4794 | 4890 | CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error) |
@@ -4972,11 +5068,10 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error): |
4972 | 5068 | include_state.include_list[-1].append((include, linenum)) |
4973 | 5069 |
|
4974 | 5070 | # We want to ensure that headers appear in the right order: |
4975 | | - # 1) for foo.cc, foo.h (preferred location) |
4976 | | - # 2) c system files |
4977 | | - # 3) cpp system files |
4978 | | - # 4) for foo.cc, foo.h (deprecated location) |
4979 | | - # 5) other google headers |
| 5071 | + # 1) for foo.cc, foo.h |
| 5072 | + # 2) other project headers |
| 5073 | + # 3) c system files |
| 5074 | + # 4) cpp system files |
4980 | 5075 | # |
4981 | 5076 | # We classify each include statement as one of those 5 types |
4982 | 5077 | # using a number of techniques. The include_state object keeps |
@@ -5239,7 +5334,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension, |
5239 | 5334 | and line[-1] != '\\'): |
5240 | 5335 | error(filename, linenum, 'build/namespaces', 4, |
5241 | 5336 | 'Do not use unnamed namespaces in header files. See ' |
5242 | | - 'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces' |
| 5337 | + 'https://google.github.io/styleguide/cppguide.html#Namespaces' |
5243 | 5338 | ' for more information.') |
5244 | 5339 |
|
5245 | 5340 |
|
@@ -6361,6 +6456,8 @@ def ProcessFileData(filename, file_extension, lines, error, |
6361 | 6456 |
|
6362 | 6457 | CheckForNewlineAtEOF(filename, lines, error) |
6363 | 6458 |
|
| 6459 | + CheckInlineHeader(filename, include_state, error) |
| 6460 | + |
6364 | 6461 | def ProcessConfigOverrides(filename): |
6365 | 6462 | """ Loads the configuration files and processes the config overrides. |
6366 | 6463 |
|
@@ -6379,7 +6476,7 @@ def ProcessConfigOverrides(filename): |
6379 | 6476 | if not base_name: |
6380 | 6477 | break # Reached the root directory. |
6381 | 6478 |
|
6382 | | - cfg_file = os.path.join(abs_path, "CPPLINT.cfg") |
| 6479 | + cfg_file = os.path.join(abs_path, ".cpplint") |
6383 | 6480 | abs_filename = abs_path |
6384 | 6481 | if not os.path.isfile(cfg_file): |
6385 | 6482 | continue |
|