xref: /freebsd/sys/contrib/openzfs/man/man1/cstyle.1 (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
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
11*271171e0SMartin Matuska.\" or https://opensource.org/licenses/CDDL-1.0.
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
333ff01b23SMartin Matuska.Oo Ar file Oc Ns …
3416038816SMartin Matuska.Sh DESCRIPTION
3516038816SMartin Matuska.Nm
3616038816SMartin Matuskainspects C source files (*.c and *.h) for common stylistic errors.
3716038816SMartin MatuskaIt attempts to check for the cstyle documented in
3816038816SMartin Matuska.Lk http://www.cis.upenn.edu/~lee/06cse480/data/cstyle.ms.pdf .
39eda14cbcSMatt MacyNote that there is much in that document that
4016038816SMartin Matuska.Em cannot
4116038816SMartin Matuskabe checked for; just because your code is
4216038816SMartin Matuska.Nm Ns -clean
4316038816SMartin Matuskadoes not mean that you've followed Sun's C style.
4416038816SMartin Matuska.Em Caveat emptor .
4516038816SMartin Matuska.
4616038816SMartin Matuska.Sh OPTIONS
4716038816SMartin Matuska.Bl -tag -width "-c"
4816038816SMartin Matuska.It Fl c
4916038816SMartin MatuskaCheck continuation line indentation inside of functions.
5016038816SMartin MatuskaSun's C style
51eda14cbcSMatt Macystates that all statements must be indented to an appropriate tab stop,
5216038816SMartin Matuskaand any continuation lines after them must be indented
5316038816SMartin Matuska.Em exactly
5416038816SMartin Matuskafour spaces from the start line.
5516038816SMartin MatuskaThis option enables a series of checks designed to find
5616038816SMartin Matuskacontinuation line problems within functions only.
5716038816SMartin MatuskaThe checks have some limitations; see
5816038816SMartin Matuska.Sy CONTINUATION CHECKING ,
5916038816SMartin Matuskabelow.
6016038816SMartin Matuska.It Fl p
6116038816SMartin MatuskaPerforms some of the more picky checks.
6216038816SMartin MatuskaIncludes ANSI
6316038816SMartin Matuska.Sy #else
6416038816SMartin Matuskaand
6516038816SMartin Matuska.Sy #endif
6616038816SMartin Matuskarules, and tries to detect spaces after casts.
6716038816SMartin MatuskaUsed as part of the putback checks.
6816038816SMartin Matuska.It Fl v
69eda14cbcSMatt MacyVerbose output; includes the text of the line of error, and, for
7016038816SMartin Matuska.Fl c ,
7116038816SMartin Matuskathe first statement in the current continuation block.
7216038816SMartin Matuska.It Fl P
7316038816SMartin MatuskaCheck for use of non-POSIX types.
7416038816SMartin MatuskaHistorically, types like
7516038816SMartin Matuska.Sy u_int
7616038816SMartin Matuskaand
7716038816SMartin Matuska.Sy u_long
7816038816SMartin Matuskawere used, but they are now deprecated in favor of the POSIX
7916038816SMartin Matuskatypes
8016038816SMartin Matuska.Sy uint_t ,
8116038816SMartin Matuska.Sy ulong_t ,
8216038816SMartin Matuskaetc.
8316038816SMartin MatuskaThis detects any use of the deprecated types.
8416038816SMartin MatuskaUsed as part of the putback checks.
85716fd348SMartin Matuska.It Fl g
86716fd348SMartin MatuskaAlso print GitHub-Actions-style
87716fd348SMartin Matuska.Li ::error
88716fd348SMartin Matuskaoutput.
8916038816SMartin Matuska.El
90716fd348SMartin Matuska.
91716fd348SMartin Matuska.Sh ENVIRONMENT
92716fd348SMartin Matuska.Bl -tag -compact -width ".Ev CI"
93716fd348SMartin Matuska.It Ev CI
94716fd348SMartin MatuskaIf set and nonempty, equivalent to
95716fd348SMartin Matuska.Fl g .
9616038816SMartin Matuska.El
9716038816SMartin Matuska.
9816038816SMartin Matuska.Sh CONTINUATION CHECKING
99eda14cbcSMatt MacyThe continuation checker is a reasonably simple state machine that knows
100eda14cbcSMatt Macysomething about how C is laid out, and can match parenthesis, etc. over
10116038816SMartin Matuskamultiple lines.
10216038816SMartin MatuskaIt does have some limitations:
10316038816SMartin Matuska.Bl -enum
10416038816SMartin Matuska.It
105eda14cbcSMatt MacyPreprocessor macros which cause unmatched parenthesis will confuse the
10616038816SMartin Matuskachecker for that line.
10716038816SMartin MatuskaTo fix this, you'll need to make sure that each branch of the
10816038816SMartin Matuska.Sy #if
10916038816SMartin Matuskastatement has balanced parenthesis.
11016038816SMartin Matuska.It
11116038816SMartin MatuskaSome
11216038816SMartin Matuska.Xr cpp 1
1133ff01b23SMartin Matuskamacros do not require
1143ff01b23SMartin Matuska.Sy ;\& Ns s after them.
11516038816SMartin MatuskaAny such macros
11616038816SMartin Matuska.Em must
11716038816SMartin Matuskabe ALL_CAPS; any lower case letters will cause bad output.
11816038816SMartin Matuska.Pp
1193ff01b23SMartin MatuskaThe bad output will generally be corrected after the next
1203ff01b23SMartin Matuska.Sy ;\& , { , No or Sy } .
12116038816SMartin Matuska.El
12216038816SMartin MatuskaSome continuation error messages deserve some additional explanation:
12316038816SMartin Matuska.Bl -tag -width Ds
12416038816SMartin Matuska.It Sy multiple statements continued over multiple lines
12516038816SMartin MatuskaA multi-line statement which is not broken at statement boundaries.
12616038816SMartin MatuskaFor example:
1273ff01b23SMartin Matuska.Bd -literal -compact -offset Ds
128eda14cbcSMatt Macyif (this_is_a_long_variable == another_variable) a =
129eda14cbcSMatt Macy    b + c;
13016038816SMartin Matuska.Ed
13116038816SMartin Matuska.Pp
13216038816SMartin MatuskaWill trigger this error.
13316038816SMartin MatuskaInstead, do:
1343ff01b23SMartin Matuska.Bd -literal -compact -offset Ds
135eda14cbcSMatt Macyif (this_is_a_long_variable == another_variable)
136eda14cbcSMatt Macy    a = b + c;
13716038816SMartin Matuska.Ed
13816038816SMartin Matuska.It Sy empty if/for/while body not on its own line
139eda14cbcSMatt MacyFor visibility, empty bodies for if, for, and while statements should be
14016038816SMartin Matuskaon their own line.
14116038816SMartin MatuskaFor example:
1423ff01b23SMartin Matuska.Bd -literal -compact -offset Ds
143eda14cbcSMatt Macywhile (do_something(&x) == 0);
14416038816SMartin Matuska.Ed
14516038816SMartin Matuska.Pp
14616038816SMartin MatuskaWill trigger this error.
14716038816SMartin MatuskaInstead, do:
1483ff01b23SMartin Matuska.Bd -literal -compact -offset Ds
149eda14cbcSMatt Macywhile (do_something(&x) == 0)
150eda14cbcSMatt Macy    ;
15116038816SMartin Matuska.Ed
15216038816SMartin Matuska.El
153