This checker can be used to warn about potential stack overflows or be used to estimate the size of used stack space. Both Clang Static Analyzer and Clang-Tidy versions exist.
The Clang-Tidy version references the StackUsageMeasuringVisitor class included in this commit so it can only come after this.
Both versions work by examining function bodies via the AST:
- the static analyzer version can follow and summarize the space used by call chains
- while the Tidy one version can analyze function bodies faster, one by one.
This version emits warnings if the estimated stack size surpasses the StackUsageLimit parameter, which is measured in bytes and defaults to 100 000. Setting it to some small value can be used to turn the checker into a statistical tool since the calculated values are included in the warning messages.
The calculations used for the size estimations do not take into account potential compile-time optimizations and contain some
minor simplifications. Only the information stored in the AST is used and variable lifetime rules are respected. The calculations in a particular function body are carried out by the StackUsageMeasuringVisitor class, which gives a composite result about a piece of the AST containing the maximal estimated space, the space that remain in use after the execution of those lines and flags about encountered variable length arrays or special nodes that satisfy a given predicate (e.g.: templates).
The current version takes no special actions upon encountering variable length arrays, the Tidy version has a simple extra logic
for them. The tests are divided between the two versions, the static analyzer ones are about the ability to calculate the size
of a complete call stack and the Tidy ones focus on particular statements and expression types, this is referenced
in one of the comments of the test files for those who want to use the test cases to understand the checker a little bit more.
The code favors readability and maintainability over performance in some places.
Alphabetical sorting?
(get it? alphabetical? I'm sorry.)