public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
@ 2024-03-02  6:45 Ken Matsui
  2024-03-02 13:04 ` [PATCH v2] " Ken Matsui
  0 siblings, 1 reply; 12+ messages in thread
From: Ken Matsui @ 2024-03-02  6:45 UTC (permalink / raw)
  To: gcc-patches; +Cc: Ken Matsui

This patch adds a warning switch for "#pragma once in main file".  The
warning option name is Wpragma-once-outside-header, which is the same
as Clang.

	PR preprocessor/89808

gcc/c-family/ChangeLog:

	* c-opts.cc (c_common_handle_option): Handle
	OPT_Wpragma_once_outside_header.
	* c.opt (Wpragma_once_outside_header): Define new option.

gcc/ChangeLog:

	* doc/invoke.texi (Warning Options): Document
	-Wno-pragma-once-outside-header.

libcpp/ChangeLog:

	* include/cpplib.h (struct cpp_options): Define
	cpp_warn_pragma_once_outside_header.
	(enum cpp_warning_reason): Define
	CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
	* directives.cc (do_pragma_once): Use
	cpp_warn_pragma_once_outside_header.
	* init.cc (cpp_create_reader): Handle
	cpp_warn_pragma_once_outside_header.

gcc/testsuite/ChangeLog:

	* g++.dg/Wpragma-once-outside-header.C: New test.
	* g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
	* g++.dg/warn/Wpragma-once-outside-header.C: New test.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 gcc/c-family/c-opts.cc                                 |  4 ++++
 gcc/c-family/c.opt                                     |  4 ++++
 gcc/doc/invoke.texi                                    | 10 ++++++++--
 gcc/testsuite/g++.dg/Wpragma-once-outside-header.C     |  6 ++++++
 .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
 .../g++.dg/warn/Wpragma-once-outside-header.C          |  6 ++++++
 libcpp/directives.cc                                   |  6 ++++--
 libcpp/include/cpplib.h                                |  6 +++++-
 libcpp/init.cc                                         |  1 +
 9 files changed, 43 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
 create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
 create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C

diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index be3058dca63..2868748a020 100644
--- a/gcc/c-family/c-opts.cc
+++ b/gcc/c-family/c-opts.cc
@@ -430,6 +430,10 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
       cpp_opts->warn_num_sign_change = value;
       break;
 
+    case OPT_Wpragma_once_outside_header:
+      cpp_opts->cpp_warn_pragma_once_outside_header = value;
+      break;
+
     case OPT_Wunknown_pragmas:
       /* Set to greater than 1, so that even unknown pragmas in
 	 system headers will be warned about.  */
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index b7a4a1a68e3..6841a5a5e81 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1180,6 +1180,10 @@ Wpragmas
 C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
 Warn about misuses of pragmas.
 
+Wpragma-once-outside-header
+C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) Init(1) Warning
+Warn about #pragma once outside of a header.
+
 Wprio-ctor-dtor
 C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
 Warn if constructor or destructors with priorities from 0 to 100 are used.
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index bdf05be387d..eeb8954bcdf 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
 -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
 -Wparentheses  -Wno-pedantic-ms-format
 -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
--Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
--Wrestrict  -Wno-return-local-addr  -Wreturn-type
+-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
+-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
 -Wno-scalar-storage-order  -Wsequence-point
 -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
 -Wno-shadow-ivar
@@ -7955,6 +7955,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
 invalid syntax, or conflicts between pragmas.  See also
 @option{-Wunknown-pragmas}.
 
+@opindex Wno-pragma-once-outside-header
+@opindex Wpragma-once-outside-header
+@item -Wno-pragma-once-outside-header
+Do not warn when @code{#pragma once} is used in a file that is not a header
+file, such as a main file.
+
 @opindex Wno-prio-ctor-dtor
 @opindex Wprio-ctor-dtor
 @item -Wno-prio-ctor-dtor
diff --git a/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
new file mode 100644
index 00000000000..66b2c862344
--- /dev/null
+++ b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
@@ -0,0 +1,6 @@
+/* { dg-do assemble  } */
+/* { dg-options "-Werror=pragma-once-outside-header" } */
+/* { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 } */
+
+#pragma once  // { dg-error "#pragma once in main file" }
+int main() {}
diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
new file mode 100644
index 00000000000..b5be4d25a9d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
@@ -0,0 +1,5 @@
+// { dg-do assemble  }
+// { dg-options "-Wno-pragma-once-outside-header" }
+
+#pragma once
+int main() {}
diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
new file mode 100644
index 00000000000..324b0638c3f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
@@ -0,0 +1,6 @@
+// { dg-do assemble  }
+// { dg-options "-Werror=pragma-once-outside-header" }
+// { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 }
+
+#pragma once  // { dg-error "#pragma once in main file" }
+int main() {}
diff --git a/libcpp/directives.cc b/libcpp/directives.cc
index 479f8c716e8..cc8f110b66b 100644
--- a/libcpp/directives.cc
+++ b/libcpp/directives.cc
@@ -1588,8 +1588,10 @@ do_pragma (cpp_reader *pfile)
 static void
 do_pragma_once (cpp_reader *pfile)
 {
-  if (_cpp_in_main_source_file (pfile))
-    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
+  if (CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header)
+      && _cpp_in_main_source_file (pfile))
+    cpp_warning (pfile, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
+		 "#pragma once in main file");
 
   check_eol (pfile, false);
   _cpp_mark_file_once_only (pfile, pfile->buffer->file);
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index c62374d3192..7cf49fb2f74 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -583,6 +583,9 @@ struct cpp_options
      2 if it should be a pedwarn.  */
   unsigned char cpp_warn_invalid_utf8;
 
+  /* True if libcpp should warn about #pragma once outside of a header.  */
+  bool cpp_warn_pragma_once_outside_header;
+
   /* True if libcpp should warn about invalid forms of delimited or named
      escape sequences.  */
   bool cpp_warn_unicode;
@@ -701,7 +704,8 @@ enum cpp_warning_reason {
   CPP_W_EXPANSION_TO_DEFINED,
   CPP_W_BIDIRECTIONAL,
   CPP_W_INVALID_UTF8,
-  CPP_W_UNICODE
+  CPP_W_UNICODE,
+  CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER
 };
 
 /* Callback for header lookup for HEADER, which is the name of a
diff --git a/libcpp/init.cc b/libcpp/init.cc
index 54fc9236d38..4be1afdb2c2 100644
--- a/libcpp/init.cc
+++ b/libcpp/init.cc
@@ -235,6 +235,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
   CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
   CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0;
   CPP_OPTION (pfile, cpp_warn_unicode) = 1;
+  CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header) = 1;
   CPP_OPTION (pfile, cpp_input_charset_explicit) = 0;
 
   /* Default CPP arithmetic to something sensible for the host for the
-- 
2.44.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v2] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-03-02  6:45 [PATCH] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808] Ken Matsui
@ 2024-03-02 13:04 ` Ken Matsui
  2024-03-14  8:01   ` Ken Matsui
  2024-06-13 14:31   ` [PATCH v3] " Ken Matsui
  0 siblings, 2 replies; 12+ messages in thread
From: Ken Matsui @ 2024-03-02 13:04 UTC (permalink / raw)
  To: gcc-patches; +Cc: Ken Matsui

This patch adds a warning switch for "#pragma once in main file".  The
warning option name is Wpragma-once-outside-header, which is the same
as Clang.

	PR preprocessor/89808

gcc/c-family/ChangeLog:

	* c-opts.cc (c_common_handle_option): Handle
	OPT_Wpragma_once_outside_header.
	* c.opt (Wpragma_once_outside_header): Define new option.

gcc/ChangeLog:

	* doc/invoke.texi (Warning Options): Document
	-Wno-pragma-once-outside-header.

libcpp/ChangeLog:

	* include/cpplib.h (struct cpp_options): Define
	cpp_warn_pragma_once_outside_header.
	* directives.cc (do_pragma_once): Use
	cpp_warn_pragma_once_outside_header.
	* init.cc (cpp_create_reader): Handle
	cpp_warn_pragma_once_outside_header.

gcc/testsuite/ChangeLog:

	* g++.dg/Wpragma-once-outside-header.C: New test.
	* g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
	* g++.dg/warn/Wpragma-once-outside-header.C: New test.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 gcc/c-family/c-opts.cc                                 |  9 +++++++++
 gcc/c-family/c.opt                                     |  4 ++++
 gcc/doc/invoke.texi                                    | 10 ++++++++--
 gcc/testsuite/g++.dg/Wpragma-once-outside-header.C     |  5 +++++
 .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
 .../g++.dg/warn/Wpragma-once-outside-header.C          |  5 +++++
 libcpp/directives.cc                                   |  8 ++++++--
 libcpp/include/cpplib.h                                |  4 ++++
 libcpp/init.cc                                         |  1 +
 9 files changed, 47 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
 create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
 create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C

diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index be3058dca63..4edd8c6c515 100644
--- a/gcc/c-family/c-opts.cc
+++ b/gcc/c-family/c-opts.cc
@@ -430,6 +430,15 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
       cpp_opts->warn_num_sign_change = value;
       break;
 
+    case OPT_Wpragma_once_outside_header:
+      if (value == 0)
+	cpp_opts->cpp_warn_pragma_once_outside_header = 0;
+      else if (kind == DK_ERROR)
+	cpp_opts->cpp_warn_pragma_once_outside_header = 2;
+      else
+	cpp_opts->cpp_warn_pragma_once_outside_header = 1;
+      break;
+
     case OPT_Wunknown_pragmas:
       /* Set to greater than 1, so that even unknown pragmas in
 	 system headers will be warned about.  */
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index b7a4a1a68e3..6841a5a5e81 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1180,6 +1180,10 @@ Wpragmas
 C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
 Warn about misuses of pragmas.
 
