#! /bin/sh

t=__wt.$$
trap 'rm -f $t' 0 1 2 3 13 15

cd ..

# Parse a C file, discarding functions that don't return an int, and formatting
# the remaining functions as a single line.
file_parse()
{
	sed -n \
	    -e '/^int$/b loop' \
	    -e '/^static int$/b loop' \
	    -e '/^static inline int$/b loop' \
	    -e 'd' \
	    -e ': loop' \
	    -e 'H' \
	    -e 'n' \
	    -e '/^}$/!b loop' \
	    -e 'H' \
	    -e 'x' \
	    -e 's/\n/ /g' \
	    -e 'p' \
	    -e 's/.*//' \
	    -e 'x' \
	    -e 'd' $1
}

# Strip out a list of functions that will be flagged, but are OK.
func_ok()
{
	sed \
	    -e '/int __bm_stat$/d' \
	    -e '/int __checkpoint_presync$/d' \
	    -e '/int __compact_uri_analyze$/d' \
	    -e '/int __config_parser_close$/d' \
	    -e '/int __curlog_reset$/d' \
	    -e '/int __cursor_fix_implicit$/d' \
	    -e '/int __handle_close_default$/d' \
	    -e '/int __handle_progress_default$/d' \
	    -e '/int __im_file_close$/d' \
	    -e '/int __im_file_lock$/d' \
	    -e '/int __im_file_size$/d' \
	    -e '/int __im_file_sync$/d' \
	    -e '/int __im_fs_directory_list_free$/d' \
	    -e '/int __im_fs_exist$/d' \
	    -e '/int __page_write_gen_wrapped_check$/d' \
	    -e '/int __posix_terminate$/d' \
	    -e '/int __rec_destroy_session$/d' \
	    -e '/int __tree_walk_skip_count_callback$/d' \
	    -e '/int __txn_rollback_to_stable_custom_skip$/d' \
	    -e '/int __win_terminate$/d' \
	    -e '/int __wt_block_compact_end$/d' \
	    -e '/int __wt_block_compact_start$/d' \
	    -e '/int __wt_block_manager_size$/d' \
	    -e '/int __wt_block_write_size$/d' \
	    -e '/int __wt_curjoin_joined$/d' \
	    -e '/int __wt_cursor_close$/d' \
	    -e '/int __wt_cursor_noop$/d' \
	    -e '/int __wt_epoch$/d' \
	    -e '/int __wt_errno$/d' \
	    -e '/int __wt_get_vm_pagesize$/d' \
	    -e '/int __wt_lsm_manager_pop_entry$/d' \
	    -e '/int __wt_once$/d' \
	    -e '/int __wt_posix_directory_list_free$/d' \
	    -e '/int __wt_session_breakpoint$/d' \
	    -e '/int __wt_spin_init$/d' \
	    -e '/int __wt_spin_trylock$/d' \
	    -e '/int __wt_stat_connection_desc$/d' \
	    -e '/int __wt_stat_dsrc_desc$/d' \
	    -e '/int __wt_stat_join_desc$/d' \
	    -e '/int __wt_txn_rollback_required$/d' \
	    -e '/int __wt_win_directory_list_free$/d' \
	    -e '/int bdb_compare_reverse$/d' \
	    -e '/int copyout_val$/d' \
	    -e '/int csv_error$/d' \
	    -e '/int csv_terminate$/d' \
	    -e '/int demo_file_close$/d' \
	    -e '/int demo_file_lock$/d' \
	    -e '/int demo_file_size$/d' \
	    -e '/int demo_file_sync$/d' \
	    -e '/int demo_fs_directory_list_free$/d' \
	    -e '/int demo_fs_exist$/d' \
	    -e '/int fail_file_lock$/d' \
	    -e '/int fail_file_sync$/d' \
	    -e '/int fail_fs_directory_list_free$/d' \
	    -e '/int fail_fs_exist$/d' \
	    -e '/int fail_fs_simulate_fail$/d' \
	    -e '/int fail_fs_terminate$/d' \
	    -e '/int handle_message$/d' \
	    -e '/int handle_progress$/d' \
	    -e '/int helium_cursor_reserve$/d' \
	    -e '/int helium_cursor_reset$/d' \
	    -e '/int helium_session_verify$/d' \
	    -e '/int index_compare_S$/d' \
	    -e '/int index_compare_primary$/d' \
	    -e '/int index_compare_u$/d' \
	    -e '/int index_extractor_u$/d' \
	    -e '/int log_print_err$/d' \
	    -e '/int lz4_error$/d' \
	    -e '/int lz4_pre_size$/d' \
	    -e '/int lz4_terminate$/d' \
	    -e '/int main$/d' \
	    -e '/int nop_decompress$/d' \
	    -e '/int nop_decrypt$/d' \
	    -e '/int nop_error$/d' \
	    -e '/int nop_pre_size$/d' \
	    -e '/int nop_sizing$/d' \
	    -e '/int nop_terminate$/d' \
	    -e '/int os_errno$/d' \
	    -e '/int revint_terminate$/d' \
	    -e '/int rotn_error$/d' \
	    -e '/int rotn_sizing$/d' \
	    -e '/int rotn_terminate$/d' \
	    -e '/int snappy_pre_size$/d' \
	    -e '/int snappy_terminate$/d' \
	    -e '/int subtest_error_handler$/d' \
	    -e '/int uri2name$/d' \
	    -e '/int usage$/d' \
	    -e '/int util_err$/d' \
	    -e '/int wiredtiger_extension_init$/d' \
	    -e '/int wiredtiger_extension_terminate$/d' \
	    -e '/int wiredtiger_pack_close$/d' \
	    -e '/int zlib_error$/d' \
	    -e '/int zlib_terminate$/d' \
	    -e '/int zstd_error$/d' \
	    -e '/int zstd_pre_size$/d' \
	    -e '/int zstd_terminate$/d'
}

