2016-10-18 19:59:54 +08:00
|
|
|
# curl C code style
|
2016-03-14 17:28:54 +08:00
|
|
|
|
|
|
|
Source code that has a common style is easier to read than code that uses
|
|
|
|
different styles in different places. It helps making the code feel like one
|
2021-11-01 20:43:11 +08:00
|
|
|
single code base. Easy-to-read is an important property of code and helps
|
2016-03-14 17:28:54 +08:00
|
|
|
making it easier to review when new things are added and it helps debugging
|
|
|
|
code when developers are trying to figure out why things go wrong. A unified
|
|
|
|
style is more important than individual contributors having their own personal
|
|
|
|
tastes satisfied.
|
|
|
|
|
|
|
|
Our C code has a few style rules. Most of them are verified and upheld by the
|
2022-03-23 22:26:09 +08:00
|
|
|
`scripts/checksrc.pl` script. Invoked with `make checksrc` or even by default
|
|
|
|
by the build system when built after `./configure --enable-debug` has been
|
|
|
|
used.
|
2016-03-14 17:28:54 +08:00
|
|
|
|
|
|
|
It is normally not a problem for anyone to follow the guidelines, as you just
|
|
|
|
need to copy the style already used in the source code and there are no
|
|
|
|
particularly unusual rules in our set of rules.
|
|
|
|
|
|
|
|
We also work hard on writing code that are warning-free on all the major
|
|
|
|
platforms and in general on as many platforms as possible. Code that obviously
|
|
|
|
will cause warnings will not be accepted as-is.
|
|
|
|
|
|
|
|
## Naming
|
|
|
|
|
|
|
|
Try using a non-confusing naming scheme for your new functions and variable
|
2021-10-31 23:34:44 +08:00
|
|
|
names. It does not necessarily have to mean that you should use the same as in
|
2016-03-14 17:28:54 +08:00
|
|
|
other places of the code, just that the names should be logical,
|
2021-10-31 23:34:44 +08:00
|
|
|
understandable and be named according to what they are used for. File-local
|
2016-03-14 17:28:54 +08:00
|
|
|
functions should be made static. We like lower case names.
|
|
|
|
|
2021-02-22 06:32:59 +08:00
|
|
|
See the [INTERNALS](https://curl.se/dev/internals.html#symbols) document on
|
|
|
|
how we name non-exported library-global symbols.
|
2016-03-14 17:28:54 +08:00
|
|
|
|
|
|
|
## Indenting
|
|
|
|
|
|
|
|
We use only spaces for indentation, never TABs. We use two spaces for each new
|
|
|
|
open brace.
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
if(something_is_true) {
|
|
|
|
while(second_statement == fine) {
|
|
|
|
moo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2016-03-14 17:40:02 +08:00
|
|
|
|
2016-03-14 17:28:54 +08:00
|
|
|
## Comments
|
|
|
|
|
2021-10-31 23:34:44 +08:00
|
|
|
Since we write C89 code, **//** comments are not allowed. They were not
|
2021-08-14 05:07:54 +08:00
|
|
|
introduced in the C standard until C99. We use only __/* comments */__.
|
2016-03-14 17:28:54 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
/* this is a comment */
|
|
|
|
```
|
2016-03-14 17:28:54 +08:00
|
|
|
|
|
|
|
## Long lines
|
|
|
|
|
2016-09-12 13:51:37 +08:00
|
|
|
Source code in curl may never be wider than 79 columns and there are two
|
2021-11-01 20:43:11 +08:00
|
|
|
reasons for maintaining this even in the modern era of large and high
|
2016-03-14 17:28:54 +08:00
|
|
|
resolution screens:
|
|
|
|
|
2023-08-22 23:40:39 +08:00
|
|
|
1. Narrower columns are easier to read than wide ones. There is a reason
|
2016-03-14 17:28:54 +08:00
|
|
|
newspapers have used columns for decades or centuries.
|
|
|
|
|
|
|
|
2. Narrower columns allow developers to easier show multiple pieces of code
|
2023-04-27 17:31:36 +08:00
|
|
|
next to each other in different windows. It allows two or three source
|
2016-03-14 17:28:54 +08:00
|
|
|
code windows next to each other on the same screen - as well as multiple
|
|
|
|
terminal and debugging windows.
|
|
|
|
|
2016-03-14 17:36:51 +08:00
|
|
|
## Braces
|
2016-03-14 17:28:54 +08:00
|
|
|
|
|
|
|
In if/while/do/for expressions, we write the open brace on the same line as
|
|
|
|
the keyword and we then set the closing brace on the same indentation level as
|
|
|
|
the initial keyword. Like this:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
if(age < 40) {
|
|
|
|
/* clearly a youngster */
|
|
|
|
}
|
|
|
|
```
|
2016-03-14 17:28:54 +08:00
|
|
|
|
2016-09-12 07:12:14 +08:00
|
|
|
You may omit the braces if they would contain only a one-line statement:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
if(!x)
|
|
|
|
continue;
|
|
|
|
```
|
2016-09-12 07:12:14 +08:00
|
|
|
|
|
|
|
For functions the opening brace should be on a separate line:
|
2016-03-14 17:36:51 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
```
|
2016-03-14 17:36:51 +08:00
|
|
|
|
2016-03-14 17:28:54 +08:00
|
|
|
## 'else' on the following line
|
|
|
|
|
2018-05-14 16:19:52 +08:00
|
|
|
When adding an **else** clause to a conditional expression using braces, we
|
|
|
|
add it on a new line after the closing brace. Like this:
|
2016-03-14 17:28:54 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
if(age < 40) {
|
|
|
|
/* clearly a youngster */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* probably grumpy */
|
|
|
|
}
|
|
|
|
```
|
2016-03-14 17:28:54 +08:00
|
|
|
|
|
|
|
## No space before parentheses
|
|
|
|
|
|
|
|
When writing expressions using if/while/do/for, there shall be no space
|
|
|
|
between the keyword and the open parenthesis. Like this:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
while(1) {
|
|
|
|
/* loop forever */
|
|
|
|
}
|
|
|
|
```
|
2016-03-14 17:28:54 +08:00
|
|
|
|
2016-03-20 19:51:11 +08:00
|
|
|
## Use boolean conditions
|
|
|
|
|
|
|
|
Rather than test a conditional value such as a bool against TRUE or FALSE, a
|
|
|
|
pointer against NULL or != NULL and an int against zero or not zero in
|
|
|
|
if/while conditions we prefer:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
result = do_something();
|
|
|
|
if(!result) {
|
|
|
|
/* something went wrong */
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
```
|
2016-03-20 19:51:11 +08:00
|
|
|
|
2016-03-14 17:28:54 +08:00
|
|
|
## No assignments in conditions
|
|
|
|
|
|
|
|
To increase readability and reduce complexity of conditionals, we avoid
|
|
|
|
assigning variables within if/while conditions. We frown upon this style:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
if((ptr = malloc(100)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
```
|
2016-03-14 17:28:54 +08:00
|
|
|
|
|
|
|
and instead we encourage the above version to be spelled out more clearly:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
ptr = malloc(100);
|
|
|
|
if(!ptr)
|
|
|
|
return NULL;
|
|
|
|
```
|
2016-03-14 17:28:54 +08:00
|
|
|
|
|
|
|
## New block on a new line
|
|
|
|
|
2021-11-01 20:43:11 +08:00
|
|
|
We never write multiple statements on the same source line, even for short
|
|
|
|
if() conditions.
|
2016-03-14 17:28:54 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
if(a)
|
|
|
|
return TRUE;
|
|
|
|
else if(b)
|
|
|
|
return FALSE;
|
|
|
|
```
|
2016-03-14 17:28:54 +08:00
|
|
|
|
|
|
|
and NEVER:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
if(a) return TRUE;
|
|
|
|
else if(b) return FALSE;
|
|
|
|
```
|
2016-03-14 17:28:54 +08:00
|
|
|
|
2016-03-14 21:31:14 +08:00
|
|
|
## Space around operators
|
|
|
|
|
2021-11-26 15:46:59 +08:00
|
|
|
Please use spaces on both sides of operators in C expressions. Postfix **(),
|
2021-08-14 06:02:38 +08:00
|
|
|
[], ->, ., ++, --** and Unary **+, -, !, ~, &** operators excluded they should
|
2016-03-14 21:31:14 +08:00
|
|
|
have no space.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
bla = func();
|
|
|
|
who = name[0];
|
|
|
|
age += 1;
|
|
|
|
true = !false;
|
|
|
|
size += -2 + 3 * (a + b);
|
|
|
|
ptr->member = a++;
|
|
|
|
struct.field = b--;
|
|
|
|
ptr = &address;
|
|
|
|
contents = *pointer;
|
|
|
|
complement = ~bits;
|
|
|
|
empty = (!*string) ? TRUE : FALSE;
|
|
|
|
```
|
2016-03-14 21:31:14 +08:00
|
|
|
|
2018-05-14 16:19:52 +08:00
|
|
|
## No parentheses for return values
|
|
|
|
|
|
|
|
We use the 'return' statement without extra parentheses around the value:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
int works(void)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
```
|
2018-05-14 16:19:52 +08:00
|
|
|
|
|
|
|
## Parentheses for sizeof arguments
|
|
|
|
|
|
|
|
When using the sizeof operator in code, we prefer it to be written with
|
|
|
|
parentheses around its argument:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
int size = sizeof(int);
|
|
|
|
```
|
2018-05-14 16:19:52 +08:00
|
|
|
|
2016-09-12 07:12:14 +08:00
|
|
|
## Column alignment
|
|
|
|
|
2018-05-14 16:19:52 +08:00
|
|
|
Some statements cannot be completed on a single line because the line would be
|
|
|
|
too long, the statement too hard to read, or due to other style guidelines
|
2016-09-12 07:12:14 +08:00
|
|
|
above. In such a case the statement will span multiple lines.
|
|
|
|
|
|
|
|
If a continuation line is part of an expression or sub-expression then you
|
2024-01-23 22:12:09 +08:00
|
|
|
should align on the appropriate column so that it is easy to tell what part of
|
2016-09-12 07:12:14 +08:00
|
|
|
the statement it is. Operators should not start continuation lines. In other
|
2018-05-14 16:19:52 +08:00
|
|
|
cases follow the 2-space indent guideline. Here are some examples from
|
|
|
|
libcurl:
|
2016-09-12 07:12:14 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
if(Curl_pipeline_wanted(handle->multi, CURLPIPE_HTTP1) &&
|
|
|
|
(handle->set.httpversion != CURL_HTTP_VERSION_1_0) &&
|
|
|
|
(handle->set.httpreq == HTTPREQ_GET ||
|
|
|
|
handle->set.httpreq == HTTPREQ_HEAD))
|
2021-10-31 23:34:44 +08:00
|
|
|
/* did not ask for HTTP/1.0 and a GET or HEAD */
|
2020-12-08 01:09:37 +08:00
|
|
|
return TRUE;
|
|
|
|
```
|
2016-09-12 07:12:14 +08:00
|
|
|
|
2018-05-14 16:19:52 +08:00
|
|
|
If no parenthesis, use the default indent:
|
2016-09-12 07:12:14 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
data->set.http_disable_hostname_check_before_authentication =
|
|
|
|
(0 != va_arg(param, long)) ? TRUE : FALSE;
|
|
|
|
```
|
2018-05-14 16:19:52 +08:00
|
|
|
|
|
|
|
Function invoke with an open parenthesis:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
if(option) {
|
|
|
|
result = parse_login_details(option, strlen(option),
|
|
|
|
(userp ? &user : NULL),
|
|
|
|
(passwdp ? &passwd : NULL),
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
```
|
2018-05-14 16:19:52 +08:00
|
|
|
|
|
|
|
Align with the "current open" parenthesis:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
DEBUGF(infof(data, "Curl_pp_readresp_ %d bytes of trailing "
|
|
|
|
"server response left\n",
|
|
|
|
(int)clipamount));
|
|
|
|
```
|
2016-09-12 07:12:14 +08:00
|
|
|
|
2016-03-14 17:28:54 +08:00
|
|
|
## Platform dependent code
|
|
|
|
|
2018-05-14 16:19:52 +08:00
|
|
|
Use **#ifdef HAVE_FEATURE** to do conditional code. We avoid checking for
|
2016-03-14 17:28:54 +08:00
|
|
|
particular operating systems or hardware in the #ifdef lines. The HAVE_FEATURE
|
|
|
|
shall be generated by the configure script for unix-like systems and they are
|
2019-05-17 06:11:27 +08:00
|
|
|
hard-coded in the `config-[system].h` files for the others.
|
2016-03-14 17:28:54 +08:00
|
|
|
|
|
|
|
We also encourage use of macros/functions that possibly are empty or defined
|
|
|
|
to constants when libcurl is built without that feature, to make the code
|
2018-05-14 16:19:52 +08:00
|
|
|
seamless. Like this example where the **magic()** function works differently
|
2016-03-14 17:28:54 +08:00
|
|
|
depending on a build-time conditional:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
#ifdef HAVE_MAGIC
|
|
|
|
void magic(int a)
|
|
|
|
{
|
|
|
|
return a + 2;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define magic(x) 1
|
|
|
|
#endif
|
2016-03-14 17:28:54 +08:00
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
int content = magic(3);
|
|
|
|
```
|
2020-05-14 06:05:04 +08:00
|
|
|
|
|
|
|
## No typedefed structs
|
|
|
|
|
|
|
|
Use structs by all means, but do not typedef them. Use the `struct name` way
|
|
|
|
of identifying them:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
struct something {
|
|
|
|
void *valid;
|
|
|
|
size_t way_to_write;
|
|
|
|
};
|
|
|
|
struct something instance;
|
|
|
|
```
|
2020-05-14 06:05:04 +08:00
|
|
|
|
|
|
|
**Not okay**:
|
|
|
|
|
2020-12-08 01:09:37 +08:00
|
|
|
```c
|
|
|
|
typedef struct {
|
|
|
|
void *wrong;
|
|
|
|
size_t way_to_write;
|
|
|
|
} something;
|
|
|
|
something instance;
|
|
|
|
```
|