From patchwork Fri Aug 4 16:06:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9881605 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0915B60375 for ; Fri, 4 Aug 2017 16:06:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EA840289B4 for ; Fri, 4 Aug 2017 16:06:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DF69D289BE; Fri, 4 Aug 2017 16:06:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 835DC289B4 for ; Fri, 4 Aug 2017 16:06:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752514AbdHDQGf (ORCPT ); Fri, 4 Aug 2017 12:06:35 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:36198 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752242AbdHDQGe (ORCPT ); Fri, 4 Aug 2017 12:06:34 -0400 Received: by mail-wr0-f194.google.com with SMTP id y67so3286676wrb.3 for ; Fri, 04 Aug 2017 09:06:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=JcT2ddb+XOHcx8DAZQXNGbqofIbH869YmBpnzNXMBww=; b=We80NT6hJPbSjhLAHFGZpnmU1GPqrObi1vB0wdDPJCE27vZlhHTb22nqLyODMqwN9y TsW8wikLi4ajtSa1Fj1ZQWFDjQsN0PF8M2xKrVIjdgyW330odABPzMroWTPcXY2ivaqD L4AvSv++MRisJTr4ubB9BxoeTKulBfe1tMbR4TXul5OmOz4491zcGIN1gaKgNDIP2jAt LQhyrhQzXbFWHVgmHVaz/wk0NikXorEicgEAZLKK6XbMeT2dySVuIIR/RScbRoSHs6Zq rRMTCgejk7KIXX7COyE5e/2F0SwzUkBjKLW7cOGawFNUHp1IG+6XkkeyZSYCyUF6dK1P k8Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=JcT2ddb+XOHcx8DAZQXNGbqofIbH869YmBpnzNXMBww=; b=CwGGRDDJdB/BUKKfepx74shgRW/v4uc37DrUx0F2U/aNAyBWcdQPvYFMQy9ZilaYkF Sc2k5abzuaIRhzF23LnaKqN/R8jATZAK8PaJfLPobc0MxV2bpap+F8/QXOBeYZCeH5V2 BSzmG82LI/QO/3NxlihFoq96/xLhN/lIyz87E0AAd9F+HcKqGUme3ytv/iZUnghCav+C azOXuV0+gnQrPrVC8mO9CCHlczv7iVuZeHC9N8s5+8FL71ukjfUrg4ldpv0u0VolDDyP gpTv7pEPVMeBck8aPogGRDS7jBDElafAi9/Pksj4f5V1OQvBdoQ9wD13kSUQy4ORDBca mpHQ== X-Gm-Message-State: AIVw111skqcg3LlpZ2yxJA5ZGi0AuzifI4+IdnCI6FY/+2ZKTd/mXUPs AcbkIW7uxR+eWhgJ+08= X-Received: by 10.223.145.65 with SMTP id j59mr2342573wrj.245.1501862793287; Fri, 04 Aug 2017 09:06:33 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:40c4:ab00:f447:cdc2:b82f:f97d]) by smtp.gmail.com with ESMTPSA id l29sm4697330wre.46.2017.08.04.09.06.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Aug 2017 09:06:32 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Luc Van Oostenryck Subject: [PATCH v2 2/2] fix: give a type to bad cond expr with known condition Date: Fri, 4 Aug 2017 18:06:22 +0200 Message-Id: <20170804160622.18260-3-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.13.2 In-Reply-To: <20170804160622.18260-1-luc.vanoostenryck@gmail.com> References: <20170804160622.18260-1-luc.vanoostenryck@gmail.com> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Conditional expressions whose second & third operands have non-compatible types are not conform to the C standard and sparse emit a warning for them and return the expression as being erroneous. In consequence, such expressions are not given a type. This, in turn, makes that some further processing cannot be done (correctly). It seems that neither GCC nor clang emit a warning when there is a type mismatch but the condition is a constant. In the case we're interested here (the slow compilation of a file) the operation that cannot be done is the expansion its operands. This, in turn and among other things, makes that builtins like __builtin_constant_p() are not evaluated with disatrous consequence for the amount of work done in the next phases. Fix this by giving to conditional expressions with constant condition the same type as the operand selected by the conditional (but keeping the warning) as GCC & clang seems to do. Signed-off-by: Luc Van Oostenryck --- evaluate.c | 12 ++++++++++++ validation/cond-err-expand.c | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 validation/cond-err-expand.c diff --git a/evaluate.c b/evaluate.c index cf3cf244d..649e132b8 100644 --- a/evaluate.c +++ b/evaluate.c @@ -1220,6 +1220,18 @@ static struct symbol *evaluate_conditional_expression(struct expression *expr) Err: expression_error(expr, "incompatible types in conditional expression (%s)", typediff); + /* + * if the condition is constant, the type is in fact known + * so use it, as gcc & clang do. + */ + switch (expr_truth_value(expr->conditional)) { + case 1: expr->ctype = ltype; + break; + case 0: expr->ctype = rtype; + break; + default: + break; + } return NULL; out: diff --git a/validation/cond-err-expand.c b/validation/cond-err-expand.c new file mode 100644 index 000000000..93bbac153 --- /dev/null +++ b/validation/cond-err-expand.c @@ -0,0 +1,27 @@ +static inline void f(void) +{ + __builtin_constant_p(0); +} + +void foo(void) +{ + 0 ? 0 : f(); +} + +void bar(void) +{ + 1 ? f() : 0; +} + +/* + * check-name: cond-err-expand.c + * check-command: test-linearize -Wno-decl $file + * + * check-error-start +cond-err-expand.c:8:11: error: incompatible types in conditional expression (different base types) +cond-err-expand.c:13:11: error: incompatible types in conditional expression (different base types) + * check-error-end + * + * check-output-ignore + * check-excludes: call.* __builtin_constant_p + */