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)
| 3682 | |
| 3683 | |
| 3684 | def CheckIncludeLine(filename, clean_lines, linenum, include_state, error): |
| 3685 | """Check rules that are applicable to #include lines. |
| 3686 | |
| 3687 | Strings on #include lines are NOT removed from elided line, to make |
| 3688 | certain tasks easier. However, to prevent false positives, checks |
| 3689 | applicable to #include lines in CheckLanguage must be put here. |
| 3690 | |
| 3691 | Args: |
| 3692 | filename: The name of the current file. |
| 3693 | clean_lines: A CleansedLines instance containing the file. |
| 3694 | linenum: The number of the line to check. |
| 3695 | include_state: An _IncludeState instance in which the headers are inserted. |
| 3696 | error: The function to call with any errors found. |
| 3697 | """ |
| 3698 | fileinfo = FileInfo(filename) |
| 3699 | |
| 3700 | line = clean_lines.lines[linenum] |
| 3701 | |
| 3702 | # "include" should use the new style "foo/bar.h" instead of just "bar.h" |
| 3703 | if _RE_PATTERN_INCLUDE_NEW_STYLE.search(line): |
| 3704 | error(filename, linenum, 'build/include_dir', 4, |
| 3705 | 'Include the directory when naming .h files') |
| 3706 | |
| 3707 | # we shouldn't include a file more than once. actually, there are a |
| 3708 | # handful of instances where doing so is okay, but in general it's |
| 3709 | # not. |
| 3710 | match = _RE_PATTERN_INCLUDE.search(line) |
| 3711 | if match: |
| 3712 | include = match.group(2) |
| 3713 | is_system = (match.group(1) == '<') |
| 3714 | if include in include_state: |
| 3715 | error(filename, linenum, 'build/include', 4, |
| 3716 | '"%s" already included at %s:%s' % |
| 3717 | (include, filename, include_state[include])) |
| 3718 | else: |
| 3719 | include_state[include] = linenum |
| 3720 | |
| 3721 | # We want to ensure that headers appear in the right order: |
| 3722 | # 1) for foo.cc, foo.h (preferred location) |
| 3723 | # 2) c system files |
| 3724 | # 3) cpp system files |
| 3725 | # 4) for foo.cc, foo.h (deprecated location) |
| 3726 | # 5) other google headers |
| 3727 | # |
| 3728 | # We classify each include statement as one of those 5 types |
| 3729 | # using a number of techniques. The include_state object keeps |
| 3730 | # track of the highest type seen, and complains if we see a |
| 3731 | # lower type after that. |
| 3732 | error_message = include_state.CheckNextIncludeOrder( |
| 3733 | _ClassifyInclude(fileinfo, include, is_system)) |
| 3734 | if error_message: |
| 3735 | error(filename, linenum, 'build/include_order', 4, |
| 3736 | '%s. Should be: %s.h, c system, c++ system, other.' % |
| 3737 | (error_message, fileinfo.BaseName())) |
| 3738 | canonical_include = include_state.CanonicalizeAlphabeticalOrder(include) |
| 3739 | if not include_state.IsInAlphabeticalOrder( |
| 3740 | clean_lines, linenum, canonical_include): |
| 3741 | error(filename, linenum, 'build/include_alpha', 4, |
no test coverage detected