MCPcopy Create free account
hub / github.com/OpenMS/OpenMS / CheckForIncludeWhatYouUse

Function CheckForIncludeWhatYouUse

src/tests/coding/cpplint.py:6053–6151  ·  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

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

Callers 1

ProcessFileDataFunction · 0.85

Calls 8

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

Tested by

no test coverage detected