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)
| 3016 | |
| 3017 | |
| 3018 | def CheckIncludeLine(filename, clean_lines, linenum, include_state, error): |
| 3019 | """Check rules that are applicable to #include lines. |
| 3020 | |
| 3021 | Strings on #include lines are NOT removed from elided line, to make |
| 3022 | certain tasks easier. However, to prevent false positives, checks |
| 3023 | applicable to #include lines in CheckLanguage must be put here. |
| 3024 | |
| 3025 | Args: |
| 3026 | filename: The name of the current file. |
| 3027 | clean_lines: A CleansedLines instance containing the file. |
| 3028 | linenum: The number of the line to check. |
| 3029 | include_state: An _IncludeState instance in which the headers are inserted. |
| 3030 | error: The function to call with any errors found. |
| 3031 | """ |
| 3032 | fileinfo = FileInfo(filename) |
| 3033 | |
| 3034 | line = clean_lines.lines[linenum] |
| 3035 | |
| 3036 | # "include" should use the new style "foo/bar.h" instead of just "bar.h" |
| 3037 | if _RE_PATTERN_INCLUDE_NEW_STYLE.search(line): |
| 3038 | error(filename, linenum, 'build/include', 4, |
| 3039 | 'Include the directory when naming .h files') |
| 3040 | |
| 3041 | # we shouldn't include a file more than once. actually, there are a |
| 3042 | # handful of instances where doing so is okay, but in general it's |
| 3043 | # not. |
| 3044 | match = _RE_PATTERN_INCLUDE.search(line) |
| 3045 | if match: |
| 3046 | include = match.group(2) |
| 3047 | is_system = (match.group(1) == '<') |
| 3048 | if include in include_state: |
| 3049 | error(filename, linenum, 'build/include', 4, |
| 3050 | '"%s" already included at %s:%s' % |
| 3051 | (include, filename, include_state[include])) |
| 3052 | else: |
| 3053 | include_state[include] = linenum |
| 3054 | |
| 3055 | # We want to ensure that headers appear in the right order: |
| 3056 | # 1) for foo.cc, foo.h (preferred location) |
| 3057 | # 2) c system files |
| 3058 | # 3) cpp system files |
| 3059 | # 4) for foo.cc, foo.h (deprecated location) |
| 3060 | # 5) other google headers |
| 3061 | # |
| 3062 | # We classify each include statement as one of those 5 types |
| 3063 | # using a number of techniques. The include_state object keeps |
| 3064 | # track of the highest type seen, and complains if we see a |
| 3065 | # lower type after that. |
| 3066 | error_message = include_state.CheckNextIncludeOrder( |
| 3067 | _ClassifyInclude(fileinfo, include, is_system)) |
| 3068 | if error_message: |
| 3069 | error(filename, linenum, 'build/include_order', 4, |
| 3070 | '%s. Should be: %s.h, c system, c++ system, other.' % |
| 3071 | (error_message, fileinfo.BaseName())) |
| 3072 | if not include_state.IsInAlphabeticalOrder(include): |
| 3073 | error(filename, linenum, 'build/include_alpha', 4, |
| 3074 | 'Include "%s" not in alphabetical order' % include) |
| 3075 |
no test coverage detected