Skip to content

[clang] option to leave out exported function bodies by path - #2344

Open
VladimirMakaev wants to merge 1 commit into
meta:mainfrom
VladimirMakaev:pr/skip-bodies-1201
Open

VladimirMakaev wants to merge 1 commit into
meta:mainfrom
VladimirMakaev:pr/skip-bodies-1201

Conversation

@VladimirMakaev

@VladimirMakaev VladimirMakaev commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

What this enables

Infer's clang frontend serializes the AST of a translation unit, including the bodies of every
function defined in the headers it pulls in. When a build includes large generated or vendored
headers whose functions will never be analyzed, those bodies are most of the serialized AST, and
the capture pays for them in time, memory and output size.

SKIP_BODIES_PATH_REGEX lets the caller name those files by path. A function defined in a
matching file is still exported, with its name, signature, attributes and source location; only
its body is left out, so it is exported exactly as if it had been declared and not defined.

The option is for the caller who already knows which headers are irrelevant to the analysis they
are running: a build where generated protocol or serialization headers are included everywhere, a
vendored library analyzed separately, or a bisection that only needs one directory's code. Infer
does not set it, and nothing in the default pipeline changes.

Usage

It is a plugin argument, unset by default:

-Xclang -plugin-arg-BiniouASTExporter -Xclang SKIP_BODIES_PATH_REGEX=generated/.*\.h$

The expression is matched against the normalized path recorded in the AST for the location where
the function is defined.

What changes, and what does not

Changed: the body of a function whose definition is in a file matching the regex.

Not changed:

  • Declarations. The function is still exported, with everything except its body.
  • Bodies physically written in the file being compiled. They are exported whatever the regex says,
    including when a #line directive gives them a different presumed name and when they arrive
    through a macro expanded from a header.
  • Anything, when the option is unset, empty, or not a valid regular expression. An unusable
    expression exports every body, which is the safe direction.
  • Everything other than function bodies: types, fields, comments and the rest of the AST are
    untouched.

Measured effect

One header-heavy translation unit, facebook-clang-plugins/libtooling/ASTExporter.cpp, compiled
against the bundled clang headers with the plugin attached as Infer attaches it, with the regex
naming the clang include directory. Seven runs per arm, the two arms interleaved; same binary,
same input, same flags, the option the only difference. Median [min..max]:

option off option on delta
capture wall time 15.90 s [15.68..16.04] 14.37 s [14.19..14.57] -1.53 s (-9.6%)
peak RSS 778.5 MiB [776.8..779.3] 723.7 MiB [722.8..724.0] -54.9 MiB (-7.0%)
serialized AST 271.5 MB 200.9 MB -70.7 MB (-26.0%)

The two arms do not overlap on any of the three measures. With -fsyntax-only, which drops code
generation and leaves the frontend and the export, the same comparison gives -10.6% wall time,
-7.7% peak RSS and the same -26.0% of AST bytes.

The saving scales with how much of the translation unit is bodies in matching files, so it says
more about the shape of the input than about the option: a unit dominated by generated headers
should do better than this one, and a unit whose code is all in the main file gains nothing.

Setting the option but giving it a regex that matches nothing, or an invalid one, produces
byte-identical output to leaving it unset and costs no measurable time (+0.6% and +1.8% of median
wall time, both inside the run-to-run spread). Enabling the feature is not itself a cost; skipping
bodies is where the win comes from.

Limitations

Skipping a body the analysis needed removes that definition from the analysis, so a caller must
validate the paths they choose; this is why the option is off by default and why Infer does not
set it.

On preprocessed input (.i) every body is physically written in the file being compiled, so the
main-file protection covers all of them and SKIP_BODIES_PATH_REGEX skips nothing there,
including when the regex names the .i file itself.

Test plan

73 direct tests and the annotation target pass without regeneration. The native JSON fixture
passes. Paired base/candidate output is identical with the option unset, empty or invalid; header
and broad filters preserve both main-file bodies.

@meta-cla meta-cla Bot added the CLA Signed label Dec 1, 2026
@dulmarod

dulmarod commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Can the description be more clear about what this enables? Again, do we have any perf/memory win?

Generated headers can dominate the serialized AST when their function bodies
will not be translated. Add an optional SKIP_BODIES_PATH_REGEX filter that
retains declarations while omitting matching header bodies. Empty or invalid
expressions preserve export. Bodies physically written in the main file remain
protected, including #line remapping and header macro expansions.

The option is unset by default, and Infer does not enable it. Explicit activation
can remove needed definitions, so callers must validate their chosen paths.

## Test plan
73 direct tests and the annotation target pass without regeneration. The native
JSON fixture passes. Paired base/candidate output is identical with the option
unset, empty or invalid; header and broad filters preserve both main-file bodies.
@VladimirMakaev

Copy link
Copy Markdown
Contributor Author

Thanks, I've updated the PR description to cover both questions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants