Bug 9471 - #pragma system_header vs. precompiled headers
Summary: #pragma system_header vs. precompiled headers
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: pch (show other bugs)
Version: 3.4.0
: P3 normal
Target Milestone: 14.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2003-01-28 03:46 UTC by Benjamin Kosnik
Modified: 2024-02-11 10:30 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-11-02 01:54:42


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Benjamin Kosnik 2003-01-28 03:46:01 UTC
This is odd behavior, but just annoying.

If I create a pch this way:
%COMP.sh "-x c++-header -Winvalid-pch" ostream

I get:
ostream:43:13: warning: #pragma system_header ignored outside include file

However, if I create a pch by doing

cat > precomp_ostream.hh
#include <ostream>

I don't get the error. 

These cases, which are identical input, should give identical output.

Release:
gcc-3.4 20030125

Environment:
rh linux 7.3
Comment 1 Andrew Pinski 2003-05-26 00:07:29 UTC
Assigning to PCH expert:

still happens (20030525):
tin:~/src/gnu/gcctest>g++ -x c++-header -Winvalid-pch /home/gates/pinskia/linux/include/
c++/3.4/ostream
/home/gates/pinskia/linux/include/c++/3.4/ostream:43:13: warning: #pragma 
system_header ignored outside include file
Comment 2 Andrew Pinski 2003-08-04 04:11:11 UTC
Simple testcase:
testsys.h:
#pragma GCC system_header
g++ -ansi -Werror -pedantic -Winvalid-pch -o testsys.h.gch -c testsys.h
testsys.h:1:14: #pragma system_header ignored outside include file
Comment 3 Geoff Keating 2004-06-09 20:18:20 UTC
Although the simplified testcase still works, note that with an installed compiler saying

g++ -x c++-header ostream

will say 'file not found' unless there is actually an ostream in the current directory.
Comment 4 Tom Tromey 2006-12-29 04:30:21 UTC
I think this same problem will occur with #include_next
and #pragma once.  cpp doesn't currently seem to differentiate between
the PCH case and other kinds of preprocessing.
Comment 5 GCC Commits 2023-11-14 22:55:12 UTC
The master branch has been updated by Lewis Hyatt <lhyatt@gcc.gnu.org>:

https://gcc.gnu.org/g:9938645fcf914ec2a3fa8137cf1456ad4af5f77c

commit r14-5471-g9938645fcf914ec2a3fa8137cf1456ad4af5f77c
Author: Lewis Hyatt <lhyatt@gmail.com>
Date:   Fri Nov 10 11:10:18 2023 -0500

    c-family: Let libcpp know when the compilation is for a PCH [PR9471]
    
    libcpp will generate diagnostics when it encounters things in the main file
    that only belong in a header file, such as `#pragma once' or `#pragma GCC
    system_header'. But sometimes the main file is a header file that is just
    being compiled separately, e.g. to produce a C++ module or a PCH, in which
    case such diagnostics should be suppressed. libcpp already has an interface
    to request that, so make use of it in the C frontends to prevent libcpp from
    issuing unwanted diagnostics when compiling a PCH.
    
    gcc/c-family/ChangeLog:
    
            PR pch/9471
            PR pch/47857
            * c-opts.cc (c_common_post_options): Set cpp_opts->main_search
            so libcpp knows it is compiling a header file separately.
    
    gcc/testsuite/ChangeLog:
    
            PR pch/9471
            PR pch/47857
            * g++.dg/pch/main-file-warnings.C: New test.
            * g++.dg/pch/main-file-warnings.Hs: New test.
            * gcc.dg/pch/main-file-warnings.c: New test.
            * gcc.dg/pch/main-file-warnings.hs: New test.
Comment 6 Lewis Hyatt 2023-11-14 22:57:27 UTC
Fixed for GCC 14.