From patchwork Tue Sep 25 14:25:08 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 186825 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 0DFCF2C0094 for ; Wed, 26 Sep 2012 00:25:46 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1349187947; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Message-ID:Date:From:User-Agent:MIME-Version:To:Subject: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=5Qltbb4 aXApbp8c3i3q4xBZLL40=; b=gTZaYikpHHgSh0o7LzxybwVeTWF6CICvX3R/zVr WWmuHiAxj5fClSVzCQRaaG0HDjZeu/g2mKT/DJp7w8QNUFz2Jo5Am1Z/so/K+X8g rVp9cjyA2RGFwdGCdAbe8xtFHMNK1/2LRUdXFs7298lrU2zv+y16SSiyIN7LWlXE V4r0= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:Content-Type:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=aNJ2f3OYBP3dgVyKlN5Ievyr4+bdOneW5wyWGWFkDizCS0QOUzVF2I+O6rsO61 nhaizJ/tIINw8vaSnPISsKN0RYbnj/Lgsjdh4GWStaNJnx85G8HgyDTPIEInSGjX LS1FTibYYJi1Y+TiSRjm1CWwdCi88Ui79PhpSEXO17xVU=; Received: (qmail 27467 invoked by alias); 25 Sep 2012 14:25:38 -0000 Received: (qmail 27394 invoked by uid 22791); 25 Sep 2012 14:25:35 -0000 X-SWARE-Spam-Status: No, hits=-7.4 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, RP_MATCHES_RCVD, SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 25 Sep 2012 14:25:10 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q8PEPAmf001892 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 25 Sep 2012 10:25:10 -0400 Received: from fweimer.str.redhat.com (dhcp-5-241.str.redhat.com [10.32.5.241]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q8PEP8Qv016292 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 25 Sep 2012 10:25:09 -0400 Message-ID: <5061BEC4.7080506@redhat.com> Date: Tue, 25 Sep 2012 16:25:08 +0200 From: Florian Weimer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120911 Thunderbird/15.0.1 MIME-Version: 1.0 To: gcc-patches@gcc.gnu.org Subject: [CPP] Add pragmas for emitting diagnostics X-IsSubscribed: yes 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 This patch adds support for #pragma GCC warning and #pragma GCC error. These pragmas can be used from preprocessor macros, unlike the existing #warning and #error directives. Library authors can use these pragmas to add deprecation warnings to macros they define. Bootstrapped and tested on x86_64-redhat-linux-gnu, with no apparent regressions. Okay for trunk? gcc/ChangeLog: 2012-09-25 Florian Weimer * doc/cpp.texi (Pragmas): Document #pragma GCC warning, #pragma GCC error. gcc/testsuite/ChangeLog: 2012-09-25 Florian Weimer * c-c++-common/cpp/diagnostic-pragma-1.c: New testcase. libcpp/ChangeLog: 2012-09-25 Florian Weimer * directives.c (do_pragma_warning_or_error): New. (do_pragma_warning): New. (do_pragma_error): New. (_cpp_init_internal_pragmas): Register new pragmas. Index: gcc/doc/cpp.texi =================================================================== --- gcc/doc/cpp.texi (revision 191700) +++ gcc/doc/cpp.texi (working copy) @@ -3634,6 +3634,16 @@ current file to be treated as if it came from a system header. @xref{System Headers}. +@item #pragma GCC warning +@itemx #pragma GCC error +@code{#pragma GCC warning "message"} causes the preprocessor to issue +a warning diagnostic with the text @samp{message}. The message +contained in the pragma must be a single string literal. Similary, +@code{#pragma GCC error "message"} issues an error message. Unlike +the @samp{#warning} and @samp{#error} directives provided by +compilers, these pragmas can be embedded in preprocessor macros using +@samp{_Pragma}. + @end ftable @node Other Directives Index: gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c =================================================================== --- gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c (revision 0) +++ gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c (working copy) @@ -0,0 +1,11 @@ +// { dg-do compile } + +#pragma GCC warning "warn-a" // { dg-warning warn-a } +#pragma GCC error "err-b" // { dg-error err-b } + +#define CONST1 _Pragma("GCC warning \"warn-c\"") 1 +#define CONST2 _Pragma("GCC error \"err-d\"") 2 + +char a[CONST1]; // { dg-warning warn-c } +char b[CONST2]; // { dg-error err-d } + Index: libcpp/directives.c =================================================================== --- libcpp/directives.c (revision 191700) +++ libcpp/directives.c (working copy) @@ -1,7 +1,5 @@ /* CPP Library. (Directive handling.) - Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2001, 2002, 2003, 2004, 2005, - 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. + Copyright (C) 1986-2012 Free Software Foundation, Inc. Contributed by Per Bothner, 1994-95. Based on CCCP program by Paul Rubin, June 1986 Adapted to ANSI C, Richard Stallman, Jan 1987 @@ -118,6 +116,9 @@ static void do_pragma_poison (cpp_reader *); static void do_pragma_system_header (cpp_reader *); static void do_pragma_dependency (cpp_reader *); +static void do_pragma_warning_or_error (cpp_reader *, bool error); +static void do_pragma_warning (cpp_reader *); +static void do_pragma_error (cpp_reader *); static void do_linemarker (cpp_reader *); static const cpp_token *get_token_no_padding (cpp_reader *); static const cpp_token *get__Pragma_string (cpp_reader *); @@ -1263,6 +1264,8 @@ register_pragma_internal (pfile, "GCC", "system_header", do_pragma_system_header); register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency); + register_pragma_internal (pfile, "GCC", "warning", do_pragma_warning); + register_pragma_internal (pfile, "GCC", "error", do_pragma_error); } /* Return the number of registered pragmas in PE. */ @@ -1634,6 +1637,42 @@ free ((void *) fname); } +/* Issue a diagnostic with the message taken from the pragma. If + ERROR is true, the diagnostic is a warning, otherwise, it is an + error. */ +static void +do_pragma_warning_or_error (cpp_reader *pfile, bool error) +{ + const cpp_token *tok = _cpp_lex_token (pfile); + cpp_string str; + if (tok->type != CPP_STRING + || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str, + CPP_STRING) + || str.len == 0) + { + cpp_error (pfile, CPP_DL_ERROR, "invalid #pragma GCC %s directive", + error ? "error" : "warning"); + return; + } + cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING, + "%s", str.text); + free ((void *)str.text); +} + +/* Issue a warning diagnostic. */ +static void +do_pragma_warning (cpp_reader *pfile) +{ + do_pragma_warning_or_error (pfile, false); +} + +/* Issue an error diagnostic. */ +static void +do_pragma_error (cpp_reader *pfile) +{ + do_pragma_warning_or_error (pfile, true); +} + /* Get a token but skip padding. */ static const cpp_token * get_token_no_padding (cpp_reader *pfile)