xref: /freebsd/sys/contrib/openzfs/man/man1/cstyle.1 (revision 3ff01b231dfa83d518854c63e7c9cd1debd1139e)
1eda14cbcSMatt Macy.\" Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2eda14cbcSMatt Macy.\" Use is subject to license terms.
3eda14cbcSMatt Macy.\"
4eda14cbcSMatt Macy.\" CDDL HEADER START
5eda14cbcSMatt Macy.\"
6eda14cbcSMatt Macy.\" The contents of this file are subject to the terms of the
7eda14cbcSMatt Macy.\" Common Development and Distribution License (the "License").
8eda14cbcSMatt Macy.\" You may not use this file except in compliance with the License.
9eda14cbcSMatt Macy.\"
10eda14cbcSMatt Macy.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11eda14cbcSMatt Macy.\" or http://www.opensolaris.org/os/licensing.
12eda14cbcSMatt Macy.\" See the License for the specific language governing permissions
13eda14cbcSMatt Macy.\" and limitations under the License.
14eda14cbcSMatt Macy.\"
15eda14cbcSMatt Macy.\" When distributing Covered Code, include this CDDL HEADER in each
16eda14cbcSMatt Macy.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17eda14cbcSMatt Macy.\" If applicable, add the following below this CDDL HEADER, with the
18eda14cbcSMatt Macy.\" fields enclosed by brackets "[]" replaced with your own identifying
19eda14cbcSMatt Macy.\" information: Portions Copyright [yyyy] [name of copyright owner]
20eda14cbcSMatt Macy.\"
21eda14cbcSMatt Macy.\" CDDL HEADER END
22eda14cbcSMatt Macy.\"
2316038816SMartin Matuska.Dd May 26, 2021
2416038816SMartin Matuska.Dt CSTYLE 1
2516038816SMartin Matuska.Os
2616038816SMartin Matuska.
2716038816SMartin Matuska.Sh NAME
2816038816SMartin Matuska.Nm cstyle
2916038816SMartin Matuska.Nd check for some common stylistic errors in C source files
3016038816SMartin Matuska.Sh SYNOPSIS
3116038816SMartin Matuska.Nm
3216038816SMartin Matuska.Op Fl chpvCP
3316038816SMartin Matuska.Op Fl o Ar construct Ns Op , Ns Ar construct Ns …
34*3ff01b23SMartin Matuska.Oo Ar file Oc Ns …
3516038816SMartin Matuska.Sh DESCRIPTION
3616038816SMartin Matuska.Nm
3716038816SMartin Matuskainspects C source files (*.c and *.h) for common stylistic errors.
3816038816SMartin MatuskaIt attempts to check for the cstyle documented in
3916038816SMartin Matuska.Lk http://www.cis.upenn.edu/~lee/06cse480/data/cstyle.ms.pdf .
40eda14cbcSMatt MacyNote that there is much in that document that
4116038816SMartin Matuska.Em cannot
4216038816SMartin Matuskabe checked for; just because your code is
4316038816SMartin Matuska.Nm Ns -clean
4416038816SMartin Matuskadoes not mean that you've followed Sun's C style.
4516038816SMartin Matuska.Em Caveat emptor .
4616038816SMartin Matuska.
4716038816SMartin Matuska.Sh OPTIONS
48eda14cbcSMatt MacyThe following options are supported:
4916038816SMartin Matuska.Bl -tag -width "-c"
5016038816SMartin Matuska.It Fl c
5116038816SMartin MatuskaCheck continuation line indentation inside of functions.
5216038816SMartin MatuskaSun's C style
53eda14cbcSMatt Macystates that all statements must be indented to an appropriate tab stop,
5416038816SMartin Matuskaand any continuation lines after them must be indented
5516038816SMartin Matuska.Em exactly
5616038816SMartin Matuskafour spaces from the start line.
5716038816SMartin MatuskaThis option enables a series of checks designed to find
5816038816SMartin Matuskacontinuation line problems within functions only.
5916038816SMartin MatuskaThe checks have some limitations; see
6016038816SMartin Matuska.Sy CONTINUATION CHECKING ,
6116038816SMartin Matuskabelow.
6216038816SMartin Matuska.It Fl h
6316038816SMartin MatuskaPerforms heuristic checks that are sometimes wrong.
6416038816SMartin MatuskaNot generally used.
6516038816SMartin Matuska.It Fl p
6616038816SMartin MatuskaPerforms some of the more picky checks.
6716038816SMartin MatuskaIncludes ANSI
6816038816SMartin Matuska.Sy #else
6916038816SMartin Matuskaand
7016038816SMartin Matuska.Sy #endif
7116038816SMartin Matuskarules, and tries to detect spaces after casts.
7216038816SMartin MatuskaUsed as part of the putback checks.
7316038816SMartin Matuska.It Fl v
74eda14cbcSMatt MacyVerbose output; includes the text of the line of error, and, for
7516038816SMartin Matuska.Fl c ,
7616038816SMartin Matuskathe first statement in the current continuation block.
7716038816SMartin Matuska.It Fl C
78eda14cbcSMatt MacyIgnore errors in header comments (i.e. block comments starting in the
7916038816SMartin Matuskafirst column).
8016038816SMartin MatuskaNot generally used.
8116038816SMartin Matuska.It Fl P
8216038816SMartin MatuskaCheck for use of non-POSIX types.
8316038816SMartin MatuskaHistorically, types like
8416038816SMartin Matuska.Sy u_int
8516038816SMartin Matuskaand
8616038816SMartin Matuska.Sy u_long
8716038816SMartin Matuskawere used, but they are now deprecated in favor of the POSIX
8816038816SMartin Matuskatypes
8916038816SMartin Matuska.Sy uint_t ,
9016038816SMartin Matuska.Sy ulong_t ,
9116038816SMartin Matuskaetc.
9216038816SMartin MatuskaThis detects any use of the deprecated types.
9316038816SMartin MatuskaUsed as part of the putback checks.
9416038816SMartin Matuska.It Fl o Ar construct Ns Op , Ns Ar construct Ns …
9516038816SMartin MatuskaAvailable constructs include:
9616038816SMartin Matuska.Bl -tag -compact -width "doxygen"
9716038816SMartin Matuska.It Sy doxygen
9816038816SMartin MatuskaAllow doxygen-style block comments
9916038816SMartin Matuska.Pq Sy /** No and Sy /*!\& .
10016038816SMartin Matuska.It Sy splint
10116038816SMartin MatuskaAllow splint-style lint comments
102*3ff01b23SMartin Matuska.Pq Sy /*@ Ns ... Ns Sy @*/ .
10316038816SMartin Matuska.El
10416038816SMartin Matuska.El
10516038816SMartin Matuska.
10616038816SMartin Matuska.Sh CONTINUATION CHECKING
107eda14cbcSMatt MacyThe continuation checker is a reasonably simple state machine that knows
108eda14cbcSMatt Macysomething about how C is laid out, and can match parenthesis, etc. over
10916038816SMartin Matuskamultiple lines.
11016038816SMartin MatuskaIt does have some limitations:
11116038816SMartin Matuska.Bl -enum
11216038816SMartin Matuska.It
113eda14cbcSMatt MacyPreprocessor macros which cause unmatched parenthesis will confuse the
11416038816SMartin Matuskachecker for that line.
11516038816SMartin MatuskaTo fix this, you'll need to make sure that each branch of the
11616038816SMartin Matuska.Sy #if
11716038816SMartin Matuskastatement has balanced parenthesis.
11816038816SMartin Matuska.It
11916038816SMartin MatuskaSome
12016038816SMartin Matuska.Xr cpp 1
121*3ff01b23SMartin Matuskamacros do not require
122*3ff01b23SMartin Matuska.Sy ;\& Ns s after them.
12316038816SMartin MatuskaAny such macros
12416038816SMartin Matuska.Em must
12516038816SMartin Matuskabe ALL_CAPS; any lower case letters will cause bad output.
12616038816SMartin Matuska.Pp
127*3ff01b23SMartin MatuskaThe bad output will generally be corrected after the next
128*3ff01b23SMartin Matuska.Sy ;\& , { , No or Sy } .
12916038816SMartin Matuska.El
13016038816SMartin MatuskaSome continuation error messages deserve some additional explanation:
13116038816SMartin Matuska.Bl -tag -width Ds
13216038816SMartin Matuska.It Sy multiple statements continued over multiple lines
13316038816SMartin MatuskaA multi-line statement which is not broken at statement boundaries.
13416038816SMartin MatuskaFor example:
135*3ff01b23SMartin Matuska.Bd -literal -compact -offset Ds
136eda14cbcSMatt Macyif (this_is_a_long_variable == another_variable) a =
137eda14cbcSMatt Macy    b + c;
13816038816SMartin Matuska.Ed
13916038816SMartin Matuska.Pp
14016038816SMartin MatuskaWill trigger this error.
14116038816SMartin MatuskaInstead, do:
142*3ff01b23SMartin Matuska.Bd -literal -compact -offset Ds
143eda14cbcSMatt Macyif (this_is_a_long_variable == another_variable)
144eda14cbcSMatt Macy    a = b + c;
14516038816SMartin Matuska.Ed
14616038816SMartin Matuska.It Sy empty if/for/while body not on its own line
147eda14cbcSMatt MacyFor visibility, empty bodies for if, for, and while statements should be
14816038816SMartin Matuskaon their own line.
14916038816SMartin MatuskaFor example:
150*3ff01b23SMartin Matuska.Bd -literal -compact -offset Ds
151eda14cbcSMatt Macywhile (do_something(&x) == 0);
15216038816SMartin Matuska.Ed
15316038816SMartin Matuska.Pp
15416038816SMartin MatuskaWill trigger this error.
15516038816SMartin MatuskaInstead, do:
156*3ff01b23SMartin Matuska.Bd -literal -compact -offset Ds
157eda14cbcSMatt Macywhile (do_something(&x) == 0)
158eda14cbcSMatt Macy    ;
15916038816SMartin Matuska.Ed
16016038816SMartin Matuska.El
161