Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Automatically handle includes in testsuite/lib/
@ 2025-08-13  0:41 Pedro Alves
  2025-08-16 17:59 ` Kevin Buettner
  0 siblings, 1 reply; 3+ messages in thread
From: Pedro Alves @ 2025-08-13  0:41 UTC (permalink / raw)
  To: gdb-patches

Instead of manually calling lappend_include_file in every testcase
that needs to include a file in testsuite/lib/, handle testsuite/lib/
includes automatically in gdb_compile.

As an example, gdb.base/backtrace.exp is adjusted to no longer
explicitly call lappend_include_file for testsuite/lib/attributes.h.

Tested on x86-64 GNU/Linux with both:

 $ make check RUNTESTFLAGS=" \
     --host_board=local-remote-host-native \
     --target_board=local-remote-host-native \
     HOST_DIR=/tmp/foo/" \
     TESTS="gdb.base/backtrace.exp"

and:

 $ make check TESTS="gdb.base/backtrace.exp"

and confirming that the testcase still compiles and passes cleanly.

Also ran full testsuite on x86-64 GNU/Linux in normal mode.

Change-Id: I5ca77426ea4a753a995c3ad125618c02cd952576
---
 gdb/testsuite/gdb.base/backtrace.exp |  6 +--
 gdb/testsuite/lib/gdb.exp            | 63 ++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 5 deletions(-)

diff --git a/gdb/testsuite/gdb.base/backtrace.exp b/gdb/testsuite/gdb.base/backtrace.exp
index 35784b413cf..3020666d741 100644
--- a/gdb/testsuite/gdb.base/backtrace.exp
+++ b/gdb/testsuite/gdb.base/backtrace.exp
@@ -17,11 +17,7 @@
 
 standard_testfile
 
-set flags {}
-lappend flags debug
-lappend_include_file flags $srcdir/lib/attributes.h
-
-if { [prepare_for_testing "failed to prepare" $testfile $srcfile $flags] } {
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
     return -1
 }
 
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 98691dfce25..0361f10b9a6 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -6311,6 +6311,9 @@ proc gdb_compile {source dest type options} {
 	}
     }
 
+    # Automatically handle includes in testsuite/lib/.
+    auto_lappend_include_files options $source
+
     cond_wrap [expr $pie != -1 || $nopie != -1] \
 	with_PIE_multilib_flags_filtered {
 	set result [target_compile $source $dest $type $options]
@@ -11114,6 +11117,66 @@ proc lappend_include_file { flags file } {
     }
 }
 
+# Helper for auto_lappend_include_files that handles one source file,
+# and tracks the list of already-visited files.
+
+proc auto_lappend_include_files_1 {flags source {visited {}}} {
+    upvar $flags up_flags
+    upvar $visited up_visited
+    global srcdir
+
+    set ext [string tolower [file extension $source]]
+    if {$ext ni {".c" ".cpp" ".cc" ".h" ".s"}} {
+	return
+    }
+
+    if {[catch {open $source r} fh err]} {
+	error "Failed to open file '$source': $err"
+    }
+    set contents [read $fh]
+    close $fh
+
+    lappend up_visited $source
+
+    # Match lines like:
+    #  #include "gdb_foo.h"
+    set re "^\\s*#include\\s+\"(.*)\""
+
+    foreach line [split $contents "\n"] {
+	if {[regexp $re $line -> basename]} {
+	    set lib_file "$srcdir/lib/$basename"
+
+	    # If already processed, skip.
+	    if {[lsearch -exact $up_visited $lib_file] != -1} {
+		continue
+	    }
+
+	    if {![file exists $lib_file]} {
+		continue
+	    }
+
+	    # Append to include list, and recurse into the included
+	    # file.
+	    lappend_include_file up_flags $lib_file
+	    auto_lappend_include_files_1 up_flags $lib_file up_visited
+	}
+    }
+}
+
+# Automatically handle includes under gdb/testsuite/lib/.
+#
+# For each source file in SOURCES, look for #include directives
+# including files that live in testsuite/lib/.  For each such included
+# file, call lappend_include_file for it.
+
+proc auto_lappend_include_files {flags sources} {
+    upvar $flags up_flags
+    set visited {}
+    foreach src $sources {
+	auto_lappend_include_files_1 up_flags $src visited
+    }
+}
+
 # Return a list of supported host locales.
 
 gdb_caching_proc host_locales { } {

base-commit: f5b1b0288a97965bd71668b046fa35a85c4cea04
-- 
2.50.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-08-22 18:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-13  0:41 [PATCH] Automatically handle includes in testsuite/lib/ Pedro Alves
2025-08-16 17:59 ` Kevin Buettner
2025-08-22 18:29   ` Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox