From patchwork Thu Jun 13 14:31:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ken Matsui X-Patchwork-Id: 1947441 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=wDt+sZt7; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W0PyD4vffz1ydW for ; Fri, 14 Jun 2024 00:32:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E748F388216B for ; Thu, 13 Jun 2024 14:32:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E748F388216B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1718289154; bh=Qe9JT5n1pTsgVXLdm5WKcEF9/J/m+D6VhiuFDWSjq5Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=wDt+sZt70MiBLADgKbZIsiWbd3zUSqXUVagCGV7Kt5gEoy1hXDJkqyPKHrSiIlpZX 114mhohWpOtXiVhnhmsUcz03FXVMnQUxYAsjY1uSynYUBsMbkfdNBYnn+F+bhwqlSZ mcnx/5L5+OtqbmQf017PQ0TFaiLmwus84fP93Zvc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00641c01.pphosted.com (mx0a-00641c01.pphosted.com [205.220.165.146]) by sourceware.org (Postfix) with ESMTPS id B13DC3882168; Thu, 13 Jun 2024 14:32:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B13DC3882168 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 B13DC3882168 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=1718289126; cv=none; b=SQfH+wBz812meYUCWfR7g/cOQqCb65BI8/MT0Xnl444EObYs+v4f6J5aXHPbjy1Ka9n6iG+Mwnx6rH6vmJKJ0l1bDBFFv9YlYBVmJXgAuuVz4AMh+u6sjOJexuiMsa1GCQkK052pTOfJnAI4f3H6/Alx9HH1ldeWwBnQPEB7Pdc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718289126; c=relaxed/simple; bh=joJcOpW9K2kmus8r1qqjzDI6es4u72KDSDI6O2IJ3nQ=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=meS/1MZpQvNCnbfIIkklPq76Zbvz61/58smQDQW1pTvqHm2SvQ+Pc/CpJ90uTYPC1WR37SZrlbAP0sqO/FClgAXVF6goHu7CfC+RtdiAerDbh+4wBitAYNQeJrA/L9qXeoLZS2R0g1S/sJpcOTsdN5smYzd4tGMqZ8jvf1Geyuk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0247475.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45DE58Ax030047; Thu, 13 Jun 2024 14:32:02 GMT Received: from mxout23.cac.washington.edu (mxout23.cac.washington.edu [140.142.32.140]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3yqybesnu0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 13 Jun 2024 14:32:02 +0000 (GMT) Received: from smtp.washington.edu (smtp.washington.edu [128.208.60.132]) by mxout23.cac.washington.edu (8.14.4+UW20.07/8.14.4+UW24.02) with ESMTP id 45DEVg93019063 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 13 Jun 2024 07:31:42 -0700 X-Auth-Received: from ken-20xw00qgus.dhcp4.washington.edu ([10.154.74.241]) (authenticated authid=kmatsui) by smtp.washington.edu (8.16.1+UW21.10/8.14.4+UW24.02) with ESMTPSA id 45DEVgbT023567 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 13 Jun 2024 07:31:42 -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 v3] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808] Date: Thu, 13 Jun 2024 07:31:40 -0700 Message-ID: <20240613143140.660743-1-kmatsui@gcc.gnu.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240302130436.2694515-1-kmatsui@gcc.gnu.org> References: <20240302130436.2694515-1-kmatsui@gcc.gnu.org> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: XfUewKBVCUtz8hJcbB7SnZK_5_o-PG3G X-Proofpoint-GUID: XfUewKBVCUtz8hJcbB7SnZK_5_o-PG3G 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-13_08,2024-06-13_02,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 priorityscore=1501 phishscore=0 spamscore=0 clxscore=1034 lowpriorityscore=0 impostorscore=0 malwarescore=0 bulkscore=0 adultscore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406130106 X-Spam-Status: No, score=-12.9 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org 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 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..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_HEADER, + "#pragma once in main file"); check_eol (pfile, false); _cpp_mark_file_once_only (pfile, pfile->buffer->file); @@ -2822,4 +2826,3 @@ _cpp_bracket_include(cpp_reader *pfile) { return glue_header_name (pfile); } - diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index c62374d3192..21deaf72ec5 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -583,6 +583,10 @@ struct cpp_options 2 if it should be a pedwarn. */ unsigned char cpp_warn_invalid_utf8; + /* True if libcpp should warn about #pragma once outside of a header. + 2 if it should be an error, i.e., -Werror. */ + unsigned char cpp_warn_pragma_once_outside_header; + /* True if libcpp should warn about invalid forms of delimited or named escape sequences. */ bool cpp_warn_unicode; @@ -701,7 +705,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 diff --git a/libcpp/init.cc b/libcpp/init.cc index c457fa659e7..ba6e5f08446 100644 --- a/libcpp/init.cc +++ b/libcpp/init.cc @@ -235,6 +235,7 @@ cpp_create_reader (enum c_lang lang, cpp_hash_table *table, CPP_OPTION (pfile, cpp_warn_bidirectional) = bidirectional_unpaired; CPP_OPTION (pfile, cpp_warn_invalid_utf8) = 0; CPP_OPTION (pfile, cpp_warn_unicode) = 1; + CPP_OPTION (pfile, cpp_warn_pragma_once_outside_header) = 1; CPP_OPTION (pfile, cpp_input_charset_explicit) = 0; /* Default CPP arithmetic to something sensible for the host for the