Skip to content

Commit 03b187d

Browse files
pkastingLUCI CQ
authored and
LUCI CQ
committed
Avoid IWYU warnings on other namespaces for <algorithm>.
Cherry-pick of google/styleguide#722. Bug: chromium:1368812 Change-Id: I3a604ce2e617b3f145c02519ec3528bff22f691c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4004597 Commit-Queue: Gavin Mak <[email protected]> Auto-Submit: Peter Kasting <[email protected]> Reviewed-by: Gavin Mak <[email protected]>
1 parent 8fbdc51 commit 03b187d

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

cpplint.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -5348,12 +5348,10 @@ def ExpectingFunctionArgs(clean_lines, linenum):
53485348
_re_pattern_headers_maybe_templates = []
53495349
for _header, _templates in _HEADERS_MAYBE_TEMPLATES:
53505350
for _template in _templates:
5351-
# Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max
5352-
# or type::max().
5351+
# Match max<type>(..., ...), max(..., ...), but not foo->max or foo.max.
53535352
_re_pattern_headers_maybe_templates.append(
5354-
(re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
5355-
_template,
5356-
_header))
5353+
(re.compile(r'(?<![>.]\b)' + _template + r'(<.*?>)?\([^\)]'), _template,
5354+
_header))
53575355

53585356
# Other scripts may reach in and modify this pattern.
53595357
_re_pattern_templates = []
@@ -5447,6 +5445,18 @@ def UpdateIncludeState(filename, include_dict, io=codecs):
54475445
return True
54485446

54495447

5448+
def UpdateRequiredHeadersForLine(patterns, line, linenum, required):
5449+
for pattern, template, header in patterns:
5450+
matched = pattern.search(line)
5451+
if matched:
5452+
# Don't warn about IWYU 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[header] = (linenum, template)
5457+
return required
5458+
5459+
54505460
def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
54515461
io=codecs):
54525462
"""Reports for missing stl includes.
@@ -5482,22 +5492,15 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
54825492
if prefix.endswith('std::') or not prefix.endswith('::'):
54835493
required['<string>'] = (linenum, 'string')
54845494

5485-
for pattern, template, header in _re_pattern_headers_maybe_templates:
5486-
if pattern.search(line):
5487-
required[header] = (linenum, template)
5495+
required = UpdateRequiredHeadersForLine(_re_pattern_headers_maybe_templates,
5496+
line, linenum, required)
54885497

54895498
# The following function is just a speed up, no semantics are changed.
54905499
if not '<' in line: # Reduces the cpu time usage by skipping lines.
54915500
continue
54925501

5493-
for pattern, template, header in _re_pattern_templates:
5494-
matched = pattern.search(line)
5495-
if matched:
5496-
# Don't warn about IWYU in non-STL namespaces:
5497-
# (We check only the first match per line; good enough.)
5498-
prefix = line[:matched.start()]
5499-
if prefix.endswith('std::') or not prefix.endswith('::'):
5500-
required[header] = (linenum, template)
5502+
required = UpdateRequiredHeadersForLine(_re_pattern_templates, line,
5503+
linenum, required)
55015504

55025505
# The policy is that if you #include something in foo.h you don't need to
55035506
# include it again in foo.cc. Here, we will look at possible includes.

0 commit comments

Comments
 (0)