MCPcopy Create free account
hub / github.com/4paradigm/OpenMLDB / CheckForIncludeWhatYouUse

Function CheckForIncludeWhatYouUse

steps/cpplint.py:6066–6164  ·  view source on GitHub ↗

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, only one

(filename, clean_lines, include_state, error,
                              io=codecs)

Source from the content-addressed store, hash-verified

6064
6065
6066def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
6067 io=codecs):
6068 """Reports for missing stl includes.
6069
6070 This function will output warnings to make sure you are including the headers
6071 necessary for the stl containers and functions that you use. We only give one
6072 reason to include a header. For example, if you use both equal_to<> and
6073 less<> in a .h file, only one (the latter in the file) of these will be
6074 reported as a reason to include the <functional>.
6075
6076 Args:
6077 filename: The name of the current file.
6078 clean_lines: A CleansedLines instance containing the file.
6079 include_state: An _IncludeState instance.
6080 error: The function to call with any errors found.
6081 io: The IO factory to use to read the header file. Provided for unittest
6082 injection.
6083 """
6084 required = {} # A map of header name to linenumber and the template entity.
6085 # Example of required: { '<functional>': (1219, 'less<>') }
6086
6087 for linenum in xrange(clean_lines.NumLines()):
6088 line = clean_lines.elided[linenum]
6089 if not line or line[0] == '#':
6090 continue
6091
6092 # String is special -- it is a non-templatized type in STL.
6093 matched = _RE_PATTERN_STRING.search(line)
6094 if matched:
6095 # Don't warn about strings in non-STL namespaces:
6096 # (We check only the first match per line; good enough.)
6097 prefix = line[:matched.start()]
6098 if prefix.endswith('std::') or not prefix.endswith('::'):
6099 required['<string>'] = (linenum, 'string')
6100
6101 for pattern, template, header in _re_pattern_headers_maybe_templates:
6102 if pattern.search(line):
6103 required[header] = (linenum, template)
6104
6105 # The following function is just a speed up, no semantics are changed.
6106 if not '<' in line: # Reduces the cpu time usage by skipping lines.
6107 continue
6108
6109 for pattern, template, header in _re_pattern_templates:
6110 matched = pattern.search(line)
6111 if matched:
6112 # Don't warn about IWYU in non-STL namespaces:
6113 # (We check only the first match per line; good enough.)
6114 prefix = line[:matched.start()]
6115 if prefix.endswith('std::') or not prefix.endswith('::'):
6116 required[header] = (linenum, template)
6117
6118 # The policy is that if you #include something in foo.h you don't need to
6119 # include it again in foo.cc. Here, we will look at possible includes.
6120 # Let's flatten the include_state include_list and copy it into a dictionary.
6121 include_dict = dict([item for sublist in include_state.include_list
6122 for item in sublist])
6123

Callers 1

ProcessFileDataFunction · 0.85

Calls 7

FileInfoClass · 0.85
FilesBelongToSameModuleFunction · 0.85
UpdateIncludeStateFunction · 0.85
GetNonHeaderExtensionsFunction · 0.85
NumLinesMethod · 0.80
FullNameMethod · 0.80
startMethod · 0.45

Tested by

no test coverage detected