From patchwork Sat Mar 2 13:04:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ken Matsui X-Patchwork-Id: 209216 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:fa17:b0:10a:f01:a869 with SMTP id ju23csp465751dyc; Sat, 2 Mar 2024 05:05:54 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX34/bvZ/A5GYO3EG9a6/kDzxI0pxkwtFs6QeedHq+Z+9uZSgndQjkV+/JPL17AkylrosfkJoRQr8vbzf7yb2/iNTXpxA== X-Google-Smtp-Source: AGHT+IE47dqR9Bqo+cwiaDLQER7Pmpy6k02fx4BvsFWA7s9iP0IBru5pR48WQED+R+18FrNwvIt7 X-Received: by 2002:a05:6122:4812:b0:4d3:3236:e91 with SMTP id ef18-20020a056122481200b004d332360e91mr2678531vkb.11.1709384753723; Sat, 02 Mar 2024 05:05:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709384753; cv=pass; d=google.com; s=arc-20160816; b=ziw54RBSf2VsG9vgZphqwspkw/s7tKtQrdiWp8vTA123qEUiotw3W+HClunr/Vin/M l4YHIPmsYurtNkq72yTs77kzI4sMLV6QfCwigpJtu7bx268iFG2bcYAXOJ8cC5cN9zLf 4cVC2YSnDdPGRrBxcCxKkiJTrBXR+e1rW79P+AzPK8LCC6x04TjIyIyjueO0i6KGCMJ0 IiO4GDJlml9dYctvcyHEiTiEuldJjVmap5CZt4/8Nxl8jCMvwoOVsb9mnzdgPQpjahbk H1uOy0ygRpKUSxSBJQhXsJry9Y/QPVYKLmekOEYLRpqWl5sYXKDmt5t+MZjQsYiIkEdo 9T4g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:arc-filter:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=s53ZKzyPdDnhvHbe/iMNzibvcqh2FMPYl0dggx9kY0Q=; fh=INZSbTxGthcJ6LtyIn70+VzOfwJL3SRgAHGh2CURDOU=; b=jFpY3Bhuz58clEkby/bhHVHbaPAAtjK2P41wGVHMsMKCwt/qH/xyiumxsIjZ4UZZvh 72mp6pR/HWwsnIjKe5r1QyKSNrKB5we6R8TAmLLI7ebRcdFRmpF3Q9D9K4SfqnJ48WVw IM2nb4SzSUkyU7doJ0pdTP4dd2a6kjOeMKGvQI9KV2o3JlAab4W1sLyXBjkevNnxjojv /FVDsyg33Xoh/NWkDX+LFnM3F4ueFXdxeVcWtFw/1cF7dF+1GgQ/mYzb87AEFYDuQvR4 kkN1lXjc6qz0lObGDIi+RKG9I2/KjHALqG6RUow90Yg7Oye0uiav8MaDLEdu5zKZXnpp fisw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=dHrdJnlV; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id n21-20020ac5cd55000000b004d188891572si806552vkm.212.2024.03.02.05.05.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Mar 2024 05:05:53 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=dHrdJnlV; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 41C663858C42 for ; Sat, 2 Mar 2024 13:05:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 41C663858C42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709384753; bh=s53ZKzyPdDnhvHbe/iMNzibvcqh2FMPYl0dggx9kY0Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=dHrdJnlVVQpNEzCdxaeetyXnilt2Ve3NpZ97pV/Xg8lcLbeuejVPqF+kNFLOMaGq5 IiSu6DSKpUqmp0whEkWI7zjltaSGSKDLpwoLHmSW+LG8mMB58jj4Du3BcktxzJcBni RXw2LTyo4qeF9gOhvXmiVtpN7y1aFSeeT/wLVV30= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00641c01.pphosted.com (mx0b-00641c01.pphosted.com [205.220.177.146]) by sourceware.org (Postfix) with ESMTPS id 07B5E3858D1E; Sat, 2 Mar 2024 13:04:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 07B5E3858D1E 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 07B5E3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.177.146 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709384693; cv=none; b=J+p1kMEVLgE8GRqejrmeeaOXJCAzhSc2kjU0OupowmrnIcFS8yVTc8/6NgJj/+08TkblKMJ0Xu5hH6QXm7McLMGd2srbRyJNyiAQ8hunHeQflsQhk6y8qXxKgyN3gfhHgzhCnZkLMf2K3xWoyzXeDprgs36m3gEPo2K8nT63+FA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709384693; c=relaxed/simple; bh=J9GbZbdqJ9VyOV4FKFHACgQEpZ8EcNg3bupnOwqb+xg=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=UrAOGQ+4Vqn1/G4sa6X9Zdgi39/yK0G7cMVS0wExrt/lDzmjSPoJGqH/q/24CpmmkCcfcd+rKZZqC5N4I2NOrj/4UlkuWcSVo1MSn0evJ0xpHPvEfkSnCROuW8f8g5BHPnpGpFei7+xaFFnvD/98nZZkupmsoBZEgDbvgdItFZU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0247476.ppops.net [127.0.0.1]) by mx0a-00641c01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 422D4Zia004683; Sat, 2 Mar 2024 13:04:44 GMT Received: from mxout21.s.uw.edu (mxout21.s.uw.edu [140.142.32.139]) by mx0a-00641c01.pphosted.com (PPS) with ESMTPS id 3wkuy5sjy9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 02 Mar 2024 13:04:44 +0000 Received: from smtp.washington.edu (smtp.washington.edu [128.208.60.132]) by mxout21.s.uw.edu (8.14.4+UW20.07/8.14.4+UW24.02) with ESMTP id 422D4bE4014390 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 2 Mar 2024 05:04:38 -0800 X-Auth-Received: from kmatsui-ThinkPad-X1-Carbon-Gen-9.dhcp4.washington.edu ([10.154.74.128]) (authenticated authid=kmatsui) by smtp.washington.edu (8.16.1+UW21.10/8.14.4+UW24.02) with ESMTPSA id 422D4bGh018073 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 2 Mar 2024 05:04:37 -0800 X-UW-Orig-Sender: kmatsui@smtp.washington.edu From: Ken Matsui To: gcc-patches@gcc.gnu.org Cc: Ken Matsui Subject: [PATCH v2] gcc, libcpp: Add warning switch for "#pragma once in main file" [PR89808] Date: Sat, 2 Mar 2024 05:04:36 -0800 Message-ID: <20240302130436.2694515-1-kmatsui@gcc.gnu.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240302064520.2076664-1-kmatsui@gcc.gnu.org> References: <20240302064520.2076664-1-kmatsui@gcc.gnu.org> MIME-Version: 1.0 X-Proofpoint-GUID: w3UPfzxfZullOIZ1k14KNixBh3321x8I X-Proofpoint-ORIG-GUID: w3UPfzxfZullOIZ1k14KNixBh3321x8I X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-02_04,2024-03-01_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 clxscore=1034 adultscore=0 priorityscore=1501 malwarescore=0 bulkscore=0 mlxlogscore=999 lowpriorityscore=0 impostorscore=0 mlxscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2403020113 X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792395961874817596 X-GMAIL-MSGID: 1792419827465299395 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-opts.cc (c_common_handle_option): Handle OPT_Wpragma_once_outside_header. * 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. * directives.cc (do_pragma_once): Use cpp_warn_pragma_once_outside_header. * init.cc (cpp_create_reader): Handle cpp_warn_pragma_once_outside_header. gcc/testsuite/ChangeLog: * g++.dg/Wpragma-once-outside-header.C: New test. * 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-opts.cc | 9 +++++++++ gcc/c-family/c.opt | 4 ++++ gcc/doc/invoke.texi | 10 ++++++++-- gcc/testsuite/g++.dg/Wpragma-once-outside-header.C | 5 +++++ .../g++.dg/warn/Wno-pragma-once-outside-header.C | 5 +++++ .../g++.dg/warn/Wpragma-once-outside-header.C | 5 +++++ libcpp/directives.cc | 8 ++++++-- libcpp/include/cpplib.h | 4 ++++ libcpp/init.cc | 1 + 9 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/Wpragma-once-outside-header.C 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-opts.cc b/gcc/c-family/c-opts.cc index be3058dca63..4edd8c6c515 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -430,6 +430,15 @@ c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value, cpp_opts->warn_num_sign_change = value; break; + case OPT_Wpragma_once_outside_header: + if (value == 0) + cpp_opts->cpp_warn_pragma_once_outside_header = 0; + else if (kind == DK_ERROR) + cpp_opts->cpp_warn_pragma_once_outside_header = 2; + else + cpp_opts->cpp_warn_pragma_once_outside_header = 1; + break; + case OPT_Wunknown_pragmas: /* Set to greater than 1, so that even unknown pragmas in system headers will be warned about. */ diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index b7a4a1a68e3..6841a5a5e81 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1180,6 +1180,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) 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 bdf05be387d..eeb8954bcdf 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 @@ -7955,6 +7955,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/Wpragma-once-outside-header.C b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C new file mode 100644 index 00000000000..678bd4e7626 --- /dev/null +++ b/gcc/testsuite/g++.dg/Wpragma-once-outside-header.C @@ -0,0 +1,5 @@ +/* { dg-do assemble } */ +/* { dg-options "-Werror=pragma-once-outside-header" } */ + +#pragma once // { dg-error "#pragma once in main file" } +int main() {} 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..ae958d3beb8 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wpragma-once-outside-header.C @@ -0,0 +1,5 @@ +// { dg-do assemble } +// { dg-options "-Werror=pragma-once-outside-header" } + +#pragma once // { dg-error "#pragma once in main file" } +int main() {} diff --git a/libcpp/directives.cc b/libcpp/directives.cc index 479f8c716e8..b6121a459f8 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_error (pfile, (warn_level == 1 ? CPP_DL_WARNING : CPP_DL_ERROR), + "#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..d25b9606153 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; diff --git a/libcpp/init.cc b/libcpp/init.cc index 54fc9236d38..4be1afdb2c2 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