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)
| 4524 | |
| 4525 | |
| 4526 | def CheckIncludeLine(filename, clean_lines, linenum, include_state, error): |
| 4527 | """Check rules that are applicable to #include lines. |
| 4528 | |
| 4529 | Strings on #include lines are NOT removed from elided line, to make |
| 4530 | certain tasks easier. However, to prevent false positives, checks |
| 4531 | applicable to #include lines in CheckLanguage must be put here. |
| 4532 | |
| 4533 | Args: |
| 4534 | filename: The name of the current file. |
| 4535 | clean_lines: A CleansedLines instance containing the file. |
| 4536 | linenum: The number of the line to check. |
| 4537 | include_state: An _IncludeState instance in which the headers are inserted. |
| 4538 | error: The function to call with any errors found. |
| 4539 | """ |
| 4540 | fileinfo = FileInfo(filename) |
| 4541 | line = clean_lines.lines[linenum] |
| 4542 | |
| 4543 | # "include" should use the new style "foo/bar.h" instead of just "bar.h" |
| 4544 | # Only do this check if the included header follows google naming |
| 4545 | # conventions. If not, assume that it's a 3rd party API that |
| 4546 | # requires special include conventions. |
| 4547 | # |
| 4548 | # We also make an exception for Lua headers, which follow google |
| 4549 | # naming convention but not the include convention. |
| 4550 | match = Match(r'#include\s*"([^/]+\.h)"', line) |
| 4551 | if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)): |
| 4552 | error(filename, linenum, 'build/include', 4, |
| 4553 | 'Include the directory when naming .h files') |
| 4554 | |
| 4555 | # we shouldn't include a file more than once. actually, there are a |
| 4556 | # handful of instances where doing so is okay, but in general it's |
| 4557 | # not. |
| 4558 | match = _RE_PATTERN_INCLUDE.search(line) |
| 4559 | if match: |
| 4560 | include = match.group(2) |
| 4561 | is_system = (match.group(1) == '<') |
| 4562 | duplicate_line = include_state.FindHeader(include) |
| 4563 | if duplicate_line >= 0: |
| 4564 | error(filename, linenum, 'build/include', 4, |
| 4565 | '"%s" already included at %s:%s' % |
| 4566 | (include, filename, duplicate_line)) |
| 4567 | elif (include.endswith('.cc') and |
| 4568 | os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)): |
| 4569 | error(filename, linenum, 'build/include', 4, |
| 4570 | 'Do not include .cc files from other packages') |
| 4571 | elif not _THIRD_PARTY_HEADERS_PATTERN.match(include): |
| 4572 | include_state.include_list[-1].append((include, linenum)) |
| 4573 | |
| 4574 | # We want to ensure that headers appear in the right order: |
| 4575 | # 1) for foo.cc, foo.h (preferred location) |
| 4576 | # 2) c system files |
| 4577 | # 3) cpp system files |
| 4578 | # 4) for foo.cc, foo.h (deprecated location) |
| 4579 | # 5) other google headers |
| 4580 | # |
| 4581 | # We classify each include statement as one of those 5 types |
| 4582 | # using a number of techniques. The include_state object keeps |
| 4583 | # track of the highest type seen, and complains if we see a |
no test coverage detected