MCPcopy Create free account
hub / github.com/alibaba/GraphScope / CheckForIncludeWhatYouUse

Function CheckForIncludeWhatYouUse

analytical_engine/misc/cpplint.py:6056–6154  ·  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

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

Callers 1

ProcessFileDataFunction · 0.85

Calls 9

FileInfoClass · 0.85
FilesBelongToSameModuleFunction · 0.85
UpdateIncludeStateFunction · 0.85
GetNonHeaderExtensionsFunction · 0.85
NumLinesMethod · 0.80
FullNameMethod · 0.80
subMethod · 0.80
startMethod · 0.65
keysMethod · 0.45

Tested by

no test coverage detected