From patchwork Mon Sep 30 15:16:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephan Bergmann X-Patchwork-Id: 1169468 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-509882-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="I13lxePN"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="TOAEQ/ZQ"; 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 46hmFz6Q26z9sP7 for ; Tue, 1 Oct 2019 01:16:34 +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:to :from:subject:message-id:date:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=U1YEpuVp39L+2qUK fxUIxrMFDZH8Vt/HMNiL43jLWKlK2SDT8JuXT8vOC68tmXyzmdLbJicEAl9/DVSz 77bXrWQ8dJs9scCrfbpZBtdC4LuMGzfbMu0cMAqYjJFakDWsIjNzL54fMH0m1PlZ eTlpUTaaZ8szxV5Gj5abW7wEaY8= 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 :from:subject:message-id:date:mime-version:content-type :content-transfer-encoding; s=default; bh=+GAhrvaqGtT55AxgM5PL8G fIguM=; b=I13lxePNRGnSQpXDezDz747C6Rytkog9K1frMoBOxu8ZYrCWenT0i6 n6z3TdtIkjWvhr4VwJJQCSrOM5/BT7Vs5KknQX601OB5+kMV7Vw6W+xahhuwHKfy meAFYeMOz3VDwP76ZtoHOE1/P5kaRf4/77ASccGw8JYvLt+QnrgVQ= Received: (qmail 48324 invoked by alias); 30 Sep 2019 15:16:27 -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 48297 invoked by uid 89); 30 Sep 2019 15:16:24 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT autolearn=ham version=3.3.1 spammy=submission, bear, HX-Languages-Length:3948, highlights X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 30 Sep 2019 15:16:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1569856581; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2ivcluZbvGRANemp+3uQqhVxzSCAqrggJpSVYvUWOfw=; b=TOAEQ/ZQA7hWFmtJ6Nf6l1ESLN/jDoJUwIp1/7b992GBv//QE9ZT1wio5pkvm10XQXVVg3 e/C5oCUApnrH6GCsHeZmxfq6hqOZn/rRXjxkalLdXKut3ENt+EASiaqYfEmz7C3pyuBG/A lGJyKvvYbjva6wkVNMjDaRpfxsEdB+w= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-228-eRHZXxzIN4ie7y1oR6_kNQ-1; Mon, 30 Sep 2019 11:16:18 -0400 Received: by mail-wr1-f69.google.com with SMTP id w8so4675211wrm.3 for ; Mon, 30 Sep 2019 08:16:18 -0700 (PDT) Received: from [192.168.188.21] (x4d0a3256.dyn.telefonica.de. [77.10.50.86]) by smtp.gmail.com with ESMTPSA id v16sm16436007wrt.12.2019.09.30.08.16.15 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Sep 2019 08:16:15 -0700 (PDT) To: gcc-patches@gcc.gnu.org From: Stephan Bergmann Subject: [WIP PATCH] PR71102: Make #pragma GCC error/warning concat strings Message-ID: <621f37aa-1ac5-05aa-674b-0d387bece69a@redhat.com> Date: Mon, 30 Sep 2019 17:16:14 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-IsSubscribed: yes (I think this is my first patch submission here, so please bear with me.) As discussed at "_Pragma("GCC warning ...") should concatenate string literals", it would be useful if #pragma GCC error/warning behaved like #pragma message and consumed multiple (macro-expanded) string literal tokens. The below patch gets close, but: * I don't know how to get a location_t that spans all the tokens, see the TODO in the patch. (If that's hard to get right, an alternative might be to instead highlight the "error" resp. "warning" token, in the same way as #pragma message highlights the "message" token.) * Additional tokens that are not (ordinary) string literals keep being ignored (to be exact, the first such non--string-literal and all remaining tokens are ignored). That's in line with existing behavior for those pragmas, where they ignored everything following the first (necessarily string-literal) token (but unlike #pragma message, which emits "warning: junk at end of '#pragma message' [-Wpragmas]"). Something of a corner case happens when such a non--string-literal token is the result of macro expansion as in #define FOO "2" 3 #pragma GCC error "1" FOO emitting a message of "12". (My naive understanding of how cpp_get_token works in do_pragma_warnign_or_error is that the end of the #pragma line is represented by a CPP_EOF token.) * I have left the documentation in gcc/doc/extend.texi alone, as it didn't specify any details of how the #pragma message string is parsed, either. } diff --git a/gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c b/gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c index 9867c94a8dd..53bae44e210 100644 --- a/gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c +++ b/gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c @@ -9,3 +9,6 @@ char a[CONST1]; // { dg-warning warn-c } char b[CONST2]; // { dg-error err-d } +#define EXPAND "exp-" +#pragma GCC warning "warn-" EXPAND "e" // { dg-warning warn-exp-e } +#pragma GCC error "err-" EXPAND "f" // { dg-error err-exp-f } diff --git a/libcpp/directives.c b/libcpp/directives.c index 61f1fef9489..4d06a2d2934 100644 --- a/libcpp/directives.c +++ b/libcpp/directives.c @@ -1740,19 +1740,49 @@ do_pragma_dependency (cpp_reader *pfile) static void do_pragma_warning_or_error (cpp_reader *pfile, bool error) { - const cpp_token *tok = _cpp_lex_token (pfile); + size_t count = 0; + obstack str_ob; + obstack_specify_allocation (&str_ob, 0, 0, xmalloc, free); + location_t loc; + location_t loc_end; + gcc_assert (pfile->state.prevent_expansion > 0); + pfile->state.prevent_expansion--; + for (;; ++count) + { + const cpp_token *tok = cpp_get_token (pfile); + if (tok->type != CPP_STRING) + break; + if (count == 0) + loc = tok->src_loc; + obstack_grow (&str_ob, &tok->val.str, sizeof (cpp_string)); + loc_end = tok->src_loc; + } + pfile->state.prevent_expansion++; + if (count == 0) + { + cpp_error (pfile, CPP_DL_ERROR, "invalid \"#pragma GCC %s\" directive", + error ? "error" : "warning"); + return; + } + cpp_string * strs = (cpp_string *) obstack_finish (&str_ob); cpp_string str; - if (tok->type != CPP_STRING - || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str, - CPP_STRING) - || str.len == 0) + bool combine = cpp_interpret_string_notranslate (pfile, strs, count, + &str, CPP_STRING); + obstack_free (&str_ob, 0); + if (!combine || 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); + if (count != 1) + { + //TODO: combine loc, loc_end into + // loc = COMBINE_LOCATION_DATA (pfile->line_table, ..., ..., NULL); + (void)loc_end; + } + cpp_error_at (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING, loc, + "%s", str.text); free ((void *)str.text);