From 08fad0f8f6601e93b325990b4d3bba8533f34c2f Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sun, 6 Jul 2014 16:54:59 +0200 Subject: [PATCH 01/10] hashtab.h: don't include vim.h Including vim.h in another header filer is asking for trouble. Test code that includes separate header files (e.g.: cimport './src/nvim/buffer.h'), has a really bad time with this. This is just one piece of the puzzle though. --- src/nvim/hashtab.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nvim/hashtab.h b/src/nvim/hashtab.h index 172f5ca13e..7233d8c47c 100644 --- a/src/nvim/hashtab.h +++ b/src/nvim/hashtab.h @@ -1,7 +1,9 @@ #ifndef NVIM_HASHTAB_H #define NVIM_HASHTAB_H -#include "nvim/vim.h" +#include + +#include "nvim/types.h" /// Type for hash number (hash calculation result). typedef size_t hash_T; From fb72f1ee37872137c19297268e3cc2a05f27c357 Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sun, 6 Jul 2014 17:13:28 +0200 Subject: [PATCH 02/10] vim: move long_u from vim.h to types.h Seems to make no difference to the main binary, but it helps the tests a bit further along. --- src/nvim/memfile_defs.h | 2 ++ src/nvim/types.h | 5 +++++ src/nvim/vim.h | 5 ----- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/nvim/memfile_defs.h b/src/nvim/memfile_defs.h index 818246d6db..2e6e914b57 100644 --- a/src/nvim/memfile_defs.h +++ b/src/nvim/memfile_defs.h @@ -1,6 +1,8 @@ #ifndef NVIM_MEMFILE_DEFS_H #define NVIM_MEMFILE_DEFS_H +#include "nvim/types.h" + typedef struct block_hdr bhdr_T; typedef long blocknr_T; diff --git a/src/nvim/types.h b/src/nvim/types.h index 3bc6bfb9bf..a3c4509756 100644 --- a/src/nvim/types.h +++ b/src/nvim/types.h @@ -10,6 +10,11 @@ #include +// Make sure long_u is big enough to hold a pointer. +// On Win64, longs are 32 bits and pointers are 64 bits. +// For printf() and scanf(), we need to take care of long_u specifically. +typedef unsigned long long_u; + /* * Shorthand for unsigned variables. Many systems, but not all, have u_char * already defined, so we use char_u to avoid trouble. diff --git a/src/nvim/vim.h b/src/nvim/vim.h index 63b9436da7..1ff49c8011 100644 --- a/src/nvim/vim.h +++ b/src/nvim/vim.h @@ -58,11 +58,6 @@ Error: configure did not run properly.Check auto/config.log. #define NUMBUFLEN 30 /* length of a buffer to store a number in ASCII */ -// Make sure long_u is big enough to hold a pointer. -// On Win64, longs are 32 bits and pointers are 64 bits. -// For printf() and scanf(), we need to take care of long_u specifically. -typedef unsigned long long_u; - # define MAX_TYPENR 65535 /* From e288ddaee7d9c186c5829178691dbfac507d757d Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sun, 6 Jul 2014 17:27:09 +0200 Subject: [PATCH 03/10] vim: move linenr_T and colnr_T to pos.h Try to cut down vim.h's size. It's keeping us from testing more things. --- src/nvim/buffer.h | 2 ++ src/nvim/ex_cmds_defs.h | 1 + src/nvim/ex_eval.h | 2 ++ src/nvim/pos.h | 6 ++++++ src/nvim/vim.h | 6 +----- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/nvim/buffer.h b/src/nvim/buffer.h index 9690d58e0b..493ebffc63 100644 --- a/src/nvim/buffer.h +++ b/src/nvim/buffer.h @@ -1,6 +1,8 @@ #ifndef NVIM_BUFFER_H #define NVIM_BUFFER_H +#include "nvim/pos.h" // for linenr_T + /* Values for buflist_getfile() */ #define GETF_SETMARK 0x01 /* set pcmark before jumping */ #define GETF_ALT 0x02 /* jumping to alternate file (not buf num) */ diff --git a/src/nvim/ex_cmds_defs.h b/src/nvim/ex_cmds_defs.h index 1edc1bb8c6..4eafa46c10 100644 --- a/src/nvim/ex_cmds_defs.h +++ b/src/nvim/ex_cmds_defs.h @@ -16,6 +16,7 @@ #include +#include "nvim/pos.h" // for linenr_T #include "nvim/normal.h" /* diff --git a/src/nvim/ex_eval.h b/src/nvim/ex_eval.h index 7523aff792..3f5e295c18 100644 --- a/src/nvim/ex_eval.h +++ b/src/nvim/ex_eval.h @@ -1,6 +1,8 @@ #ifndef NVIM_EX_EVAL_H #define NVIM_EX_EVAL_H +#include "nvim/pos.h" // for linenr_T + /* * A list used for saving values of "emsg_silent". Used by ex_try() to save the * value of "emsg_silent" if it was non-zero. When this is done, the CSF_SILENT diff --git a/src/nvim/pos.h b/src/nvim/pos.h index 11f62ad480..7cfb52b283 100644 --- a/src/nvim/pos.h +++ b/src/nvim/pos.h @@ -1,6 +1,12 @@ #ifndef NVIM_POS_H #define NVIM_POS_H +typedef long linenr_T; // line number type +typedef int colnr_T; // column number type + +#define MAXLNUM (0x7fffffffL) // maximum (invalid) line number +#define MAXCOL (0x7fffffffL) // maximum column number, 31 bits + /* * position in file or buffer */ diff --git a/src/nvim/vim.h b/src/nvim/vim.h index 1ff49c8011..5cde6e7989 100644 --- a/src/nvim/vim.h +++ b/src/nvim/vim.h @@ -9,6 +9,7 @@ # define NVIM_VIM_H #include "nvim/types.h" +#include "nvim/pos.h" // for linenr_T, MAXCOL, etc... /* Some defines from the old feature.h */ #define SESSION_FILE "Session.vim" @@ -354,13 +355,8 @@ enum { #define PERROR(msg) \ (void) emsg3((char_u *) "%s: %s", (char_u *)msg, (char_u *)strerror(errno)) -typedef long linenr_T; /* line number type */ -typedef int colnr_T; /* column number type */ typedef unsigned short disptick_T; /* display tick type */ -#define MAXLNUM (0x7fffffffL) /* maximum (invalid) line number */ -#define MAXCOL (0x7fffffffL) /* maximum column number, 31 bits */ - #define SHOWCMD_COLS 10 /* columns needed by shown command */ #define STL_MAX_ITEM 80 /* max nr of % in statusline */ From 0564f781ab27aa7b419543f7867da6e761e179c5 Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sun, 6 Jul 2014 17:36:31 +0200 Subject: [PATCH 04/10] vim: move disptick_T from vim.h to syntax_defs.h Make vim.h smaller, bit by bit. --- src/nvim/globals.h | 1 + src/nvim/syntax.c | 1 + src/nvim/syntax_defs.h | 2 ++ src/nvim/vim.h | 2 -- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/nvim/globals.h b/src/nvim/globals.h index cdfa882a3e..1f402f3ef9 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -13,6 +13,7 @@ #include "nvim/ex_eval.h" #include "nvim/mbyte.h" #include "nvim/menu.h" +#include "nvim/syntax_defs.h" /* * definition of global variables diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index 51aeda7293..7dd3453d16 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -42,6 +42,7 @@ #include "nvim/regexp.h" #include "nvim/screen.h" #include "nvim/strings.h" +#include "nvim/syntax_defs.h" #include "nvim/term.h" #include "nvim/ui.h" #include "nvim/os/os.h" diff --git a/src/nvim/syntax_defs.h b/src/nvim/syntax_defs.h index d8ef007ffc..11e342f870 100644 --- a/src/nvim/syntax_defs.h +++ b/src/nvim/syntax_defs.h @@ -9,6 +9,8 @@ # define SST_DIST 16 /* normal distance between entries */ # define SST_INVALID (synstate_T *)-1 /* invalid syn_state pointer */ +typedef unsigned short disptick_T; /* display tick type */ + /* struct passed to in_id_list() */ struct sp_syn { int inc_tag; /* ":syn include" unique tag */ diff --git a/src/nvim/vim.h b/src/nvim/vim.h index 5cde6e7989..6bdc58c54a 100644 --- a/src/nvim/vim.h +++ b/src/nvim/vim.h @@ -355,8 +355,6 @@ enum { #define PERROR(msg) \ (void) emsg3((char_u *) "%s: %s", (char_u *)msg, (char_u *)strerror(errno)) -typedef unsigned short disptick_T; /* display tick type */ - #define SHOWCMD_COLS 10 /* columns needed by shown command */ #define STL_MAX_ITEM 80 /* max nr of % in statusline */ From 109c70dc60b763362923549beeef312324fefba4 Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sat, 12 Jul 2014 14:56:04 +0200 Subject: [PATCH 05/10] test/preprocess: always declare EXTERN Unit tests never need to declare globals, only access them. In the main code base this is handled by including "vim.h". If a file wants to declare globals (in the case of neovim that's only main.c), it #define's EXTERN and includes "vim.h". Otherwise, a file just includes "vim.h" (that's the majority case). Since we want to be able to run unit tests without including "vim.h", we predefine "EXTERN" to mean extern. That way, we don't have to include "vim.h". --- test/unit/preprocess.moon | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/unit/preprocess.moon b/test/unit/preprocess.moon index aa74b8ce24..cb734da2f7 100644 --- a/test/unit/preprocess.moon +++ b/test/unit/preprocess.moon @@ -85,6 +85,8 @@ class Gcc '-D "__asm(ARGS)="', '-D "__asm__(ARGS)="', '-D "__inline__="', + '-D "EXTERN=extern"', + '-D "INIT(...)="', '-D_GNU_SOURCE', '-DINCLUDE_GENERATED_DECLARATIONS' } From 1710fa43376f4844abe056b3f87aece1845ff89a Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sat, 12 Jul 2014 19:16:41 +0200 Subject: [PATCH 06/10] vim: move vim_acl_T to types.h Also include "types.h" in os_unix.h because it declares functions that return vim_acl_T. --- src/nvim/fileio.c | 1 + src/nvim/os_unix.c | 1 + src/nvim/os_unix.h | 1 + src/nvim/types.h | 3 +++ src/nvim/undo.c | 1 + src/nvim/vim.h | 2 -- 6 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index c867211a66..9b5df80a0c 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -51,6 +51,7 @@ #include "nvim/strings.h" #include "nvim/tempfile.h" #include "nvim/term.h" +#include "nvim/types.h" #include "nvim/ui.h" #include "nvim/undo.h" #include "nvim/window.h" diff --git a/src/nvim/os_unix.c b/src/nvim/os_unix.c index 4a0fbf5c18..9e7940bc2a 100644 --- a/src/nvim/os_unix.c +++ b/src/nvim/os_unix.c @@ -54,6 +54,7 @@ #include "nvim/syntax.h" #include "nvim/tempfile.h" #include "nvim/term.h" +#include "nvim/types.h" #include "nvim/ui.h" #include "nvim/os/os.h" #include "nvim/os/time.h" diff --git a/src/nvim/os_unix.h b/src/nvim/os_unix.h index 5610fe68eb..5a3eb84ba4 100644 --- a/src/nvim/os_unix.h +++ b/src/nvim/os_unix.h @@ -1,6 +1,7 @@ #ifndef NVIM_OS_UNIX_H #define NVIM_OS_UNIX_H +#include "nvim/types.h" // for vim_acl_T #include "nvim/os/shell.h" /* Values returned by mch_nodetype() */ diff --git a/src/nvim/types.h b/src/nvim/types.h index a3c4509756..ad905aa95b 100644 --- a/src/nvim/types.h +++ b/src/nvim/types.h @@ -10,6 +10,9 @@ #include +// dummy to pass an ACL to a function +typedef void *vim_acl_T; + // Make sure long_u is big enough to hold a pointer. // On Win64, longs are 32 bits and pointers are 64 bits. // For printf() and scanf(), we need to take care of long_u specifically. diff --git a/src/nvim/undo.c b/src/nvim/undo.c index b9f3309cef..96b83a3e2d 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -107,6 +107,7 @@ #include "nvim/screen.h" #include "nvim/sha256.h" #include "nvim/strings.h" +#include "nvim/types.h" #include "nvim/os/os.h" #include "nvim/os/time.h" diff --git a/src/nvim/vim.h b/src/nvim/vim.h index 6bdc58c54a..e324a8bedc 100644 --- a/src/nvim/vim.h +++ b/src/nvim/vim.h @@ -358,8 +358,6 @@ enum { #define SHOWCMD_COLS 10 /* columns needed by shown command */ #define STL_MAX_ITEM 80 /* max nr of % in statusline */ -typedef void *vim_acl_T; /* dummy to pass an ACL to a function */ - /* * fnamecmp() is used to compare file names. * On some systems case in a file name does not matter, on others it does. From 32ddfec84f9335673e38b31950544a24304697f8 Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sat, 12 Jul 2014 19:35:57 +0200 Subject: [PATCH 07/10] memory.h: don't include vim.h in header files Also include stdint.h in khash.h. It was transitively included by vim.h via memory.h before. khash.h accidentally relied on that. --- src/nvim/lib/khash.h | 1 + src/nvim/memory.h | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nvim/lib/khash.h b/src/nvim/lib/khash.h index f706e994d5..c9198e048c 100644 --- a/src/nvim/lib/khash.h +++ b/src/nvim/lib/khash.h @@ -128,6 +128,7 @@ int main() { #include #include #include +#include #include "nvim/memory.h" diff --git a/src/nvim/memory.h b/src/nvim/memory.h index 3a05797e89..4ff31ff732 100644 --- a/src/nvim/memory.h +++ b/src/nvim/memory.h @@ -1,8 +1,7 @@ #ifndef NVIM_MEMORY_H #define NVIM_MEMORY_H -#include -#include "nvim/vim.h" +#include // for size_t #ifdef INCLUDE_GENERATED_DECLARATIONS # include "memory.h.generated.h" From c376cf46bf7672772d1b5fec218c44c9fb6cd2de Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sat, 12 Jul 2014 19:37:38 +0200 Subject: [PATCH 08/10] test/helpers: add 'vim_init' helper - Initializes some global variables. - Necessary for the buffer tests in PR #904. --- test/unit/helpers.moon | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/unit/helpers.moon b/test/unit/helpers.moon index 84dfeb20e9..438f36811c 100644 --- a/test/unit/helpers.moon +++ b/test/unit/helpers.moon @@ -97,6 +97,17 @@ cstr = ffi.typeof 'char[?]' to_cstr = (string) -> cstr (string.len string) + 1, string +export vim_init_called +-- initialize some global variables, this is still necessary to unit test +-- functions that rely on global state. +vim_init = -> + if vim_init_called ~= nil + return + -- import os_unix.h for mch_early_init(), which initializes some globals + os = cimport './src/nvim/os_unix.h' + os.mch_early_init! + vim_init_called = true + return { cimport: cimport cppimport: cppimport @@ -107,4 +118,5 @@ return { lib: libnvim cstr: cstr to_cstr: to_cstr + vim_init: vim_init } From 06ca70b191229da4d827d3751fa18ffed17c0189 Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sat, 12 Jul 2014 19:38:36 +0200 Subject: [PATCH 09/10] test/fs: move tests of path_full_dir_name to path Move tests of path_full_dir_name to path_spec. It is only defined in path.h. Not sure why this works most of the time (I can only trigger a failure when running under lldb). It's a more logical place to have the test as well. --- test/unit/os/fs_spec.moon | 31 ------------------------------- test/unit/path_spec.moon | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/test/unit/os/fs_spec.moon b/test/unit/os/fs_spec.moon index a1445aeb0c..6d87cb95df 100644 --- a/test/unit/os/fs_spec.moon +++ b/test/unit/os/fs_spec.moon @@ -57,37 +57,6 @@ describe 'fs function', -> buf = cstr (len-1), '' eq FAIL, (os_dirname buf, (len-1)) - describe 'path_full_dir_name', -> - path_full_dir_name = (directory, buffer, len) -> - directory = to_cstr directory - fs.path_full_dir_name directory, buffer, len - - before_each -> - -- Create empty string buffer which will contain the resulting path. - export len = (string.len lfs.currentdir!) + 22 - export buffer = cstr len, '' - - it 'returns the absolute directory name of a given relative one', -> - result = path_full_dir_name '..', buffer, len - eq OK, result - old_dir = lfs.currentdir! - lfs.chdir '..' - expected = lfs.currentdir! - lfs.chdir old_dir - eq expected, (ffi.string buffer) - - it 'returns the current directory name if the given string is empty', -> - eq OK, (path_full_dir_name '', buffer, len) - eq lfs.currentdir!, (ffi.string buffer) - - it 'fails if the given directory does not exist', -> - eq FAIL, path_full_dir_name('does_not_exist', buffer, len) - - it 'works with a normal relative dir', -> - result = path_full_dir_name('unit-test-directory', buffer, len) - eq lfs.currentdir! .. '/unit-test-directory', (ffi.string buffer) - eq OK, result - os_isdir = (name) -> fs.os_isdir (to_cstr name) diff --git a/test/unit/path_spec.moon b/test/unit/path_spec.moon index 762bad09a2..4a4170e463 100644 --- a/test/unit/path_spec.moon +++ b/test/unit/path_spec.moon @@ -10,6 +10,43 @@ OK = 1 FAIL = 0 describe 'path function', -> + describe 'path_full_dir_name', -> + setup -> + lfs.mkdir 'unit-test-directory' + + teardown -> + lfs.rmdir 'unit-test-directory' + + path_full_dir_name = (directory, buffer, len) -> + directory = to_cstr directory + path.path_full_dir_name directory, buffer, len + + before_each -> + -- Create empty string buffer which will contain the resulting path. + export len = (string.len lfs.currentdir!) + 22 + export buffer = cstr len, '' + + it 'returns the absolute directory name of a given relative one', -> + result = path_full_dir_name '..', buffer, len + eq OK, result + old_dir = lfs.currentdir! + lfs.chdir '..' + expected = lfs.currentdir! + lfs.chdir old_dir + eq expected, (ffi.string buffer) + + it 'returns the current directory name if the given string is empty', -> + eq OK, (path_full_dir_name '', buffer, len) + eq lfs.currentdir!, (ffi.string buffer) + + it 'fails if the given directory does not exist', -> + eq FAIL, path_full_dir_name('does_not_exist', buffer, len) + + it 'works with a normal relative dir', -> + result = path_full_dir_name('unit-test-directory', buffer, len) + eq lfs.currentdir! .. '/unit-test-directory', (ffi.string buffer) + eq OK, result + describe 'path_full_compare', -> path_full_compare = (s1, s2, cn) -> From 7c6079f6f0d0fc59dd747c2ecd9e1e1ca1e0e66d Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Sat, 12 Jul 2014 20:13:56 +0200 Subject: [PATCH 10/10] vim: include used definitions in headers This is not an exhaustive commit, it merely ameliorates the situations a bit. There are quite a few header files that don't include all the types they use in their function/struct/... definitions. This throws of the testing infrastructure (but is not such a problem for the main binary that has the "tumbleweed of includes"-phenomenon). --- src/nvim/buffer.h | 1 + src/nvim/buffer_defs.h | 4 +++- src/nvim/ex_eval.h | 1 + src/nvim/message.h | 1 + src/nvim/normal.h | 1 + src/nvim/undo_defs.h | 2 ++ 6 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/nvim/buffer.h b/src/nvim/buffer.h index 493ebffc63..a8220c65a0 100644 --- a/src/nvim/buffer.h +++ b/src/nvim/buffer.h @@ -2,6 +2,7 @@ #define NVIM_BUFFER_H #include "nvim/pos.h" // for linenr_T +#include "nvim/ex_cmds_defs.h" // for exarg_T /* Values for buflist_getfile() */ #define GETF_SETMARK 0x01 /* set pcmark before jumping */ diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index 9d81388a3c..e827642d8a 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -2,10 +2,12 @@ #define NVIM_BUFFER_DEFS_H #include +// for FILE +#include // for garray_T #include "nvim/garray.h" -// for pos_T and lpos_T +// for pos_T, lpos_T and linenr_T #include "nvim/pos.h" // for the number window-local and buffer-local options #include "nvim/option_defs.h" diff --git a/src/nvim/ex_eval.h b/src/nvim/ex_eval.h index 3f5e295c18..30871c7711 100644 --- a/src/nvim/ex_eval.h +++ b/src/nvim/ex_eval.h @@ -2,6 +2,7 @@ #define NVIM_EX_EVAL_H #include "nvim/pos.h" // for linenr_T +#include "nvim/ex_cmds_defs.h" // for exarg_T /* * A list used for saving values of "emsg_silent". Used by ex_try() to save the diff --git a/src/nvim/message.h b/src/nvim/message.h index f04005a7ad..c620597f33 100644 --- a/src/nvim/message.h +++ b/src/nvim/message.h @@ -2,6 +2,7 @@ #define NVIM_MESSAGE_H #include +#include "nvim/eval_defs.h" // for typval_T /* * Types of dialogs passed to do_dialog(). diff --git a/src/nvim/normal.h b/src/nvim/normal.h index 341da6d473..599f4771b9 100644 --- a/src/nvim/normal.h +++ b/src/nvim/normal.h @@ -2,6 +2,7 @@ #define NVIM_NORMAL_H #include "nvim/pos.h" +#include "nvim/buffer_defs.h" // for win_T /* Values for find_ident_under_cursor() */ #define FIND_IDENT 1 /* find identifier (word) */ diff --git a/src/nvim/undo_defs.h b/src/nvim/undo_defs.h index 6263dd91d4..2579f13b93 100644 --- a/src/nvim/undo_defs.h +++ b/src/nvim/undo_defs.h @@ -1,6 +1,8 @@ #ifndef NVIM_UNDO_DEFS_H #define NVIM_UNDO_DEFS_H +#include // for time_t + #include "nvim/pos.h" /* Structure to store info about the Visual area. */