MCPcopy Create free account
hub / github.com/WolfireGames/overgrowth / CheckForIncludeWhatYouUse

Function CheckForIncludeWhatYouUse

Tools/cpplint.py:5423–5519  ·  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

5421
5422
5423def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5424 io=codecs):
5425 """Reports for missing stl includes.
5426
5427 This function will output warnings to make sure you are including the headers
5428 necessary for the stl containers and functions that you use. We only give one
5429 reason to include a header. For example, if you use both equal_to<> and
5430 less<> in a .h file, only one (the latter in the file) of these will be
5431 reported as a reason to include the <functional>.
5432
5433 Args:
5434 filename: The name of the current file.
5435 clean_lines: A CleansedLines instance containing the file.
5436 include_state: An _IncludeState instance.
5437 error: The function to call with any errors found.
5438 io: The IO factory to use to read the header file. Provided for unittest
5439 injection.
5440 """
5441 required = {} # A map of header name to linenumber and the template entity.
5442 # Example of required: { '<functional>': (1219, 'less<>') }
5443
5444 for linenum in xrange(clean_lines.NumLines()):
5445 line = clean_lines.elided[linenum]
5446 if not line or line[0] == '#':
5447 continue
5448
5449 # String is special -- it is a non-templatized type in STL.
5450 matched = _RE_PATTERN_STRING.search(line)
5451 if matched:
5452 # Don't warn about strings in non-STL namespaces:
5453 # (We check only the first match per line; good enough.)
5454 prefix = line[:matched.start()]
5455 if prefix.endswith('std::') or not prefix.endswith('::'):
5456 required['<string>'] = (linenum, 'string')
5457
5458 for pattern, template, header in _re_pattern_headers_maybe_templates:
5459 if pattern.search(line):
5460 required[header] = (linenum, template)
5461
5462 # The following function is just a speed up, no semantics are changed.
5463 if not '<' in line: # Reduces the cpu time usage by skipping lines.
5464 continue
5465
5466 for pattern, template, header in _re_pattern_templates:
5467 matched = pattern.search(line)
5468 if matched:
5469 # Don't warn about IWYU in non-STL namespaces:
5470 # (We check only the first match per line; good enough.)
5471 prefix = line[:matched.start()]
5472 if prefix.endswith('std::') or not prefix.endswith('::'):
5473 required[header] = (linenum, template)
5474
5475 # The policy is that if you #include something in foo.h you don't need to
5476 # include it again in foo.cc. Here, we will look at possible includes.
5477 # Let's flatten the include_state include_list and copy it into a dictionary.
5478 include_dict = dict([item for sublist in include_state.include_list
5479 for item in sublist])
5480

Callers 1

ProcessFileDataFunction · 0.85

Calls 9

FileInfoClass · 0.85
FilesBelongToSameModuleFunction · 0.85
UpdateIncludeStateFunction · 0.85
NumLinesMethod · 0.80
searchMethod · 0.80
FullNameMethod · 0.80
errorFunction · 0.50
startMethod · 0.45
subMethod · 0.45

Tested by

no test coverage detected