Reports for missing stl includes. This function will output warnings to make sure you are including the headers necessary for the stl containers and functions that you use. We only give one reason to include a header. For example, if you use both equal_to<> and less<> in a .h file,
(filename, clean_lines, include_state, error, io=codecs)
| 7106 | |
| 7107 | |
| 7108 | def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error, io=codecs): |
| 7109 | """Reports for missing stl includes. |
| 7110 | |
| 7111 | This function will output warnings to make sure you are including the headers |
| 7112 | necessary for the stl containers and functions that you use. We only give one |
| 7113 | reason to include a header. For example, if you use both equal_to<> and |
| 7114 | less<> in a .h file, only one (the latter in the file) of these will be |
| 7115 | reported as a reason to include the <functional>. |
| 7116 | |
| 7117 | Args: |
| 7118 | filename: The name of the current file. |
| 7119 | clean_lines: A CleansedLines instance containing the file. |
| 7120 | include_state: An _IncludeState instance. |
| 7121 | error: The function to call with any errors found. |
| 7122 | io: The IO factory to use to read the header file. Provided for unittest |
| 7123 | injection. |
| 7124 | """ |
| 7125 | required = {} # A map of header name to linenumber and the template entity. |
| 7126 | # Example of required: { '<functional>': (1219, 'less<>') } |
| 7127 | |
| 7128 | for linenum in range(clean_lines.NumLines()): |
| 7129 | line = clean_lines.elided[linenum] |
| 7130 | if not line or line[0] == "#": |
| 7131 | continue |
| 7132 | |
| 7133 | _re_patterns = [] |
| 7134 | _re_patterns.extend(_re_pattern_types_or_objs) |
| 7135 | _re_patterns.extend(_re_pattern_functions) |
| 7136 | for pattern, item, header in _re_patterns: |
| 7137 | matched = pattern.search(line) |
| 7138 | if matched: |
| 7139 | # Don't warn about strings in non-STL namespaces: |
| 7140 | # (We check only the first match per line; good enough.) |
| 7141 | prefix = line[: matched.start()] |
| 7142 | if prefix.endswith("std::") or not prefix.endswith("::"): |
| 7143 | required[header] = (linenum, item) |
| 7144 | |
| 7145 | for pattern, template, header in _re_pattern_headers_maybe_templates: |
| 7146 | if pattern.search(line): |
| 7147 | required[header] = (linenum, template) |
| 7148 | |
| 7149 | # The following function is just a speed up, no semantics are changed. |
| 7150 | if "<" not in line: # Reduces the cpu time usage by skipping lines. |
| 7151 | continue |
| 7152 | |
| 7153 | for pattern, template, header in _re_pattern_templates: |
| 7154 | matched = pattern.search(line) |
| 7155 | if matched: |
| 7156 | # Don't warn about IWYU in non-STL namespaces: |
| 7157 | # (We check only the first match per line; good enough.) |
| 7158 | prefix = line[: matched.start()] |
| 7159 | if prefix.endswith("std::") or not prefix.endswith("::"): |
| 7160 | required[header] = (linenum, template) |
| 7161 | |
| 7162 | # Let's flatten the include_state include_list and copy it into a dictionary. |
| 7163 | include_dict = dict([item for sublist in include_state.include_list for item in sublist]) |
| 7164 | |
| 7165 | # All the lines have been processed, report the errors found. |
no test coverage detected
searching dependent graphs…