tags.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. # Generate tags or cscope files
  4. # Usage tags.sh <mode>
  5. #
  6. # mode may be any of: tags, TAGS, cscope
  7. #
  8. # Uses the following environment variables:
  9. # SUBARCH, SRCARCH, srctree
  10. if [ "$KBUILD_VERBOSE" = "1" ]; then
  11. set -x
  12. fi
  13. # RCS_FIND_IGNORE has escaped ()s -- remove them.
  14. ignore="$(echo "$RCS_FIND_IGNORE" | sed 's|\\||g' )"
  15. # tags and cscope files should also ignore MODVERSION *.mod.c files
  16. ignore="$ignore ( -name *.mod.c ) -prune -o"
  17. # Use make KBUILD_ABS_SRCTREE=1 {tags|cscope}
  18. # to force full paths for a non-O= build
  19. if [ "${srctree}" = "." -o -z "${srctree}" ]; then
  20. tree=
  21. else
  22. tree=${srctree}/
  23. fi
  24. # ignore userspace tools
  25. if [ -n "$COMPILED_SOURCE" ]; then
  26. ignore="$ignore ( -path ./tools ) -prune -o"
  27. else
  28. ignore="$ignore ( -path ${tree}tools ) -prune -o"
  29. fi
  30. # Detect if ALLSOURCE_ARCHS is set. If not, we assume SRCARCH
  31. if [ "${ALLSOURCE_ARCHS}" = "" ]; then
  32. ALLSOURCE_ARCHS=${SRCARCH}
  33. elif [ "${ALLSOURCE_ARCHS}" = "all" ]; then
  34. ALLSOURCE_ARCHS=$(find ${tree}arch/ -mindepth 1 -maxdepth 1 -type d -printf '%f ')
  35. fi
  36. # find sources in arch/$1
  37. find_arch_sources()
  38. {
  39. for i in $archincludedir; do
  40. prune="$prune -wholename $i -prune -o"
  41. done
  42. find ${tree}arch/$1 $ignore $prune -name "$2" -not -type l -print;
  43. }
  44. # find sources in arch/$1/include
  45. find_arch_include_sources()
  46. {
  47. include=$(find ${tree}arch/$1/ -name include -type d -print);
  48. if [ -n "$include" ]; then
  49. archincludedir="$archincludedir $include"
  50. find $include $ignore -name "$2" -not -type l -print;
  51. fi
  52. }
  53. # find sources in include/
  54. find_include_sources()
  55. {
  56. find ${tree}include $ignore -name config -prune -o -name "$1" \
  57. -not -type l -print;
  58. }
  59. # find sources in rest of tree
  60. # we could benefit from a list of dirs to search in here
  61. find_other_sources()
  62. {
  63. find ${tree}* $ignore \
  64. \( -path ${tree}include -o -path ${tree}arch -o -name '.tmp_*' \) -prune -o \
  65. -name "$1" -not -type l -print;
  66. }
  67. find_sources()
  68. {
  69. find_arch_sources $1 "$2"
  70. }
  71. all_sources()
  72. {
  73. find_arch_include_sources ${SRCARCH} '*.[chS]'
  74. if [ ! -z "$archinclude" ]; then
  75. find_arch_include_sources $archinclude '*.[chS]'
  76. fi
  77. find_include_sources '*.[chS]'
  78. for arch in $ALLSOURCE_ARCHS
  79. do
  80. find_sources $arch '*.[chS]'
  81. done
  82. find_other_sources '*.[chS]'
  83. }
  84. all_compiled_sources()
  85. {
  86. realpath -es $([ -z "$KBUILD_ABS_SRCTREE" ] && echo --relative-to=.) \
  87. include/generated/autoconf.h $(find $ignore -name "*.cmd" -exec \
  88. grep -Poh '(?(?=^source_.* \K).*|(?=^ \K\S).*(?= \\))' {} \+ |
  89. awk '!a[$0]++') | sort -u
  90. }
  91. all_target_sources()
  92. {
  93. if [ -n "$COMPILED_SOURCE" ]; then
  94. all_compiled_sources
  95. else
  96. all_sources
  97. fi
  98. }
  99. all_kconfigs()
  100. {
  101. find ${tree}arch/ -maxdepth 1 $ignore \
  102. -name "Kconfig*" -not -type l -print;
  103. for arch in $ALLSOURCE_ARCHS; do
  104. find_sources $arch 'Kconfig*'
  105. done
  106. find_other_sources 'Kconfig*'
  107. }
  108. docscope()
  109. {
  110. (echo \-k; echo \-q; all_target_sources) > cscope.files
  111. cscope -b -f cscope.out
  112. }
  113. dogtags()
  114. {
  115. all_target_sources | gtags -i -f -
  116. }
  117. # Basic regular expressions with an optional /kind-spec/ for ctags and
  118. # the following limitations:
  119. # - No regex modifiers
  120. # - Use \{0,1\} instead of \?, because etags expects an unescaped ?
  121. # - \s is not working with etags, use a space or [ \t]
  122. # - \w works, but does not match underscores in etags
  123. # - etags regular expressions have to match at the start of a line;
  124. # a ^[^#] is prepended by setup_regex unless an anchor is already present
  125. regex_asm=(
  126. '/^\(ENTRY\|_GLOBAL\)(\([[:alnum:]_\\]*\)).*/\2/'
  127. )
  128. regex_c=(
  129. '/^SYSCALL_DEFINE[0-9](\([[:alnum:]_]*\).*/sys_\1/'
  130. '/^BPF_CALL_[0-9](\([[:alnum:]_]*\).*/\1/'
  131. '/^COMPAT_SYSCALL_DEFINE[0-9](\([[:alnum:]_]*\).*/compat_sys_\1/'
  132. '/^TRACE_EVENT(\([[:alnum:]_]*\).*/trace_\1/'
  133. '/^TRACE_EVENT(\([[:alnum:]_]*\).*/trace_\1_rcuidle/'
  134. '/^DEFINE_EVENT([^,)]*, *\([[:alnum:]_]*\).*/trace_\1/'
  135. '/^DEFINE_EVENT([^,)]*, *\([[:alnum:]_]*\).*/trace_\1_rcuidle/'
  136. '/^DEFINE_INSN_CACHE_OPS(\([[:alnum:]_]*\).*/get_\1_slot/'
  137. '/^DEFINE_INSN_CACHE_OPS(\([[:alnum:]_]*\).*/free_\1_slot/'
  138. '/^PAGEFLAG(\([[:alnum:]_]*\).*/Page\1/'
  139. '/^PAGEFLAG(\([[:alnum:]_]*\).*/SetPage\1/'
  140. '/^PAGEFLAG(\([[:alnum:]_]*\).*/ClearPage\1/'
  141. '/^TESTSETFLAG(\([[:alnum:]_]*\).*/TestSetPage\1/'
  142. '/^TESTPAGEFLAG(\([[:alnum:]_]*\).*/Page\1/'
  143. '/^SETPAGEFLAG(\([[:alnum:]_]*\).*/SetPage\1/'
  144. '/\<__SETPAGEFLAG(\([[:alnum:]_]*\).*/__SetPage\1/'
  145. '/\<TESTCLEARFLAG(\([[:alnum:]_]*\).*/TestClearPage\1/'
  146. '/\<__TESTCLEARFLAG(\([[:alnum:]_]*\).*/TestClearPage\1/'
  147. '/\<CLEARPAGEFLAG(\([[:alnum:]_]*\).*/ClearPage\1/'
  148. '/\<__CLEARPAGEFLAG(\([[:alnum:]_]*\).*/__ClearPage\1/'
  149. '/^__PAGEFLAG(\([[:alnum:]_]*\).*/__SetPage\1/'
  150. '/^__PAGEFLAG(\([[:alnum:]_]*\).*/__ClearPage\1/'
  151. '/^PAGEFLAG_FALSE(\([[:alnum:]_]*\).*/Page\1/'
  152. '/\<TESTSCFLAG(\([[:alnum:]_]*\).*/TestSetPage\1/'
  153. '/\<TESTSCFLAG(\([[:alnum:]_]*\).*/TestClearPage\1/'
  154. '/\<SETPAGEFLAG_NOOP(\([[:alnum:]_]*\).*/SetPage\1/'
  155. '/\<CLEARPAGEFLAG_NOOP(\([[:alnum:]_]*\).*/ClearPage\1/'
  156. '/\<__CLEARPAGEFLAG_NOOP(\([[:alnum:]_]*\).*/__ClearPage\1/'
  157. '/\<TESTCLEARFLAG_FALSE(\([[:alnum:]_]*\).*/TestClearPage\1/'
  158. '/^PAGE_TYPE_OPS(\([[:alnum:]_]*\).*/Page\1/'
  159. '/^PAGE_TYPE_OPS(\([[:alnum:]_]*\).*/__SetPage\1/'
  160. '/^PAGE_TYPE_OPS(\([[:alnum:]_]*\).*/__ClearPage\1/'
  161. '/^TASK_PFA_TEST([^,]*, *\([[:alnum:]_]*\))/task_\1/'
  162. '/^TASK_PFA_SET([^,]*, *\([[:alnum:]_]*\))/task_set_\1/'
  163. '/^TASK_PFA_CLEAR([^,]*, *\([[:alnum:]_]*\))/task_clear_\1/'
  164. '/^DEF_MMIO_\(IN\|OUT\)_[XD](\([[:alnum:]_]*\),[^)]*)/\2/'
  165. '/^DEBUGGER_BOILERPLATE(\([[:alnum:]_]*\))/\1/'
  166. '/^DEF_PCI_AC_\(\|NO\)RET(\([[:alnum:]_]*\).*/\2/'
  167. '/^PCI_OP_READ(\(\w*\).*[1-4])/pci_bus_read_config_\1/'
  168. '/^PCI_OP_WRITE(\(\w*\).*[1-4])/pci_bus_write_config_\1/'
  169. '/\<DEFINE_\(RT_MUTEX\|MUTEX\|SEMAPHORE\|SPINLOCK\)(\([[:alnum:]_]*\)/\2/v/'
  170. '/\<DEFINE_\(RAW_SPINLOCK\|RWLOCK\|SEQLOCK\)(\([[:alnum:]_]*\)/\2/v/'
  171. '/\<DECLARE_\(RWSEM\|COMPLETION\)(\([[:alnum:]_]\+\)/\2/v/'
  172. '/\<DECLARE_BITMAP(\([[:alnum:]_]*\)/\1/v/'
  173. '/\(^\|\s\)\(\|L\|H\)LIST_HEAD(\([[:alnum:]_]*\)/\3/v/'
  174. '/\(^\|\s\)RADIX_TREE(\([[:alnum:]_]*\)/\2/v/'
  175. '/\<DEFINE_PER_CPU([^,]*, *\([[:alnum:]_]*\)/\1/v/'
  176. '/\<DEFINE_PER_CPU_SHARED_ALIGNED([^,]*, *\([[:alnum:]_]*\)/\1/v/'
  177. '/\<DECLARE_WAIT_QUEUE_HEAD(\([[:alnum:]_]*\)/\1/v/'
  178. '/\<DECLARE_\(TASKLET\|WORK\|DELAYED_WORK\)(\([[:alnum:]_]*\)/\2/v/'
  179. '/\(^\s\)OFFSET(\([[:alnum:]_]*\)/\2/v/'
  180. '/\(^\s\)DEFINE(\([[:alnum:]_]*\)/\2/v/'
  181. '/\<\(DEFINE\|DECLARE\)_HASHTABLE(\([[:alnum:]_]*\)/\2/v/'
  182. '/\<DEFINE_ID\(R\|A\)(\([[:alnum:]_]\+\)/\2/'
  183. '/\<DEFINE_WD_CLASS(\([[:alnum:]_]\+\)/\1/'
  184. '/\<ATOMIC_NOTIFIER_HEAD(\([[:alnum:]_]\+\)/\1/'
  185. '/\<RAW_NOTIFIER_HEAD(\([[:alnum:]_]\+\)/\1/'
  186. '/\<DECLARE_FAULT_ATTR(\([[:alnum:]_]\+\)/\1/'
  187. '/\<BLOCKING_NOTIFIER_HEAD(\([[:alnum:]_]\+\)/\1/'
  188. '/\<DEVICE_ATTR_\(RW\|RO\|WO\)(\([[:alnum:]_]\+\)/dev_attr_\2/'
  189. '/\<DRIVER_ATTR_\(RW\|RO\|WO\)(\([[:alnum:]_]\+\)/driver_attr_\2/'
  190. '/\<\(DEFINE\|DECLARE\)_STATIC_KEY_\(TRUE\|FALSE\)\(\|_RO\)(\([[:alnum:]_]\+\)/\4/'
  191. '/^SEQCOUNT_LOCKTYPE(\([^,]*\),[[:space:]]*\([^,]*\),[^)]*)/seqcount_\2_t/'
  192. '/^SEQCOUNT_LOCKTYPE(\([^,]*\),[[:space:]]*\([^,]*\),[^)]*)/seqcount_\2_init/'
  193. )
  194. regex_kconfig=(
  195. '/^[[:blank:]]*\(menu\|\)config[[:blank:]]\+\([[:alnum:]_]\+\)/\2/'
  196. '/^[[:blank:]]*\(menu\|\)config[[:blank:]]\+\([[:alnum:]_]\+\)/CONFIG_\2/'
  197. )
  198. setup_regex()
  199. {
  200. local mode=$1 lang tmp=() r
  201. shift
  202. regex=()
  203. for lang; do
  204. case "$lang" in
  205. asm) tmp=("${regex_asm[@]}") ;;
  206. c) tmp=("${regex_c[@]}") ;;
  207. kconfig) tmp=("${regex_kconfig[@]}") ;;
  208. esac
  209. for r in "${tmp[@]}"; do
  210. if test "$mode" = "exuberant"; then
  211. regex[${#regex[@]}]="--regex-$lang=${r}b"
  212. else
  213. # Remove ctags /kind-spec/
  214. case "$r" in
  215. /*/*/?/)
  216. r=${r%?/}
  217. esac
  218. # Prepend ^[^#] unless already anchored
  219. case "$r" in
  220. /^*) ;;
  221. *)
  222. r="/^[^#]*${r#/}"
  223. esac
  224. regex[${#regex[@]}]="--regex=$r"
  225. fi
  226. done
  227. done
  228. }
  229. exuberant()
  230. {
  231. setup_regex exuberant asm c
  232. all_target_sources | xargs $1 -a \
  233. -I __initdata,__exitdata,__initconst,__ro_after_init \
  234. -I __initdata_memblock \
  235. -I __refdata,__attribute,__maybe_unused,__always_unused \
  236. -I __acquires,__releases,__deprecated,__always_inline \
  237. -I __read_mostly,__aligned,____cacheline_aligned \
  238. -I ____cacheline_aligned_in_smp \
  239. -I __cacheline_aligned,__cacheline_aligned_in_smp \
  240. -I ____cacheline_internodealigned_in_smp \
  241. -I __used,__packed,__packed2__,__must_check,__must_hold \
  242. -I EXPORT_SYMBOL,EXPORT_SYMBOL_GPL,ACPI_EXPORT_SYMBOL \
  243. -I DEFINE_TRACE,EXPORT_TRACEPOINT_SYMBOL,EXPORT_TRACEPOINT_SYMBOL_GPL \
  244. -I static,const \
  245. --extra=+fq --c-kinds=+px --fields=+iaS --langmap=c:+.h \
  246. "${regex[@]}"
  247. setup_regex exuberant kconfig
  248. all_kconfigs | xargs $1 -a \
  249. --langdef=kconfig --language-force=kconfig "${regex[@]}"
  250. }
  251. emacs()
  252. {
  253. setup_regex emacs asm c
  254. all_target_sources | xargs $1 -a "${regex[@]}"
  255. setup_regex emacs kconfig
  256. all_kconfigs | xargs $1 -a "${regex[@]}"
  257. }
  258. xtags()
  259. {
  260. if $1 --version 2>&1 | grep -iq exuberant; then
  261. exuberant $1
  262. elif $1 --version 2>&1 | grep -iq emacs; then
  263. emacs $1
  264. else
  265. all_target_sources | xargs $1 -a
  266. fi
  267. }
  268. # Support um (which uses SUBARCH)
  269. if [ "${ARCH}" = "um" ]; then
  270. if [ "$SUBARCH" = "i386" ]; then
  271. archinclude=x86
  272. elif [ "$SUBARCH" = "x86_64" ]; then
  273. archinclude=x86
  274. else
  275. archinclude=${SUBARCH}
  276. fi
  277. fi
  278. remove_structs=
  279. case "$1" in
  280. "cscope")
  281. docscope
  282. ;;
  283. "gtags")
  284. dogtags
  285. ;;
  286. "tags")
  287. rm -f tags
  288. xtags ctags
  289. remove_structs=y
  290. ;;
  291. "TAGS")
  292. rm -f TAGS
  293. xtags etags
  294. remove_structs=y
  295. ;;
  296. esac
  297. # Remove structure forward declarations.
  298. if [ -n "$remove_structs" ]; then
  299. LANG=C sed -i -e '/^\([a-zA-Z_][a-zA-Z0-9_]*\)\t.*\t\/\^struct \1;.*\$\/;"\tx$/d' $1
  300. fi