From patchwork Tue Nov 9 23:30:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 47341 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E551E3858033 for ; Tue, 9 Nov 2021 23:32:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E551E3858033 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636500727; bh=JeeSZdCJmzhhv3d+rbG3N3ihxDjX/PgiO2j31PjUv78=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=tWro39gfdFnBgT9ubJETht2dz05PiffI8F2VSmIM4rAKaUtgzwv5+oIyEN1s/L9SO QYBmz7I9HClKd4+Sp62gYyNwTBPqCMd2F0XNHdNfF7ogbODDmqy+bm0gd5+RvA4GwQ 31K4QnPrBSjbxm1npZwQFpK4JRySWVj+CFZ7cK7g= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id A41F9385800A for ; Tue, 9 Nov 2021 23:30:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A41F9385800A 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-450-TD-lvL4pPayfBOmN1hPLSg-1; Tue, 09 Nov 2021 18:30:08 -0500 X-MC-Unique: TD-lvL4pPayfBOmN1hPLSg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 346AE180831D for ; Tue, 9 Nov 2021 23:30:07 +0000 (UTC) Received: from t14s.localdomain.com (ovpn-113-202.phx2.redhat.com [10.3.113.202]) by smtp.corp.redhat.com (Postfix) with ESMTP id C8447104253B; Tue, 9 Nov 2021 23:30:06 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [committed] c: more precise locations for some -Wpragmas diagnostics Date: Tue, 9 Nov 2021 18:30:03 -0500 Message-Id: <20211109233003.1758273-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as 8722a17067f1590e82f49b3fd385647b762a45dc. gcc/c-family/ChangeLog: * c-pragma.c (GCC_BAD_AT): New macro. (GCC_BAD2_AT): New macro. (handle_pragma_pack): Use the location of the pertinent token when issuing diagnostics about invalid constants/actions, and trailing junk. (handle_pragma_target): Likewise for non-string "GCC option". (handle_pragma_message): Likewise for trailing junk. gcc/testsuite/ChangeLog: * gcc.dg/bad-pragma-locations.c: New test. Signed-off-by: David Malcolm --- gcc/c-family/c-pragma.c | 33 +++++--- gcc/testsuite/gcc.dg/bad-pragma-locations.c | 86 +++++++++++++++++++++ 2 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/bad-pragma-locations.c diff --git a/gcc/c-family/c-pragma.c b/gcc/c-family/c-pragma.c index a9be8df0384..b2ad991008a 100644 --- a/gcc/c-family/c-pragma.c +++ b/gcc/c-family/c-pragma.c @@ -39,6 +39,10 @@ along with GCC; see the file COPYING3. If not see do { warning (OPT_Wpragmas, gmsgid); return; } while (0) #define GCC_BAD2(gmsgid, arg) \ do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0) +#define GCC_BAD_AT(loc, gmsgid) \ + do { warning_at (loc, OPT_Wpragmas, gmsgid); return; } while (0) +#define GCC_BAD2_AT(loc, gmsgid, arg) \ + do { warning_at (loc, OPT_Wpragmas, gmsgid, arg); return; } while (0) struct GTY(()) align_stack { int alignment; @@ -130,6 +134,7 @@ pop_alignment (tree id) static void handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy)) { + location_t loc; tree x, id = 0; int align = -1; enum cpp_ttype token; @@ -138,7 +143,7 @@ handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy)) if (pragma_lex (&x) != CPP_OPEN_PAREN) GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored"); - token = pragma_lex (&x); + token = pragma_lex (&x, &loc); if (token == CPP_CLOSE_PAREN) { action = set; @@ -147,7 +152,7 @@ handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy)) else if (token == CPP_NUMBER) { if (TREE_CODE (x) != INTEGER_CST) - GCC_BAD ("invalid constant in %<#pragma pack%> - ignored"); + GCC_BAD_AT (loc, "invalid constant in %<#pragma pack%> - ignored"); align = TREE_INT_CST_LOW (x); action = set; if (pragma_lex (&x) != CPP_CLOSE_PAREN) @@ -167,11 +172,12 @@ handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy)) else if (!strcmp (op, "pop")) action = pop; else - GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x); + GCC_BAD2_AT (loc, "unknown action %qE for %<#pragma pack%> - ignored", + x); while ((token = pragma_lex (&x)) == CPP_COMMA) { - token = pragma_lex (&x); + token = pragma_lex (&x, &loc); if (token == CPP_NAME && id == 0) { id = x; @@ -179,7 +185,8 @@ handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy)) else if (token == CPP_NUMBER && action == push && align == -1) { if (TREE_CODE (x) != INTEGER_CST) - GCC_BAD ("invalid constant in %<#pragma pack%> - ignored"); + GCC_BAD_AT (loc, + "invalid constant in %<#pragma pack%> - ignored"); align = TREE_INT_CST_LOW (x); if (align == -1) action = set; @@ -195,8 +202,8 @@ handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy)) else GCC_BAD ("malformed %<#pragma pack%> - ignored"); - if (pragma_lex (&x) != CPP_EOF) - warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>"); + if (pragma_lex (&x, &loc) != CPP_EOF) + warning_at (loc, OPT_Wpragmas, "junk at end of %<#pragma pack%>"); if (flag_pack_struct) GCC_BAD ("%<#pragma pack%> has no effect with %<-fpack-struct%> - ignored"); @@ -857,6 +864,7 @@ handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy)) static void handle_pragma_target(cpp_reader *ARG_UNUSED(dummy)) { + location_t loc; enum cpp_ttype token; tree x; bool close_paren_needed_p = false; @@ -867,16 +875,16 @@ handle_pragma_target(cpp_reader *ARG_UNUSED(dummy)) return; } - token = pragma_lex (&x); + token = pragma_lex (&x, &loc); if (token == CPP_OPEN_PAREN) { close_paren_needed_p = true; - token = pragma_lex (&x); + token = pragma_lex (&x, &loc); } if (token != CPP_STRING) { - GCC_BAD ("%<#pragma GCC option%> is not a string"); + GCC_BAD_AT (loc, "%<#pragma GCC option%> is not a string"); return; } @@ -1149,6 +1157,7 @@ handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy)) static void handle_pragma_message (cpp_reader *ARG_UNUSED(dummy)) { + location_t loc; enum cpp_ttype token; tree x, message = 0; @@ -1170,8 +1179,8 @@ handle_pragma_message (cpp_reader *ARG_UNUSED(dummy)) gcc_assert (message); - if (pragma_lex (&x) != CPP_EOF) - warning (OPT_Wpragmas, "junk at end of %<#pragma message%>"); + if (pragma_lex (&x, &loc) != CPP_EOF) + warning_at (loc, OPT_Wpragmas, "junk at end of %<#pragma message%>"); if (TREE_STRING_LENGTH (message) > 1) inform (input_location, "%<#pragma message: %s%>", diff --git a/gcc/testsuite/gcc.dg/bad-pragma-locations.c b/gcc/testsuite/gcc.dg/bad-pragma-locations.c new file mode 100644 index 00000000000..8068839881d --- /dev/null +++ b/gcc/testsuite/gcc.dg/bad-pragma-locations.c @@ -0,0 +1,86 @@ +/* Verify that we use precise locations when emitting diagnostics + about pragmas. */ + +/* { dg-do assemble } */ +/* { dg-options "-fdiagnostics-show-caret" } */ + +/* pack ****************************************************************************/ + +#pragma pack +/* { dg-warning "missing '\\(' after '#pragma pack' - ignored" "" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + #pragma pack + ^~~~ + { dg-end-multiline-output "" } */ + +#pragma pack ( +/* { dg-warning "malformed '#pragma pack' - ignored" "" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + #pragma pack ( + ^~~~ + { dg-end-multiline-output "" } */ + +#pragma pack (32 +/* { dg-warning "malformed '#pragma pack' - ignored" "" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + #pragma pack (32 + ^~~~ + { dg-end-multiline-output "" } */ + +#pragma pack (3.14159 +/* { dg-warning "invalid constant in '#pragma pack' - ignored" "" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + #pragma pack (3.14159 + ^~~~~~~ + { dg-end-multiline-output "" } */ + +#pragma pack (push, 3.14159 +/* { dg-warning "invalid constant in '#pragma pack' - ignored" "" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + #pragma pack (push, 3.14159 + ^~~~~~~ + { dg-end-multiline-output "" } */ + +#pragma pack (toothbrush +/* { dg-warning "unknown action 'toothbrush' for '#pragma pack' - ignored" "" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + #pragma pack (toothbrush + ^~~~~~~~~~ + { dg-end-multiline-output "" } */ + +#pragma pack() pyjamas +/* { dg-warning "junk at end of '#pragma pack'" "" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + #pragma pack() pyjamas + ^~~~~~~ + { dg-end-multiline-output "" } */ + +/* target ****************************************************************************/ + +#pragma GCC target 42 +/* { dg-warning "#pragma GCC option' is not a string" "" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + #pragma GCC target 42 + ^~ + { dg-end-multiline-output "" } */ + +#pragma GCC target ( 1776 +/* { dg-warning "#pragma GCC option' is not a string" "" { target *-*-* } .-1 } + { dg-begin-multiline-output "" } + #pragma GCC target ( 1776 + ^~~~ + { dg-end-multiline-output "" } */ + +/* message ****************************************************************************/ + +#pragma message "foo" int +/* { dg-warning "junk at end of '#pragma message'" "" { target *-*-* } .-1 } + { dg-message "'#pragma message: foo'" "" { target *-*-* } .-2 } + { dg-begin-multiline-output "" } + #pragma message "foo" int + ^~~ + { dg-end-multiline-output "" } + { dg-begin-multiline-output "" } + #pragma message "foo" int + ^~~~~~~ + { dg-end-multiline-output "" } */