+Wpragma-once-outside-header
+C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) Init(1) Warning
+Warn about #pragma once outside of a header.
+
 Wprio-ctor-dtor
 C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
 Warn if constructor or destructors with priorities from 0 to 100 are used.
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index bdf05be387d..eeb8954bcdf 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
 -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
 -Wparentheses  -Wno-pedantic-ms-format
 -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
--Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
--Wrestrict  -Wno-return-local-addr  -Wreturn-type
+-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
+-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
 -Wno-scalar-storage-order  -Wsequence-point
 -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
 -Wno-shadow-ivar
@@ -7955,6 +7955,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
 invalid syntax, or conflicts between pragmas.  See also
 @option{-Wunknown-pragmas}.
 
+@opindex Wno-pragma-once-outside-header
+@opindex Wpragma-once-outside-header
+@item -Wno-pragma-once-outside-header
+Do not warn when @code{#pragma once} is used in a file that is not a header
+file, such as a main file.
+
 @opindex Wno-prio-ctor-dtor
 @opindex Wprio-ctor-dtor
 @item -Wno-prio-ctor-dtor
diff --git a/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
new file mode 100644
index 00000000000..678bd4e7626
--- /dev/null
+++ b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
@@ -0,0 +1,5 @@
+/* { dg-do assemble  } */
+/* { dg-options "-Werror=pragma-once-outside-header" } */
+
+#pragma once  // { dg-error "#pragma once in main file" }
+int main() {}
diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
new file mode 100644
index 00000000000..b5be4d25a9d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
@@ -0,0 +1,5 @@
+// { dg-do assemble  }
+// { dg-options "-Wno-pragma-once-outside-header" }
+
+#pragma once
+int main() {}
diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
new file mode 100644
index 00000000000..ae958d3beb8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
@@ -0,0 +1,5 @@
+// { dg-do assemble  }
+// { dg-options "-Werror=pragma-once-outside-header" }
+
+#pragma once  // { dg-error "#pragma once in main file" }
+int main() {}
diff --git a/libcpp/directives.cc b/libcpp/directives.cc
index 479f8c716e8..b6121a459f8 100644
--- a/libcpp/directives.cc
+++ b/libcpp/directives.cc
@@ -1588,8 +1588,12 @@ do_pragma (cpp_reader *pfile)
 static void
 do_pragma_once (cpp_reader *pfile)
 {
-  if (_cpp_in_main_source_file (pfile))
-    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
+  const unsigned char warn_level =
+    CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header);
+
+  if (warn_level && _cpp_in_main_source_file (pfile))
+    cpp_error (pfile, (warn_level == 1 ? CPP_DL_WARNING : CPP_DL_ERROR),
+	       "#pragma once in main file");
 
   check_eol (pfile, false);
   _cpp_mark_file_once_only (pfile, pfile->buffer->file);
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index c62374d3192..d25b9606153 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -583,6 +583,10 @@ struct cpp_options
      2 if it should be a pedwarn.  */
   unsigned char cpp_warn_invalid_utf8;
 
+  /* True if libcpp should warn about #pragma once outside of a header.
+     2 if it should be an error, i.e., -Werror.  */
+  unsigned char cpp_warn_pragma_once_outside_header;
+
   /* True if libcpp should warn about invalid forms of delimited or named
      escape sequences.  */
   bool cpp_warn_unicode;
diff --git a/libcpp/init.cc b/libcpp/init.cc
index 54fc9236d38..4be1afdb2c2 100644
--- a/libcpp/init.cc
+++ b/libcpp/init.cc
@@ -235,6 +235,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
   CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
   CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0;
   CPP_OPTION (pfile, cpp_warn_unicode) = 1;
+  CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header) = 1;
   CPP_OPTION (pfile, cpp_input_charset_explicit) = 0;
 
   /* Default CPP arithmetic to something sensible for the host for the
-- 
2.44.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-03-02 13:04 ` [PATCH v2] " Ken Matsui
@ 2024-03-14  8:01   ` Ken Matsui
  2024-05-06 13:24     ` Ken Matsui
  2024-06-04 14:54     ` Jason Merrill
  2024-06-13 14:31   ` [PATCH v3] " Ken Matsui
  1 sibling, 2 replies; 12+ messages in thread
From: Ken Matsui @ 2024-03-14  8:01 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill

On Sat, Mar 2, 2024 at 5:04 AM Ken Matsui <kmatsui@gcc.gnu.org> wrote:
>
> This patch adds a warning switch for "#pragma once in main file".  The
> warning option name is Wpragma-once-outside-header, which is the same
> as Clang.

Ping.

>
>         PR preprocessor/89808
>
> gcc/c-family/ChangeLog:
>
>         * c-opts.cc (c_common_handle_option): Handle
>         OPT_Wpragma_once_outside_header.
>         * c.opt (Wpragma_once_outside_header): Define new option.
>
> gcc/ChangeLog:
>
>         * doc/invoke.texi (Warning Options): Document
>         -Wno-pragma-once-outside-header.
>
> libcpp/ChangeLog:
>
>         * include/cpplib.h (struct cpp_options): Define
>         cpp_warn_pragma_once_outside_header.
>         * directives.cc (do_pragma_once): Use
>         cpp_warn_pragma_once_outside_header.
>         * init.cc (cpp_create_reader): Handle
>         cpp_warn_pragma_once_outside_header.
>
> gcc/testsuite/ChangeLog:
>
>         * g++.dg/Wpragma-once-outside-header.C: New test.
>         * g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
>         * g++.dg/warn/Wpragma-once-outside-header.C: New test.
>
> Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> ---
>  gcc/c-family/c-opts.cc                                 |  9 +++++++++
>  gcc/c-family/c.opt                                     |  4 ++++
>  gcc/doc/invoke.texi                                    | 10 ++++++++--
>  gcc/testsuite/g++.dg/Wpragma-once-outside-header.C     |  5 +++++
>  .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
>  .../g++.dg/warn/Wpragma-once-outside-header.C          |  5 +++++
>  libcpp/directives.cc                                   |  8 ++++++--
>  libcpp/include/cpplib.h                                |  4 ++++
>  libcpp/init.cc                                         |  1 +
>  9 files changed, 47 insertions(+), 4 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
>  create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
>  create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
>
> diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
> index be3058dca63..4edd8c6c515 100644
> --- a/gcc/c-family/c-opts.cc
> +++ b/gcc/c-family/c-opts.cc
> @@ -430,6 +430,15 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
>        cpp_opts->warn_num_sign_change = value;
>        break;
>
> +    case OPT_Wpragma_once_outside_header:
> +      if (value == 0)
> +       cpp_opts->cpp_warn_pragma_once_outside_header = 0;
> +      else if (kind == DK_ERROR)
> +       cpp_opts->cpp_warn_pragma_once_outside_header = 2;
> +      else
> +       cpp_opts->cpp_warn_pragma_once_outside_header = 1;
> +      break;
> +
>      case OPT_Wunknown_pragmas:
>        /* Set to greater than 1, so that even unknown pragmas in
>          system headers will be warned about.  */
> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> index b7a4a1a68e3..6841a5a5e81 100644
> --- a/gcc/c-family/c.opt
> +++ b/gcc/c-family/c.opt
> @@ -1180,6 +1180,10 @@ Wpragmas
>  C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
>  Warn about misuses of pragmas.
>
> +Wpragma-once-outside-header
> +C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) Init(1) Warning
> +Warn about #pragma once outside of a header.
> +
>  Wprio-ctor-dtor
>  C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
>  Warn if constructor or destructors with priorities from 0 to 100 are used.
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index bdf05be387d..eeb8954bcdf 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
>  -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
>  -Wparentheses  -Wno-pedantic-ms-format
>  -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
> --Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
> --Wrestrict  -Wno-return-local-addr  -Wreturn-type
> +-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
> +-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
>  -Wno-scalar-storage-order  -Wsequence-point
>  -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
>  -Wno-shadow-ivar
> @@ -7955,6 +7955,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
>  invalid syntax, or conflicts between pragmas.  See also
>  @option{-Wunknown-pragmas}.
>
> +@opindex Wno-pragma-once-outside-header
> +@opindex Wpragma-once-outside-header
> +@item -Wno-pragma-once-outside-header
> +Do not warn when @code{#pragma once} is used in a file that is not a header
> +file, such as a main file.
> +
>  @opindex Wno-prio-ctor-dtor
>  @opindex Wprio-ctor-dtor
>  @item -Wno-prio-ctor-dtor
> diff --git a/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
> new file mode 100644
> index 00000000000..678bd4e7626
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
> @@ -0,0 +1,5 @@
> +/* { dg-do assemble  } */
> +/* { dg-options "-Werror=pragma-once-outside-header" } */
> +
> +#pragma once  // { dg-error "#pragma once in main file" }
> +int main() {}
> diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> new file mode 100644
> index 00000000000..b5be4d25a9d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> @@ -0,0 +1,5 @@
> +// { dg-do assemble  }
> +// { dg-options "-Wno-pragma-once-outside-header" }
> +
> +#pragma once
> +int main() {}
> diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> new file mode 100644
> index 00000000000..ae958d3beb8
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> @@ -0,0 +1,5 @@
> +// { dg-do assemble  }
> +// { dg-options "-Werror=pragma-once-outside-header" }
> +
> +#pragma once  // { dg-error "#pragma once in main file" }
> +int main() {}
> diff --git a/libcpp/directives.cc b/libcpp/directives.cc
> index 479f8c716e8..b6121a459f8 100644
> --- a/libcpp/directives.cc
> +++ b/libcpp/directives.cc
> @@ -1588,8 +1588,12 @@ do_pragma (cpp_reader *pfile)
>  static void
>  do_pragma_once (cpp_reader *pfile)
>  {
> -  if (_cpp_in_main_source_file (pfile))
> -    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
> +  const unsigned char warn_level =
> +    CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header);
> +
> +  if (warn_level && _cpp_in_main_source_file (pfile))
> +    cpp_error (pfile, (warn_level == 1 ? CPP_DL_WARNING : CPP_DL_ERROR),
> +              "#pragma once in main file");
>
>    check_eol (pfile, false);
>    _cpp_mark_file_once_only (pfile, pfile->buffer->file);
> diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
> index c62374d3192..d25b9606153 100644
> --- a/libcpp/include/cpplib.h
> +++ b/libcpp/include/cpplib.h
> @@ -583,6 +583,10 @@ struct cpp_options
>       2 if it should be a pedwarn.  */
>    unsigned char cpp_warn_invalid_utf8;
>
> +  /* True if libcpp should warn about #pragma once outside of a header.
> +     2 if it should be an error, i.e., -Werror.  */
> +  unsigned char cpp_warn_pragma_once_outside_header;
> +
>    /* True if libcpp should warn about invalid forms of delimited or named
>       escape sequences.  */
>    bool cpp_warn_unicode;
> diff --git a/libcpp/init.cc b/libcpp/init.cc
> index 54fc9236d38..4be1afdb2c2 100644
> --- a/libcpp/init.cc
> +++ b/libcpp/init.cc
> @@ -235,6 +235,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
>    CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
>    CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0;
>    CPP_OPTION (pfile, cpp_warn_unicode) = 1;
> +  CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header) = 1;
>    CPP_OPTION (pfile, cpp_input_charset_explicit) = 0;
>
>    /* Default CPP arithmetic to something sensible for the host for the
> --
> 2.44.0
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-03-14  8:01   ` Ken Matsui
@ 2024-05-06 13:24     ` Ken Matsui
  2024-06-04 14:54     ` Jason Merrill
  1 sibling, 0 replies; 12+ messages in thread
From: Ken Matsui @ 2024-05-06 13:24 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill

On Thu, Mar 14, 2024 at 1:01 AM Ken Matsui <kmatsui@cs.washington.edu> wrote:
>
> On Sat, Mar 2, 2024 at 5:04 AM Ken Matsui <kmatsui@gcc.gnu.org> wrote:
> >
> > This patch adds a warning switch for "#pragma once in main file".  The
> > warning option name is Wpragma-once-outside-header, which is the same
> > as Clang.
>
> Ping.

Ping.  Ok for trunk?



>
> >
> >         PR preprocessor/89808
> >
> > gcc/c-family/ChangeLog:
> >
> >         * c-opts.cc (c_common_handle_option): Handle
> >         OPT_Wpragma_once_outside_header.
> >         * c.opt (Wpragma_once_outside_header): Define new option.
> >
> > gcc/ChangeLog:
> >
> >         * doc/invoke.texi (Warning Options): Document
> >         -Wno-pragma-once-outside-header.
> >
> > libcpp/ChangeLog:
> >
> >         * include/cpplib.h (struct cpp_options): Define
> >         cpp_warn_pragma_once_outside_header.
> >         * directives.cc (do_pragma_once): Use
> >         cpp_warn_pragma_once_outside_header.
> >         * init.cc (cpp_create_reader): Handle
> >         cpp_warn_pragma_once_outside_header.
> >
> > gcc/testsuite/ChangeLog:
> >
> >         * g++.dg/Wpragma-once-outside-header.C: New test.
> >         * g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
> >         * g++.dg/warn/Wpragma-once-outside-header.C: New test.
> >
> > Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> > ---
> >  gcc/c-family/c-opts.cc                                 |  9 +++++++++
> >  gcc/c-family/c.opt                                     |  4 ++++
> >  gcc/doc/invoke.texi                                    | 10 ++++++++--
> >  gcc/testsuite/g++.dg/Wpragma-once-outside-header.C     |  5 +++++
> >  .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
> >  .../g++.dg/warn/Wpragma-once-outside-header.C          |  5 +++++
> >  libcpp/directives.cc                                   |  8 ++++++--
> >  libcpp/include/cpplib.h                                |  4 ++++
> >  libcpp/init.cc                                         |  1 +
> >  9 files changed, 47 insertions(+), 4 deletions(-)
> >  create mode 100644 gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
> >  create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> >  create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> >
> > diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
> > index be3058dca63..4edd8c6c515 100644
> > --- a/gcc/c-family/c-opts.cc
> > +++ b/gcc/c-family/c-opts.cc
> > @@ -430,6 +430,15 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
> >        cpp_opts->warn_num_sign_change = value;
> >        break;
> >
> > +    case OPT_Wpragma_once_outside_header:
> > +      if (value == 0)
> > +       cpp_opts->cpp_warn_pragma_once_outside_header = 0;
> > +      else if (kind == DK_ERROR)
> > +       cpp_opts->cpp_warn_pragma_once_outside_header = 2;
> > +      else
> > +       cpp_opts->cpp_warn_pragma_once_outside_header = 1;
> > +      break;
> > +
> >      case OPT_Wunknown_pragmas:
> >        /* Set to greater than 1, so that even unknown pragmas in
> >          system headers will be warned about.  */
> > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> > index b7a4a1a68e3..6841a5a5e81 100644
> > --- a/gcc/c-family/c.opt
> > +++ b/gcc/c-family/c.opt
> > @@ -1180,6 +1180,10 @@ Wpragmas
> >  C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
> >  Warn about misuses of pragmas.
> >
> > +Wpragma-once-outside-header
> > +C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) Init(1) Warning
> > +Warn about #pragma once outside of a header.
> > +
> >  Wprio-ctor-dtor
> >  C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
> >  Warn if constructor or destructors with priorities from 0 to 100 are used.
> > diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> > index bdf05be387d..eeb8954bcdf 100644
> > --- a/gcc/doc/invoke.texi
> > +++ b/gcc/doc/invoke.texi
> > @@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
> >  -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
> >  -Wparentheses  -Wno-pedantic-ms-format
> >  -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
> > --Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
> > --Wrestrict  -Wno-return-local-addr  -Wreturn-type
> > +-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
> > +-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
> >  -Wno-scalar-storage-order  -Wsequence-point
> >  -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
> >  -Wno-shadow-ivar
> > @@ -7955,6 +7955,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
> >  invalid syntax, or conflicts between pragmas.  See also
> >  @option{-Wunknown-pragmas}.
> >
> > +@opindex Wno-pragma-once-outside-header
> > +@opindex Wpragma-once-outside-header
> > +@item -Wno-pragma-once-outside-header
> > +Do not warn when @code{#pragma once} is used in a file that is not a header
> > +file, such as a main file.
> > +
> >  @opindex Wno-prio-ctor-dtor
> >  @opindex Wprio-ctor-dtor
> >  @item -Wno-prio-ctor-dtor
> > diff --git a/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
> > new file mode 100644
> > index 00000000000..678bd4e7626
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
> > @@ -0,0 +1,5 @@
> > +/* { dg-do assemble  } */
> > +/* { dg-options "-Werror=pragma-once-outside-header" } */
> > +
> > +#pragma once  // { dg-error "#pragma once in main file" }
> > +int main() {}
> > diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> > new file mode 100644
> > index 00000000000..b5be4d25a9d
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> > @@ -0,0 +1,5 @@
> > +// { dg-do assemble  }
> > +// { dg-options "-Wno-pragma-once-outside-header" }
> > +
> > +#pragma once
> > +int main() {}
> > diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> > new file mode 100644
> > index 00000000000..ae958d3beb8
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> > @@ -0,0 +1,5 @@
> > +// { dg-do assemble  }
> > +// { dg-options "-Werror=pragma-once-outside-header" }
> > +
> > +#pragma once  // { dg-error "#pragma once in main file" }
> > +int main() {}
> > diff --git a/libcpp/directives.cc b/libcpp/directives.cc
> > index 479f8c716e8..b6121a459f8 100644
> > --- a/libcpp/directives.cc
> > +++ b/libcpp/directives.cc
> > @@ -1588,8 +1588,12 @@ do_pragma (cpp_reader *pfile)
> >  static void
> >  do_pragma_once (cpp_reader *pfile)
> >  {
> > -  if (_cpp_in_main_source_file (pfile))
> > -    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
> > +  const unsigned char warn_level =
> > +    CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header);
> > +
> > +  if (warn_level && _cpp_in_main_source_file (pfile))
> > +    cpp_error (pfile, (warn_level == 1 ? CPP_DL_WARNING : CPP_DL_ERROR),
> > +              "#pragma once in main file");
> >
> >    check_eol (pfile, false);
> >    _cpp_mark_file_once_only (pfile, pfile->buffer->file);
> > diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
> > index c62374d3192..d25b9606153 100644
> > --- a/libcpp/include/cpplib.h
> > +++ b/libcpp/include/cpplib.h
> > @@ -583,6 +583,10 @@ struct cpp_options
> >       2 if it should be a pedwarn.  */
> >    unsigned char cpp_warn_invalid_utf8;
> >
> > +  /* True if libcpp should warn about #pragma once outside of a header.
> > +     2 if it should be an error, i.e., -Werror.  */
> > +  unsigned char cpp_warn_pragma_once_outside_header;
> > +
> >    /* True if libcpp should warn about invalid forms of delimited or named
> >       escape sequences.  */
> >    bool cpp_warn_unicode;
> > diff --git a/libcpp/init.cc b/libcpp/init.cc
> > index 54fc9236d38..4be1afdb2c2 100644
> > --- a/libcpp/init.cc
> > +++ b/libcpp/init.cc
> > @@ -235,6 +235,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
> >    CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
> >    CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0;
> >    CPP_OPTION (pfile, cpp_warn_unicode) = 1;
> > +  CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header) = 1;
> >    CPP_OPTION (pfile, cpp_input_charset_explicit) = 0;
> >
> >    /* Default CPP arithmetic to something sensible for the host for the
> > --
> > 2.44.0
> >

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-03-14  8:01   ` Ken Matsui
  2024-05-06 13:24     ` Ken Matsui
@ 2024-06-04 14:54     ` Jason Merrill
  2024-06-13 14:31       ` Ken Matsui
  1 sibling, 1 reply; 12+ messages in thread
From: Jason Merrill @ 2024-06-04 14:54 UTC (permalink / raw)
  To: Ken Matsui, gcc-patches

On 3/14/24 04:01, Ken Matsui wrote:
> On Sat, Mar 2, 2024 at 5:04 AM Ken Matsui <kmatsui@gcc.gnu.org> wrote:
>>
>> This patch adds a warning switch for "#pragma once in main file".  The
>> warning option name is Wpragma-once-outside-header, which is the same
>> as Clang.
> 
> Ping.
> 
>>
>>          PR preprocessor/89808
>>
>> gcc/c-family/ChangeLog:
>>
>>          * c-opts.cc (c_common_handle_option): Handle
>>          OPT_Wpragma_once_outside_header.
>>          * c.opt (Wpragma_once_outside_header): Define new option.
>>
>> gcc/ChangeLog:
>>
>>          * doc/invoke.texi (Warning Options): Document
>>          -Wno-pragma-once-outside-header.
>>
>> libcpp/ChangeLog:
>>
>>          * include/cpplib.h (struct cpp_options): Define
>>          cpp_warn_pragma_once_outside_header.
>>          * directives.cc (do_pragma_once): Use
>>          cpp_warn_pragma_once_outside_header.
>>          * init.cc (cpp_create_reader): Handle
>>          cpp_warn_pragma_once_outside_header.
>>
>> gcc/testsuite/ChangeLog:
>>
>>          * g++.dg/Wpragma-once-outside-header.C: New test.

Please drop this file, keeping the duplicate in the warn subdirectory.

>>          * g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
>>          * g++.dg/warn/Wpragma-once-outside-header.C: New test.
>>
>> Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
>> ---
>>   gcc/c-family/c-opts.cc                                 |  9 +++++++++
>>   gcc/c-family/c.opt                                     |  4 ++++
>>   gcc/doc/invoke.texi                                    | 10 ++++++++--
>>   gcc/testsuite/g++.dg/Wpragma-once-outside-header.C     |  5 +++++
>>   .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
>>   .../g++.dg/warn/Wpragma-once-outside-header.C          |  5 +++++
>>   libcpp/directives.cc                                   |  8 ++++++--
>>   libcpp/include/cpplib.h                                |  4 ++++
>>   libcpp/init.cc                                         |  1 +
>>   9 files changed, 47 insertions(+), 4 deletions(-)
>>   create mode 100644 gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
>>   create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
>>   create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
>>
>> diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
>> index be3058dca63..4edd8c6c515 100644
>> --- a/gcc/c-family/c-opts.cc
>> +++ b/gcc/c-family/c-opts.cc
>> @@ -430,6 +430,15 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
>>         cpp_opts->warn_num_sign_change = value;
>>         break;
>>
>> +    case OPT_Wpragma_once_outside_header:
>> +      if (value == 0)
>> +       cpp_opts->cpp_warn_pragma_once_outside_header = 0;
>> +      else if (kind == DK_ERROR)
>> +       cpp_opts->cpp_warn_pragma_once_outside_header = 2;
>> +      else
>> +       cpp_opts->cpp_warn_pragma_once_outside_header = 1;
>> +      break;

Rather than encode the -Werror this way...

>>       case OPT_Wunknown_pragmas:
>>         /* Set to greater than 1, so that even unknown pragmas in
>>           system headers will be warned about.  */
>> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
>> index b7a4a1a68e3..6841a5a5e81 100644
>> --- a/gcc/c-family/c.opt
>> +++ b/gcc/c-family/c.opt
>> @@ -1180,6 +1180,10 @@ Wpragmas
>>   C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
>>   Warn about misuses of pragmas.
>>
>> +Wpragma-once-outside-header
>> +C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) Init(1) Warning
>> +Warn about #pragma once outside of a header.
>> +
>>   Wprio-ctor-dtor
>>   C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
>>   Warn if constructor or destructors with priorities from 0 to 100 are used.
>> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
>> index bdf05be387d..eeb8954bcdf 100644
>> --- a/gcc/doc/invoke.texi
>> +++ b/gcc/doc/invoke.texi
>> @@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
>>   -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
>>   -Wparentheses  -Wno-pedantic-ms-format
>>   -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
>> --Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
>> --Wrestrict  -Wno-return-local-addr  -Wreturn-type
>> +-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
>> +-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
>>   -Wno-scalar-storage-order  -Wsequence-point
>>   -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
>>   -Wno-shadow-ivar
>> @@ -7955,6 +7955,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
>>   invalid syntax, or conflicts between pragmas.  See also
>>   @option{-Wunknown-pragmas}.
>>
>> +@opindex Wno-pragma-once-outside-header
>> +@opindex Wpragma-once-outside-header
>> +@item -Wno-pragma-once-outside-header
>> +Do not warn when @code{#pragma once} is used in a file that is not a header
>> +file, such as a main file.
>> +
>>   @opindex Wno-prio-ctor-dtor
>>   @opindex Wprio-ctor-dtor
>>   @item -Wno-prio-ctor-dtor
>> diff --git a/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
>> new file mode 100644
>> index 00000000000..678bd4e7626
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
>> @@ -0,0 +1,5 @@
>> +/* { dg-do assemble  } */
>> +/* { dg-options "-Werror=pragma-once-outside-header" } */
>> +
>> +#pragma once  // { dg-error "#pragma once in main file" }
>> +int main() {}
>> diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
>> new file mode 100644
>> index 00000000000..b5be4d25a9d
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
>> @@ -0,0 +1,5 @@
>> +// { dg-do assemble  }
>> +// { dg-options "-Wno-pragma-once-outside-header" }
>> +
>> +#pragma once
>> +int main() {}
>> diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
>> new file mode 100644
>> index 00000000000..ae958d3beb8
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
>> @@ -0,0 +1,5 @@
>> +// { dg-do assemble  }
>> +// { dg-options "-Werror=pragma-once-outside-header" }
>> +
>> +#pragma once  // { dg-error "#pragma once in main file" }
>> +int main() {}
>> diff --git a/libcpp/directives.cc b/libcpp/directives.cc
>> index 479f8c716e8..b6121a459f8 100644
>> --- a/libcpp/directives.cc
>> +++ b/libcpp/directives.cc
>> @@ -1588,8 +1588,12 @@ do_pragma (cpp_reader *pfile)
>>   static void
>>   do_pragma_once (cpp_reader *pfile)
>>   {
>> -  if (_cpp_in_main_source_file (pfile))
>> -    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
>> +  const unsigned char warn_level =
>> +    CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header);
>> +
>> +  if (warn_level && _cpp_in_main_source_file (pfile))
>> +    cpp_error (pfile, (warn_level == 1 ? CPP_DL_WARNING : CPP_DL_ERROR),
>> +              "#pragma once in main file");

...it would seem better to use cpp_warning and add a cpp_warning_reason 
for this diagnostic, so the normal -Werror handling (including #pragma 
GCC diagnostic) takes care of it?

Jason


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-06-04 14:54     ` Jason Merrill
@ 2024-06-13 14:31       ` Ken Matsui
  0 siblings, 0 replies; 12+ messages in thread
From: Ken Matsui @ 2024-06-13 14:31 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

On Tue, Jun 4, 2024 at 7:54 AM Jason Merrill <jason@redhat.com> wrote:
>
> On 3/14/24 04:01, Ken Matsui wrote:
> > On Sat, Mar 2, 2024 at 5:04 AM Ken Matsui <kmatsui@gcc.gnu.org> wrote:
> >>
> >> This patch adds a warning switch for "#pragma once in main file".  The
> >> warning option name is Wpragma-once-outside-header, which is the same
> >> as Clang.
> >
> > Ping.
> >
> >>
> >>          PR preprocessor/89808
> >>
> >> gcc/c-family/ChangeLog:
> >>
> >>          * c-opts.cc (c_common_handle_option): Handle
> >>          OPT_Wpragma_once_outside_header.
> >>          * c.opt (Wpragma_once_outside_header): Define new option.
> >>
> >> gcc/ChangeLog:
> >>
> >>          * doc/invoke.texi (Warning Options): Document
> >>          -Wno-pragma-once-outside-header.
> >>
> >> libcpp/ChangeLog:
> >>
> >>          * include/cpplib.h (struct cpp_options): Define
> >>          cpp_warn_pragma_once_outside_header.
> >>          * directives.cc (do_pragma_once): Use
> >>          cpp_warn_pragma_once_outside_header.
> >>          * init.cc (cpp_create_reader): Handle
> >>          cpp_warn_pragma_once_outside_header.
> >>
> >> gcc/testsuite/ChangeLog:
> >>
> >>          * g++.dg/Wpragma-once-outside-header.C: New test.
>
> Please drop this file, keeping the duplicate in the warn subdirectory.
>
> >>          * g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
> >>          * g++.dg/warn/Wpragma-once-outside-header.C: New test.
> >>
> >> Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> >> ---
> >>   gcc/c-family/c-opts.cc                                 |  9 +++++++++
> >>   gcc/c-family/c.opt                                     |  4 ++++
> >>   gcc/doc/invoke.texi                                    | 10 ++++++++--
> >>   gcc/testsuite/g++.dg/Wpragma-once-outside-header.C     |  5 +++++
> >>   .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
> >>   .../g++.dg/warn/Wpragma-once-outside-header.C          |  5 +++++
> >>   libcpp/directives.cc                                   |  8 ++++++--
> >>   libcpp/include/cpplib.h                                |  4 ++++
> >>   libcpp/init.cc                                         |  1 +
> >>   9 files changed, 47 insertions(+), 4 deletions(-)
> >>   create mode 100644 gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
> >>   create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> >>   create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> >>
> >> diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
> >> index be3058dca63..4edd8c6c515 100644
> >> --- a/gcc/c-family/c-opts.cc
> >> +++ b/gcc/c-family/c-opts.cc
> >> @@ -430,6 +430,15 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
> >>         cpp_opts->warn_num_sign_change = value;
> >>         break;
> >>
> >> +    case OPT_Wpragma_once_outside_header:
> >> +      if (value == 0)
> >> +       cpp_opts->cpp_warn_pragma_once_outside_header = 0;
> >> +      else if (kind == DK_ERROR)
> >> +       cpp_opts->cpp_warn_pragma_once_outside_header = 2;
> >> +      else
> >> +       cpp_opts->cpp_warn_pragma_once_outside_header = 1;
> >> +      break;
>
> Rather than encode the -Werror this way...
>
> >>       case OPT_Wunknown_pragmas:
> >>         /* Set to greater than 1, so that even unknown pragmas in
> >>           system headers will be warned about.  */
> >> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> >> index b7a4a1a68e3..6841a5a5e81 100644
> >> --- a/gcc/c-family/c.opt
> >> +++ b/gcc/c-family/c.opt
> >> @@ -1180,6 +1180,10 @@ Wpragmas
> >>   C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
> >>   Warn about misuses of pragmas.
> >>
> >> +Wpragma-once-outside-header
> >> +C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) Init(1) Warning
> >> +Warn about #pragma once outside of a header.
> >> +
> >>   Wprio-ctor-dtor
> >>   C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
> >>   Warn if constructor or destructors with priorities from 0 to 100 are used.
> >> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> >> index bdf05be387d..eeb8954bcdf 100644
> >> --- a/gcc/doc/invoke.texi
> >> +++ b/gcc/doc/invoke.texi
> >> @@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
> >>   -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
> >>   -Wparentheses  -Wno-pedantic-ms-format
> >>   -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
> >> --Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
> >> --Wrestrict  -Wno-return-local-addr  -Wreturn-type
> >> +-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
> >> +-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
> >>   -Wno-scalar-storage-order  -Wsequence-point
> >>   -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
> >>   -Wno-shadow-ivar
> >> @@ -7955,6 +7955,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
> >>   invalid syntax, or conflicts between pragmas.  See also
> >>   @option{-Wunknown-pragmas}.
> >>
> >> +@opindex Wno-pragma-once-outside-header
> >> +@opindex Wpragma-once-outside-header
> >> +@item -Wno-pragma-once-outside-header
> >> +Do not warn when @code{#pragma once} is used in a file that is not a header
> >> +file, such as a main file.
> >> +
> >>   @opindex Wno-prio-ctor-dtor
> >>   @opindex Wprio-ctor-dtor
> >>   @item -Wno-prio-ctor-dtor
> >> diff --git a/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
> >> new file mode 100644
> >> index 00000000000..678bd4e7626
> >> --- /dev/null
> >> +++ b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C
> >> @@ -0,0 +1,5 @@
> >> +/* { dg-do assemble  } */
> >> +/* { dg-options "-Werror=pragma-once-outside-header" } */
> >> +
> >> +#pragma once  // { dg-error "#pragma once in main file" }
> >> +int main() {}
> >> diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> >> new file mode 100644
> >> index 00000000000..b5be4d25a9d
> >> --- /dev/null
> >> +++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> >> @@ -0,0 +1,5 @@
> >> +// { dg-do assemble  }
> >> +// { dg-options "-Wno-pragma-once-outside-header" }
> >> +
> >> +#pragma once
> >> +int main() {}
> >> diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> >> new file mode 100644
> >> index 00000000000..ae958d3beb8
> >> --- /dev/null
> >> +++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> >> @@ -0,0 +1,5 @@
> >> +// { dg-do assemble  }
> >> +// { dg-options "-Werror=pragma-once-outside-header" }
> >> +
> >> +#pragma once  // { dg-error "#pragma once in main file" }
> >> +int main() {}
> >> diff --git a/libcpp/directives.cc b/libcpp/directives.cc
> >> index 479f8c716e8..b6121a459f8 100644
> >> --- a/libcpp/directives.cc
> >> +++ b/libcpp/directives.cc
> >> @@ -1588,8 +1588,12 @@ do_pragma (cpp_reader *pfile)
> >>   static void
> >>   do_pragma_once (cpp_reader *pfile)
> >>   {
> >> -  if (_cpp_in_main_source_file (pfile))
> >> -    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
> >> +  const unsigned char warn_level =
> >> +    CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header);
> >> +
> >> +  if (warn_level && _cpp_in_main_source_file (pfile))
> >> +    cpp_error (pfile, (warn_level == 1 ? CPP_DL_WARNING : CPP_DL_ERROR),
> >> +              "#pragma once in main file");
>
> ...it would seem better to use cpp_warning and add a cpp_warning_reason
> for this diagnostic, so the normal -Werror handling (including #pragma
> GCC diagnostic) takes care of it?

Thank you for your review!  I'll soon update this patch.

>
> Jason
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-03-02 13:04 ` [PATCH v2] " Ken Matsui
  2024-03-14  8:01   ` Ken Matsui
@ 2024-06-13 14:31   ` Ken Matsui
  2024-06-13 14:42     ` David Malcolm
                       ` (2 more replies)
  1 sibling, 3 replies; 12+ messages in thread
From: Ken Matsui @ 2024-06-13 14:31 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Ken Matsui

This patch adds a warning switch for "#pragma once in main file".  The
warning option name is Wpragma-once-outside-header, which is the same
as Clang.

	PR preprocessor/89808

gcc/c-family/ChangeLog:

	* c.opt (Wpragma_once_outside_header): Define new option.

gcc/ChangeLog:

	* doc/invoke.texi (Warning Options): Document
	-Wno-pragma-once-outside-header.

libcpp/ChangeLog:

	* include/cpplib.h (struct cpp_options): Define
	cpp_warn_pragma_once_outside_header.
	(cpp_warning_reason): Define CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
	* directives.cc (do_pragma_once): Use
	cpp_warn_pragma_once_outside_header and
	CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
	* init.cc (cpp_create_reader): Handle
	cpp_warn_pragma_once_outside_header.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
	* g++.dg/warn/Wpragma-once-outside-header.C: New test.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 gcc/c-family/c.opt                                     |  4 ++++
 gcc/doc/invoke.texi                                    | 10 ++++++++--
 .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
 .../g++.dg/warn/Wpragma-once-outside-header.C          |  6 ++++++
 libcpp/directives.cc                                   |  9 ++++++---
 libcpp/include/cpplib.h                                |  7 ++++++-
 libcpp/init.cc                                         |  1 +
 7 files changed, 36 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
 create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 403abc1f26e..3439f36fe45 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1188,6 +1188,10 @@ Wpragmas
 C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
 Warn about misuses of pragmas.
 
+Wpragma-once-outside-header
+C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) CppReason(CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER) Init(1) Warning
+Warn about #pragma once outside of a header.
+
 Wprio-ctor-dtor
 C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
 Warn if constructor or destructors with priorities from 0 to 100 are used.
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 9456ced468a..c7f17ca9eb7 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
 -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
 -Wparentheses  -Wno-pedantic-ms-format
 -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
--Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
--Wrestrict  -Wno-return-local-addr  -Wreturn-type
+-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
+-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
 -Wno-scalar-storage-order  -Wsequence-point
 -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
 -Wno-shadow-ivar
@@ -7983,6 +7983,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
 invalid syntax, or conflicts between pragmas.  See also
 @option{-Wunknown-pragmas}.
 
+@opindex Wno-pragma-once-outside-header
+@opindex Wpragma-once-outside-header
+@item -Wno-pragma-once-outside-header
+Do not warn when @code{#pragma once} is used in a file that is not a header
+file, such as a main file.
+
 @opindex Wno-prio-ctor-dtor
 @opindex Wprio-ctor-dtor
 @item -Wno-prio-ctor-dtor
diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
new file mode 100644
index 00000000000..b5be4d25a9d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
@@ -0,0 +1,5 @@
+// { dg-do assemble  }
+// { dg-options "-Wno-pragma-once-outside-header" }
+
+#pragma once
+int main() {}
diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
new file mode 100644
index 00000000000..324b0638c3f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
@@ -0,0 +1,6 @@
+// { dg-do assemble  }
+// { dg-options "-Werror=pragma-once-outside-header" }
+// { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 }
+
+#pragma once  // { dg-error "#pragma once in main file" }
+int main() {}
diff --git a/libcpp/directives.cc b/libcpp/directives.cc
index 479f8c716e8..68f47104dea 100644
--- a/libcpp/directives.cc
+++ b/libcpp/directives.cc
@@ -1588,8 +1588,12 @@ do_pragma (cpp_reader *pfile)
 static void
 do_pragma_once (cpp_reader *pfile)
 {
-  if (_cpp_in_main_source_file (pfile))
-    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
+  const unsigned char warn_level =
+    CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header);
+
+  if (warn_level && _cpp_in_main_source_file (pfile))
+    cpp_warning (pfile, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
+		 "#pragma once in main file");
 
   check_eol (pfile, false);
   _cpp_mark_file_once_only (pfile, pfile->buffer->file);
@@ -2822,4 +2826,3 @@ _cpp_bracket_include(cpp_reader *pfile)
 {
   return glue_header_name (pfile);
 }
-
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index c62374d3192..21deaf72ec5 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -583,6 +583,10 @@ struct cpp_options
      2 if it should be a pedwarn.  */
   unsigned char cpp_warn_invalid_utf8;
 
+  /* True if libcpp should warn about #pragma once outside of a header.
+     2 if it should be an error, i.e., -Werror.  */
+  unsigned char cpp_warn_pragma_once_outside_header;
+
   /* True if libcpp should warn about invalid forms of delimited or named
      escape sequences.  */
   bool cpp_warn_unicode;
@@ -701,7 +705,8 @@ enum cpp_warning_reason {
   CPP_W_EXPANSION_TO_DEFINED,
   CPP_W_BIDIRECTIONAL,
   CPP_W_INVALID_UTF8,
-  CPP_W_UNICODE
+  CPP_W_UNICODE,
+  CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER
 };
 
 /* Callback for header lookup for HEADER, which is the name of a
diff --git a/libcpp/init.cc b/libcpp/init.cc
index c457fa659e7..ba6e5f08446 100644
--- a/libcpp/init.cc
+++ b/libcpp/init.cc
@@ -235,6 +235,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
   CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
   CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0;
   CPP_OPTION (pfile, cpp_warn_unicode) = 1;
+  CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header) = 1;
   CPP_OPTION (pfile, cpp_input_charset_explicit) = 0;
 
   /* Default CPP arithmetic to something sensible for the host for the
-- 
2.45.1


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v3] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-06-13 14:31   ` [PATCH v3] " Ken Matsui
@ 2024-06-13 14:42     ` David Malcolm
  2024-06-13 14:44     ` Jason Merrill
  2024-06-16  0:59     ` [PATCH v4] " Ken Matsui
  2 siblings, 0 replies; 12+ messages in thread
From: David Malcolm @ 2024-06-13 14:42 UTC (permalink / raw)
  To: Ken Matsui, gcc-patches; +Cc: jason

On Thu, 2024-06-13 at 07:31 -0700, Ken Matsui wrote:
> This patch adds a warning switch for "#pragma once in main file". 
> The
> warning option name is Wpragma-once-outside-header, which is the same
> as Clang.
> 
>         PR preprocessor/89808
> 
> gcc/c-family/ChangeLog:
> 
>         * c.opt (Wpragma_once_outside_header): Define new option.
> 
> gcc/ChangeLog:
> 
>         * doc/invoke.texi (Warning Options): Document
>         -Wno-pragma-once-outside-header.
> 
> libcpp/ChangeLog:
> 
>         * include/cpplib.h (struct cpp_options): Define
>         cpp_warn_pragma_once_outside_header.
>         (cpp_warning_reason): Define
> CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
>         * directives.cc (do_pragma_once): Use
>         cpp_warn_pragma_once_outside_header and
>         CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
>         * init.cc (cpp_create_reader): Handle
>         cpp_warn_pragma_once_outside_header.
> 
> gcc/testsuite/ChangeLog:
> 
>         * g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
>         * g++.dg/warn/Wpragma-once-outside-header.C: New test.
> 
> Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>

[...snip...]

Thanks for the updated patch.

> @@ -7983,6 +7983,12 @@ Do not warn about misuses of pragmas, such as
> incorrect parameters,
>  invalid syntax, or conflicts between pragmas.  See also
>  @option{-Wunknown-pragmas}.
>  
> +@opindex Wno-pragma-once-outside-header
> +@opindex Wpragma-once-outside-header
> +@item -Wno-pragma-once-outside-header
> +Do not warn when @code{#pragma once} is used in a file that is not a
> header
> +file, such as a main file.
> +
>  @opindex Wno-prio-ctor-dtor
>  @opindex Wprio-ctor-dtor
>  @item -Wno-prio-ctor-dtor

Please run "make html && make regenerate-opt-urls" so that the
diagnostic gets a documentation URL.  Sorry that you have to do this
manually (it's to avoid complicating the build dependencies for someone
just building gcc).

[...snip...]


> diff --git a/libcpp/directives.cc b/libcpp/directives.cc
> index 479f8c716e8..68f47104dea 100644
> --- a/libcpp/directives.cc
> +++ b/libcpp/directives.cc
> @@ -1588,8 +1588,12 @@ do_pragma (cpp_reader *pfile)
>  static void
>  do_pragma_once (cpp_reader *pfile)
>  {
> -  if (_cpp_in_main_source_file (pfile))
> -    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
> +  const unsigned char warn_level =
> +    CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header);
> +
> +  if (warn_level && _cpp_in_main_source_file (pfile))
> +    cpp_warning (pfile, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
> +                "#pragma once in main file");

Please put the "#pragma once" in the message in quotes, such as via:

    cpp_warning (pfile, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
                "%<pragma once%> in main file");

or via:

    cpp_warning (pfile, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
                "%qs in main file", "pragma once");

Although it's a minor style nit, I'm working on patches to
automatically add URLs to GCC's documentation for certain quoted
strings on sufficiently capable terminals (I've done command-line
options, I'm working on attributes, and I hope to eventually do
pragmas).

Dave


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v3] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-06-13 14:31   ` [PATCH v3] " Ken Matsui
  2024-06-13 14:42     ` David Malcolm
@ 2024-06-13 14:44     ` Jason Merrill
  2024-06-16  0:52       ` Ken Matsui
  2024-06-16  0:59     ` [PATCH v4] " Ken Matsui
  2 siblings, 1 reply; 12+ messages in thread
From: Jason Merrill @ 2024-06-13 14:44 UTC (permalink / raw)
  To: Ken Matsui, gcc-patches

On 6/13/24 10:31, Ken Matsui wrote:
> This patch adds a warning switch for "#pragma once in main file".  The
> warning option name is Wpragma-once-outside-header, which is the same
> as Clang.
> 
> 	PR preprocessor/89808
> 
> gcc/c-family/ChangeLog:
> 
> 	* c.opt (Wpragma_once_outside_header): Define new option.
> 
> gcc/ChangeLog:
> 
> 	* doc/invoke.texi (Warning Options): Document
> 	-Wno-pragma-once-outside-header.
> 
> libcpp/ChangeLog:
> 
> 	* include/cpplib.h (struct cpp_options): Define
> 	cpp_warn_pragma_once_outside_header.

This bit-field should be unneeded now, along with the uses of it.

> 	(cpp_warning_reason): Define CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
> 	* directives.cc (do_pragma_once): Use
> 	cpp_warn_pragma_once_outside_header and
> 	CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
> 	* init.cc (cpp_create_reader): Handle
> 	cpp_warn_pragma_once_outside_header.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
> 	* g++.dg/warn/Wpragma-once-outside-header.C: New test.
> 
> Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> ---
>   gcc/c-family/c.opt                                     |  4 ++++
>   gcc/doc/invoke.texi                                    | 10 ++++++++--
>   .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
>   .../g++.dg/warn/Wpragma-once-outside-header.C          |  6 ++++++
>   libcpp/directives.cc                                   |  9 ++++++---
>   libcpp/include/cpplib.h                                |  7 ++++++-
>   libcpp/init.cc                                         |  1 +
>   7 files changed, 36 insertions(+), 6 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> 
> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> index 403abc1f26e..3439f36fe45 100644
> --- a/gcc/c-family/c.opt
> +++ b/gcc/c-family/c.opt
> @@ -1188,6 +1188,10 @@ Wpragmas
>   C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
>   Warn about misuses of pragmas.
>   
> +Wpragma-once-outside-header
> +C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) CppReason(CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER) Init(1) Warning
> +Warn about #pragma once outside of a header.
> +
>   Wprio-ctor-dtor
>   C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
>   Warn if constructor or destructors with priorities from 0 to 100 are used.
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index 9456ced468a..c7f17ca9eb7 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
>   -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
>   -Wparentheses  -Wno-pedantic-ms-format
>   -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
> --Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
> --Wrestrict  -Wno-return-local-addr  -Wreturn-type
> +-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
> +-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
>   -Wno-scalar-storage-order  -Wsequence-point
>   -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
>   -Wno-shadow-ivar
> @@ -7983,6 +7983,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
>   invalid syntax, or conflicts between pragmas.  See also
>   @option{-Wunknown-pragmas}.
>   
> +@opindex Wno-pragma-once-outside-header
> +@opindex Wpragma-once-outside-header
> +@item -Wno-pragma-once-outside-header
> +Do not warn when @code{#pragma once} is used in a file that is not a header
> +file, such as a main file.
> +
>   @opindex Wno-prio-ctor-dtor
>   @opindex Wprio-ctor-dtor
>   @item -Wno-prio-ctor-dtor
> diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> new file mode 100644
> index 00000000000..b5be4d25a9d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> @@ -0,0 +1,5 @@
> +// { dg-do assemble  }
> +// { dg-options "-Wno-pragma-once-outside-header" }
> +
> +#pragma once
> +int main() {}
> diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> new file mode 100644
> index 00000000000..324b0638c3f
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> @@ -0,0 +1,6 @@
> +// { dg-do assemble  }
> +// { dg-options "-Werror=pragma-once-outside-header" }
> +// { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 }
> +
> +#pragma once  // { dg-error "#pragma once in main file" }
> +int main() {}
> diff --git a/libcpp/directives.cc b/libcpp/directives.cc
> index 479f8c716e8..68f47104dea 100644
> --- a/libcpp/directives.cc
> +++ b/libcpp/directives.cc
> @@ -1588,8 +1588,12 @@ do_pragma (cpp_reader *pfile)
>   static void
>   do_pragma_once (cpp_reader *pfile)
>   {
> -  if (_cpp_in_main_source_file (pfile))
> -    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
> +  const unsigned char warn_level =
> +    CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header);
> +
> +  if (warn_level && _cpp_in_main_source_file (pfile))
> +    cpp_warning (pfile, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
> +		 "#pragma once in main file");
>   
>     check_eol (pfile, false);
>     _cpp_mark_file_once_only (pfile, pfile->buffer->file);
> @@ -2822,4 +2826,3 @@ _cpp_bracket_include(cpp_reader *pfile)
>   {
>     return glue_header_name (pfile);
>   }
> -
> diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
> index c62374d3192..21deaf72ec5 100644
> --- a/libcpp/include/cpplib.h
> +++ b/libcpp/include/cpplib.h
> @@ -583,6 +583,10 @@ struct cpp_options
>        2 if it should be a pedwarn.  */
>     unsigned char cpp_warn_invalid_utf8;
>   
> +  /* True if libcpp should warn about #pragma once outside of a header.
> +     2 if it should be an error, i.e., -Werror.  */
> +  unsigned char cpp_warn_pragma_once_outside_header;
> +
>     /* True if libcpp should warn about invalid forms of delimited or named
>        escape sequences.  */
>     bool cpp_warn_unicode;
> @@ -701,7 +705,8 @@ enum cpp_warning_reason {
>     CPP_W_EXPANSION_TO_DEFINED,
>     CPP_W_BIDIRECTIONAL,
>     CPP_W_INVALID_UTF8,
> -  CPP_W_UNICODE
> +  CPP_W_UNICODE,
> +  CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER
>   };
>   
>   /* Callback for header lookup for HEADER, which is the name of a
> diff --git a/libcpp/init.cc b/libcpp/init.cc
> index c457fa659e7..ba6e5f08446 100644
> --- a/libcpp/init.cc
> +++ b/libcpp/init.cc
> @@ -235,6 +235,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
>     CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
>     CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0;
>     CPP_OPTION (pfile, cpp_warn_unicode) = 1;
> +  CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header) = 1;
>     CPP_OPTION (pfile, cpp_input_charset_explicit) = 0;
>   
>     /* Default CPP arithmetic to something sensible for the host for the


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v3] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-06-13 14:44     ` Jason Merrill
@ 2024-06-16  0:52       ` Ken Matsui
  0 siblings, 0 replies; 12+ messages in thread
From: Ken Matsui @ 2024-06-16  0:52 UTC (permalink / raw)
  To: Jason Merrill; +Cc: Ken Matsui, gcc-patches

On Thu, Jun 13, 2024 at 7:44 AM Jason Merrill <jason@redhat.com> wrote:
>
> On 6/13/24 10:31, Ken Matsui wrote:
> > This patch adds a warning switch for "#pragma once in main file".  The
> > warning option name is Wpragma-once-outside-header, which is the same
> > as Clang.
> >
> >       PR preprocessor/89808
> >
> > gcc/c-family/ChangeLog:
> >
> >       * c.opt (Wpragma_once_outside_header): Define new option.
> >
> > gcc/ChangeLog:
> >
> >       * doc/invoke.texi (Warning Options): Document
> >       -Wno-pragma-once-outside-header.
> >
> > libcpp/ChangeLog:
> >
> >       * include/cpplib.h (struct cpp_options): Define
> >       cpp_warn_pragma_once_outside_header.
>
> This bit-field should be unneeded now, along with the uses of it.

Thank you!  I'll update the patch shortly.

>
> >       (cpp_warning_reason): Define CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
> >       * directives.cc (do_pragma_once): Use
> >       cpp_warn_pragma_once_outside_header and
> >       CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
> >       * init.cc (cpp_create_reader): Handle
> >       cpp_warn_pragma_once_outside_header.
> >
> > gcc/testsuite/ChangeLog:
> >
> >       * g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
> >       * g++.dg/warn/Wpragma-once-outside-header.C: New test.
> >
> > Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> > ---
> >   gcc/c-family/c.opt                                     |  4 ++++
> >   gcc/doc/invoke.texi                                    | 10 ++++++++--
> >   .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
> >   .../g++.dg/warn/Wpragma-once-outside-header.C          |  6 ++++++
> >   libcpp/directives.cc                                   |  9 ++++++---
> >   libcpp/include/cpplib.h                                |  7 ++++++-
> >   libcpp/init.cc                                         |  1 +
> >   7 files changed, 36 insertions(+), 6 deletions(-)
> >   create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> >   create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> >
> > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> > index 403abc1f26e..3439f36fe45 100644
> > --- a/gcc/c-family/c.opt
> > +++ b/gcc/c-family/c.opt
> > @@ -1188,6 +1188,10 @@ Wpragmas
> >   C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
> >   Warn about misuses of pragmas.
> >
> > +Wpragma-once-outside-header
> > +C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) CppReason(CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER) Init(1) Warning
> > +Warn about #pragma once outside of a header.
> > +
> >   Wprio-ctor-dtor
> >   C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
> >   Warn if constructor or destructors with priorities from 0 to 100 are used.
> > diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> > index 9456ced468a..c7f17ca9eb7 100644
> > --- a/gcc/doc/invoke.texi
> > +++ b/gcc/doc/invoke.texi
> > @@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
> >   -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
> >   -Wparentheses  -Wno-pedantic-ms-format
> >   -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
> > --Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
> > --Wrestrict  -Wno-return-local-addr  -Wreturn-type
> > +-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
> > +-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
> >   -Wno-scalar-storage-order  -Wsequence-point
> >   -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
> >   -Wno-shadow-ivar
> > @@ -7983,6 +7983,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
> >   invalid syntax, or conflicts between pragmas.  See also
> >   @option{-Wunknown-pragmas}.
> >
> > +@opindex Wno-pragma-once-outside-header
> > +@opindex Wpragma-once-outside-header
> > +@item -Wno-pragma-once-outside-header
> > +Do not warn when @code{#pragma once} is used in a file that is not a header
> > +file, such as a main file.
> > +
> >   @opindex Wno-prio-ctor-dtor
> >   @opindex Wprio-ctor-dtor
> >   @item -Wno-prio-ctor-dtor
> > diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> > new file mode 100644
> > index 00000000000..b5be4d25a9d
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
> > @@ -0,0 +1,5 @@
> > +// { dg-do assemble  }
> > +// { dg-options "-Wno-pragma-once-outside-header" }
> > +
> > +#pragma once
> > +int main() {}
> > diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> > new file mode 100644
> > index 00000000000..324b0638c3f
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
> > @@ -0,0 +1,6 @@
> > +// { dg-do assemble  }
> > +// { dg-options "-Werror=pragma-once-outside-header" }
> > +// { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 }
> > +
> > +#pragma once  // { dg-error "#pragma once in main file" }
> > +int main() {}
> > diff --git a/libcpp/directives.cc b/libcpp/directives.cc
> > index 479f8c716e8..68f47104dea 100644
> > --- a/libcpp/directives.cc
> > +++ b/libcpp/directives.cc
> > @@ -1588,8 +1588,12 @@ do_pragma (cpp_reader *pfile)
> >   static void
> >   do_pragma_once (cpp_reader *pfile)
> >   {
> > -  if (_cpp_in_main_source_file (pfile))
> > -    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
> > +  const unsigned char warn_level =
> > +    CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header);
> > +
> > +  if (warn_level && _cpp_in_main_source_file (pfile))
> > +    cpp_warning (pfile, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
> > +              "#pragma once in main file");
> >
> >     check_eol (pfile, false);
> >     _cpp_mark_file_once_only (pfile, pfile->buffer->file);
> > @@ -2822,4 +2826,3 @@ _cpp_bracket_include(cpp_reader *pfile)
> >   {
> >     return glue_header_name (pfile);
> >   }
> > -
> > diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
> > index c62374d3192..21deaf72ec5 100644
> > --- a/libcpp/include/cpplib.h
> > +++ b/libcpp/include/cpplib.h
> > @@ -583,6 +583,10 @@ struct cpp_options
> >        2 if it should be a pedwarn.  */
> >     unsigned char cpp_warn_invalid_utf8;
> >
> > +  /* True if libcpp should warn about #pragma once outside of a header.
> > +     2 if it should be an error, i.e., -Werror.  */
> > +  unsigned char cpp_warn_pragma_once_outside_header;
> > +
> >     /* True if libcpp should warn about invalid forms of delimited or named
> >        escape sequences.  */
> >     bool cpp_warn_unicode;
> > @@ -701,7 +705,8 @@ enum cpp_warning_reason {
> >     CPP_W_EXPANSION_TO_DEFINED,
> >     CPP_W_BIDIRECTIONAL,
> >     CPP_W_INVALID_UTF8,
> > -  CPP_W_UNICODE
> > +  CPP_W_UNICODE,
> > +  CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER
> >   };
> >
> >   /* Callback for header lookup for HEADER, which is the name of a
> > diff --git a/libcpp/init.cc b/libcpp/init.cc
> > index c457fa659e7..ba6e5f08446 100644
> > --- a/libcpp/init.cc
> > +++ b/libcpp/init.cc
> > @@ -235,6 +235,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table,
> >     CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired;
> >     CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0;
> >     CPP_OPTION (pfile, cpp_warn_unicode) = 1;
> > +  CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header) = 1;
> >     CPP_OPTION (pfile, cpp_input_charset_explicit) = 0;
> >
> >     /* Default CPP arithmetic to something sensible for the host for the
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v4] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-06-13 14:31   ` [PATCH v3] " Ken Matsui
  2024-06-13 14:42     ` David Malcolm
  2024-06-13 14:44     ` Jason Merrill
@ 2024-06-16  0:59     ` Ken Matsui
  2024-06-16  5:30       ` [PATCH v5] " Ken Matsui
  2 siblings, 1 reply; 12+ messages in thread
From: Ken Matsui @ 2024-06-16  0:59 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Ken Matsui

This patch adds a warning switch for "#pragma once in main file".  The
warning option name is Wpragma-once-outside-header, which is the same
as Clang provides.

	PR preprocessor/89808

gcc/c-family/ChangeLog:

	* c.opt (Wpragma_once_outside_header): Define new option.
	* c.opt.urls: Regenerate.

gcc/ChangeLog:

	* doc/invoke.texi (Warning Options): Document
	-Wno-pragma-once-outside-header.

libcpp/ChangeLog:

	* include/cpplib.h (cpp_warning_reason): Define
	CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
	* directives.cc (do_pragma_once): Use
	CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
	* g++.dg/warn/Wpragma-once-outside-header.C: New test.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 gcc/c-family/c.opt                                     |  4 ++++
 gcc/c-family/c.opt.urls                                |  3 +++
 gcc/doc/invoke.texi                                    | 10 ++++++++--
 .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
 .../g++.dg/warn/Wpragma-once-outside-header.C          |  6 ++++++
 libcpp/directives.cc                                   |  3 ++-
 libcpp/include/cpplib.h                                |  3 ++-
 7 files changed, 30 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
 create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 403abc1f26e..3439f36fe45 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1188,6 +1188,10 @@ Wpragmas
 C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
 Warn about misuses of pragmas.
 
+Wpragma-once-outside-header
+C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) CppReason(CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER) Init(1) Warning
+Warn about #pragma once outside of a header.
+
 Wprio-ctor-dtor
 C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
 Warn if constructor or destructors with priorities from 0 to 100 are used.
diff --git a/gcc/c-family/c.opt.urls b/gcc/c-family/c.opt.urls
index dd455d7c0dc..778ca08be2e 100644
--- a/gcc/c-family/c.opt.urls
+++ b/gcc/c-family/c.opt.urls
@@ -672,6 +672,9 @@ UrlSuffix(gcc/Warning-Options.html#index-Wno-pointer-to-int-cast)
 Wpragmas
 UrlSuffix(gcc/Warning-Options.html#index-Wno-pragmas)
 
+Wpragma-once-outside-header
+UrlSuffix(gcc/Warning-Options.html#index-Wno-pragma-once-outside-header)
+
 Wprio-ctor-dtor
 UrlSuffix(gcc/Warning-Options.html#index-Wno-prio-ctor-dtor)
 
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 9456ced468a..c7f17ca9eb7 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
 -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
 -Wparentheses  -Wno-pedantic-ms-format
 -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
--Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
--Wrestrict  -Wno-return-local-addr  -Wreturn-type
+-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
+-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
 -Wno-scalar-storage-order  -Wsequence-point
 -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
 -Wno-shadow-ivar
@@ -7983,6 +7983,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
 invalid syntax, or conflicts between pragmas.  See also
 @option{-Wunknown-pragmas}.
 
+@opindex Wno-pragma-once-outside-header
+@opindex Wpragma-once-outside-header
+@item -Wno-pragma-once-outside-header
+Do not warn when @code{#pragma once} is used in a file that is not a header
+file, such as a main file.
+
 @opindex Wno-prio-ctor-dtor
 @opindex Wprio-ctor-dtor
 @item -Wno-prio-ctor-dtor
diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
new file mode 100644
index 00000000000..b5be4d25a9d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
@@ -0,0 +1,5 @@
+// { dg-do assemble  }
+// { dg-options "-Wno-pragma-once-outside-header" }
+
+#pragma once
+int main() {}
diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
new file mode 100644
index 00000000000..324b0638c3f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
@@ -0,0 +1,6 @@
+// { dg-do assemble  }
+// { dg-options "-Werror=pragma-once-outside-header" }
+// { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 }
+
+#pragma once  // { dg-error "#pragma once in main file" }
+int main() {}
diff --git a/libcpp/directives.cc b/libcpp/directives.cc
index 479f8c716e8..467efdf637d 100644
--- a/libcpp/directives.cc
+++ b/libcpp/directives.cc
@@ -1589,7 +1589,8 @@ static void
 do_pragma_once (cpp_reader *pfile)
 {
   if (_cpp_in_main_source_file (pfile))
-    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
+    cpp_warning (pfile, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
+		 "%<pragma once%> in main file");
 
   check_eol (pfile, false);
   _cpp_mark_file_once_only (pfile, pfile->buffer->file);
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index c62374d3192..da915e2101e 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -701,7 +701,8 @@ enum cpp_warning_reason {
   CPP_W_EXPANSION_TO_DEFINED,
   CPP_W_BIDIRECTIONAL,
   CPP_W_INVALID_UTF8,
-  CPP_W_UNICODE
+  CPP_W_UNICODE,
+  CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER
 };
 
 /* Callback for header lookup for HEADER, which is the name of a
-- 
2.45.1


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v5] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
  2024-06-16  0:59     ` [PATCH v4] " Ken Matsui
@ 2024-06-16  5:30       ` Ken Matsui
  0 siblings, 0 replies; 12+ messages in thread
From: Ken Matsui @ 2024-06-16  5:30 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Ken Matsui

This patch adds a warning switch for "#pragma once in main file".  The
warning option name is Wpragma-once-outside-header, which is the same
as Clang provides.

	PR preprocessor/89808

gcc/c-family/ChangeLog:

	* c.opt (Wpragma_once_outside_header): Define new option.
	* c.opt.urls: Regenerate.

gcc/ChangeLog:

	* doc/invoke.texi (Warning Options): Document
	-Wno-pragma-once-outside-header.

libcpp/ChangeLog:

	* include/cpplib.h (cpp_warning_reason): Define
	CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.
	* directives.cc (do_pragma_once): Use
	CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wno-pragma-once-outside-header.C: New test.
	* g++.dg/warn/Wpragma-once-outside-header.C: New test.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 gcc/c-family/c.opt                                     |  4 ++++
 gcc/c-family/c.opt.urls                                |  3 +++
 gcc/doc/invoke.texi                                    | 10 ++++++++--
 .../g++.dg/warn/Wno-pragma-once-outside-header.C       |  5 +++++
 .../g++.dg/warn/Wpragma-once-outside-header.C          |  6 ++++++
 libcpp/directives.cc                                   |  3 ++-
 libcpp/include/cpplib.h                                |  3 ++-
 7 files changed, 30 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
 create mode 100644 gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 403abc1f26e..3439f36fe45 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1188,6 +1188,10 @@ Wpragmas
 C ObjC C++ ObjC++ Var(warn_pragmas) Init(1) Warning
 Warn about misuses of pragmas.
 
+Wpragma-once-outside-header
+C ObjC C++ ObjC++ Var(warn_pragma_once_outside_header) CppReason(CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER) Init(1) Warning
+Warn about #pragma once outside of a header.
+
 Wprio-ctor-dtor
 C ObjC C++ ObjC++ Var(warn_prio_ctor_dtor) Init(1) Warning
 Warn if constructor or destructors with priorities from 0 to 100 are used.
diff --git a/gcc/c-family/c.opt.urls b/gcc/c-family/c.opt.urls
index dd455d7c0dc..778ca08be2e 100644
--- a/gcc/c-family/c.opt.urls
+++ b/gcc/c-family/c.opt.urls
@@ -672,6 +672,9 @@ UrlSuffix(gcc/Warning-Options.html#index-Wno-pointer-to-int-cast)
 Wpragmas
 UrlSuffix(gcc/Warning-Options.html#index-Wno-pragmas)
 
+Wpragma-once-outside-header
+UrlSuffix(gcc/Warning-Options.html#index-Wno-pragma-once-outside-header)
+
 Wprio-ctor-dtor
 UrlSuffix(gcc/Warning-Options.html#index-Wno-prio-ctor-dtor)
 
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 9456ced468a..c7f17ca9eb7 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -391,8 +391,8 @@ Objective-C and Objective-C++ Dialects}.
 -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded
 -Wparentheses  -Wno-pedantic-ms-format
 -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast
--Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls
--Wrestrict  -Wno-return-local-addr  -Wreturn-type
+-Wno-pragmas  -Wno-pragma-once-outside-header  -Wno-prio-ctor-dtor
+-Wredundant-decls  -Wrestrict  -Wno-return-local-addr  -Wreturn-type
 -Wno-scalar-storage-order  -Wsequence-point
 -Wshadow  -Wshadow=global  -Wshadow=local  -Wshadow=compatible-local
 -Wno-shadow-ivar
@@ -7983,6 +7983,12 @@ Do not warn about misuses of pragmas, such as incorrect parameters,
 invalid syntax, or conflicts between pragmas.  See also
 @option{-Wunknown-pragmas}.
 
+@opindex Wno-pragma-once-outside-header
+@opindex Wpragma-once-outside-header
+@item -Wno-pragma-once-outside-header
+Do not warn when @code{#pragma once} is used in a file that is not a header
+file, such as a main file.
+
 @opindex Wno-prio-ctor-dtor
 @opindex Wprio-ctor-dtor
 @item -Wno-prio-ctor-dtor
diff --git a/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
new file mode 100644
index 00000000000..b5be4d25a9d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wno-pragma-once-outside-header.C
@@ -0,0 +1,5 @@
+// { dg-do assemble  }
+// { dg-options "-Wno-pragma-once-outside-header" }
+
+#pragma once
+int main() {}
diff --git a/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
new file mode 100644
index 00000000000..29f09b69f71
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C
@@ -0,0 +1,6 @@
+// { dg-do assemble  }
+// { dg-options "-Werror=pragma-once-outside-header" }
+// { dg-message "some warnings being treated as errors" "" {target "*-*-*"} 0 }
+
+#pragma once  // { dg-error "'pragma once' in main file" }
+int main() {}
diff --git a/libcpp/directives.cc b/libcpp/directives.cc
index 479f8c716e8..467efdf637d 100644
--- a/libcpp/directives.cc
+++ b/libcpp/directives.cc
@@ -1589,7 +1589,8 @@ static void
 do_pragma_once (cpp_reader *pfile)
 {
   if (_cpp_in_main_source_file (pfile))
-    cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
+    cpp_warning (pfile, CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER,
+		 "%<pragma once%> in main file");
 
   check_eol (pfile, false);
   _cpp_mark_file_once_only (pfile, pfile->buffer->file);
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index c62374d3192..da915e2101e 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -701,7 +701,8 @@ enum cpp_warning_reason {
   CPP_W_EXPANSION_TO_DEFINED,
   CPP_W_BIDIRECTIONAL,
   CPP_W_INVALID_UTF8,
-  CPP_W_UNICODE
+  CPP_W_UNICODE,
+  CPP_W_PRAGMA_ONCE_OUTSIDE_HEADER
 };
 
 /* Callback for header lookup for HEADER, which is the name of a
-- 
2.45.1


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-06-16  5:30 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-02  6:45 [PATCH] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808] Ken Matsui
2024-03-02 13:04 ` [PATCH v2] " Ken Matsui
2024-03-14  8:01   ` Ken Matsui
2024-05-06 13:24     ` Ken Matsui
2024-06-04 14:54     ` Jason Merrill
2024-06-13 14:31       ` Ken Matsui
2024-06-13 14:31   ` [PATCH v3] " Ken Matsui
2024-06-13 14:42     ` David Malcolm
2024-06-13 14:44     ` Jason Merrill
2024-06-16  0:52       ` Ken Matsui
2024-06-16  0:59     ` [PATCH v4] " Ken Matsui
2024-06-16  5:30       ` [PATCH v5] " Ken Matsui

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).