From patchwork Tue Aug 9 15:59:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 657350 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3s7zX46x2Dz9sf9 for ; Wed, 10 Aug 2016 02:00:35 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=tlphswd1; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id; q=dns; s=default; b=gj4TmVnrQOma26f jKK09lw2bCi49MngFDAhcZHJt8OIl7x6irB2nq1ac678sCFHDDrRPRggMvx8ATkg MP6rqfV2358BhgrsGtEt3WraLnbQgCDxgPZ2fSFVeq5w9cAKMMSOnELKSd/L7RIV TkaepPw7bYwlXnnIgsCuVWAfnvqs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id; s=default; bh=yEjVQqHAbAGeTx5AYy8Zr 5Fbeek=; b=tlphswd1HYhtg6Lk6GRrXhXw5l1szHsLFkCGt1P5SGqc+lxFTJMGB 4mGDn+AxTCizs6BIsLhYcHIfUvIbnP9ZgWieJOFXdqev41CLFpWRxJJulQaW3jSK hgAiTs4zfsSdT54pxt/3ZqBTuMH6L4urgTmNXOrcSfZhLN5tpBQYXE= Received: (qmail 7514 invoked by alias); 9 Aug 2016 16:00:23 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 5667 invoked by uid 89); 9 Aug 2016 16:00:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=BAYES_00, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 spammy=booth, TYPE, Compute, gdr X-HELO: mail-wm0-f48.google.com Received: from mail-wm0-f48.google.com (HELO mail-wm0-f48.google.com) (74.125.82.48) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Tue, 09 Aug 2016 15:59:58 +0000 Received: by mail-wm0-f48.google.com with SMTP id o80so44780848wme.1 for ; Tue, 09 Aug 2016 08:59:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:subject:date:message-id; bh=ARgUr/emSc8BiFXSz3qs6mGbqmOjHcquT/f080nEJuA=; b=IF4Q4hNknYPFm+iqrjVj0Koep90QQgIEgsT1MuomIwb3zAkQfHnP3PYx3NiRh7M1qh Lo0AFj75Jd8QDF8P/a8192xsN6dYWtxAGcs/nvN5B0JbRDpYrG3KKL2r8yt5ZPWrofSr CA1BUf22u0l/5fN3P0x1iXycpiKftMBYPn2OB//ZZg0H38T3gf57/6BV0ddwxBcp44eH u6WBt3zMfIHAvUUNUcPITQeqhsVzyCmuYqrwJKGkpuprqF8vWElcldze1+3RejnJE+mS uwGOshfurWOaMlfKKCcDdpoqK4wwFJs6pY7qi8qNMk7X2DtxX9GEwxhNK3cnkiwKPWbR Xurw== X-Gm-Message-State: AEkooutXB513wrcSy3EfodiQI5CaQryGjpRJeiztDWoysBcxhJ1tVay6EVDSASxpOQlV+w== X-Received: by 10.28.1.23 with SMTP id 23mr23013112wmb.22.1470758394687; Tue, 09 Aug 2016 08:59:54 -0700 (PDT) Received: from 640k.lan (94-39-152-88.adsl-ull.clienti.tiscali.it. [94.39.152.88]) by smtp.gmail.com with ESMTPSA id 142sm841600wmh.12.2016.08.09.08.59.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Aug 2016 08:59:52 -0700 (PDT) From: Paolo Bonzini To: gcc-patches@gcc.gnu.org Subject: [PATCH] cpp/c: Add -Wexpansion-to-defined Date: Tue, 9 Aug 2016 17:59:50 +0200 Message-Id: <1470758390-42725-1-git-send-email-bonzini@gnu.org> clang recently added a new warning -Wexpansion-to-defined, which warns when `defined' is used outside a #if expression (including the case of a macro that is then used in a #if expression). While I disagree with their inclusion of the warning to -Wall, I think it is a good addition overall. First, it is a logical extension of the existing warning for breaking defined across a macro and its caller. Second, it is good to make these warnings for `defined' available with a command-line option other than -pedantic. This patch adds -Wexpansion-to-defined, and enables it automatically for both -pedantic (as before) and -Wextra. Bootstrapped and regression tested x86_64-pc-linux-gnu, ok? Paolo libcpp: 2016-08-09 Paolo Bonzini * include/cpplib.h (struct cpp_options): Add new member warn_expansion_to_defined. (CPP_W_EXPANSION_TO_DEFINED): New enum member. * expr.c (parse_defined): Warn for all uses of "defined" in macros, and tie warning to CPP_W_EXPANSION_TO_DEFINED. * system.h (HAVE_DESIGNATED_INITIALIZERS): Do not use "defined" in macros. gcc: 2016-08-09 Paolo Bonzini * system.h (HAVE_DESIGNATED_INITIALIZERS, HAVE_DESIGNATED_UNION_INITIALIZERS): Do not use "defined" in macros. gcc/c-family: 2016-08-09 Paolo Bonzini * c-opts (sanitize_cpp_opts): Compute default value for -Wexpansion-to-defined. * c.opt (Wexpansion-to-defined): New. gcc/doc: 2016-08-09 Paolo Bonzini * cpp.texi (Defined): Mention -Wexpansion-to-defined. * cppopts.texi (Invocation): Document -Wexpansion-to-defined. * invoke.texi (Warning Options): Document -Wexpansion-to-defined. gcc/testsuite: 2016-08-09 Paolo Bonzini * gcc.dg/cpp/defined.c: Mark newly introduced warnings. * gcc.dg/cpp/defined-Wexpansion-to-defined.c, gcc.dg/cpp/defined-Wextra-Wno-expansion-to-defined.c, gcc.dg/cpp/defined-Wextra.c, gcc.dg/cpp/defined-Wno-expansion-to-defined.c: New testcases. Index: gcc/c-family/c-opts.c =================================================================== --- gcc/c-family/c-opts.c (revision 239276) +++ gcc/c-family/c-opts.c (working copy) @@ -1256,6 +1256,10 @@ sanitize_cpp_opts (void) cpp_opts->unsigned_char = !flag_signed_char; cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS; + cpp_opts->warn_expansion_to_defined = cpp_warn_expansion_to_defined; + if (cpp_warn_expansion_to_defined == -1) + cpp_warn_expansion_to_defined = pedantic || extra_warnings; + /* Wlong-long is disabled by default. It is enabled by: [-Wpedantic | -Wtraditional] -std=[gnu|c]++98 ; or [-Wpedantic | -Wtraditional] -std=non-c99 Index: gcc/c-family/c.opt =================================================================== --- gcc/c-family/c.opt (revision 239276) +++ gcc/c-family/c.opt (working copy) @@ -506,6 +506,10 @@ Wdouble-promotion C ObjC C++ ObjC++ Var(warn_double_promotion) Warning Warn about implicit conversions from \"float\" to \"double\". +Wexpansion-to-defined +C ObjC C++ ObjC++ CppReason(CPP_W_EXPANSION_TO_DEFINED) Var(cpp_warn_expansion_to_defined) Init(-1) Warning +Warn if an undefined macro is used in an #if directive. + Wimplicit-function-declaration C ObjC Var(warn_implicit_function_declaration) Init(-1) Warning LangEnabledBy(C ObjC,Wimplicit) Warn about implicit function declarations. Index: gcc/doc/cpp.texi =================================================================== --- gcc/doc/cpp.texi (revision 239276) +++ gcc/doc/cpp.texi (working copy) @@ -3342,7 +3342,8 @@ If the @code{defined} operator appears as a result the C standard says the behavior is undefined. GNU cpp treats it as a genuine @code{defined} operator and evaluates it normally. It will warn wherever your code uses this feature if you use the command-line option -@option{-pedantic}, since other compilers may handle it differently. +@option{-pedantic}, since other compilers may handle it differently. The +warning can also be enabled individually with @option{-Wexpansion-to-defined}. @node Else @subsection Else Index: gcc/doc/cppopts.texi =================================================================== --- gcc/doc/cppopts.texi (revision 239276) +++ gcc/doc/cppopts.texi (working copy) @@ -120,6 +120,12 @@ Warn whenever an identifier which is not a macro i @samp{#if} directive, outside of @samp{defined}. Such identifiers are replaced with zero. +@item -Wexpansion-to-defined +@opindex Wexpansion-to-defined +Warn whenever @samp{defined} is encountered in the expansion of a macro +(including the case where the macro is expanded by an @samp{#if} directive). +Such usage is not portable. + @item -Wunused-macros @opindex Wunused-macros Warn about macros defined in the main file that are unused. A macro Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 239276) +++ gcc/doc/invoke.texi (working copy) @@ -4914,6 +4914,11 @@ Warn when a declaration is found after a statement construct, known from C++, was introduced with ISO C99 and is by default allowed in GCC@. It is not supported by ISO C90. @xref{Mixed Declarations}. +@item -Wexpansion-to-defined +@opindex Wexpansion-to-defined +Warn whenever @samp{defined} is encountered in the expansion of a macro. +This warning is also enabled by @option{-Wpedantic} and @option{-Wextra}. + @item -Wundef @opindex Wundef @opindex Wno-undef Index: gcc/system.h =================================================================== --- gcc/system.h (revision 239276) +++ gcc/system.h (working copy) @@ -577,16 +577,22 @@ extern int vsnprintf (char *, size_t, const char * /* 1 if we have C99 designated initializers. */ #if !defined(HAVE_DESIGNATED_INITIALIZERS) +#ifdef __cplusplus +#define HAVE_DESIGNATED_INITIALIZERS 0 +#else #define HAVE_DESIGNATED_INITIALIZERS \ - (((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)) \ - && !defined(__cplusplus)) + (((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)) #endif +#endif #if !defined(HAVE_DESIGNATED_UNION_INITIALIZERS) +#ifdef __cplusplus +#define HAVE_DESIGNATED_UNION_INITIALIZERS (GCC_VERSION >= 4007) +#else #define HAVE_DESIGNATED_UNION_INITIALIZERS \ - (((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)) \ - && (!defined(__cplusplus) || (GCC_VERSION >= 4007))) + (((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)) #endif +#endif #if HAVE_SYS_STAT_H # include Index: gcc/testsuite/gcc.dg/cpp/defined-Wexpansion-to-defined.c =================================================================== --- gcc/testsuite/gcc.dg/cpp/defined-Wexpansion-to-defined.c (revision 0) +++ gcc/testsuite/gcc.dg/cpp/defined-Wexpansion-to-defined.c (working copy) @@ -0,0 +1,30 @@ +/* Copyright (C) 2000 Free Software Foundation, Inc. */ + +/* { dg-do preprocess } */ +/* { dg-options "-Wexpansion-to-defined" } */ + +/* Use of defined in different contexts. */ + +/* Source: Neil Booth, 29 Oct 2000, Zack Weinberg 11 Dec 2000. */ + +#define Z + +#define bad0 defined Z +#if !bad0 /* { dg-warning "may not be portable" } */ +#error Z is defined +#endif + +#define bad1 defined +#if !bad1 Z /* { dg-warning "may not be portable" } */ +#error Z is defined +#endif + +#if !bad1 (Z) /* { dg-warning "may not be portable" } */ +#error Z is defined +#endif + +#define bad2 defined (Z +#if !bad2) /* { dg-warning "may not be portable" } */ +#error Z is defined +#endif + Index: gcc/testsuite/gcc.dg/cpp/defined-Wextra-Wno-expansion-to-defined.c =================================================================== --- gcc/testsuite/gcc.dg/cpp/defined-Wextra-Wno-expansion-to-defined.c (revision 0) +++ gcc/testsuite/gcc.dg/cpp/defined-Wextra-Wno-expansion-to-defined.c (working copy) @@ -0,0 +1,30 @@ +/* Copyright (C) 2000 Free Software Foundation, Inc. */ + +/* { dg-do preprocess } */ +/* { dg-options "-Wextra -Wno-expansion-to-defined" } */ + +/* Use of defined in different contexts. */ + +/* Source: Neil Booth, 29 Oct 2000, Zack Weinberg 11 Dec 2000. */ + +#define Z + +#define bad0 defined Z +#if !bad0 /* { dg-bogus "may not be portable" } */ +#error Z is defined +#endif + +#define bad1 defined +#if !bad1 Z /* { dg-bogus "may not be portable" } */ +#error Z is defined +#endif + +#if !bad1 (Z) /* { dg-bogus "may not be portable" } */ +#error Z is defined +#endif + +#define bad2 defined (Z +#if !bad2) /* { dg-bogus "may not be portable" } */ +#error Z is defined +#endif + Index: gcc/testsuite/gcc.dg/cpp/defined-Wextra.c =================================================================== --- gcc/testsuite/gcc.dg/cpp/defined-Wextra.c (revision 0) +++ gcc/testsuite/gcc.dg/cpp/defined-Wextra.c (working copy) @@ -0,0 +1,30 @@ +/* Copyright (C) 2000 Free Software Foundation, Inc. */ + +/* { dg-do preprocess } */ +/* { dg-options "-Wextra" } */ + +/* Use of defined in different contexts. */ + +/* Source: Neil Booth, 29 Oct 2000, Zack Weinberg 11 Dec 2000. */ + +#define Z + +#define bad0 defined Z +#if !bad0 /* { dg-warning "may not be portable" } */ +#error Z is defined +#endif + +#define bad1 defined +#if !bad1 Z /* { dg-warning "may not be portable" } */ +#error Z is defined +#endif + +#if !bad1 (Z) /* { dg-warning "may not be portable" } */ +#error Z is defined +#endif + +#define bad2 defined (Z +#if !bad2) /* { dg-warning "may not be portable" } */ +#error Z is defined +#endif + Index: gcc/testsuite/gcc.dg/cpp/defined-Wno-expansion-to-defined.c =================================================================== --- gcc/testsuite/gcc.dg/cpp/defined-Wno-expansion-to-defined.c (revision 0) +++ gcc/testsuite/gcc.dg/cpp/defined-Wno-expansion-to-defined.c (working copy) @@ -0,0 +1,30 @@ +/* Copyright (C) 2000 Free Software Foundation, Inc. */ + +/* { dg-do preprocess } */ +/* { dg-options "-ansi -pedantic-errors -Wno-expansion-to-defined" } */ + +/* Use of defined in different contexts. */ + +/* Source: Neil Booth, 29 Oct 2000, Zack Weinberg 11 Dec 2000. */ + +#define Z + +#define bad0 defined Z +#if !bad0 /* { dg-bogus "may not be portable" } */ +#error Z is defined +#endif + +#define bad1 defined +#if !bad1 Z /* { dg-bogus "may not be portable" } */ +#error Z is defined +#endif + +#if !bad1 (Z) /* { dg-bogus "may not be portable" } */ +#error Z is defined +#endif + +#define bad2 defined (Z +#if !bad2) /* { dg-bogus "may not be portable" } */ +#error Z is defined +#endif + Index: gcc/testsuite/gcc.dg/cpp/defined.c =================================================================== --- gcc/testsuite/gcc.dg/cpp/defined.c (revision 239276) +++ gcc/testsuite/gcc.dg/cpp/defined.c (working copy) @@ -21,7 +21,7 @@ /* The behavior of "defined" when it comes from a macro expansion is now documented. */ -#if is_Z_defined +#if is_Z_defined /* { dg-warning "may not be portable" } */ #error Macro expanding into defined operator test 1 #endif @@ -31,7 +31,7 @@ #error Z is defined #endif -#if !is_Z_defined +#if !is_Z_defined /* { dg-warning "may not be portable" } */ #error Macro expanding into defined operator test 2 #endif @@ -53,7 +53,7 @@ /* The behavior of "defined" when it comes from a macro expansion is now documented. */ -#if is_Z_defined +#if is_Z_defined /* { dg-warning "may not be portable" } */ #error Macro expanding into defined operator test 1 #endif @@ -63,7 +63,7 @@ #error Z is defined #endif -#if !is_Z_defined +#if !is_Z_defined /* { dg-warning "may not be portable" } */ #error Macro expanding into defined operator test 2 #endif Index: libcpp/expr.c =================================================================== --- libcpp/expr.c (revision 239276) +++ libcpp/expr.c (working copy) @@ -947,9 +947,11 @@ parse_defined (cpp_reader *pfile) if (node) { - if (pfile->context != initial_context && CPP_PEDANTIC (pfile)) - cpp_error (pfile, CPP_DL_WARNING, - "this use of \"defined\" may not be portable"); + if ((pfile->context != initial_context + || initial_context != &pfile->base_context) + && CPP_OPTION (pfile, warn_expansion_to_defined)) + cpp_warning (pfile, CPP_W_EXPANSION_TO_DEFINED, + "this use of \"defined\" may not be portable"); _cpp_mark_macro_used (node); if (!(node->flags & NODE_USED)) Index: libcpp/include/cpplib.h =================================================================== --- libcpp/include/cpplib.h (revision 239276) +++ libcpp/include/cpplib.h (working copy) @@ -410,6 +410,10 @@ struct cpp_options /* Nonzero means warn if undefined identifiers are evaluated in an #if. */ unsigned char warn_undef; + /* Nonzero means warn if "defined" is encountered in a place other than + an #if. */ + unsigned char warn_expansion_to_defined; + /* Nonzero means warn of unused macros from the main file. */ unsigned char warn_unused_macros; @@ -1025,7 +1029,8 @@ enum { CPP_W_DATE_TIME, CPP_W_PEDANTIC, CPP_W_C90_C99_COMPAT, - CPP_W_CXX11_COMPAT + CPP_W_CXX11_COMPAT, + CPP_W_EXPANSION_TO_DEFINED }; /* Output a diagnostic of some kind. */ Index: libcpp/system.h =================================================================== --- libcpp/system.h (revision 239276) +++ libcpp/system.h (working copy) @@ -375,10 +375,13 @@ extern void abort (void); ??? C99 designated initializers are not supported by most C++ compilers, including G++. -- gdr, 2005-05-18 */ #if !defined(HAVE_DESIGNATED_INITIALIZERS) +#ifdef __cplusplus +#define HAVE_DESIGNATED_INITIALIZERS 0 +#else #define HAVE_DESIGNATED_INITIALIZERS \ - (!defined(__cplusplus) \ - && ((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L))) + ((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)) #endif +#endif #ifndef offsetof #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)