]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
grepc: grepc_find_files: Get a reduced list of files once
authorAlejandro Colomar <alx.manpages@gmail.com>
Tue, 10 May 2022 16:44:58 +0000 (18:44 +0200)
committerAlejandro Colomar <alx@kernel.org>
Wed, 29 Oct 2025 20:28:54 +0000 (21:28 +0100)
Instead of running find and grep -l for every function, store the
result in a temporary file for reuse in all functions.

This is a considerable speedup, between 3x and 10x in some tests
within the Linux kernel source code.

Signed-off-by: Alejandro Colomar <alx.manpages@gmail.com>
bin/grepc

index c294a92b1b37f0553d7d0693fcee58bd76afb3ab..bd7b823e2286d1407b98d40075a23c5adf2e5151 100755 (executable)
--- a/bin/grepc
+++ b/bin/grepc
@@ -7,21 +7,30 @@ if (($# != 1)); then
 fi;
 
 
-function grepc_helper()
+function grepc_find_files()
 {
+       files="$(mktemp -t 'grepc.XXXXXX')";
+
        find . -type f \
-       | grep "$1" \
-       | xargs grep -lPI "$2" \
-       | xargs grep -lP  "$3" \
+       | grep -P '\.[ch]' \
+       | xargs grep -lPI "$1\b" \
+       >"$files";
+}
+
+
+function grepc_helper()
+{
+       xargs grep -lPI "$1" <"$files" \
+       | xargs grep -lP  "$2" \
        | sort \
-       | xargs pcregrep -Mn "$4" /dev/null \
+       | xargs pcregrep -Mn "$3" /dev/null \
        | sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
 }
 
 
 function grepc_macro_simple()
 {
-       grepc_helper '\.[ch]$' \
+       grepc_helper \
          "#\s*define\s+$1\b[^(]" \
          '.' \
          "(?s)#\s*define\s+$1\b(?!\().*?[^\\\\]$";
@@ -30,7 +39,7 @@ function grepc_macro_simple()
 
 function grepc_macro_func()
 {
-       grepc_helper '\.[ch]$' \
+       grepc_helper \
          "#\s*define\s+$1\(" \
          '.' \
          "(?s)#\s*define\s+$1\(.*?[^\\\\]$";
@@ -46,7 +55,7 @@ function grepc_macro()
 
 function grepc_enum_constant()
 {
-       grepc_helper '\.[ch]$' \
+       grepc_helper \
          '^enum\s' \
          "^\s*$1\s*[,=]" \
          "(?s)\benum\b\s*([\w\s[\]]|::)*\s*{[^}]*^\s*$1\s*[=,].*?^}.*?;";
@@ -55,7 +64,7 @@ function grepc_enum_constant()
 
 function grepc_func_decl()
 {
-       grepc_helper '\.[ch]$' \
+       grepc_helper \
          "\b$1\s*\(" \
          '.' \
          "(?s)^[\w[]([\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**$1\s*\([\w\s\(,\)[\]*]+?(...)?\)[\w\s\(,\)[:\]]*;";
@@ -64,7 +73,7 @@ function grepc_func_decl()
 
 function grepc_func_def()
 {
-       grepc_helper '\.[ch]$' \
+       grepc_helper \
          "\b$1\s*\(" \
          '.' \
          "(?s)^[\w[]([\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+\**$1\s*\([\w\s\(,\)[\]*]+?(...)?\)\s*{.*?^}";
@@ -80,7 +89,7 @@ function grepc_func()
 
 function grepc_linux_syscall_decl()
 {
-       grepc_helper '\.[ch]$' \
+       grepc_helper \
          "^asmlinkage\s+[\w\s]+\**sys_$1\s*\(" \
          '.' \
          "(?s)^asmlinkage\s+[\w\s]+\**sys_$1\s*\(.*?\)";
@@ -89,7 +98,7 @@ function grepc_linux_syscall_decl()
 
 function grepc_linux_syscall_def()
 {
-       grepc_helper '\.c$' \
+       grepc_helper \
          "SYSCALL_DEFINE.\($1\b" \
          '.' \
          "(?s)^\w*SYSCALL_DEFINE.\($1\b.*?^}";
@@ -117,7 +126,7 @@ function grepc_glibc()
 
 function grepc_type_struct_union_enum()
 {
-       grepc_helper '\.[ch]$' \
+       grepc_helper \
          "\b(struct|union|enum)\s+$1\b" \
          '.' \
          "(?s)^(?!^\s*typedef\b)([\w[]([\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+)?\b(struct|union|enum)\s+$1\b\s*[\w\s[\]]*{.*?^}.*?;";
@@ -126,7 +135,7 @@ function grepc_type_struct_union_enum()
 
 function grepc_type_typedef_simple()
 {
-       grepc_helper '\.[ch]$' \
+       grepc_helper \
          '^\s*typedef\s' \
          "\b$1;" \
          "(?s)^\s*typedef\s+[^{};]+$1;";
@@ -135,7 +144,7 @@ function grepc_type_typedef_simple()
 
 function grepc_type_typedef_struct_union_enum()
 {
-       grepc_helper '\.[ch]$' \
+       grepc_helper \
          '^\s*typedef\s+(struct|union|enum)\b[^;]*$' \
          "^(  )?}\s*$1;" \
          "(?s)^\s*typedef\s+(struct|union|enum)\s+(?:(?!^(  )?}|^\s*typedef).)*^(  )?}\s*$1;";
@@ -144,9 +153,7 @@ function grepc_type_typedef_struct_union_enum()
 
 function grepc_type_typedef_underlying_struct_union_enum()
 {
-       find . -type f \
-       | grep '\.[ch]$' \
-       | xargs grep -hP "^\s*typedef\s+(struct|union|enum)\s+.*\b$1;" \
+       xargs grep -hP "^\s*typedef\s+(struct|union|enum)\s+.*\b$1;" <"$files" \
        | sed -E -e 's/^\s*typedef\s+//' -e "s/\s*\**\b$1;.*//" \
        | sed -E -e 's/^struct\s+//' -e 's/^union\s+//' -e 's/^enum\s+//' \
        | while read t; do
@@ -173,6 +180,8 @@ function grepc_type()
 
 function main()
 {
+       grepc_find_files "$1";
+
        grepc_macro "$1";
        grepc_enum_constant "$1";
        grepc_func "$1";