From patchwork Wed Jun 26 00:38:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1952317 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=R0yqLBWE; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W82sW5LJWz20Xf for ; Wed, 26 Jun 2024 10:39:59 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0175D3870C1F for ; Wed, 26 Jun 2024 00:39:57 +0000 (GMT) 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 143BE3870921 for ; Wed, 26 Jun 2024 00:38:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 143BE3870921 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 143BE3870921 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719362292; cv=none; b=HJp/mjTR3S7nOmqAUwvDe9oNzmEmM8wBYuplIwdRWC5wocGjsx/fPAocvBnnLlL1MSlOZXTRmu6vb0Z9xTPiBpGWxH8C3Mt4LiAa75gI/8AvR4QQddhdjJefPEebdB7RpPity4Lerr/Ugqi9ziUHKIHN5+FB+k/XyL25d5mNpvs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719362292; c=relaxed/simple; bh=veT+gmkSjJlRA9q0Ikq+3itDThFJIjFKiC1HeEAyoIc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=cbYDQ45gpdCAefeiOq1VO3npNNHJvBtFADlpPfMNgSkZ24+0V9BZli5XLcR2Fuh90atdZsI9Yv4SQQ2G8EAY8LM6yNFX1vjyoeYWHCn6oROvdl7B+WKB97wjVWnFHX0Hj7AFUv3MyyS1NUpF8eo9qYHIuy3Buym5eJP/U/rVV10= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719362286; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=w9TEJ9d8CKNZN+++asAGbRIC5L8gbandGCxMFgen1iM=; b=R0yqLBWE+Ylv1oH2VdTbdi/TCLNVeYdkItFalZTbQphQHsXdN5nsS/OL9XykQx/To2CvjX UHEmVqrGSY2PK8S6yBqdXtAV1AqQNNgoHGwGBoiYRnTE8IQxlvidTv4qhf3ENzM/uQURn9 tRRozZiItHJn6iZgvJvfPUB7P/9LRPE= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-586-twkihRHmMNC1o42abrkf5w-1; Tue, 25 Jun 2024 20:38:04 -0400 X-MC-Unique: twkihRHmMNC1o42abrkf5w-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 069E719560AB for ; Wed, 26 Jun 2024 00:38:03 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.16.8]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D6B8B1956050; Wed, 26 Jun 2024 00:38:01 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] diagnostics: introduce diagnostic-global-context.cc Date: Tue, 25 Jun 2024 20:38:00 -0400 Message-Id: <20240626003800.1428987-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-20.3 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org This moves all of the uses of global_dc within diagnostic.cc (including the definition) to a new diagnostic-global-context.cc. My intent is to make clearer those parts of our internal API that implicitly use global_dc, and to perhaps avoid linking global_dc into a future libdiagnostics.so. No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Successful run of analyzer integration tests on x86_64-pc-linux-gnu. Pushed to trunk as r15-1635-g3800a78a767f46. gcc/ChangeLog: * Makefile.in (OBJS-libcommon): Add diagnostic-global-context.o. * diagnostic-global-context.cc: New file, taken from material in diagnostic.cc. * diagnostic.cc (global_diagnostic_context): Move to diagnostic-global-context.cc. (global_dc): Likewise. (verbatim): Likewise. (emit_diagnostic): Likewise. (emit_diagnostic_valist): Likewise. (emit_diagnostic_valist_meta): Likewise. (inform): Likewise. (inform_n): Likewise. (warning): Likewise. (warning_at): Likewise. (warning_meta): Likewise. (warning_n): Likewise. (pedwarn): Likewise. (permerror): Likewise. (permerror_opt): Likewise. (error): Likewise. (error_n): Likewise. (error_at): Likewise. (error_meta): Likewise. (sorry): Likewise. (sorry_at): Likewise. (seen_error): Likewise. (fatal_error): Likewise. (internal_error): Likewise. (internal_error_no_backtrace): Likewise. (fnotice): Likewise. (auto_diagnostic_group::auto_diagnostic_group): Likewise. (auto_diagnostic_group::~auto_diagnostic_group): Likewise. Signed-off-by: David Malcolm --- gcc/Makefile.in | 1 + gcc/diagnostic-global-context.cc | 553 +++++++++++++++++++++++++++++++ gcc/diagnostic.cc | 521 ----------------------------- 3 files changed, 554 insertions(+), 521 deletions(-) create mode 100644 gcc/diagnostic-global-context.cc diff --git a/gcc/Makefile.in b/gcc/Makefile.in index 9eacb21bf54..7d3ea27a6ab 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1824,6 +1824,7 @@ OBJS = \ OBJS-libcommon = diagnostic-spec.o diagnostic.o diagnostic-color.o \ diagnostic-format-json.o \ diagnostic-format-sarif.o \ + diagnostic-global-context.o \ diagnostic-macro-unwinding.o \ diagnostic-path.o \ diagnostic-show-locus.o \ diff --git a/gcc/diagnostic-global-context.cc b/gcc/diagnostic-global-context.cc new file mode 100644 index 00000000000..497eb9e727c --- /dev/null +++ b/gcc/diagnostic-global-context.cc @@ -0,0 +1,553 @@ +/* Language-independent diagnostic subroutines that implicitly use global_dc. + Copyright (C) 1999-2024 Free Software Foundation, Inc. + Contributed by Gabriel Dos Reis + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + + +/* This file implements the parts of the language independent aspect + of diagnostic messages that implicitly use global_dc. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "intl.h" +#include "diagnostic.h" + +/* A diagnostic_context surrogate for stderr. */ +static diagnostic_context global_diagnostic_context; +diagnostic_context *global_dc = &global_diagnostic_context; + +/* Standard error reporting routines in increasing order of severity. */ + +/* Text to be emitted verbatim to the error message stream; this + produces no prefix and disables line-wrapping. Use rarely. */ +void +verbatim (const char *gmsgid, ...) +{ + va_list ap; + + va_start (ap, gmsgid); + text_info text (_(gmsgid), &ap, errno); + pp_format_verbatim (global_dc->printer, &text); + pp_newline_and_flush (global_dc->printer); + va_end (ap); +} + +/* Wrapper around diagnostic_context::diagnostic_impl + implying global_dc and taking a variable argument list. */ + +bool +emit_diagnostic (diagnostic_t kind, location_t location, int opt, + const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, location); + bool ret = global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, &ap, + kind); + va_end (ap); + return ret; +} + +/* As above, but for rich_location *. */ + +bool +emit_diagnostic (diagnostic_t kind, rich_location *richloc, int opt, + const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + bool ret = global_dc->diagnostic_impl (richloc, nullptr, opt, gmsgid, &ap, + kind); + va_end (ap); + return ret; +} + +/* As above, but taking a variable argument list. */ + +bool +emit_diagnostic_valist (diagnostic_t kind, location_t location, int opt, + const char *gmsgid, va_list *ap) +{ + rich_location richloc (line_table, location); + return global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, ap, kind); +} + +/* As above, but with rich_location and metadata. */ + +bool +emit_diagnostic_valist_meta (diagnostic_t kind, + rich_location *richloc, + const diagnostic_metadata *metadata, + int opt, + const char *gmsgid, va_list *ap) +{ + return global_dc->diagnostic_impl (richloc, metadata, opt, gmsgid, ap, kind); +} + +/* An informative note at LOCATION. Use this for additional details on an error + message. */ +void +inform (location_t location, const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, location); + global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_NOTE); + va_end (ap); +} + +/* Same as "inform" above, but at RICHLOC. */ +void +inform (rich_location *richloc, const char *gmsgid, ...) +{ + gcc_assert (richloc); + + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + global_dc->diagnostic_impl (richloc, nullptr, -1, gmsgid, &ap, DK_NOTE); + va_end (ap); +} + +/* An informative note at LOCATION. Use this for additional details on an + error message. */ +void +inform_n (location_t location, unsigned HOST_WIDE_INT n, + const char *singular_gmsgid, const char *plural_gmsgid, ...) +{ + va_list ap; + va_start (ap, plural_gmsgid); + auto_diagnostic_group d; + rich_location richloc (line_table, location); + global_dc->diagnostic_n_impl (&richloc, nullptr, -1, n, + singular_gmsgid, plural_gmsgid, + &ap, DK_NOTE); + va_end (ap); +} + +/* A warning at INPUT_LOCATION. Use this for code which is correct according + to the relevant language specification but is likely to be buggy anyway. + Returns true if the warning was printed, false if it was inhibited. */ +bool +warning (int opt, const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, input_location); + bool ret = global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, &ap, + DK_WARNING); + va_end (ap); + return ret; +} + +/* A warning at LOCATION. Use this for code which is correct according to the + relevant language specification but is likely to be buggy anyway. + Returns true if the warning was printed, false if it was inhibited. */ + +bool +warning_at (location_t location, int opt, const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, location); + bool ret = global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, &ap, + DK_WARNING); + va_end (ap); + return ret; +} + +/* Same as "warning at" above, but using RICHLOC. */ + +bool +warning_at (rich_location *richloc, int opt, const char *gmsgid, ...) +{ + gcc_assert (richloc); + + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + bool ret = global_dc->diagnostic_impl (richloc, nullptr, opt, gmsgid, &ap, + DK_WARNING); + va_end (ap); + return ret; +} + +/* Same as "warning at" above, but using METADATA. */ + +bool +warning_meta (rich_location *richloc, + const diagnostic_metadata &metadata, + int opt, const char *gmsgid, ...) +{ + gcc_assert (richloc); + + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + bool ret = global_dc->diagnostic_impl (richloc, &metadata, opt, gmsgid, &ap, + DK_WARNING); + va_end (ap); + return ret; +} + +/* Same as warning_n plural variant below, but using RICHLOC. */ + +bool +warning_n (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n, + const char *singular_gmsgid, const char *plural_gmsgid, ...) +{ + gcc_assert (richloc); + + auto_diagnostic_group d; + va_list ap; + va_start (ap, plural_gmsgid); + bool ret = global_dc->diagnostic_n_impl (richloc, nullptr, opt, n, + singular_gmsgid, plural_gmsgid, + &ap, DK_WARNING); + va_end (ap); + return ret; +} + +/* A warning at LOCATION. Use this for code which is correct according to the + relevant language specification but is likely to be buggy anyway. + Returns true if the warning was printed, false if it was inhibited. */ + +bool +warning_n (location_t location, int opt, unsigned HOST_WIDE_INT n, + const char *singular_gmsgid, const char *plural_gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, plural_gmsgid); + rich_location richloc (line_table, location); + bool ret = global_dc->diagnostic_n_impl (&richloc, nullptr, opt, n, + singular_gmsgid, plural_gmsgid, + &ap, DK_WARNING); + va_end (ap); + return ret; +} + +/* A "pedantic" warning at LOCATION: issues a warning unless + -pedantic-errors was given on the command line, in which case it + issues an error. Use this for diagnostics required by the relevant + language standard, if you have chosen not to make them errors. + + Note that these diagnostics are issued independent of the setting + of the -Wpedantic command-line switch. To get a warning enabled + only with that switch, use either "if (pedantic) pedwarn + (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a + pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)". + + Returns true if the warning was printed, false if it was inhibited. */ + +bool +pedwarn (location_t location, int opt, const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, location); + bool ret = global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, &ap, + DK_PEDWARN); + va_end (ap); + return ret; +} + +/* Same as pedwarn above, but using RICHLOC. */ + +bool +pedwarn (rich_location *richloc, int opt, const char *gmsgid, ...) +{ + gcc_assert (richloc); + + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + bool ret = global_dc->diagnostic_impl (richloc, nullptr, opt, gmsgid, &ap, + DK_PEDWARN); + va_end (ap); + return ret; +} + +/* A "permissive" error at LOCATION: issues an error unless + -fpermissive was given on the command line, in which case it issues + a warning. Use this for things that really should be errors but we + want to support legacy code. + + Returns true if the warning was printed, false if it was inhibited. */ + +bool +permerror (location_t location, const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, location); + bool ret = global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, + DK_PERMERROR); + va_end (ap); + return ret; +} + +/* Same as "permerror" above, but at RICHLOC. */ + +bool +permerror (rich_location *richloc, const char *gmsgid, ...) +{ + gcc_assert (richloc); + + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + bool ret = global_dc->diagnostic_impl (richloc, nullptr, -1, gmsgid, &ap, + DK_PERMERROR); + va_end (ap); + return ret; +} + +/* Similar to the above, but controlled by a flag other than -fpermissive. + As above, an error by default or a warning with -fpermissive, but this + diagnostic can also be downgraded by -Wno-error=opt. */ + +bool +permerror_opt (location_t location, int opt, const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, location); + bool ret = global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, &ap, + DK_PERMERROR); + va_end (ap); + return ret; +} + +/* Same as "permerror" above, but at RICHLOC. */ + +bool +permerror_opt (rich_location *richloc, int opt, const char *gmsgid, ...) +{ + gcc_assert (richloc); + + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + bool ret = global_dc->diagnostic_impl (richloc, nullptr, opt, gmsgid, &ap, + DK_PERMERROR); + va_end (ap); + return ret; +} + +/* A hard error: the code is definitely ill-formed, and an object file + will not be produced. */ +void +error (const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, input_location); + global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_ERROR); + va_end (ap); +} + +/* A hard error: the code is definitely ill-formed, and an object file + will not be produced. */ +void +error_n (location_t location, unsigned HOST_WIDE_INT n, + const char *singular_gmsgid, const char *plural_gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, plural_gmsgid); + rich_location richloc (line_table, location); + global_dc->diagnostic_n_impl (&richloc, nullptr, -1, n, + singular_gmsgid, plural_gmsgid, + &ap, DK_ERROR); + va_end (ap); +} + +/* Same as above, but use location LOC instead of input_location. */ +void +error_at (location_t loc, const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, loc); + global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_ERROR); + va_end (ap); +} + +/* Same as above, but use RICH_LOC. */ + +void +error_at (rich_location *richloc, const char *gmsgid, ...) +{ + gcc_assert (richloc); + + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + global_dc->diagnostic_impl (richloc, nullptr, -1, gmsgid, &ap, DK_ERROR); + va_end (ap); +} + +/* Same as above, but with metadata. */ + +void +error_meta (rich_location *richloc, const diagnostic_metadata &metadata, + const char *gmsgid, ...) +{ + gcc_assert (richloc); + + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + global_dc->diagnostic_impl (richloc, &metadata, -1, gmsgid, &ap, DK_ERROR); + va_end (ap); +} + +/* "Sorry, not implemented." Use for a language feature which is + required by the relevant specification but not implemented by GCC. + An object file will not be produced. */ +void +sorry (const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, input_location); + global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_SORRY); + va_end (ap); +} + +/* Same as above, but use location LOC instead of input_location. */ +void +sorry_at (location_t loc, const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, loc); + global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_SORRY); + va_end (ap); +} + +/* Return true if an error or a "sorry" has been seen on global_dc. Various + processing is disabled after errors. */ +bool +seen_error (void) +{ + return errorcount || sorrycount; +} + +/* An error which is severe enough that we make no attempt to + continue. Do not use this for internal consistency checks; that's + internal_error. Use of this function should be rare. */ +void +fatal_error (location_t loc, const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, loc); + global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_FATAL); + va_end (ap); + + gcc_unreachable (); +} + +/* An internal consistency check has failed. We make no attempt to + continue. */ +void +internal_error (const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, input_location); + global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_ICE); + va_end (ap); + + gcc_unreachable (); +} + +/* Like internal_error, but no backtrace will be printed. Used when + the internal error does not happen at the current location, but happened + somewhere else. */ +void +internal_error_no_backtrace (const char *gmsgid, ...) +{ + auto_diagnostic_group d; + va_list ap; + va_start (ap, gmsgid); + rich_location richloc (line_table, input_location); + global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_ICE_NOBT); + va_end (ap); + + gcc_unreachable (); +} + + +/* Special case error functions. Most are implemented in terms of the + above, or should be. */ + +/* Print a diagnostic MSGID on FILE. This is just fprintf, except it + runs its second argument through gettext. */ +void +fnotice (FILE *file, const char *cmsgid, ...) +{ + /* If the user requested one of the machine-readable diagnostic output + formats on stderr (e.g. -fdiagnostics-format=sarif-stderr), then + emitting free-form text on stderr will lead to corrupt output. + Skip the message for such cases. */ + if (file == stderr && global_dc) + if (const diagnostic_output_format *output_format + = global_dc->get_output_format ()) + if (output_format->machine_readable_stderr_p ()) + return; + + va_list ap; + + va_start (ap, cmsgid); + vfprintf (file, _(cmsgid), ap); + va_end (ap); +} + +/* class auto_diagnostic_group. */ + +/* Constructor: "push" this group into global_dc. */ + +auto_diagnostic_group::auto_diagnostic_group () +{ + global_dc->begin_group (); +} + +/* Destructor: "pop" this group from global_dc. */ + +auto_diagnostic_group::~auto_diagnostic_group () +{ + global_dc->end_group (); +} diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc index 7854c74df9f..bf2bd0526c1 100644 --- a/gcc/diagnostic.cc +++ b/gcc/diagnostic.cc @@ -67,9 +67,6 @@ static void real_abort (void) ATTRIBUTE_NORETURN; const char *progname; -/* A diagnostic_context surrogate for stderr. */ -static diagnostic_context global_diagnostic_context; -diagnostic_context *global_dc = &global_diagnostic_context; /* Return a malloc'd string containing MSG formatted a la printf. The caller is responsible for freeing the memory. */ @@ -1565,23 +1562,6 @@ trim_filename (const char *name) return p; } - -/* Standard error reporting routines in increasing order of severity. - All of these take arguments like printf. */ - -/* Text to be emitted verbatim to the error message stream; this - produces no prefix and disables line-wrapping. Use rarely. */ -void -verbatim (const char *gmsgid, ...) -{ - va_list ap; - - va_start (ap, gmsgid); - text_info text (_(gmsgid), &ap, errno); - pp_format_verbatim (global_dc->printer, &text); - pp_newline_and_flush (global_dc->printer); - va_end (ap); -} /* Add a note with text GMSGID and with LOCATION to the diagnostic CONTEXT. */ void @@ -1667,466 +1647,6 @@ diagnostic_context::diagnostic_n_impl (rich_location *richloc, return report_diagnostic (&diagnostic); } -/* Wrapper around diagnostic_impl taking a variable argument list. */ - -bool -emit_diagnostic (diagnostic_t kind, location_t location, int opt, - const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, location); - bool ret = global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, &ap, - kind); - va_end (ap); - return ret; -} - -/* As above, but for rich_location *. */ - -bool -emit_diagnostic (diagnostic_t kind, rich_location *richloc, int opt, - const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - bool ret = global_dc->diagnostic_impl (richloc, nullptr, opt, gmsgid, &ap, - kind); - va_end (ap); - return ret; -} - -/* Wrapper around diagnostic_impl taking a va_list parameter. */ - -bool -emit_diagnostic_valist (diagnostic_t kind, location_t location, int opt, - const char *gmsgid, va_list *ap) -{ - rich_location richloc (line_table, location); - return global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, ap, kind); -} - -/* As above, but with rich_location and metadata. */ - -bool -emit_diagnostic_valist_meta (diagnostic_t kind, - rich_location *richloc, - const diagnostic_metadata *metadata, - int opt, - const char *gmsgid, va_list *ap) -{ - return global_dc->diagnostic_impl (richloc, metadata, opt, gmsgid, ap, kind); -} - -/* An informative note at LOCATION. Use this for additional details on an error - message. */ -void -inform (location_t location, const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, location); - global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_NOTE); - va_end (ap); -} - -/* Same as "inform" above, but at RICHLOC. */ -void -inform (rich_location *richloc, const char *gmsgid, ...) -{ - gcc_assert (richloc); - - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - global_dc->diagnostic_impl (richloc, nullptr, -1, gmsgid, &ap, DK_NOTE); - va_end (ap); -} - -/* An informative note at LOCATION. Use this for additional details on an - error message. */ -void -inform_n (location_t location, unsigned HOST_WIDE_INT n, - const char *singular_gmsgid, const char *plural_gmsgid, ...) -{ - va_list ap; - va_start (ap, plural_gmsgid); - auto_diagnostic_group d; - rich_location richloc (line_table, location); - global_dc->diagnostic_n_impl (&richloc, nullptr, -1, n, - singular_gmsgid, plural_gmsgid, - &ap, DK_NOTE); - va_end (ap); -} - -/* A warning at INPUT_LOCATION. Use this for code which is correct according - to the relevant language specification but is likely to be buggy anyway. - Returns true if the warning was printed, false if it was inhibited. */ -bool -warning (int opt, const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, input_location); - bool ret = global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, &ap, - DK_WARNING); - va_end (ap); - return ret; -} - -/* A warning at LOCATION. Use this for code which is correct according to the - relevant language specification but is likely to be buggy anyway. - Returns true if the warning was printed, false if it was inhibited. */ - -bool -warning_at (location_t location, int opt, const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, location); - bool ret = global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, &ap, - DK_WARNING); - va_end (ap); - return ret; -} - -/* Same as "warning at" above, but using RICHLOC. */ - -bool -warning_at (rich_location *richloc, int opt, const char *gmsgid, ...) -{ - gcc_assert (richloc); - - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - bool ret = global_dc->diagnostic_impl (richloc, nullptr, opt, gmsgid, &ap, - DK_WARNING); - va_end (ap); - return ret; -} - -/* Same as "warning at" above, but using METADATA. */ - -bool -warning_meta (rich_location *richloc, - const diagnostic_metadata &metadata, - int opt, const char *gmsgid, ...) -{ - gcc_assert (richloc); - - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - bool ret = global_dc->diagnostic_impl (richloc, &metadata, opt, gmsgid, &ap, - DK_WARNING); - va_end (ap); - return ret; -} - -/* Same as warning_n plural variant below, but using RICHLOC. */ - -bool -warning_n (rich_location *richloc, int opt, unsigned HOST_WIDE_INT n, - const char *singular_gmsgid, const char *plural_gmsgid, ...) -{ - gcc_assert (richloc); - - auto_diagnostic_group d; - va_list ap; - va_start (ap, plural_gmsgid); - bool ret = global_dc->diagnostic_n_impl (richloc, nullptr, opt, n, - singular_gmsgid, plural_gmsgid, - &ap, DK_WARNING); - va_end (ap); - return ret; -} - -/* A warning at LOCATION. Use this for code which is correct according to the - relevant language specification but is likely to be buggy anyway. - Returns true if the warning was printed, false if it was inhibited. */ - -bool -warning_n (location_t location, int opt, unsigned HOST_WIDE_INT n, - const char *singular_gmsgid, const char *plural_gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, plural_gmsgid); - rich_location richloc (line_table, location); - bool ret = global_dc->diagnostic_n_impl (&richloc, nullptr, opt, n, - singular_gmsgid, plural_gmsgid, - &ap, DK_WARNING); - va_end (ap); - return ret; -} - -/* A "pedantic" warning at LOCATION: issues a warning unless - -pedantic-errors was given on the command line, in which case it - issues an error. Use this for diagnostics required by the relevant - language standard, if you have chosen not to make them errors. - - Note that these diagnostics are issued independent of the setting - of the -Wpedantic command-line switch. To get a warning enabled - only with that switch, use either "if (pedantic) pedwarn - (OPT_Wpedantic,...)" or just "pedwarn (OPT_Wpedantic,..)". To get a - pedwarn independently of the -Wpedantic switch use "pedwarn (0,...)". - - Returns true if the warning was printed, false if it was inhibited. */ - -bool -pedwarn (location_t location, int opt, const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, location); - bool ret = global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, &ap, - DK_PEDWARN); - va_end (ap); - return ret; -} - -/* Same as pedwarn above, but using RICHLOC. */ - -bool -pedwarn (rich_location *richloc, int opt, const char *gmsgid, ...) -{ - gcc_assert (richloc); - - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - bool ret = global_dc->diagnostic_impl (richloc, nullptr, opt, gmsgid, &ap, - DK_PEDWARN); - va_end (ap); - return ret; -} - -/* A "permissive" error at LOCATION: issues an error unless - -fpermissive was given on the command line, in which case it issues - a warning. Use this for things that really should be errors but we - want to support legacy code. - - Returns true if the warning was printed, false if it was inhibited. */ - -bool -permerror (location_t location, const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, location); - bool ret = global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, - DK_PERMERROR); - va_end (ap); - return ret; -} - -/* Same as "permerror" above, but at RICHLOC. */ - -bool -permerror (rich_location *richloc, const char *gmsgid, ...) -{ - gcc_assert (richloc); - - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - bool ret = global_dc->diagnostic_impl (richloc, nullptr, -1, gmsgid, &ap, - DK_PERMERROR); - va_end (ap); - return ret; -} - -/* Similar to the above, but controlled by a flag other than -fpermissive. - As above, an error by default or a warning with -fpermissive, but this - diagnostic can also be downgraded by -Wno-error=opt. */ - -bool -permerror_opt (location_t location, int opt, const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, location); - bool ret = global_dc->diagnostic_impl (&richloc, nullptr, opt, gmsgid, &ap, - DK_PERMERROR); - va_end (ap); - return ret; -} - -/* Same as "permerror" above, but at RICHLOC. */ - -bool -permerror_opt (rich_location *richloc, int opt, const char *gmsgid, ...) -{ - gcc_assert (richloc); - - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - bool ret = global_dc->diagnostic_impl (richloc, nullptr, opt, gmsgid, &ap, - DK_PERMERROR); - va_end (ap); - return ret; -} - -/* A hard error: the code is definitely ill-formed, and an object file - will not be produced. */ -void -error (const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, input_location); - global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_ERROR); - va_end (ap); -} - -/* A hard error: the code is definitely ill-formed, and an object file - will not be produced. */ -void -error_n (location_t location, unsigned HOST_WIDE_INT n, - const char *singular_gmsgid, const char *plural_gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, plural_gmsgid); - rich_location richloc (line_table, location); - global_dc->diagnostic_n_impl (&richloc, nullptr, -1, n, - singular_gmsgid, plural_gmsgid, - &ap, DK_ERROR); - va_end (ap); -} - -/* Same as above, but use location LOC instead of input_location. */ -void -error_at (location_t loc, const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, loc); - global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_ERROR); - va_end (ap); -} - -/* Same as above, but use RICH_LOC. */ - -void -error_at (rich_location *richloc, const char *gmsgid, ...) -{ - gcc_assert (richloc); - - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - global_dc->diagnostic_impl (richloc, nullptr, -1, gmsgid, &ap, DK_ERROR); - va_end (ap); -} - -/* Same as above, but with metadata. */ - -void -error_meta (rich_location *richloc, const diagnostic_metadata &metadata, - const char *gmsgid, ...) -{ - gcc_assert (richloc); - - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - global_dc->diagnostic_impl (richloc, &metadata, -1, gmsgid, &ap, DK_ERROR); - va_end (ap); -} - -/* "Sorry, not implemented." Use for a language feature which is - required by the relevant specification but not implemented by GCC. - An object file will not be produced. */ -void -sorry (const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, input_location); - global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_SORRY); - va_end (ap); -} - -/* Same as above, but use location LOC instead of input_location. */ -void -sorry_at (location_t loc, const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, loc); - global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_SORRY); - va_end (ap); -} - -/* Return true if an error or a "sorry" has been seen. Various - processing is disabled after errors. */ -bool -seen_error (void) -{ - return errorcount || sorrycount; -} - -/* An error which is severe enough that we make no attempt to - continue. Do not use this for internal consistency checks; that's - internal_error. Use of this function should be rare. */ -void -fatal_error (location_t loc, const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, loc); - global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_FATAL); - va_end (ap); - - gcc_unreachable (); -} - -/* An internal consistency check has failed. We make no attempt to - continue. */ -void -internal_error (const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, input_location); - global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_ICE); - va_end (ap); - - gcc_unreachable (); -} - -/* Like internal_error, but no backtrace will be printed. Used when - the internal error does not happen at the current location, but happened - somewhere else. */ -void -internal_error_no_backtrace (const char *gmsgid, ...) -{ - auto_diagnostic_group d; - va_list ap; - va_start (ap, gmsgid); - rich_location richloc (line_table, input_location); - global_dc->diagnostic_impl (&richloc, nullptr, -1, gmsgid, &ap, DK_ICE_NOBT); - va_end (ap); - - gcc_unreachable (); -} /* Emit DIAGRAM to this context, respecting the output format. */ @@ -2139,31 +1659,6 @@ diagnostic_context::emit_diagram (const diagnostic_diagram &diagram) gcc_assert (m_output_format); m_output_format->on_diagram (diagram); } - -/* Special case error functions. Most are implemented in terms of the - above, or should be. */ - -/* Print a diagnostic MSGID on FILE. This is just fprintf, except it - runs its second argument through gettext. */ -void -fnotice (FILE *file, const char *cmsgid, ...) -{ - /* If the user requested one of the machine-readable diagnostic output - formats on stderr (e.g. -fdiagnostics-format=sarif-stderr), then - emitting free-form text on stderr will lead to corrupt output. - Skip the message for such cases. */ - if (file == stderr && global_dc) - if (const diagnostic_output_format *output_format - = global_dc->get_output_format ()) - if (output_format->machine_readable_stderr_p ()) - return; - - va_list ap; - - va_start (ap, cmsgid); - vfprintf (file, _(cmsgid), ap); - va_end (ap); -} /* Inform the user that an error occurred while trying to report some other error. This indicates catastrophic internal inconsistencies, @@ -2252,22 +1747,6 @@ diagnostic_context::end_group () } } -/* class auto_diagnostic_group. */ - -/* Constructor: "push" this group into global_dc. */ - -auto_diagnostic_group::auto_diagnostic_group () -{ - global_dc->begin_group (); -} - -/* Destructor: "pop" this group from global_dc. */ - -auto_diagnostic_group::~auto_diagnostic_group () -{ - global_dc->end_group (); -} - /* class diagnostic_text_output_format : public diagnostic_output_format. */ diagnostic_text_output_format::~diagnostic_text_output_format ()