From patchwork Wed Jul 4 10:53:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 939243 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-481000-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=hotmail.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="dE282FaQ"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41LHtJ3nmhz9s1b for ; Wed, 4 Jul 2018 20:54:14 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:content-type:mime-version; q=dns; s=default; b=YsDot+alGrWb8X1nlrZlWgdHzdwPk9yvdR+4keL4YfOe/W+RK1 RU4svC7XDCciZd8+QPng+oamG3kPnU9c2o4R1LS2ShHvJg+MdPYwDIbGLcd2+8qs 6Dx6lvVhx4YiqmRIAosz3c1Y6qhwxILlAYVBL3eNlPSfiDJYhoh9s1IrE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:content-type:mime-version; s= default; bh=ry+9LOUk1Es8GejwglLgs3zQfQA=; b=dE282FaQMtG3pjRMyRNP Ib+slOJqb+1n2vZOMipFqM1ZenGO3t7Jus09f6hAIhDLOkyCCB3G6r3UeRwU9XTG 5nG1Rdr4TjOqmts5AVo35r2RCplxqL1/pNsJCati/2KJvGIiWmT2cka32Sx/piPv qAupRrw0RXbvtW7rTv2HT9I= Received: (qmail 46431 invoked by alias); 4 Jul 2018 10:54:06 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 45222 invoked by uid 89); 4 Jul 2018 10:54:05 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.6 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=accident, spelling, wattributes, Wattributes X-HELO: EUR02-HE1-obe.outbound.protection.outlook.com Received: from mail-oln040092068011.outbound.protection.outlook.com (HELO EUR02-HE1-obe.outbound.protection.outlook.com) (40.92.68.11) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 04 Jul 2018 10:54:03 +0000 Received: from AM5EUR02FT006.eop-EUR02.prod.protection.outlook.com (10.152.8.58) by AM5EUR02HT185.eop-EUR02.prod.protection.outlook.com (10.152.9.63) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.20.906.15; Wed, 4 Jul 2018 10:54:00 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com (10.152.8.58) by AM5EUR02FT006.mail.protection.outlook.com (10.152.8.77) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.20.906.15 via Frontend Transport; Wed, 4 Jul 2018 10:54:00 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::106:1799:3361:8e9a]) by AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::106:1799:3361:8e9a%2]) with mapi id 15.20.0930.016; Wed, 4 Jul 2018 10:54:00 +0000 From: Bernd Edlinger To: GCC Patches CC: David Malcolm , Richard Biener , Jakub Jelinek , Jeff Law Subject: [PATCH] Fix _Pragma GCC diagnostic in macro expansions Date: Wed, 4 Jul 2018 10:53:59 +0000 Message-ID: received-spf: None (protection.outlook.com: hotmail.de does not designate permitted sender hosts) authentication-results: spf=none (sender IP is ) smtp.mailfrom=bernd.edlinger@hotmail.de; MIME-Version: 1.0 Hi, currently _Pragma("GCC diagnostic ...") does not properly work in macro expansions. Consider the following code: #define B _Pragma("GCC diagnostic push") \ _Pragma("GCC diagnostic ignored \"-Wattributes\"") #define E _Pragma("GCC diagnostic pop") #define X() B int __attribute((unknown_attr)) x; E /* { dg-bogus "attribute directive ignored" } */ void test1(void) { X() /* { dg-bogus "in expansion of macro" } */ } Warnings happen in C++ despite the _Pragma, while C happens to suppress the warnings more or less by accident. This is connected to the fact that GCC uses the location of the closing parenthesis of the function-like macro expansion in the _Pragma, while the rest of the locations are relative to the macro expansion point, which is the letter X in this case. This patch changes the location of builtin macros and _Pragma to use the macro expansion point instead of the closing parenthesis. A few test cases had to be adjusted, most changes were necessary because the __LINE__ location moved to the macro expansion point, which looks like a straight improvement. In pr61817-2.c the location of __LINE__ depends on -ftrack-macro-expansion, when enabled the location of the macro argument is the spelling location, while all other locations change to the macro expansion point. The C++ pagma plugin.c is also affected by the change, because the input_location is now the spelling location of _Pragma in DO_PRAGMA and has to be converted to the expansion point of the macro to get the expected result. Bootstrapped and reg-tested on x86_64-pc-linux-gnu. Is it OK for trunk? Thanks Bernd. libcpp: 2018-07-04 Bernd Edlinger * macro.c (enter_macro_context): Change the location info for builtin macros from location of the closing parenthesis to location of the macro expansion point. testsuite: 2018-07-04 Bernd Edlinger * c-c++-common/cpp/diagnostic-pragma-2.c: New test. * c-c++-common/pr69558.c: Remove xfail. * gcc.dg/cpp/builtin-macro-1.c: Adjust test expectations. * gcc.dg/pr61817-1.c: Likewise. * gcc.dg/pr61817-2.c: Likewise. * g++.dg/plugin/pragma_plugin.c: Warn at expansion_point_location. Index: gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-2.c =================================================================== --- gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-2.c (revision 0) +++ gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-2.c (working copy) @@ -0,0 +1,14 @@ +/* { dg-do compile } */ + +#define B _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wattributes\"") +#define E _Pragma("GCC diagnostic pop") + +#define X() B int __attribute((unknown_attr)) x; E /* { dg-bogus "attribute directive ignored" } */ +#define Y B int __attribute((unknown_attr)) y; E /* { dg-bogus "attribute directive ignored" } */ + +void test1(void) +{ + X() /* { dg-bogus "in expansion of macro" } */ + Y /* { dg-bogus "in expansion of macro" } */ +} Index: gcc/testsuite/c-c++-common/pr69558.c =================================================================== --- gcc/testsuite/c-c++-common/pr69558.c (revision 262287) +++ gcc/testsuite/c-c++-common/pr69558.c (working copy) @@ -11,9 +11,9 @@ _Pragma ("GCC diagnostic pop") #define C(x) \ A \ - static inline void bar (void) { x (); } /* { dg-bogus "in definition of|deprecated" "" { xfail { c++ } } } */ \ + static inline void bar (void) { x (); } /* { dg-bogus "in definition of|deprecated" "" } */ \ B -__attribute__((deprecated)) void foo (void); /* { dg-bogus "declared here" "" { xfail { c++ } } } */ +__attribute__((deprecated)) void foo (void); /* { dg-bogus "declared here" "" } */ C (foo) /* { dg-bogus "is deprecated" } */ Index: gcc/testsuite/gcc.dg/cpp/builtin-macro-1.c =================================================================== --- gcc/testsuite/gcc.dg/cpp/builtin-macro-1.c (revision 262287) +++ gcc/testsuite/gcc.dg/cpp/builtin-macro-1.c (working copy) @@ -1,8 +1,8 @@ /* Origin PR preprocessor/64803 This test ensures that the value the __LINE__ macro expands to is - constant and corresponds to the line of the closing parenthesis of - the top-most function-like macro expansion it's part of. + constant and corresponds to the line of the macro expansion point + the function-like macro expansion it's part of. { dg-do run } { do-options -no-integrated-cpp } */ @@ -19,8 +19,8 @@ M(a ); - assert(L20 == 20); /* 20 is the line number of the - closing parenthesis of the + assert(L19 == 19); /* 19 is the line number of the + macro expansion point of the invocation of the M macro. Please adjust in case the layout of this file changes. */ Index: gcc/testsuite/gcc.dg/pr61817-1.c =================================================================== --- gcc/testsuite/gcc.dg/pr61817-1.c (revision 262287) +++ gcc/testsuite/gcc.dg/pr61817-1.c (working copy) @@ -14,6 +14,6 @@ enum { ) }; -A(a == 15); -A(b == 15); -A(c == 15); +A(a == 10); +A(b == 10); +A(c == 10); Index: gcc/testsuite/gcc.dg/pr61817-2.c =================================================================== --- gcc/testsuite/gcc.dg/pr61817-2.c (revision 262287) +++ gcc/testsuite/gcc.dg/pr61817-2.c (working copy) @@ -14,6 +14,6 @@ enum { ) }; -A(a == 15); -A(b == 15); -A(c == 15); +A(a == 10); +A(b == 10); +A(c == 14); Index: gcc/testsuite/g++.dg/plugin/pragma_plugin.c =================================================================== --- gcc/testsuite/g++.dg/plugin/pragma_plugin.c (revision 262287) +++ gcc/testsuite/g++.dg/plugin/pragma_plugin.c (working copy) @@ -33,14 +33,15 @@ handle_pragma_sayhello (cpp_reader *dumm } if (TREE_STRING_LENGTH (message) > 1) { + location_t loc = expansion_point_location (input_location); if (cfun) - warning (OPT_Wpragmas, - "% from function %qE: %s", - cfun->decl, TREE_STRING_POINTER (message)); + warning_at (loc, OPT_Wpragmas, + "% from function %qE: %s", + cfun->decl, TREE_STRING_POINTER (message)); else - warning (OPT_Wpragmas, - "% outside of function: %s", - TREE_STRING_POINTER (message)); + warning_at (loc, OPT_Wpragmas, + "% outside of function: %s", + TREE_STRING_POINTER (message)); } } Index: libcpp/macro.c =================================================================== --- libcpp/macro.c (revision 262287) +++ libcpp/macro.c (working copy) @@ -1410,29 +1410,25 @@ enter_macro_context (cpp_reader *pfile, pfile->about_to_expand_macro_p = false; /* Handle built-in macros and the _Pragma operator. */ { - source_location loc, expand_loc; + source_location expand_loc; if (/* The top-level macro invocation that triggered the expansion - we are looking at is with a standard macro ...*/ + we are looking at is with a standard macro ... */ !(pfile->top_most_macro_node->flags & NODE_BUILTIN) - /* ... and it's a function-like macro invocation. */ - && pfile->top_most_macro_node->value.macro->fun_like) - { - /* Then the location of the end of the macro invocation is the - location of the closing parenthesis. */ - loc = pfile->cur_token[-1].src_loc; - expand_loc = loc; - } + /* ... and it's a function-like macro invocation, */ + && pfile->top_most_macro_node->value.macro->fun_like + /* ... and we are tracking the macro expansion. */ + && CPP_OPTION (pfile, track_macro_expansion)) + /* Then the location of the end of the macro invocation is the + location of the expansion point of this macro. */ + expand_loc = location; else - { - /* Otherwise, the location of the end of the macro invocation is - the location of the expansion point of that top-level macro - invocation. */ - loc = location; - expand_loc = pfile->invocation_location; - } + /* Otherwise, the location of the end of the macro invocation is + the location of the expansion point of that top-level macro + invocation. */ + expand_loc = pfile->invocation_location; - return builtin_macro (pfile, node, loc, expand_loc); + return builtin_macro (pfile, node, location, expand_loc); } }