From patchwork Wed Aug 24 11:59:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: herron.philip@googlemail.com X-Patchwork-Id: 726 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp1391699wro; Wed, 24 Aug 2022 05:21:59 -0700 (PDT) X-Google-Smtp-Source: AA6agR5FyutxL+GZ9TcHoFaPeIpUJ3UsJNBlo5jQ9kTEuR98Aa8K2OofzO/3kHROiZ83q0GQ+6Lp X-Received: by 2002:a17:907:3e9b:b0:73d:8ccd:2bb1 with SMTP id hs27-20020a1709073e9b00b0073d8ccd2bb1mr2747100ejc.568.1661343719720; Wed, 24 Aug 2022 05:21:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661343719; cv=none; d=google.com; s=arc-20160816; b=cKRyEU1pKOjhjuABta2MEtJmNWUGkrZVF4AFTdHABuB02J2rD95ZKClaf01CB97dQ+ kDEBIFBm6Wk3W1J2q6a12T6SUe34W7ZHWjZjV9tgGSclMZDIOY1XZTOMHZWGcVFJ4+Ay ho4B0kUF3NK2eSzGNgFSzX7nZDxrJpY7TdufVcf441HQlM0EpBpYw4HNmxGKRQoylH5F kwB3zf5NAN/xbFYZYbqfDQlXPbLOl/EdVVK48E30I4ur6J/9NQGJX6YkOoDzC3XO0oqf 2LsitUtc/a2JHKlnyBAl5/hGYQNfmiMM/V/TdIX/GFVzCQu6VFPBSf45KvPnAtTUdDZP jgGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:dkim-signature:dmarc-filter :delivered-to; bh=1GU0BGkn1An4+1tOSI96NUta0y/eKG5nxnqZewIoLsQ=; b=fCb15mr/wlEkNJmt6KzpSX2IqXjGporqh76jqtZYEifbXMLtBw34/1E0ae+dyZWaKU fHgdJF8GiOB3DvHui38TVe/T3OJof8yWsK/qdmiEWa+UKyrMeIZc4rH9rG0TfTu0aLTv Cu7HjIIvLPcyttHo/7AGBr7Zn2BFkhIDZbfE/JWs61Ic/WcnMTuHhG8Suv91HFPTEP0d /rzh9h7aJdvm93EIc9H69mjmJ3bClrvhpw42iEoSva69pDo1auSlGmphz6Hx4PVbfGkr 6DC785sjJDye/Pa46G3tcrIU6lQamZuoiu/ltS9RcftC+9E0qpizb1KQjwGenbWYWffI 094w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@googlemail.com header.s=20210112 header.b=lUNL9vmR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y8-20020a50bb08000000b0043bdf9b0ff5si4148905ede.443.2022.08.24.05.21.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Aug 2022 05:21:59 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@googlemail.com header.s=20210112 header.b=lUNL9vmR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 241E33954C7B for ; Wed, 24 Aug 2022 12:08:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id C5D9D38983A7; Wed, 24 Aug 2022 12:01:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C5D9D38983A7 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=googlemail.com Received: by mail-wm1-x335.google.com with SMTP id j26so8672098wms.0; Wed, 24 Aug 2022 05:01:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc; bh=1GU0BGkn1An4+1tOSI96NUta0y/eKG5nxnqZewIoLsQ=; b=lUNL9vmR4pJec2Jeotvr1JLFPMnqGc1QZwCipHpZBf+GOBrRt7owXIBsviePfkwi7q t1LhB/41nhyfOhDzmSc86ZpWGzyjZw0k6iehq7VtMc5wGzuvmlAUhDjXTGyX5VGUnZiA MLSK1UQ2lLBLHZ5NUznIycCvGUg0EEVo0V65vdvAepgszz60SEF55KVZuG4P8XDcu+O6 74q2t0HofylzNMnuKpdJThhTdFwkeZKSWhtbHv5IZHvbwWXrqCvCDIsktout3paA2GKS pFCiUZEfsM+G8sGPMT5EkqYNDbkyrIhgU28Wln2eHieEGgXLiBUCuA7hQG1eHnRhqrwR 4C2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc; bh=1GU0BGkn1An4+1tOSI96NUta0y/eKG5nxnqZewIoLsQ=; b=QMBXA02yIwWITzPMlKlVtMiBmUOAywnYNcoPc4PIM3R3xQI35d8XM8B0LJtMuhV11J 9RJh8RYCA9JtbyQVkHeO0velWoLhqBG4rIi3uPmFaxUrD84ICUam7x3FTXBA939ETJbY tS4dvlf1iCB1cp+oII1KZ13nfiuLdpLZeGHSJ2El+6lpg8l/Wpa0G3A+TNBxVkmA9e/a Czm8/N3L8ZOFDXeE+QPsv2Zzh4XTrmktMUpNa92dv1EB9JdaS/g3d8PxvsHcURpMFcCs VCM+mu/S9bhHZI38Y/Vt9+yaSEb6tR078DHu6NXcLjP08rqWzmi4lDG+HpshgOjwgbSd mtTw== X-Gm-Message-State: ACgBeo3ereB5WKGKqtSlBbbCItLTGl1RiEGy1rb2R9zl8YdhqN30a7Eq qFaNkksny4NtDSgZAEegvEyVrvLhv5o= X-Received: by 2002:a05:600c:198e:b0:3a6:2a00:3df3 with SMTP id t14-20020a05600c198e00b003a62a003df3mr5089567wmq.79.1661342474526; Wed, 24 Aug 2022 05:01:14 -0700 (PDT) Received: from localhost.localdomain ([86.14.124.218]) by smtp.gmail.com with ESMTPSA id cc19-20020a5d5c13000000b0022571d43d32sm1697676wrb.21.2022.08.24.05.01.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Aug 2022 05:01:14 -0700 (PDT) From: herron.philip@googlemail.com X-Google-Original-From: philip.herron@embecosm.com To: gcc-patches@gcc.gnu.org Subject: [PATCH Rust front-end v2 30/37] gccrs: These are wrappers ported from reusing gccgo Date: Wed, 24 Aug 2022 12:59:49 +0100 Message-Id: <20220824115956.737931-31-philip.herron@embecosm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220824115956.737931-1-philip.herron@embecosm.com> References: <20220824115956.737931-1-philip.herron@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: philip.herron@embecosm.com Cc: gcc-rust@gcc.gnu.org, Philip Herron Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742045152308676583?= X-GMAIL-MSGID: =?utf-8?q?1742045152308676583?= From: Philip Herron The wrappers over linemap and location will eventually disappear here but served as a useful starting point for us. We have wrappers over the diagnostics system which we might be able to get rid of as well. --- gcc/rust/rust-diagnostics.cc | 244 +++++++++++++++++++++++++++++++ gcc/rust/rust-diagnostics.h | 154 +++++++++++++++++++ gcc/rust/rust-gcc-diagnostics.cc | 84 +++++++++++ gcc/rust/rust-linemap.cc | 229 +++++++++++++++++++++++++++++ gcc/rust/rust-linemap.h | 163 +++++++++++++++++++++ gcc/rust/rust-location.h | 105 +++++++++++++ gcc/rust/rust-system.h | 86 +++++++++++ 7 files changed, 1065 insertions(+) create mode 100644 gcc/rust/rust-diagnostics.cc create mode 100644 gcc/rust/rust-diagnostics.h create mode 100644 gcc/rust/rust-gcc-diagnostics.cc create mode 100644 gcc/rust/rust-linemap.cc create mode 100644 gcc/rust/rust-linemap.h create mode 100644 gcc/rust/rust-location.h create mode 100644 gcc/rust/rust-system.h diff --git a/gcc/rust/rust-diagnostics.cc b/gcc/rust/rust-diagnostics.cc new file mode 100644 index 00000000000..c2d3e4ee8be --- /dev/null +++ b/gcc/rust/rust-diagnostics.cc @@ -0,0 +1,244 @@ +// rust-diagnostics.cc -- GCC implementation of rust diagnostics interface. +// Copyright (C) 2016-2022 Free Software Foundation, Inc. +// Contributed by Than McIntosh, Google. + +// 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 +// . + +#include "rust-system.h" +#include "rust-diagnostics.h" + +static std::string +mformat_value () +{ + return std::string (xstrerror (errno)); +} + +// Rewrite a format string to expand any extensions not +// supported by sprintf(). See comments in rust-diagnostics.h +// for list of supported format specifiers. + +static std::string +expand_format (const char *fmt) +{ + std::stringstream ss; + for (const char *c = fmt; *c; ++c) + { + if (*c != '%') + { + ss << *c; + continue; + } + c++; + switch (*c) + { + case '\0': { + // malformed format string + rust_unreachable (); + } + case '%': { + ss << "%"; + break; + } + case 'm': { + ss << mformat_value (); + break; + } + case '<': { + ss << rust_open_quote (); + break; + } + case '>': { + ss << rust_close_quote (); + break; + } + case 'q': { + ss << rust_open_quote (); + c++; + if (*c == 'm') + { + ss << mformat_value (); + } + else + { + ss << "%" << *c; + } + ss << rust_close_quote (); + break; + } + default: { + ss << "%" << *c; + } + } + } + return ss.str (); +} + +// Expand message format specifiers, using a combination of +// expand_format above to handle extensions (ex: %m, %q) and vasprintf() +// to handle regular printf-style formatting. A pragma is being used here to +// suppress this warning: +// +// warning: function ‘std::__cxx11::string expand_message(const char*, +// __va_list_tag*)’ might be a candidate for ‘gnu_printf’ format attribute +// [-Wsuggest-attribute=format] +// +// What appears to be happening here is that the checker is deciding that +// because of the call to vasprintf() (which has attribute gnu_printf), the +// calling function must need to have attribute gnu_printf as well, even +// though there is already an attribute declaration for it. + +static std::string +expand_message (const char *fmt, va_list ap) RUST_ATTRIBUTE_GCC_DIAG (1, 0); + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wsuggest-attribute=format" + +static std::string +expand_message (const char *fmt, va_list ap) +{ + char *mbuf = 0; + std::string expanded_fmt = expand_format (fmt); + int nwr = vasprintf (&mbuf, expanded_fmt.c_str (), ap); + if (nwr == -1) + { + // memory allocation failed + rust_be_error_at (Linemap::unknown_location (), + "memory allocation failed in vasprintf"); + rust_assert (0); + } + std::string rval = std::string (mbuf); + free (mbuf); + return rval; +} + +#pragma GCC diagnostic pop + +static const char *cached_open_quote = NULL; +static const char *cached_close_quote = NULL; + +const char * +rust_open_quote () +{ + if (cached_open_quote == NULL) + rust_be_get_quotechars (&cached_open_quote, &cached_close_quote); + return cached_open_quote; +} + +const char * +rust_close_quote () +{ + if (cached_close_quote == NULL) + rust_be_get_quotechars (&cached_open_quote, &cached_close_quote); + return cached_close_quote; +} + +void +rust_internal_error_at (const Location location, const char *fmt, ...) +{ + va_list ap; + + va_start (ap, fmt); + rust_be_internal_error_at (location, expand_message (fmt, ap)); + va_end (ap); +} + +void +rust_error_at (const Location location, const char *fmt, ...) +{ + va_list ap; + + va_start (ap, fmt); + rust_be_error_at (location, expand_message (fmt, ap)); + va_end (ap); +} + +void +rust_warning_at (const Location location, int opt, const char *fmt, ...) +{ + va_list ap; + + va_start (ap, fmt); + rust_be_warning_at (location, opt, expand_message (fmt, ap)); + va_end (ap); +} + +void +rust_fatal_error (const Location location, const char *fmt, ...) +{ + va_list ap; + + va_start (ap, fmt); + rust_be_fatal_error (location, expand_message (fmt, ap)); + va_end (ap); +} + +void +rust_inform (const Location location, const char *fmt, ...) +{ + va_list ap; + + va_start (ap, fmt); + rust_be_inform (location, expand_message (fmt, ap)); + va_end (ap); +} + +// Rich Locations +void +rust_error_at (const RichLocation &location, const char *fmt, ...) +{ + va_list ap; + + va_start (ap, fmt); + rust_be_error_at (location, expand_message (fmt, ap)); + va_end (ap); +} + +void +rust_debug_loc (const Location location, const char *fmt, ...) +{ + if (!rust_be_debug_p ()) + return; + + va_list ap; + + va_start (ap, fmt); + char *mbuf = NULL; + int nwr = vasprintf (&mbuf, fmt, ap); + va_end (ap); + if (nwr == -1) + { + rust_be_error_at (Linemap::unknown_location (), + "memory allocation failed in vasprintf"); + rust_assert (0); + } + std::string rval = std::string (mbuf); + free (mbuf); + rust_be_inform (location, rval); +} + +namespace Rust { +Error::Error (const Location location, const char *fmt, ...) : locus (location) +{ + va_list ap; + + va_start (ap, fmt); + message = expand_message (fmt, ap); + va_end (ap); + + message.shrink_to_fit (); +} +} // namespace Rust diff --git a/gcc/rust/rust-diagnostics.h b/gcc/rust/rust-diagnostics.h new file mode 100644 index 00000000000..93bd1b3237b --- /dev/null +++ b/gcc/rust/rust-diagnostics.h @@ -0,0 +1,154 @@ +// Copyright (C) 2020-2022 Free Software Foundation, Inc. + +// 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 +// . + +// rust-diagnostics.h -- interface to diagnostic reporting -*- C++ -*- + +#ifndef RUST_DIAGNOSTICS_H +#define RUST_DIAGNOSTICS_H + +#include "rust-linemap.h" + +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) +#define RUST_ATTRIBUTE_GCC_DIAG(m, n) \ + __attribute__ ((__format__ (__gcc_tdiag__, m, n))) \ + __attribute__ ((__nonnull__ (m))) +#else +#define RUST_ATTRIBUTE_GCC_DIAG(m, n) +#endif + +// These declarations define the interface through which the frontend +// reports errors and warnings. These functions accept printf-like +// format specifiers (e.g. %d, %f, %s, etc), with the following additional +// extensions: +// +// 1. 'q' qualifier may be applied to a specifier to add quoting, e.g. +// %qd produces a quoted decimal output, %qs a quoted string output. +// [This extension is supported only with single-character format +// specifiers]. +// +// 2. %m specifier outputs value of "strerror(errno)" at time of call. +// +// 3. %< outputs an opening quote, %> a closing quote. +// +// All other format specifiers are as defined by 'sprintf'. The final resulting +// message is then sent to the back end via rust_be_error_at/rust_be_warning_at. + +// clang-format off +// simple location +extern void +rust_internal_error_at (const Location, const char *fmt, ...) + RUST_ATTRIBUTE_GCC_DIAG (2, 3) + RUST_ATTRIBUTE_NORETURN; +extern void +rust_error_at (const Location, const char *fmt, ...) + RUST_ATTRIBUTE_GCC_DIAG (2, 3); +extern void +rust_warning_at (const Location, int opt, const char *fmt, ...) + RUST_ATTRIBUTE_GCC_DIAG (3, 4); +extern void +rust_fatal_error (const Location, const char *fmt, ...) + RUST_ATTRIBUTE_GCC_DIAG (2, 3) + RUST_ATTRIBUTE_NORETURN; +extern void +rust_inform (const Location, const char *fmt, ...) + RUST_ATTRIBUTE_GCC_DIAG (2, 3); + +// rich locations +extern void +rust_error_at (const RichLocation &, const char *fmt, ...) + RUST_ATTRIBUTE_GCC_DIAG (2, 3); +// clang-format on + +// These interfaces provide a way for the front end to ask for +// the open/close quote characters it should use when formatting +// diagnostics (warnings, errors). +extern const char * +rust_open_quote (); +extern const char * +rust_close_quote (); + +// These interfaces are used by utilities above to pass warnings and +// errors (once format specifiers have been expanded) to the back end, +// and to determine quoting style. Avoid calling these routines directly; +// instead use the equivalent routines above. The back end is required to +// implement these routines. + +// clang-format off +extern void +rust_be_internal_error_at (const Location, const std::string &errmsg) + RUST_ATTRIBUTE_NORETURN; +extern void +rust_be_error_at (const Location, const std::string &errmsg); +extern void +rust_be_error_at (const RichLocation &, const std::string &errmsg); +extern void +rust_be_warning_at (const Location, int opt, const std::string &warningmsg); +extern void +rust_be_fatal_error (const Location, const std::string &errmsg) + RUST_ATTRIBUTE_NORETURN; +extern void +rust_be_inform (const Location, const std::string &infomsg); +extern void +rust_be_get_quotechars (const char **open_quote, const char **close_quote); +extern bool +rust_be_debug_p (void); +// clang-format on + +namespace Rust { +/* A structure used to represent an error. Useful for enabling + * errors to be ignored, e.g. if backtracking. */ +struct Error +{ + Location locus; + std::string message; + // TODO: store more stuff? e.g. node id? + + Error (Location locus, std::string message) + : locus (locus), message (std::move (message)) + { + message.shrink_to_fit (); + } + + // TODO: the attribute part might be incorrect + Error (Location locus, const char *fmt, + ...) /*RUST_ATTRIBUTE_GCC_DIAG (2, 3)*/ RUST_ATTRIBUTE_GCC_DIAG (3, 4); + + // Irreversibly emits the error as an error. + void emit_error () const { rust_error_at (locus, "%s", message.c_str ()); } + + // Irreversibly emits the error as a fatal error. + void emit_fatal_error () const + { + rust_fatal_error (locus, "%s", message.c_str ()); + } +}; +} // namespace Rust + +// rust_debug uses normal printf formatting, not GCC diagnostic formatting. +#define rust_debug(...) rust_debug_loc (Location (), __VA_ARGS__) + +// rust_sorry_at wraps GCC diagnostic "sorry_at" to accept "Location" instead of +// "location_t" +#define rust_sorry_at(location, ...) \ + sorry_at (location.gcc_location (), __VA_ARGS__) + +void +rust_debug_loc (const Location location, const char *fmt, + ...) ATTRIBUTE_PRINTF_2; + +#endif // !defined(RUST_DIAGNOSTICS_H) diff --git a/gcc/rust/rust-gcc-diagnostics.cc b/gcc/rust/rust-gcc-diagnostics.cc new file mode 100644 index 00000000000..db07372dfb5 --- /dev/null +++ b/gcc/rust/rust-gcc-diagnostics.cc @@ -0,0 +1,84 @@ +// Copyright (C) 2020-2022 Free Software Foundation, Inc. + +// 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 +// . + +// rust-gcc-diagnostics.cc -- GCC implementation of rust diagnostics interface. + +#include "rust-system.h" +#include "rust-diagnostics.h" + +#include "options.h" + +void +rust_be_internal_error_at (const Location location, const std::string &errmsg) +{ + std::string loc_str = Linemap::location_to_string (location); + if (loc_str.empty ()) + internal_error ("%s", errmsg.c_str ()); + else + internal_error ("at %s, %s", loc_str.c_str (), errmsg.c_str ()); +} + +void +rust_be_error_at (const Location location, const std::string &errmsg) +{ + location_t gcc_loc = location.gcc_location (); + error_at (gcc_loc, "%s", errmsg.c_str ()); +} + +void +rust_be_warning_at (const Location location, int opt, + const std::string &warningmsg) +{ + location_t gcc_loc = location.gcc_location (); + warning_at (gcc_loc, opt, "%s", warningmsg.c_str ()); +} + +void +rust_be_fatal_error (const Location location, const std::string &fatalmsg) +{ + location_t gcc_loc = location.gcc_location (); + fatal_error (gcc_loc, "%s", fatalmsg.c_str ()); +} + +void +rust_be_inform (const Location location, const std::string &infomsg) +{ + location_t gcc_loc = location.gcc_location (); + inform (gcc_loc, "%s", infomsg.c_str ()); +} + +void +rust_be_error_at (const RichLocation &location, const std::string &errmsg) +{ + /* TODO: 'error_at' would like a non-'const' 'rich_location *'. */ + rich_location &gcc_loc = const_cast (location.get ()); + error_at (&gcc_loc, "%s", errmsg.c_str ()); +} + +void +rust_be_get_quotechars (const char **open_qu, const char **close_qu) +{ + *open_qu = open_quote; + *close_qu = close_quote; +} + +bool +rust_be_debug_p (void) +{ + return !!flag_rust_debug; +} diff --git a/gcc/rust/rust-linemap.cc b/gcc/rust/rust-linemap.cc new file mode 100644 index 00000000000..b32a965a4aa --- /dev/null +++ b/gcc/rust/rust-linemap.cc @@ -0,0 +1,229 @@ +// Copyright (C) 2020-2022 Free Software Foundation, Inc. + +// 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 +// . + +// rust-linemap.cc -- GCC implementation of Linemap. + +#include "rust-linemap.h" + +// This class implements the Linemap interface defined by the +// frontend. + +class Gcc_linemap : public Linemap +{ +public: + Gcc_linemap () : Linemap (), in_file_ (false) {} + + void start_file (const char *file_name, unsigned int line_begin); + + void start_line (unsigned int line_number, unsigned int line_size); + + Location get_location (unsigned int column); + + void stop (); + + std::string to_string (Location); + + std::string location_file (Location); + + int location_line (Location); + + int location_column (Location); + +protected: + Location get_predeclared_location (); + + Location get_unknown_location (); + + bool is_predeclared (Location); + + bool is_unknown (Location); + +private: + // Whether we are currently reading a file. + bool in_file_; +}; + +Linemap *Linemap::instance_ = NULL; + +// Start getting locations from a new file. + +void +Gcc_linemap::start_file (const char *file_name, unsigned line_begin) +{ + if (this->in_file_) + linemap_add (line_table, LC_LEAVE, 0, NULL, 0); + linemap_add (line_table, LC_ENTER, 0, file_name, line_begin); + this->in_file_ = true; +} + +// Stringify a location + +std::string +Gcc_linemap::to_string (Location location) +{ + const line_map_ordinary *lmo; + location_t resolved_location; + + // Screen out unknown and predeclared locations; produce output + // only for simple file:line locations. + resolved_location + = linemap_resolve_location (line_table, location.gcc_location (), + LRK_SPELLING_LOCATION, &lmo); + if (lmo == NULL || resolved_location < RESERVED_LOCATION_COUNT) + return ""; + const char *path = LINEMAP_FILE (lmo); + if (!path) + return ""; + + // Strip the source file down to the base file, to reduce clutter. + std::stringstream ss; + ss << lbasename (path) << ":" << SOURCE_LINE (lmo, location.gcc_location ()) + << ":" << SOURCE_COLUMN (lmo, location.gcc_location ()); + return ss.str (); +} + +// Return the file name for a given location. + +std::string +Gcc_linemap::location_file (Location loc) +{ + return LOCATION_FILE (loc.gcc_location ()); +} + +// Return the line number for a given location. + +int +Gcc_linemap::location_line (Location loc) +{ + return LOCATION_LINE (loc.gcc_location ()); +} + +// Return the column number for a given location. +int +Gcc_linemap::location_column (Location loc) +{ + return LOCATION_COLUMN (loc.gcc_location ()); +} + +// Stop getting locations. + +void +Gcc_linemap::stop () +{ + linemap_add (line_table, LC_LEAVE, 0, NULL, 0); + this->in_file_ = false; +} + +// Start a new line. + +void +Gcc_linemap::start_line (unsigned lineno, unsigned linesize) +{ + linemap_line_start (line_table, lineno, linesize); +} + +// Get a location. + +Location +Gcc_linemap::get_location (unsigned column) +{ + return Location (linemap_position_for_column (line_table, column)); +} + +// Get the unknown location. + +Location +Gcc_linemap::get_unknown_location () +{ + return Location (UNKNOWN_LOCATION); +} + +// Get the predeclared location. + +Location +Gcc_linemap::get_predeclared_location () +{ + return Location (BUILTINS_LOCATION); +} + +// Return whether a location is the predeclared location. + +bool +Gcc_linemap::is_predeclared (Location loc) +{ + return loc.gcc_location () == BUILTINS_LOCATION; +} + +// Return whether a location is the unknown location. + +bool +Gcc_linemap::is_unknown (Location loc) +{ + return loc.gcc_location () == UNKNOWN_LOCATION; +} + +// Return the Linemap to use for the gcc backend. + +Linemap * +rust_get_linemap () +{ + return new Gcc_linemap; +} + +RichLocation::RichLocation (Location root) + : gcc_rich_loc (line_table, root.gcc_location ()) +{ + /*rich_location (line_maps *set, location_t loc, + const range_label *label = NULL);*/ +} + +RichLocation::~RichLocation () {} + +void +RichLocation::add_range (Location loc) +{ + gcc_rich_loc.add_range (loc.gcc_location ()); +} + +void +RichLocation::add_fixit_insert_before (const std::string &new_parent) +{ + gcc_rich_loc.add_fixit_insert_before (new_parent.c_str ()); +} + +void +RichLocation::add_fixit_insert_before (Location where, + const std::string &new_parent) +{ + gcc_rich_loc.add_fixit_insert_before (where.gcc_location (), + new_parent.c_str ()); +} + +void +RichLocation::add_fixit_insert_after (const std::string &new_parent) +{ + gcc_rich_loc.add_fixit_insert_after (new_parent.c_str ()); +} + +void +RichLocation::add_fixit_insert_after (Location where, + const std::string &new_parent) +{ + gcc_rich_loc.add_fixit_insert_after (where.gcc_location (), + new_parent.c_str ()); +} diff --git a/gcc/rust/rust-linemap.h b/gcc/rust/rust-linemap.h new file mode 100644 index 00000000000..0ba95f87575 --- /dev/null +++ b/gcc/rust/rust-linemap.h @@ -0,0 +1,163 @@ +// rust-linemap.h -- interface to location tracking -*- C++ -*- + +// Copyright 2011 The Go Authors. All rights reserved. +// Copyright (C) 2020-2022 Free Software Foundation, Inc. + +// Use of this source code is governed by a BSD-style +// license that can be found in the '../go/gofrontend/LICENSE' file. + +#ifndef RUST_LINEMAP_H +#define RUST_LINEMAP_H + +#include "rust-system.h" + +// The backend must define a type named Location which holds +// information about a location in a source file. The only thing the +// frontend does with instances of Location is pass them back to the +// backend interface. The Location type must be assignable, and it +// must be comparable: i.e., it must support operator= and operator<. +// The type is normally passed by value rather than by reference, and +// it should support that efficiently. The type should be defined in +// "rust-location.h". +#include "rust-location.h" + +// The Linemap class is a pure abstract interface, plus some static +// convenience functions. The backend must implement the interface. + +/* TODO: probably better to replace linemap implementation as pure abstract + * interface with some sort of compile-time switch (macros or maybe templates if + * doable without too much extra annoyance) as to the definition of the methods + * or whatever. This is to improve performance, as virtual function calls would + * otherwise have to be made in tight loops like in the lexer. */ + +class Linemap +{ +public: + Linemap () + { + // Only one instance of Linemap is allowed to exist. + rust_assert (Linemap::instance_ == NULL); + Linemap::instance_ = this; + } + + virtual ~Linemap () { Linemap::instance_ = NULL; } + + // Subsequent Location values will come from the file named + // FILE_NAME, starting at LINE_BEGIN. Normally LINE_BEGIN will be + // 0, but it will be non-zero if the Rust source has a //line comment. + virtual void start_file (const char *file_name, unsigned int line_begin) = 0; + + // Subsequent Location values will come from the line LINE_NUMBER, + // in the current file. LINE_SIZE is the size of the line in bytes. + // This will normally be called for every line in a source file. + virtual void start_line (unsigned int line_number, unsigned int line_size) + = 0; + + // Get a Location representing column position COLUMN on the current + // line in the current file. + virtual Location get_location (unsigned int column) = 0; + + // Stop generating Location values. This will be called after all + // input files have been read, in case any cleanup is required. + virtual void stop () = 0; + + // Produce a human-readable description of a Location, e.g. + // "foo.rust:10". Returns an empty string for predeclared, builtin or + // unknown locations. + virtual std::string to_string (Location) = 0; + + // Return the file name for a given location. + virtual std::string location_file (Location) = 0; + + // Return the line number for a given location. + virtual int location_line (Location) = 0; + + // Return the column number for a given location. + virtual int location_column (Location) = 0; + +protected: + // Return a special Location used for predeclared identifiers. This + // Location should be different from that for any actual source + // file. This location will be used for various different types, + // functions, and objects created by the frontend. + virtual Location get_predeclared_location () = 0; + + // Return a special Location which indicates that no actual location + // is known. This is used for undefined objects and for errors. + virtual Location get_unknown_location () = 0; + + // Return whether the argument is the Location returned by + // get_predeclared_location. + virtual bool is_predeclared (Location) = 0; + + // Return whether the argument is the Location returned by + // get_unknown_location. + virtual bool is_unknown (Location) = 0; + + // The single existing instance of Linemap. + static Linemap *instance_; + +public: + // Following are convenience static functions, which allow us to + // access some virtual functions without explicitly passing around + // an instance of Linemap. + + // Return the special Location used for predeclared identifiers. + static Location predeclared_location () + { + rust_assert (Linemap::instance_ != NULL); + return Linemap::instance_->get_predeclared_location (); + } + + // Return the special Location used when no location is known. + static Location unknown_location () + { + rust_assert (Linemap::instance_ != NULL); + return Linemap::instance_->get_unknown_location (); + } + + // Return whether the argument is the special location used for + // predeclared identifiers. + static bool is_predeclared_location (Location loc) + { + rust_assert (Linemap::instance_ != NULL); + return Linemap::instance_->is_predeclared (loc); + } + + // Return whether the argument is the special location used when no + // location is known. + static bool is_unknown_location (Location loc) + { + rust_assert (Linemap::instance_ != NULL); + return Linemap::instance_->is_unknown (loc); + } + + // Produce a human-readable description of a Location. + static std::string location_to_string (Location loc) + { + rust_assert (Linemap::instance_ != NULL); + return Linemap::instance_->to_string (loc); + } + + // Return the file name of a location. + static std::string location_to_file (Location loc) + { + rust_assert (Linemap::instance_ != NULL); + return Linemap::instance_->location_file (loc); + } + + // Return line number of a location. + static int location_to_line (Location loc) + { + rust_assert (Linemap::instance_ != NULL); + return Linemap::instance_->location_line (loc); + } + + static int location_to_column (Location loc) + { + rust_assert (Linemap::instance_ != NULL); + return Linemap::instance_->location_column (loc); + } +}; + +#endif // !defined(RUST_LINEMAP_H) diff --git a/gcc/rust/rust-location.h b/gcc/rust/rust-location.h new file mode 100644 index 00000000000..1bb875fe6a4 --- /dev/null +++ b/gcc/rust/rust-location.h @@ -0,0 +1,105 @@ +// Copyright (C) 2020-2022 Free Software Foundation, Inc. + +// 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 +// . + +// rust-location.h -- GCC specific Location declaration. -*- C++ -*- + +#ifndef RUST_LOCATION_H +#define RUST_LOCATION_H + +#include "rust-system.h" + +// A location in an input source file. + +class Location +{ +public: + Location () : gcc_loc_ (UNKNOWN_LOCATION) {} + + explicit Location (location_t loc) : gcc_loc_ (loc) {} + + location_t gcc_location () const { return gcc_loc_; } + + Location operator+= (location_t rhs) + { + gcc_loc_ += rhs; + return *this; + } + + Location operator-= (location_t rhs) + { + gcc_loc_ -= rhs; + return *this; + } + + bool operator== (location_t rhs) { return rhs == gcc_loc_; } + +private: + location_t gcc_loc_; +}; + +// The Rust frontend requires the ability to compare Locations. + +inline bool +operator< (Location loca, Location locb) +{ + return loca.gcc_location () < locb.gcc_location (); +} + +inline bool +operator== (Location loca, Location locb) +{ + return loca.gcc_location () == locb.gcc_location (); +} + +inline Location +operator+ (Location lhs, location_t rhs) +{ + lhs += rhs; + return lhs; +} + +inline Location +operator- (Location lhs, location_t rhs) +{ + lhs -= rhs; + return lhs; +} + +class RichLocation +{ +public: + RichLocation (Location root); + ~RichLocation (); + + void add_range (Location loc); + + void add_fixit_insert_before (const std::string &new_parent); + + void add_fixit_insert_before (Location where, const std::string &new_parent); + + void add_fixit_insert_after (const std::string &new_parent); + + void add_fixit_insert_after (Location where, const std::string &new_parent); + + const rich_location &get () const { return gcc_rich_loc; } + +private: + rich_location gcc_rich_loc; +}; + +#endif // !defined(RUST_LOCATION_H) diff --git a/gcc/rust/rust-system.h b/gcc/rust/rust-system.h new file mode 100644 index 00000000000..3a600237966 --- /dev/null +++ b/gcc/rust/rust-system.h @@ -0,0 +1,86 @@ +// rust-system.h -- Rust frontend inclusion of gcc header files -*- C++ -*- +// Copyright (C) 2009-2022 Free Software Foundation, Inc. + +// 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 +// . + +#ifndef RUST_SYSTEM_H +#define RUST_SYSTEM_H + +#define INCLUDE_ALGORITHM +#include "config.h" + +/* Define this so that inttypes.h defines the PRI?64 macros even + when compiling with a C++ compiler. Define it here so in the + event inttypes.h gets pulled in by another header it is already + defined. */ +#define __STDC_FORMAT_MACROS + +// These must be included before the #poison declarations in system.h. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Rust frontend requires C++11 minimum, so will have unordered_map and set +#include +#include + +/* We don't really need iostream, but some versions of gmp.h include + * it when compiled with C++, which means that we need to include it + * before the macro magic of safe-ctype.h, which is included by + * system.h. */ +#include + +#include "system.h" +#include "ansidecl.h" +#include "coretypes.h" + +#include "diagnostic-core.h" /* For error_at and friends. */ +#include "intl.h" /* For _(). */ + +#define RUST_ATTRIBUTE_NORETURN ATTRIBUTE_NORETURN + +// File separator to use based on whether or not the OS we're working with is +// DOS-based +#if defined(HAVE_DOS_BASED_FILE_SYSTEM) +constexpr static const char *file_separator = "\\"; +#else +constexpr static const char *file_separator = "/"; +#endif /* HAVE_DOS_BASED_FILE_SYSTEM */ + +// When using gcc, rust_assert is just gcc_assert. +#define rust_assert(EXPR) gcc_assert (EXPR) + +// When using gcc, rust_unreachable is just gcc_unreachable. +#define rust_unreachable() gcc_unreachable () + +extern void +rust_preserve_from_gc (tree t); + +extern const char * +rust_localize_identifier (const char *ident); + +#endif // !defined(RUST_SYSTEM_H)