From patchwork Tue Jul 18 19:14:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hamza Mahfooz X-Patchwork-Id: 122204 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1955801vqt; Tue, 18 Jul 2023 12:15:41 -0700 (PDT) X-Google-Smtp-Source: APBJJlFIB0IKbYR6ARCzvdPKUnNo0ymBpfOuRJ44OCfb/jgxSeVUyDWpiN783zWHIJe/32TLAzbj X-Received: by 2002:a05:6512:3d0f:b0:4f8:7551:7485 with SMTP id d15-20020a0565123d0f00b004f875517485mr13913650lfv.5.1689707741111; Tue, 18 Jul 2023 12:15:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689707741; cv=none; d=google.com; s=arc-20160816; b=rCExmWAgCau4fRaG7BKxe56kB9DoJ/Y7pbGB9DTOSUkWDqpaHiCk8VE/ebzV4VWHpF JGO/ea25I6wKJ2Q+9g3gWyaW4I7xPjK/euZfohWGDOJNdqii9P0TUSj7rJJlAXsggu85 wqsf5sxaG8ZfSgY/nZOwt9VBKc9tWQcKkKx3vYlcDw/XOuUAg8oPaCHJbtOE2zPUTpMi 8ymwn1DV9zn0vKp6SJT7p0e3HpKYRYq5bFt0+znZJ1ytSCmXoEJtcg4StKtAZJSceL2l 26XYyT/Ly+bD0mF05SBGbBig7URLQRZchJcgd3CXcEqJFhFJqK29K2UTy7AJJtR8yqiX nvcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dmarc-filter :delivered-to; bh=pk601fdmrU6kw3UnUzedVdPWjIIgWfVnjPrBP2pjpdM=; fh=9NLLBenRkBdlgveUyxwc/Q0X5JtZJKTt3pVHPp/7tGY=; b=Mt61zNeSAUQaRF7XhJaFedPayOMpYySJhvZqCqhen/T0KRZLj+JykvQjXf+3mp14SR geUWvyigfK2ogOkeuyaooiM4LhA0o6xWHFq0+yWUlUaaRcDsJ39p5dC9zMnM4M73V6QQ F6+qaOY+0GzCUjWS9SyxJKDQk+duOKly3pt5a2K0iqjUmLaRRXhBYmNmReQu7xuIchMD 67JB1DSgnQvMzCaVsEktBDCeo7v7QHWejTKNX/eS8vx+p8yJAATZUoX7k8cswxE76de/ P4Ds1K2bq9upEjzr095CgpuFizVQWEzljyLXuw/VmEmHeVK3LEPO11xNr3JccXsPuViG zn0A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id a14-20020a50ff0e000000b0052177c077fbsi1643983edu.484.2023.07.18.12.15.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 12:15:41 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F2739385700F for ; Tue, 18 Jul 2023 19:15:32 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from MTA-05-3.privateemail.com (mta-05-3.privateemail.com [198.54.118.212]) by sourceware.org (Postfix) with ESMTPS id 342603858D32 for ; Tue, 18 Jul 2023 19:15:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 342603858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=effective-light.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=effective-light.com Received: from mta-05.privateemail.com (localhost [127.0.0.1]) by mta-05.privateemail.com (Postfix) with ESMTP id 63274180216A; Tue, 18 Jul 2023 15:15:07 -0400 (EDT) Received: from hal-station.. (unknown [76.66.148.229]) by mta-05.privateemail.com (Postfix) with ESMTPA id 89927180216B; Tue, 18 Jul 2023 15:15:01 -0400 (EDT) From: Hamza Mahfooz To: gcc-patches@gcc.gnu.org Cc: Richard Biener , Joseph Myers , Marek Polacek , Martin Uecker , Hamza Mahfooz Subject: [PATCH RESEND] c: add -Wmissing-variable-declarations [PR65213] Date: Tue, 18 Jul 2023 15:14:31 -0400 Message-ID: <20230718191431.50059-1-someguy@effective-light.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, 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.29 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 Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771786984044878831 X-GMAIL-MSGID: 1771786984044878831 Resolves: PR c/65213 - Extend -Wmissing-declarations to variables [i.e. add -Wmissing-variable-declarations] gcc/c-family/ChangeLog: PR c/65213 * c.opt (-Wmissing-variable-declarations): New option. gcc/c/ChangeLog: PR c/65213 * c-decl.cc (start_decl): Handle -Wmissing-variable-declarations. gcc/ChangeLog: PR c/65213 * doc/invoke.texi (-Wmissing-variable-declarations): Document new option. gcc/testsuite/ChangeLog: PR c/65213 * gcc.dg/Wmissing-variable-declarations.c: New test. Signed-off-by: Hamza Mahfooz --- gcc/c-family/c.opt | 4 +++ gcc/c/c-decl.cc | 10 +++++- gcc/doc/invoke.texi | 11 +++++-- .../gcc.dg/Wmissing-variable-declarations.c | 33 +++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Wmissing-variable-declarations.c diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 4abdc8d0e77..0ed87fcc7be 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1048,6 +1048,10 @@ Wmissing-prototypes C ObjC Var(warn_missing_prototypes) Warning Warn about global functions without prototypes. +Wmissing-variable-declarations +C ObjC Var(warn_missing_variable_declarations) Warning +Warn about global variables without previous declarations. + Wmudflap C ObjC C++ ObjC++ WarnRemoved diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index ecd10ebb69c..1f9eb44dbaa 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -5340,6 +5340,7 @@ start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, location_t *lastloc /* = NULL */) { tree decl; + tree old_decl; tree tem; tree expr = NULL_TREE; enum deprecated_states deprecated_state = DEPRECATED_NORMAL; @@ -5360,7 +5361,9 @@ start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, if (!decl || decl == error_mark_node) return NULL_TREE; - if (tree lastdecl = lastloc ? lookup_last_decl (decl) : NULL_TREE) + old_decl = lookup_last_decl (decl); + + if (tree lastdecl = lastloc ? old_decl : NULL_TREE) if (lastdecl != error_mark_node) *lastloc = DECL_SOURCE_LOCATION (lastdecl); @@ -5372,6 +5375,11 @@ start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs, && TREE_PUBLIC (decl)) warning (OPT_Wmain, "%q+D is usually a function", decl); + if (warn_missing_variable_declarations && VAR_P (decl) + && !DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) && old_decl == NULL_TREE) + warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_variable_declarations, + "no previous declaration for %qD", decl); + if (initialized) /* Is it valid for this decl to have an initializer at all? If not, set INITIALIZED to zero, which will indirectly diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 88e3c625030..c2a0562b9e6 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -498,8 +498,8 @@ Objective-C and Objective-C++ Dialects}. @item C and Objective-C-only Warning Options @gccoptlist{-Wbad-function-cast -Wmissing-declarations --Wmissing-parameter-type -Wmissing-prototypes -Wnested-externs --Wold-style-declaration -Wold-style-definition +-Wmissing-parameter-type -Wmissing-prototypes -Wmissing-variable-declarations +-Wnested-externs -Wold-style-declaration -Wold-style-definition -Wstrict-prototypes -Wtraditional -Wtraditional-conversion -Wdeclaration-after-statement -Wpointer-sign} @@ -9610,6 +9610,13 @@ provide prototypes and a non-matching declaration declares an overload rather than conflict with an earlier declaration. Use @option{-Wmissing-declarations} to detect missing declarations in C++. +@opindex Wmissing-variable-declarations +@opindex Wno-missing-variable-declarations +@item -Wmissing-variable-declarations @r{(C and Objective-C only)} +Warn if a global variable is defined without a previous declaration. +Use this option to detect global variables that do not have a matching +extern declaration in a header file. + @opindex Wmissing-declarations @opindex Wno-missing-declarations @item -Wmissing-declarations diff --git a/gcc/testsuite/gcc.dg/Wmissing-variable-declarations.c b/gcc/testsuite/gcc.dg/Wmissing-variable-declarations.c new file mode 100644 index 00000000000..b292dbe8c22 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wmissing-variable-declarations.c @@ -0,0 +1,33 @@ +/* { dg-do compile } */ +/* { dg-options "-Wmissing-variable-declarations" } */ + +int b0; /* { dg-warning "no previous declaration for 'b0'" } */ + +int b1 = 1; /* { dg-warning "no previous declaration for 'b1'" } */ + +int b2; /* { dg-warning "no previous declaration for 'b2'" } */ +int b2 = 2; + +struct { + int g0; +} b3; /* { dg-warning "no previous declaration for 'b3'" } */ + +int b4; /* { dg-warning "no previous declaration for 'b4'" } */ +int b4 = 3; +extern int b4; + +static int g1; + +void g2(void); + +extern int g3; +int g3; +int g3 = 4; + +struct g4 { + int g5; +}; + +void g6(void) { + int g7; +}