bin/find-unneeded-includes: make --headersfwd work without --recursive

Also add text=True for good output - previously, this did not work at all
(it let include/vcl/BitmapTools.hxx checked, see it in subsequent patch)

Previously this check was placed a bit incorrectly, which made
it run only if both --headersfwd and --recursive was provided

Now it runs with simple glob patterns too, such as:
bin/find-unneeded-includes --headersfwd include/vcl/[A-Z]*

Change-Id: Ib8a5209bac553a74137e3a4ac6fdf88319b03982
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/196234
Reviewed-by: Gabor Kelemen <gabor.kelemen@collabora.com>
Tested-by: Jenkins
This commit is contained in:
Gabor Kelemen
2025-12-26 21:31:43 +01:00
parent a01c8b7adc
commit 930ff2b830

View File

@ -562,7 +562,18 @@ def tidy(compileCommands, paths, noexclude, checknamespaces, finderrors, removef
invocation = "include-what-you-use -Xiwyu --no_fwd_decls -Xiwyu --max_line_length=200 " + args
# Suggest headers' replacement with forward declarations
elif headersfwd:
invocation = "include-what-you-use -Xiwyu --cxx17ns -Xiwyu --max_line_length=200 " + args
# Peek inside the file to check for code behind #ifdef or similar
# This is the fragile part, skip forward declaration suggestions for such
p1 = subprocess.Popen(['git', 'grep', '-v', '-e', 'INCLUDED', path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
p2 = subprocess.Popen(['grep', '-e', '#if'], stdin=p1.stdout, stdout=subprocess.PIPE, text=True)
p1.stdout.close()
output, _ = p2.communicate()
if not output:
invocation = "include-what-you-use -Xiwyu --cxx17ns -Xiwyu --max_line_length=200 " + args
else:
# '#if' found, skipping file
continue
# In --fwdecl mode we ask for fw declaration removal suggestions.
# In this mode obsolete fw declarations are suggested for removal.
# Later we ignore the header removal suggestions, which may be
@ -626,7 +637,7 @@ def main(argv):
if args.recursive:
for root, dirs, files in os.walk(args.recursive[0]):
for file in files:
if args.headers:
if args.headers or args.headersfwd:
if not args.fwdecl:
if (file.endswith(".hxx") or file.endswith(".hrc") or file.endswith(".h")):
list_of_files.append(os.path.join(root,file))
@ -635,18 +646,6 @@ def main(argv):
# used in defines and iwyu (0.21 at least) can not yet understand those properly
if (file.endswith(".hxx") or file.endswith(".h")):
list_of_files.append(os.path.join(root,file))
elif args.headersfwd:
if (file.endswith(".hxx") or file.endswith(".hrc") or file.endswith(".h")):
# Peek inside the file to check for code behind #ifdef or similar
# This is the fragile part, skip forward declaration suggestions for such
p1 = subprocess.Popen(['git', 'grep', '-v', '-e', 'INCLUDED', os.path.join(root,file) ], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', '-e', '#if'], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
output, _ = p2.communicate()
if not output:
list_of_files.append(os.path.join(root,file))
else:
continue
else:
if (file.endswith(".cxx") or file.endswith(".c")):
list_of_files.append(os.path.join(root,file))