comparison xml/en/docs/dev/development_guide.xml @ 2153:ccc41545bf55

Extended code style description.
author Vladimir Homutov <vl@nginx.com>
date Mon, 23 Apr 2018 15:47:07 +0300
parents ed6b44206600
children 2bc348966475
comparison
equal deleted inserted replaced
2152:a226918def86 2153:ccc41545bf55
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> 7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
8 8
9 <article name="Development guide" 9 <article name="Development guide"
10 link="/en/docs/dev/development_guide.html" 10 link="/en/docs/dev/development_guide.html"
11 lang="en" 11 lang="en"
12 rev="3"> 12 rev="4">
13 13
14 <section name="Introduction" id="introduction"> 14 <section name="Introduction" id="introduction">
15 15
16 16
17 <section name="Code layout" id="code_layout"> 17 <section name="Code layout" id="code_layout">
3511 3511
3512 <list type="bullet"> 3512 <list type="bullet">
3513 3513
3514 <listitem> 3514 <listitem>
3515 <literal>NGX_CONF_BLOCK</literal> — Directive is a block, that is, it can 3515 <literal>NGX_CONF_BLOCK</literal> — Directive is a block, that is, it can
3516 contain other directives within its opening and closing curly braces, or even 3516 contain other directives within its opening and closing braces, or even
3517 implement its own parser to handle contents inside. 3517 implement its own parser to handle contents inside.
3518 </listitem> 3518 </listitem>
3519 3519
3520 <listitem> 3520 <listitem>
3521 <literal>NGX_CONF_FLAG</literal> — Directive takes a boolean value, either 3521 <literal>NGX_CONF_FLAG</literal> — Directive takes a boolean value, either
6603 6603
6604 </para> 6604 </para>
6605 6605
6606 </section> 6606 </section>
6607 6607
6608
6609 <section name="Code style" id="code_style">
6610
6611 <section name="General rules" id="code_style_general_rules">
6612
6613 <para>
6614 <list type="bullet">
6615
6616 <listitem>
6617 maximum text width is 80 characters
6618 </listitem>
6619
6620 <listitem>
6621 indentation is 4 spaces
6622 </listitem>
6623
6624 <listitem>
6625 no tabs, no trailing spaces
6626 </listitem>
6627
6628 <listitem>
6629 list elements on the same line are separated with spaces
6630 </listitem>
6631
6632 <listitem>
6633 hexadecimal literals are lowercase
6634 </listitem>
6635
6636 <listitem>
6637 file names, function and type names, and global variables have the
6638 <literal>ngx_</literal> or more specific prefix such as
6639 <literal>ngx_http_</literal> and <literal>ngx_mail_</literal>
6640 </listitem>
6641
6642 </list>
6643
6644 <programlisting>
6645 size_t
6646 ngx_utf8_length(u_char *p, size_t n)
6647 {
6648 u_char c, *last;
6649 size_t len;
6650
6651 last = p + n;
6652
6653 for (len = 0; p &lt; last; len++) {
6654
6655 c = *p;
6656
6657 if (c &lt; 0x80) {
6658 p++;
6659 continue;
6660 }
6661
6662 if (ngx_utf8_decode(&amp;p, n) > 0x10ffff) {
6663 /* invalid UTF-8 */
6664 return n;
6665 }
6666 }
6667
6668 return len;
6669 }
6670 </programlisting>
6671
6672 </para>
6673
6674 </section>
6675
6676
6677 <section name="Files" id="code_style_files">
6678
6679 <para>
6680 A typical source file may contain the following sections separated by
6681 two empty lines:
6682
6683 <list type="bullet">
6684 <listitem>
6685 copyright statements
6686 </listitem>
6687
6688 <listitem>
6689 includes
6690 </listitem>
6691
6692 <listitem>
6693 preprocessor definitions
6694 </listitem>
6695
6696 <listitem>
6697 type definitions
6698 </listitem>
6699
6700 <listitem>
6701 function prototypes
6702 </listitem>
6703
6704 <listitem>
6705 variable definitions
6706 </listitem>
6707
6708 <listitem>
6709 function definitions
6710 </listitem>
6711
6712 </list>
6713 </para>
6714
6715 <para>
6716 Copyright statements look like this:
6717 <programlisting>
6718 /*
6719 * Copyright (C) Author Name
6720 * Copyright (C) Organization, Inc.
6721 */
6722 </programlisting>
6723 If the file is modified significantly, the list of authors should be updated,
6724 the new author is added to the top.
6725 </para>
6726
6727 <para>
6728 The <literal>ngx_config.h</literal> and <literal>ngx_core.h</literal> files
6729 are always included first, followed by one of
6730 <literal>ngx_http.h</literal>, <literal>ngx_stream.h</literal>,
6731 or <literal>ngx_mail.h</literal>.
6732 Then follow optional external header files:
6733 <programlisting>
6734 #include &lt;ngx_config.h>
6735 #include &lt;ngx_core.h>
6736 #include &lt;ngx_http.h>
6737
6738 #include &lt;libxml/parser.h>
6739 #include &lt;libxml/tree.h>
6740 #include &lt;libxslt/xslt.h>
6741
6742 #if (NGX_HAVE_EXSLT)
6743 #include &lt;libexslt/exslt.h>
6744 #endif
6745 </programlisting>
6746
6747 </para>
6748
6749 <para>
6750 Header files should include the so called "header protection":
6751 <programlisting>
6752 #ifndef _NGX_PROCESS_CYCLE_H_INCLUDED_
6753 #define _NGX_PROCESS_CYCLE_H_INCLUDED_
6754 ...
6755 #endif /* _NGX_PROCESS_CYCLE_H_INCLUDED_ */
6756 </programlisting>
6757 </para>
6758
6759 </section>
6760
6761
6762 <section name="Comments" id="code_style_comments">
6763 <para>
6764 <list type="bullet">
6765
6766 <listitem>
6767 “<literal>//</literal>” comments are not used
6768 </listitem>
6769
6770 <listitem>
6771 text is written in English, American spelling is preferred
6772 </listitem>
6773
6774 <listitem>
6775 multi-line comments are formatted like this:
6776 <programlisting>
6777 /*
6778 * The red-black tree code is based on the algorithm described in
6779 * the "Introduction to Algorithms" by Cormen, Leiserson and Rivest.
6780 */
6781 </programlisting>
6782 <programlisting>
6783 /* find the server configuration for the address:port */
6784 </programlisting>
6785 </listitem>
6786
6787 </list>
6788 </para>
6789
6790 </section>
6791
6792
6793 <section name="Preprocessor" id="code_style_preprocessor">
6794 <para>
6795 Macro names start from <literal>ngx_</literal> or <literal>NGX_</literal>
6796 (or more specific) prefix.
6797 Macro names for constants are uppercase.
6798 Parameterized macros and macros for initializers are lowercase.
6799 The macro name and value are separated by at least two spaces:
6800 <programlisting>
6801 #define NGX_CONF_BUFFER 4096
6802
6803 #define ngx_buf_in_memory(b) (b->temporary || b->memory || b->mmap)
6804
6805 #define ngx_buf_size(b) \
6806 (ngx_buf_in_memory(b) ? (off_t) (b->last - b->pos): \
6807 (b->file_last - b->file_pos))
6808
6809 #define ngx_null_string { 0, NULL }
6810 </programlisting>
6811 Conditions are inside parentheses, negation is outside:
6812 <programlisting>
6813 #if (NGX_HAVE_KQUEUE)
6814 ...
6815 #elif ((NGX_HAVE_DEVPOLL &amp;&amp; !(NGX_TEST_BUILD_DEVPOLL)) \
6816 || (NGX_HAVE_EVENTPORT &amp;&amp; !(NGX_TEST_BUILD_EVENTPORT)))
6817 ...
6818 #elif (NGX_HAVE_EPOLL &amp;&amp; !(NGX_TEST_BUILD_EPOLL))
6819 ...
6820 #elif (NGX_HAVE_POLL)
6821 ...
6822 #else /* select */
6823 ...
6824 #endif /* NGX_HAVE_KQUEUE */
6825 </programlisting>
6826 </para>
6827 </section>
6828
6829
6830 <section name="Types" id="code_style_types">
6831
6832 <para>
6833 Type names end with the “<literal>_t</literal>” suffix.
6834 A defined type name is separated by at least two spaces:
6835 <programlisting>
6836 typedef ngx_uint_t ngx_rbtree_key_t;
6837 </programlisting>
6838 </para>
6839
6840 <para>
6841 Structure types are defined using <literal>typedef</literal>.
6842 Inside structures, member types and names are aligned:
6843 <programlisting>
6844 typedef struct {
6845 size_t len;
6846 u_char *data;
6847 } ngx_str_t;
6848 </programlisting>
6849 Keep alignment identical among different structures in the file.
6850 A structure that points to itself has the name, ending with
6851 “<literal>_s</literal>”.
6852 Adjacent structure definitions are separated with two empty lines:
6853 <programlisting>
6854 typedef struct ngx_list_part_s ngx_list_part_t;
6855
6856 struct ngx_list_part_s {
6857 void *elts;
6858 ngx_uint_t nelts;
6859 ngx_list_part_t *next;
6860 };
6861
6862
6863 typedef struct {
6864 ngx_list_part_t *last;
6865 ngx_list_part_t part;
6866 size_t size;
6867 ngx_uint_t nalloc;
6868 ngx_pool_t *pool;
6869 } ngx_list_t;
6870 </programlisting>
6871 Each structure member is declared on its own line:
6872 <programlisting>
6873 typedef struct {
6874 ngx_uint_t hash;
6875 ngx_str_t key;
6876 ngx_str_t value;
6877 u_char *lowcase_key;
6878 } ngx_table_elt_t;
6879 </programlisting>
6880 </para>
6881
6882 <para>
6883 Function pointers inside structures have defined types ending
6884 with “<literal>_pt</literal>”:
6885 <programlisting>
6886 typedef ssize_t (*ngx_recv_pt)(ngx_connection_t *c, u_char *buf, size_t size);
6887 typedef ssize_t (*ngx_recv_chain_pt)(ngx_connection_t *c, ngx_chain_t *in,
6888 off_t limit);
6889 typedef ssize_t (*ngx_send_pt)(ngx_connection_t *c, u_char *buf, size_t size);
6890 typedef ngx_chain_t *(*ngx_send_chain_pt)(ngx_connection_t *c, ngx_chain_t *in,
6891 off_t limit);
6892
6893 typedef struct {
6894 ngx_recv_pt recv;
6895 ngx_recv_chain_pt recv_chain;
6896 ngx_recv_pt udp_recv;
6897 ngx_send_pt send;
6898 ngx_send_pt udp_send;
6899 ngx_send_chain_pt udp_send_chain;
6900 ngx_send_chain_pt send_chain;
6901 ngx_uint_t flags;
6902 } ngx_os_io_t;
6903 </programlisting>
6904 </para>
6905
6906 <para>
6907 Enumerations have types ending with “<literal>_e</literal>”:
6908 <programlisting>
6909 typedef enum {
6910 ngx_http_fastcgi_st_version = 0,
6911 ngx_http_fastcgi_st_type,
6912 ...
6913 ngx_http_fastcgi_st_padding
6914 } ngx_http_fastcgi_state_e;
6915 </programlisting>
6916 </para>
6917
6918 </section>
6919
6920
6921 <section name="Variables" id="code_style_variables">
6922
6923 <para>
6924 Variables are declared sorted by length of a base type, then alphabetically.
6925 Type names and variable names are aligned.
6926 The type and name “columns” are separated with two spaces.
6927 Large arrays are put at the end of a declaration block:
6928 <programlisting>
6929 u_char | | *rv, *p;
6930 ngx_conf_t | | *cf;
6931 ngx_uint_t | | i, j, k;
6932 unsigned int | | len;
6933 struct sockaddr | | *sa;
6934 const unsigned char | | *data;
6935 ngx_peer_connection_t | | *pc;
6936 ngx_http_core_srv_conf_t | |**cscfp;
6937 ngx_http_upstream_srv_conf_t| | *us, *uscf;
6938 u_char | | text[NGX_SOCKADDR_STRLEN];
6939 </programlisting>
6940 </para>
6941
6942 <para>
6943 Static and global variables may be initialized on declaration:
6944 <programlisting>
6945 static ngx_str_t ngx_http_memcached_key = ngx_string("memcached_key");
6946 </programlisting>
6947
6948 <programlisting>
6949 static ngx_uint_t mday[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
6950 </programlisting>
6951
6952 <programlisting>
6953 static uint32_t ngx_crc32_table16[] = {
6954 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
6955 ...
6956 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
6957 };
6958 </programlisting>
6959 </para>
6960
6961 <para>
6962 There is a bunch of commonly used type/name combinations:
6963 <programlisting>
6964 u_char *rv;
6965 ngx_int_t rc;
6966 ngx_conf_t *cf;
6967 ngx_connection_t *c;
6968 ngx_http_request_t *r;
6969 ngx_peer_connection_t *pc;
6970 ngx_http_upstream_srv_conf_t *us, *uscf;
6971 </programlisting>
6972 </para>
6973 </section>
6974
6975
6976 <section name="Functions" id="code_style_functions">
6977
6978 <para>
6979 All functions (even static ones) should have prototypes.
6980 Prototypes include argument names.
6981 Long prototypes are wrapped with a single indentation on continuation lines:
6982 <programlisting>
6983 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
6984 static ngx_int_t ngx_http_init_phases(ngx_conf_t *cf,
6985 ngx_http_core_main_conf_t *cmcf);
6986
6987 static char *ngx_http_merge_servers(ngx_conf_t *cf,
6988 ngx_http_core_main_conf_t *cmcf, ngx_http_module_t *module,
6989 ngx_uint_t ctx_index);
6990 </programlisting>
6991 The function name in a definition starts with a new line.
6992 The function body opening and closing braces are on separate lines.
6993 The body of a function is indented.
6994 There are two empty lines between functions:
6995 <programlisting>
6996 static ngx_int_t
6997 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
6998 {
6999 ...
7000 }
7001
7002
7003 static ngx_int_t
7004 ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
7005 ngx_http_conf_port_t *port, ngx_http_listen_opt_t *lsopt)
7006 {
7007 ...
7008 }
7009 </programlisting>
7010 There is no space after the function name and opening parenthesis.
7011 Long function calls are wrapped such that continuation lines start
7012 from the position of the first function argument.
7013 If this is impossible, format the first continuation line such that it
7014 ends at position 79:
7015 <programlisting>
7016 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
7017 "http header: \"%V: %V\"",
7018 &amp;h->key, &amp;h->value);
7019
7020 hc->busy = ngx_palloc(r->connection->pool,
7021 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
7022 </programlisting>
7023 The <literal>ngx_inline</literal> macro should be used instead of
7024 <literal>inline</literal>:
7025 <programlisting>
7026 static ngx_inline void ngx_cpuid(uint32_t i, uint32_t *buf);
7027 </programlisting>
7028 </para>
7029
7030 </section>
7031
7032
7033 <section name="Expressions" id="code_style_expressions">
7034
7035 <para>
7036 Binary operators except “<literal>.</literal>” and “<literal>−></literal>”
7037 should be separated from their operands by one space.
7038 Unary operators and subscripts are not separated from their operands by spaces:
7039 <programlisting>
7040 width = width * 10 + (*fmt++ - '0');
7041 </programlisting>
7042 <programlisting>
7043 ch = (u_char) ((decoded &lt;&lt; 4) + (ch - '0'));
7044 </programlisting>
7045 <programlisting>
7046 r->exten.data = &amp;r->uri.data[i + 1];
7047 </programlisting>
7048 </para>
7049
7050 <para>
7051 Type casts are separated by one space from casted expressions.
7052 An asterisk inside type cast is separated with space from type name:
7053 <programlisting>
7054 len = ngx_sock_ntop((struct sockaddr *) sin6, p, len, 1);
7055 </programlisting>
7056 </para>
7057
7058 <para>
7059 If an expression does not fit into single line, it is wrapped.
7060 The preferred point to break a line is a binary operator.
7061 The continuation line is lined up with the start of expression:
7062 <programlisting>
7063 if (status == NGX_HTTP_MOVED_PERMANENTLY
7064 || status == NGX_HTTP_MOVED_TEMPORARILY
7065 || status == NGX_HTTP_SEE_OTHER
7066 || status == NGX_HTTP_TEMPORARY_REDIRECT
7067 || status == NGX_HTTP_PERMANENT_REDIRECT)
7068 {
7069 ...
7070 }
7071 </programlisting>
7072 <programlisting>
7073 p->temp_file->warn = "an upstream response is buffered "
7074 "to a temporary file";
7075 </programlisting>
7076 As a last resort, it is possible to wrap an expression so that the
7077 continuation line ends at position 79:
7078 <programlisting>
7079 hinit->hash = ngx_pcalloc(hinit->pool, sizeof(ngx_hash_wildcard_t)
7080 + size * sizeof(ngx_hash_elt_t *));
7081 </programlisting>
7082 The above rules also apply to sub-expressions,
7083 where each sub-expression has its own indentation level:
7084 <programlisting>
7085 if (((u->conf->cache_use_stale &amp; NGX_HTTP_UPSTREAM_FT_UPDATING)
7086 || c->stale_updating) &amp;&amp; !r->background
7087 &amp;&amp; u->conf->cache_background_update)
7088 {
7089 ...
7090 }
7091 </programlisting>
7092 Sometimes, it is convenient to wrap an expression after a cast.
7093 In this case, the continuation line is indented:
7094 <programlisting>
7095 node = (ngx_rbtree_node_t *)
7096 ((u_char *) lr - offsetof(ngx_rbtree_node_t, color));
7097 </programlisting>
7098 </para>
7099
7100 <para>
7101 Pointers are explicitly compared to
7102 <literal>NULL</literal> (not <literal>0</literal>):
7103
7104 <programlisting>
7105 if (ptr != NULL) {
7106 ...
7107 }
7108 </programlisting>
7109 </para>
7110
7111 </section>
7112
7113
7114 <section name="Conditionals and Loops" id="code_style_conditionals_and_loops">
7115
7116 <para>
7117 The “<literal>if</literal>” keyword is separated from the condition by
7118 one space.
7119 Opening brace is located on the same line, or on a
7120 dedicated line if the condition takes several lines.
7121 Closing brace is located on a dedicated line, optionally followed
7122 by “<literal>else if</literal> / <literal>else</literal>”.
7123 Usually, there is an empty line before the
7124 “<literal>else if</literal> / <literal>else</literal>” part:
7125 <programlisting>
7126 if (node->left == sentinel) {
7127 temp = node->right;
7128 subst = node;
7129
7130 } else if (node->right == sentinel) {
7131 temp = node->left;
7132 subst = node;
7133
7134 } else {
7135 subst = ngx_rbtree_min(node->right, sentinel);
7136
7137 if (subst->left != sentinel) {
7138 temp = subst->left;
7139
7140 } else {
7141 temp = subst->right;
7142 }
7143 }
7144 </programlisting>
7145 </para>
7146
7147 <para>
7148 Similar formatting rules are applied to “<literal>do</literal>”
7149 and “<literal>while</literal>” loops:
7150 <programlisting>
7151 while (p &lt; last &amp;&amp; *p == ' ') {
7152 p++;
7153 }
7154 </programlisting>
7155
7156 <programlisting>
7157 do {
7158 ctx->node = rn;
7159 ctx = ctx->next;
7160 } while (ctx);
7161 </programlisting>
7162 </para>
7163
7164 <para>
7165 The “<literal>switch</literal>” keyword is separated from the condition by
7166 one space.
7167 Opening brace is located on the same line.
7168 Closing brace is located on a dedicated line.
7169 The “<literal>case</literal>” keywords are lined up with
7170 “<literal>switch</literal>”:
7171 <programlisting>
7172 switch (ch) {
7173 case '!':
7174 looked = 2;
7175 state = ssi_comment0_state;
7176 break;
7177
7178 case '&lt;':
7179 copy_end = p;
7180 break;
7181
7182 default:
7183 copy_end = p;
7184 looked = 0;
7185 state = ssi_start_state;
7186 break;
7187 }
7188 </programlisting>
7189 </para>
7190
7191 <para>
7192 Most “<literal>for</literal>” loops are formatted like this:
7193 <programlisting>
7194 for (i = 0; i &lt; ccf->env.nelts; i++) {
7195 ...
7196 }
7197 </programlisting>
7198 <programlisting>
7199 for (q = ngx_queue_head(locations);
7200 q != ngx_queue_sentinel(locations);
7201 q = ngx_queue_next(q))
7202 {
7203 ...
7204 }
7205 </programlisting>
7206 If some part of the “<literal>for</literal>” statement is omitted,
7207 this is indicated by the “<literal>/* void */</literal>” comment:
7208 <programlisting>
7209 for (i = 0; /* void */ ; i++) {
7210 ...
7211 }
7212 </programlisting>
7213 A loop with an empty body is also indicated by the
7214 “<literal>/* void */</literal>” comment which may be put on the same line:
7215 <programlisting>
7216 for (cl = *busy; cl->next; cl = cl->next) { /* void */ }
7217 </programlisting>
7218 An endless loop looks like this:
7219 <programlisting>
7220 for ( ;; ) {
7221 ...
7222 }
7223 </programlisting>
7224 </para>
7225
7226 </section>
7227
7228
7229 <section name="Labels" id="code_style_labels">
7230
7231 <para>
7232 Labels are surrounded with empty lines and are indented at the previous level:
7233 <programlisting>
7234 if (i == 0) {
7235 u->err = "host not found";
7236 goto failed;
7237 }
7238
7239 u->addrs = ngx_pcalloc(pool, i * sizeof(ngx_addr_t));
7240 if (u->addrs == NULL) {
7241 goto failed;
7242 }
7243
7244 u->naddrs = i;
7245
7246 ...
7247
7248 return NGX_OK;
7249
7250 failed:
7251
7252 freeaddrinfo(res);
7253 return NGX_ERROR;
7254 </programlisting>
7255 </para>
7256 </section>
7257
7258 </section>
7259
6608 </article> 7260 </article>