From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 114435 invoked by alias); 31 Oct 2019 17:17:33 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 114406 invoked by uid 89); 31 Oct 2019 17:17:33 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.6 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_2,GIT_PATCH_3 autolearn=ham version=3.3.1 spammy=became, array_size X-HELO: us-smtp-delivery-1.mimecast.com Received: from us-smtp-2.mimecast.com (HELO us-smtp-delivery-1.mimecast.com) (205.139.110.61) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 31 Oct 2019 17:17:31 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1572542250; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Pz5Vd/zIwAFN1PUzQRvmtl1Qe+KiMIovYq0HQAYtnmU=; b=LM4EIDn/BhVu0WPlyfZCbK52V/C72sa+Ylx/RlcMugKG7M9WLzNK8YST+l3vAi4rF74DVW Emh21n5oqeSAPtQoHpOfIMCD7oY5RZ3WMtZCg3CbABwWMn6gI4oz8Cbgaqu1RiO6GULiqc T1xKsquGrrfSC3bsmW6mzPZkOlePtOs= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-234-kTKmadCxO-SkLKh_SzfUYg-1; Thu, 31 Oct 2019 13:17:27 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2FCDD1005500; Thu, 31 Oct 2019 17:17:26 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.36.118.135]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6037B5DA60; Thu, 31 Oct 2019 17:17:23 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id x9VHHKtf031570; Thu, 31 Oct 2019 18:17:20 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id x9VHHJlj031569; Thu, 31 Oct 2019 18:17:19 +0100 Date: Thu, 31 Oct 2019 17:18:00 -0000 From: Jakub Jelinek To: "Joseph S. Myers" , Marek Polacek , Jason Merrill , Nathan Sidwell , Jeff Law Cc: gcc-patches@gcc.gnu.org, Martin Sebor Subject: [PATCH] Fix libcpp #pragma {push,pop}_macro handling of builtin macros (PR preprocessor/92296) Message-ID: <20191031171719.GN4650@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 User-Agent: Mutt/1.11.3 (2019-02-01) X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-IsSubscribed: yes X-SW-Source: 2019-10/txt/msg02248.txt.bz2 Hi! Seems libcpp has been ICEing on push_macro of a builtin macro since 2010, but it became more urgent with Martin's __has_builtin builtin macro addition, because the mingw headers already do use #pragma push_macro("__has_builtin") #ifndef __has_builtin #define __has_builtin(x) 0 #endif ... #pragma pop_macro("__has_builtin") and so GCC ICEs any time it sees that. The following patch fixes it by handling builtin macros properly, noting that they were builtin previously and when restoring them into the builtin state from whatever other state changing them according to the builtin_array. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2019-10-31 Jakub Jelinek PR preprocessor/92296 * internal.h (struct def_pragma_macro): Add is_builtin bitfield. (_cpp_restore_special_builtin): Declare. * init.c (_cpp_restore_special_builtin): New function. * directives.c (do_pragma_push_macro): For NT_BUILTIN_MACRO set is_builtin and don't try to grab definition. (cpp_pop_definition): Use _cpp_restore_special_builtin to restore builtin macros. * c-c++-common/cpp/pr92296-1.c: New test. * c-c++-common/cpp/pr92296-2.c: New test. --- libcpp/internal.h.jj 2019-10-03 00:26:24.755766153 +0200 +++ libcpp/internal.h 2019-10-31 12:14:58.554037427 +0100 @@ -391,6 +391,8 @@ struct def_pragma_macro { =20 /* Mark if we save an undefined macro. */ unsigned int is_undef : 1; + /* Nonzero if it was a builtin macro. */ + unsigned int is_builtin : 1; }; =20 /* A cpp_reader encapsulates the "state" of a pre-processor run. @@ -722,6 +724,8 @@ extern void *_cpp_commit_buff (cpp_reade /* In init.c. */ extern void _cpp_maybe_push_include_file (cpp_reader *); extern const char *cpp_named_operator2name (enum cpp_ttype type); +extern void _cpp_restore_special_builtin (cpp_reader *pfile, + struct def_pragma_macro *); =20 /* In directives.c */ extern int _cpp_test_assertion (cpp_reader *, unsigned int *); --- libcpp/init.c.jj 2019-10-29 09:26:09.740514434 +0100 +++ libcpp/init.c 2019-10-31 12:14:40.954308484 +0100 @@ -497,6 +497,25 @@ cpp_init_special_builtins (cpp_reader *p } } =20 +/* Restore macro C to builtin macro definition. */ + +void +_cpp_restore_special_builtin (cpp_reader *pfile, struct def_pragma_macro *= c) +{ + const struct builtin_macro *b; + size_t len =3D strlen (c->name); + + for (b =3D builtin_array; b < builtin_array + ARRAY_SIZE (builtin_array)= ; b++) + if (b->len =3D=3D len && memcmp (c->name, b->name, len + 1) =3D=3D 0) + { + cpp_hashnode *hp =3D cpp_lookup (pfile, b->name, b->len); + hp->type =3D NT_BUILTIN_MACRO; + if (b->always_warn_if_redefined) + hp->flags |=3D NODE_WARN; + hp->value.builtin =3D (enum cpp_builtin_type) b->value; + } +} + /* Read the builtins table above and enter them, and language-specific macros, into the hash table. HOSTED is true if this is a hosted environment. */ --- libcpp/directives.c.jj 2019-09-09 12:22:36.965261910 +0200 +++ libcpp/directives.c 2019-10-31 12:15:15.223780690 +0100 @@ -1582,6 +1582,8 @@ do_pragma_push_macro (cpp_reader *pfile) node =3D _cpp_lex_identifier (pfile, c->name); if (node->type =3D=3D NT_VOID) c->is_undef =3D 1; + else if (node->type =3D=3D NT_BUILTIN_MACRO) + c->is_builtin =3D 1; else { defn =3D cpp_macro_definition (pfile, node); @@ -2470,6 +2472,11 @@ cpp_pop_definition (cpp_reader *pfile, s =20 if (c->is_undef) return; + if (c->is_builtin) + { + _cpp_restore_special_builtin (pfile, c); + return; + } =20 { size_t namelen; --- gcc/testsuite/c-c++-common/cpp/pr92296-1.c.jj 2019-10-31 12:45:41.97568= 8125 +0100 +++ gcc/testsuite/c-c++-common/cpp/pr92296-1.c 2019-10-31 12:26:12.05166472= 3 +0100 @@ -0,0 +1,35 @@ +/* PR preprocessor/92296 */ +/* { dg-do preprocess } */ + +#pragma push_macro("__TIMESTAMP__") +#pragma pop_macro("__TIMESTAMP__") + +#pragma push_macro("__TIME__") +#pragma pop_macro("__TIME__") + +#pragma push_macro("__DATE__") +#pragma pop_macro("__DATE__") + +#pragma push_macro("__FILE__") +#pragma pop_macro("__FILE__") + +#pragma push_macro("__BASE_FILE__") +#pragma pop_macro("__BASE_FILE__") + +#pragma push_macro("__LINE__") +#pragma pop_macro("__LINE__") + +#pragma push_macro("__INCLUDE_LEVEL__") +#pragma pop_macro("__INCLUDE_LEVEL__") + +#pragma push_macro("__COUNTER__") +#pragma pop_macro("__COUNTER__") + +#pragma push_macro("__has_attribute") +#pragma pop_macro("__has_attribute") + +#pragma push_macro("__has_cpp_attribute") +#pragma pop_macro("__has_cpp_attribute") + +#pragma push_macro("__has_builtin") +#pragma pop_macro("__has_builtin") --- gcc/testsuite/c-c++-common/cpp/pr92296-2.c.jj 2019-10-31 12:45:45.08164= 0352 +0100 +++ gcc/testsuite/c-c++-common/cpp/pr92296-2.c 2019-10-31 12:52:56.04601122= 4 +0100 @@ -0,0 +1,80 @@ +/* PR preprocessor/92296 */ +/* { dg-do preprocess } */ +/* { dg-options "-Wno-builtin-macro-redefined" } */ + +#pragma push_macro("__TIMESTAMP__") +#undef __TIMESTAMP__ +#define __TIMESTAMP__ "Thu Oct 31 12:00:00 2019" +timestamp1 =3D __TIMESTAMP__ +#pragma pop_macro("__TIMESTAMP__") +timestamp2 =3D __TIMESTAMP__ + +#pragma push_macro("__TIME__") +#undef __TIME__ +#define __TIME__ "12:00:00" +time1 =3D __TIME__ +#pragma pop_macro("__TIME__") +time2 =3D __TIME__ + +#pragma push_macro("__DATE__") +#undef __DATE__ +#define __DATE__ "Oct 31 2019" +date1 =3D __DATE__ +#pragma pop_macro("__DATE__") +date2 =3D __DATE__ + +#pragma push_macro("__FILE__") +#undef __FILE__ +#define __FILE__ "pr92296-3.c" +file1 =3D __FILE__ /* { dg-final { scan-file pr92296-2.i "file1 =3D \"pr92= 296-3.c\"" } } */ +#pragma pop_macro("__FILE__") +file2 =3D __FILE__ /* { dg-final { scan-file-not pr92296-2.i "file2 =3D \"= pr92296-3.c\"" } } */ + +#pragma push_macro("__BASE_FILE__") +#undef __BASE_FILE__ +#define __BASE_FILE__ "pr92296-4.c" +filebase1 =3D __BASE_FILE__ /* { dg-final { scan-file pr92296-2.i "filebas= e1 =3D \"pr92296-4.c\"" } } */ +#pragma pop_macro("__BASE_FILE__") +filebase2 =3D __BASE_FILE__ /* { dg-final { scan-file-not pr92296-2.i "fil= ebase2 =3D \"pr92296-4.c\"" } } */ + +#pragma push_macro("__LINE__") +#undef __LINE__ /* { dg-warning "undefining" } */ +#define __LINE__ 142 +line1 =3D __LINE__ /* { dg-final { scan-file pr92296-2.i "line1 =3D 142" }= } */ +#pragma pop_macro("__LINE__") +line2 =3D __LINE__ /* { dg-final { scan-file pr92296-2.i "line2 =3D 45" } = } */ + +#pragma push_macro("__INCLUDE_LEVEL__") +#undef __INCLUDE_LEVEL__ /* { dg-warning "undefining" } */ +#define __INCLUDE_LEVEL__ 42 +includelevel1 =3D __INCLUDE_LEVEL__ /* { dg-final { scan-file pr92296-2.i = "includelevel1 =3D 42" } } */ +#pragma pop_macro("__INCLUDE_LEVEL__") +includelevel2 =3D __INCLUDE_LEVEL__ /* { dg-final { scan-file pr92296-2.i = "includelevel2 =3D 0" } } */ + +#pragma push_macro("__COUNTER__") +#undef __COUNTER__ /* { dg-warning "undefining" } */ +#define __COUNTER__ 172 +counter1 =3D __COUNTER__ /* { dg-final { scan-file pr92296-2.i "counter1 = =3D 172" } } */ +#pragma pop_macro("__COUNTER__") +counter2 =3D __COUNTER__ /* { dg-final { scan-file-not pr92296-2.i "counte= r2 =3D 172" } } */ + +#pragma push_macro("__has_attribute") +#undef __has_attribute /* { dg-warning "undefining" } */ +#define __has_attribute(x) 0 +hasattr1 =3D __has_attribute(noreturn) /* { dg-final { scan-file pr92296-2= .i "hasattr1 =3D 0" } } */ +#pragma pop_macro("__has_attribute") +hasattr2 =3D __has_attribute(noreturn) /* { dg-final { scan-file-not pr922= 96-2.i "hasattr2 =3D 0" } } */ + +#pragma push_macro("__has_cpp_attribute") +#undef __has_cpp_attribute /* { dg-warning "undefining" } */ +#define __has_cpp_attribute(x) 0 +hasattrcpp1 =3D __has_cpp_attribute(noreturn) /* { dg-final { scan-file pr= 92296-2.i "hasattrcpp1 =3D 0" } } */ +#pragma pop_macro("__has_cpp_attribute") +hasattrcpp2 =3D __has_cpp_attribute(noreturn) /* { dg-final { scan-file-no= t pr92296-2.i "hasattrcpp2 =3D 0" } } */ + +#pragma push_macro("__has_builtin") +#undef __has_builtin /* { dg-warning "undefining" } */ +#define __has_builtin(x) 0 +hasbuiltin1 =3D __has_builtin(__builtin_expect) /* { dg-final { scan-file = pr92296-2.i "hasbuiltin1 =3D 0" } } */ +#pragma pop_macro("__has_builtin") +hasbuiltin2 =3D __has_builtin(__builtin_expect) /* { dg-final { scan-file = pr92296-2.i "hasbuiltin2 =3D 1" } } */ Jakub