2018-09-29 11:38:59 +08:00
|
|
|
#!/usr/bin/env fish
|
|
|
|
|
|
|
|
# Finds global variables by parsing the output of 'nm'
|
|
|
|
# for object files in this directory.
|
|
|
|
# This was written for macOS nm.
|
|
|
|
|
2020-05-15 13:56:06 +08:00
|
|
|
set -l FISH_SOURCE_DIR $argv[1]
|
2019-04-29 02:52:56 +08:00
|
|
|
if not test -d "$FISH_SOURCE_DIR"
|
|
|
|
echo "FISH_SOURCE_DIR not given"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
set -g whitelist \
|
|
|
|
# unclear what this is \
|
|
|
|
l_constinit \
|
|
|
|
# hacks to work around missing ncurses strings on mac \
|
2021-11-23 11:55:15 +08:00
|
|
|
sitm_esc ritm_esc dim_esc
|
2019-04-29 02:52:56 +08:00
|
|
|
|
|
|
|
# In our nm regex, we are interested in data (dD) and bss (bB) segments.
|
|
|
|
set -g nm_regex '^([^ ]+) ([dDbB])'
|
|
|
|
|
2020-05-15 13:56:06 +08:00
|
|
|
set -l total_globals 0
|
|
|
|
set -l boring_files \
|
2018-09-29 12:58:44 +08:00
|
|
|
fish_key_reader.cpp.o \
|
|
|
|
fish_tests.cpp.o \
|
2021-11-23 11:55:15 +08:00
|
|
|
fish_indent.cpp.o
|
2018-09-29 12:58:44 +08:00
|
|
|
|
2019-04-29 02:52:56 +08:00
|
|
|
# return if we should ignore the given symbol name
|
|
|
|
function should_ignore
|
2020-05-15 13:56:06 +08:00
|
|
|
set -l symname $argv[1]
|
2019-04-29 02:52:56 +08:00
|
|
|
string match -q '*guard variable for*' $symname
|
|
|
|
and return 0
|
|
|
|
contains $symname $whitelist
|
|
|
|
and return 0
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# echo a cleaned-up symbol name, e.g. replacing template gunk
|
|
|
|
function cleanup_syname
|
2020-05-15 13:56:06 +08:00
|
|
|
set -l symname $argv[1]
|
2019-04-29 02:52:56 +08:00
|
|
|
set symname (string replace --all 'std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >' 'wcstring' $symname)
|
|
|
|
set symname (string replace --all 'std::__1::vector<wcstring, std::__1::allocator<wcstring > >' 'wcstring_list_t' $symname)
|
|
|
|
echo $symname
|
|
|
|
end
|
|
|
|
|
|
|
|
# Output the declaration for a symbol name in a given file.
|
2020-05-15 13:56:06 +08:00
|
|
|
function print_decl -a FISH_SOURCE_DIR objfile symname
|
2019-04-29 02:52:56 +08:00
|
|
|
set -l varname (string split '::' $symname)[-1]
|
|
|
|
set -l srcfile (basename $objfile .o)
|
|
|
|
set -l srcpath $FISH_SOURCE_DIR/src/$srcfile
|
2018-09-29 12:58:44 +08:00
|
|
|
|
2019-04-29 02:52:56 +08:00
|
|
|
# A leading underscore indicates a global, strip it.
|
|
|
|
set varname (string replace --regex '^_' '' $varname)
|
|
|
|
|
|
|
|
if not test -f "$srcpath"
|
|
|
|
echo "Could not find $srcpath"
|
|
|
|
end
|
|
|
|
# Guess the variable as the first usage of the name.
|
|
|
|
# Strip everything after the first =.
|
2020-05-15 13:56:06 +08:00
|
|
|
set -l vardecl (egrep -m 1 " $varname\\b" $srcpath | cut -f -1 -d '=' | string trim)
|
2019-04-29 02:52:56 +08:00
|
|
|
if test -z "$vardecl"
|
|
|
|
echo "COULD_NOT_FIND_$varname"
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
echo $vardecl
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return if a variable declaration is "thread safe".
|
|
|
|
function decl_is_threadsafe
|
|
|
|
set -l vardecl $argv[1]
|
|
|
|
# decls starting with 'const ' or containing ' const ' are assumed safe.
|
2019-04-29 09:13:55 +08:00
|
|
|
string match -q --regex '(^|\\*| )const ' $vardecl
|
2019-04-29 02:52:56 +08:00
|
|
|
and return 0
|
|
|
|
|
|
|
|
# Ordinary types indicating a safe variable.
|
2020-05-15 13:56:06 +08:00
|
|
|
set -l safes relaxed_atomic_bool_t std::mutex std::condition_variable std::once_flag sig_atomic_t
|
2019-04-29 02:52:56 +08:00
|
|
|
for safe in $safes
|
|
|
|
string match -q "*$safe*" $vardecl
|
|
|
|
and return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# Template types indicate a safe variable.
|
2019-04-29 09:13:55 +08:00
|
|
|
set safes owning_lock mainthread_t std::atomic relaxed_atomic_t latch_t
|
2019-04-29 02:52:56 +08:00
|
|
|
for safe in $safes
|
|
|
|
string match -q "*$safe<*" $vardecl
|
|
|
|
and return 0
|
|
|
|
end
|
|
|
|
end
|
2018-09-29 11:38:59 +08:00
|
|
|
|
|
|
|
for file in ./**.o
|
2020-05-15 13:56:06 +08:00
|
|
|
set -l filename (basename $file)
|
2018-09-29 12:58:44 +08:00
|
|
|
# Skip boring files.
|
|
|
|
contains $filename $boring_files
|
|
|
|
and continue
|
2019-04-29 02:52:56 +08:00
|
|
|
for line in (nm -p -P -U $file | egrep $nm_regex)
|
2020-05-15 13:56:06 +08:00
|
|
|
set -l matches (string match --regex $nm_regex -- $line)
|
2018-09-29 12:58:44 +08:00
|
|
|
or continue
|
2020-05-15 13:56:06 +08:00
|
|
|
set -l symname (cleanup_syname (echo $matches[2] | c++filt))
|
2019-04-29 02:52:56 +08:00
|
|
|
should_ignore $symname
|
|
|
|
and continue
|
2020-05-15 13:56:06 +08:00
|
|
|
set -l vardecl (print_decl $FISH_SOURCE_DIR $filename $symname)
|
2019-04-29 02:52:56 +08:00
|
|
|
decl_is_threadsafe $vardecl
|
2018-09-29 12:58:44 +08:00
|
|
|
and continue
|
2019-04-29 02:52:56 +08:00
|
|
|
echo $filename $symname $matches[3] ":" $vardecl
|
2018-09-29 12:58:44 +08:00
|
|
|
set total_globals (math $total_globals + 1)
|
|
|
|
end
|
2018-09-29 11:38:59 +08:00
|
|
|
end
|
|
|
|
|
2018-09-29 12:58:44 +08:00
|
|
|
echo "Total: $total_globals"
|