3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 # This parses the output of 'include-what-you-use', focusing on just removing
8 # not needed includes and providing a relatively conservative output by
9 # filtering out a number of LibreOffice-specific false positives.
11 # It assumes you have a 'compile_commands.json' around (similar to clang-tidy),
12 # you can generate one with 'make vim-ide-integration'.
15 # - excludelist mechanism, so a warning is either fixed or excluded
16 # - works in a plugins-enabled clang build
17 # - no custom configure options required
18 # - no need to generate a dummy library to build a header
21 import multiprocessing
33 def ignoreRemoval(include, toAdd, absFileName, moduleRules, noexclude):
36 # Avoid replacing .hpp with .hdl in the com::sun::star and ooo::vba namespaces.
37 if ( include.startswith("com/sun/star") or include.startswith("ooo/vba") ) and include.endswith(".hpp"):
38 hdl = include.replace(".hpp", ".hdl")
44 "array": ("debug/array", ),
45 "bitset": ("debug/bitset", ),
46 "deque": ("debug/deque", ),
47 "forward_list": ("debug/forward_list", ),
48 "list": ("debug/list", ),
49 "map": ("debug/map.h", "debug/multimap.h"),
50 "set": ("debug/set.h", "debug/multiset.h"),
51 "unordered_map": ("debug/unordered_map", ),
52 "unordered_set": ("debug/unordered_set", ),
53 "vector": ("debug/vector", ),
55 for k, values in debugStl.items():
61 # Avoid proposing to use libstdc++ internal headers.
63 "exception": "bits/exception.h",
64 "memory": "bits/shared_ptr.h",
65 "functional": "bits/std_function.h",
66 "cmath": "bits/std_abs.h",
67 "ctime": "bits/types/clock_t.h",
68 "cstdint": "bits/stdint-uintn.h",
70 for k, v in bits.items():
71 if include == k and v in toAdd:
74 # Avoid proposing o3tl fw declaration
76 "o3tl/typed_flags_set.hxx" : "namespace o3tl { template <typename T> struct typed_flags; }",
77 "o3tl/deleter.hxx" : "namespace o3tl { template <typename T> struct default_delete; }",
79 for k, v, in o3tl.items():
80 if include == k and v in toAdd:
83 # Follow boost documentation.
84 if include == "boost/optional.hpp" and "boost/optional/optional.hpp" in toAdd:
86 if include == "boost/intrusive_ptr.hpp" and "boost/smart_ptr/intrusive_ptr.hpp" in toAdd:
88 if include == "boost/shared_ptr.hpp" and "boost/smart_ptr/shared_ptr.hpp" in toAdd:
90 if include == "boost/variant.hpp" and "boost/variant/variant.hpp" in toAdd:
92 if include == "boost/unordered_map.hpp" and "boost/unordered/unordered_map.hpp" in toAdd:
94 if include == "boost/functional/hash.hpp" and "boost/container_hash/extensions.hpp" in toAdd:
97 # Avoid .hxx to .h proposals in basic css/uno/* API
99 "com/sun/star/uno/Any.hxx": "com/sun/star/uno/Any.h",
100 "com/sun/star/uno/Reference.hxx": "com/sun/star/uno/Reference.h",
101 "com/sun/star/uno/Sequence.hxx": "com/sun/star/uno/Sequence.h",
102 "com/sun/star/uno/Type.hxx": "com/sun/star/uno/Type.h"
104 for k, v in unoapi.items():
105 if include == k and v in toAdd:
108 # 3rd-party, non-self-contained headers.
109 if include == "libepubgen/libepubgen.h" and "libepubgen/libepubgen-decls.h" in toAdd:
111 if include == "librevenge/librevenge.h" and "librevenge/RVNGPropertyList.h" in toAdd:
113 if include == "libetonyek/libetonyek.h" and "libetonyek/EtonyekDocument.h" in toAdd:
117 # <https://www.openoffice.org/tools/CodingGuidelines.sxw> insists on not
120 # Works around a build breakage specific to the broken Android
122 "android/compatibility.hxx",
123 # Removing this would change the meaning of '#if defined OSL_BIGENDIAN'.
126 if include in noRemove:
129 # Ignore when <foo> is to be replaced with "foo".
133 fileName = os.path.relpath(absFileName, os.getcwd())
135 # Skip headers used only for compile test
136 if fileName == "cppu/qa/cppumaker/test_cppumaker.cxx":
137 if include.endswith(".hpp"):
140 # yaml rules, except when --noexclude is given
142 if "excludelist" in moduleRules.keys() and not noexclude:
143 excludelistRules = moduleRules["excludelist"]
144 if fileName in excludelistRules.keys():
145 if include in excludelistRules[fileName]:
151 def unwrapInclude(include):
152 # Drop <> or "" around the include.
156 def processIWYUOutput(iwyuOutput, moduleRules, fileName, noexclude, checknamespaces):
162 currentFileName = None
164 for line in iwyuOutput:
167 # Bail out if IWYU gave an error due to non self-containedness
168 if re.match ("(.*): error: (.*)", line):
182 shouldAdd = fileName + " should add these lines:"
183 match = re.match(shouldAdd, line)
185 currentFileName = match.group(0).split(' ')[0]
189 shouldRemove = fileName + " should remove these lines:"
190 match = re.match(shouldRemove, line)
192 currentFileName = match.group(0).split(' ')[0]
197 match = re.match("The full include-list for " + fileName, line)
203 match = re.match('#include ([^ ]+)', line)
205 include = unwrapInclude(match.group(1))
206 toAdd.append(include)
208 # Forward declaration.
211 if inRemove and not checknamespaces:
212 match = re.match("- #include (.*) // lines (.*)-.*", line)
214 # Only suggest removals for now. Removing fwd decls is more complex: they may be
215 # indeed unused or they may removed to be replaced with an include. And we want to
217 include = unwrapInclude(match.group(1))
218 lineno = match.group(2)
219 if not ignoreRemoval(include, toAdd, currentFileName, moduleRules, noexclude):
220 toRemove.append("%s:%s: %s" % (currentFileName, lineno, include))
224 # match for all possible URE/UNO namespaces, created with:
225 # find udkapi/com/sun/star/ -type d | sort| xargs basename -a | tr '\012' '|'
226 # find offapi/com/sun/star/ -type d | sort | xargs basename -a | tr '\012' '|'
227 # and ooo::vba namespaces
228 # plus a few popular ones about other modules
233 'bridge|oleautomation|'
255 'configuration|bootstrap|backend|xml|'
257 'datatransfer|clipboard|dnd|'
258 'deployment|test|ui|'
262 'form|binding|runtime|control|inspection|submission|component|validation|'
278 'packages|zip|manifest|'
279 'presentation|textfield|'
283 'report|inspection|meta|'
286 'script|vba|browse|provider|'
287 'sdb|application|tools|'
299 'text|textfield|docinfo|fieldmaster|'
306 'xml|xslt|wrapper|csax|sax|input|xpath|dom|views|events|crypto|sax|'
308 # ooo::vba and its namespaces
309 'ooo|vba|excel|powerpoint|adodb|access|office|word|stdole|msforms|dao|'
310 # use of module namespaces, as spotted in the code
311 'analysis|pricing' # sca internals
312 'apphelper|CloneHelper|DataSeriesProperties|SceneProperties|wrapper|' # for chart internals
314 'boost|posix_time|gregorian'
320 'cpp|java|' # for codemaker::
322 'dbaccess|dbahsql|dbaui|dbtools|'
324 'drawinglayer|attribute|geometry|primitive2d|processor2d|'
330 'http_dav_ucp|tdoc_ucp|package_ucp|hierarchy_ucp|gio|fileaccess|ucb_impl|hcp_impl|ucb_cmdenv|' # for ucb internal
332 'internal|ColorComponentTag|' # for slideshow internals
338 'mtv|' # for mdds::mtv
339 'nsSwDocInfoSubType|SWUnoHelper|nsHdFtFlags|' # sw internal
341 'odfflatxml|' # filter internal
342 'oox|core|drawingml|ole|vml|'
353 'sax|' # for xml::sax
355 'SchXMLTools|' # for xmloff
356 'sd|slidesorter|cache|controller|model|view|'
359 'sidebar|' # for sfx2::sidebar
361 'star|' # for com::sun::star
362 'std|chrono_literals|literals|'
368 'svx|sdr|contact|table|'
369 'sw|access|annotation|mark|types|util|'
375 'util|db|qe|' # for xmlsearch::
380 'xmloff|token|EnhancedCustomShapeToken' # for xmloff::
385 reason = re.match(ns, line)
387 # Warn about namespaces: if a header is suggested only '// for $namespace', then the namespace is not used
388 # otherwise the used classes name would show up after the '// for'
389 # Cleaning out the respective header (if there is any
390 # - which is not always the case) is for the next run!
391 nameSpace = reason.group(1).split(' ')[0]
392 print("WARNING:", fileName, "This 'using namespace' is likely unnecessary:", nameSpace)
394 # Get the row number, normal IWYU output does not contain this info
395 subprocess.run(["git", "grep", "-n", "namespace.*[^a-zA-Z]"+nameSpace+" *;", fileName])
397 for remove in sorted(toRemove):
398 print("ERROR: %s: remove not needed include" % remove)
402 def run_tool(task_queue, failed_files, dontstop, noexclude, checknamespaces):
404 invocation, moduleRules = task_queue.get()
405 if not len(failed_files):
406 print("[IWYU] " + invocation.split(' ')[-1])
407 p = subprocess.Popen(invocation, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
408 retcode = processIWYUOutput(p.communicate()[0].decode('utf-8').splitlines(), moduleRules, invocation.split(' ')[-1], noexclude, checknamespaces)
409 if retcode == -1 and not checknamespaces:
410 print("ERROR: A file is probably not self contained, check this commands output:\n" + invocation)
412 print("ERROR: The following command found unused includes:\n" + invocation)
414 failed_files.append(invocation)
415 task_queue.task_done()
417 # Workaround: sometimes running git grep makes the letters typed into the terminal disappear after the script is finished
418 os.system('stty sane')
421 def isInUnoIncludeFile(path):
422 return path.startswith("include/com/") \
423 or path.startswith("include/cppu/") \
424 or path.startswith("include/cppuhelper/") \
425 or path.startswith("include/osl/") \
426 or path.startswith("include/rtl/") \
427 or path.startswith("include/sal/") \
428 or path.startswith("include/salhelper/") \
429 or path.startswith("include/systools/") \
430 or path.startswith("include/typelib/") \
431 or path.startswith("include/uno/")
434 def tidy(compileCommands, paths, dontstop, noexclude,checknamespaces):
438 max_task = multiprocessing.cpu_count()
439 task_queue = queue.Queue(max_task)
441 for _ in range(max_task):
442 t = threading.Thread(target=run_tool, args=(task_queue, failed_files, dontstop, noexclude,checknamespaces))
446 for path in sorted(paths):
447 if isInUnoIncludeFile(path):
450 # IWYU fails on these with #error: don't use this in new code
451 if path.startswith("include/vcl/toolkit"):
454 moduleName = path.split("/")[0]
456 rulePath = os.path.join(moduleName, "IwyuFilter_" + moduleName + ".yaml")
458 if os.path.exists(rulePath):
459 moduleRules = yaml.full_load(open(rulePath))
461 pathAbs = os.path.abspath(path)
462 compileFile = pathAbs
463 matches = [i for i in compileCommands if i["file"] == compileFile]
465 # Only use assume-filename for headers, so we don't try to analyze e.g. Windows-only
467 if "assumeFilename" in moduleRules.keys() and not path.endswith("cxx"):
468 assume = moduleRules["assumeFilename"]
470 assumeAbs = os.path.abspath(assume)
471 compileFile = assumeAbs
472 matches = [i for i in compileCommands if i["file"] == compileFile]
474 print("WARNING: no compile commands for '" + path + "' (assumed filename: '" + assume + "'")
477 print("WARNING: no compile commands for '" + path + "'")
480 _, _, args = matches[0]["command"].partition(" ")
482 args = args.replace(assumeAbs, "-x c++ " + pathAbs)
484 invocation = "include-what-you-use -Xiwyu --no_fwd_decls -Xiwyu --max_line_length=200 " + args
485 task_queue.put((invocation, moduleRules))
488 if len(failed_files):
491 except KeyboardInterrupt:
492 print('\nCtrl-C detected, goodbye.')
495 sys.exit(return_code)
499 parser = argparse.ArgumentParser(description='Check source files for unneeded includes.')
500 parser.add_argument('--continue', action='store_true',
501 help='Don\'t stop on errors. Useful for periodic re-check of large amount of files')
502 parser.add_argument('Files' , nargs='*',
503 help='The files to be checked')
504 parser.add_argument('--recursive', metavar='DIR', nargs=1, type=str,
505 help='Recursively search a directory for source files to check')
506 parser.add_argument('--headers', action='store_true',
507 help='Check header files. If omitted, check source files. Use with --recursive.')
508 parser.add_argument('--noexclude', action='store_true',
509 help='Ignore excludelist. Useful to check whether its exclusions are still all valid.')
510 parser.add_argument('--ns', action='store_true',
511 help='Warn about unused "using namespace" statements. '
512 'Removing these may uncover more removable headers '
513 'in a subsequent normal run')
515 args = parser.parse_args()
523 for root, dirs, files in os.walk(args.recursive[0]):
526 if (file.endswith(".hxx") or file.endswith(".hrc") or file.endswith(".h")):
527 list_of_files.append(os.path.join(root,file))
529 if (file.endswith(".cxx") or file.endswith(".c")):
530 list_of_files.append(os.path.join(root,file))
532 list_of_files = args.Files
535 with open("compile_commands.json", 'r') as compileCommandsSock:
536 compileCommands = json.load(compileCommandsSock)
537 except FileNotFoundError:
538 print ("File 'compile_commands.json' does not exist, please run:\nmake vim-ide-integration")
541 # quickly sanity check whether files with exceptions in yaml still exists
542 # only check for the module of the very first filename passed
544 # Verify there are files selected for checking, with --recursive it
545 # may happen that there are in fact no C/C++ files in a module directory
546 if not list_of_files:
547 print("No files found to check!")
550 moduleName = sorted(list_of_files)[0].split("/")[0]
551 rulePath = os.path.join(moduleName, "IwyuFilter_" + moduleName + ".yaml")
553 if os.path.exists(rulePath):
554 moduleRules = yaml.full_load(open(rulePath))
555 if "excludelist" in moduleRules.keys():
556 excludelistRules = moduleRules["excludelist"]
557 for pathname in excludelistRules.keys():
558 file = pathlib.Path(pathname)
559 if not file.exists():
560 print("WARNING: File listed in " + rulePath + " no longer exists: " + pathname)
562 tidy(compileCommands, paths=list_of_files, dontstop=vars(args)["continue"], noexclude=args.noexclude, checknamespaces=args.ns)
564 if __name__ == '__main__':
567 # vim:set shiftwidth=4 softtabstop=4 expandtab: