build: add check to clint to prevent non-defs header includes

Also enable iwyu on headers, but add an ignore for each file separately.

Work on https://github.com/neovim/neovim/issues/6371.
This commit is contained in:
dundargoc 2023-11-26 13:24:38 +01:00 committed by dundargoc
parent 17d81ac2ab
commit ce6075f82a
2 changed files with 392 additions and 1 deletions

201
Makefile
View File

@ -139,7 +139,206 @@ test: $(TEST)
iwyu: build/.ran-cmake iwyu: build/.ran-cmake
cmake --preset iwyu cmake --preset iwyu
cmake --build build > build/iwyu.log cmake --build build > build/iwyu.log
iwyu-fix-includes --only_re="src/nvim" --ignore_re="src/nvim/(auto|map.h|eval/encode.c)" --safe_headers < build/iwyu.log iwyu-fix-includes --only_re="src/nvim" --ignore_re="(src/nvim/eval/encode.c|src/nvim/auto/\
|src/nvim/api/autocmd.h\
|src/nvim/api/buffer.h\
|src/nvim/api/command.h\
|src/nvim/api/deprecated.h\
|src/nvim/api/extmark.h\
|src/nvim/api/keysets.h\
|src/nvim/api/options.h\
|src/nvim/api/private/converter.h\
|src/nvim/api/private/defs.h\
|src/nvim/api/private/dispatch.h\
|src/nvim/api/private/helpers.h\
|src/nvim/api/private/validate.h\
|src/nvim/api/tabpage.h\
|src/nvim/api/ui.h\
|src/nvim/api/vim.h\
|src/nvim/api/vimscript.h\
|src/nvim/api/win_config.h\
|src/nvim/api/window.h\
|src/nvim/arabic.h\
|src/nvim/arglist.h\
|src/nvim/arglist_defs.h\
|src/nvim/ascii.h\
|src/nvim/assert.h\
|src/nvim/autocmd.c\
|src/nvim/autocmd.h\
|src/nvim/base64.h\
|src/nvim/buffer.h\
|src/nvim/buffer_defs.h\
|src/nvim/buffer_updates.h\
|src/nvim/bufwrite.h\
|src/nvim/change.h\
|src/nvim/channel.h\
|src/nvim/charset.h\
|src/nvim/cmdexpand.h\
|src/nvim/cmdexpand_defs.h\
|src/nvim/cmdhist.h\
|src/nvim/context.h\
|src/nvim/cursor.h\
|src/nvim/cursor_shape.h\
|src/nvim/debugger.h\
|src/nvim/decoration.h\
|src/nvim/decoration_defs.h\
|src/nvim/decoration_provider.h\
|src/nvim/diff.h\
|src/nvim/digraph.h\
|src/nvim/drawline.h\
|src/nvim/drawscreen.h\
|src/nvim/edit.h\
|src/nvim/eval.h\
|src/nvim/eval/buffer.h\
|src/nvim/eval/decode.h\
|src/nvim/eval/encode.h\
|src/nvim/eval/executor.h\
|src/nvim/eval/funcs.h\
|src/nvim/eval/gc.h\
|src/nvim/eval/typval.h\
|src/nvim/eval/typval_defs.h\
|src/nvim/eval/typval_encode.h\
|src/nvim/eval/userfunc.h\
|src/nvim/eval/vars.h\
|src/nvim/eval/window.h\
|src/nvim/event/libuv_process.h\
|src/nvim/event/loop.h\
|src/nvim/event/multiqueue.h\
|src/nvim/event/process.h\
|src/nvim/event/rstream.h\
|src/nvim/event/signal.h\
|src/nvim/event/socket.h\
|src/nvim/event/stream.h\
|src/nvim/event/time.h\
|src/nvim/event/wstream.h\
|src/nvim/ex_cmds.h\
|src/nvim/ex_cmds2.h\
|src/nvim/ex_cmds_defs.h\
|src/nvim/ex_docmd.h\
|src/nvim/ex_eval.h\
|src/nvim/ex_eval_defs.h\
|src/nvim/ex_getln.h\
|src/nvim/ex_session.h\
|src/nvim/extmark.h\
|src/nvim/extmark_defs.h\
|src/nvim/file_search.h\
|src/nvim/fileio.h\
|src/nvim/fold.h\
|src/nvim/fold_defs.h\
|src/nvim/garray.h\
|src/nvim/getchar.h\
|src/nvim/getchar_defs.h\
|src/nvim/globals.h\
|src/nvim/grid.h\
|src/nvim/grid_defs.h\
|src/nvim/hashtab.h\
|src/nvim/help.h\
|src/nvim/highlight.h\
|src/nvim/highlight_defs.h\
|src/nvim/highlight_group.h\
|src/nvim/iconv.h\
|src/nvim/indent.h\
|src/nvim/indent_c.h\
|src/nvim/input.h\
|src/nvim/insexpand.h\
|src/nvim/keycodes.h\
|src/nvim/linematch.h\
|src/nvim/log.h\
|src/nvim/lua/base64.h\
|src/nvim/lua/converter.h\
|src/nvim/lua/executor.h\
|src/nvim/lua/secure.h\
|src/nvim/lua/spell.h\
|src/nvim/lua/stdlib.h\
|src/nvim/lua/treesitter.h\
|src/nvim/lua/xdiff.h\
|src/nvim/macros.h\
|src/nvim/main.h\
|src/nvim/map.h\
|src/nvim/mapping.h\
|src/nvim/mapping_defs.h\
|src/nvim/mark.h\
|src/nvim/mark_defs.h\
|src/nvim/marktree.h\
|src/nvim/match.h\
|src/nvim/mbyte.h\
|src/nvim/mbyte_defs.h\
|src/nvim/memfile.h\
|src/nvim/memfile_defs.h\
|src/nvim/memline.h\
|src/nvim/memory.h\
|src/nvim/menu.h\
|src/nvim/message.h\
|src/nvim/mouse.h\
|src/nvim/move.h\
|src/nvim/msgpack_rpc/channel.h\
|src/nvim/msgpack_rpc/channel_defs.h\
|src/nvim/msgpack_rpc/helpers.h\
|src/nvim/msgpack_rpc/server.h\
|src/nvim/msgpack_rpc/unpacker.h\
|src/nvim/normal.h\
|src/nvim/nvim/extmark.h\
|src/nvim/ops.h\
|src/nvim/option.h\
|src/nvim/option_defs.h\
|src/nvim/option_vars.h\
|src/nvim/optionstr.h\
|src/nvim/os/dl.h\
|src/nvim/os/fileio.h\
|src/nvim/os/fs.h\
|src/nvim/os/input.h\
|src/nvim/os/lang.h\
|src/nvim/os/os.h\
|src/nvim/os/process.h\
|src/nvim/os/pty_conpty_win.h\
|src/nvim/os/pty_process_unix.h\
|src/nvim/os/pty_process_win.h\
|src/nvim/os/shell.h\
|src/nvim/os/time.h\
|src/nvim/os/tty.h\
|src/nvim/path.h\
|src/nvim/plines.h\
|src/nvim/popupmenu.h\
|src/nvim/profile.h\
|src/nvim/quickfix.h\
|src/nvim/regexp.h\
|src/nvim/regexp_defs.h\
|src/nvim/runtime.h\
|src/nvim/search.h\
|src/nvim/sha256.h\
|src/nvim/shada.h\
|src/nvim/sign.h\
|src/nvim/sign_defs.h\
|src/nvim/spell.h\
|src/nvim/spell_defs.h\
|src/nvim/spellfile.h\
|src/nvim/spellsuggest.h\
|src/nvim/state.h\
|src/nvim/statusline.h\
|src/nvim/statusline_defs.h\
|src/nvim/strings.h\
|src/nvim/syntax.h\
|src/nvim/tag.h\
|src/nvim/terminal.h\
|src/nvim/testing.h\
|src/nvim/textformat.h\
|src/nvim/textobject.h\
|src/nvim/tui/input.h\
|src/nvim/tui/terminfo.h\
|src/nvim/tui/tui.h\
|src/nvim/ugrid.h\
|src/nvim/ui.h\
|src/nvim/ui_client.h\
|src/nvim/ui_compositor.h\
|src/nvim/undo.h\
|src/nvim/undo_defs.h\
|src/nvim/usercmd.h\
|src/nvim/version.h\
|src/nvim/vim.h\
|src/nvim/viml/parser/expressions.h\
|src/nvim/viml/parser/parser.h\
|src/nvim/window.h\
|src/nvim/winfloat.h)" --nosafe_headers < build/iwyu.log
cmake -B build -U ENABLE_IWYU cmake -B build -U ENABLE_IWYU
cmake --build build cmake --build build

View File

@ -151,6 +151,7 @@ Syntax: clint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
_ERROR_CATEGORIES = [ _ERROR_CATEGORIES = [
'build/endif_comment', 'build/endif_comment',
'build/header_guard', 'build/header_guard',
'build/include_defs',
'build/printf_format', 'build/printf_format',
'build/storage_class', 'build/storage_class',
'readability/bool', 'readability/bool',
@ -879,6 +880,196 @@ def CheckForHeaderGuard(filename, lines, error):
error(filename, 0, 'build/header_guard', 5, error(filename, 0, 'build/header_guard', 5,
'No "#pragma once" found in header') 'No "#pragma once" found in header')
def CheckIncludes(filename, lines, error):
"""Checks that headers only include _defs headers
Args:
filename: The name of the C++ header file.
lines: An array of strings, each representing a line of the file.
error: The function to call with any errors found.
"""
if filename.endswith('.c.h') or filename.endswith('.in.h') or FileInfo(filename).RelativePath() in {
'func_attr.h',
}:
return
check_includes_ignore = [
"api/command.h",
"api/deprecated.h",
"api/extmark.h",
"api/keysets.h",
"api/options.h",
"api/private/converter.h",
"api/private/defs.h",
"api/private/dispatch.h",
"api/private/helpers.h",
"api/private/validate.h",
"api/tabpage.h",
"api/ui.h",
"api/vim.h",
"api/vimscript.h",
"api/win_config.h",
"arglist.h",
"arglist_defs.h",
"ascii.h",
"assert.h",
"autocmd.h",
"buffer.h",
"buffer_defs.h",
"buffer_updates.h",
"change.h",
"channel.h",
"charset.h",
"cmdexpand.h",
"cmdexpand_defs.h",
"cmdhist.h",
"context.h",
"cursor.h",
"cursor_shape.h",
"decoration.h",
"decoration_defs.h",
"decoration_provider.h",
"diff.h",
"digraph.h",
"drawline.h",
"drawscreen.h",
"edit.h",
"eval.h",
"eval/decode.h",
"eval/encode.h",
"eval/funcs.h",
"eval/typval.h",
"eval/typval_defs.h",
"eval/typval_encode.h",
"eval/userfunc.h",
"event/libuv_process.h",
"event/loop.h",
"event/multiqueue.h",
"event/process.h",
"event/rstream.h",
"event/signal.h",
"event/socket.h",
"event/stream.h",
"event/time.h",
"event/wstream.h",
"ex_cmds.h",
"ex_cmds_defs.h",
"ex_docmd.h",
"ex_eval_defs.h",
"ex_getln.h",
"extmark_defs.h",
"file_search.h",
"fileio.h",
"fold.h",
"fold_defs.h",
"garray.h",
"getchar.h",
"getchar_defs.h",
"globals.h",
"grid.h",
"grid_defs.h",
"hashtab.h",
"highlight.h",
"highlight_defs.h",
"highlight_group.h",
"iconv.h",
"indent.h",
"indent_c.h",
"input.h",
"insexpand.h",
"keycodes.h",
"linematch.h",
"log.h",
"lua/converter.h",
"lua/executor.h",
"lua/treesitter.h",
"macros.h",
"main.h",
"map.h",
"mapping.h",
"mapping_defs.h",
"mark.h",
"mark_defs.h",
"marktree.h",
"mbyte.h",
"mbyte_defs.h",
"memfile_defs.h",
"memline.h",
"memory.h",
"menu.h",
"message.h",
"mouse.h",
"move.h",
"msgpack_rpc/channel_defs.h",
"msgpack_rpc/helpers.h",
"msgpack_rpc/unpacker.h",
"normal.h",
"nvim/extmark.h",
"ops.h",
"option.h",
"option_defs.h",
"option_vars.h",
"os/fileio.h",
"os/fs.h",
"os/input.h",
"os/lang.h",
"os/os.h",
"os/process.h",
"os/pty_conpty_win.h",
"os/pty_process_unix.h",
"os/pty_process_win.h",
"os/shell.h",
"path.h",
"plines.h",
"popupmenu.h",
"profile.h",
"quickfix.h",
"regexp.h",
"regexp_defs.h",
"runtime.h",
"search.h",
"sha256.h",
"sign_defs.h",
"spell.h",
"spell_defs.h",
"spellfile.h",
"spellsuggest.h",
"statusline.h",
"statusline_defs.h",
"strings.h",
"syntax.h",
"tag.h",
"textformat.h",
"textobject.h",
"tui/input.h",
"tui/terminfo.h",
"tui/tui.h",
"ugrid.h",
"ui.h",
"ui_client.h",
"ui_compositor.h",
"undo_defs.h",
"usercmd.h",
"version.h",
"vim.h",
"viml/parser/expressions.h",
"viml/parser/parser.h",
"window.h",
"winfloat.h",
]
for i in check_includes_ignore:
if filename.endswith(i):
return
for i, line in enumerate(lines):
if("#include" in line):
if("#include <" in line):
continue
if "_defs" not in line:
error(filename, i, 'build/include_defs', 5,
'Headers should not include non-"_defs" headers')
def CheckForBadCharacters(filename, lines, error): def CheckForBadCharacters(filename, lines, error):
"""Logs an error for each line containing bad characters. """Logs an error for each line containing bad characters.
@ -2166,6 +2357,7 @@ def ProcessFileData(filename, file_extension, lines, error,
if file_extension == 'h': if file_extension == 'h':
CheckForHeaderGuard(filename, lines, error) CheckForHeaderGuard(filename, lines, error)
CheckIncludes(filename, lines, error)
RemoveMultiLineComments(filename, lines, error) RemoveMultiLineComments(filename, lines, error)
clean_lines = CleansedLines(lines, init_lines) clean_lines = CleansedLines(lines, init_lines)