From patchwork Tue Dec 12 23:18:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lewis Hyatt X-Patchwork-Id: 82012 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 00450385DC29 for ; Tue, 12 Dec 2023 23:18:25 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by sourceware.org (Postfix) with ESMTPS id 9D0093858019 for ; Tue, 12 Dec 2023 23:18:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9D0093858019 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 9D0093858019 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::72f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702423090; cv=none; b=hftu8MClTXNPg5YFZ4l9a7fIWBHkOTvaSJf0hLDHpC59V3fHUx9WMJfQ8AB53bKpqFM+xzqgHyZbssYyHJjwCgnQrTo6KD9+QIb1xQ3ch+vTrGOGNIOpxr8QUEhl01Z2WQ8E03lpKX52JUww/vByApNSVQ6xOovcXbABfkMCJEw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702423090; c=relaxed/simple; bh=0BIZVJry0rtMfPX94NkjEglQ1axWIEXOdWIcTGkLRSw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=pdHbHN8f1sHWTeTzc/DmBdXxfEwhJVqRSCP3ZQ6s1MUNlsWWcgsbz7Jwu8pam6o3ZLDjO65jm72MgX6cqcBKQHMFqX8Tjhs7tm9K0sC/8ulmx+Lhy0Y29pKOFIwja6o263WvhDp/6vpGNlso5fxdxO+Bc/sojaSOkUHX37+t8vI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-qk1-x72f.google.com with SMTP id af79cd13be357-77f43042268so427032185a.1 for ; Tue, 12 Dec 2023 15:18:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702423088; x=1703027888; 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=LqtDVKO22dLqDZHYqZo1znjrz7ILSA3pjqguNYaw2Pc=; b=JbuN0OuR5o66cQV7U0WpTsNDakXqfAdyO7PhbD8EFzpbPout7MvlKt5fcypJ1E+c6D H3E80LLLl1pN9TnonqNzBFmRqQUt4kz26ITgkIE33ZL63AGdgimnZ6OLo3g9GLJ9KQY9 F1Ve8+ErBhazjtBkaLWt9aamvknCUfP+YOXuMSzdPCqHvszH8dlv5izGb3noJcdq8AIT P7QvtZHwzLNjk4zqzyyW/Ovdo/rfe7kHXKuG65+1dlSEft17mVMSfasPx0JeuL2OyYvm cLipRmE35/6Nsss7UEmN94mM1sxKOWyml/6bS4dG9T0aV72tqPqln6WWFQf29xHpHfly bUJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702423088; x=1703027888; 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=LqtDVKO22dLqDZHYqZo1znjrz7ILSA3pjqguNYaw2Pc=; b=mZMWdkITsviObpNEX2cjfjnT96erMTiaeAP7shFhqF11vgQHrcGs1fbkvYYpebH33B QzXLNWe1KHibmRZGGTNZ4Hvz18CXu5Y0hfGVi/cXgsyv1giEv5whFm0FwClfYjhL+Xgm psliUP586tt5YOWB3gBsM6m7dO2TjWs8SJVoIddjE4fBwTw5U9e2WgOtYHmsTQ3Bj+uG oNLvM5j4HokgAtrUcgR7e0a5J7qFbV/8Np7lE0i1HcEE6WiKxBQwSRDLG+miNtEGRODB IlCcWqpFaV/X7FajEJYealjd7BQEKrr4nvyOF7smazZcXK5wXZYOkmY2KG/kcnqUJmzT ivEg== X-Gm-Message-State: AOJu0YxNozCSEMNLMO8kJ93NcF1Ie/tbRcXMNfYTW9vrr2TTQw1li7d+ vAbfpBtQQapdSL2VYhbKtjQ33fxFgdk= X-Google-Smtp-Source: AGHT+IFSwIXVU1VZ0LQZkB1R85VaMKzmJOzU5cu6sU+22+KemtwRsbH6SWSP28jPm3AfCsRqeZ1lqA== X-Received: by 2002:a05:6214:1764:b0:67a:a721:721a with SMTP id et4-20020a056214176400b0067aa721721amr9542420qvb.55.1702423087860; Tue, 12 Dec 2023 15:18:07 -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 dg1-20020a056214084100b0067ed87e51edsm1873666qvb.36.2023.12.12.15.18.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 15:18:07 -0800 (PST) From: Lewis Hyatt To: gcc-patches@gcc.gnu.org Cc: Lewis Hyatt Subject: [PATCH] libcpp: Fix macro expansion for argument of __has_include [PR110558] Date: Tue, 12 Dec 2023 18:18:03 -0500 Message-Id: <20231212231803.339670-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=110558 This is a small fix for the libcpp issue noted in the PR. Bootstrap + regtest all languages on x86-64 Linux. Is it ok for trunk please? Also, it's not a regression, having never worked since __has_include was introduced in GCC 5, but FWIW the fix would backport fine to all branches since then... so I think backport to 11,12,13 would make sense assuming the patch is OK. Thanks! -Lewis -- >8 -- When the file name for a #include directive is the result of stringifying a macro argument, libcpp needs to take some care to get the whitespace correct; in particular stringify_arg() needs to see a CPP_PADDING token between macro tokens so that it can figure out when to output space between tokens. The CPP_PADDING tokens are not normally generated when handling a preprocessor directive, but for #include-like directives, libcpp sets the state variable pfile->state.directive_wants_padding to TRUE so that the CPP_PADDING tokens will be output, and then everything works fine for computed includes. As the PR points out, things do not work fine for __has_include. Fix that by setting the state variable the same as is done for #include. libcpp/ChangeLog: PR preprocessor/110558 * macro.cc (builtin_has_include): Set pfile->state.directive_wants_padding prior to lexing the file name, in case it comes from macro expansion. gcc/testsuite/ChangeLog: PR preprocessor/110558 * c-c++-common/cpp/has-include-2.c: New test. * c-c++-common/cpp/has-include-2.h: New test. --- libcpp/macro.cc | 3 +++ gcc/testsuite/c-c++-common/cpp/has-include-2.c | 12 ++++++++++++ gcc/testsuite/c-c++-common/cpp/has-include-2.h | 1 + 3 files changed, 16 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/cpp/has-include-2.c create mode 100644 gcc/testsuite/c-c++-common/cpp/has-include-2.h diff --git a/libcpp/macro.cc b/libcpp/macro.cc index 6f24a9d6f3a..15140c60023 100644 --- a/libcpp/macro.cc +++ b/libcpp/macro.cc @@ -398,6 +398,8 @@ builtin_has_include (cpp_reader *pfile, cpp_hashnode *op, bool has_next) NODE_NAME (op)); pfile->state.angled_headers = true; + const auto sav_padding = pfile->state.directive_wants_padding; + pfile->state.directive_wants_padding = true; const cpp_token *token = cpp_get_token_no_padding (pfile); bool paren = token->type == CPP_OPEN_PAREN; if (paren) @@ -406,6 +408,7 @@ builtin_has_include (cpp_reader *pfile, cpp_hashnode *op, bool has_next) cpp_error (pfile, CPP_DL_ERROR, "missing '(' before \"%s\" operand", NODE_NAME (op)); pfile->state.angled_headers = false; + pfile->state.directive_wants_padding = sav_padding; bool bracket = token->type != CPP_STRING; char *fname = NULL; diff --git a/gcc/testsuite/c-c++-common/cpp/has-include-2.c b/gcc/testsuite/c-c++-common/cpp/has-include-2.c new file mode 100644 index 00000000000..5cd00cb3fb5 --- /dev/null +++ b/gcc/testsuite/c-c++-common/cpp/has-include-2.c @@ -0,0 +1,12 @@ +/* PR preprocessor/110558 */ +/* { dg-do preprocess } */ +#define STRINGIZE(x) #x +#define GET_INCLUDE(i) STRINGIZE(has-include-i.h) +/* Spaces surrounding the macro args previously caused a problem for __has_include(). */ +#if __has_include(GET_INCLUDE(2)) && __has_include(GET_INCLUDE( 2)) && __has_include(GET_INCLUDE( 2 )) +#include GET_INCLUDE(2) +#include GET_INCLUDE( 2) +#include GET_INCLUDE( 2 ) +#else +#error "__has_include did not handle padding properly" /* { dg-bogus "__has_include" } */ +#endif diff --git a/gcc/testsuite/c-c++-common/cpp/has-include-2.h b/gcc/testsuite/c-c++-common/cpp/has-include-2.h new file mode 100644 index 00000000000..57c402b32a8 --- /dev/null +++ b/gcc/testsuite/c-c++-common/cpp/has-include-2.h @@ -0,0 +1 @@ +/* PR preprocessor/110558 */