.clang-format
About this file
Configuration for clang-format, a tool that rewrites C source files into a consistent
layout (indentation, line breaks, brace placement). make fmt runs it on every .c and
.h file in kernel/, user/ and mkfs/ (Makefile:202), and the CI job fails if
that changes anything (.github/workflows/test.yml:9). So every C file you read on this
site is exactly in the format these settings produce.
The file is YAML. It does not name a base style, so clang-format starts from its default,
the LLVM style: indent by 2 spaces, lines at most 80 characters. The lines here change only
what xv6 does differently. The file needs clang-format 20 or later:
BreakAfterReturnType appeared in version 19 (replacing AlwaysBreakAfterReturnType)
and the value Never for ReflowComments in 20; an older version stops with an error
about the configuration.
Leave includes and comments alone
SortIncludes: Never: keep#includelines in the order written. In xv6 order matters: most headers use types such asuintwithout includingkernel/types.hthemselves, so it must come first. Sorting (which reorders each run of consecutive#includelines) would, for example, turnmkfs/mkfs.c:9–12 intofs.h,param.h,stat.h,types.h, puttingtypes.hafter the headers that need it and breaking the build.ReflowComments: Never: do not re-wrap long comments. Many xv6 comments are laid out by hand (diagrams, lists, the disk-layout picture inkernel/fs.h:7).
Where function definitions break
-
BreakAfterReturnType: TopLevelDefinitions: in a function definition, put the return type on its own line, so the function name starts the next line:void * kalloc(void)This is the classic Unix style, and it means a search for
^kallocfinds the definition and nothing else. Declarations (prototypes) are not affected. -
BreakBeforeBraces: CustomwithBraceWrapping: AfterFunction: true: the{that opens a function body goes on its own line. Because the style isCustom, all other brace positions keep the LLVM default:if (...) {,for (...) {andstruct x {keep the brace on the same line.
Macros, continuation lines and strings
AlignConsecutiveMacros: Consecutive: in a run of#definelines with no blank line between them, line up the values in one column, as inkernel/param.handkernel/fs.h.ContinuationIndentWidth: 2: when a statement is too long and wraps, indent the continuation by 2 spaces instead of the LLVM default 4.BreakStringLiterals: false: never split a long string constant into pieces to fit in 80 columns. The longprintfformat onmkfs/mkfs.c:109stays whole, so you can still search the code for a message you saw printed.