From patchwork Tue Jun 13 16:29:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 107458 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp675471vqr; Tue, 13 Jun 2023 09:30:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4vFjSiqBx/hyAg2MBGJzAzLGvaMIv67LpcA6tBv/6TdbwjY2vzG0ol8P22hsIdgZGBI/NZ X-Received: by 2002:a17:906:fe06:b0:960:d9d:ffb5 with SMTP id wy6-20020a170906fe0600b009600d9dffb5mr12206545ejb.41.1686673826728; Tue, 13 Jun 2023 09:30:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686673826; cv=none; d=google.com; s=arc-20160816; b=J9+qRrdgJ10yMvF2DWN0dPg27wekoH9bn9Gz8jjGy+qDwTqVHMP/VtBVKdfMjBqfjT qamKjW5QXTideYPJcu+yEMIiZIJSTPPlFI9X2bFMVut1iPZ6K8s/QAoU1ZEP+eY0QSD6 vHFD8phY4s1ta2JY4hQwleI0PAgDTGV+IM5C5yNhPVyLXMw0fjcmYlx71LVbX1G5r6N0 CcC868kSFRx/IxSKXRjaFqRW2i/1nsC3Jks2BkNEVRZQAPO1mhkamoeHAlt9UTfvcgx1 sM0fs+CyO6/JzJ5ffqBJ+tCHJ/ruqxs/IgSC+YLi9rjHhyixlwCUhy+KAbEztFOUGokK ADQw== 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-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=phLw0EfQYyjcMgZF7/0cSCcYCxfkxxVJBLSygPVK6Gg=; b=aXwtwdH1R1E2UoRHU53Y0QFUf+i/9pVvruy3p44uF7lunHa+YM8XK0SVIq+1kEDi30 AbxxlXRNihbXbpAHIJHerq9lFcXtmDcD4/QdNIHBn9Fsjv2K9LuL0yjV3+J0OU3oMEiQ rASo1wX0L6ge3Rl3hiNKAPvTkyaajwo5IrzbGt86U11vHrjspZG0Ffx3I4RlGAAIWwZe 5K+fyTWRC6sVy5vdKQJ1z85Xc9F4cV7x4zVrR+4uU44x+CX/uZO0QSyPiTq/FZNB9iON X4BVij/n8Lp7GgS8++hVRAUhHxCON3qswq1IfObzwJ5rk4Q7p8EREgFl5WzSbKruCGxA bxlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=V4I2dofF; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id n9-20020a170906688900b009823d9f9eccsi1084724ejr.502.2023.06.13.09.30.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jun 2023 09:30:26 -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=@gcc.gnu.org header.s=default header.b=V4I2dofF; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 62E9E3858416 for ; Tue, 13 Jun 2023 16:30:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 62E9E3858416 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686673825; bh=phLw0EfQYyjcMgZF7/0cSCcYCxfkxxVJBLSygPVK6Gg=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=V4I2dofFEq/9Yq6K4JRmqOYjUFmGMTtNszyJHkVBfOc9vz2dqjy3BNmGXjO/fGGxg LLcKonLuC34Ikk40v8z8J62Ev3gtw97RebS+CHbvIx4wuASLHAHKNNeSdwq9qDLRlY pAd2sO5dIaVSlkHvm7CVmc8zKkzdpBPDmJMj3EUA= 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 8A98C3858CDA for ; Tue, 13 Jun 2023 16:29:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8A98C3858CDA 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-133-P4VA_r0KOpChGY6A6MrX_g-1; Tue, 13 Jun 2023 12:29:40 -0400 X-MC-Unique: P4VA_r0KOpChGY6A6MrX_g-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AB4CB811E85; Tue, 13 Jun 2023 16:29:39 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 650A940C6F5D; Tue, 13 Jun 2023 16:29:38 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 35DGTZ6N1131445 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 13 Jun 2023 18:29:36 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 35DGTYjc1131444; Tue, 13 Jun 2023 18:29:34 +0200 Date: Tue, 13 Jun 2023 18:29:34 +0200 To: "Joseph S. Myers" , Marek Polacek , Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] libcpp: Diagnose #include after failed __has_include [PR80753] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek 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?1768605694367605863?= X-GMAIL-MSGID: =?utf-8?q?1768605694367605863?= Hi! As can be seen in the testcase, we don't diagnose #include/#include_next of a non-existent header if __has_include/__has_include_next is done for that header first. The problem is that we normally error the first time some header is not found, but in the _cpp_FFK_HAS_INCLUDE case obviously don't want to diagnose it, just expand it to 0. And libcpp caches both successful includes and unsuccessful ones. The following patch fixes that by remembering that we haven't diagnosed error when using __has_include* on it, and diagnosing it when using the cache entry in normal mode the first time. I think _cpp_FFK_NORMAL is the only mode in which we normally diagnose errors, for _cpp_FFK_PRE_INCLUDE that open_file_failed isn't reached and for _cpp_FFK_FAKE neither. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk and after a while for backports? 2023-06-13 Jakub Jelinek PR preprocessor/80753 libcpp/ * files.cc (struct _cpp_file): Add deferred_error bitfield. (_cpp_find_file): When finding a file in cache with deferred_error set in _cpp_FFK_NORMAL mode, call open_file_failed and clear the flag. Set deferred_error in _cpp_FFK_HAS_INCLUDE mode if open_file_failed hasn't been called. gcc/testsuite/ * c-c++-common/missing-header-5.c: New test. Jakub --- libcpp/files.cc.jj 2023-01-16 11:52:16.326730483 +0100 +++ libcpp/files.cc 2023-06-13 11:27:59.867465878 +0200 @@ -109,6 +109,10 @@ struct _cpp_file /* If this file is implicitly preincluded. */ bool implicit_preinclude : 1; + /* Set if a header wasn't found with __has_include or __has_include_next + and error should be emitted if it is included normally. */ + bool deferred_error : 1; + /* > 0: Known C++ Module header unit, <0: known not. ==0, unknown */ int header_unit : 2; }; @@ -523,7 +527,14 @@ _cpp_find_file (cpp_reader *pfile, const cpp_file_hash_entry *entry = search_cache ((struct cpp_file_hash_entry *) *hash_slot, start_dir); if (entry) - return entry->u.file; + { + if (entry->u.file->deferred_error && kind == _cpp_FFK_NORMAL) + { + open_file_failed (pfile, entry->u.file, angle_brackets, loc); + entry->u.file->deferred_error = false; + } + return entry->u.file; + } _cpp_file *file = make_cpp_file (start_dir, fname); file->implicit_preinclude @@ -589,6 +600,8 @@ _cpp_find_file (cpp_reader *pfile, const if (kind != _cpp_FFK_HAS_INCLUDE) open_file_failed (pfile, file, angle_brackets, loc); + else + file->deferred_error = true; break; } --- gcc/testsuite/c-c++-common/missing-header-5.c.jj 2023-06-13 11:29:49.345931030 +0200 +++ gcc/testsuite/c-c++-common/missing-header-5.c 2023-06-13 11:25:34.952497526 +0200 @@ -0,0 +1,15 @@ +/* PR preprocessor/80753 */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +#if __has_include("nonexistent.h") +# error +#endif + +#include "nonexistent.h" + +/* { dg-message "nonexistent.h" "nonexistent.h" { target *-*-* } 0 } */ +/* { dg-message "terminated" "terminated" { target *-*-* } 0 } */ + +/* This declaration should not receive any diagnostic. */ +foo bar;