From patchwork Thu Jul 28 14:01:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lewis Hyatt X-Patchwork-Id: 271 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp252959pxt; Thu, 28 Jul 2022 07:03:09 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uSvtOHGkvt9vC0jC3RLYL8Vqnj+KhuhIOGvGd+2wWRF0B2EWI7cBtcQNZeo5aXx1Aj7VVz X-Received: by 2002:a05:6402:11cb:b0:43c:c7a3:ff86 with SMTP id j11-20020a05640211cb00b0043cc7a3ff86mr6157487edw.383.1659016989592; Thu, 28 Jul 2022 07:03:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659016989; cv=none; d=google.com; s=arc-20160816; b=dEFcL9ogOMmrCsm5W3xxtVoc+MjIw/YcFWmzhV4+U7A4kkEuK6ZWGN+J9Io+VIptyr U7Dbv2ypXY0jmYqqSZYrrfHXCIjiLlgqLzpZ6HFrWR1JVQiKMej0yD4dbXdp9JTCFI3d PITiXtzHtqAmd27qVVJGm3U57BnlPJ0+K2bVlB/oLedzuyePMTSanyEJuccbQIDU4fyw cHmb9BYCcuDsJHQxXn1GBD1Wv+kpV+7HSoDll3xBrI41M4ipL2u9T5badufzQjXYGpt/ EvnJNahaQCpx8Gzg6/0xFL3dvmZ6vMDFC1NJKSt/T/uavRu/0JDIMlm+t/oR4WzyWBQj zuQQ== 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:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=GXRCh8nk8jJ7xXCT5sAUgoSOAO4n5U3hYcW6TGmHVOk=; b=gvxdf6+UhFQB8auxWqjZnz1qwjEMUvoI4AGIlU4JUBsKisyOLejsVOU7+ZR8EHjDrp ZxMPJb8iygDx48ihFG96lZJw8oamIgMVp7zZcIg+EhxLJv6LjHE6zJLqpsFp+/6U0yHa VrnLZfgBdyRGdOTqbRH8mTDOPFKNBBsUxEouHz8Zk/qtliyY3lMdQtD8amJaWNcwfXFJ bhFhRa+MPaV9miiHDZlbjWJpf10Q1wzTqnTx/kEX/YxP1atJYQYzuUk8408YfV0E9ZTC 0ZDUkqDCsQnmS+qCHprNWTXgm2E0fUEhYRaXAS8aMu+BUlckqeKlcgRllVQgFWk0ZkZ/ KNeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=WrW9ReNX; 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 o23-20020a170906769700b007269a156f59si737494ejm.348.2022.07.28.07.03.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 07:03:09 -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=WrW9ReNX; 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 6F4A6385800B for ; Thu, 28 Jul 2022 14:03:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6F4A6385800B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1659016988; bh=GXRCh8nk8jJ7xXCT5sAUgoSOAO4n5U3hYcW6TGmHVOk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=WrW9ReNXnQ7k+b+yVoBDR42FvSM7qzEFwP/ZN/a/Z3ItYjJ6GJa/LYvewU1S9nZLn LSBKcqEsF8GU3WWrP615FR26YsB1N9VOjOfJsTugp8+o5w3iFwtK84E+yu8gfhzzFo Y7rZ+2Nd+JIHOkg/Zoto9q2KIXMSoHbE6kcG/uM8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qv1-xf35.google.com (mail-qv1-xf35.google.com [IPv6:2607:f8b0:4864:20::f35]) by sourceware.org (Postfix) with ESMTPS id 585513858C50 for ; Thu, 28 Jul 2022 14:02:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 585513858C50 Received: by mail-qv1-xf35.google.com with SMTP id m10so1471256qvu.4 for ; Thu, 28 Jul 2022 07:02:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GXRCh8nk8jJ7xXCT5sAUgoSOAO4n5U3hYcW6TGmHVOk=; b=DKiZES0oQzCgVM9xOxwdgJa1Ow4O99kMsB5yDrY4KLY8lWWwWxfRkbHhsyp8qGYjji U+o95tTwe57DaEkVccoVGBD8BrOuo/M8idzczpmQsGpRaYF9Tx0LxyPakXABpzuMO7vf Kg0hD5cpOHpFwVoS/Bf8PDRGxJS8YX9MrV/FiaGjWqU0z3qZN3HURd71Lx/Ow3Iijzsu +X5TrhwhrmtVd8oRbbpPNFRDs6tXPAT+FZNJaJeEEbU+UIvGiNHBWK8SGN++xEXja2EA 2R5+t1R1VJgltkOQFM/vsx7svbG4mTmlqvFUlChV2fSxkexQDLeUBVqCdDOFPm6Uu4Ek 2GEg== X-Gm-Message-State: AJIora9oyIb5DisnFuOh1rwJQuF47TLlQevD44/nS9d64EHh0HL6UYhs RFj9Qc94MWF64O3bc7tqDOkangOOxHA= X-Received: by 2002:a05:6214:c62:b0:474:3b6f:cb6 with SMTP id t2-20020a0562140c6200b004743b6f0cb6mr20446158qvj.126.1659016939863; Thu, 28 Jul 2022 07:02:19 -0700 (PDT) Received: from localhost.localdomain (96-67-140-173-static.hfc.comcastbusiness.net. [96.67.140.173]) by smtp.gmail.com with ESMTPSA id f8-20020a05620a408800b006b60f5f53ccsm582907qko.25.2022.07.28.07.02.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 07:02:19 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix location for -Wunused-macros [PR66290] Date: Thu, 28 Jul 2022 10:01:32 -0400 Message-Id: <62328b0cdbdd374fdca89723306c34331d0531fa.1659015329.git.lhyatt@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3039.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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: Lewis Hyatt via Gcc-patches From: Lewis Hyatt Reply-To: Lewis Hyatt 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?1739605398732753935?= X-GMAIL-MSGID: =?utf-8?q?1739605398732753935?= In C++, since all tokens are lexed from libcpp up front, diagnostics generated by libcpp after lexing has completed do not get a valid location from libcpp (rather, libcpp thinks they all pertain to the end of the file.) This has long been addressed using the global variable "done_lexing", which the C++ frontend sets at the appropriate time; when done_lexing is true, then c_cpp_diagnostic(), which outputs libcpp's diagnostics, uses input_location instead of the wrong libcpp location. The C++ frontend arranges that input_location will point to the token it is currently processing, so this generally works fine. However, there is one exception currently, which is -Wunused-macros. This gets generated at the end of processing in cpp_finish (), since we need to wait until then to determine whether a macro was eventually used or not. But the locations it passes to c_cpp_diagnostic () were remembered from the original lexing and hence they should not be overridden with input_location, which is now the one incorrectly pointing to the end of the file. Fixed by setting done_lexing=false again just prior to calling cpp_finish (). I also renamed the variable from done_lexing to "override_libcpp_locations", since it's now not strictly about lexing anymore. There is no new testcase with this patch, since we already had an xfailed testcase which is now fixed. gcc/c-family/ChangeLog: PR c++/66290 * c-common.h: Rename global done_lexing to override_libcpp_locations. * c-common.cc (c_cpp_diagnostic): Likewise. * c-opts.cc (c_common_finish): Set override_libcpp_locations (formerly done_lexing) immediately prior to calling cpp_finish (). gcc/cp/ChangeLog: PR c++/66290 * parser.cc (cp_lexer_new_main): Rename global done_lexing to override_libcpp_locations. gcc/testsuite/ChangeLog: PR c++/66290 * c-c++-common/pragma-diag-15.c: Remove xfail for C++. --- Notes: Hello- The attached patch fixes PR66290, which is about C++ diagnostics using the wrong location for -Wunused-macros. Please let me know if it looks OK? Bootstrap + regtest all languages on x86-64 linux looks good, no changes other than the un-XFAILed testcases. Thank you! -Lewis gcc/c-family/c-common.cc | 10 ++++++---- gcc/c-family/c-common.h | 8 +++++--- gcc/c-family/c-opts.cc | 6 ++++++ gcc/cp/parser.cc | 2 +- gcc/testsuite/c-c++-common/pragma-diag-15.c | 2 +- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index 655c3aefee6..6e41ceb38e9 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -284,9 +284,11 @@ int c_inhibit_evaluation_warnings; be generated. */ bool in_late_binary_op; -/* Whether lexing has been completed, so subsequent preprocessor - errors should use the compiler's input_location. */ -bool done_lexing = false; +/* Depending on which phase of processing we are in, we may need + to prefer input_location to libcpp's locations. (Specifically, + after the C++ lexer is done lexing tokens, but prior to calling + cpp_finish (), we need to do so. */ +bool override_libcpp_locations; /* Information about how a function name is generated. */ struct fname_var_t @@ -6681,7 +6683,7 @@ c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED, default: gcc_unreachable (); } - if (done_lexing) + if (override_libcpp_locations) richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET); diagnostic_set_info_translated (&diagnostic, msg, ap, richloc, dlevel); diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index f9064393b4e..c06769b6f0b 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -767,10 +767,12 @@ extern int max_tinst_depth; extern int c_inhibit_evaluation_warnings; -/* Whether lexing has been completed, so subsequent preprocessor - errors should use the compiler's input_location. */ +/* Depending on which phase of processing we are in, we may need + to prefer input_location to libcpp's locations. (Specifically, + after the C++ lexer is done lexing tokens, but prior to calling + cpp_finish (), we need to do so. */ -extern bool done_lexing; +extern bool override_libcpp_locations; /* C types are partitioned into three subsets: object, function, and incomplete types. */ diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc index b9f01a65ed7..4e1463689de 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -1281,6 +1281,12 @@ c_common_finish (void) } } + /* When we call cpp_finish (), it may generate some diagnostics using + locations it remembered from the preprocessing phase, e.g. for + -Wunused-macros. So inform c_cpp_diagnostic () not to override those + locations with input_location, which would be incorrect now. */ + override_libcpp_locations = false; + /* For performance, avoid tearing down cpplib's internal structures with cpp_destroy (). */ cpp_finish (parse_in, deps_stream); diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 4f67441eeb1..66e6fde3e28 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -755,7 +755,7 @@ cp_lexer_new_main (void) /* Subsequent preprocessor diagnostics should use compiler diagnostic functions to get the compiler source location. */ - done_lexing = true; + override_libcpp_locations = true; maybe_check_all_macros (parse_in); diff --git a/gcc/testsuite/c-c++-common/pragma-diag-15.c b/gcc/testsuite/c-c++-common/pragma-diag-15.c index d8076b4f93a..8ffff88d5e3 100644 --- a/gcc/testsuite/c-c++-common/pragma-diag-15.c +++ b/gcc/testsuite/c-c++-common/pragma-diag-15.c @@ -9,5 +9,5 @@ because the location of the macro definition is incorrectly set. This is a separate issue, will resolve it in a later patch. */ -#define X /* { dg-warning "-:-Wunused-macros" {} { xfail c++ } } */ +#define X /* { dg-warning "-:-Wunused-macros" } */ #pragma GCC diagnostic ignored "-Wunused-macros"