From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id 32D793858D1E for ; Wed, 2 Aug 2023 13:36:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 32D793858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-3179ed1dfbbso3417221f8f.1 for ; Wed, 02 Aug 2023 06:36:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690983382; x=1691588182; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=6fS9jdLQUQFvxKKBQ4tpbwI4kLrSWtudFk4SaF28T+E=; b=XLJITRjkhikTgZf4fA91x3ZGMoJjiHIfr8Cc+uVOZSaiq4eYEei6RBP/QSNXynFwtQ P7sayYP8WxmzvvZzbs+ZhyQ8wHHZ8bEMrlytpkcLvGYLTO5d0nTv938APc+oCn/XILlF gF0Jib43uJxzxrYSWG/CJ7Y5DZU7M/7fvVaCrK6gjsAOZ/lfPQClQ+SNf/kWT5quNQss fqWJmnW5RMvjkM4nzJ+AvDzicTJn2bZF7nikpNPeGgfG4MZKE4XDoE/IMPG+JZ6MXPJz 35+iVsPvzEEkYOpsK6Aa+Zi/PJ5V3OOw7eqLUUcAmT2rsbvq5+wSY1OZS1URb0+juCr8 JoaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690983382; x=1691588182; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6fS9jdLQUQFvxKKBQ4tpbwI4kLrSWtudFk4SaF28T+E=; b=fDQ+FHASHJmuitnP5Cr6zQFO2Y+VsPstCbggNFMoC1jf64nXvytIn0UZP5Iq0+m9G2 6yOS4iyNQxSEPs0TgLHk7lED89lxYoKsC1HU9mOnqsv3hny1ZRC+dfJSZOODNq2nGhsl ZTVYzHwDCsUcx5VOgMmDBmP8ToRYrMS2w0HS9tX2bgd+oRIBd1zReJF+oDL3NBPsRg5h fvPmLnIxQZAZN5Rj7uMqXGfL6y+K8jGwAiw/i7XZ6SEEuZKs+7mM+L6DtY2AIsuG7Ghk KzvYYOo7I2m2idrqVr2TZsKAUq0N4KcrKkKdc5f6aH2Ca6JClruFROXwVBH0Yc44LN1P LggQ== X-Gm-Message-State: ABy/qLYytwRcnrRL0oeV9vM2/Fv1ekagGVt7XYTvGGRvT3/+LHUQdee2 nvA1Okpm9G50U4LBckton2GgbnghObcrwZchdiqs2A== X-Google-Smtp-Source: APBJJlEYTK/7ueS3gKiv5165BcCAEoyKW4ldrZ7bLGuhPnx9AHbX+gRqOLjEMkf1rKfsCVqEkOgh9g== X-Received: by 2002:a05:6000:8:b0:317:5686:e4b9 with SMTP id h8-20020a056000000800b003175686e4b9mr5138950wrx.56.1690983381731; Wed, 02 Aug 2023 06:36:21 -0700 (PDT) Received: from whitegate.. (cpc77030-warw18-2-0-cust254.3-2.cable.virginm.net. [86.9.96.255]) by smtp.gmail.com with ESMTPSA id o10-20020a056000010a00b00317b063590fsm4251277wrx.55.2023.08.02.06.36.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 06:36:21 -0700 (PDT) From: Richard Bunt To: gdb-patches@sourceware.org Cc: Richard Bunt Subject: [PATCH v2] gdb/fortran: Align intrinsic/variable precedence Date: Wed, 2 Aug 2023 14:36:13 +0100 Message-Id: <20230802133613.941743-1-richard.bunt@linaro.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Fortran allows variables and function to be named after language defined intrinsics as they are not reserved keywords. For example, the abs maths intrinsic can be hidden by a user declaring a variable called abs. The behavior before this patch was to favour the intrinsic, which meant that any variables named, for example "allocated", could not be inspected by GDB. This patch inverts this priority to bring GDB's behaviour closer to the Fortran language, where the user defined symbol can hide the intrinsic. Special care was need to prevent any C symbols from overriding either Fortran intrinsics or user defined variables. This was observed to be the case when GDB has access to symbols for abs from libm. This was solved by only allowing symbols not marked with language_fortran to be overridden. In total this brings the order of precedence to the following (highest first): 1. User defined Fortran variable or function. 2. Fortran intrinsic. 3. Symbols from languages other than Fortran. The sizeof intrinsic is now case insensitive. This is closer to the Fortran language. I believe this change is safe enough as it increases the acceptance of the grammar, rather than restricts it. I.e. it should not break any existing scripts which rely on it. Unless of course they rely on SIZEOF being rejected. GDB built with GCC 13. No test suite regressions detected. Compilers: GCC, ACfL, Intel, Intel LLVM, NVHPC; Platforms: x86_64, aarch64. Existing tests in gdb.fortran cover the invocation of intrinsics including: intrinsics.exp, shape.exp, rank.exp, lbound-ubound.exp. --- gdb/f-exp.y | 57 +++++++++----- .../gdb.fortran/intrinsic-precedence.c | 18 +++++ .../gdb.fortran/intrinsic-precedence.exp | 74 +++++++++++++++++++ .../gdb.fortran/intrinsic-precedence.f90 | 33 +++++++++ 4 files changed, 163 insertions(+), 19 deletions(-) create mode 100644 gdb/testsuite/gdb.fortran/intrinsic-precedence.c create mode 100644 gdb/testsuite/gdb.fortran/intrinsic-precedence.exp create mode 100644 gdb/testsuite/gdb.fortran/intrinsic-precedence.f90 diff --git a/gdb/f-exp.y b/gdb/f-exp.y index c0afebc0589..8a9c94dc0d3 100644 --- a/gdb/f-exp.y +++ b/gdb/f-exp.y @@ -1278,6 +1278,28 @@ static const struct f77_boolean_val boolean_values[] = { ".false.", 0 } }; +static const struct token f_intrinsics[] = +{ + /* The following correspond to actual functions in Fortran and are case + insensitive. */ + { "kind", KIND, OP_NULL, false }, + { "abs", UNOP_INTRINSIC, UNOP_ABS, false }, + { "mod", BINOP_INTRINSIC, BINOP_MOD, false }, + { "floor", UNOP_OR_BINOP_INTRINSIC, FORTRAN_FLOOR, false }, + { "ceiling", UNOP_OR_BINOP_INTRINSIC, FORTRAN_CEILING, false }, + { "modulo", BINOP_INTRINSIC, BINOP_FORTRAN_MODULO, false }, + { "cmplx", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC, FORTRAN_CMPLX, false }, + { "lbound", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC, FORTRAN_LBOUND, false }, + { "ubound", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC, FORTRAN_UBOUND, false }, + { "allocated", UNOP_INTRINSIC, UNOP_FORTRAN_ALLOCATED, false }, + { "associated", UNOP_OR_BINOP_INTRINSIC, FORTRAN_ASSOCIATED, false }, + { "rank", UNOP_INTRINSIC, UNOP_FORTRAN_RANK, false }, + { "size", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC, FORTRAN_ARRAY_SIZE, false }, + { "shape", UNOP_INTRINSIC, UNOP_FORTRAN_SHAPE, false }, + { "loc", UNOP_INTRINSIC, UNOP_FORTRAN_LOC, false }, + { "sizeof", SIZEOF, OP_NULL, false }, +}; + static const token f_keywords[] = { /* Historically these have always been lowercase only in GDB. */ @@ -1300,27 +1322,9 @@ static const token f_keywords[] = { "real_4", REAL_S4_KEYWORD, OP_NULL, true }, { "real_8", REAL_S8_KEYWORD, OP_NULL, true }, { "real_16", REAL_S16_KEYWORD, OP_NULL, true }, - { "sizeof", SIZEOF, OP_NULL, true }, { "single", SINGLE, OP_NULL, true }, { "double", DOUBLE, OP_NULL, true }, { "precision", PRECISION, OP_NULL, true }, - /* The following correspond to actual functions in Fortran and are case - insensitive. */ - { "kind", KIND, OP_NULL, false }, - { "abs", UNOP_INTRINSIC, UNOP_ABS, false }, - { "mod", BINOP_INTRINSIC, BINOP_MOD, false }, - { "floor", UNOP_OR_BINOP_INTRINSIC, FORTRAN_FLOOR, false }, - { "ceiling", UNOP_OR_BINOP_INTRINSIC, FORTRAN_CEILING, false }, - { "modulo", BINOP_INTRINSIC, BINOP_FORTRAN_MODULO, false }, - { "cmplx", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC, FORTRAN_CMPLX, false }, - { "lbound", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC, FORTRAN_LBOUND, false }, - { "ubound", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC, FORTRAN_UBOUND, false }, - { "allocated", UNOP_INTRINSIC, UNOP_FORTRAN_ALLOCATED, false }, - { "associated", UNOP_OR_BINOP_INTRINSIC, FORTRAN_ASSOCIATED, false }, - { "rank", UNOP_INTRINSIC, UNOP_FORTRAN_RANK, false }, - { "size", UNOP_OR_BINOP_OR_TERNOP_INTRINSIC, FORTRAN_ARRAY_SIZE, false }, - { "shape", UNOP_INTRINSIC, UNOP_FORTRAN_SHAPE, false }, - { "loc", UNOP_INTRINSIC, UNOP_FORTRAN_LOC, false }, }; /* Implementation of a dynamically expandable buffer for processing input @@ -1663,7 +1667,22 @@ yylex (void) pstate->gdbarch (), tmp.c_str ()); if (yylval.tsym.type != NULL) return TYPENAME; - + + /* This is post the symbol search as symbols can hide intrinsics. Also, + give Fortran intrinsics priority over C symbols. This prevents + non-Fortran symbols from hiding intrinsics, for example abs. */ + if (!result.symbol || result.symbol->language () != language_fortran) + for (int i = 0; i < ARRAY_SIZE (f_intrinsics); i++) + { + gdb_assert (!f_intrinsics[i].case_sensitive); + if (strlen (f_intrinsics[i].oper) == namelen + && strncasecmp (tokstart, f_intrinsics[i].oper, namelen) == 0) + { + yylval.opcode = f_intrinsics[i].opcode; + return f_intrinsics[i].token; + } + } + /* Input names that aren't symbols but ARE valid hex numbers, when the input radix permits them, can be names or numbers depending on the parse. Note we support radixes > 16 here. */ diff --git a/gdb/testsuite/gdb.fortran/intrinsic-precedence.c b/gdb/testsuite/gdb.fortran/intrinsic-precedence.c new file mode 100644 index 00000000000..cec960c6bff --- /dev/null +++ b/gdb/testsuite/gdb.fortran/intrinsic-precedence.c @@ -0,0 +1,18 @@ +/* Copyright 2023 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 . */ + +int kind (int a) { + return 7; +} diff --git a/gdb/testsuite/gdb.fortran/intrinsic-precedence.exp b/gdb/testsuite/gdb.fortran/intrinsic-precedence.exp new file mode 100644 index 00000000000..32690333f9e --- /dev/null +++ b/gdb/testsuite/gdb.fortran/intrinsic-precedence.exp @@ -0,0 +1,74 @@ +# Copyright 2023 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 . + +require allow_fortran_tests +require allow_shlib_tests + +standard_testfile .f90 +load_lib fortran.exp + +set srcfile_lib ${srcdir}/${subdir}/${testfile}.c +set binfile_lib [standard_output_file ${testfile}.so] +set lib_flags [list debug] +set bin_flags [list f90 debug shlib=${binfile_lib}] + +if {[gdb_compile_shlib ${srcfile_lib} ${binfile_lib} $lib_flags] != "" } { + return -1 +} + +if {[prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \ + ${bin_flags}]} { + return -1 +} + +if ![fortran_runto_main] { + return -1 +} + +gdb_breakpoint [gdb_get_line_number "all-assigned"] +gdb_continue_to_breakpoint "all-assigned" + +# Variable in source is upper case. +gdb_test "print LOC" "17" +gdb_test "print loc" "17" + +# Variable in source is lower case +gdb_test "print UBOUND" "79" +gdb_test "print ubound" "79" + +# Intrinsic hides a C symbol that has debug information. This mimics the abs +# scenario, where it can exist as a function in C, a Fortran intrinsic and a +# user defined variable/function. +gdb_test "print kind(minus)" "4" +# Confirm that the C symbol is there to be chosen if the precedence order is +# incorrect. +gdb_test "set lang c" \ + "Warning: the current language does not match this frame." +gdb_test "print kind(3)" "7" +gdb_test_no_output "set lang fortran" + +# User defined abs function hides the intrinsic. +gdb_breakpoint [gdb_get_line_number "user-abs"] +gdb_continue_to_breakpoint "user-abs" +set integer4 [fortran_int4] +gdb_test "whatis abs" "void \\\(${integer4}(, uinteger\\\*8)?\\\)" + +# Test the scenario where the C defined version of kind is not returned by +# lookup_symbol. +gdb_test_no_output "set confirm off" +# breakpoints cannot be reset without symbol information. +gdb_test_no_output "delete" +gdb_test "symbol-file" "No symbol file now." +gdb_test "print kind(0)" "4" diff --git a/gdb/testsuite/gdb.fortran/intrinsic-precedence.f90 b/gdb/testsuite/gdb.fortran/intrinsic-precedence.f90 new file mode 100644 index 00000000000..7b7c3b2c349 --- /dev/null +++ b/gdb/testsuite/gdb.fortran/intrinsic-precedence.f90 @@ -0,0 +1,33 @@ +! Copyright 2023 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 2 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, write to the Free Software +! Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +program intrinsic_precedence + implicit none + integer(kind=4) LOC, ubound, minus + LOC = 17 + ubound = 79 + minus = -1 + print *, minus, LOC, ubound + call abs(minus) !all-assigned +contains + subroutine abs(i) + integer(kind=4) :: i + if(i .lt. 0) then + i = -i + endif + print *, i !user-abs + end subroutine abs +end program intrinsic_precedence -- 2.32.0