From patchwork Mon Oct 9 10:37:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 823188 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-463739-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="HyU8itU6"; 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 3y9cCV1spCz9tXx for ; Mon, 9 Oct 2017 21:38:13 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to:cc :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=CaeoKkdWOKi8fznNiKN/xIQIgL2iEAgpUjN01SktW7neTOb5Uw WsW6HPmm+Zmlo6L+5UV1f/5XpRdpXBg1MtRJlk6qclzI3oxhbJKMepmuo0csvCS/ TxMhsBo4V4hlS2JklIF7R1YPDxDRZfGIOrMSDrcEe3FTWetaENUY9iUSA= 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:to:cc :from:subject:message-id:date:mime-version:content-type; s= default; bh=JPYDoJ33cXNEa2PHBmovv7pRweM=; b=HyU8itU63zkjhdXwQof8 mCu6j/A+D7CZHWN8I2Ze9Yl1+YjwArWcRewUiSE0JdtiYo6os+BdDSJ66IEh8vkJ X8/uQMP9XEp0XknAUeBewOBCKNcvWxSEJG8HRhH1zrsOXYjsHJg1M7/P//FvOmk0 6a/WsGaVLKt8xykVYrfns2M= Received: (qmail 10052 invoked by alias); 9 Oct 2017 10:38:04 -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 5997 invoked by uid 89); 9 Oct 2017 10:38:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-15.1 required=5.0 tests=BAYES_00, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=flattened, his X-HELO: userp1040.oracle.com Received: from userp1040.oracle.com (HELO userp1040.oracle.com) (156.151.31.81) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 09 Oct 2017 10:37:59 +0000 Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v99AbX4W011973 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 9 Oct 2017 10:37:34 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id v99AbXKH002333 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 9 Oct 2017 10:37:33 GMT Received: from abhmp0009.oracle.com (abhmp0009.oracle.com [141.146.116.15]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id v99AbWi5010776; Mon, 9 Oct 2017 10:37:32 GMT Received: from [192.168.1.15] (/87.11.217.181) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 09 Oct 2017 03:37:32 -0700 To: "gcc-patches@gcc.gnu.org" Cc: Nathan Sidwell , Jason Merrill , "Joseph S. Myers" From: Paolo Carlini Subject: [C++ Patch] PR 82466 ("Missing warning for re-declaration of built-in function as variable") Message-ID: <1715c8fd-ad86-39b4-770e-9318b27def0c@oracle.com> Date: Mon, 9 Oct 2017 12:37:29 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 X-IsSubscribed: yes Hi, this issue filed by Bernd is sort-of a small follow-up to his c++/71973: the C front-end already warns for things like:     int printf; where we are declaring in the global namespace a built-in. Beyond copying over to the C++ front-end the code already existing in the C front-end, there are a couple of nits: 1- We probably want to enable suppressing the warning, give it a name, for this I'm overloading the name already added by Bernd for c++/71973 (using it in the C front-end too, without changes to the default behavior, seems a very safe tweak to me); 2- as a pure C++ nit, we probably want to take into account namespaces, thus the additional check CP_DECL_CONTEXT (newdecl) == global_namespace and the new C++ test (for a couple of hours I had in the tree CP_DECL_CONTEXT (newdecl) == CP_DECL_CONTEXT (olddecl), probably unnecessarily more verbose!?). What else? A couple of existing C++ testcases needed rather obvious adjusting, note in particular g++.old-deja/g++.mike/p811.C, which has flattened in the global namespace a declaration 'extern ostream clog;', which, were in namespace std, as per the standard, would not trigger the new warning, even if 'clog' is also the name of a math builtin, thanks to the additional check mentioned above. Tested x86_64. Thanks, Paolo. ////////////////// 2017-10-09 Paolo Carlini PR c++/82466 * doc/invoke.texi ([Wbuiltin-declaration-mismatch]): Extend description. /cp 2017-10-19 Paolo Carlini PR c++/82466 * decl.c (duplicate_decls): Warn for built-in functions declared as non-function, use OPT_Wbuiltin_declaration_mismatch. * decl.c (duplicate_decls): Avoid redundant '+' in warning_at. /c 2017-10-19 Paolo Carlini PR c++/82466 * c-decl.c (diagnose_mismatched_decls): Use OPT_Wbuiltin_declaration_mismatch. /testsuite 2017-10-19 Paolo Carlini PR c++/82466 * c-c++-common/Wbuiltin-declaration-mismatch-1.c: New. * c-c++-common/Wno-builtin-declaration-mismatch-1.c: Likewise. * g++.dg/warn/Wbuiltin_declaration_mismatch-1.C: Likewise. * g++.dg/parse/builtin2.C: Adjust. * g++.old-deja/g++.mike/p811.C: Likewise. Index: c/c-decl.c =================================================================== --- c/c-decl.c (revision 253536) +++ c/c-decl.c (working copy) @@ -1837,7 +1837,8 @@ diagnose_mismatched_decls (tree newdecl, tree oldd locate_old_decl (olddecl); } else if (TREE_PUBLIC (newdecl)) - warning (0, "built-in function %q+D declared as non-function", + warning (OPT_Wbuiltin_declaration_mismatch, + "built-in function %q+D declared as non-function", newdecl); else warning (OPT_Wshadow, "declaration of %q+D shadows " Index: cp/decl.c =================================================================== --- cp/decl.c (revision 253536) +++ cp/decl.c (working copy) @@ -1431,7 +1431,15 @@ duplicate_decls (tree newdecl, tree olddecl, bool /* Avoid warnings redeclaring built-ins which have not been explicitly declared. */ if (DECL_ANTICIPATED (olddecl)) - return NULL_TREE; + { + if (TREE_PUBLIC (newdecl) + && CP_DECL_CONTEXT (newdecl) == global_namespace) + warning_at (DECL_SOURCE_LOCATION (newdecl), + OPT_Wbuiltin_declaration_mismatch, + "built-in function %qD declared as non-function", + newdecl); + return NULL_TREE; + } /* If you declare a built-in or predefined function name as static, the old definition is overridden, but optionally warn this was a @@ -1522,7 +1530,7 @@ next_arg:; warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wbuiltin_declaration_mismatch, - "declaration of %q+#D conflicts with built-in " + "declaration of %q#D conflicts with built-in " "declaration %q#D", newdecl, olddecl); } else if ((DECL_EXTERN_C_P (newdecl) Index: doc/invoke.texi =================================================================== --- doc/invoke.texi (revision 253536) +++ doc/invoke.texi (working copy) @@ -6313,7 +6313,8 @@ attributes. @item -Wno-builtin-declaration-mismatch @opindex Wno-builtin-declaration-mismatch @opindex Wbuiltin-declaration-mismatch -Warn if a built-in function is declared with the wrong signature. +Warn if a built-in function is declared with the wrong signature or +as non-function. This warning is enabled by default. @item -Wno-builtin-macro-redefined Index: testsuite/c-c++-common/Wbuiltin-declaration-mismatch-1.c =================================================================== --- testsuite/c-c++-common/Wbuiltin-declaration-mismatch-1.c (nonexistent) +++ testsuite/c-c++-common/Wbuiltin-declaration-mismatch-1.c (working copy) @@ -0,0 +1,4 @@ +/* PR c++/82466 */ +/* { dg-options "-Wbuiltin-declaration-mismatch" } */ + +int printf; /* { dg-warning "declared as non-function" } */ Index: testsuite/c-c++-common/Wno-builtin-declaration-mismatch-1.c =================================================================== --- testsuite/c-c++-common/Wno-builtin-declaration-mismatch-1.c (nonexistent) +++ testsuite/c-c++-common/Wno-builtin-declaration-mismatch-1.c (working copy) @@ -0,0 +1,4 @@ +/* PR c++/82466 */ +/* { dg-options "-Wno-builtin-declaration-mismatch" } */ + +int printf; Index: testsuite/g++.dg/parse/builtin2.C =================================================================== --- testsuite/g++.dg/parse/builtin2.C (revision 253536) +++ testsuite/g++.dg/parse/builtin2.C (working copy) @@ -1,5 +1,5 @@ // PR c++/14432 -// { dg-options "" } +// { dg-options "-Wno-builtin-declaration-mismatch" } struct Y {}; Y y1; Index: testsuite/g++.dg/warn/Wbuiltin_declaration_mismatch-1.C =================================================================== --- testsuite/g++.dg/warn/Wbuiltin_declaration_mismatch-1.C (nonexistent) +++ testsuite/g++.dg/warn/Wbuiltin_declaration_mismatch-1.C (working copy) @@ -0,0 +1,7 @@ +// PR c++/82466 +// { dg-options "-Wbuiltin-declaration-mismatch" } + +namespace N +{ + int printf; +} Index: testsuite/g++.old-deja/g++.mike/p811.C =================================================================== --- testsuite/g++.old-deja/g++.mike/p811.C (revision 253536) +++ testsuite/g++.old-deja/g++.mike/p811.C (working copy) @@ -1,5 +1,5 @@ // { dg-do assemble } -// { dg-options "" } +// { dg-options "-Wno-builtin-declaration-mismatch" } // This test case caused the compiler to abort at one point in time. // prms-id: 811