Skip to main content

Crate cpp_formatter

Crate cpp_formatter 

Source
Expand description

A C++ formatter whose configuration is clang-format’s.

The crate is built in the order the design document measures, and each module says which step of that order it is:

config    `.clang-format` and the seven presets, as a flattened key/value style
tokens    the token stream with its trivia split into "gaps" (the only regions a formatter may rewrite)
roles     what a token *is* — template angle, declarator, block brace — read from the syntax tree
space     the width between two tokens that stay on one line
render    the source with those widths substituted, and nothing else touched

What is not here yet, and is the next step (layout.rs): where the line breaks go and how deep each line sits. Until then [format] keeps the input’s line structure, which makes the tool safe to run on a file and lets the spacing decisions be audited against clang-format on their own (examples/spacing_audit.rs).

Re-exports§

pub use config::ConfigError;
pub use config::Style;

Modules§

breaks
Logical lines: the unit a line breaker works on.
columns
The column model: what a column number means.
comments
The text of a comment, which one rule changes: SpacesInLineCommentPrefix.
config
.clang-format: the YAML subset clang-format itself accepts, and the style it resolves to.
functions
A function definition as a span — what AllowShortFunctionsOnASingleLine needs to decide, and what the reflow will need for every construct.
includes
P3: sorting an include block.
layout
How deep each line sits.
macros
AlignEscapedNewlines: the backslashes of a multi-line macro.
options
Every style option, as a Rust enum whose discriminant is its index in crate::config::Style.
output
-output-replacements-xml: the formatter’s answer as clang-format’s replacement list.
presets
The seven built-in presets, flattened to key / value strings.
regions
// clang-format off … // clang-format on and DisableFormat.
render
The renderer: the source with new whitespace, and not one other byte changed.
roles
What a token is, where the token stream alone cannot say — read from the syntax tree.
rows
AlignArrayOfStructures: Left — the columns of a braced table.
space
Where the spaces go between two tokens that stay on the same line.
tables
A long braced initializer list laid out as a column table — clang-format 23’s answer for a list of literals that does not fit on one line.
tokens
The token stream a formatter needs: every token, and the trivia between them.
trailing
Where a trailing comment starts: AlignTrailingComments with SpacesBeforeTrailingComments.
wrapping
Where a { goes — and else/catch/while after a } — BreakBeforeBraces and its BraceWrapping table.

Functions§

format
Format source with style, keeping the source’s line structure.
format_file
Format source as the file at path, which is what lets the include sorter put the file’s own header first (clang-format’s main-header rule; see includes.rs).
format_named
Format text that belongs to a named file, which is what the main-header promotion needs: clang-format sorts a file’s own header (#include "SpaceAnalyzer.h" in SpaceAnalyzer.cpp) above the rest of its block (IncludeIsMainRegex), and it knows the name from the path. format cannot answer that question and format_file can; a caller that has the name but not a path — the CLI reading stdin with -assume-filename — uses this.
format_range
Format only the lines first..=last (0-based), the library half of clang-format’s -lines.
format_with_config
Format source with the style that text (a .clang-format) describes.
reflow
Format source with clang-format’s line layout — a break before every logical line, then the same indentation and spacing [format] decides, so text that had no line structure (a macro’s expansion) comes out as code.