xref: /aosp_15_r20/external/curl/docs/internals/CHECKSRC.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker<!--
2*6236dae4SAndroid Build Coastguard WorkerCopyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard Worker
4*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
5*6236dae4SAndroid Build Coastguard Worker-->
6*6236dae4SAndroid Build Coastguard Worker
7*6236dae4SAndroid Build Coastguard Worker# checksrc
8*6236dae4SAndroid Build Coastguard Worker
9*6236dae4SAndroid Build Coastguard WorkerThis is the tool we use within the curl project to scan C source code and
10*6236dae4SAndroid Build Coastguard Workercheck that it adheres to our [Source Code Style guide](CODE_STYLE.md).
11*6236dae4SAndroid Build Coastguard Worker
12*6236dae4SAndroid Build Coastguard Worker## Usage
13*6236dae4SAndroid Build Coastguard Worker
14*6236dae4SAndroid Build Coastguard Worker    checksrc.pl [options] [file1] [file2] ...
15*6236dae4SAndroid Build Coastguard Worker
16*6236dae4SAndroid Build Coastguard Worker## Command line options
17*6236dae4SAndroid Build Coastguard Worker
18*6236dae4SAndroid Build Coastguard Worker`-W[file]` skip that file and exclude it from being checked. Helpful
19*6236dae4SAndroid Build Coastguard Workerwhen, for example, one of the files is generated.
20*6236dae4SAndroid Build Coastguard Worker
21*6236dae4SAndroid Build Coastguard Worker`-D[dir]` directory name to prepend to filenames when accessing them.
22*6236dae4SAndroid Build Coastguard Worker
23*6236dae4SAndroid Build Coastguard Worker`-h` shows the help output, that also lists all recognized warnings
24*6236dae4SAndroid Build Coastguard Worker
25*6236dae4SAndroid Build Coastguard Worker## What does `checksrc` warn for?
26*6236dae4SAndroid Build Coastguard Worker
27*6236dae4SAndroid Build Coastguard Worker`checksrc` does not check and verify the code against the entire style guide.
28*6236dae4SAndroid Build Coastguard WorkerThe script is an effort to detect the most common mistakes and syntax mistakes
29*6236dae4SAndroid Build Coastguard Workerthat contributors make before they get accustomed to our code style. Heck,
30*6236dae4SAndroid Build Coastguard Workermany of us regulars do the mistakes too and this script helps us keep the code
31*6236dae4SAndroid Build Coastguard Workerin shape.
32*6236dae4SAndroid Build Coastguard Worker
33*6236dae4SAndroid Build Coastguard Worker    checksrc.pl -h
34*6236dae4SAndroid Build Coastguard Worker
35*6236dae4SAndroid Build Coastguard WorkerLists how to use the script and it lists all existing warnings it has and
36*6236dae4SAndroid Build Coastguard Workerproblems it detects. At the time of this writing, the existing `checksrc`
37*6236dae4SAndroid Build Coastguard Workerwarnings are:
38*6236dae4SAndroid Build Coastguard Worker
39*6236dae4SAndroid Build Coastguard Worker- `ASSIGNWITHINCONDITION`: Assignment within a conditional expression. The
40*6236dae4SAndroid Build Coastguard Worker  code style mandates the assignment to be done outside of it.
41*6236dae4SAndroid Build Coastguard Worker
42*6236dae4SAndroid Build Coastguard Worker- `ASTERISKNOSPACE`: A pointer was declared like `char* name` instead of the
43*6236dae4SAndroid Build Coastguard Worker   more appropriate `char *name` style. The asterisk should sit next to the
44*6236dae4SAndroid Build Coastguard Worker   name.
45*6236dae4SAndroid Build Coastguard Worker
46*6236dae4SAndroid Build Coastguard Worker- `ASTERISKSPACE`: A pointer was declared like `char * name` instead of the
47*6236dae4SAndroid Build Coastguard Worker   more appropriate `char *name` style. The asterisk should sit right next to
48*6236dae4SAndroid Build Coastguard Worker   the name without a space in between.
49*6236dae4SAndroid Build Coastguard Worker
50*6236dae4SAndroid Build Coastguard Worker- `BADCOMMAND`: There is a bad `checksrc` instruction in the code. See the
51*6236dae4SAndroid Build Coastguard Worker   **Ignore certain warnings** section below for details.
52*6236dae4SAndroid Build Coastguard Worker
53*6236dae4SAndroid Build Coastguard Worker- `BANNEDFUNC`: A banned function was used. The functions sprintf, vsprintf,
54*6236dae4SAndroid Build Coastguard Worker   strcat, strncat, gets are **never** allowed in curl source code.
55*6236dae4SAndroid Build Coastguard Worker
56*6236dae4SAndroid Build Coastguard Worker- `BRACEELSE`: '} else' on the same line. The else is supposed to be on the
57*6236dae4SAndroid Build Coastguard Worker   following line.
58*6236dae4SAndroid Build Coastguard Worker
59*6236dae4SAndroid Build Coastguard Worker- `BRACEPOS`: wrong position for an open brace (`{`).
60*6236dae4SAndroid Build Coastguard Worker
61*6236dae4SAndroid Build Coastguard Worker- `BRACEWHILE`: more than once space between end brace and while keyword
62*6236dae4SAndroid Build Coastguard Worker
63*6236dae4SAndroid Build Coastguard Worker- `COMMANOSPACE`: a comma without following space
64*6236dae4SAndroid Build Coastguard Worker
65*6236dae4SAndroid Build Coastguard Worker- `COPYRIGHT`: the file is missing a copyright statement
66*6236dae4SAndroid Build Coastguard Worker
67*6236dae4SAndroid Build Coastguard Worker- `CPPCOMMENTS`: `//` comment detected, that is not C89 compliant
68*6236dae4SAndroid Build Coastguard Worker
69*6236dae4SAndroid Build Coastguard Worker- `DOBRACE`: only use one space after do before open brace
70*6236dae4SAndroid Build Coastguard Worker
71*6236dae4SAndroid Build Coastguard Worker- `EMPTYLINEBRACE`: found empty line before open brace
72*6236dae4SAndroid Build Coastguard Worker
73*6236dae4SAndroid Build Coastguard Worker- `EQUALSNOSPACE`: no space after `=` sign
74*6236dae4SAndroid Build Coastguard Worker
75*6236dae4SAndroid Build Coastguard Worker- `EQUALSNULL`: comparison with `== NULL` used in if/while. We use `!var`.
76*6236dae4SAndroid Build Coastguard Worker
77*6236dae4SAndroid Build Coastguard Worker- `EXCLAMATIONSPACE`: space found after exclamations mark
78*6236dae4SAndroid Build Coastguard Worker
79*6236dae4SAndroid Build Coastguard Worker- `FOPENMODE`: `fopen()` needs a macro for the mode string, use it
80*6236dae4SAndroid Build Coastguard Worker
81*6236dae4SAndroid Build Coastguard Worker- `INDENTATION`: detected a wrong start column for code. Note that this
82*6236dae4SAndroid Build Coastguard Worker   warning only checks some specific places and can certainly miss many bad
83*6236dae4SAndroid Build Coastguard Worker   indentations.
84*6236dae4SAndroid Build Coastguard Worker
85*6236dae4SAndroid Build Coastguard Worker- `LONGLINE`: A line is longer than 79 columns.
86*6236dae4SAndroid Build Coastguard Worker
87*6236dae4SAndroid Build Coastguard Worker- `MULTISPACE`: Multiple spaces were found where only one should be used.
88*6236dae4SAndroid Build Coastguard Worker
89*6236dae4SAndroid Build Coastguard Worker- `NOSPACEEQUALS`: An equals sign was found without preceding space. We prefer
90*6236dae4SAndroid Build Coastguard Worker  `a = 2` and *not* `a=2`.
91*6236dae4SAndroid Build Coastguard Worker
92*6236dae4SAndroid Build Coastguard Worker- `NOTEQUALSZERO`: check found using `!= 0`. We use plain `if(var)`.
93*6236dae4SAndroid Build Coastguard Worker
94*6236dae4SAndroid Build Coastguard Worker- `ONELINECONDITION`: do not put the conditional block on the same line as `if()`
95*6236dae4SAndroid Build Coastguard Worker
96*6236dae4SAndroid Build Coastguard Worker- `OPENCOMMENT`: File ended with a comment (`/*`) still "open".
97*6236dae4SAndroid Build Coastguard Worker
98*6236dae4SAndroid Build Coastguard Worker- `PARENBRACE`: `){` was used without sufficient space in between.
99*6236dae4SAndroid Build Coastguard Worker
100*6236dae4SAndroid Build Coastguard Worker- `RETURNNOSPACE`: `return` was used without space between the keyword and the
101*6236dae4SAndroid Build Coastguard Worker   following value.
102*6236dae4SAndroid Build Coastguard Worker
103*6236dae4SAndroid Build Coastguard Worker- `SEMINOSPACE`: There was no space (or newline) following a semicolon.
104*6236dae4SAndroid Build Coastguard Worker
105*6236dae4SAndroid Build Coastguard Worker- `SIZEOFNOPAREN`: Found use of sizeof without parentheses. We prefer
106*6236dae4SAndroid Build Coastguard Worker  `sizeof(int)` style.
107*6236dae4SAndroid Build Coastguard Worker
108*6236dae4SAndroid Build Coastguard Worker- `SNPRINTF` - Found use of `snprintf()`. Since we use an internal replacement
109*6236dae4SAndroid Build Coastguard Worker   with a different return code etc, we prefer `msnprintf()`.
110*6236dae4SAndroid Build Coastguard Worker
111*6236dae4SAndroid Build Coastguard Worker- `SPACEAFTERPAREN`: there was a space after open parenthesis, `( text`.
112*6236dae4SAndroid Build Coastguard Worker
113*6236dae4SAndroid Build Coastguard Worker- `SPACEBEFORECLOSE`: there was a space before a close parenthesis, `text )`.
114*6236dae4SAndroid Build Coastguard Worker
115*6236dae4SAndroid Build Coastguard Worker- `SPACEBEFORECOMMA`: there was a space before a comma, `one , two`.
116*6236dae4SAndroid Build Coastguard Worker
117*6236dae4SAndroid Build Coastguard Worker- `SPACEBEFOREPAREN`: there was a space before an open parenthesis, `if (`,
118*6236dae4SAndroid Build Coastguard Worker   where one was not expected
119*6236dae4SAndroid Build Coastguard Worker
120*6236dae4SAndroid Build Coastguard Worker- `SPACESEMICOLON`: there was a space before semicolon, ` ;`.
121*6236dae4SAndroid Build Coastguard Worker
122*6236dae4SAndroid Build Coastguard Worker- `TABS`: TAB characters are not allowed
123*6236dae4SAndroid Build Coastguard Worker
124*6236dae4SAndroid Build Coastguard Worker- `TRAILINGSPACE`: Trailing whitespace on the line
125*6236dae4SAndroid Build Coastguard Worker
126*6236dae4SAndroid Build Coastguard Worker- `TYPEDEFSTRUCT`: we frown upon (most) typedefed structs
127*6236dae4SAndroid Build Coastguard Worker
128*6236dae4SAndroid Build Coastguard Worker- `UNUSEDIGNORE`: a `checksrc` inlined warning ignore was asked for but not
129*6236dae4SAndroid Build Coastguard Worker   used, that is an ignore that should be removed or changed to get used.
130*6236dae4SAndroid Build Coastguard Worker
131*6236dae4SAndroid Build Coastguard Worker### Extended warnings
132*6236dae4SAndroid Build Coastguard Worker
133*6236dae4SAndroid Build Coastguard WorkerSome warnings are quite computationally expensive to perform, so they are
134*6236dae4SAndroid Build Coastguard Workerturned off by default. To enable these warnings, place a `.checksrc` file in
135*6236dae4SAndroid Build Coastguard Workerthe directory where they should be activated with commands to enable the
136*6236dae4SAndroid Build Coastguard Workerwarnings you are interested in. The format of the file is to enable one
137*6236dae4SAndroid Build Coastguard Workerwarning per line like so: `enable <EXTENDEDWARNING>`
138*6236dae4SAndroid Build Coastguard Worker
139*6236dae4SAndroid Build Coastguard WorkerCurrently these are the extended warnings which can be enabled:
140*6236dae4SAndroid Build Coastguard Worker
141*6236dae4SAndroid Build Coastguard Worker- `COPYRIGHTYEAR`: the current changeset has not updated the copyright year in
142*6236dae4SAndroid Build Coastguard Worker   the source file
143*6236dae4SAndroid Build Coastguard Worker
144*6236dae4SAndroid Build Coastguard Worker- `STRERROR`: use of banned function strerror()
145*6236dae4SAndroid Build Coastguard Worker
146*6236dae4SAndroid Build Coastguard Worker- `STDERR`: use of banned variable `stderr`
147*6236dae4SAndroid Build Coastguard Worker
148*6236dae4SAndroid Build Coastguard Worker## Ignore certain warnings
149*6236dae4SAndroid Build Coastguard Worker
150*6236dae4SAndroid Build Coastguard WorkerDue to the nature of the source code and the flaws of the `checksrc` tool,
151*6236dae4SAndroid Build Coastguard Workerthere is sometimes a need to ignore specific warnings. `checksrc` allows a few
152*6236dae4SAndroid Build Coastguard Workerdifferent ways to do this.
153*6236dae4SAndroid Build Coastguard Worker
154*6236dae4SAndroid Build Coastguard Worker### Inline ignore
155*6236dae4SAndroid Build Coastguard Worker
156*6236dae4SAndroid Build Coastguard WorkerYou can control what to ignore within a specific source file by providing
157*6236dae4SAndroid Build Coastguard Workerinstructions to `checksrc` in the source code itself. See examples below. The
158*6236dae4SAndroid Build Coastguard Workerinstruction can ask to ignore a specific warning a specific number of times or
159*6236dae4SAndroid Build Coastguard Workeryou ignore all of them until you mark the end of the ignored section.
160*6236dae4SAndroid Build Coastguard Worker
161*6236dae4SAndroid Build Coastguard WorkerInline ignores are only done for that single specific source code file.
162*6236dae4SAndroid Build Coastguard Worker
163*6236dae4SAndroid Build Coastguard WorkerExample
164*6236dae4SAndroid Build Coastguard Worker
165*6236dae4SAndroid Build Coastguard Worker    /* !checksrc! disable LONGLINE all */
166*6236dae4SAndroid Build Coastguard Worker
167*6236dae4SAndroid Build Coastguard WorkerThis ignores the warning for overly long lines until it is re-enabled with:
168*6236dae4SAndroid Build Coastguard Worker
169*6236dae4SAndroid Build Coastguard Worker    /* !checksrc! enable LONGLINE */
170*6236dae4SAndroid Build Coastguard Worker
171*6236dae4SAndroid Build Coastguard WorkerIf the enabling is not performed before the end of the file, it is enabled
172*6236dae4SAndroid Build Coastguard Workeragain automatically for the next file.
173*6236dae4SAndroid Build Coastguard Worker
174*6236dae4SAndroid Build Coastguard WorkerYou can also opt to ignore just N violations so that if you have a single long
175*6236dae4SAndroid Build Coastguard Workerline you just cannot shorten and is agreed to be fine anyway:
176*6236dae4SAndroid Build Coastguard Worker
177*6236dae4SAndroid Build Coastguard Worker    /* !checksrc! disable LONGLINE 1 */
178*6236dae4SAndroid Build Coastguard Worker
179*6236dae4SAndroid Build Coastguard Worker... and the warning for long lines is enabled again automatically after it has
180*6236dae4SAndroid Build Coastguard Workerignored that single warning. The number `1` can of course be changed to any
181*6236dae4SAndroid Build Coastguard Workerother integer number. It can be used to make sure only the exact intended
182*6236dae4SAndroid Build Coastguard Workerinstances are ignored and nothing extra.
183*6236dae4SAndroid Build Coastguard Worker
184*6236dae4SAndroid Build Coastguard Worker### Directory wide ignore patterns
185*6236dae4SAndroid Build Coastguard Worker
186*6236dae4SAndroid Build Coastguard WorkerThis is a method we have transitioned away from. Use inline ignores as far as
187*6236dae4SAndroid Build Coastguard Workerpossible.
188*6236dae4SAndroid Build Coastguard Worker
189*6236dae4SAndroid Build Coastguard WorkerMake a `checksrc.skip` file in the directory of the source code with the
190*6236dae4SAndroid Build Coastguard Workerfalse positive, and include the full offending line into this file.
191