59
59
# if empty, use defaults
60
60
_valid_extensions = set ([])
61
61
62
- __VERSION__ = '1.5.0 '
62
+ __VERSION__ = '1.5.1 '
63
63
64
64
try :
65
65
xrange # Python 2
77
77
[--recursive]
78
78
[--exclude=path]
79
79
[--extensions=hpp,cpp,...]
80
+ [--includeorder=default|standardcfirst]
80
81
[--quiet]
81
82
[--version]
82
83
<file> [file] ...
209
210
Examples:
210
211
--extensions=%s
211
212
213
+ includeorder=default|standardcfirst
214
+ For the build/include_order rule, the default is to blindly assume angle
215
+ bracket includes with file extension are c-system-headers (default),
216
+ even knowing this will have false classifications.
217
+ The default is established at google.
218
+ standardcfirst means to instead use an allow-list of known c headers and
219
+ treat all others as separate group of "other system headers". The C headers
220
+ included are those of the C-standard lib and closely related ones.
221
+
212
222
headers=x,y,...
213
223
The header extensions that cpplint will treat as .h in checks. Values are
214
224
automatically added to --extensions list.
280
290
'build/include' ,
281
291
'build/include_subdir' ,
282
292
'build/include_alpha' ,
283
- 'build/include_inline' ,
284
293
'build/include_order' ,
285
294
'build/include_what_you_use' ,
286
295
'build/namespaces_literals' ,
295
304
'readability/constructors' ,
296
305
'readability/fn_size' ,
297
306
'readability/inheritance' ,
298
- 'readability/pointer_notation' ,
299
307
'readability/multiline_comment' ,
300
308
'readability/multiline_string' ,
301
309
'readability/namespace' ,
302
310
'readability/nolint' ,
303
311
'readability/nul' ,
304
- 'readability/null_usage' ,
305
312
'readability/strings' ,
306
313
'readability/todo' ,
307
314
'readability/utf8' ,
321
328
'runtime/string' ,
322
329
'runtime/threadsafe_fn' ,
323
330
'runtime/vlog' ,
324
- 'runtime/v8_persistent' ,
325
331
'whitespace/blank_line' ,
326
332
'whitespace/braces' ,
327
333
'whitespace/comma' ,
550
556
'uchar.h' ,
551
557
'wchar.h' ,
552
558
'wctype.h' ,
553
- # POSIX C headers
559
+ # additional POSIX C headers
554
560
'aio.h' ,
555
561
'arpa/inet.h' ,
556
562
'cpio.h' ,
594
600
'utime.h' ,
595
601
'utmpx.h' ,
596
602
'wordexp.h' ,
597
- # GNUlib headers
603
+ # additional GNUlib headers
598
604
'a.out.h' ,
599
605
'aliases.h' ,
600
606
'alloca.h' ,
626
632
'shadow.h' ,
627
633
'sysexits.h' ,
628
634
'ttyent.h' ,
635
+ # Additional linux glibc headers
636
+ 'dlfcn.h' ,
637
+ 'elf.h' ,
638
+ 'features.h' ,
639
+ 'gconv.h' ,
640
+ 'gnu-versions.h' ,
641
+ 'lastlog.h' ,
642
+ 'libio.h' ,
643
+ 'link.h' ,
644
+ 'malloc.h' ,
645
+ 'memory.h' ,
646
+ 'netash/ash.h' ,
647
+ 'netatalk/at.h' ,
648
+ 'netax25/ax25.h' ,
649
+ 'neteconet/ec.h' ,
650
+ 'netipx/ipx.h' ,
651
+ 'netiucv/iucv.h' ,
652
+ 'netpacket/packet.h' ,
653
+ 'netrom/netrom.h' ,
654
+ 'netrose/rose.h' ,
655
+ 'nfs/nfs.h' ,
656
+ 'nl_types.h' ,
657
+ 'nss.h' ,
658
+ 're_comp.h' ,
659
+ 'regexp.h' ,
660
+ 'sched.h' ,
661
+ 'sgtty.h' ,
662
+ 'stab.h' ,
663
+ 'stdc-predef.h' ,
664
+ 'stdio_ext.h' ,
665
+ 'syscall.h' ,
666
+ 'termio.h' ,
667
+ 'thread_db.h' ,
668
+ 'ucontext.h' ,
669
+ 'ustat.h' ,
670
+ 'utmp.h' ,
671
+ 'values.h' ,
672
+ 'wait.h' ,
673
+ 'xlocale.h' ,
629
674
# Hardware specific headers
630
675
'arm_neon.h' ,
631
676
'emmintrin.h' ,
632
677
'xmmintin.h' ,
633
678
])
634
679
680
+ # Folders of C libraries so commonly used in C++,
681
+ # that they have parity with standard C libraries.
682
+ C_STANDARD_HEADER_FOLDERS = frozenset ([
683
+ # standard C library
684
+ "sys" ,
685
+ # glibc for linux
686
+ "arpa" ,
687
+ "asm-generic" ,
688
+ "bits" ,
689
+ "gnu" ,
690
+ "net" ,
691
+ "netinet" ,
692
+ "protocols" ,
693
+ "rpc" ,
694
+ "rpcsvc" ,
695
+ "scsi" ,
696
+ # linux kernel header
697
+ "drm" ,
698
+ "linux" ,
699
+ "misc" ,
700
+ "mtd" ,
701
+ "rdma" ,
702
+ "sound" ,
703
+ "video" ,
704
+ "xen" ,
705
+ ])
706
+
635
707
# Type names
636
708
_TYPES = re .compile (
637
709
r'^(?:'
741
813
# Match string that indicates we're working on a Linux Kernel file.
742
814
_SEARCH_KERNEL_FILE = re .compile (r'\b(?:LINT_KERNEL_FILE)' )
743
815
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
-
752
816
_regexp_compile_cache = {}
753
817
754
818
# {str, set(int)}: a map from error categories to sets of linenumbers
768
832
# Files to exclude from linting. This is set by the --exclude flag.
769
833
_excludes = None
770
834
771
- # Whether to suppress PrintInfo messages
835
+ # Whether to supress PrintInfo messages
772
836
_quiet = False
773
837
774
838
# The allowed line length of files.
775
839
# This is set by --linelength flag.
776
840
_line_length = 80
777
841
842
+ # This allows to use different include order rule than default
843
+ _include_order = "default"
844
+
778
845
try :
779
846
unicode
780
847
except NameError :
@@ -818,6 +885,15 @@ def ProcessHppHeadersOption(val):
818
885
except ValueError :
819
886
PrintUsage ('Header extensions must be comma separated list.' )
820
887
888
+ def ProcessIncludeOrderOption (val ):
889
+ if val is None or val == "default" :
890
+ pass
891
+ elif val == "standardcfirst" :
892
+ global _include_order
893
+ _include_order = val
894
+ else :
895
+ PrintUsage ('Invalid includeorder value %s. Expected default|standardcfirst' )
896
+
821
897
def IsHeaderExtension (file_extension ):
822
898
return file_extension in GetHeaderExtensions ()
823
899
@@ -977,11 +1053,10 @@ class _IncludeState(object):
977
1053
# needs to move backwards, CheckNextIncludeOrder will raise an error.
978
1054
_INITIAL_SECTION = 0
979
1055
_MY_H_SECTION = 1
980
- _OTHER_H_SECTION = 2
981
- _OTHER_SYS_SECTION = 3
982
- _C_SECTION = 4
983
- _CPP_SECTION = 5
984
-
1056
+ _C_SECTION = 2
1057
+ _CPP_SECTION = 3
1058
+ _OTHER_SYS_SECTION = 4
1059
+ _OTHER_H_SECTION = 5
985
1060
986
1061
_TYPE_NAMES = {
987
1062
_C_SYS_HEADER : 'C system header' ,
@@ -2399,21 +2474,6 @@ def CheckForBadCharacters(filename, lines, error):
2399
2474
error (filename , linenum , 'readability/nul' , 5 , 'Line contains NUL byte.' )
2400
2475
2401
2476
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
-
2417
2477
def CheckForNewlineAtEOF (filename , lines , error ):
2418
2478
"""Logs an error if there is no newline char at the end of the file.
2419
2479
@@ -3437,7 +3497,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum,
3437
3497
"""Reports for long function bodies.
3438
3498
3439
3499
For an overview why this is done, see:
3440
- https://google.github.io/styleguide/ cppguide.html #Write_Short_Functions
3500
+ https://google-styleguide.googlecode.com/svn/trunk/ cppguide.xml #Write_Short_Functions
3441
3501
3442
3502
Uses a simplistic algorithm assuming other style guidelines
3443
3503
(especially spacing) are followed.
@@ -4663,71 +4723,6 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
4663
4723
'Use operator %s instead of %s' % (
4664
4724
_ALT_TOKEN_REPLACEMENT [match .group (1 )], match .group (1 )))
4665
4725
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' )
4731
4726
4732
4727
def GetLineWidth (line ):
4733
4728
"""Determines the width of the line in column positions.
@@ -4882,9 +4877,6 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
4882
4877
CheckSpacingForFunctionCall (filename , clean_lines , linenum , error )
4883
4878
CheckCheck (filename , clean_lines , linenum , error )
4884
4879
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 )
4888
4880
classinfo = nesting_state .InnermostClass ()
4889
4881
if classinfo :
4890
4882
CheckSectionSpacing (filename , clean_lines , classinfo , linenum , error )
@@ -4929,13 +4921,14 @@ def _DropCommonSuffixes(filename):
4929
4921
return os .path .splitext (filename )[0 ]
4930
4922
4931
4923
4932
- def _ClassifyInclude (fileinfo , include , is_system ):
4924
+ def _ClassifyInclude (fileinfo , include , used_angle_brackets , include_order = "default" ):
4933
4925
"""Figures out what kind of header 'include' is.
4934
4926
4935
4927
Args:
4936
4928
fileinfo: The current file cpplint is running over. A FileInfo instance.
4937
4929
include: The path to a #included file.
4938
- is_system: True if the #include used <> rather than "".
4930
+ used_angle_brackets: True if the #include used <> rather than "".
4931
+ include_order: "default" or other value allowed in program arguments
4939
4932
4940
4933
Returns:
4941
4934
One of the _XXX_HEADER constants.
@@ -4945,7 +4938,7 @@ def _ClassifyInclude(fileinfo, include, is_system):
4945
4938
_C_SYS_HEADER
4946
4939
>>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
4947
4940
_CPP_SYS_HEADER
4948
- >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', True)
4941
+ >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', True, "standardcfirst" )
4949
4942
_OTHER_SYS_HEADER
4950
4943
>>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
4951
4944
_LIKELY_MY_HEADER
@@ -4957,19 +4950,20 @@ def _ClassifyInclude(fileinfo, include, is_system):
4957
4950
"""
4958
4951
# This is a list of all standard c++ header files, except
4959
4952
# those already checked for above.
4960
- is_cpp_h = include in _CPP_HEADERS
4953
+ is_cpp_header = include in _CPP_HEADERS
4961
4954
4962
- # Mark include as C header if in list or of type 'sys/*.h'.
4963
- is_c_h = include in _C_HEADERS or Search (r'sys\/.*\.h' , include )
4955
+ # Mark include as C header if in list or in a known folder for standard-ish C headers.
4956
+ is_std_c_header = (include_order == "default" ) or (include in _C_HEADERS
4957
+ # additional linux glibc header folders
4958
+ or Search (r'(?:%s)\/.*\.h' % "|" .join (C_STANDARD_HEADER_FOLDERS ), include ))
4964
4959
4965
4960
# Headers with C++ extensions shouldn't be considered C system headers
4966
- if is_system and os .path .splitext (include )[1 ] in ['.hpp' , '.hxx' , '.h++' ]:
4967
- is_system = False
4961
+ is_system = used_angle_brackets and not os .path .splitext (include )[1 ] in ['.hpp' , '.hxx' , '.h++' ]
4968
4962
4969
4963
if is_system :
4970
- if is_cpp_h :
4964
+ if is_cpp_header :
4971
4965
return _CPP_SYS_HEADER
4972
- if is_c_h :
4966
+ if is_std_c_header :
4973
4967
return _C_SYS_HEADER
4974
4968
else :
4975
4969
return _OTHER_SYS_HEADER
@@ -5037,7 +5031,7 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
5037
5031
match = _RE_PATTERN_INCLUDE .search (line )
5038
5032
if match :
5039
5033
include = match .group (2 )
5040
- is_system = (match .group (1 ) == '<' )
5034
+ used_angle_brackets = (match .group (1 ) == '<' )
5041
5035
duplicate_line = include_state .FindHeader (include )
5042
5036
if duplicate_line >= 0 :
5043
5037
error (filename , linenum , 'build/include' , 4 ,
@@ -5068,17 +5062,18 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
5068
5062
include_state .include_list [- 1 ].append ((include , linenum ))
5069
5063
5070
5064
# We want to ensure that headers appear in the right order:
5071
- # 1) for foo.cc, foo.h
5072
- # 2) other project headers
5073
- # 3) c system files
5074
- # 4) cpp system files
5065
+ # 1) for foo.cc, foo.h (preferred location)
5066
+ # 2) c system files
5067
+ # 3) cpp system files
5068
+ # 4) for foo.cc, foo.h (deprecated location)
5069
+ # 5) other google headers
5075
5070
#
5076
5071
# We classify each include statement as one of those 5 types
5077
5072
# using a number of techniques. The include_state object keeps
5078
5073
# track of the highest type seen, and complains if we see a
5079
5074
# lower type after that.
5080
5075
error_message = include_state .CheckNextIncludeOrder (
5081
- _ClassifyInclude (fileinfo , include , is_system ))
5076
+ _ClassifyInclude (fileinfo , include , used_angle_brackets , _include_order ))
5082
5077
if error_message :
5083
5078
error (filename , linenum , 'build/include_order' , 4 ,
5084
5079
'%s. Should be: %s.h, c system, c++ system, other.' %
@@ -5334,7 +5329,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
5334
5329
and line [- 1 ] != '\\ ' ):
5335
5330
error (filename , linenum , 'build/namespaces' , 4 ,
5336
5331
'Do not use unnamed namespaces in header files. See '
5337
- 'https://google.github.io/styleguide/ cppguide.html #Namespaces'
5332
+ 'https://google-styleguide.googlecode.com/svn/trunk/ cppguide.xml #Namespaces'
5338
5333
' for more information.' )
5339
5334
5340
5335
@@ -6456,8 +6451,6 @@ def ProcessFileData(filename, file_extension, lines, error,
6456
6451
6457
6452
CheckForNewlineAtEOF (filename , lines , error )
6458
6453
6459
- CheckInlineHeader (filename , include_state , error )
6460
-
6461
6454
def ProcessConfigOverrides (filename ):
6462
6455
""" Loads the configuration files and processes the config overrides.
6463
6456
@@ -6476,7 +6469,7 @@ def ProcessConfigOverrides(filename):
6476
6469
if not base_name :
6477
6470
break # Reached the root directory.
6478
6471
6479
- cfg_file = os .path .join (abs_path , ".cpplint " )
6472
+ cfg_file = os .path .join (abs_path , "CPPLINT.cfg " )
6480
6473
abs_filename = abs_path
6481
6474
if not os .path .isfile (cfg_file ):
6482
6475
continue
@@ -6527,6 +6520,8 @@ def ProcessConfigOverrides(filename):
6527
6520
_root = os .path .join (os .path .dirname (cfg_file ), val )
6528
6521
elif name == 'headers' :
6529
6522
ProcessHppHeadersOption (val )
6523
+ elif name == 'includeorder' :
6524
+ ProcessIncludeOrderOption (val )
6530
6525
else :
6531
6526
_cpplint_state .PrintError (
6532
6527
'Invalid configuration option (%s) in file %s\n ' %
@@ -6693,6 +6688,7 @@ def ParseArguments(args):
6693
6688
'exclude=' ,
6694
6689
'recursive' ,
6695
6690
'headers=' ,
6691
+ 'includeorder=' ,
6696
6692
'quiet' ])
6697
6693
except getopt .GetoptError :
6698
6694
PrintUsage ('Invalid arguments.' )
@@ -6749,6 +6745,8 @@ def ParseArguments(args):
6749
6745
ProcessHppHeadersOption (val )
6750
6746
elif opt == '--recursive' :
6751
6747
recursive = True
6748
+ elif opt == '--includeorder' :
6749
+ ProcessIncludeOrderOption (val )
6752
6750
6753
6751
if not filenames :
6754
6752
PrintUsage ('No files were specified.' )
0 commit comments