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