Skip to main content

Count directly and indirectly included headers in a C/C++ project

Project description

Headercount – Count which header is included how many times

Headercount is a small debugging script that is meant to help developers who are trying to cut down compile times in a medium-sized C++ project.

Headercount recursively searches a project for #include and prints statistics about which header was included how many times. This indicates which headers are depended on the most. With this information, a developer knows where to start with methods such as the PIMPL idiom, forward declarations, reduction of templatization, etc.

Installation

Headercount is a Python package based on Setuptools. It requires Python 3.4 or higher. It has no dependencies beyond the Python standard library. The easiest way to install it is via pip:

pip install headercount

You can also install Headercount manually by cloning this repository:

git clone https://git.sr.ht/~brokenpen/headercount
cd headercount
pip install .

Working Principle

Headercount works on a best-effort basis and may give incorrect results when faced with strange include pattern. Contributions to improve the algorithm are highly welcome!

For any given input file, Headercount checks whether it is a C/C++ file based on its file name suffix. Non-matching files are ignored. Directories are ignored as well, unless --recursive is passed; if it is, directories are searched recursively for C/C++ files.

Any accepted file is searched for #include <...> and #include "..." directives. Any found directive is counted towards the final tally. Additionally, Headercount attempts to match each include directive to one of its input files, based on the input files' base names. If such a match succeeds, the includes of the matched files are added to the list of includes found in the originally searched file.

This means that a header file is not searched for additional includes if it is not specified on the command line. This helps make the behavior of Headercount predictable.

Finally, the total numbers are tallied for each include across all input files.

The following arguments allow to modify this default behavior:

  • --no-headers: Only source files (as termined by the file name suffix) are used to form the final tally. Header files are only used to determine recursive dependencies. This is useful for many developers, who are only interested in the impact of an include on compilation times.

  • --no-system: The final tally excludes all #include <...>-style directives. This is useful if the developer is only interested in intra-project dependencies.

  • --direct-only: No recursive search is done. The tally only counts literal #include directives.

  • --allow-duplicates: If a file is included multiple times (literally or due to indirect includes), all occurrences are counted.

Contributing

Headercount is only a tiny project made by a tiny guy. Any help is highly encouraged. If you wish to contribute, simply open an issue or send a pull request.

Headercount supports Python 3.4, C11, and C++11 and onwards. If it does not, it is a bug and should be reported. It uses Pep8 and uses PyLint for style checking. Before sending a pull request, please make sure that these tools don't give any warnings.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

headercount-1.0.1.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

headercount-1.0.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file headercount-1.0.1.tar.gz.

File metadata

  • Download URL: headercount-1.0.1.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.10

File hashes

Hashes for headercount-1.0.1.tar.gz
Algorithm Hash digest
SHA256 31d33037b5580b5d9446f485ec06c83199f0296808d8d00fc0c1dd38d1820170
MD5 b989e02fecc7560f21972734496a3408
BLAKE2b-256 c1436fabe3833f39fda8a80ca55e839b20c95728ce2540f24c45fb995c6c20f5

See more details on using hashes here.

File details

Details for the file headercount-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: headercount-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.10

File hashes

Hashes for headercount-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 559f63e87d0f6243c7aa652a3f8c96de2d059905b8bd86c8296a000dd8a45f1e
MD5 72898488e33e828f2231bfff047b1947
BLAKE2b-256 2a27cfff508fd1e2daeffedf05b41f046f66e07736e4365ae2d983853fa128d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page