From 86cc791debba09c8ed1aa0d863be844108866a38 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 29 Nov 2023 23:10:21 +0800 Subject: [PATCH] refactor: move function macros out of vim_defs.h (#26300) --- Makefile | 1 - src/clint.py | 1 - src/nvim/api/ui.c | 1 - src/nvim/api/win_config.c | 1 + src/nvim/cursor_shape.c | 1 - src/nvim/highlight.c | 1 + src/nvim/keycodes.c | 2 +- src/nvim/lua/stdlib.c | 2 +- src/nvim/lua/xdiff.c | 1 - src/nvim/memory.c | 19 ++++++++----- src/nvim/memory.h | 15 ++++++++++ src/nvim/mouse.c | 1 + src/nvim/os/fs.c | 1 + src/nvim/os/os.h | 10 +++++++ src/nvim/os/os_defs.h | 10 ++++++- src/nvim/sha256.c | 2 +- src/nvim/state.c | 2 +- src/nvim/statusline.c | 1 - src/nvim/strings.c | 19 +++++-------- src/nvim/strings.h | 22 ++++++++++++++ src/nvim/version.c | 2 +- src/nvim/vim_defs.h | 60 ++------------------------------------- 22 files changed, 86 insertions(+), 89 deletions(-) diff --git a/Makefile b/Makefile index c6a0c053ea..3781d06a6c 100644 --- a/Makefile +++ b/Makefile @@ -221,7 +221,6 @@ iwyu: build/.ran-cmake |src/nvim/ui.h\ |src/nvim/ui_client.h\ |src/nvim/ui_compositor.h\ - |src/nvim/vim_defs.h\ |src/nvim/viml/parser/expressions.h\ |src/nvim/viml/parser/parser.h\ |src/nvim/window.h\ diff --git a/src/clint.py b/src/clint.py index e730b9276b..1f588322f3 100755 --- a/src/clint.py +++ b/src/clint.py @@ -976,7 +976,6 @@ def CheckIncludes(filename, lines, error): "src/nvim/ui.h", "src/nvim/ui_client.h", "src/nvim/ui_compositor.h", - "src/nvim/vim_defs.h", "src/nvim/viml/parser/expressions.h", "src/nvim/viml/parser/parser.h", "src/nvim/window.h", diff --git a/src/nvim/api/ui.c b/src/nvim/api/ui.c index bf554a8829..836a68546c 100644 --- a/src/nvim/api/ui.c +++ b/src/nvim/api/ui.c @@ -31,7 +31,6 @@ #include "nvim/option.h" #include "nvim/types_defs.h" #include "nvim/ui.h" -#include "nvim/vim_defs.h" #define BUF_POS(data) ((size_t)((data)->buf_wptr - (data)->buf)) diff --git a/src/nvim/api/win_config.c b/src/nvim/api/win_config.c index 874be200ee..4e23717dc6 100644 --- a/src/nvim/api/win_config.c +++ b/src/nvim/api/win_config.c @@ -22,6 +22,7 @@ #include "nvim/memory.h" #include "nvim/option.h" #include "nvim/pos_defs.h" +#include "nvim/strings.h" #include "nvim/syntax.h" #include "nvim/ui.h" #include "nvim/window.h" diff --git a/src/nvim/cursor_shape.c b/src/nvim/cursor_shape.c index 745c0cc6ed..5aff3b5598 100644 --- a/src/nvim/cursor_shape.c +++ b/src/nvim/cursor_shape.c @@ -18,7 +18,6 @@ #include "nvim/state_defs.h" #include "nvim/strings.h" #include "nvim/ui.h" -#include "nvim/vim_defs.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "cursor_shape.c.generated.h" diff --git a/src/nvim/highlight.c b/src/nvim/highlight.c index 6cd966c3b4..141761c52e 100644 --- a/src/nvim/highlight.c +++ b/src/nvim/highlight.c @@ -25,6 +25,7 @@ #include "nvim/message.h" #include "nvim/option.h" #include "nvim/popupmenu.h" +#include "nvim/strings.h" #include "nvim/types_defs.h" #include "nvim/ui.h" #include "nvim/vim_defs.h" diff --git a/src/nvim/keycodes.c b/src/nvim/keycodes.c index 00d2ad3ec9..745500fe39 100644 --- a/src/nvim/keycodes.c +++ b/src/nvim/keycodes.c @@ -19,7 +19,7 @@ #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/mouse.h" -#include "nvim/vim_defs.h" +#include "nvim/strings.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "keycodes.c.generated.h" diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 9c46179269..33770b2e62 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -36,8 +36,8 @@ #include "nvim/pos_defs.h" #include "nvim/regexp.h" #include "nvim/runtime.h" +#include "nvim/strings.h" #include "nvim/types_defs.h" -#include "nvim/vim_defs.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/stdlib.c.generated.h" diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index 93c8933649..16c3aa5e11 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -14,7 +14,6 @@ #include "nvim/macros_defs.h" #include "nvim/memory.h" #include "nvim/pos_defs.h" -#include "nvim/vim_defs.h" #include "xdiff/xdiff.h" #define COMPARED_BUFFER0 (1 << 0) diff --git a/src/nvim/memory.c b/src/nvim/memory.c index a13091793c..df6c81fe0d 100644 --- a/src/nvim/memory.c +++ b/src/nvim/memory.c @@ -213,6 +213,18 @@ void *xmemdupz(const void *data, size_t len) return memcpy(xmallocz(len), data, len); } +#ifndef HAVE_STRNLEN +size_t xstrnlen(const char *s, size_t n) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE +{ + const char *end = memchr(s, '\0', n); + if (end == NULL) { + return n; + } + return (size_t)(end - s); +} +#endif + /// A version of strchr() that returns a pointer to the terminating NUL if it /// doesn't find `c`. /// @@ -496,13 +508,6 @@ bool strequal(const char *a, const char *b) return (a == NULL && b == NULL) || (a && b && strcmp(a, b) == 0); } -/// Case-insensitive `strequal`. -bool striequal(const char *a, const char *b) - FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT -{ - return (a == NULL && b == NULL) || (a && b && STRICMP(a, b) == 0); -} - // Avoid repeating the error message many times (they take 1 second each). // Did_outofmem_msg is reset when a character is read. void do_outofmem_msg(size_t size) diff --git a/src/nvim/memory.h b/src/nvim/memory.h index c37866ebc5..ffdc4c7366 100644 --- a/src/nvim/memory.h +++ b/src/nvim/memory.h @@ -4,6 +4,7 @@ #include // IWYU pragma: keep #include // IWYU pragma: keep +#include "auto/config.h" #include "nvim/macros_defs.h" #include "nvim/memory_defs.h" // IWYU pragma: export @@ -57,3 +58,17 @@ EXTERN size_t arena_alloc_count INIT( = 0); *ptr_ = NULL; \ (void)(*ptr_); \ } while (0) + +#define CLEAR_FIELD(field) memset(&(field), 0, sizeof(field)) +#define CLEAR_POINTER(ptr) memset((ptr), 0, sizeof(*(ptr))) + +#ifndef HAVE_STRNLEN +# define strnlen xstrnlen // Older versions of SunOS may not have strnlen +#endif + +#define STRCPY(d, s) strcpy((char *)(d), (char *)(s)) // NOLINT(runtime/printf) + +// Like strcpy() but allows overlapped source and destination. +#define STRMOVE(d, s) memmove((d), (s), strlen(s) + 1) + +#define STRCAT(d, s) strcat((char *)(d), (char *)(s)) // NOLINT(runtime/printf) diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c index b07163d1f8..8fe3864424 100644 --- a/src/nvim/mouse.c +++ b/src/nvim/mouse.c @@ -24,6 +24,7 @@ #include "nvim/mark.h" #include "nvim/mbyte.h" #include "nvim/memline.h" +#include "nvim/memory.h" #include "nvim/menu.h" #include "nvim/message.h" #include "nvim/mouse.h" diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 3fc9e0ab69..8f939c3b40 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -53,6 +53,7 @@ #ifdef MSWIN # include "nvim/mbyte.h" # include "nvim/option.h" +# include "nvim/strings.h" #endif #ifdef INCLUDE_GENERATED_DECLARATIONS diff --git a/src/nvim/os/os.h b/src/nvim/os/os.h index cbc2be47e0..302d84d066 100644 --- a/src/nvim/os/os.h +++ b/src/nvim/os/os.h @@ -10,6 +10,16 @@ #include "nvim/os/os_defs.h" // IWYU pragma: export #include "nvim/os/stdpaths_defs.h" // IWYU pragma: keep +#define HAVE_PATHDEF + +// Some file names are stored in pathdef.c, which is generated from the +// Makefile to make their value depend on the Makefile. +#ifdef HAVE_PATHDEF +extern char *default_vim_dir; +extern char *default_vimruntime_dir; +extern char *default_lib_dir; +#endif + #ifdef INCLUDE_GENERATED_DECLARATIONS // IWYU pragma: begin_exports # include "os/env.h.generated.h" diff --git a/src/nvim/os/os_defs.h b/src/nvim/os/os_defs.h index 9db559e7a5..12de55a227 100644 --- a/src/nvim/os/os_defs.h +++ b/src/nvim/os/os_defs.h @@ -6,10 +6,12 @@ #include #include +#include "auto/config.h" + // Note: Some systems need both string.h and strings.h (Savage). #include #ifdef HAVE_STRINGS_H -# include +# include // IWYU pragma: export #endif #ifdef MSWIN @@ -105,3 +107,9 @@ # define S_ISLNK(m) 0 # endif #endif + +// BSD is supposed to cover FreeBSD and similar systems. +#if (defined(BSD) || defined(__FreeBSD_kernel__)) \ + && (defined(S_ISCHR) || defined(S_IFCHR)) +# define OPEN_CHR_FILES +#endif diff --git a/src/nvim/sha256.c b/src/nvim/sha256.c index 1a07c0856d..d49224a987 100644 --- a/src/nvim/sha256.c +++ b/src/nvim/sha256.c @@ -15,8 +15,8 @@ #include #include +#include "nvim/memory.h" #include "nvim/sha256.h" -#include "nvim/vim_defs.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "sha256.c.generated.h" diff --git a/src/nvim/state.c b/src/nvim/state.c index 91258a5c8b..900eac0826 100644 --- a/src/nvim/state.c +++ b/src/nvim/state.c @@ -16,6 +16,7 @@ #include "nvim/log.h" #include "nvim/macros_defs.h" #include "nvim/main.h" +#include "nvim/memory.h" #include "nvim/option.h" #include "nvim/option_vars.h" #include "nvim/os/input.h" @@ -23,7 +24,6 @@ #include "nvim/strings.h" #include "nvim/types_defs.h" #include "nvim/ui.h" -#include "nvim/vim_defs.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "state.c.generated.h" // IWYU pragma: export diff --git a/src/nvim/statusline.c b/src/nvim/statusline.c index 604ec3dfae..4dac1b1451 100644 --- a/src/nvim/statusline.c +++ b/src/nvim/statusline.c @@ -42,7 +42,6 @@ #include "nvim/strings.h" #include "nvim/ui.h" #include "nvim/undo.h" -#include "nvim/vim_defs.h" #include "nvim/window.h" // Determines how deeply nested %{} blocks will be evaluated in statusline. diff --git a/src/nvim/strings.c b/src/nvim/strings.c index 1533a486bc..a439d11818 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -380,18 +380,6 @@ void del_trailing_spaces(char *ptr) } } -#if !defined(HAVE_STRNLEN) -size_t xstrnlen(const char *s, size_t n) - FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE -{ - const char *end = memchr(s, '\0', n); - if (end == NULL) { - return n; - } - return (size_t)(end - s); -} -#endif - #if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) // Compare two strings, ignoring case, using current locale. // Doesn't work for multi-byte characters. @@ -441,6 +429,13 @@ int vim_strnicmp(const char *s1, const char *s2, size_t len) } #endif +/// Case-insensitive `strequal`. +bool striequal(const char *a, const char *b) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + return (a == NULL && b == NULL) || (a && b && STRICMP(a, b) == 0); +} + /// strchr() version which handles multibyte strings /// /// @param[in] string String to search in. diff --git a/src/nvim/strings.h b/src/nvim/strings.h index f71396817d..d717362f87 100644 --- a/src/nvim/strings.h +++ b/src/nvim/strings.h @@ -3,9 +3,11 @@ #include // IWYU pragma: keep #include +#include "auto/config.h" #include "klib/kvec.h" #include "nvim/eval/typval_defs.h" // IWYU pragma: keep #include "nvim/func_attr.h" +#include "nvim/os/os_defs.h" #include "nvim/types_defs.h" // IWYU pragma: keep /// Append string to string and return pointer to the next byte @@ -30,3 +32,23 @@ typedef kvec_t(char) StringBuilder; #ifdef INCLUDE_GENERATED_DECLARATIONS # include "strings.h.generated.h" #endif + +#ifdef HAVE_STRCASECMP +# define STRICMP(d, s) strcasecmp((char *)(d), (char *)(s)) +#else +# ifdef HAVE_STRICMP +# define STRICMP(d, s) stricmp((char *)(d), (char *)(s)) +# else +# define STRICMP(d, s) vim_stricmp((char *)(d), (char *)(s)) +# endif +#endif + +#ifdef HAVE_STRNCASECMP +# define STRNICMP(d, s, n) strncasecmp((char *)(d), (char *)(s), (size_t)(n)) +#else +# ifdef HAVE_STRNICMP +# define STRNICMP(d, s, n) strnicmp((char *)(d), (char *)(s), (size_t)(n)) +# else +# define STRNICMP(d, s, n) vim_strnicmp((char *)(d), (char *)(s), (size_t)(n)) +# endif +#endif diff --git a/src/nvim/version.c b/src/nvim/version.c index 227ecf7b3c..cb9088afae 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -30,9 +30,9 @@ #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/option_vars.h" +#include "nvim/os/os.h" #include "nvim/strings.h" #include "nvim/version.h" -#include "nvim/vim_defs.h" // for ":version", ":intro", and "nvim --version" #ifndef NVIM_VERSION_MEDIUM diff --git a/src/nvim/vim_defs.h b/src/nvim/vim_defs.h index 84c81e0960..faf79b1c79 100644 --- a/src/nvim/vim_defs.h +++ b/src/nvim/vim_defs.h @@ -1,8 +1,5 @@ #pragma once -#include "nvim/pos_defs.h" -#include "nvim/types_defs.h" - // Some defines from the old feature.h #define SESSION_FILE "Session.vim" #define MAX_MSG_HIST_LEN 200 @@ -10,15 +7,6 @@ #define RUNTIME_DIRNAME "runtime" #include "auto/config.h" -#define HAVE_PATHDEF - -// Some file names are stored in pathdef.c, which is generated from the -// Makefile to make their value depend on the Makefile. -#ifdef HAVE_PATHDEF -extern char *default_vim_dir; -extern char *default_vimruntime_dir; -extern char *default_lib_dir; -#endif // Check if configure correctly managed to find sizeof(int). If this failed, // it becomes zero. This is likely a problem of not being able to run the @@ -27,17 +15,14 @@ extern char *default_lib_dir; # error Configure did not run properly. #endif -#include "nvim/os/os_defs.h" // bring lots of system header files +// bring lots of system header files +#include "nvim/os/os_defs.h" // IWYU pragma: keep /// length of a buffer to store a number in ASCII (64 bits binary + NUL) enum { NUMBUFLEN = 65, }; #define MAX_TYPENR 65535 -#include "nvim/gettext.h" -#include "nvim/keycodes.h" -#include "nvim/macros_defs.h" - /// Directions. typedef enum { kDirectionNotSet = 0, @@ -54,44 +39,3 @@ typedef enum { #endif #define FAIL 0 #define NOTDONE 2 // not OK or FAIL but skipped - -#define CLEAR_FIELD(field) memset(&(field), 0, sizeof(field)) -#define CLEAR_POINTER(ptr) memset((ptr), 0, sizeof(*(ptr))) - -// (vim_strchr() is now in strings.c) - -#ifndef HAVE_STRNLEN -# define strnlen xstrnlen // Older versions of SunOS may not have strnlen -#endif - -#define STRCPY(d, s) strcpy((char *)(d), (char *)(s)) // NOLINT(runtime/printf) -#ifdef HAVE_STRCASECMP -# define STRICMP(d, s) strcasecmp((char *)(d), (char *)(s)) -#else -# ifdef HAVE_STRICMP -# define STRICMP(d, s) stricmp((char *)(d), (char *)(s)) -# else -# define STRICMP(d, s) vim_stricmp((char *)(d), (char *)(s)) -# endif -#endif - -// Like strcpy() but allows overlapped source and destination. -#define STRMOVE(d, s) memmove((d), (s), strlen(s) + 1) - -#ifdef HAVE_STRNCASECMP -# define STRNICMP(d, s, n) strncasecmp((char *)(d), (char *)(s), (size_t)(n)) -#else -# ifdef HAVE_STRNICMP -# define STRNICMP(d, s, n) strnicmp((char *)(d), (char *)(s), (size_t)(n)) -# else -# define STRNICMP(d, s, n) vim_strnicmp((char *)(d), (char *)(s), (size_t)(n)) -# endif -#endif - -#define STRCAT(d, s) strcat((char *)(d), (char *)(s)) // NOLINT(runtime/printf) - -// BSD is supposed to cover FreeBSD and similar systems. -#if (defined(BSD) || defined(__FreeBSD_kernel__)) \ - && (defined(S_ISCHR) || defined(S_IFCHR)) -# define OPEN_CHR_FILES -#endif