xref: /freebsd/share/man/man9/style.9 (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1.\"
2.\" Copyright (c) 1995-2025 The FreeBSD Project
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.Dd March 27, 2025
26.Dt STYLE 9
27.Os
28.Sh NAME
29.Nm style
30.Nd kernel source file style guide
31.Sh DESCRIPTION
32This file specifies the preferred style for kernel source files in the
33.Fx
34source tree.
35It is also a guide for the preferred userland code style.
36The preferred line width is 80 characters, but some exceptions are
37made when a slightly longer line is clearer or easier to read.
38Anything that is frequently grepped for, such as diagnostic, error, or panic
39messages, should not be broken up over multiple lines despite this rule.
40Many of the style rules are implicit in the examples.
41Be careful to check the examples before assuming that
42.Nm
43is silent on an issue.
44.Bd -literal
45/*
46 * Style guide for FreeBSD.  Based on the CSRG's KNF (Kernel Normal Form).
47 */
48
49/*
50 * VERY important single-line comments look like this.
51 */
52
53/* Most single-line comments look like this. */
54
55// Although they may look like this.
56
57/*
58 * Multi-line comments look like this.  Make them real sentences.  Fill
59 * them so they look like real paragraphs.
60 */
61.Ed
62.Pp
63C++ comments may be used in C and C++ code.
64Single-line comments should be consistently either C or C++ within a file.
65Multi-line comments should also be consistently either C or C++, but may differ
66from single-line comments.
67.Pp
68The copyright header should be a multi-line comment like so:
69.Bd -literal
70/*
71 * Copyright (c) 1984-2025 John Q. Public
72 *
73 * SPDX-License-Identifier: BSD-2-Clause
74 */
75.Ed
76.Pp
77Comments starting in columns other than the first are never
78considered license statements.
79Write the copyright lines before the appropriate SPDX-License-Identifier.
80If the copyright assertion contains the phrase
81.Dq Li "All Rights Reserved"
82that should be on the same line as the word
83.Dq Li "Copyright" .
84You should not insert a new copyright line between an old
85copyright line and this phrase.
86Instead, you should insert a new copyright phrase after
87a pre-existing
88.Dq Li "All Rights Reserved"
89line.
90When making changes, it is acceptable to fold an
91.Dq Li "All Rights Reserved"
92line with each of the
93.Dq Li "Copyright"
94lines.
95For files that have the
96.Dq Li "All Rights Reserved"
97line on the same line(s) as the word
98.Dq Li "Copyright" ,
99new copyright assertions should be added last.
100New
101.Dq Li "Copyright"
102lines should only be added when making substantial changes to the file,
103not for trivial changes.
104.Pp
105After any copyright and license comment, there is a blank line.
106Non-C/C++ source files follow the example above, while C/C++ source files
107follow the one below.
108Version control system ID tags should only exist once in a file
109(unlike in this one).
110All VCS (version control system) revision identification in files obtained
111from elsewhere should be maintained, including, where applicable, multiple IDs
112showing a file's history.
113In general, do not edit foreign IDs or their infrastructure.
114Unless otherwise wrapped (such as
115.Dq Li "#if defined(LIBC_SCCS)" ) ,
116enclose both in
117.Dq Li "#if 0 ... #endif"
118to hide any uncompilable bits
119and to keep the IDs out of object files.
120Only add
121.Dq Li "From: "
122in front of foreign VCS IDs if the file is renamed.
123Add
124.Dq Li "From: "
125and the
126.Fx
127git hash with full path name if the file was derived from another
128.Fx
129file and include relevant copyright info from the original file.
130.Pp
131Leave one blank line before the header files.
132.Pp
133Kernel include files
134.Pa ( sys/*.h )
135come first.
136If either
137.In sys/types.h
138or
139.In sys/param.h
140is needed, include it before other include files.
141.Po
142.In sys/param.h
143includes
144.In sys/types.h ;
145do not include both.
146.Pc
147Next, include
148.In sys/systm.h ,
149if needed.
150The remaining kernel headers should be sorted alphabetically.
151.Bd -literal
152#include <sys/types.h>	/* Non-local includes in angle brackets. */
153#include <sys/systm.h>
154#include <sys/endian.h>
155#include <sys/lock.h>
156#include <sys/queue.h>
157.Ed
158.Pp
159For a network program, put the network include files next.
160.Bd -literal
161#include <net/if.h>
162#include <net/if_dl.h>
163#include <net/route.h>
164#include <netinet/in.h>
165#include <protocols/rwhod.h>
166.Ed
167.Pp
168Do not include files from
169.Pa /usr/include
170in the kernel.
171.Pp
172Leave a blank line before the next group, the
173.Pa /usr/include
174files,
175which should be sorted alphabetically by name.
176.Bd -literal
177#include <stdio.h>
178.Ed
179.Pp
180Global pathnames are defined in
181.In paths.h .
182Pathnames local
183to the program go in
184.Qq Pa pathnames.h
185in the local directory.
186.Bd -literal
187#include <paths.h>
188.Ed
189.Pp
190Leave another blank line before the local include files.
191.Bd -literal
192#include "pathnames.h"		/* Local includes in double quotes. */
193.Ed
194.Pp
195Do not
196.Ic #define
197or declare names in the implementation namespace except
198for implementing application interfaces.
199.Pp
200The names of
201.Dq unsafe
202macros (ones that have side effects), and the names of macros for
203manifest constants, are all in uppercase.
204The expansions of expression-like macros are either a single token
205or have outer parentheses.
206Put a single space or tab character between the
207.Ic #define
208and the macro name, but be consistent within a file.
209If a macro is an inline expansion of a function, the function name is
210all in lowercase and the macro has the same name all in uppercase.
211.\" XXX the above conflicts with ANSI style where the names are the
212.\" same and you #undef the macro (if any) to get the function.
213.\" It is not followed for MALLOC(), and not very common if inline
214.\" functions are used.
215Right-justify the
216backslashes; it makes it easier to read.
217If the macro encapsulates a compound statement, enclose it in a
218.Ic do
219loop,
220so that it can safely be used in
221.Ic if
222statements.
223Any final statement-terminating semicolon should be
224supplied by the macro invocation rather than the macro, to make parsing easier
225for pretty-printers and editors.
226.Bd -literal
227#define	MACRO(x, y) do {						\e
228	variable = (x) + (y);						\e
229	(y) += 2;							\e
230} while (0)
231.Ed
232.Pp
233When code is conditionally compiled using
234.Ic #ifdef
235or
236.Ic #if ,
237a comment may be added following the matching
238.Ic #endif
239or
240.Ic #else
241to permit the reader to easily discern where conditionally compiled code
242regions end.
243This comment should be used only for (subjectively) long regions, regions
244greater than 20 lines, or where a series of nested
245.Ic #ifdef 's
246may be confusing to the reader.
247The comment should be separated from the
248.Ic #endif
249or
250.Ic #else
251by a single space.
252For short conditionally compiled regions, a closing comment should not be
253used.
254.Pp
255The comment for
256.Ic #endif
257should match the expression used in the corresponding
258.Ic #if
259or
260.Ic #ifdef .
261The comment for
262.Ic #else
263and
264.Ic #elif
265should match the inverse of the expression(s) used in the preceding
266.Ic #if
267and/or
268.Ic #elif
269statements.
270In the comments, the subexpression
271.Dq Li defined(FOO)
272is abbreviated as
273.Dq Li FOO .
274For the purposes of comments,
275.Dq Ic #ifndef Li FOO
276is treated as
277.Dq Ic #if Li !defined(FOO) .
278.Bd -literal
279#ifdef KTRACE
280#include <sys/ktrace.h>
281#endif
282
283#ifdef COMPAT_43
284/* A large region here, or other conditional code. */
285#else /* !COMPAT_43 */
286/* Or here. */
287#endif /* COMPAT_43 */
288
289#ifndef COMPAT_43
290/* Yet another large region here, or other conditional code. */
291#else /* COMPAT_43 */
292/* Or here. */
293#endif /* !COMPAT_43 */
294.Ed
295.Pp
296The project prefers the use of
297.St -isoC-99
298unsigned integer identifiers of the form
299.Vt uintXX_t
300rather than the older
301.Bx Ns -style
302integer identifiers of the form
303.Vt u_intXX_t .
304New code should use the former, and old code should be converted to
305the new form if other major work is being done in that area and
306there is no overriding reason to prefer the older
307.Bx Ns -style .
308Like white-space commits, care should be taken in making
309.Vt uintXX_t
310only commits.
311.Pp
312Similarly, the project prefers the use of
313ISO C99
314.Vt bool
315rather than the older
316.Vt int
317or
318.Vt boolean_t .
319New code should use
320.Vt bool ,
321and old code may be converted if it is
322reasonable to do so.
323Literal values are named
324.Dv true
325and
326.Dv false .
327These are preferred to the old spellings
328.Dv TRUE
329and
330.Dv FALSE .
331Userspace code should include
332.In stdbool.h ,
333while kernel code should include
334.In sys/types.h .
335.Pp
336Likewise, the project prefers
337ISO C99
338designated initializers when it makes sense to do so.
339.Pp
340Enumeration values are all uppercase.
341.Bd -literal
342enum enumtype { ONE, TWO } et;
343.Ed
344.Pp
345The use of internal_underscores in identifiers is preferred over
346camelCase or TitleCase.
347.Pp
348In declarations, do not put any whitespace between asterisks and
349adjacent tokens, except for tokens that are identifiers related to
350types.
351(These identifiers are the names of basic types, type
352qualifiers, and
353.Ic typedef Ns -names
354other than the one being declared.)
355Separate these identifiers from asterisks using a single space.
356.Pp
357When declaring variables in structures, declare them sorted by use, then
358by size (largest to smallest), and then in alphabetical order.
359The first category normally does not apply, but there are exceptions.
360Each one gets its own line.
361Try to make the structure
362readable by aligning the member names using either one or two tabs
363depending upon your judgment.
364You should use one tab only if it suffices to align at least 90% of
365the member names.
366Names following extremely long types
367should be separated by a single space.
368.Pp
369Major structures should be declared at the top of the file in which they
370are used, or in separate header files if they are used in multiple
371source files.
372Use of the structures should be by separate declarations
373and should be
374.Ic extern
375if they are declared in a header file.
376.Bd -literal
377struct foo {
378	struct foo	*next;		/* List of active foo. */
379	struct mumble	amumble;	/* Comment for mumble. */
380	int		bar;		/* Try to align the comments. */
381	struct verylongtypename *baz;	/* Does not fit in 2 tabs. */
382};
383struct foo *foohead;			/* Head of global foo list. */
384.Ed
385.Pp
386Use
387.Xr queue 3
388macros rather than rolling your own lists, whenever possible.
389Thus,
390the previous example would be better written:
391.Bd -literal
392#include <sys/queue.h>
393
394struct foo {
395	LIST_ENTRY(foo)	link;		/* Use queue macros for foo lists. */
396	struct mumble	amumble;	/* Comment for mumble. */
397	int		bar;		/* Try to align the comments. */
398	struct verylongtypename *baz;	/* Does not fit in 2 tabs. */
399};
400LIST_HEAD(, foo) foohead;		/* Head of global foo list. */
401.Ed
402.Pp
403Avoid using typedefs for structure types.
404Typedefs are problematic because they do not properly hide their
405underlying type; for example you need to know if the typedef is
406the structure itself or a pointer to the structure.
407In addition they must be declared exactly once, whereas an
408incomplete structure type can be mentioned as many times as
409necessary.
410Typedefs are difficult to use in stand-alone header files:
411the header that defines the typedef must be included
412before the header that uses it, or by the header that uses
413it (which causes namespace pollution), or there must be a
414back-door mechanism for obtaining the typedef.
415.Pp
416When convention requires a
417.Ic typedef ,
418make its name match the struct tag.
419Avoid typedefs ending in
420.Dq Li _t ,
421except as specified in Standard C or by POSIX.
422.Bd -literal
423/* Make the structure name match the typedef. */
424typedef	struct bar {
425	int	level;
426} BAR;
427typedef	int		foo;		/* This is foo. */
428typedef	const long	baz;		/* This is baz. */
429.Ed
430.Pp
431All functions are prototyped somewhere.
432.Pp
433Function prototypes for private functions (i.e., functions not used
434elsewhere) go at the top of the first source module.
435Functions
436local to one source module should be declared
437.Ic static .
438.Pp
439Functions used from other parts of the kernel are prototyped in the
440relevant include file.
441Function prototypes should be listed in a logical order, preferably
442alphabetical unless there is a compelling reason to use a different
443ordering.
444.Pp
445Functions that are used locally in more than one module go into a
446separate header file, e.g.,
447.Qq Pa extern.h .
448.Pp
449In general code can be considered
450.Dq "new code"
451when it makes up about 50% or more of the file(s) involved.
452This is enough
453to break precedents in the existing code and use the current
454.Nm
455guidelines.
456.Pp
457The kernel has a name associated with parameter types, e.g., in the kernel
458use:
459.Bd -literal
460void	function(int fd);
461.Ed
462.Pp
463In header files visible to userland applications, prototypes that are
464visible must use either
465.Dq protected
466names (ones beginning with an underscore)
467or no names with the types.
468It is preferable to use protected names.
469E.g., use:
470.Bd -literal
471void	function(int);
472.Ed
473.Pp
474or:
475.Bd -literal
476void	function(int _fd);
477.Ed
478.Pp
479Prototypes may have an extra space after a tab to enable function names
480to line up:
481.Bd -literal
482static char	*function(int _arg, const char *_arg2, struct foo *_arg3,
483		    struct bar *_arg4);
484static void	 usage(void);
485
486/*
487 * All major routines should have a comment briefly describing what
488 * they do.  The comment before the "main" routine should describe
489 * what the program does.
490 */
491int
492main(int argc, char *argv[])
493{
494	char *ep;
495	long num;
496	int ch;
497.Ed
498.Pp
499For consistency,
500.Xr getopt 3
501should be used to parse options.
502Options
503should be sorted in the
504.Xr getopt 3
505call and the
506.Ic switch
507statement, unless
508parts of the
509.Ic switch
510cascade.
511Elements in a
512.Ic switch
513statement that execute some code and then cascade to the next case should have a
514.Li FALLTHROUGH
515comment.
516Numerical arguments should be checked for accuracy.
517Code which is unreachable for non-obvious reasons may be marked /*
518.Li NOTREACHED
519*/.
520.Bd -literal
521	while ((ch = getopt(argc, argv, "abNn:")) != -1)
522		switch (ch) {		/* Indent the switch. */
523		case 'a':		/* Do not indent the case. */
524			aflag = 1;	/* Indent case body one tab. */
525			/* FALLTHROUGH */
526		case 'b':
527			bflag = 1;
528			break;
529		case 'N':
530			Nflag = 1;
531			break;
532		case 'n':
533			num = strtol(optarg, &ep, 10);
534			if (num <= 0 || *ep != '\e0') {
535				warnx("illegal number, -n argument -- %s",
536				    optarg);
537				usage();
538			}
539			break;
540		case '?':
541		default:
542			usage();
543		}
544	argc -= optind;
545	argv += optind;
546.Ed
547.Pp
548Space after keywords
549.Pq Ic if , while , for , return , switch .
550Two styles of braces
551.Ql ( \&{
552and
553.Ql \&} )
554are allowed for single line statements.
555Either they are used for all single statements, or
556they are used only where needed for clarity.
557Usage within a function should be consistent.
558Forever loops are done with
559.Ic for Ns 's ,
560not
561.Ic while Ns 's .
562.Bd -literal
563	for (p = buf; *p != '\e0'; ++p)
564		;	/* nothing */
565	for (;;)
566		stmt;
567	for (;;) {
568		z = a + really + long + statement + that + needs +
569		    two + lines + gets + indented + four + spaces +
570		    on + the + second + and + subsequent + lines;
571	}
572	for (;;) {
573		if (cond)
574			stmt;
575	}
576	if (val != NULL)
577		val = realloc(val, newsize);
578.Ed
579.Pp
580Parts of a
581.Ic for
582loop may be left empty.
583.Bd -literal
584	for (; cnt < 15; cnt++) {
585		stmt1;
586		stmt2;
587	}
588.Ed
589.Pp
590A
591.Ic for
592loop may declare and initialize its counting variable.
593.Bd -literal
594	for (int i = 0; i < 15; i++) {
595		stmt1;
596	}
597.Ed
598.Pp
599Indentation is an 8 character tab.
600Second level indents are four spaces.
601If you have to wrap a long statement, put the operator at the end of the
602line.
603.Bd -literal
604	while (cnt < 20 && this_variable_name_is_too_long &&
605	    ep != NULL)
606		z = a + really + long + statement + that + needs +
607		    two + lines + gets + indented + four + spaces +
608		    on + the + second + and + subsequent + lines;
609.Ed
610.Pp
611Do not add whitespace at the end of a line, and only use tabs
612followed by spaces
613to form the indentation.
614Do not use more spaces than a tab will produce
615and do not use spaces in front of tabs.
616.Pp
617Closing and opening braces go on the same line as the
618.Ic else .
619Braces that are not necessary may be left out.
620.Bd -literal
621	if (test)
622		stmt;
623	else if (bar) {
624		stmt;
625		stmt;
626	} else
627		stmt;
628.Ed
629.Pp
630No spaces after function names.
631Commas have a space after them.
632No spaces
633after
634.Ql \&(
635or
636.Ql \&[
637or preceding
638.Ql \&]
639or
640.Ql \&)
641characters.
642.Bd -literal
643	error = function(a1, a2);
644	if (error != 0)
645		exit(error);
646.Ed
647.Pp
648Unary operators do not require spaces, binary operators do.
649Do not use parentheses unless they are required for precedence or unless the
650statement is confusing without them.
651Remember that other people may
652confuse easier than you.
653Do YOU understand the following?
654.Bd -literal
655	a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
656	k = !(l & FLAGS);
657.Ed
658.Pp
659Exits should be 0 on success, or 1 on failure.
660.Bd -literal
661	exit(0);	/*
662			 * Avoid obvious comments such as
663			 * "Exit 0 on success."
664			 */
665}
666.Ed
667.Pp
668The function type should be on a line by itself
669preceding the function.
670The opening brace of the function body should be
671on a line by itself.
672.Bd -literal
673static char *
674function(int a1, int a2, float fl, int a4, struct bar *bar)
675{
676.Ed
677.Pp
678When declaring variables in functions declare them sorted by size,
679then in alphabetical order; multiple ones per line are okay.
680If a line overflows reuse the type keyword.
681Variables may be initialized where declared especially when they
682are constant for the rest of the scope.
683Declarations may be in any block, but must be placed before statements.
684Calls to complicated functions should be avoided when initializing variables.
685.Bd -literal
686	struct foo one, *two;
687	struct baz *three = bar_get_baz(bar);
688	double four;
689	int *five, six;
690	char *seven, eight, nine, ten, eleven, twelve;
691
692	four = my_complicated_function(a1, f1, a4);
693.Ed
694.Pp
695Do not declare functions inside other functions; ANSI C says that
696such declarations have file scope regardless of the nesting of the
697declaration.
698Hiding file declarations in what appears to be a local
699scope is undesirable and will elicit complaints from a good compiler.
700.Pp
701Casts and
702.Ic sizeof Ns 's
703are not followed by a space.
704.Ic sizeof Ns 's
705are written with parenthesis always.
706The redundant parenthesis rules do not apply to
707.Fn sizeof var
708instances.
709.Pp
710.Dv NULL
711is the preferred null pointer constant.
712Use
713.Dv NULL
714instead of
715.Vt ( "type *" ) Ns 0
716or
717.Vt ( "type *" ) Ns Dv NULL
718in contexts where the compiler knows the
719type, e.g., in assignments.
720Use
721.Vt ( "type *" ) Ns Dv NULL
722in other contexts,
723in particular for all function args.
724(Casting is essential for
725variadic args and is necessary for other args if the function prototype
726might not be in scope.)
727Test pointers against
728.Dv NULL ,
729e.g., use:
730.Bd -literal
731(p = f()) == NULL
732.Ed
733.Pp
734not:
735.Bd -literal
736!(p = f())
737.Ed
738.Pp
739Do not test without a comparison, or with unary
740.Ic \&!
741(except for booleans).
742For example, use:
743.Bd -literal
744if (*p == '\e0')
745.Ed
746.Pp
747not:
748.Bd -literal
749if (!*p)
750.Ed
751.Pp
752Prefer:
753.Bd -literal
754if (count != 0)
755.Ed
756.Pp
757over:
758.Bd -literal
759if (count)
760.Ed
761.Pp
762Routines returning
763.Vt "void *"
764should not have their return values cast
765to any pointer type.
766.Pp
767Values in
768.Ic return
769statements should be enclosed in parentheses where possible.
770For example, parentheses cannot be used if the value is a C++ braced-init-list.
771.Pp
772Use
773.Xr err 3
774or
775.Xr warn 3 ,
776do not roll your own.
777.Bd -literal
778	if ((four = malloc(sizeof(struct foo))) == NULL)
779		err(1, (char *)NULL);
780	if ((six = (int *)overflow()) == NULL)
781		errx(1, "number overflowed");
782	return (eight);
783}
784.Ed
785.Pp
786Do not use K&R style declarations or definitions, they are obsolete
787and are forbidden in C23.
788Compilers warn of their use and some treat them as an error by default.
789When converting K&R style definitions to ANSI style, preserve
790any comments about parameters.
791.Pp
792Long parameter lists are wrapped with a normal four space indent.
793.Pp
794Variable numbers of arguments should look like this:
795.Bd -literal
796#include <stdarg.h>
797
798void
799vaf(const char *fmt, ...)
800{
801	va_list ap;
802
803	va_start(ap, fmt);
804	STUFF;
805	va_end(ap);
806	/* No return needed for void functions. */
807}
808
809static void
810usage(void)
811{
812.Ed
813.Pp
814Functions should have local variable declarations first, followed by one
815blank line, followed by the first statement.
816If no local variables are declared, the first line should be a statement.
817Older versions of this
818.Nm
819document required a blank line before code.
820Such lines should be removed when signficant changes are made to the code.
821.Pp
822Use
823.Xr printf 3 ,
824not
825.Xr fputs 3 ,
826.Xr puts 3 ,
827.Xr putchar 3 ,
828whatever; it is faster and usually cleaner, not
829to mention avoiding stupid bugs.
830.Pp
831Usage statements should look like the manual pages
832.Sx SYNOPSIS .
833The usage statement should be structured in the following order:
834.Bl -enum
835.It
836Options without operands come first,
837in alphabetical order,
838inside a single set of brackets
839.Ql ( \&[
840and
841.Ql \&] ) .
842.It
843Options with operands come next,
844also in alphabetical order,
845with each option and its argument inside its own pair of brackets.
846.It
847Required arguments
848(if any)
849are next,
850listed in the order they should be specified on the command line.
851.It
852Finally,
853any optional arguments should be listed,
854listed in the order they should be specified,
855and all inside brackets.
856.El
857.Pp
858A bar
859.Pq Ql \&|
860separates
861.Dq either-or
862options/arguments,
863and multiple options/arguments which are specified together are
864placed in a single set of brackets.
865.Bd -literal -offset 4n
866"usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\en"
867"usage: f [-a | -b] [-c [-dEe] [-n number]]\en"
868.Ed
869.Bd -literal
870	(void)fprintf(stderr, "usage: f [-ab]\en");
871	exit(1);
872}
873.Ed
874.Pp
875Note that the manual page options description should list the options in
876pure alphabetical order.
877That is, without regard to whether an option takes arguments or not.
878The alphabetical ordering should take into account the case ordering
879shown above.
880.Pp
881New core kernel code should be reasonably compliant with the
882.Nm
883guides.
884The guidelines for third-party maintained modules and device drivers are more
885relaxed but at a minimum should be internally consistent with their style.
886.Pp
887Stylistic changes (including whitespace changes) are hard on the source
888repository and are to be avoided without good reason.
889Code that is approximately
890.Fx
891KNF
892.Nm
893compliant in the repository must not diverge from compliance.
894.Pp
895Whenever possible, code should be run through a code checker
896(e.g., various static analyzers or
897.Nm cc Fl Wall )
898and produce minimal warnings.
899.Pp
900New code should use
901.Fn _Static_assert
902instead of the older
903.Fn CTASSERT .
904.Pp
905.Fn __predict_true
906and
907.Fn __predict_false
908should only be used in frequently executed code when it makes the code
909measurably faster.
910It is wasteful to make predictions for infrequently run code, like subsystem
911initialization.
912When using branch prediction hints, atypical error conditions should use
913.Fn __predict_false
914(document the exceptions).
915Operations that almost always succeed use
916.Fn __predict_true .
917Only use the annotation for the entire if statement,
918rather than individual clauses.
919Do not add these annotations without empirical evidence of the likelihood of the
920branch.
921.Sh FILES
922.Bl -tag -width indent
923.It Pa /usr/src/tools/build/checkstyle9.pl
924A script to check for violations of
925.Nm
926in a source file.
927.It Pa /usr/src/tools/tools/editing/freebsd.el
928An Emacs plugin to follow the
929.Fx
930.Nm
931indentation rules.
932.It Pa /usr/src/tools/tools/editing/freebsd.vim
933A Vim plugin to follow the
934.Fx
935.Nm
936indentation rules.
937.El
938.Sh SEE ALSO
939.Xr indent 1 ,
940.Xr err 3 ,
941.Xr warn 3 ,
942.Xr style.Makefile 5 ,
943.Xr style.mdoc 5 ,
944.Xr style.lua 9
945.Sh HISTORY
946This manual page was originally based on the
947.Pa src/admin/style/style
948file from the
949.Bx 4.4 Lite2
950release, with frequent updates to reflect the current practice and
951desire of the
952.Fx
953project.
954.Pa src/admin/style/style
955is a codification by the CSRG of the programming style of Ken Thompson and
956Dennis Ritchie in
957.At v6 .
958