53
53
import sys
54
54
import unicodedata
55
55
56
+ # if empty, use defaults
57
+ _header_extensions = set ([])
58
+
59
+ # if empty, use defaults
60
+ _valid_extensions = set ([])
61
+
62
+
56
63
# Files with any of these extensions are considered to be
57
64
# header files (and will undergo different style checks).
58
65
# This set can be extended by using the --headers
59
66
# option (also supported in CPPLINT.cfg)
60
- _header_extensions = set (['h' , 'hpp' , 'hxx' , 'h++' , 'cuh' ])
61
- _nonheader_extensions = set (['c' , 'cc' , 'cpp' , 'cxx' , 'c++' , 'cu' ])
62
-
67
+ def GetHeaderExtensions ():
68
+ if not _header_extensions :
69
+ return set (['h' , 'hpp' , 'hxx' , 'h++' , 'cuh' ])
70
+ return _header_extensions
63
71
64
72
# The allowed extensions for file names
65
- # This is set by --extensions flag.
66
- _valid_extensions = _nonheader_extensions .union (_header_extensions )
73
+ # This is set by --extensions flag
74
+ def GetAllExtensions ():
75
+ if not _valid_extensions :
76
+ return GetHeaderExtensions ().union (set (['c' , 'cc' , 'cpp' , 'cxx' , 'c++' , 'cu' ]))
77
+ return _valid_extensions
78
+
79
+ def GetNonHeaderExtensions ():
80
+ return GetAllExtensions ().difference (GetHeaderExtensions ())
81
+
67
82
68
83
# files with this suffix before the extension will be treated as test files
69
84
_test_suffixes = set (['_unittest' , '_test' , '_regtest' ])
72
87
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
73
88
[--counting=total|toplevel|detailed] [--root=subdir]
74
89
[--linelength=digits]
90
+ [--headers=ext1,ext2]
91
+ [--extensions=ext1,ext2]
75
92
<file> [file] ...
76
93
77
94
The style guidelines this tries to follow are those in
148
165
The allowed file extensions that cpplint will check
149
166
150
167
Examples:
151
- --extensions=hpp,cpp
168
+ --extensions=%s
169
+
170
+ headers=extension,extension,...
171
+ The allowed header extensions that cpplint will consider to be header files
172
+ (by default, only files with extensions %s
173
+ will be assumed to be headers)
174
+
175
+ Examples:
176
+ --headers=%s
152
177
153
178
cpplint.py supports per-directory configurations specified in CPPLINT.cfg
154
179
files. CPPLINT.cfg file can contain a number of key=value pairs.
184
209
build/include_alpha as well as excludes all .cc from being
185
210
processed by linter, in the current directory (where the .cfg
186
211
file is located) and all sub-directories.
187
- """ % (list (_valid_extensions ))
212
+ """ % (list (GetAllExtensions ()),
213
+ ',' .join (list (GetAllExtensions ())),
214
+ GetHeaderExtensions (),
215
+ ',' .join (GetHeaderExtensions ()))
188
216
189
217
# We categorize each error message we print. Here are the categories.
190
218
# We want an explicit list so we can list them all in cpplint --filter=.
@@ -536,6 +564,7 @@ def u(x):
536
564
itervalues = dict .values
537
565
iteritems = dict .items
538
566
567
+
539
568
def ParseNolintSuppressions (filename , raw_line , linenum , error ):
540
569
"""Updates the global list of error-suppressions.
541
570
@@ -1094,7 +1123,7 @@ def NoExtension(self):
1094
1123
1095
1124
def IsSource (self ):
1096
1125
"""File has a source file extension."""
1097
- return self .Extension ()[1 :] in _valid_extensions
1126
+ return self .Extension ()[1 :] in GetAllExtensions ()
1098
1127
1099
1128
1100
1129
def _ShouldPrintError (category , confidence , linenum ):
@@ -1816,7 +1845,7 @@ def CheckHeaderFileIncluded(filename, include_state, error):
1816
1845
return
1817
1846
1818
1847
fileinfo = FileInfo (filename )
1819
- for ext in _header_extensions :
1848
+ for ext in GetHeaderExtensions () :
1820
1849
headerfile = filename [:filename .rfind ('.' ) + 1 ] + ext
1821
1850
if not os .path .exists (headerfile ):
1822
1851
continue
@@ -4472,7 +4501,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
4472
4501
4473
4502
# Check if the line is a header guard.
4474
4503
is_header_guard = False
4475
- if file_extension in _header_extensions :
4504
+ if file_extension in GetHeaderExtensions () :
4476
4505
cppvar = GetHeaderGuardCPPVariable (filename )
4477
4506
if (line .startswith ('#ifndef %s' % cppvar ) or
4478
4507
line .startswith ('#define %s' % cppvar ) or
@@ -4567,9 +4596,9 @@ def _DropCommonSuffixes(filename):
4567
4596
"""
4568
4597
for suffix in itertools .chain (
4569
4598
('%s.%s' % (test_suffix .lstrip ('_' ), ext )
4570
- for test_suffix , ext in itertools .product (_test_suffixes , _nonheader_extensions )),
4599
+ for test_suffix , ext in itertools .product (_test_suffixes , GetNonHeaderExtensions () )),
4571
4600
('%s.%s' % (suffix , ext )
4572
- for suffix , ext in itertools .product (['inl' , 'imp' , 'internal' ], _header_extensions ))):
4601
+ for suffix , ext in itertools .product (['inl' , 'imp' , 'internal' ], GetHeaderExtensions () ))):
4573
4602
if (filename .endswith (suffix ) and len (filename ) > len (suffix ) and
4574
4603
filename [- len (suffix ) - 1 ] in ('-' , '_' )):
4575
4604
return filename [:- len (suffix ) - 1 ]
@@ -4585,7 +4614,7 @@ def _IsTestFilename(filename):
4585
4614
Returns:
4586
4615
True if 'filename' looks like a test, False otherwise.
4587
4616
"""
4588
- for test_suffix , ext in itertools .product (_test_suffixes , _nonheader_extensions ):
4617
+ for test_suffix , ext in itertools .product (_test_suffixes , GetNonHeaderExtensions () ):
4589
4618
if filename .endswith (test_suffix + '.' + ext ):
4590
4619
return True
4591
4620
return False
@@ -4694,7 +4723,7 @@ def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
4694
4723
(include , filename , duplicate_line ))
4695
4724
return
4696
4725
4697
- for extension in _nonheader_extensions :
4726
+ for extension in GetNonHeaderExtensions () :
4698
4727
if (include .endswith ('.' + extension ) and
4699
4728
os .path .dirname (fileinfo .RepositoryName ()) != os .path .dirname (include )):
4700
4729
error (filename , linenum , 'build/include' , 4 ,
@@ -4851,7 +4880,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
4851
4880
CheckGlobalStatic (filename , clean_lines , linenum , error )
4852
4881
CheckPrintf (filename , clean_lines , linenum , error )
4853
4882
4854
- if file_extension in _header_extensions :
4883
+ if file_extension in GetHeaderExtensions () :
4855
4884
# TODO(unknown): check that 1-arg constructors are explicit.
4856
4885
# How to tell it's a constructor?
4857
4886
# (handled in CheckForNonStandardConstructs for now)
@@ -4958,7 +4987,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
4958
4987
# Check for use of unnamed namespaces in header files. Registration
4959
4988
# macros are typically OK, so we allow use of "namespace {" on lines
4960
4989
# that end with backslashes.
4961
- if (file_extension in _header_extensions
4990
+ if (file_extension in GetHeaderExtensions ()
4962
4991
and Search (r'\bnamespace\s*{' , line )
4963
4992
and line [- 1 ] != '\\ ' ):
4964
4993
error (filename , linenum , 'build/namespaces' , 4 ,
@@ -5600,11 +5629,11 @@ def FilesBelongToSameModule(filename_cc, filename_h):
5600
5629
string: the additional prefix needed to open the header file.
5601
5630
"""
5602
5631
fileinfo_cc = FileInfo (filename_cc )
5603
- if not fileinfo_cc .Extension ().lstrip ('.' ) in _nonheader_extensions :
5632
+ if not fileinfo_cc .Extension ().lstrip ('.' ) in GetNonHeaderExtensions () :
5604
5633
return (False , '' )
5605
5634
5606
5635
fileinfo_h = FileInfo (filename_h )
5607
- if not fileinfo_h .Extension ().lstrip ('.' ) in _header_extensions :
5636
+ if not fileinfo_h .Extension ().lstrip ('.' ) in GetHeaderExtensions () :
5608
5637
return (False , '' )
5609
5638
5610
5639
filename_cc = filename_cc [:- (len (fileinfo_cc .Extension ()))]
@@ -5739,7 +5768,7 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5739
5768
# TODO(unknown): Do a better job of finding .h files so we are confident that
5740
5769
# not having the .h file means there isn't one.
5741
5770
if not header_found :
5742
- for extension in _nonheader_extensions :
5771
+ for extension in GetNonHeaderExtensions () :
5743
5772
if filename .endswith ('.' + extension ):
5744
5773
return
5745
5774
@@ -6080,7 +6109,7 @@ def ProcessFileData(filename, file_extension, lines, error,
6080
6109
RemoveMultiLineComments (filename , lines , error )
6081
6110
clean_lines = CleansedLines (lines )
6082
6111
6083
- if file_extension in _header_extensions :
6112
+ if file_extension in GetHeaderExtensions () :
6084
6113
CheckForHeaderGuard (filename , clean_lines , error )
6085
6114
6086
6115
for line in range (clean_lines .NumLines ()):
@@ -6093,7 +6122,7 @@ def ProcessFileData(filename, file_extension, lines, error,
6093
6122
CheckForIncludeWhatYouUse (filename , clean_lines , include_state , error )
6094
6123
6095
6124
# Check that the .cc file has included its header if it exists.
6096
- if file_extension in _nonheader_extensions :
6125
+ if file_extension in GetNonHeaderExtensions () :
6097
6126
CheckHeaderFileIncluded (filename , include_state , error )
6098
6127
6099
6128
# We check here rather than inside ProcessLine so that we see raw
@@ -6160,6 +6189,24 @@ def ProcessConfigOverrides(filename):
6160
6189
_line_length = int (val )
6161
6190
except ValueError :
6162
6191
sys .stderr .write ('Line length must be numeric.' )
6192
+ elif name == 'extensions' :
6193
+ global _valid_extensions
6194
+ try :
6195
+ extensions = [ext .strip () for ext in val .split (',' )]
6196
+ _valid_extensions = set (extensions )
6197
+ except ValueError :
6198
+ sys .stderr .write ('Extensions should be a comma-separated list of values;'
6199
+ 'for example: extensions=hpp,cpp\n '
6200
+ 'This could not be parsed: "%s"' % (val ,))
6201
+ elif name == 'headers' :
6202
+ global _header_extensions
6203
+ try :
6204
+ extensions = [ext .strip () for ext in val .split (',' )]
6205
+ _header_extensions = set (extensions )
6206
+ except ValueError :
6207
+ sys .stderr .write ('Extensions should be a comma-separated list of values;'
6208
+ 'for example: extensions=hpp,cpp\n '
6209
+ 'This could not be parsed: "%s"' % (val ,))
6163
6210
else :
6164
6211
sys .stderr .write (
6165
6212
'Invalid configuration option (%s) in file %s\n ' %
@@ -6237,9 +6284,9 @@ def ProcessFile(filename, vlevel, extra_check_functions=[]):
6237
6284
6238
6285
# When reading from stdin, the extension is unknown, so no cpplint tests
6239
6286
# should rely on the extension.
6240
- if filename != '-' and file_extension not in _valid_extensions :
6287
+ if filename != '-' and file_extension not in GetAllExtensions () :
6241
6288
sys .stderr .write ('Ignoring %s; not a valid file name '
6242
- '(%s)\n ' % (filename , ', ' .join (_valid_extensions )))
6289
+ '(%s)\n ' % (filename , ', ' .join (GetAllExtensions () )))
6243
6290
else :
6244
6291
ProcessFileData (filename , file_extension , lines , Error ,
6245
6292
extra_check_functions )
@@ -6306,7 +6353,8 @@ def ParseArguments(args):
6306
6353
'filter=' ,
6307
6354
'root=' ,
6308
6355
'linelength=' ,
6309
- 'extensions=' ])
6356
+ 'extensions=' ,
6357
+ 'headers=' ])
6310
6358
except getopt .GetoptError :
6311
6359
PrintUsage ('Invalid arguments.' )
6312
6360
@@ -6347,6 +6395,12 @@ def ParseArguments(args):
6347
6395
_valid_extensions = set (val .split (',' ))
6348
6396
except ValueError :
6349
6397
PrintUsage ('Extensions must be comma seperated list.' )
6398
+ elif opt == '--headers' :
6399
+ global _header_extensions
6400
+ try :
6401
+ _header_extensions = set (val .split (',' ))
6402
+ except ValueError :
6403
+ PrintUsage ('Extensions must be comma seperated list.' )
6350
6404
6351
6405
if not filenames :
6352
6406
PrintUsage ('No files were specified.' )
0 commit comments