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