Check rules that are applicable to #include lines. Strings on #include lines are NOT removed from elided line, to make certain tasks easier. However, to prevent false positives, checks applicable to #include lines in CheckLanguage must be put here. Args: filename: The name of the curre
(filename, clean_lines, linenum, include_state, error)
| 5045 | |
| 5046 | |
| 5047 | def CheckIncludeLine(filename, clean_lines, linenum, include_state, error): |
| 5048 | """Check rules that are applicable to #include lines. |
| 5049 | |
| 5050 | Strings on #include lines are NOT removed from elided line, to make |
| 5051 | certain tasks easier. However, to prevent false positives, checks |
| 5052 | applicable to #include lines in CheckLanguage must be put here. |
| 5053 | |
| 5054 | Args: |
| 5055 | filename: The name of the current file. |
| 5056 | clean_lines: A CleansedLines instance containing the file. |
| 5057 | linenum: The number of the line to check. |
| 5058 | include_state: An _IncludeState instance in which the headers are inserted. |
| 5059 | error: The function to call with any errors found. |
| 5060 | """ |
| 5061 | fileinfo = FileInfo(filename) |
| 5062 | line = clean_lines.lines[linenum] |
| 5063 | |
| 5064 | # "include" should use the new style "foo/bar.h" instead of just "bar.h" |
| 5065 | # Only do this check if the included header follows google naming |
| 5066 | # conventions. If not, assume that it's a 3rd party API that |
| 5067 | # requires special include conventions. |
| 5068 | # |
| 5069 | # We also make an exception for Lua headers, which follow google |
| 5070 | # naming convention but not the include convention. |
| 5071 | match = Match(r'#include\s*"([^/]+\.h)"', line) |
| 5072 | if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)): |
| 5073 | error(filename, linenum, 'build/include_subdir', 4, |
| 5074 | 'Include the directory when naming .h files') |
| 5075 | |
| 5076 | # we shouldn't include a file more than once. actually, there are a |
| 5077 | # handful of instances where doing so is okay, but in general it's |
| 5078 | # not. |
| 5079 | match = _RE_PATTERN_INCLUDE.search(line) |
| 5080 | if match: |
| 5081 | include = match.group(2) |
| 5082 | used_angle_brackets = (match.group(1) == '<') |
| 5083 | duplicate_line = include_state.FindHeader(include) |
| 5084 | if duplicate_line >= 0: |
| 5085 | error(filename, linenum, 'build/include', 4, |
| 5086 | '"%s" already included at %s:%s' % |
| 5087 | (include, filename, duplicate_line)) |
| 5088 | return |
| 5089 | |
| 5090 | for extension in GetNonHeaderExtensions(): |
| 5091 | if (include.endswith('.' + extension) and |
| 5092 | os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)): |
| 5093 | error(filename, linenum, 'build/include', 4, |
| 5094 | 'Do not include .' + extension + ' files from other packages') |
| 5095 | return |
| 5096 | |
| 5097 | # We DO want to include a 3rd party looking header if it matches the |
| 5098 | # filename. Otherwise we get an erroneous error "...should include its |
| 5099 | # header" error later. |
| 5100 | third_src_header = False |
| 5101 | for ext in GetHeaderExtensions(): |
| 5102 | basefilename = filename[0:len(filename) - len(fileinfo.Extension())] |
| 5103 | headerfile = basefilename + '.' + ext |
| 5104 | headername = FileInfo(headerfile).RepositoryName() |
no test coverage detected