Skip to content
This repository was archived by the owner on Jul 13, 2019. It is now read-only.

Add extensible set of header file extensions #11

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
[--counting=total|toplevel|detailed] [--root=subdir]
[--linelength=digits]
[--headers=ext1,ext2]
<file> [file] ...

The style guidelines this tries to follow are those in
Expand Down Expand Up @@ -139,6 +140,13 @@
Examples:
--extensions=hpp,cpp

headers=extension,extension,...
The allowed header extensions that cpplint will consider to be header files
(by default, only .h files will be assumed to be headers)

Examples:
--headers=h,hpp

cpplint.py supports per-directory configurations specified in CPPLINT.cfg
files. CPPLINT.cfg file can contain a number of key=value pairs.
Currently the following options are supported:
Expand Down Expand Up @@ -519,6 +527,12 @@ def u(x):
itervalues = dict.values
iteritems = dict.items

# Files with any of these extensions are considered to be
# header files (and will undergo different style checks).
# This set can be extended by using the --headers
# option (also supported in CPPLINT.cfg)
_header_extensions = set(['h'])

def ParseNolintSuppressions(filename, raw_line, linenum, error):
"""Updates the global list of error-suppressions.

Expand Down Expand Up @@ -1684,7 +1698,7 @@ def GetHeaderGuardCPPVariable(filename):
filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
# Replace 'c++' with 'cpp'.
filename = filename.replace('C++', 'cpp').replace('c++', 'cpp')

fileinfo = FileInfo(filename)
file_path_from_root = fileinfo.RepositoryName()
if _root:
Expand Down Expand Up @@ -1798,21 +1812,22 @@ def CheckHeaderFileIncluded(filename, include_state, error):
return

fileinfo = FileInfo(filename)
headerfile = filename[0:len(filename) - 2] + 'h'
if not os.path.exists(headerfile):
return
headername = FileInfo(headerfile).RepositoryName()
first_include = 0
for section_list in include_state.include_list:
for f in section_list:
if headername in f[0] or f[0] in headername:
return
if not first_include:
first_include = f[1]
for ext in _header_extensions:
headerfile = filename[0:len(filename) - 2] + ext
if not os.path.exists(headerfile):
continue
headername = FileInfo(headerfile).RepositoryName()
first_include = None
for section_list in include_state.include_list:
for f in section_list:
if headername in f[0] or f[0] in headername:
return
if not first_include:
first_include = f[1]

error(filename, first_include, 'build/include', 5,
'%s should include its header file %s' % (fileinfo.RepositoryName(),
headername))
error(filename, first_include, 'build/include', 5,
'%s should include its header file %s' % (fileinfo.RepositoryName(),
headername))


def CheckForBadCharacters(filename, lines, error):
Expand Down Expand Up @@ -4453,7 +4468,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,

# Check if the line is a header guard.
is_header_guard = False
if file_extension == 'h':
if file_extension in _header_extensions:
cppvar = GetHeaderGuardCPPVariable(filename)
if (line.startswith('#ifndef %s' % cppvar) or
line.startswith('#define %s' % cppvar) or
Expand Down Expand Up @@ -4819,13 +4834,13 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,

# Make Windows paths like Unix.
fullname = os.path.abspath(filename).replace('\\', '/')

# Perform other checks now that we are sure that this is not an include line
CheckCasts(filename, clean_lines, linenum, error)
CheckGlobalStatic(filename, clean_lines, linenum, error)
CheckPrintf(filename, clean_lines, linenum, error)

if file_extension == 'h':
if file_extension in _header_extensions:
# TODO(unknown): check that 1-arg constructors are explicit.
# How to tell it's a constructor?
# (handled in CheckForNonStandardConstructs for now)
Expand Down Expand Up @@ -4932,7 +4947,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
# Check for use of unnamed namespaces in header files. Registration
# macros are typically OK, so we allow use of "namespace {" on lines
# that end with backslashes.
if (file_extension == 'h'
if (file_extension in _header_extensions
and Search(r'\bnamespace\s*{', line)
and line[-1] != '\\'):
error(filename, linenum, 'build/namespaces', 4,
Expand Down Expand Up @@ -6048,7 +6063,7 @@ def ProcessFileData(filename, file_extension, lines, error,
RemoveMultiLineComments(filename, lines, error)
clean_lines = CleansedLines(lines)

if file_extension == 'h':
if file_extension in _header_extensions:
CheckForHeaderGuard(filename, clean_lines, error)

for line in range(clean_lines.NumLines()):
Expand All @@ -6059,7 +6074,7 @@ def ProcessFileData(filename, file_extension, lines, error,
nesting_state.CheckCompletedBlocks(filename, error)

CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)

# Check that the .cc file has included its header if it exists.
if file_extension == 'cc':
CheckHeaderFileIncluded(filename, include_state, error)
Expand Down Expand Up @@ -6274,7 +6289,8 @@ def ParseArguments(args):
'filter=',
'root=',
'linelength=',
'extensions='])
'extensions=',
'headers='])
except getopt.GetoptError:
PrintUsage('Invalid arguments.')

Expand Down Expand Up @@ -6315,6 +6331,12 @@ def ParseArguments(args):
_valid_extensions = set(val.split(','))
except ValueError:
PrintUsage('Extensions must be comma seperated list.')
elif opt == '--headers':
global _header_extensions
try:
_header_extensions = set(val.split(','))
except ValueError:
PrintUsage('Extensions must be comma seperated list.')

if not filenames:
PrintUsage('No files were specified.')
Expand Down