From patchwork Wed Sep 22 22:06:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tobias Burnus X-Patchwork-Id: 45321 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BD8493858020 for ; Wed, 22 Sep 2021 22:06:43 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa2.mentor.iphmx.com (esa2.mentor.iphmx.com [68.232.141.98]) by sourceware.org (Postfix) with ESMTPS id CD618385841A; Wed, 22 Sep 2021 22:06:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CD618385841A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com IronPort-SDR: AivpAm6ekjpvQjcxW6d5Af3KjtMVb/xE/XutLTGNvbNdJW34STp/jH0PldmSg/WYZ/Rzti8ojJ 4NJaX/DqEDlezs9Qu2c2YwjXSt2N/+YMs9xc2fb6AxoiSjq2mDWdNtCRlnfSp+ggt9TiYOKEUV OgAMQnqqkZGwdw15Pq0vPW33olIyjyKLFJNGyaVKc7Uqy9mlQbyis7Tn/ykdKyMWV+iFeIzIdi +Ir6hHEHtfb7fLGsJHNcBdg0z/GV2SC/J+1Er3BLguT9G1J5s7o99WHUVQsHiRW1fxd0jisK32 8VjCBkh3ZpR/9rxYUrLJxbIy X-IronPort-AV: E=Sophos;i="5.85,315,1624348800"; d="diff'?scan'208";a="66185904" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa2.mentor.iphmx.com with ESMTP; 22 Sep 2021 14:06:15 -0800 IronPort-SDR: DDO8RZFB9NdhTffcym3xO1AxwnfFISoHDOtB73FU+PCyN81/pUpZihy+tNXy4AOqIwpx4ZQ3rd jxA+UitI0ZCI0ydtQanN0Wxq0jpW9H9oM42v0tuX8zOG6nFtD2X4/4hEb0CD7AggQ6P/xbpFMw 5rNWSXte9aYOcWHQfh8wqrvxazvhnD/TX+rCuDP4E8/8tnqWUu7euFAQxsYtNNhXeRIu308Lrk HQEveDS/S3QWxx+DPA5r+PQ8w1Yqv8j9T2Hgrb7gei5VcuR0Bwy6UlU7UWXeJ3B6q78fqZJXwD eKQ= Subject: Fortran: Improve file-reading error diagnostic [PR55534] (was: Re: [Patch] Fortran: Improve -Wmissing-include-dirs warnings [PR55534]) To: Harald Anlauf , , References: <690e3e15-e13a-c0ec-3d7a-eb04ae989240@codesourcery.com> From: Tobias Burnus Message-ID: <4312b54b-6fb2-c57c-e102-9474ffd56e7a@codesourcery.com> Date: Thu, 23 Sep 2021 00:06:00 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-08.mgc.mentorg.com (139.181.222.8) To svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi Harald, On 22.09.21 20:29, Harald Anlauf via Gcc-patches wrote: > What I find a bit confusing - from the viewpoint of a user - is the > case of using the preprocessor (-cpp), as one gets e.g. > > : Warning: ./no/such/dir: No such file or directory > [-Wmissing-include-dirs] > > while without -cpp: > > f951: Warning: Nonexistent include directory './no/such/dir/' > [-Wmissing-include-dirs] C/C++ do something likewise (grep for that string). The reason for the is the code in cpp.c's gfc_cpp_init, which uses: cpp_change_file (cpp_in, LC_RENAME, _("")); It might be possible to reset it by passing NULL to it, at the end of that function but I don't know whether that causes side effects. At least linemap_add then uses set->depth--. It might work just fine, but I do not know. (Additionally, cb_file_change or print_line needs to be updated to handle to_file == NULL.) Feel free to experiment there. Otherwise, I leave it as is. * * * However, this patch now improves the diagnostic printed by load_file – and uses directly an fatal error instead of a usual error and then propagating the error through. Errors are now also properly colored. Note: * -fpre-included= is not easily testable. It works when calling the compiler itself (f951) but the driver (gfortran) overrides it here with: -fpre-include=/usr/include/finclude/math-vector-fortran.h which exits. * I did not include the test "include_22.f90" with: include "include_22.f90" ! { dg-error "File 'include_22.f90' is being included recursively" } as the error message seemingly confused DejaGNU and causes it to enter an endless loop. OK for mainline? Tobias ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955 Fortran: Improve file-reading error diagnostic [PR55534] PR fortran/55534 gcc/fortran/ChangeLog: * scanner.c (load_file): Return void, call (gfc_)fatal_error for all errors. (include_line, include_stmt, gfc_new_file): Remove exit call for failed load_file run. gcc/testsuite/ChangeLog: * gfortran.dg/include_9.f90: Add dg-prune-output. * gfortran.dg/include_23.f90: New test. * gfortran.dg/include_24.f90: New test. gcc/fortran/scanner.c | 66 ++++++++++++-------------------- gcc/testsuite/gfortran.dg/include_23.f90 | 4 ++ gcc/testsuite/gfortran.dg/include_24.f90 | 4 ++ gcc/testsuite/gfortran.dg/include_9.f90 | 1 + 4 files changed, 33 insertions(+), 42 deletions(-) diff --git a/gcc/fortran/scanner.c b/gcc/fortran/scanner.c index 52124bd5d36..5a450692ba3 100644 --- a/gcc/fortran/scanner.c +++ b/gcc/fortran/scanner.c @@ -47,6 +47,7 @@ along with GCC; see the file COPYING3. If not see #include "toplev.h" /* For set_src_pwd. */ #include "debug.h" #include "options.h" +#include "diagnostic-core.h" /* For fatal_error. */ #include "cpp.h" #include "scanner.h" @@ -2230,7 +2231,7 @@ preprocessor_line (gfc_char_t *c) } -static bool load_file (const char *, const char *, bool); +static void load_file (const char *, const char *, bool); /* include_line()-- Checks a line buffer to see if it is an include line. If so, we call load_file() recursively to load the included @@ -2396,9 +2397,7 @@ include_line (gfc_char_t *line) read by anything else. */ filename = gfc_widechar_to_char (begin, -1); - if (!load_file (filename, NULL, false)) - exit (FATAL_EXIT_CODE); - + load_file (filename, NULL, false); free (filename); return 1; } @@ -2505,9 +2504,7 @@ include_stmt (gfc_linebuf *b) filename[i] = (unsigned char) c; } filename[length] = '\0'; - if (!load_file (filename, NULL, false)) - exit (FATAL_EXIT_CODE); - + load_file (filename, NULL, false); free (filename); do_ret: @@ -2525,9 +2522,11 @@ do_ret: return ret; } + + /* Load a file into memory by calling load_line until the file ends. */ -static bool +static void load_file (const char *realfilename, const char *displayedname, bool initial) { gfc_char_t *line; @@ -2549,13 +2548,8 @@ load_file (const char *realfilename, const char *displayedname, bool initial) for (f = current_file; f; f = f->up) if (filename_cmp (filename, f->filename) == 0) - { - fprintf (stderr, "%s:%d: Error: File '%s' is being included " - "recursively\n", current_file->filename, current_file->line, - filename); - return false; - } - + fatal_error (linemap_line_start (line_table, current_file->line, 0), + "File %qs is being included recursively", filename); if (initial) { if (gfc_src_file) @@ -2567,10 +2561,7 @@ load_file (const char *realfilename, const char *displayedname, bool initial) input = gfc_open_file (realfilename); if (input == NULL) - { - gfc_error_now ("Cannot open file %qs", filename); - return false; - } + gfc_fatal_error ("Cannot open file %qs", filename); } else { @@ -2579,22 +2570,20 @@ load_file (const char *realfilename, const char *displayedname, bool initial) { /* For -fpre-include file, current_file is NULL. */ if (current_file) - fprintf (stderr, "%s:%d: Error: Can't open included file '%s'\n", - current_file->filename, current_file->line, filename); + fatal_error (linemap_line_start (line_table, current_file->line, 0), + "Cannot open included file %qs", filename); else - fprintf (stderr, "Error: Can't open pre-included file '%s'\n", - filename); - - return false; + gfc_fatal_error ("Cannot open pre-included file %qs", filename); } stat_result = stat (realfilename, &st); - if (stat_result == 0 && !S_ISREG(st.st_mode)) + if (stat_result == 0 && !S_ISREG (st.st_mode)) { - fprintf (stderr, "%s:%d: Error: Included path '%s'" - " is not a regular file\n", - current_file->filename, current_file->line, filename); fclose (input); - return false; + if (current_file) + fatal_error (linemap_line_start (line_table, current_file->line, 0), + "Included file %qs is not a regular file", filename); + else + gfc_fatal_error ("Included file %qs is not a regular file", filename); } } @@ -2768,7 +2757,6 @@ load_file (const char *realfilename, const char *displayedname, bool initial) add_file_change (NULL, current_file->inclusion_line + 1); current_file = current_file->up; linemap_add (line_table, LC_LEAVE, 0, NULL, 0); - return true; } @@ -2780,23 +2768,17 @@ load_file (const char *realfilename, const char *displayedname, bool initial) void gfc_new_file (void) { - bool result; - - if (flag_pre_include != NULL - && !load_file (flag_pre_include, NULL, false)) - exit (FATAL_EXIT_CODE); + if (flag_pre_include != NULL) + load_file (flag_pre_include, NULL, false); if (gfc_cpp_enabled ()) { - result = gfc_cpp_preprocess (gfc_source_file); + gfc_cpp_preprocess (gfc_source_file); if (!gfc_cpp_preprocess_only ()) - result = load_file (gfc_cpp_temporary_file (), gfc_source_file, true); + load_file (gfc_cpp_temporary_file (), gfc_source_file, true); } else - result = load_file (gfc_source_file, NULL, true); - - if (!result) - exit (FATAL_EXIT_CODE); + load_file (gfc_source_file, NULL, true); gfc_current_locus.lb = line_head; gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line; diff --git a/gcc/testsuite/gfortran.dg/include_23.f90 b/gcc/testsuite/gfortran.dg/include_23.f90 new file mode 100644 index 00000000000..421ddda87bc --- /dev/null +++ b/gcc/testsuite/gfortran.dg/include_23.f90 @@ -0,0 +1,4 @@ +implicit none +include "nonexisting/file.f90" ! { dg-error "Cannot open included file 'nonexisting/file.f90'" } +end +! { dg-prune-output "compilation terminated." } diff --git a/gcc/testsuite/gfortran.dg/include_24.f90 b/gcc/testsuite/gfortran.dg/include_24.f90 new file mode 100644 index 00000000000..1fe9eb57625 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/include_24.f90 @@ -0,0 +1,4 @@ +implicit none +include "." ! { dg-error "Included file '.' is not a regular file" } +end +! { dg-prune-output "compilation terminated." } diff --git a/gcc/testsuite/gfortran.dg/include_9.f90 b/gcc/testsuite/gfortran.dg/include_9.f90 index c4ef50f6e50..6b0648b3ee5 100644 --- a/gcc/testsuite/gfortran.dg/include_9.f90 +++ b/gcc/testsuite/gfortran.dg/include_9.f90 @@ -4,3 +4,4 @@ program main end program ! { dg-error "is not a regular file" " " { target *-*-* } 3 } +! { dg-prune-output "compilation terminated." }