Re: [PATCH v3] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]

2024-06-15 Thread Ken Matsui
On Thu, Jun 13, 2024 at 7:44 AM Jason Merrill  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 
> > ---
> >   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 000..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 000..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 }
>

Re: [PATCH v3] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]

2024-06-13 Thread Jason Merrill

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

Re: [PATCH v3] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]

2024-06-13 Thread David Malcolm
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 

[...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,
"% 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



[PATCH v3] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]

2024-06-13 Thread 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 
---
 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 000..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 000..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_HEA