From abe3a245591f1c7b7a693726219382ff317b2937 Mon Sep 17 00:00:00 2001 From: Marco Massenzio Date: Sat, 23 Apr 2016 14:30:59 +0200 Subject: [PATCH 1/3] Added an extensible set() for header extensions --- cpplint.py | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/cpplint.py b/cpplint.py index 4ca4c88..2f036b4 100644 --- a/cpplint.py +++ b/cpplint.py @@ -519,6 +519,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 --header-extensions +# option (also supported in CPPLINT.cfg) +_header_extensions = set(['h']) + def ParseNolintSuppressions(filename, raw_line, linenum, error): """Updates the global list of error-suppressions. @@ -1684,7 +1690,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: @@ -1798,21 +1804,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: + for ext in _header_extensions: + headerfile = filename[0:len(filename) - 2] + ext + if not os.path.exists(headerfile): return - if not first_include: - first_include = f[1] + 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] - 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): @@ -4453,7 +4460,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 @@ -4819,13 +4826,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) @@ -4932,7 +4939,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, @@ -6048,7 +6055,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()): @@ -6059,7 +6066,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) From d2c9b9436d13ab50ed24aec6541c595a4107732e Mon Sep 17 00:00:00 2001 From: Marco Massenzio Date: Sat, 23 Apr 2016 14:31:55 +0200 Subject: [PATCH 2/3] Changed flag name to --headers as it makes more sense --- cpplint.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/cpplint.py b/cpplint.py index 2f036b4..72d5ad6 100644 --- a/cpplint.py +++ b/cpplint.py @@ -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] ... The style guidelines this tries to follow are those in @@ -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: @@ -521,7 +529,7 @@ def u(x): # 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 --header-extensions +# This set can be extended by using the --headers # option (also supported in CPPLINT.cfg) _header_extensions = set(['h']) @@ -6281,7 +6289,8 @@ def ParseArguments(args): 'filter=', 'root=', 'linelength=', - 'extensions=']) + 'extensions=', + 'headers=']) except getopt.GetoptError: PrintUsage('Invalid arguments.') @@ -6322,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.') From 392a4b7752a6228e07195d0b7eb66f473fa895fc Mon Sep 17 00:00:00 2001 From: Marco Massenzio Date: Sat, 23 Apr 2016 15:03:19 +0200 Subject: [PATCH 3/3] add headers with fixed return --- cpplint.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpplint.py b/cpplint.py index 72d5ad6..53b63d1 100644 --- a/cpplint.py +++ b/cpplint.py @@ -1815,9 +1815,9 @@ def CheckHeaderFileIncluded(filename, include_state, error): for ext in _header_extensions: headerfile = filename[0:len(filename) - 2] + ext if not os.path.exists(headerfile): - return + continue headername = FileInfo(headerfile).RepositoryName() - first_include = 0 + 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: