summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Poughon <victor.poughon@cnes.fr>2018-06-14 14:46:01 +0000
committerKim Gräsman <kim.grasman@gmail.com>2018-07-08 15:31:42 +0200
commit67b6ef4918230da2c01be7ec0256dae42007face (patch)
tree0cb09ebbc725c21b73bdcc89f4b418e29ab4f7cb
parent23f592039aff4cbc2bea4c79168b469cff6a579d (diff)
Add --only_re option to fix_includes.py
-rwxr-xr-xfix_includes.py8
-rwxr-xr-xfix_includes_test.py92
2 files changed, 100 insertions, 0 deletions
diff --git a/fix_includes.py b/fix_includes.py
index 37c535f..c93ae59 100755
--- a/fix_includes.py
+++ b/fix_includes.py
@@ -2101,6 +2101,10 @@ def ProcessIWYUOutput(f, files_to_process, flags):
print('(skipping %s: it matches --ignore_re, which is %s)' % (
filename, flags.ignore_re))
continue
+ if flags.only_re and not re.search(flags.only_re, filename):
+ print('(skipping %s: it does not match --only_re, which is %s)' % (
+ filename, flags.only_re))
+ continue
if filename in iwyu_output_records:
iwyu_output_records[filename].Merge(iwyu_record)
@@ -2183,6 +2187,10 @@ def main(argv):
help=('fix_includes.py will skip editing any file whose'
' name matches this regular expression.'))
+ parser.add_option('--only_re', default=None,
+ help='fix_includes.py will skip editing any file whose'
+ ' name does not match this regular exression.')
+
parser.add_option('--separate_project_includes', default=None,
help=('Sort #includes for current project separately'
' from all other #includes. This flag specifies'
diff --git a/fix_includes_test.py b/fix_includes_test.py
index 11e09f5..d460c4a 100755
--- a/fix_includes_test.py
+++ b/fix_includes_test.py
@@ -36,6 +36,7 @@ class FakeFlags(object):
self.comments = True
self.dry_run = False
self.ignore_re = None
+ self.only_re = None
self.safe_headers = False
self.separate_project_includes = None
self.invoking_command_line = 'iwyu.py my_targets'
@@ -2905,6 +2906,97 @@ The full include-list for changed:
self.flags.ignore_re = 'nch'
self.ProcessAndTest(iwyu_output, unedited_files=['unchanged'])
+ def testOnlyRe(self):
+ """Test the behavior of the --only_re flag."""
+ changed_infile = """\
+// Copyright 2010
+
+#include <notused.h> ///-
+///+#include <stdio.h>
+#include "used.h"
+///+#include "used2.h"
+
+int main() { return 0; }
+"""
+ unchanged_infile = """\
+// Copyright 2010
+
+#include <notused.h>
+#include "used.h"
+
+int main() { return 0; }
+"""
+ iwyu_output = """\
+output should add these lines:
+#include <stdio.h>
+#include "used2.h"
+
+output should remove these lines:
+- #include <notused.h> // lines 3-3
+
+The full include-list for output:
+#include <stdio.h>
+#include "used.h"
+#include "used2.h"
+---
+"""
+ # Have the exact same iwyu output for 'alice.cpp' as for 'bob.cpp'.
+ iwyu_output = (iwyu_output.replace('output', 'alice.cpp') +
+ iwyu_output.replace('output', 'bob.cpp'))
+
+ self.RegisterFileContents({'alice.cpp': changed_infile,
+ 'bob.cpp': unchanged_infile})
+ # only alice.cpp should be edited, since it matches only_re.
+ self.flags.only_re = 'lice'
+ self.ProcessAndTest(iwyu_output, unedited_files=['bob.cpp'])
+
+ def testIgnoreAndOnlyRe(self):
+ """Test the behavior of both --ignore_re and --only_re flags."""
+ changed_infile = """\
+// Copyright 2010
+
+#include <notused.h> ///-
+///+#include <stdio.h>
+#include "used.h"
+///+#include "used2.h"
+
+int main() { return 0; }
+"""
+ unchanged_infile = """\
+// Copyright 2010
+
+#include <notused.h>
+#include "used.h"
+
+int main() { return 0; }
+"""
+ iwyu_output = """\
+output should add these lines:
+#include <stdio.h>
+#include "used2.h"
+
+output should remove these lines:
+- #include <notused.h> // lines 3-3
+
+The full include-list for output:
+#include <stdio.h>
+#include "used.h"
+#include "used2.h"
+---
+"""
+ # Have the exact same iwyu output for 'alice.cpp' as for 'bob.cpp' and 'charlie.cpp'
+ iwyu_output = (iwyu_output.replace('output', 'alice.cpp') +
+ iwyu_output.replace('output', 'bob.cpp') +
+ iwyu_output.replace('output', 'charlie.cpp'))
+
+ self.RegisterFileContents({'alice.cpp': changed_infile,
+ 'bob.cpp': unchanged_infile,
+ 'charlie.cpp': changed_infile})
+ # only alice.cpp should be edited, since it matches only_re and not ignore_re
+ self.flags.only_re = 'li'
+ self.flags.ignore_re = 'char'
+ self.ProcessAndTest(iwyu_output, unedited_files=['bob.cpp', 'charlie.cpp'])
+
def testSortIncludes(self):
"""Test sorting includes only -- like running fix_includes.py -s."""
infile = """\