2016-04-04 06:11:32 +08:00
|
|
|
# checksrc
|
|
|
|
|
|
|
|
This is the tool we use within the curl project to scan C source code and
|
|
|
|
check that it adheres to our [Source Code Style guide](CODE_STYLE.md).
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
checksrc.pl [options] [file1] [file2] ...
|
|
|
|
|
|
|
|
## Command line options
|
|
|
|
|
2022-01-27 09:12:50 +08:00
|
|
|
`-W[file]` skip that file and exclude it from being checked. Helpful
|
2016-04-06 08:00:01 +08:00
|
|
|
when, for example, one of the files is generated.
|
2016-04-04 14:36:21 +08:00
|
|
|
|
|
|
|
`-D[dir]` directory name to prepend to file names when accessing them.
|
|
|
|
|
|
|
|
`-h` shows the help output, that also lists all recognized warnings
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
## What does `checksrc` warn for?
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
`checksrc` does not check and verify the code against the entire style guide.
|
2021-12-09 19:03:35 +08:00
|
|
|
The script is an effort to detect the most common mistakes and syntax mistakes
|
|
|
|
that contributors make before they get accustomed to our code style. Heck,
|
|
|
|
many of us regulars do the mistakes too and this script helps us keep the code
|
|
|
|
in shape.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
|
|
|
checksrc.pl -h
|
|
|
|
|
|
|
|
Lists how to use the script and it lists all existing warnings it has and
|
2022-09-21 05:30:19 +08:00
|
|
|
problems it detects. At the time of this writing, the existing `checksrc`
|
2016-04-04 06:11:32 +08:00
|
|
|
warnings are:
|
|
|
|
|
2018-12-03 15:25:58 +08:00
|
|
|
- `ASSIGNWITHINCONDITION`: Assignment within a conditional expression. The
|
|
|
|
code style mandates the assignment to be done outside of it.
|
|
|
|
|
2021-04-19 16:45:29 +08:00
|
|
|
- `ASTERISKNOSPACE`: A pointer was declared like `char* name` instead of the
|
|
|
|
more appropriate `char *name` style. The asterisk should sit next to the
|
|
|
|
name.
|
2018-12-03 15:25:58 +08:00
|
|
|
|
|
|
|
- `ASTERISKSPACE`: A pointer was declared like `char * name` instead of the
|
|
|
|
more appropriate `char *name` style. The asterisk should sit right next to
|
|
|
|
the name without a space in between.
|
|
|
|
|
2022-09-19 23:30:30 +08:00
|
|
|
- `BADCOMMAND`: There's a bad `checksrc` instruction in the code. See the
|
2016-04-04 06:11:32 +08:00
|
|
|
**Ignore certain warnings** section below for details.
|
|
|
|
|
2016-11-15 17:47:07 +08:00
|
|
|
- `BANNEDFUNC`: A banned function was used. The functions sprintf, vsprintf,
|
2016-04-04 06:11:32 +08:00
|
|
|
strcat, strncat, gets are **never** allowed in curl source code.
|
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `BRACEELSE`: '} else' on the same line. The else is supposed to be on the
|
2021-04-19 16:45:29 +08:00
|
|
|
following line.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `BRACEPOS`: wrong position for an open brace (`{`).
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2021-04-19 16:45:29 +08:00
|
|
|
- `BRACEWHILE`: more than once space between end brace and while keyword
|
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `COMMANOSPACE`: a comma without following space
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2022-09-19 23:30:30 +08:00
|
|
|
- `COPYRIGHT`: the file is missing a copyright statement
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2021-10-31 23:34:44 +08:00
|
|
|
- `CPPCOMMENTS`: `//` comment detected, that is not C89 compliant
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2021-04-19 16:45:29 +08:00
|
|
|
- `DOBRACE`: only use one space after do before open brace
|
|
|
|
|
|
|
|
- `EMPTYLINEBRACE`: found empty line before open brace
|
|
|
|
|
|
|
|
- `EQUALSNOSPACE`: no space after `=` sign
|
|
|
|
|
|
|
|
- `EQUALSNULL`: comparison with `== NULL` used in if/while. We use `!var`.
|
|
|
|
|
|
|
|
- `EXCLAMATIONSPACE`: space found after exclamations mark
|
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `FOPENMODE`: `fopen()` needs a macro for the mode string, use it
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2018-12-03 15:25:58 +08:00
|
|
|
- `INDENTATION`: detected a wrong start column for code. Note that this
|
|
|
|
warning only checks some specific places and will certainly miss many bad
|
2016-04-04 06:11:32 +08:00
|
|
|
indentations.
|
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `LONGLINE`: A line is longer than 79 columns.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2018-12-03 15:25:58 +08:00
|
|
|
- `MULTISPACE`: Multiple spaces were found where only one should be used.
|
|
|
|
|
|
|
|
- `NOSPACEEQUALS`: An equals sign was found without preceding space. We prefer
|
|
|
|
`a = 2` and *not* `a=2`.
|
|
|
|
|
2021-04-19 16:45:29 +08:00
|
|
|
- `NOTEQUALSZERO`: check found using `!= 0`. We use plain `if(var)`.
|
|
|
|
|
2020-09-18 19:54:27 +08:00
|
|
|
- `ONELINECONDITION`: do not put the conditional block on the same line as `if()`
|
|
|
|
|
2018-12-03 15:25:58 +08:00
|
|
|
- `OPENCOMMENT`: File ended with a comment (`/*`) still "open".
|
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `PARENBRACE`: `){` was used without sufficient space in between.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `RETURNNOSPACE`: `return` was used without space between the keyword and the
|
2016-04-04 06:11:32 +08:00
|
|
|
following value.
|
|
|
|
|
2018-12-03 15:25:58 +08:00
|
|
|
- `SEMINOSPACE`: There was no space (or newline) following a semicolon.
|
|
|
|
|
|
|
|
- `SIZEOFNOPAREN`: Found use of sizeof without parentheses. We prefer
|
|
|
|
`sizeof(int)` style.
|
|
|
|
|
|
|
|
- `SNPRINTF` - Found use of `snprintf()`. Since we use an internal replacement
|
|
|
|
with a different return code etc, we prefer `msnprintf()`.
|
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `SPACEAFTERPAREN`: there was a space after open parenthesis, `( text`.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `SPACEBEFORECLOSE`: there was a space before a close parenthesis, `text )`.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `SPACEBEFORECOMMA`: there was a space before a comma, `one , two`.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2016-04-04 14:36:21 +08:00
|
|
|
- `SPACEBEFOREPAREN`: there was a space before an open parenthesis, `if (`,
|
2016-04-04 06:11:32 +08:00
|
|
|
where one was not expected
|
|
|
|
|
2018-04-15 06:47:36 +08:00
|
|
|
- `SPACESEMICOLON`: there was a space before semicolon, ` ;`.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2022-09-19 23:30:30 +08:00
|
|
|
- `TABS`: TAB characters are not allowed
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2020-10-01 03:10:14 +08:00
|
|
|
- `TRAILINGSPACE`: Trailing whitespace on the line
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2020-09-18 19:54:27 +08:00
|
|
|
- `TYPEDEFSTRUCT`: we frown upon (most) typedefed structs
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
- `UNUSEDIGNORE`: a `checksrc` inlined warning ignore was asked for but not
|
|
|
|
used, that is an ignore that should be removed or changed to get used.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2018-12-04 06:13:40 +08:00
|
|
|
### Extended warnings
|
|
|
|
|
|
|
|
Some warnings are quite computationally expensive to perform, so they are
|
|
|
|
turned off by default. To enable these warnings, place a `.checksrc` file in
|
|
|
|
the directory where they should be activated with commands to enable the
|
|
|
|
warnings you are interested in. The format of the file is to enable one
|
|
|
|
warning per line like so: `enable <EXTENDEDWARNING>`
|
|
|
|
|
2021-11-11 22:26:39 +08:00
|
|
|
Currently these are the extended warnings which can be enabled:
|
2018-12-04 06:13:40 +08:00
|
|
|
|
2021-10-31 23:34:44 +08:00
|
|
|
- `COPYRIGHTYEAR`: the current changeset has not updated the copyright year in
|
2018-12-04 06:13:40 +08:00
|
|
|
the source file
|
|
|
|
|
2021-11-11 22:26:39 +08:00
|
|
|
- `STRERROR`: use of banned function strerror()
|
|
|
|
|
2016-04-04 06:11:32 +08:00
|
|
|
## Ignore certain warnings
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
Due to the nature of the source code and the flaws of the `checksrc` tool,
|
|
|
|
there is sometimes a need to ignore specific warnings. `checksrc` allows a few
|
2016-04-06 08:00:01 +08:00
|
|
|
different ways to do this.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
|
|
|
### Inline ignore
|
|
|
|
|
|
|
|
You can control what to ignore within a specific source file by providing
|
2022-09-21 05:30:19 +08:00
|
|
|
instructions to `checksrc` in the source code itself. See examples below. The
|
|
|
|
instruction can ask to ignore a specific warning a specific number of times or
|
|
|
|
you ignore all of them until you mark the end of the ignored section.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
|
|
|
Inline ignores are only done for that single specific source code file.
|
|
|
|
|
|
|
|
Example
|
|
|
|
|
|
|
|
/* !checksrc! disable LONGLINE all */
|
|
|
|
|
2016-04-06 08:00:01 +08:00
|
|
|
This will ignore the warning for overly long lines until it is re-enabled with:
|
2016-04-04 06:11:32 +08:00
|
|
|
|
|
|
|
/* !checksrc! enable LONGLINE */
|
|
|
|
|
2021-10-31 23:34:44 +08:00
|
|
|
If the enabling is not performed before the end of the file, it will be enabled
|
2016-04-06 08:00:01 +08:00
|
|
|
automatically for the next file.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
|
|
|
You can also opt to ignore just N violations so that if you have a single long
|
2021-10-31 23:34:44 +08:00
|
|
|
line you just cannot shorten and is agreed to be fine anyway:
|
2016-04-04 06:11:32 +08:00
|
|
|
|
|
|
|
/* !checksrc! disable LONGLINE 1 */
|
|
|
|
|
|
|
|
... and the warning for long lines will be enabled again automatically after
|
|
|
|
it has ignored that single warning. The number `1` can of course be changed to
|
|
|
|
any other integer number. It can be used to make sure only the exact intended
|
|
|
|
instances are ignored and nothing extra.
|
|
|
|
|
|
|
|
### Directory wide ignore patterns
|
|
|
|
|
2021-10-31 23:34:44 +08:00
|
|
|
This is a method we have transitioned away from. Use inline ignores as far as
|
2016-05-02 17:18:59 +08:00
|
|
|
possible.
|
2016-04-04 06:11:32 +08:00
|
|
|
|
2020-06-09 22:08:11 +08:00
|
|
|
Make a `checksrc.skip` file in the directory of the source code with the
|
2016-05-02 17:18:59 +08:00
|
|
|
false positive, and include the full offending line into this file.
|