From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 4RiYCryD82l0jgYAWB0awg (envelope-from ) for ; Thu, 30 Apr 2026 12:30:52 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=QUixYZSs; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 261361E0BA; Thu, 30 Apr 2026 12:30:52 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 3949F1E067 for ; Thu, 30 Apr 2026 12:30:51 -0400 (EDT) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id CA8CD436F3E1 for ; Thu, 30 Apr 2026 16:30:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA8CD436F3E1 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=QUixYZSs Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) by sourceware.org (Postfix) with ESMTPS id 5A3C3436F3CB for ; Thu, 30 Apr 2026 16:30:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A3C3436F3CB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5A3C3436F3CB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777566623; cv=none; b=LNBqNk0DrOmD7G7b+J4/8GeSBoHaR4Bsn6nber+L7X5ea80Yu6y79DR8Fn0On2cOSR/ncQ2wdpbI2T5YBVC+bz8YIoFkJOHjF+kienh6cMz/4H+mvKwBk/GELeeWtDepaPfBJu2fE7PCRWWPS6w3JWFDny7iujHmeXbMzqs/3zw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777566623; c=relaxed/simple; bh=gLQSTWZVjbZfdG9uJ7p0/2nKAqa+S1rGT2QXD+wsTUM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=HDEhn1FD5DKfw1+5TG8uhItRk1tx/3l9d5OtofyNRbEqYSE8qGzKOmscPLkzo+TRJ2W4POBv/SZ3S/0DWjVXRmEzvRFuq8J+ZkE6+UzIkKc1B0ICOF+j+yR46NW8tO0Hs8wFCLZge0sagEPDZOTs10A2mnaEGB7yJ8PXk0b03eI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A3C3436F3CB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1777566624; x=1809102624; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gLQSTWZVjbZfdG9uJ7p0/2nKAqa+S1rGT2QXD+wsTUM=; b=QUixYZSsg2TENLQfw4473LQOpb1jx3kJuGo34Hqn65KKF59+zsJEQcQC lmey9WdxJtwvRmYiS7pIADrQjF6NQ+otiuMUrH5OlU34UFvqmWeMxEo0h Bj6psY4Rtg39jio2sPbFgAlP3iPyrTYR49slPdcSdn8amGgtNwUgv+qf5 ExOcAP9hsWs/kUs0r4zMS3kkaZR5KCpbeHCDZxsIrlNW3vvHFNYPSS227 Tw43HMpNIYBK/7wbFvvqR17Kl4cVPC1FnL6w2w6IATH3VOsQk1RK2gskr Xrh+jkrbLQKEw2Yd8dWs3nMuw8iUDkqv5Yacoucy2j8WLKkLafb6E91Yn A==; X-CSE-ConnectionGUID: pRunHyFzSJ6ABOQd4GGazA== X-CSE-MsgGUID: 8yhI8l/+RPigk58nvFQ/GA== X-IronPort-AV: E=McAfee;i="6800,10657,11772"; a="89216544" X-IronPort-AV: E=Sophos;i="6.23,208,1770624000"; d="scan'208";a="89216544" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2026 09:30:23 -0700 X-CSE-ConnectionGUID: L0g0hh+lQ/ONLyFBRkoI4A== X-CSE-MsgGUID: naxwppw8TsCFJXtL4ivM1Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,208,1770624000"; d="scan'208";a="233602265" Received: from unknown (HELO localhost) ([10.245.116.136]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Apr 2026 09:30:21 -0700 From: Abdul Basit Ijaz To: gdb-patches@sourceware.org Cc: abdul.b.ijaz@intel.com, tom@tromey.com, kevinb@redhat.com Subject: [PATCH v2 1/1] gdb, fortran: Fix local variable lookup in Fortran parser Date: Thu, 30 Apr 2026 18:29:55 +0200 Message-Id: <20260430162955.295647-2-abdul.b.ijaz@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260430162955.295647-1-abdul.b.ijaz@intel.com> References: <20260430162955.295647-1-abdul.b.ijaz@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org This change fixes https://sourceware.org/bugzilla/show_bug.cgi?id=34059. The Fortran expression parser in GDB ("gdb/f-exp.y") previously performed symbol lookup in this order when parsing identifiers: - SEARCH_STRUCT_DOMAIN (types/structs) - SEARCH_VFT ("C-like" name lookups for variables/types/functions) - SEARCH_MODULE_DOMAIN (modules) It searched for "types before variables", causing type names from shared libraries to shadow local variable names. For a reproducer like the one given below, the Fortran parser fails to look up local variable name "array" and treats it as a type value instead of a variable, because there's a conflicting "array" type from system libraries. 1 program test 2 3 ! Declare variables used in this test. 4 integer, dimension (-2:2) :: array 5 6 array = 1 7 8 print *, "" ! Break here 9 print *, array 10 11 end program test Before the change, GDB shows: ''' ./gdb --data-directory=./data-directory --args /tmp/a.out GNU gdb (GDB) 18.0.50.20260408-git Copyright (C) 2026 Free Software Foundation, Inc. ... Reading symbols from /tmp/a.out... (gdb) break 8 Breakpoint 1 at 0x11b3: file test.f90, line 8. (gdb) run Starting program: /tmp/a.out [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Breakpoint 1, test () at test.f90:8 8 print *, "" ! Break here (gdb) info locals array = (1, 1, 1, 1, 1) (gdb) print array Attempt to use a type name as an expression (gdb) ptype array type = Type array Type, C_Union :: :: u PTR TO -> ( char :: scratch(0:15 )) End Type array ''' This issue is fixed in the Fortran expression parser in GDB ("gdb/f-exp.y") by prioritizing SEARCH_VFT over other search domains during symbol lookup. After the change, the 'array' variable is resolved to the correct value. ''' (gdb) print array $1 = (1, 1, 1, 1, 1) (gdb) ptype array type = integer(kind=4) (-2:2) ''' --- gdb/f-exp.y | 2 +- .../gdb.fortran/var-type-precedence-lib.c | 30 ++++++++++ .../gdb.fortran/var-type-precedence.exp | 60 +++++++++++++++++++ .../gdb.fortran/var-type-precedence.f90 | 40 +++++++++++++ 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.fortran/var-type-precedence-lib.c create mode 100644 gdb/testsuite/gdb.fortran/var-type-precedence.exp create mode 100644 gdb/testsuite/gdb.fortran/var-type-precedence.f90 diff --git a/gdb/f-exp.y b/gdb/f-exp.y index 278e2091403..4216112c10b 100644 --- a/gdb/f-exp.y +++ b/gdb/f-exp.y @@ -1651,8 +1651,8 @@ yylex (void) struct block_symbol result; const domain_search_flags lookup_domains[] = { - SEARCH_STRUCT_DOMAIN, SEARCH_VFT, + SEARCH_STRUCT_DOMAIN, SEARCH_MODULE_DOMAIN }; int hextype; diff --git a/gdb/testsuite/gdb.fortran/var-type-precedence-lib.c b/gdb/testsuite/gdb.fortran/var-type-precedence-lib.c new file mode 100644 index 00000000000..87bd19fad84 --- /dev/null +++ b/gdb/testsuite/gdb.fortran/var-type-precedence-lib.c @@ -0,0 +1,30 @@ +/* Copyright 2026 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Define a struct type that will conflict with Fortran variable name. */ +struct type_shadowing_var +{ + int value; +}; + +/* Global variables to ensure types are in debug info. */ +static struct type_shadowing_var global_conflicting_var = {42}; + +/* Function to ensure library is linked and types are used. */ +void +fortran_var_type_order_test (void) +{ + global_conflicting_var.value = 42; +} diff --git a/gdb/testsuite/gdb.fortran/var-type-precedence.exp b/gdb/testsuite/gdb.fortran/var-type-precedence.exp new file mode 100644 index 00000000000..8c9f80d43d9 --- /dev/null +++ b/gdb/testsuite/gdb.fortran/var-type-precedence.exp @@ -0,0 +1,60 @@ +# Copyright 2026 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test variable lookup when local Fortran variable names conflict with +# type names from a C shared library. + +require allow_fortran_tests + +standard_testfile var-type-precedence.f90 var-type-precedence-lib.c +load_lib fortran.exp + +set libname "${testfile}-lib" +set srcfile_lib ${srcdir}/${subdir}/${srcfile2} +set binfile_lib [standard_output_file ${libname}.so] + +# Compile the C shared library. +if {[gdb_compile_shlib ${srcfile_lib} ${binfile_lib} \ + {debug}] != ""} { + untested "failed to compile C shared library" + return +} + +# Compile the Fortran main program with the C shared library. +if {[prepare_for_testing "failed to prepare" $testfile \ + ${srcfile} [list debug f90 shlib=${binfile_lib}]]} { + return +} + +if {![fortran_runto_main]} { + return +} + +set bp_loc [gdb_get_line_number "break-here"] +gdb_breakpoint "$srcfile:$bp_loc" +gdb_continue_to_breakpoint "stop-at-bp" ".*$srcfile:$bp_loc.*" + +set integer4 [fortran_int4] + +# Test that local variable 'type_shadowing_var' is resolved correctly. +gdb_test "print type_shadowing_var" "= \\(1, 1, 1, 1, 1\\)" \ + "print local type_shadowing_var variable" +gdb_test "ptype type_shadowing_var" "type = $integer4 \\(-2:2\\)" \ + "ptype local type_shadowing_var" + +# Verify the C types exist in debug info by checking info types. +gdb_test "info types type_shadowing_var" \ + ".*File.*var-type-precedence-lib\\.c:.*struct type_shadowing_var.*" \ + "C type type_shadowing_var exists in debug info" diff --git a/gdb/testsuite/gdb.fortran/var-type-precedence.f90 b/gdb/testsuite/gdb.fortran/var-type-precedence.f90 new file mode 100644 index 00000000000..d692018faba --- /dev/null +++ b/gdb/testsuite/gdb.fortran/var-type-precedence.f90 @@ -0,0 +1,40 @@ +! Copyright 2026 Free Software Foundation, Inc. +! +! This program is free software; you can redistribute it and/or modify +! it under the terms of the GNU General Public License as published by +! the Free Software Foundation; either version 3 of the License, or +! (at your option) any later version. +! +! This program is distributed in the hope that it will be useful, +! but WITHOUT ANY WARRANTY; without even the implied warranty of +! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +! GNU General Public License for more details. +! +! You should have received a copy of the GNU General Public License +! along with this program. If not, see . + +! Source code for var-type-precedence test to verify that local Fortran +! variables take precedence over conflicting C type names from shared +! libraries. + +program test + use iso_c_binding + implicit none + + interface + subroutine fortran_var_type_order_test () bind (C) + end subroutine fortran_var_type_order_test + end interface + + ! Declare variables with names that conflict with types in C library. + integer, dimension (-2:2) :: type_shadowing_var + + ! Call C library function to ensure it's linked. + call fortran_var_type_order_test () + + type_shadowing_var = 1 + + print *, "" ! break-here + print *, type_shadowing_var + +end program test -- 2.34.1 Intel Deutschland GmbH Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany Tel: +49 89 991 430, www.intel.de Managing Directors: Harry Demas, Jeffrey Schneiderman, Yin Chong Sorrell Chairperson of the Supervisory Board: Nicole Lau Registered Seat: Munich Commercial Register: Amtsgericht Muenchen HRB 186928