@@ -122,7 +122,7 @@ def GetNonHeaderExtensions():
122
122
likely to be false positives.
123
123
124
124
quiet
125
- Supress output other than linting errors, such as information about
125
+ Suppress output other than linting errors, such as information about
126
126
which files have been processed and excluded.
127
127
128
128
filter=-x,+y,...
@@ -298,11 +298,13 @@ def GetNonHeaderExtensions():
298
298
'readability/constructors' ,
299
299
'readability/fn_size' ,
300
300
'readability/inheritance' ,
301
+ 'readability/pointer_notation' ,
301
302
'readability/multiline_comment' ,
302
303
'readability/multiline_string' ,
303
304
'readability/namespace' ,
304
305
'readability/nolint' ,
305
306
'readability/nul' ,
307
+ 'readability/null_usage' ,
306
308
'readability/strings' ,
307
309
'readability/todo' ,
308
310
'readability/utf8' ,
@@ -353,7 +355,11 @@ def GetNonHeaderExtensions():
353
355
# flag. By default all errors are on, so only add here categories that should be
354
356
# off by default (i.e., categories that must be enabled by the --filter= flags).
355
357
# All entries here should start with a '-' or '+', as in the --filter= flag.
356
- _DEFAULT_FILTERS = ['-build/include_alpha' ]
358
+ _DEFAULT_FILTERS = [
359
+ '-build/include' ,
360
+ '-build/include_subdir' ,
361
+ '-legal/copyright' ,
362
+ ]
357
363
358
364
# The default list of categories suppressed for C (not C++) files.
359
365
_DEFAULT_C_SUPPRESSED_CATEGORIES = [
@@ -626,6 +632,12 @@ def GetNonHeaderExtensions():
626
632
# Match string that indicates we're working on a Linux Kernel file.
627
633
_SEARCH_KERNEL_FILE = re .compile (r'\b(?:LINT_KERNEL_FILE)' )
628
634
635
+ _NULL_TOKEN_PATTERN = re .compile (r'\bNULL\b' )
636
+
637
+ _RIGHT_LEANING_POINTER_PATTERN = re .compile (r'[^=|(,\s><);&?:}]'
638
+ r'(?<!(sizeof|return))'
639
+ r'\s\*[a-zA-z_][0-9a-zA-z_]*' )
640
+
629
641
_regexp_compile_cache = {}
630
642
631
643
# {str, set(int)}: a map from error categories to sets of linenumbers
@@ -644,7 +656,7 @@ def GetNonHeaderExtensions():
644
656
# Files to exclude from linting. This is set by the --exclude flag.
645
657
_excludes = None
646
658
647
- # Whether to supress PrintInfo messages
659
+ # Whether to suppress PrintInfo messages
648
660
_quiet = False
649
661
650
662
# The allowed line length of files.
@@ -1284,54 +1296,12 @@ def RepositoryName(self):
1284
1296
locations won't see bogus errors.
1285
1297
"""
1286
1298
fullname = self .FullName ()
1287
-
1288
- if os .path .exists (fullname ):
1289
- project_dir = os .path .dirname (fullname )
1290
-
1291
- # If the user specified a repository path, it exists, and the file is
1292
- # contained in it, use the specified repository path
1293
- if _repository :
1294
- repo = FileInfo (_repository ).FullName ()
1295
- root_dir = project_dir
1296
- while os .path .exists (root_dir ):
1297
- # allow case insensitive compare on Windows
1298
- if os .path .normcase (root_dir ) == os .path .normcase (repo ):
1299
- return os .path .relpath (fullname , root_dir ).replace ('\\ ' , '/' )
1300
- one_up_dir = os .path .dirname (root_dir )
1301
- if one_up_dir == root_dir :
1302
- break
1303
- root_dir = one_up_dir
1304
-
1305
- if os .path .exists (os .path .join (project_dir , ".svn" )):
1306
- # If there's a .svn file in the current directory, we recursively look
1307
- # up the directory tree for the top of the SVN checkout
1308
- root_dir = project_dir
1309
- one_up_dir = os .path .dirname (root_dir )
1310
- while os .path .exists (os .path .join (one_up_dir , ".svn" )):
1311
- root_dir = os .path .dirname (root_dir )
1312
- one_up_dir = os .path .dirname (one_up_dir )
1313
-
1314
- prefix = os .path .commonprefix ([root_dir , project_dir ])
1315
- return fullname [len (prefix ) + 1 :]
1316
-
1317
- # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
1318
- # searching up from the current path.
1319
- root_dir = current_dir = os .path .dirname (fullname )
1320
- while current_dir != os .path .dirname (current_dir ):
1321
- if (os .path .exists (os .path .join (current_dir , ".git" )) or
1322
- os .path .exists (os .path .join (current_dir , ".hg" )) or
1323
- os .path .exists (os .path .join (current_dir , ".svn" ))):
1324
- root_dir = current_dir
1325
- current_dir = os .path .dirname (current_dir )
1326
-
1327
- if (os .path .exists (os .path .join (root_dir , ".git" )) or
1328
- os .path .exists (os .path .join (root_dir , ".hg" )) or
1329
- os .path .exists (os .path .join (root_dir , ".svn" ))):
1330
- prefix = os .path .commonprefix ([root_dir , project_dir ])
1331
- return fullname [len (prefix ) + 1 :]
1332
-
1333
- # Don't know what to do; header guard warnings may be wrong...
1334
- return fullname
1299
+ # XXX(bnoordhuis) Expects that cpplint.py lives in the tools/ directory.
1300
+ toplevel = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )).replace ('\\ ' , '/' )
1301
+ toplevel = unicode_escape_decode (toplevel )
1302
+ prefix = os .path .commonprefix ([fullname , toplevel ])
1303
+ return fullname [len (prefix ) + 1 :]
1304
+ # End Node.js patch
1335
1305
1336
1306
def Split (self ):
1337
1307
"""Splits the file into the directory, basename, and extension.
@@ -2148,6 +2118,21 @@ def CheckForBadCharacters(filename, lines, error):
2148
2118
error (filename , linenum , 'readability/nul' , 5 , 'Line contains NUL byte.' )
2149
2119
2150
2120
2121
+ def CheckInlineHeader (filename , include_state , error ):
2122
+ """Logs an error if both a header and its inline variant are included."""
2123
+
2124
+ all_headers = dict (item for sublist in include_state .include_list
2125
+ for item in sublist )
2126
+ bad_headers = set ('%s.h' % name [:- 6 ] for name in all_headers .keys ()
2127
+ if name .endswith ('-inl.h' ))
2128
+ bad_headers &= set (all_headers .keys ())
2129
+
2130
+ for name in bad_headers :
2131
+ err = '%s includes both %s and %s-inl.h' % (filename , name , name )
2132
+ linenum = all_headers [name ]
2133
+ error (filename , linenum , 'build/include' , 5 , err )
2134
+
2135
+
2151
2136
def CheckForNewlineAtEOF (filename , lines , error ):
2152
2137
"""Logs an error if there is no newline char at the end of the file.
2153
2138
@@ -4425,6 +4410,49 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
4425
4410
'Use operator %s instead of %s' % (
4426
4411
_ALT_TOKEN_REPLACEMENT [match .group (1 )], match .group (1 )))
4427
4412
4413
+ def CheckNullTokens (filename , clean_lines , linenum , error ):
4414
+ """Check NULL usage.
4415
+
4416
+ Args:
4417
+ filename: The name of the current file.
4418
+ clean_lines: A CleansedLines instance containing the file.
4419
+ linenum: The number of the line to check.
4420
+ error: The function to call with any errors found.
4421
+ """
4422
+ line = clean_lines .elided [linenum ]
4423
+
4424
+ # Avoid preprocessor lines
4425
+ if Match (r'^\s*#' , line ):
4426
+ return
4427
+
4428
+ if line .find ('/*' ) >= 0 or line .find ('*/' ) >= 0 :
4429
+ return
4430
+
4431
+ for match in _NULL_TOKEN_PATTERN .finditer (line ):
4432
+ error (filename , linenum , 'readability/null_usage' , 2 ,
4433
+ 'Use nullptr instead of NULL' )
4434
+
4435
+ def CheckLeftLeaningPointer (filename , clean_lines , linenum , error ):
4436
+ """Check for left-leaning pointer placement.
4437
+
4438
+ Args:
4439
+ filename: The name of the current file.
4440
+ clean_lines: A CleansedLines instance containing the file.
4441
+ linenum: The number of the line to check.
4442
+ error: The function to call with any errors found.
4443
+ """
4444
+ line = clean_lines .elided [linenum ]
4445
+
4446
+ # Avoid preprocessor lines
4447
+ if Match (r'^\s*#' , line ):
4448
+ return
4449
+
4450
+ if '/*' in line or '*/' in line :
4451
+ return
4452
+
4453
+ for match in _RIGHT_LEANING_POINTER_PATTERN .finditer (line ):
4454
+ error (filename , linenum , 'readability/pointer_notation' , 2 ,
4455
+ 'Use left leaning pointer instead of right leaning' )
4428
4456
4429
4457
def GetLineWidth (line ):
4430
4458
"""Determines the width of the line in column positions.
@@ -4477,6 +4505,10 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
4477
4505
error (filename , linenum , 'whitespace/tab' , 1 ,
4478
4506
'Tab found; better to use spaces' )
4479
4507
4508
+ if line .find ('template<' ) != - 1 :
4509
+ error (filename , linenum , 'whitespace/template' , 1 ,
4510
+ 'Leave a single space after template, as in `template <...>`' )
4511
+
4480
4512
# One or three blank spaces at the beginning of the line is weird; it's
4481
4513
# hard to reconcile that with 2-space indents.
4482
4514
# NOTE: here are the conditions rob pike used for his tests. Mine aren't
@@ -4570,6 +4602,8 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
4570
4602
CheckSpacingForFunctionCall (filename , clean_lines , linenum , error )
4571
4603
CheckCheck (filename , clean_lines , linenum , error )
4572
4604
CheckAltTokens (filename , clean_lines , linenum , error )
4605
+ CheckNullTokens (filename , clean_lines , linenum , error )
4606
+ CheckLeftLeaningPointer (filename , clean_lines , linenum , error )
4573
4607
classinfo = nesting_state .InnermostClass ()
4574
4608
if classinfo :
4575
4609
CheckSectionSpacing (filename , clean_lines , classinfo , linenum , error )
@@ -6112,6 +6146,8 @@ def ProcessFileData(filename, file_extension, lines, error,
6112
6146
6113
6147
CheckForNewlineAtEOF (filename , lines , error )
6114
6148
6149
+ CheckInlineHeader (filename , include_state , error )
6150
+
6115
6151
def ProcessConfigOverrides (filename ):
6116
6152
""" Loads the configuration files and processes the config overrides.
6117
6153
0 commit comments