public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Lewis Hyatt <lhyatt@gmail.com>
To: gcc-patches@gcc.gnu.org
Cc: Lewis Hyatt <lhyatt@gmail.com>
Subject: [PATCH] c-family: Let libcpp know when the compilation is for a PCH [PR9471]
Date: Fri, 10 Nov 2023 20:17:09 -0500	[thread overview]
Message-ID: <20231111011709.3071117-1-lhyatt@gmail.com> (raw)

Hello-

The PR may be 20 years old, but by now it only needs a one-line fix :). Is
it OK please? Bootstrapped + regtested all langauges on x86-64 Linux.
Thanks!

-Lewis

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=9471
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47857

-- >8 --

libcpp will generate diagnostics when it encounters things in the main file
that only belong in a header file, such as `#pragma once' or `#pragma GCC
system_header'. But sometimes the main file is a header file that is just
being compiled separately, e.g. to produce a C++ module or a PCH, in which
case such diagnostics should be suppressed. libcpp already has an interface
to request that, so make use of it in the C frontends to prevent libcpp from
issuing unwanted diagnostics when compiling a PCH.

gcc/c-family/ChangeLog:

	PR pch/9471
	PR pch/47857
	* c-opts.cc (c_common_post_options): Set cpp_opts->main_search
	so libcpp knows it is compiling a header file separately.

gcc/testsuite/ChangeLog:

	PR pch/9471
	PR pch/47857
	* g++.dg/pch/main-file-warnings.C: New test.
	* g++.dg/pch/main-file-warnings.Hs: New test.
	* gcc.dg/pch/main-file-warnings.c: New test.
	* gcc.dg/pch/main-file-warnings.hs: New test.
---
 gcc/c-family/c-opts.cc                         | 3 +++
 gcc/testsuite/g++.dg/pch/main-file-warnings.C  | 7 +++++++
 gcc/testsuite/g++.dg/pch/main-file-warnings.Hs | 3 +++
 gcc/testsuite/gcc.dg/pch/main-file-warnings.c  | 7 +++++++
 gcc/testsuite/gcc.dg/pch/main-file-warnings.hs | 3 +++
 5 files changed, 23 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/pch/main-file-warnings.C
 create mode 100644 gcc/testsuite/g++.dg/pch/main-file-warnings.Hs
 create mode 100644 gcc/testsuite/gcc.dg/pch/main-file-warnings.c
 create mode 100644 gcc/testsuite/gcc.dg/pch/main-file-warnings.hs

diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index fbabd1816c1..10403c03bd6 100644
--- a/gcc/c-family/c-opts.cc
+++ b/gcc/c-family/c-opts.cc
@@ -1174,6 +1174,9 @@ c_common_post_options (const char **pfilename)
 		       "the %qs debug info cannot be used with "
 		       "pre-compiled headers",
 		       debug_set_names (write_symbols & ~DWARF2_DEBUG));
+	  /* Let libcpp know that the main file is a header so it won't
+	     complain about things like #include_next and #pragma once.  */
+	  cpp_opts->main_search = CMS_header;
 	}
       else if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
 	c_common_no_more_pch ();
diff --git a/gcc/testsuite/g++.dg/pch/main-file-warnings.C b/gcc/testsuite/g++.dg/pch/main-file-warnings.C
new file mode 100644
index 00000000000..a9e8b0ba9f2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pch/main-file-warnings.C
@@ -0,0 +1,7 @@
+/* PR pch/9471 */
+/* PR pch/47857 */
+/* Test will fail if any warnings get issued while compiling the header into a PCH.  */
+#include "main-file-warnings.H"
+#pragma once /* { dg-warning "in main file" } */
+#pragma GCC system_header /* { dg-warning "outside include file" } */
+#include_next <stdint.h> /* { dg-warning "in primary source file" } */
diff --git a/gcc/testsuite/g++.dg/pch/main-file-warnings.Hs b/gcc/testsuite/g++.dg/pch/main-file-warnings.Hs
new file mode 100644
index 00000000000..d1582bb8290
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pch/main-file-warnings.Hs
@@ -0,0 +1,3 @@
+#pragma once /* { dg-bogus "in main file" } */
+#pragma GCC system_header /* { dg-bogus "outside include file" } */
+#include_next <stdint.h> /* { dg-bogus "in primary source file" } */
diff --git a/gcc/testsuite/gcc.dg/pch/main-file-warnings.c b/gcc/testsuite/gcc.dg/pch/main-file-warnings.c
new file mode 100644
index 00000000000..aedbc15f7ba
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pch/main-file-warnings.c
@@ -0,0 +1,7 @@
+/* PR pch/9471 */
+/* PR pch/47857 */
+/* Test will fail if any warnings get issued while compiling the header into a PCH.  */
+#include "main-file-warnings.h"
+#pragma once /* { dg-warning "in main file" } */
+#pragma GCC system_header /* { dg-warning "outside include file" } */
+#include_next <stdint.h> /* { dg-warning "in primary source file" } */
diff --git a/gcc/testsuite/gcc.dg/pch/main-file-warnings.hs b/gcc/testsuite/gcc.dg/pch/main-file-warnings.hs
new file mode 100644
index 00000000000..d1582bb8290
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pch/main-file-warnings.hs
@@ -0,0 +1,3 @@
+#pragma once /* { dg-bogus "in main file" } */
+#pragma GCC system_header /* { dg-bogus "outside include file" } */
+#include_next <stdint.h> /* { dg-bogus "in primary source file" } */

             reply	other threads:[~2023-11-11  1:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-11  1:17 Lewis Hyatt [this message]
2023-11-14  0:30 ` Joseph Myers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231111011709.3071117-1-lhyatt@gmail.com \
    --to=lhyatt@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).