for f in `find bench ext src test -name '*.[ci]'`; do
	if expr "$f" : '.*/windows_shim.c' > /dev/null; then
		continue
	fi

	# Complain about functions which return an "int" but which don't return
	# except at the end of the function.
	#
	# Turn each function into a single line, then discard the function's
	# final "return" call, then discard any function that still has some
	# form of return assignment or call.
	file_parse $f |
	sed -e 's/return ([^)]*); }$//' \
	    -e '/[_A-Z]*_API_CALL[_A-Z]*(/d' \
	    -e '/WT_CURSOR_NEEDKEY(/d' \
	    -e '/WT_CURSOR_NEEDVALUE(/d' \
	    -e '/WT_ERR[A-Z_]*(/d' \
	    -e '/WT_ILLEGAL_VALUE[A-Z_]*(/d' \
	    -e '/WT_PANIC[A-Z_]*(/d' \
	    -e '/WT_RET[A-Z_]*(/d' \
	    -e '/WT_SIZE_CHECK_PACK(/d' \
	    -e '/WT_SIZE_CHECK_UNPACK(/d' \
	    -e '/WT_SYSCALL(/d' \
	    -e '/WT_TRET(/d' \
	    -e '/[^a-z_]ret = /d' \
	    -e '/[^a-z_]return (/d' \
	    -e 's/^\([^(]*\).*/\1/' \
	    -e 's/^ *//' |
	func_ok > $t
	test -s $t && {
		echo "=============================================="
		echo "$f:"
		cat $t | sed 's/^/	/'
		echo "Function could return void instead of int."
		echo "Add false positives to the list in dist/s_void."
		echo "=============================================="
	}

	# Complain about functions which declare a "ret" value but never use it.
	file_parse $f |
	grep 'WT_DECL_RET' |
	sed -e '/ret =/d' \
	    -e '/[_A-Z]*_API_CALL[_A-Z]*(/d' \
	    -e '/WT_CURSOR_NEEDKEY/d' \
	    -e '/WT_CURSOR_NEEDVALUE/d' \
	    -e '/WT_ERR/d' \
	    -e '/WT_ILLEGAL_VALUE_ERR/d' \
	    -e '/WT_SYSCALL.*ret/d' \
	    -e '/WT_TRET/d' \
	    -e 's/^\([^(]*\).*/\1/' \
	    -e 's/^ *//' > $t
	test -s $t && {
		echo "=============================================="
		echo "$f:"
		cat $t | sed 's/^/	/'
		echo "Function declares ret without using it."
		echo "Add false positives to the list in dist/s_void."
		echo "=============================================="
	}
done

exit 0
