From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00641c01.pphosted.com (mx0a-00641c01.pphosted.com [205.220.165.146]) by sourceware.org (Postfix) with ESMTPS id 8C0453858D26; Sun, 16 Jun 2024 05:30:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8C0453858D26 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=gcc.gnu.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8C0453858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.165.146 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718515845; cv=none; b=HzXVDbI5E1A0LV17zlXhFsYbLbjpj8lFWblGFCl6hRICoMtqvgoozvPO/yHwZ9FvZFa9vZiR0ONB5poKzi6Oa6ilnBoud+GVUZ0FpXcmGSHOeBSnT05FfWUOWe3GY9q4UC9RR4C5JsTHUjP0AZFjQvBr5mC95NHcnt5NQSF276g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718515845; c=relaxed/simple; bh=VqFrxCCYHoNd6zWaTH8guWhV0sGkw6IPSZQjQPZp/KI=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=knPVYdMWRYeMZj3gxaQDWSAjgK6Tg2Tx06qC9wQXyEOJXUwUkJxsZX+KAbQFOqs15ZUf56uiCxnGKbJwo2Rqw3ssEgRttUeuF2JERapyOhvWFrLUyThA8MU/MFB1P3Oyq6OceuVT8e0h8W1dFKCvIqymWQ0G9X/70XCeggc6+Kw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0247473.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45G5RSDr031313; Sun, 16 Jun 2024 05:30:42 GMT Received: from mxout24.cac.washington.edu (mxout24.cac.washington.edu [140.142.234.158]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3ysmf30sry-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 16 Jun 2024 05:30:42 +0000 (GMT) Received: from smtp.washington.edu (smtp.washington.edu [128.208.60.54]) by mxout24.cac.washington.edu (8.14.4+UW20.07/8.14.4+UW24.02) with ESMTP id 45G5Ua7G016076 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 15 Jun 2024 22:30:36 -0700 X-Auth-Received: from ken-20xw00qgus.dhcp4.washington.edu ([10.154.75.87]) (authenticated authid=kmatsui) by smtp.washington.edu (8.16.1+UW21.10/8.14.4+UW24.02) with ESMTPSA id 45G5UaJo024151 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 15 Jun 2024 22:30:36 -0700 X-UW-Orig-Sender: kmatsui@smtp.washington.edu From: Ken Matsui To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Ken Matsui Subject: [PATCH v5] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808] Date: Sat, 15 Jun 2024 22:30:35 -0700 Message-ID: <20240616053035.3076026-1-kmatsui@gcc.gnu.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240616005925.3046783-1-kmatsui@gcc.gnu.org> References: <20240616005925.3046783-1-kmatsui@gcc.gnu.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-GUID: 0yDFPDv-vHKvsfuwWr1_2vOzgUsWEkq6 X-Proofpoint-ORIG-GUID: 0yDFPDv-vHKvsfuwWr1_2vOzgUsWEkq6 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-16_04,2024-06-14_03,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 mlxlogscore=970 lowpriorityscore=0 mlxscore=0 adultscore=0 spamscore=0 bulkscore=0 impostorscore=0 suspectscore=0 clxscore=1034 phishscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406160041 X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NEUTRAL,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 --- 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, + "% 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