From patchwork Thu Jun 15 00:28:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 108217 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp309774vqr; Wed, 14 Jun 2023 17:29:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5iKI3o5LmodeKejNBBt4rkjt3zRJfDn9tGEHQsUiOVPJcPq4h5/txCq1WQNHqoDPM/8wsQ X-Received: by 2002:a17:907:1b1a:b0:978:992e:efd1 with SMTP id mp26-20020a1709071b1a00b00978992eefd1mr14940113ejc.35.1686788954952; Wed, 14 Jun 2023 17:29:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686788954; cv=none; d=google.com; s=arc-20160816; b=t6/9YUguNmGt6jY3OhIe9EhhQAr1cKo5t3hnBFvdy9gUZVswNsLVp5j6vUvo170Xkt 6MZGPGXJ0lrAPZN82j8fQgKF0en2R7mvm8CNi3hapOycCM4gKqLkSlTx91odsTKlIlun Z4IRpj6aLdwHbEJSG5oAIa3BOsrQqZtc3iW5BHRZXqeCjuFrj+5CK33sXKPPIpP2oEla VBl/CfY1Z18DnpgeiDoTl/TnKUUPGhRS5WsZ+KCx7Srp0ecDabQAMh5MZLHlwLSbKMKy XcdAgxJ5CbOP1iWa8A8J+8cNhTnc/xn0RuNhdGbfau/tWCI5TRgKKe2Qy+TyXe5k/GEr kzDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=nK+A7on4KThmKi+2UPkcryjNcUIFxcl3lN2fdyQsIfg=; b=SbPuwWNAPZQUOajQ8yG96tQzoGhxocjj/ndAMT17WRduT/0xJYww5XaFoo2xbl4TEF L0YkqQ+j5ZSuQ8sHQk2Tv1myd9JUQNzEbanqcSmo9lY7xZNA+FHiLKyzD6SBquBOz1CK v1YtWPXq/u8EnSn3MfRDUHNdBhFOWkLr6aXDgFGmhrSJGAMRTbSHlvJni5/nfsODtAam xpFoutOhGkZwSzc+Z8egZ5HzzPAMXse4MFlu7X5ZtuDGXe0DU9OY/nmDjIgZCItfj6T3 LKY4oDyEfHCOSabehpJ95JwT4Wl9TtXvsO5Owr+f+0+jBIJ2akLML82WpyROzaQSl4CH 9eig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=EpSiqbhB; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y9-20020a17090668c900b00982a59bf247si56087ejr.1027.2023.06.14.17.29.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jun 2023 17:29:14 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=EpSiqbhB; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB4C43858414 for ; Thu, 15 Jun 2023 00:29:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB4C43858414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686788953; bh=nK+A7on4KThmKi+2UPkcryjNcUIFxcl3lN2fdyQsIfg=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=EpSiqbhBslRSZB6VnJSH/8GsRwG05bXaGC2fuonZCtBlhOuq0MPgOIgkK01fgKMuL B/3IpZOXFtEVuH5GMb++nHHPKbrdxqTEdZvqVmZRt+3cQ5L248HlP/3PoOViNQgne0 xpdvQye66XgGL6Fg7uD0wcQxddvFdn/UzQgfl0kQ= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 8B3863858C1F for ; Thu, 15 Jun 2023 00:28:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B3863858C1F Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-263-EhY4ySNePpKkt6TNsuMgoA-1; Wed, 14 Jun 2023 20:28:16 -0400 X-MC-Unique: EhY4ySNePpKkt6TNsuMgoA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6DBD28028B2 for ; Thu, 15 Jun 2023 00:28:16 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.8.92]) by smtp.corp.redhat.com (Postfix) with ESMTP id 44CA41121315; Thu, 15 Jun 2023 00:28:16 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH] c++: provide #include hint for missing includes [PR110164] Date: Wed, 14 Jun 2023 20:28:14 -0400 Message-Id: <20230615002814.967814-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.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_NONE, SPF_HELO_NONE, SPF_NONE, 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: , X-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm 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?1768726415379734825?= X-GMAIL-MSGID: =?utf-8?q?1768726415379734825?= PR c++/110164 notes that in cases where we have a forward decl of a std library type such as: std::array x; we omit this diagnostic: error: aggregate ‘std::array x’ has incomplete type and cannot be defined This patch adds this hint to the diagnostic: note: ‘std::array’ is defined in header ‘’; this is probably fixable by adding ‘#include ’ Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. OK for trunk? gcc/cp/ChangeLog: PR c++/110164 * cp-name-hint.h (maybe_suggest_missing_header): New decl. * decl.cc: Define INCLUDE_MEMORY. Add include of "cp/cp-name-hint.h". (start_decl_1): Call maybe_suggest_missing_header. * name-lookup.cc (maybe_suggest_missing_header): Remove "static". gcc/testsuite/ChangeLog: PR c++/110164 * g++.dg/missing-header-pr110164.C: New test. --- gcc/cp/cp-name-hint.h | 3 +++ gcc/cp/decl.cc | 10 ++++++++++ gcc/cp/name-lookup.cc | 2 +- gcc/testsuite/g++.dg/missing-header-pr110164.C | 10 ++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/missing-header-pr110164.C diff --git a/gcc/cp/cp-name-hint.h b/gcc/cp/cp-name-hint.h index bfa7c53c8f6..e2387e23d1f 100644 --- a/gcc/cp/cp-name-hint.h +++ b/gcc/cp/cp-name-hint.h @@ -32,6 +32,9 @@ along with GCC; see the file COPYING3. If not see extern name_hint suggest_alternatives_for (location_t, tree, bool); extern name_hint suggest_alternatives_in_other_namespaces (location_t, tree); +extern name_hint maybe_suggest_missing_header (location_t location, + tree name, + tree scope); extern name_hint suggest_alternative_in_explicit_scope (location_t, tree, tree); extern name_hint suggest_alternative_in_scoped_enum (tree, tree); diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index a672e4844f1..504b08ec250 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -27,6 +27,7 @@ along with GCC; see the file COPYING3. If not see line numbers. For example, the CONST_DECLs for enum values. */ #include "config.h" +#define INCLUDE_MEMORY #include "system.h" #include "coretypes.h" #include "target.h" @@ -46,6 +47,7 @@ along with GCC; see the file COPYING3. If not see #include "c-family/c-objc.h" #include "c-family/c-pragma.h" #include "c-family/c-ubsan.h" +#include "cp/cp-name-hint.h" #include "debug.h" #include "plugin.h" #include "builtins.h" @@ -5995,7 +5997,11 @@ start_decl_1 (tree decl, bool initialized) ; /* An auto type is ok. */ else if (TREE_CODE (type) != ARRAY_TYPE) { + auto_diagnostic_group d; error ("variable %q#D has initializer but incomplete type", decl); + maybe_suggest_missing_header (input_location, + TYPE_IDENTIFIER (type), + TYPE_CONTEXT (type)); type = TREE_TYPE (decl) = error_mark_node; } else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) @@ -6011,8 +6017,12 @@ start_decl_1 (tree decl, bool initialized) gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type)); else { + auto_diagnostic_group d; error ("aggregate %q#D has incomplete type and cannot be defined", decl); + maybe_suggest_missing_header (input_location, + TYPE_IDENTIFIER (type), + TYPE_CONTEXT (type)); /* Change the type so that assemble_variable will give DECL an rtl we can live with: (mem (const_int 0)). */ type = TREE_TYPE (decl) = error_mark_node; diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 6ac58a35b56..917b481c163 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -6796,7 +6796,7 @@ maybe_suggest_missing_std_header (location_t location, tree name) for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't applicable. */ -static name_hint +name_hint maybe_suggest_missing_header (location_t location, tree name, tree scope) { if (scope == NULL_TREE) diff --git a/gcc/testsuite/g++.dg/missing-header-pr110164.C b/gcc/testsuite/g++.dg/missing-header-pr110164.C new file mode 100644 index 00000000000..15980071c38 --- /dev/null +++ b/gcc/testsuite/g++.dg/missing-header-pr110164.C @@ -0,0 +1,10 @@ +// { dg-require-effective-target c++11 } + +#include + +std::array a1; /* { dg-error "incomplete type" } */ +/* { dg-message "'std::array' is defined in header ''; this is probably fixable by adding '#include '" "hint" { target *-*-* } .-1 } */ + +std::array a2 {5}; /* { dg-error "incomplete type" } */ +/* { dg-message "'std::array' is defined in header ''; this is probably fixable by adding '#include '" "hint" { target *-*-* } .-1 } */ +