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)
| 4603 | |
| 4604 | |
| 4605 | def CheckIncludeLine(filename, clean_lines, linenum, include_state, error): |
| 4606 | """Check rules that are applicable to #include lines. |
| 4607 | |
| 4608 | Strings on #include lines are NOT removed from elided line, to make |
| 4609 | certain tasks easier. However, to prevent false positives, checks |
| 4610 | applicable to #include lines in CheckLanguage must be put here. |
| 4611 | |
| 4612 | Args: |
| 4613 | filename: The name of the current file. |
| 4614 | clean_lines: A CleansedLines instance containing the file. |
| 4615 | linenum: The number of the line to check. |
| 4616 | include_state: An _IncludeState instance in which the headers are inserted. |
| 4617 | error: The function to call with any errors found. |
| 4618 | """ |
| 4619 | fileinfo = FileInfo(filename) |
| 4620 | line = clean_lines.lines[linenum] |
| 4621 | |
| 4622 | # "include" should use the new style "foo/bar.h" instead of just "bar.h" |
| 4623 | # Only do this check if the included header follows google naming |
| 4624 | # conventions. If not, assume that it's a 3rd party API that |
| 4625 | # requires special include conventions. |
| 4626 | # |
| 4627 | # We also make an exception for Lua headers, which follow google |
| 4628 | # naming convention but not the include convention. |
| 4629 | match = Match(r'#include\s*"([^/]+\.h)"', line) |
| 4630 | if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)): |
| 4631 | error(filename, linenum, 'build/include', 4, |
| 4632 | 'Include the directory when naming .h files') |
| 4633 | |
| 4634 | # we shouldn't include a file more than once. actually, there are a |
| 4635 | # handful of instances where doing so is okay, but in general it's |
| 4636 | # not. |
| 4637 | match = _RE_PATTERN_INCLUDE.search(line) |
| 4638 | if match: |
| 4639 | include = match.group(2) |
| 4640 | is_system = (match.group(1) == '<') |
| 4641 | duplicate_line = include_state.FindHeader(include) |
| 4642 | if duplicate_line >= 0: |
| 4643 | error(filename, linenum, 'build/include', 4, |
| 4644 | '"%s" already included at %s:%s' % |
| 4645 | (include, filename, duplicate_line)) |
| 4646 | elif (include.endswith('.cc') and |
| 4647 | os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)): |
| 4648 | error(filename, linenum, 'build/include', 4, |
| 4649 | 'Do not include .cc files from other packages') |
| 4650 | elif not _THIRD_PARTY_HEADERS_PATTERN.match(include): |
| 4651 | include_state.include_list[-1].append((include, linenum)) |
| 4652 | |
| 4653 | # We want to ensure that headers appear in the right order: |
| 4654 | # 1) for foo.cc, foo.h (preferred location) |
| 4655 | # 2) c system files |
| 4656 | # 3) cpp system files |
| 4657 | # 4) for foo.cc, foo.h (deprecated location) |
| 4658 | # 5) other google headers |
| 4659 | # |
| 4660 | # We classify each include statement as one of those 5 types |
| 4661 | # using a number of techniques. The include_state object keeps |
| 4662 | # track of the highest type seen, and complains if we see a |
no test coverage detected