Bug 89808 - An option to disable warning "#pragma once in main file"
Summary: An option to disable warning "#pragma once in main file"
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: preprocessor (show other bugs)
Version: 8.3.1
: P3 enhancement
Target Milestone: 15.0
Assignee: Ken Matsui
URL:
Keywords: diagnostic, easyhack
: 113873 (view as bug list)
Depends on:
Blocks: 44209
  Show dependency treegraph
 
Reported: 2019-03-24 13:10 UTC by sduguay
Modified: 2024-10-09 06:40 UTC (History)
9 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-03-25 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description sduguay 2019-03-24 13:10:40 UTC
I am building an environnement to execute c++ code and I do need to use #pragma once in my main cpp source code (and all cpp files actually). When I compile, I get the "#pragma once in main file" warning.

The only way to turn this warning off is to disable all warnings with the -w option. I want to only disable this specific warning and keep all the other ones. gcc needs something like clang's -Wno-pragma-once-outside-header option.

I do appreciate that this warning is actually useful but there are corner cases where this warning must be disabled.
Comment 1 Richard Biener 2019-03-25 09:27:24 UTC
Testcase:

#pragma once
int main() {}

> g++ t.C -S
t.C:1:9: warning: #pragma once in main file
 #pragma once
         ^~~~


you could work around this by wrapping the pragma in a preprocessor
conditional
Comment 2 Jakub Jelinek 2019-03-25 09:50:41 UTC
You haven't explained why do you need to use #pragma once in the main cpp source file, are you sometimes including it as a header and other times compiling it as the TU itself?  Not to mention that it is best not to use #pragma once at all anywhere.
Comment 3 sduguay 2019-03-25 11:36:16 UTC
(In reply to Richard Biener from comment #1)
> Testcase:
> 
> #pragma once
> int main() {}
> 
> > g++ t.C -S
> t.C:1:9: warning: #pragma once in main file
>  #pragma once
>          ^~~~
> 
> 
> you could work around this by wrapping the pragma in a preprocessor
> conditional

Hi, thanks for adding a minimal test case. I tried:

#if 1
#pragma once
#endif

but I'm still getting the warning.
Comment 4 sduguay 2019-03-25 11:42:19 UTC
(In reply to Jakub Jelinek from comment #2)
> You haven't explained why do you need to use #pragma once in the main cpp
> source file, are you sometimes including it as a header and other times
> compiling it as the TU itself?  Not to mention that it is best not to use
> #pragma once at all anywhere.

Yes, in the system I'm working on, there is no "main file", all C++ (xcpp files actually) are small C++ programs that I link with a generated main file. xcpp programs can use other programs (xcpp files) or libs (xhpp files).

This is all working pretty well right now, except for that #pragma once warning. It's still under pretty heavy development.

You can try it @ https://github.com/binarez/xcpp


Thanks
Comment 5 Richard Biener 2019-03-25 12:37:32 UTC
(In reply to sduguay from comment #3)
> (In reply to Richard Biener from comment #1)
> > Testcase:
> > 
> > #pragma once
> > int main() {}
> > 
> > > g++ t.C -S
> > t.C:1:9: warning: #pragma once in main file
> >  #pragma once
> >          ^~~~
> > 
> > 
> > you could work around this by wrapping the pragma in a preprocessor
> > conditional
> 
> Hi, thanks for adding a minimal test case. I tried:
> 
> #if 1
> #pragma once
> #endif
> 
> but I'm still getting the warning.

I of course meant

#if !defined(THIS_IS_THE_MAIN_FILE)
#pragma once
#endif

and compile the main file with -DTHIS_IS_THE_MAIN_FILE
Comment 6 Jonathan Wakely 2019-03-25 13:35:51 UTC
In any case, I agree with confirming this as a bug: all warnings should be controllable by a -Wxxx option.

Adding such an option is quite easy, and a good first contribution to GCC. For an example of adding a new option see https://gcc.gnu.org/r192968
Comment 7 Eric Gallager 2019-03-25 18:24:19 UTC
(In reply to Jonathan Wakely from comment #6)
> In any case, I agree with confirming this as a bug: all warnings should be
> controllable by a -Wxxx option.

This is bug 44209

> 
> Adding such an option is quite easy, and a good first contribution to GCC.
> For an example of adding a new option see https://gcc.gnu.org/r192968
Comment 8 sduguay 2019-03-26 00:01:35 UTC
(In reply to Jonathan Wakely from comment #6)
> In any case, I agree with confirming this as a bug: all warnings should be
> controllable by a -Wxxx option.
> 
> Adding such an option is quite easy, and a good first contribution to GCC.
> For an example of adding a new option see https://gcc.gnu.org/r192968

I was going to propose looking into it. I'll try to find some time.
Comment 9 Eric Gallager 2019-06-26 04:53:37 UTC
(In reply to sduguay from comment #8)
> (In reply to Jonathan Wakely from comment #6)
> > In any case, I agree with confirming this as a bug: all warnings should be
> > controllable by a -Wxxx option.
> > 
> > Adding such an option is quite easy, and a good first contribution to GCC.
> > For an example of adding a new option see https://gcc.gnu.org/r192968
> 
> I was going to propose looking into it. I'll try to find some time.

Have you found some time yet?
Comment 10 Martin Renold 2019-10-05 16:32:45 UTC
Just adding a second use-case.

Emacs with cmake-ide (using flycheck) shows warnings that update as you type. They are generated by running the file through gcc, which could be a .hpp file. This usually works well enough for interactive use. But it leaves you with an irrelevant "#pragma once in main file" warning in the editor.

(Not sure if this whole setup is "legit", or if there are better alternatives. It involves guessing which compile flags of some .cpp file belong to the .hpp file being edited.)
Comment 11 Justin Bassett 2021-03-31 05:31:52 UTC
An additional use case: to test that header files can compile in isolation, one could compile the header file with -fsyntax-only ( g++ -xc++ -fsyntax-only myheader.hpp ). However, this emits the "#pragma once in main file" warning, which is undesirable, especially because it makes -Werror impossible. This means that a tool for checking header file isolation would have to emit a new file to check header isolation.
Comment 12 Alejandro Colomar 2021-06-14 19:05:34 UTC
Still buggy in gcc-11 (Debian 11.1.0-3) 11.1.0

I'll have a look at the code and see if I can fix it.
Comment 13 Diego SC 2023-08-24 06:10:59 UTC
Another use case.

We are using precompiled headers in our application, and that includes headers which have "#pragma once". When generating the precompiled header we get this warning:

  warning: #pragma once in main file

In that context the warning is actually invalid, so it would be really nice to be able to turn this warning off, without turning off all warnings.
Comment 14 Drea Pinski 2023-08-24 06:30:26 UTC
(In reply to Diego SC from comment #13)
> Another use case.
> 
> We are using precompiled headers in our application, and that includes
> headers which have "#pragma once". When generating the precompiled header we
> get this warning:
> 
>   warning: #pragma once in main file
> 
> In that context the warning is actually invalid, so it would be really nice
> to be able to turn this warning off, without turning off all warnings.

That is PR 47857 .
Comment 15 Drea Pinski 2024-02-11 10:28:37 UTC
*** Bug 113873 has been marked as a duplicate of this bug. ***
Comment 16 GCC Commits 2024-10-09 01:18:00 UTC
The master branch has been updated by Ken Matsui <kmatsui@gcc.gnu.org>:

https://gcc.gnu.org/g:821d56100e1110ab6a166f50819522254eb30923

commit r15-4191-g821d56100e1110ab6a166f50819522254eb30923
Author: Ken Matsui <kmatsui@gcc.gnu.org>
Date:   Fri Mar 1 22:10:55 2024 -0800

    gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808]
    
    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>
    Reviewed-by: Marek Polacek <polacek@redhat.com>
Comment 17 Ken Matsui 2024-10-09 01:26:21 UTC
Thank you for your report, sduguay.  This issue has been resolved in https://gcc.gnu.org/g:821d56100e1110ab6a166f50819522254eb30923.