diff mbox series

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

Message ID 20240616053035.3076026-1-kmatsui@gcc.gnu.org
State New
Headers show
Series [v5] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808] | expand

Commit Message

Ken Matsui June 16, 2024, 5:30 a.m. UTC
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

Comments

Ken Matsui June 27, 2024, 3 p.m. UTC | #1
Ping.


On Sat, Jun 15, 2024 at 10:30 PM 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 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
>
Ken Matsui Oct. 4, 2024, noon UTC | #2
Ping for -Wno-pragma-once-outside-header.

On Thursday, June 27th, 2024 at 11:00 AM, Ken Matsui <kmatsui@cs.washington.edu> wrote:

> 
> 
> Ping.
> 
> 
> On Sat, Jun 15, 2024 at 10:30 PM 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 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
Marek Polacek Oct. 7, 2024, 8:41 p.m. UTC | #3
On Sat, Jun 15, 2024 at 10:30:35PM -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 provides.

I think the patch is OK now, thanks.  Other diagnostics inlude the '#'
character but I know you just did what David suggested.
 
> 	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
> 

Marek
Ken Matsui Oct. 7, 2024, 11:16 p.m. UTC | #4
On Monday, October 7th, 2024 at 4:41 PM, Marek Polacek <polacek@redhat.com> wrote:

>
>
> On Sat, Jun 15, 2024 at 10:30:35PM -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 provides.
>
>
> I think the patch is OK now, thanks. Other diagnostics inlude the '#'
> character but I know you just did what David suggested.

Thank you for your review!  It might be better to keep consistency between other compilers, but do we proceed with the current change?

Just to confirm, since you are a C front end reviewer, am I now ok to push this patch?

>
> > 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
>
>
> Marek
Marek Polacek Oct. 8, 2024, 3:21 p.m. UTC | #5
On Mon, Oct 07, 2024 at 11:16:20PM +0000, Ken Matsui wrote:
> On Monday, October 7th, 2024 at 4:41 PM, Marek Polacek <polacek@redhat.com> wrote:
> 
> >
> >
> > On Sat, Jun 15, 2024 at 10:30:35PM -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 provides.
> >
> >
> > I think the patch is OK now, thanks. Other diagnostics inlude the '#'
> > character but I know you just did what David suggested.
> 
> Thank you for your review!  It might be better to keep consistency between other compilers, but do we proceed with the current change?

I personally would have included that '#' but others may not
really care either way.
 
> Just to confirm, since you are a C front end reviewer, am I now ok to push this patch?

Yes, thanks.

Marek
Ken Matsui Oct. 8, 2024, 11:43 p.m. UTC | #6
On Tuesday, October 8th, 2024 at 11:21 AM, Marek Polacek <polacek@redhat.com> wrote:

> 
> 
> On Mon, Oct 07, 2024 at 11:16:20PM +0000, Ken Matsui wrote:
> 
> > On Monday, October 7th, 2024 at 4:41 PM, Marek Polacek polacek@redhat.com wrote:
> > 
> > > On Sat, Jun 15, 2024 at 10:30:35PM -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 provides.
> > > 
> > > I think the patch is OK now, thanks. Other diagnostics inlude the '#'
> > > character but I know you just did what David suggested.
> > 
> > Thank you for your review! It might be better to keep consistency between other compilers, but do we proceed with the current change?
> 
> 
> I personally would have included that '#' but others may not
> really care either way.

I also personally prefer to have '#', so let me add it.  Thank you.

> 
> > Just to confirm, since you are a C front end reviewer, am I now ok to push this patch?
> 
> 
> Yes, thanks.
> 
> Marek
Andreas Schwab Oct. 9, 2024, 6:03 a.m. UTC | #7
../../libcpp/directives.cc: In function 'void do_pragma_once(cpp_reader*)':
../../libcpp/directives.cc:2078:20: error: unknown conversion type character '<' in format [-Werror=format=]
 2078 |                  "%<#pragma once%> in main file");
      |                    ^
../../libcpp/directives.cc:2078:34: error: unknown conversion type character '>' in format [-Werror=format=]
 2078 |                  "%<#pragma once%> in main file");
      |                                  ^
cc1plus: all warnings being treated as errors
make[3]: *** [Makefile:227: directives.o] Error 1
Jiang, Haochen Oct. 9, 2024, 8:27 a.m. UTC | #8
> From: Andreas Schwab <schwab@suse.de>
> Sent: Wednesday, October 9, 2024 2:04 PM
> 
> ../../libcpp/directives.cc: In function 'void do_pragma_once(cpp_reader*)':
> ../../libcpp/directives.cc:2078:20: error: unknown conversion type character
> '<' in format [-Werror=format=]
>  2078 |                  "%<#pragma once%> in main file");
>       |                    ^
> ../../libcpp/directives.cc:2078:34: error: unknown conversion type character
> '>' in format [-Werror=format=]
>  2078 |                  "%<#pragma once%> in main file");
>       |                                  ^
> cc1plus: all warnings being treated as errors
> make[3]: *** [Makefile:227: directives.o] Error 1
> 

Same bootstrap fail for me and my script on x86_64:

https://gcc.gnu.org/pipermail/gcc-regression/2024-October/080957.html

Thx,
Haochen

> --
> Andreas Schwab, SUSE Labs, schwab@suse.de GPG Key fingerprint = 0196
> BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7 "And now for something
> completely different."
Ken Matsui Oct. 9, 2024, 11:38 a.m. UTC | #9
On Wednesday, October 9th, 2024 at 4:27 AM, Jiang, Haochen <haochen.jiang@intel.com> wrote:

> 
> 
> > From: Andreas Schwab schwab@suse.de
> 
> > Sent: Wednesday, October 9, 2024 2:04 PM
> > 
> > ../../libcpp/directives.cc: In function 'void do_pragma_once(cpp_reader*)':
> > ../../libcpp/directives.cc:2078:20: error: unknown conversion type character
> > '<' in format [-Werror=format=]
> > 2078 | "%<#pragma once%> in main file");
> > | ^
> > ../../libcpp/directives.cc:2078:34: error: unknown conversion type character
> > '>' in format [-Werror=format=]
> > 2078 | "%<#pragma once%> in main file");
> > | ^
> > cc1plus: all warnings being treated as errors
> > make[3]: *** [Makefile:227: directives.o] Error 1
> 
> 
> Same bootstrap fail for me and my script on x86_64:
> 
> https://gcc.gnu.org/pipermail/gcc-regression/2024-October/080957.html
> 
> Thx,
> Haochen

Thank you for your report.  I addressed this issue in https://gcc.gnu.org/g:f709990333597b30dff54876bfdaada14e9cde30.

> 
> > --
> > Andreas Schwab, SUSE Labs, schwab@suse.de GPG Key fingerprint = 0196
> > BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something
> > completely different."
diff mbox series

Patch

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