From patchwork Thu Dec 21 12:37:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lewis Hyatt X-Patchwork-Id: 82668 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 205963858C30 for ; Thu, 21 Dec 2023 12:38:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by sourceware.org (Postfix) with ESMTPS id CCC223858C52 for ; Thu, 21 Dec 2023 12:37:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CCC223858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CCC223858C52 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::736 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703162276; cv=none; b=hqUC6YAcoWvALMrBTLAYogFO76uhrey2QsBzj5bCH+J4U6nyrLSr/qalWxpetZweD9EVRyrOA/N4wsrppoKNmRa55yv/bASX3iOzE0z3tJjyV3pAWa+gfyVJMwNu9aZFSkuNNiiq1QLgWoW3MIyjenlmZjROWchy3UjL82sZvtI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703162276; c=relaxed/simple; bh=xpp7g+tPlTlP2836Qty/LfS9yyl62huEPO9x8GLMn9o=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=sIwUNf/RcOZHe5pgaxLGZI4hr2q9ZRlgyNTY4RulK/cHpHSQxmgVQy9S1G8HMIz4I7K7c/lN+HZTManQmfbqpfuJ/X5AZWDJPy2VgouERvNWtwgiBPyi5b75lJlWUiGy5CuFKD8tuWdzQ84IkD+6HYcv5Crj+MIxXiTZasJoZkE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-qk1-x736.google.com with SMTP id af79cd13be357-7812275feccso19316685a.3 for ; Thu, 21 Dec 2023 04:37:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703162274; x=1703767074; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=m6bwgyt3qGAl4B30nGNJ6RWp4vVfrHp6+nRCsP2x24I=; b=BMFnUjwOILEpZSiB8RYjwlo7/j1rO1tBnc559FC2ukXwfSM000CD263WjoTNaZB7P+ 78EZuo5ryhi1yKfO1QXxZw25r1uju/X/JQYOemrq9/l5dmgWSkjxrU19UbGYwVcFxRAD CBHrx16wK6Avgpd9SdBgrIO9gHCLa7GGsF1n08PVCKWAUc0m6lXPsKDT3/j+e9YiG7/H Ya6LuWXGEQtDxaUnGXQ0TCfy26lSTJEWn87EjdqayQs1kt8Z9fNEHjD4OLER1jaob9QV GTs05ZEfmdCTwwd2i+do8y3bf7gyzr0+WdNgxPCq8lSfo2uio34SqsiHb4fzkbqEcPgE sdxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703162274; x=1703767074; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=m6bwgyt3qGAl4B30nGNJ6RWp4vVfrHp6+nRCsP2x24I=; b=JYmaGFosrw8+1UE5P8ySZ6/TzPcmeusc2CGjgTbkjB/NpnC5Se18udaKeflgurJlZH Qs8tbl8OAqYqi6Jy63zX6IkOiwXU+NuxOrnKCfJcwIGiU3z1ypENvYVgdizOdOKLF13X lM3GH34r+ZN3CNA0cYeI7nMnRDzMD9oMgJGYzgg9Tel4MZNjUFbyKxrPMlRY/vWrmw2K 43vYcznr2u98ZL1Lyr9quxfc1xaib80gdQdArUMSvBiTdmVzcNnU0rnkJLRzKnG0CFYd hcGbgsS8dA81XgbEIJc3ftrPE2lFN43E7k+6HxWi1+MV4vr6D8I4x2N5LbDyT5XaSCf7 9tTw== X-Gm-Message-State: AOJu0YzkQ94B2gIh7G1y6V6s/L9WzxSEEHqzolr+50dsVFy6UgmHLhen JOIX9RBIedtgHXKZoUeQvfZoiOF3V3NpxA== X-Google-Smtp-Source: AGHT+IFRSa7h4rG6ObTvTFO5HSRHnGIYKUayDUWPM2H7CD4qMjssJU5CW43VPs24XVYRfikjYHw8ag== X-Received: by 2002:a05:620a:640f:b0:77d:cb8c:38af with SMTP id pz15-20020a05620a640f00b0077dcb8c38afmr27733017qkn.4.1703162273735; Thu, 21 Dec 2023 04:37:53 -0800 (PST) Received: from localhost.localdomain (96-67-140-173-static.hfc.comcastbusiness.net. [96.67.140.173]) by smtp.gmail.com with ESMTPSA id o7-20020a05620a2a0700b007810a3294cfsm610887qkp.40.2023.12.21.04.37.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 04:37:53 -0800 (PST) From: Lewis Hyatt To: gcc-patches@gcc.gnu.org Cc: Lewis Hyatt Subject: [PATCH] libcpp: Fix __has_include_next ICE in the last directory of the path [PR80755] Date: Thu, 21 Dec 2023 07:37:50 -0500 Message-Id: <20231221123750.2303405-1-lhyatt@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3038.7 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Hello- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80755 Here is a short fix for the ICE in libcpp noted in the PR. Bootstrap + regtest all languages on x86-64 Linux. Is it OK please? Thanks! -Lewis -- >8 -- In libcpp/files.cc, the function _cpp_has_header(), which implements __has_include and __has_include_next, does not check for a NULL return value from search_path_head(), leading to an ICE tripping an assert when _cpp_find_file() tries to use it. Fix it by checking for that case and silently returning false instead. As suggested by the PR author, it is easiest to make a testcase by using the -idirafter option. To enable that, also modify the dg-additional-options testsuite procedure to make the global $srcdir available, since -idirafter requires the full path. libcpp/ChangeLog: PR preprocessor/80755 * files.cc (search_path_head): Add SUPPRESS_DIAGNOSTIC argument defaulting to false. (_cpp_has_header): Silently return false if the search path has been exhausted, rather than issuing a diagnostic and then hitting an assert. gcc/testsuite/ChangeLog: * lib/gcc-defs.exp (dg-additional-options): Make $srcdir usable in a dg-additional-options directive. * c-c++-common/cpp/has-include-next-2-dir/has-include-next-2.h: New test. * c-c++-common/cpp/has-include-next-2.c: New test. --- libcpp/files.cc | 12 ++++++++---- .../cpp/has-include-next-2-dir/has-include-next-2.h | 3 +++ gcc/testsuite/c-c++-common/cpp/has-include-next-2.c | 4 ++++ gcc/testsuite/lib/gcc-defs.exp | 1 + 4 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/cpp/has-include-next-2-dir/has-include-next-2.h create mode 100644 gcc/testsuite/c-c++-common/cpp/has-include-next-2.c diff --git a/libcpp/files.cc b/libcpp/files.cc index 27301d79fa4..aaab4b13a6a 100644 --- a/libcpp/files.cc +++ b/libcpp/files.cc @@ -181,7 +181,8 @@ static bool read_file_guts (cpp_reader *pfile, _cpp_file *file, static bool read_file (cpp_reader *pfile, _cpp_file *file, location_t loc); static struct cpp_dir *search_path_head (cpp_reader *, const char *fname, - int angle_brackets, enum include_type); + int angle_brackets, enum include_type, + bool suppress_diagnostic = false); static const char *dir_name_of_file (_cpp_file *file); static void open_file_failed (cpp_reader *pfile, _cpp_file *file, int, location_t); @@ -1041,7 +1042,7 @@ _cpp_mark_file_once_only (cpp_reader *pfile, _cpp_file *file) nothing left in the path, returns NULL. */ static struct cpp_dir * search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets, - enum include_type type) + enum include_type type, bool suppress_diagnostic) { cpp_dir *dir; _cpp_file *file; @@ -1070,7 +1071,7 @@ search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets, return make_cpp_dir (pfile, dir_name_of_file (file), pfile->buffer ? pfile->buffer->sysp : 0); - if (dir == NULL) + if (dir == NULL && !suppress_diagnostic) cpp_error (pfile, CPP_DL_ERROR, "no include path in which to search for %s", fname); @@ -2164,7 +2165,10 @@ bool _cpp_has_header (cpp_reader *pfile, const char *fname, int angle_brackets, enum include_type type) { - cpp_dir *start_dir = search_path_head (pfile, fname, angle_brackets, type); + cpp_dir *start_dir = search_path_head (pfile, fname, angle_brackets, type, + /* suppress_diagnostic = */ true); + if (!start_dir) + return false; _cpp_file *file = _cpp_find_file (pfile, fname, start_dir, angle_brackets, _cpp_FFK_HAS_INCLUDE, 0); return file->err_no != ENOENT; diff --git a/gcc/testsuite/c-c++-common/cpp/has-include-next-2-dir/has-include-next-2.h b/gcc/testsuite/c-c++-common/cpp/has-include-next-2-dir/has-include-next-2.h new file mode 100644 index 00000000000..1e4be6ce7a3 --- /dev/null +++ b/gcc/testsuite/c-c++-common/cpp/has-include-next-2-dir/has-include-next-2.h @@ -0,0 +1,3 @@ +#if __has_include_next() +/* This formerly led to an ICE when the current directory was the last one in the path. */ +#endif diff --git a/gcc/testsuite/c-c++-common/cpp/has-include-next-2.c b/gcc/testsuite/c-c++-common/cpp/has-include-next-2.c new file mode 100644 index 00000000000..4928d3e992c --- /dev/null +++ b/gcc/testsuite/c-c++-common/cpp/has-include-next-2.c @@ -0,0 +1,4 @@ +/* PR preprocessor/80755 */ +/* { dg-do preprocess } */ +/* { dg-additional-options "-idirafter $srcdir/c-c++-common/cpp/has-include-next-2-dir" } */ +#include diff --git a/gcc/testsuite/lib/gcc-defs.exp b/gcc/testsuite/lib/gcc-defs.exp index fc569c18ad5..091520ff69e 100644 --- a/gcc/testsuite/lib/gcc-defs.exp +++ b/gcc/testsuite/lib/gcc-defs.exp @@ -280,6 +280,7 @@ if { [info exists env(GCC_RUNTEST_PARALLELIZE_DIR)] \ proc dg-additional-options { args } { upvar dg-extra-tool-flags extra-tool-flags + global srcdir if { [llength $args] > 3 } { error "[lindex $args 0]: too many arguments"