From patchwork Wed Dec 29 10:01:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 49396 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 92F793858004 for ; Wed, 29 Dec 2021 10:02:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 92F793858004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1640772165; bh=ptNab5Ag9ZnchTPbtbcihn2TkKiLlfy9S2qHyFyrvv8=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=sqGZXBP7VsFyXXjZ0n8fz3HXVNVWELFfyBSyJxNeyUTgQJNFoLgfzx6Up2Lyh+OmV evtX0KOL8MqJSbaR/B4yPmOWw0jdTVDn0ho83u5FxiZA4L8ev7r6skwutWm/IUZUyF tuzYt2W7NjsRIbZFmLoNCRFSfRRG2PVCvjTgTsJ4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id A2BA03858D39 for ; Wed, 29 Dec 2021 10:02:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A2BA03858D39 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-445-kJ5qOvqGM_CmBsJI_vS63A-1; Wed, 29 Dec 2021 05:02:11 -0500 X-MC-Unique: kJ5qOvqGM_CmBsJI_vS63A-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 668B0801B25; Wed, 29 Dec 2021 10:02:10 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.2.16.169]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B2DAC795B1; Wed, 29 Dec 2021 10:02:00 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 1BTA1vZ13339406 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 29 Dec 2021 11:01:58 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 1BTA1u8c3339405; Wed, 29 Dec 2021 11:01:56 +0100 Date: Wed, 29 Dec 2021 11:01:56 +0100 To: Jason Merrill , "Joseph S. Myers" , Marek Polacek Subject: [PATCH] c-family: Use BULTINS_LOCATION for predefined macros changed upon optimize or target pragmas [PR103012] Message-ID: <20211229100156.GS2646553@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! The following testcases ICE when an optimize or target pragma is followed by a long line (4096+ chars). This is because on such long lines we can't use columns anymore, but the cpp_define calls performed by c_cpp_builtins_optimize_pragma or from the backend hooks for target pragma are done on temporary buffers and expect to get columns from whatever line they appear on (which happens to be the long line after optimize/target pragma), and we run into: #0 fancy_abort (file=0x3abec67 "../../libcpp/line-map.c", line=502, function=0x3abecfc "linemap_add") at ../../gcc/diagnostic.c:1986 #1 0x0000000002e7c335 in linemap_add (set=0x7ffff7fca000, reason=LC_RENAME, sysp=0, to_file=0x41287a0 "pr103012.i", to_line=3) at ../../libcpp/line-map.c:502 #2 0x0000000002e7cc24 in linemap_line_start (set=0x7ffff7fca000, to_line=3, max_column_hint=128) at ../../libcpp/line-map.c:827 #3 0x0000000002e7ce2b in linemap_position_for_column (set=0x7ffff7fca000, to_column=1) at ../../libcpp/line-map.c:898 #4 0x0000000002e771f9 in _cpp_lex_direct (pfile=0x40c3b60) at ../../libcpp/lex.c:3592 #5 0x0000000002e76c3e in _cpp_lex_token (pfile=0x40c3b60) at ../../libcpp/lex.c:3394 #6 0x0000000002e610ef in lex_macro_node (pfile=0x40c3b60, is_def_or_undef=true) at ../../libcpp/directives.c:601 #7 0x0000000002e61226 in do_define (pfile=0x40c3b60) at ../../libcpp/directives.c:639 #8 0x0000000002e610b2 in run_directive (pfile=0x40c3b60, dir_no=0, buf=0x7fffffffd430 "__OPTIMIZE__ 1\n", count=14) at ../../libcpp/directives.c:589 #9 0x0000000002e650c1 in cpp_define (pfile=0x40c3b60, str=0x2f784d1 "__OPTIMIZE__") at ../../libcpp/directives.c:2513 #10 0x0000000002e65100 in cpp_define_unused (pfile=0x40c3b60, str=0x2f784d1 "__OPTIMIZE__") at ../../libcpp/directives.c:2522 #11 0x0000000000f50685 in c_cpp_builtins_optimize_pragma (pfile=0x40c3b60, prev_tree=, cur_tree=) at ../../gcc/c-family/c-cppbuiltin.c:600 assertion that LC_RENAME doesn't happen first. I think the right fix is emit those predefined macros upon optimize/target pragmas with BUILTINS_LOCATION, like we already do for those macros at the start of the TU, they don't appear in columns of the next line after it. Another possibility would be to force them at the location of the pragma. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-12-28 Jakub Jelinek PR c++/103012 gcc/ * config/i386/i386-c.c (ix86_pragma_target_parse): Perform cpp_define/cpp_undef calls with forced token locations BUILTINS_LOCATION. * config/arm/arm-c.c (arm_pragma_target_parse): Likewise. * config/aarch64/aarch64-c.c (aarch64_pragma_target_parse): Likewise. * config/s390/s390-c.c (s390_pragma_target_parse): Likewise. gcc/c-family/ * c-cppbuiltin.c (c_cpp_builtins_optimize_pragma): Perform cpp_define_unused/cpp_undef calls with forced token locations BUILTINS_LOCATION. gcc/testsuite/ PR c++/103012 * g++.dg/cpp/pr103012.C: New test. * g++.target/i386/pr103012.C: New test. Jakub --- gcc/c-family/c-cppbuiltin.c.jj 2021-11-25 08:35:39.855073852 +0100 +++ gcc/c-family/c-cppbuiltin.c 2021-12-28 12:16:15.774616659 +0100 @@ -589,6 +589,10 @@ c_cpp_builtins_optimize_pragma (cpp_read if (flag_undef) return; + /* Make sure all of the builtins about to be declared have + BUILTINS_LOCATION has their location_t. */ + cpp_force_token_locations (parse_in, BUILTINS_LOCATION); + /* Other target-independent built-ins determined by command-line options. */ if (!prev->x_optimize_size && cur->x_optimize_size) @@ -653,6 +657,8 @@ c_cpp_builtins_optimize_pragma (cpp_read cpp_define_unused (pfile, "__ROUNDING_MATH__"); else if (prev->x_flag_rounding_math && !cur->x_flag_rounding_math) cpp_undef (pfile, "__ROUNDING_MATH__"); + + cpp_stop_forcing_token_locations (parse_in); } --- gcc/config/i386/i386-c.c.jj 2021-09-08 09:55:28.732722638 +0200 +++ gcc/config/i386/i386-c.c 2021-12-28 12:26:21.491086880 +0100 @@ -702,12 +702,14 @@ ix86_pragma_target_parse (tree args, tre cur_tune = prev_tune = PROCESSOR_max; /* Undef all of the macros for that are no longer current. */ + cpp_force_token_locations (parse_in, BUILTINS_LOCATION); ix86_target_macros_internal (prev_isa & diff_isa, prev_isa2 & diff_isa2, prev_arch, prev_tune, (enum fpmath_unit) prev_opt->x_ix86_fpmath, cpp_undef); + cpp_stop_forcing_token_locations (parse_in); /* For the definitions, ensure all newly defined macros are considered as used for -Wunused-macros. There is no point warning about the @@ -717,12 +719,14 @@ ix86_pragma_target_parse (tree args, tre cpp_opts->warn_unused_macros = 0; /* Define all of the macros for new options that were just turned on. */ + cpp_force_token_locations (parse_in, BUILTINS_LOCATION); ix86_target_macros_internal (cur_isa & diff_isa, cur_isa2 & diff_isa2, cur_arch, cur_tune, (enum fpmath_unit) cur_opt->x_ix86_fpmath, cpp_define); + cpp_stop_forcing_token_locations (parse_in); cpp_opts->warn_unused_macros = saved_warn_unused_macros; --- gcc/config/arm/arm-c.c.jj 2021-12-27 10:59:22.542829758 +0100 +++ gcc/config/arm/arm-c.c 2021-12-28 12:30:26.043647229 +0100 @@ -464,7 +464,9 @@ arm_pragma_target_parse (tree args, tree acond_macro = get_identifier ("__ARM_FEATURE_LDREX"); C_CPP_HASHNODE (acond_macro)->flags |= NODE_CONDITIONAL; + cpp_force_token_locations (parse_in, BUILTINS_LOCATION); arm_cpu_builtins (parse_in); + cpp_stop_forcing_token_locations (parse_in); cpp_opts->warn_unused_macros = saved_warn_unused_macros; --- gcc/config/aarch64/aarch64-c.c.jj 2021-12-14 18:40:21.307135222 +0100 +++ gcc/config/aarch64/aarch64-c.c 2021-12-28 12:28:54.582931026 +0100 @@ -259,7 +259,9 @@ aarch64_pragma_target_parse (tree args, unsigned char saved_warn_unused_macros = cpp_opts->warn_unused_macros; cpp_opts->warn_unused_macros = 0; + cpp_force_token_locations (parse_in, BUILTINS_LOCATION); aarch64_update_cpp_builtins (parse_in); + cpp_stop_forcing_token_locations (parse_in); cpp_opts->warn_unused_macros = saved_warn_unused_macros; --- gcc/config/s390/s390-c.c.jj 2021-05-18 10:04:31.399436824 +0200 +++ gcc/config/s390/s390-c.c 2021-12-28 12:35:01.275794183 +0100 @@ -457,7 +457,9 @@ s390_pragma_target_parse (tree args, tre cpp_opts->warn_unused_macros = 0; /* Define all of the macros for new options that were just turned on. */ + cpp_force_token_locations (parse_in, BUILTINS_LOCATION); s390_cpu_cpp_builtins_internal (parse_in, cur_opt, prev_opt); + cpp_stop_forcing_token_locations (parse_in); cpp_opts->warn_unused_macros = saved_warn_unused_macros; } --- gcc/testsuite/g++.target/i386/pr103012.C.jj 2021-12-28 12:45:22.754093937 +0100 +++ gcc/testsuite/g++.target/i386/pr103012.C 2021-12-28 12:45:06.259324853 +0100 @@ -0,0 +1,19 @@ +// PR c++/103012 +// { dg-do compile } +// { dg-options "-mno-avx2" } + +int a = 1; +#pragma GCC target "avx2" +#define A(a) a + +#define B(a) A(a)A(a) +#define C(a) B(a)B(a) +#define D(a) C(a)C(a) +#define E(a) D(a)D(a) +#define F(a) E(a)E(a) +#define G(a) F(a)F(a) +#define H(a) G(a)G(a) +#define I(a) H(a)H(a) +#define J(a) I(a)I(a) +#define K(a) J(a)J(a) +#define L(a) K(a)K(a) +int b = L(a) 1; --- gcc/testsuite/g++.dg/cpp/pr103012.C.jj 2021-12-28 12:44:25.640893482 +0100 +++ gcc/testsuite/g++.dg/cpp/pr103012.C 2021-12-28 12:43:59.619257770 +0100 @@ -0,0 +1,18 @@ +// PR c++/103012 +// { dg-do compile } + +int a = 1; +#pragma GCC optimize "Og" +#define A(a) a + +#define B(a) A(a)A(a) +#define C(a) B(a)B(a) +#define D(a) C(a)C(a) +#define E(a) D(a)D(a) +#define F(a) E(a)E(a) +#define G(a) F(a)F(a) +#define H(a) G(a)G(a) +#define I(a) H(a)H(a) +#define J(a) I(a)I(a) +#define K(a) J(a)J(a) +#define L(a) K(a)K(a) +int b = L(a) 1;