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)
| 4446 | |
| 4447 | |
| 4448 | def CheckIncludeLine(filename, clean_lines, linenum, include_state, error): |
| 4449 | """Check rules that are applicable to #include lines. |
| 4450 | |
| 4451 | Strings on #include lines are NOT removed from elided line, to make |
| 4452 | certain tasks easier. However, to prevent false positives, checks |
| 4453 | applicable to #include lines in CheckLanguage must be put here. |
| 4454 | |
| 4455 | Args: |
| 4456 | filename: The name of the current file. |
| 4457 | clean_lines: A CleansedLines instance containing the file. |
| 4458 | linenum: The number of the line to check. |
| 4459 | include_state: An _IncludeState instance in which the headers are inserted. |
| 4460 | error: The function to call with any errors found. |
| 4461 | """ |
| 4462 | fileinfo = FileInfo(filename) |
| 4463 | line = clean_lines.lines[linenum] |
| 4464 | |
| 4465 | # "include" should use the new style "foo/bar.h" instead of just "bar.h" |
| 4466 | # Only do this check if the included header follows google naming |
| 4467 | # conventions. If not, assume that it's a 3rd party API that |
| 4468 | # requires special include conventions. |
| 4469 | # |
| 4470 | # We also make an exception for Lua headers, which follow google |
| 4471 | # naming convention but not the include convention. |
| 4472 | match = Match(r'#include\s*"([^/]+\.h)"', line) |
| 4473 | if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)): |
| 4474 | error(filename, linenum, 'build/include', 4, |
| 4475 | 'Include the directory when naming .h files') |
| 4476 | |
| 4477 | # we shouldn't include a file more than once. actually, there are a |
| 4478 | # handful of instances where doing so is okay, but in general it's |
| 4479 | # not. |
| 4480 | match = _RE_PATTERN_INCLUDE.search(line) |
| 4481 | if match: |
| 4482 | include = match.group(2) |
| 4483 | is_system = (match.group(1) == '<') |
| 4484 | duplicate_line = include_state.FindHeader(include) |
| 4485 | if duplicate_line >= 0: |
| 4486 | error(filename, linenum, 'build/include', 4, |
| 4487 | '"%s" already included at %s:%s' % |
| 4488 | (include, filename, duplicate_line)) |
| 4489 | elif (include.endswith('.cc') and |
| 4490 | os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)): |
| 4491 | error(filename, linenum, 'build/include', 4, |
| 4492 | 'Do not include .cc files from other packages') |
| 4493 | elif not _THIRD_PARTY_HEADERS_PATTERN.match(include): |
| 4494 | include_state.include_list[-1].append((include, linenum)) |
| 4495 | |
| 4496 | # We want to ensure that headers appear in the right order: |
| 4497 | # 1) for foo.cc, foo.h (preferred location) |
| 4498 | # 2) c system files |
| 4499 | # 3) cpp system files |
| 4500 | # 4) for foo.cc, foo.h (deprecated location) |
| 4501 | # 5) other google headers |
| 4502 | # |
| 4503 | # We classify each include statement as one of those 5 types |
| 4504 | # using a number of techniques. The include_state object keeps |
| 4505 | # track of the highest type seen, and complains if we see a |
no test coverage detected