Fixed #define style.

We had a mix of styles for declaring function-like macros:

Style A:
 #define                    \
 foo()                      \
     do {                   \
         ...                \
     } while (0)

Style B:
 #define foo()              \
     do {                   \
         ...                \
     } while (0)

We had a similar number of occurences of each style:

 $ grep -rnI '^\w*(.*\\' | wc -l
 244
 $ grep -rn 'define.*(.*)' | wc -l
 239

(Those regexes aren't perfect, but a very decent approximation.)

Real examples:

 $ find src -type f | xargs sed -n '/^nxt_double_is_zero/,/^$/p'
 nxt_double_is_zero(f)                                                         \
     (fabs(f) <= FLT_EPSILON)

 $ find src -type f | xargs sed -n '/define nxt_http_field_set/,/^$/p'
 #define nxt_http_field_set(_field, _name, _value)                             \
     do {                                                                      \
         (_field)->name_length = nxt_length(_name);                            \
         (_field)->value_length = nxt_length(_value);                          \
         (_field)->name = (u_char *) _name;                                    \
         (_field)->value = (u_char *) _value;                                  \
     } while (0)

I'd like to standardize on a single style for them, and IMO,
having the identifier in the same line as #define is a better
option for the following reasons:

- Programmers are used to `#define foo() ...` (readability).
- One less line of code.
- The program for finding them is really simple (see below).

 function grep_ngx_func()
 {
     if (($# != 1)); then
         >&2 echo "Usage: ${FUNCNAME[0]} <func>";
         return 1;
     fi;

     find src -type f \
     | grep '\.[ch]$' \
     | xargs grep -l "$1" \
     | sort \
     | xargs pcregrep -Mn "(?s)^\$[\w\s*]+?^$1\(.*?^}";

     find src -type f \
     | grep '\.[ch]$' \
     | xargs grep -l "$1" \
     | sort \
     | xargs pcregrep -Mn "(?s)define $1\(.*?^$" \
     | sed -E '1s/^[^:]+:[0-9]+:/&\n\n/';
 }

 $ grep_ngx_func
 Usage: grep_ngx_func <func>

 $ grep_ngx_func nxt_http_field_set
 src/nxt_http.h:98:

 #define nxt_http_field_set(_field, _name, _value)                             \
     do {                                                                      \
         (_field)->name_length = nxt_length(_name);                            \
         (_field)->value_length = nxt_length(_value);                          \
         (_field)->name = (u_char *) _name;                                    \
         (_field)->value = (u_char *) _value;                                  \
     } while (0)

 $ grep_ngx_func nxt_sprintf
 src/nxt_sprintf.c:56:

 u_char * nxt_cdecl
 nxt_sprintf(u_char *buf, u_char *end, const char *fmt, ...)
 {
     u_char   *p;
     va_list  args;

     va_start(args, fmt);
     p = nxt_vsprintf(buf, end, fmt, args);
     va_end(args);

     return p;
 }

................
Scripted change:
................

$ find src -type f \
  | grep '\.[ch]$' \
  | xargs sed -i '/define *\\$/{N;s/ *\\\n/ /;s/        //}'
This commit is contained in:
Alejandro Colomar
2022-04-30 19:20:23 +02:00
parent d929fbe1a4
commit 952bcc50bf
41 changed files with 259 additions and 518 deletions

View File

@@ -8,50 +8,40 @@
#define _NXT_STRING_H_INCLUDED_
#define \
nxt_lowcase(c) \
#define nxt_lowcase(c) \
(u_char) ((c >= 'A' && c <= 'Z') ? c | 0x20 : c)
#define \
nxt_upcase(c) \
#define nxt_upcase(c) \
(u_char) ((c >= 'a' && c <= 'z') ? c & ~0x20 : c)
#define \
nxt_isdigit(c) \
#define nxt_isdigit(c) \
((u_char) ((c) - '0') <= 9)
#define \
nxt_strtod(s, endptr) \
#define nxt_strtod(s, endptr) \
strtod((char *) s, (char **) endptr)
#define \
nxt_strlen(s) \
#define nxt_strlen(s) \
strlen((char *) s)
#define \
nxt_strdup(s) \
#define nxt_strdup(s) \
strdup((char *) s)
#define \
nxt_strchr(buf, delim) \
#define nxt_strchr(buf, delim) \
(u_char *) strchr((char *) buf, delim)
#define \
nxt_memzero(buf, length) \
#define nxt_memzero(buf, length) \
(void) memset(buf, 0, length)
#define \
nxt_memset(buf, c, length) \
#define nxt_memset(buf, c, length) \
(void) memset(buf, c, length)
#define \
nxt_memcpy(dst, src, length) \
#define nxt_memcpy(dst, src, length) \
(void) memcpy(dst, src, length)
@@ -72,28 +62,23 @@ nxt_cpymem(void *dst, const void *src, size_t length)
}
#define \
nxt_memmove(dst, src, length) \
#define nxt_memmove(dst, src, length) \
(void) memmove(dst, src, length)
#define \
nxt_memcmp(s1, s2, length) \
#define nxt_memcmp(s1, s2, length) \
memcmp((char *) s1, (char *) s2, length)
#define \
nxt_memchr(s, c, length) \
#define nxt_memchr(s, c, length) \
memchr((char *) s, c, length)
#define \
nxt_strcmp(s1, s2) \
#define nxt_strcmp(s1, s2) \
strcmp((char *) s1, (char *) s2)
#define \
nxt_strncmp(s1, s2, length) \
#define nxt_strncmp(s1, s2, length) \
strncmp((char *) s1, (char *) s2, length)
@@ -125,16 +110,14 @@ typedef struct {
#define nxt_null_string { 0, NULL }
#define \
nxt_str_set(str, text) \
#define nxt_str_set(str, text) \
do { \
(str)->length = nxt_length(text); \
(str)->start = (u_char *) text; \
} while (0)
#define \
nxt_str_null(str) \
#define nxt_str_null(str) \
do { \
(str)->length = 0; \
(str)->start = NULL; \
@@ -147,35 +130,29 @@ NXT_EXPORT nxt_str_t *nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst,
NXT_EXPORT char *nxt_str_cstrz(nxt_mp_t *mp, const nxt_str_t *src);
#define \
nxt_strstr_eq(s1, s2) \
#define nxt_strstr_eq(s1, s2) \
(((s1)->length == (s2)->length) \
&& (nxt_memcmp((s1)->start, (s2)->start, (s1)->length) == 0))
#define \
nxt_strcasestr_eq(s1, s2) \
#define nxt_strcasestr_eq(s1, s2) \
(((s1)->length == (s2)->length) \
&& (nxt_memcasecmp((s1)->start, (s2)->start, (s1)->length) == 0))
#define \
nxt_str_eq(s, p, _length) \
#define nxt_str_eq(s, p, _length) \
(((s)->length == _length) && (nxt_memcmp((s)->start, p, _length) == 0))
#define \
nxt_str_start(s, p, _length) \
#define nxt_str_start(s, p, _length) \
(((s)->length >= _length) && (nxt_memcmp((s)->start, p, _length) == 0))
#define \
nxt_strchr_eq(s, c) \
#define nxt_strchr_eq(s, c) \
(((s)->length == 1) && ((s)->start[0] == c))
#define \
nxt_strchr_start(s, c) \
#define nxt_strchr_start(s, c) \
(((s)->length != 0) && ((s)->start[0] == c))