From 7e0751b7c7d9104830b0d96b30bddb9965f150f6 Mon Sep 17 00:00:00 2001 From: Peter Kasting Date: Thu, 3 Nov 2022 11:40:35 -0700 Subject: [PATCH 1/2] Avoid IWYU warnings on other namespaces for . This is an extension of the logic already used for other STL headers. --- cpplint/cpplint.py | 35 +++++++++++++++++++---------------- cpplint/cpplint_unittest.py | 5 +++++ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/cpplint/cpplint.py b/cpplint/cpplint.py index 704618f..334a127 100755 --- a/cpplint/cpplint.py +++ b/cpplint/cpplint.py @@ -5396,12 +5396,10 @@ _RE_PATTERN_STRING = re.compile(r'\bstring\b') _re_pattern_headers_maybe_templates = [] for _header, _templates in _HEADERS_MAYBE_TEMPLATES: for _template in _templates: - # Match max(..., ...), max(..., ...), but not foo->max, foo.max or - # type::max(). + # Match max(..., ...), max(..., ...), but not foo->max or foo.max. _re_pattern_headers_maybe_templates.append( - (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'), - _template, - _header)) + (re.compile(r'(?.]\b)' + _template + r'(<.*?>)?\([^\)]'), _template, + _header)) # Other scripts may reach in and modify this pattern. _re_pattern_templates = [] @@ -5495,6 +5493,18 @@ def UpdateIncludeState(filename, include_dict, io=codecs): return True +def UpdateRequiredHeadersForLine(patterns, line, linenum, required): + for pattern, template, header in patterns: + matched = pattern.search(line) + if matched: + # Don't warn about IWYU in non-STL namespaces: + # (We check only the first match per line; good enough.) + prefix = line[:matched.start()] + if prefix.endswith('std::') or not prefix.endswith('::'): + required[header] = (linenum, template) + return required + + def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error, io=codecs): """Reports for missing stl includes. @@ -5530,22 +5540,15 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error, if prefix.endswith('std::') or not prefix.endswith('::'): required[''] = (linenum, 'string') - for pattern, template, header in _re_pattern_headers_maybe_templates: - if pattern.search(line): - required[header] = (linenum, template) + required = UpdateRequiredHeadersForLine(_re_pattern_headers_maybe_templates, + line, linenum, required) # The following function is just a speed up, no semantics are changed. if not '<' in line: # Reduces the cpu time usage by skipping lines. continue - for pattern, template, header in _re_pattern_templates: - matched = pattern.search(line) - if matched: - # Don't warn about IWYU in non-STL namespaces: - # (We check only the first match per line; good enough.) - prefix = line[:matched.start()] - if prefix.endswith('std::') or not prefix.endswith('::'): - required[header] = (linenum, template) + required = UpdateRequiredHeadersForLine(_re_pattern_templates, line, + linenum, required) # The policy is that if you #include something in foo.h you don't need to # include it again in foo.cc. Here, we will look at possible includes. diff --git a/cpplint/cpplint_unittest.py b/cpplint/cpplint_unittest.py index 6087d8f..44d243e 100755 --- a/cpplint/cpplint_unittest.py +++ b/cpplint/cpplint_unittest.py @@ -968,6 +968,11 @@ class CpplintTest(CpplintTestBase): base::hash_map foobar; """, '') + self.TestIncludeWhatYouUse( + """#include "base/ranges/algorithm.h" + base::ranges::copy(foo, bar.begin()); + """, + '') self.TestIncludeWhatYouUse( """#include "base/foobar.h" bool foobar = std::less(0,1); From f947b7ee504eda7157883bc96d979c7601d0a49f Mon Sep 17 00:00:00 2001 From: Peter Kasting Date: Mon, 7 Nov 2022 14:14:43 -0800 Subject: [PATCH 2/2] Fix bad regex --- cpplint/cpplint.py | 2 +- cpplint/cpplint_unittest.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cpplint/cpplint.py b/cpplint/cpplint.py index 334a127..81bd9f5 100755 --- a/cpplint/cpplint.py +++ b/cpplint/cpplint.py @@ -5398,7 +5398,7 @@ for _header, _templates in _HEADERS_MAYBE_TEMPLATES: for _template in _templates: # Match max(..., ...), max(..., ...), but not foo->max or foo.max. _re_pattern_headers_maybe_templates.append( - (re.compile(r'(?.]\b)' + _template + r'(<.*?>)?\([^\)]'), _template, + (re.compile(r'(?.])\b' + _template + r'(<.*?>)?\([^\)]'), _template, _header)) # Other scripts may reach in and modify this pattern. diff --git a/cpplint/cpplint_unittest.py b/cpplint/cpplint_unittest.py index 44d243e..8625718 100755 --- a/cpplint/cpplint_unittest.py +++ b/cpplint/cpplint_unittest.py @@ -973,6 +973,11 @@ class CpplintTest(CpplintTestBase): base::ranges::copy(foo, bar.begin()); """, '') + self.TestIncludeWhatYouUse( + """#include "base/foobar.h" + void FooBar::Remove(size_t index); + """, + '') self.TestIncludeWhatYouUse( """#include "base/foobar.h" bool foobar = std::less(0,1);