From patchwork Mon Oct 7 15:53:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1993723 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=RgQPRnrB; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XMkHz6RQgz1xsn for ; Tue, 8 Oct 2024 02:55:11 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8ACCD3861005 for ; Mon, 7 Oct 2024 15:55:09 +0000 (GMT) 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 ESMTP id 6E3D13858CDA for ; Mon, 7 Oct 2024 15:54:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6E3D13858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6E3D13858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728316490; cv=none; b=psrkUz0VVoJeeZUovjXToZGZNFzKbpJWgqiKCspUwFj/QWvu+JqmDpvqX4v6T5Fz/TZ5NfcOAwjpfqtsoCsjDDF48QgvWjO18UswqhotAAke4Fi+OannFA/FM9TwTLus/LPLWAQLkHc85ATZihpnGFjFWNolnEjpi/rX6bmM5DU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728316490; c=relaxed/simple; bh=NwT/LA0Dck88yTksZRDL+pTT/1UZbmTT7GcUu21RIaM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=D3Q9CVKtnQzCOlrK7OrHIUf0W3CS/Q19PM5uYpnOXXD3W/4kglCvgj26CSr2gd9S7IL3JaIYD2cxUMRpoiU9P5vZ48al6oYcD4eCj8D8vNZ55uOTrfFvNRpeA8LlLg5Db+auFLbL01WA986QfLehh51EnKCMGRylPio8kl7QeRI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728316487; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hZKrTzfhS/rnTV40zdKiK4TSFLISHbqmgFGBUjpIEkY=; b=RgQPRnrBNOKijDoXhR+BssjtqxIyafZHYAM67m8dtx7oChZ7AO2bx2Ab3eY1zdzeRWScKL JMov6asZKbme/OFs6u3rKyDreME+E0t5zL4zxmhrIBBfnGBk81dp8+SasLvLHvDD5vI2e2 LHlhDqSc37XR3z9dPiFmv3JwYJpLhFE= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-433--5QC7SwZOlCHK9BIEvxONQ-1; Mon, 07 Oct 2024 11:54:45 -0400 X-MC-Unique: -5QC7SwZOlCHK9BIEvxONQ-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-6cb4b3573b0so86116096d6.1 for ; Mon, 07 Oct 2024 08:54:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728316485; x=1728921285; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hZKrTzfhS/rnTV40zdKiK4TSFLISHbqmgFGBUjpIEkY=; b=NmH6kVjWyD8tfERU6el2sEJPjoxTfSeVEWMuSld2fyHzg6Suh+ETl/6CM0LJCBfOO9 e1DrK0jWEJRfsQh3neqMa7l6ad8YjIYP57NpQNvk6Kjed0wbgRMzIb6Kn+EQhV45HXHE Q0H5kDCR/7osJ4ZuVLizbW/O/MflNfD37qHvihexP1N4SfeypHH01BCqXPRLBodYzhVX UwE85P7P0CUHm93ihKLEjVl3BR8SXQUrRqL5/PvOVJNkvmM2T/ZeIDH5mTrCXObTboL9 NLIvQkVw4yUMTCyK5ftYQQ2ZU/Wmx6CKc+kRd1G4irrNSY6UciA5x5AfCwq+HvQDZdAr +CPA== X-Gm-Message-State: AOJu0YxLr+GNMvKsN8pAi2Kl1BaKHlH2fIhp1yqmTGLVmDVTv8T2btI4 vuEmke+kEuPsCw7mfaTadmP6OMySeEbtue6AhAnbH8qSfAkE+GfZVD+XUr3cAIWLL4MNLOkg/zq LZnbs/A3qFfPxGjp20MIeFYmeqcGqrCyibF8kQ4jvy+6uocUBI/uBVcm6aKRkNrNmz/Bu1+eUR7 7W6KsIFnmAUIOgXuTECovw3gBkhOZqxzX5dno= X-Received: by 2002:a05:6214:2f0d:b0:6cb:8339:5f51 with SMTP id 6a1803df08f44-6cb9a1ef990mr198065946d6.1.1728316484722; Mon, 07 Oct 2024 08:54:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEQRNijr/fThOeONCQV7xKYyON4M/tlaNqacQFK9hQknqL+Ea+tvphAUBXUrzBvV/Aa+B8XZg== X-Received: by 2002:a05:6214:2f0d:b0:6cb:8339:5f51 with SMTP id 6a1803df08f44-6cb9a1ef990mr198065546d6.1.1728316484091; Mon, 07 Oct 2024 08:54:44 -0700 (PDT) Received: from jason-thinkpadp1gen4i.rmtusma.csb (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6cba46dfeefsm26979456d6.44.2024.10.07.08.54.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 08:54:43 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: modules don't require preprocessor output Date: Mon, 7 Oct 2024 11:53:56 -0400 Message-ID: <20241007155436.2096405-1-jason@redhat.com> X-Mailer: git-send-email 2.46.2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- init_modules has rejected -M -fmodules-ts on the premise that module dependency analysis requires macro expansion, but this is no longer accurate; P1857 prohibited module directives produced by macro expansion. They can still be dependent on #if directives, but those are still handled with -fdirectives-only. What wasn't working was -M or -dM, because cpp_scan_nooutput never called module_token_pre to implement the import. The simplest fix is to use the -fdirectives-only scan when modules are enabled and teach directives_only_cb about flag_no_output. gcc/cp/ChangeLog: * module.cc (init_modules): Don't warn about -M. gcc/c-family/ChangeLog: * c-ppoutput.cc (preprocess_file): For modules, use directives-only scan even with flag_no_output. (directives_only_cb): Respect flag_no_output. gcc/ChangeLog: * doc/invoke.texi (C++ Module Preprocessing): Allow -M, refer to -fdeps. gcc/testsuite/ChangeLog: * g++.dg/modules/macro-8_a.H: New test. * g++.dg/modules/macro-8_b.C: New test. * g++.dg/modules/macro-8_c.C: New test. * g++.dg/modules/macro-8_d.C: New test. --- gcc/doc/invoke.texi | 13 ++++---- gcc/c-family/c-ppoutput.cc | 39 +++++++++++++++--------- gcc/cp/module.cc | 17 ----------- gcc/testsuite/g++.dg/modules/macro-8_b.C | 13 ++++++++ gcc/testsuite/g++.dg/modules/macro-8_c.C | 13 ++++++++ gcc/testsuite/g++.dg/modules/macro-8_d.C | 13 ++++++++ gcc/testsuite/g++.dg/modules/macro-8_a.H | 4 +++ 7 files changed, 73 insertions(+), 39 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/macro-8_b.C create mode 100644 gcc/testsuite/g++.dg/modules/macro-8_c.C create mode 100644 gcc/testsuite/g++.dg/modules/macro-8_d.C create mode 100644 gcc/testsuite/g++.dg/modules/macro-8_a.H base-commit: 5fb1ab539e3315175d2e843f4ce40bde6dd7c520 diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index d38c1feb86f..987b6360152 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -38206,13 +38206,12 @@ Whether a particular directive is translated is controlled by the module mapper. Header unit names are canonicalized during preprocessing. -Dependency information can be emitted for macro import, extending the -functionality of @option{-MD} and @option{-MMD} options. Detection of -import declarations also requires phase 4 preprocessing, and thus -requires full preprocessing (or compilation). - -The @option{-M}, @option{-MM} and @option{-E -fdirectives-only} options halt -preprocessing before phase 4. +Dependency information can be emitted for module import, extending the +functionality of the various @option{-M} options. Detection of import +declarations requires phase 4 handling of preprocessor directives, but +does not require macro expansion, so it is not necessary to use +@option{-MD}. See also @option{-fdeps-*} for an alternate format for +module dependency information. The @option{-save-temps} option uses @option{-fdirectives-only} for preprocessing, and preserve the macro definitions in the preprocessed diff --git a/gcc/c-family/c-ppoutput.cc b/gcc/c-family/c-ppoutput.cc index e3f5ca3ec97..374252bb4f3 100644 --- a/gcc/c-family/c-ppoutput.cc +++ b/gcc/c-family/c-ppoutput.cc @@ -92,10 +92,16 @@ preprocess_file (cpp_reader *pfile) cpp_scan_nooutput or cpp_get_token next. */ if (flag_no_output && pfile->buffer) { - /* Scan -included buffers, then the main file. */ - while (pfile->buffer->prev) - cpp_scan_nooutput (pfile); - cpp_scan_nooutput (pfile); + if (flag_modules) + /* For macros from imported headers we need directives_only_cb. */ + scan_translation_unit_directives_only (pfile); + else + { + /* Scan -included buffers, then the main file. */ + while (pfile->buffer->prev) + cpp_scan_nooutput (pfile); + cpp_scan_nooutput (pfile); + } } else if (cpp_get_options (pfile)->traditional) scan_translation_unit_trad (pfile); @@ -389,28 +395,31 @@ directives_only_cb (cpp_reader *pfile, CPP_DO_task task, void *data_, ...) gcc_unreachable (); case CPP_DO_print: - { - print.src_line += va_arg (args, unsigned); + if (!flag_no_output) + { + print.src_line += va_arg (args, unsigned); - const void *buf = va_arg (args, const void *); - size_t size = va_arg (args, size_t); - fwrite (buf, 1, size, print.outf); - } + const void *buf = va_arg (args, const void *); + size_t size = va_arg (args, size_t); + fwrite (buf, 1, size, print.outf); + } break; case CPP_DO_location: - maybe_print_line (va_arg (args, location_t)); + if (!flag_no_output) + maybe_print_line (va_arg (args, location_t)); break; case CPP_DO_token: { const cpp_token *token = va_arg (args, const cpp_token *); - location_t spelling_loc = va_arg (args, location_t); - streamer->stream (pfile, token, spelling_loc); + unsigned flags = 0; if (streamer->filter) + flags = lang_hooks.preprocess_token (pfile, token, streamer->filter); + if (!flag_no_output) { - unsigned flags = lang_hooks.preprocess_token - (pfile, token, streamer->filter); + location_t spelling_loc = va_arg (args, location_t); + streamer->stream (pfile, token, spelling_loc); if (flags & lang_hooks::PT_begin_pragma) streamer->begin_pragma (); } diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 65b37b4b554..2dc59ce8a12 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -20534,23 +20534,6 @@ init_modules (cpp_reader *reader) fatal_error (input_location, "C++ modules are incompatible with traditional preprocessing"); - if (flag_preprocess_only) - { - cpp_options *cpp_opts = cpp_get_options (reader); - if (flag_no_output - || (cpp_opts->deps.style != DEPS_NONE - && !cpp_opts->deps.need_preprocessor_output)) - { - auto_diagnostic_group d; - warning (0, flag_dump_macros == 'M' - ? G_("macro debug output may be incomplete with modules") - : G_("module dependencies require preprocessing")); - if (cpp_opts->deps.style != DEPS_NONE) - inform (input_location, "you should use the %<-%s%> option", - cpp_opts->deps.style == DEPS_SYSTEM ? "MD" : "MMD"); - } - } - /* :: is always exported. */ DECL_MODULE_EXPORT_P (global_namespace) = true; diff --git a/gcc/testsuite/g++.dg/modules/macro-8_b.C b/gcc/testsuite/g++.dg/modules/macro-8_b.C new file mode 100644 index 00000000000..562d4ac0e65 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/macro-8_b.C @@ -0,0 +1,13 @@ +// Test that -dM works with a header unit. + +// { dg-do preprocess } +// { dg-additional-options "-dM -fmodules-ts" } + +import "macro-8_a.H"; + +#ifndef FOO +#error FOOBAR +#endif + +// { dg-final { scan-file macro-8_b.i {#define FOO foo} } } +// { dg-final { scan-file-not macro-8_b.i {import *"} } } diff --git a/gcc/testsuite/g++.dg/modules/macro-8_c.C b/gcc/testsuite/g++.dg/modules/macro-8_c.C new file mode 100644 index 00000000000..875f8fa5ddb --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/macro-8_c.C @@ -0,0 +1,13 @@ +// Test that -M works with a header unit. + +// { dg-do preprocess } +// { dg-additional-options "-M -fmodules-ts" } + +import "macro-8_a.H"; + +#ifndef FOO +#error FOOBAR +#endif + +// { dg-final { scan-file macro-8_c.i {macro-8_a.H.gcm} } } +// { dg-final { scan-file-not macro-8_c.i {import *"} } } diff --git a/gcc/testsuite/g++.dg/modules/macro-8_d.C b/gcc/testsuite/g++.dg/modules/macro-8_d.C new file mode 100644 index 00000000000..c6322169a01 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/macro-8_d.C @@ -0,0 +1,13 @@ +// Test that -MM works with a header unit. + +// { dg-do preprocess } +// { dg-additional-options "-MM -MF macro-8_d.d -fmodules-ts" } + +#include "macro-8_a.H" + +#ifndef FOO +#error FOOBAR +#endif + +// { dg-final { scan-file macro-8_d.d {macro-8_a.H.gcm} } } +// { dg-final { scan-file-not macro-8_d.i {import *"} } } diff --git a/gcc/testsuite/g++.dg/modules/macro-8_a.H b/gcc/testsuite/g++.dg/modules/macro-8_a.H new file mode 100644 index 00000000000..281d5d79f88 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/macro-8_a.H @@ -0,0 +1,4 @@ +// { dg-additional-options "-fmodule-header" } +// { dg-module-cmi {} } + +#define FOO foo