From patchwork Sat Aug 15 15:28:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11715667 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 309A4109B for ; Sat, 15 Aug 2020 21:52:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 16CB52053B for ; Sat, 15 Aug 2020 21:52:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="h+jsJCsY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729049AbgHOVwQ (ORCPT ); Sat, 15 Aug 2020 17:52:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729022AbgHOVwN (ORCPT ); Sat, 15 Aug 2020 17:52:13 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 728F5C09B04A for ; Sat, 15 Aug 2020 08:28:14 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id a26so13001839ejc.2 for ; Sat, 15 Aug 2020 08:28:14 -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 :mime-version:content-transfer-encoding; bh=3I2KTIDywX0WzYJMKmMB6Y9Qt2gPgAiitm+bK/CVbuE=; b=h+jsJCsYDjxVqbXj4pz8iIwk76cfb5UpYHvjsYzqBXgH4E+yqyaNVCtyrghWDkDXX1 10t6U1YoDSuelOCg5IJanCnUShcUEGA8LgADvhWlM1LZKmW9TwSU9QjNrhZXnJlnldMQ 5TB6TGWR59Mu1pXnFqwBw11FufidWjqs6U1NBxRX01gw77hk//ZHMOe2T78m0LTbGtre ZP/DssI4oO3QoPS2MFKNTQI5mx288fXjn2MOgcEyS8Ismcg69JF1US3OWxLce6A+u5GZ TG1AnJvTQbaDRZrjphIwvAnEplcCpakUidOLaAIJ3kLQLkwotLIXJCycCIg3o9CSHMQt eYEw== 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:mime-version:content-transfer-encoding; bh=3I2KTIDywX0WzYJMKmMB6Y9Qt2gPgAiitm+bK/CVbuE=; b=thU1rGmNaHQBBJASgDg8jSw6w9RGFROLFADv5d94Zyb64vWF0aedbvgMpYreoeRSWv npXKneKDnRrm7C9yMYky+iBHqwH1k7pLYr2d1UuaFTj2dTTopBchk+cjLciNT0BFDwp7 NdCI1ohSn709FPTW+ozW+iGLinTuERYVaNQRwF29uhI0wB6l/NmVURNq82GunoQ+F6yo a6Si93Ay05JPJ/wnofwZHBBze9Ve4isO23S9P48FDuiEoa0LmFB/BYFLmhxm0XVEEWs6 zT0YLv/7VZA/lxGPsJok400JhW+JQvjWD5vIzih8XS/2ALnY2GEy9djsa4Ron7ZdAVNS ljfg== X-Gm-Message-State: AOAM530FXXdUpdYzz0RUzrWG3Lh3OKbfsghoMB69sSimmRH+b2RkE4OJ ct0qO/cUuQvfsPqL0CD68A2mPRLPCxQ= X-Google-Smtp-Source: ABdhPJxoEsfSp13oV/PufRBynBm+xjuUNKge5X3Mo2lmVylpE6/DwXIPMl4bEuqCP6YOj04iWceSCQ== X-Received: by 2002:a17:906:4d4f:: with SMTP id b15mr7289130ejv.534.1597505289944; Sat, 15 Aug 2020 08:28:09 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:a7fb:e200:9042:5c68:7405:68b0]) by smtp.gmail.com with ESMTPSA id o7sm9065091edq.53.2020.08.15.08.28.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Aug 2020 08:28:09 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 1/3] union-cast: add some testcases Date: Sat, 15 Aug 2020 17:28:01 +0200 Message-Id: <20200815152803.2788-2-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200815152803.2788-1-luc.vanoostenryck@gmail.com> References: <20200815152803.2788-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Casts to union type are a GCC extension and are similar to compound literals. However, sparse doesn't know about them and treats them like other casts to non-scalars. Add some testcases for this and its upcoming warning flag. Signed-off-by: Luc Van Oostenryck --- validation/eval/union-cast-no.c | 24 ++++++++++++++++++++++++ validation/eval/union-cast.c | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/validation/eval/union-cast-no.c b/validation/eval/union-cast-no.c new file mode 100644 index 000000000000..d06b348d209e --- /dev/null +++ b/validation/eval/union-cast-no.c @@ -0,0 +1,24 @@ +union u { + int i; + char x[8]; +}; + +static union u foo(int i) +{ + return (union u)i; +} + +static union u bar(long l) +{ + return (union u)l; +} + +/* + * check-name: union-cast-no + * check-command: sparse -Wno-union-cast $file + * check-known-to-fail + * + * check-error-start +eval/union-cast-no.c:13:17: warning: cast to non-scalar + * check-error-end + */ diff --git a/validation/eval/union-cast.c b/validation/eval/union-cast.c new file mode 100644 index 000000000000..1d8167531081 --- /dev/null +++ b/validation/eval/union-cast.c @@ -0,0 +1,25 @@ +union u { + int i; + char x[8]; +}; + +static union u foo(int a) +{ + return (union u)a; +} + +static union u bar(long a) +{ + return (union u)a; +} + +/* + * check-name: union-cast + * check-command: sparse -Wunion-cast $file + * check-known-to-fail + * + * check-error-start +eval/union-cast.c:8:17: warning: cast to union type +eval/union-cast.c:13:17: warning: cast to non-scalar + * check-error-end + */ From patchwork Sat Aug 15 15:28:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11715713 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6B56B913 for ; Sat, 15 Aug 2020 21:56:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 528632053B for ; Sat, 15 Aug 2020 21:56:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GiFOJTXj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729031AbgHOVwN (ORCPT ); Sat, 15 Aug 2020 17:52:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728999AbgHOVwI (ORCPT ); Sat, 15 Aug 2020 17:52:08 -0400 Received: from mail-ej1-x643.google.com (mail-ej1-x643.google.com [IPv6:2a00:1450:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67381C09B048 for ; Sat, 15 Aug 2020 08:28:12 -0700 (PDT) Received: by mail-ej1-x643.google.com with SMTP id p24so12943124ejf.13 for ; Sat, 15 Aug 2020 08:28:12 -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 :mime-version:content-transfer-encoding; bh=jTBPlGckpLgZr6G5fFVFi9pIJh4KjZHk8+baZOaSTkY=; b=GiFOJTXjZWfvxmVYqEECDUv6xeaXZsODQ7t2ThXOnLf/bMZb4MnavriAAPD6HiA8RV 9rFLkmI108INEDoyZGZb/47lD4fTao+T+nj6XM0mrluxlbmxxb+clAU3NnkkOLX46Kta gZToj26sAqOLM/SS9HdPRDUnY9uAM2GWpmME3AmbU2lxVhIbkGCH2CmEEM7LmpT/T++N 2SJ8BuIldw3Gud4NibEANXsEaJTzgSz1M2LmDSG1vyP7rqS3D9dnvnWMcg+I30BT/oxZ Y5XJY7pefWuUIAFg1tz1QKqpVIyrlQfOgDRcK3Iwg28DnH54v0OV1Yz63ixPULOab0Tn tdoQ== 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:mime-version:content-transfer-encoding; bh=jTBPlGckpLgZr6G5fFVFi9pIJh4KjZHk8+baZOaSTkY=; b=EMQvzBwxkjMvV4J9w6puMfEvoXGxWOvShau9kSHgCPhp1lgH4T5Nv0CNJoD4tbqrz5 Cg0/DBmJG+xjqEeluMbdNhd3g7sMfIzfozwKHwbqn+06HdZkFnw+BmWigmT3lqoXIY7h DOFr+xvTVAb059+gYFI/y4FzgWrYaBE+ztYbgv3Kgr4XUrJ8j16zI1hYYyLhZO5YrImf W1FT+DQjGRVO9D7oqcTBNrU0QCwrs+sNPPvkwGGM56ZUJB2jGwnaMaXdnIpeZO3CEOVB xp+RScpoEOFHApADpRtN6c2XqTiR9M41ownOH4jbqFj5i94tVTojoCmJ/VWd0RIgonI6 gA7g== X-Gm-Message-State: AOAM532Xyc62za/QAIyFL37gUvVhcCaHOdZPUyLxjt8xCJhva1oIO2BY miIPPHwXUxcxKvt1Td6RlwKSgNVcTYg= X-Google-Smtp-Source: ABdhPJyaC7uGbfjYd7+nUqle2GeXW5j/+s03qmbFAH9Ry/tioDgXNAFp3YMY4axgiRAVdD+44CNZQQ== X-Received: by 2002:a17:906:6558:: with SMTP id u24mr7027723ejn.364.1597505290897; Sat, 15 Aug 2020 08:28:10 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:a7fb:e200:9042:5c68:7405:68b0]) by smtp.gmail.com with ESMTPSA id o7sm9065091edq.53.2020.08.15.08.28.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Aug 2020 08:28:10 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 2/3] union-cast: extract evaluate_compound_literal() Date: Sat, 15 Aug 2020 17:28:02 +0200 Message-Id: <20200815152803.2788-3-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200815152803.2788-1-luc.vanoostenryck@gmail.com> References: <20200815152803.2788-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org extract evaluate_compound_literal() from evaluate_cast, in preparation for supporting union casts. Signed-off-by: Luc Van Oostenryck --- evaluate.c | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/evaluate.c b/evaluate.c index 63a9390b5ee7..0563be939f23 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2948,6 +2948,26 @@ static int cast_flags(struct expression *expr, struct expression *old) return flags; } +static struct symbol *evaluate_compound_literal(struct expression *expr, struct expression *source) +{ + struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL); + struct symbol *sym = expr->cast_type; + + sym->initializer = source; + evaluate_symbol(sym); + + addr->ctype = &lazy_ptr_ctype; /* Lazy eval */ + addr->symbol = sym; + if (sym->ctype.modifiers & MOD_TOPLEVEL) + addr->flags |= CEF_ADDR; + + expr->type = EXPR_PREOP; + expr->op = '*'; + expr->deref = addr; + expr->ctype = sym; + return sym; +} + static struct symbol *evaluate_cast(struct expression *expr) { struct expression *source = expr->cast_expression; @@ -2970,25 +2990,8 @@ static struct symbol *evaluate_cast(struct expression *expr) * dereferenced as part of a post-fix expression. * We need to produce an expression that can be dereferenced. */ - if (source->type == EXPR_INITIALIZER) { - struct symbol *sym = expr->cast_type; - struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL); - - sym->initializer = source; - evaluate_symbol(sym); - - addr->ctype = &lazy_ptr_ctype; /* Lazy eval */ - addr->symbol = sym; - if (sym->ctype.modifiers & MOD_TOPLEVEL) - addr->flags |= CEF_ADDR; - - expr->type = EXPR_PREOP; - expr->op = '*'; - expr->unop = addr; - expr->ctype = sym; - - return sym; - } + if (source->type == EXPR_INITIALIZER) + return evaluate_compound_literal(expr, source); ctype = examine_symbol_type(expr->cast_type); expr->ctype = ctype; From patchwork Sat Aug 15 15:28:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11715777 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 80F2514F6 for ; Sat, 15 Aug 2020 22:02:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 687A4205CB for ; Sat, 15 Aug 2020 22:02:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Wlhe7Qey" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728763AbgHOVvZ (ORCPT ); Sat, 15 Aug 2020 17:51:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728719AbgHOVvY (ORCPT ); Sat, 15 Aug 2020 17:51:24 -0400 Received: from mail-ej1-x642.google.com (mail-ej1-x642.google.com [IPv6:2a00:1450:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06EA7C09B049 for ; Sat, 15 Aug 2020 08:28:14 -0700 (PDT) Received: by mail-ej1-x642.google.com with SMTP id f24so12977634ejx.6 for ; Sat, 15 Aug 2020 08:28:13 -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 :mime-version:content-transfer-encoding; bh=/vvEvB/Rt/gQET+Q2V8zco3STgeZML4cIwynYccYaRA=; b=Wlhe7QeyCDFfPQmJSiNAe7ro3yMmjkjZwNbo2n1E0RkL/DDrwp+vOdzxqLs/wy0FPQ 5KofllQcQvg9SsOs7Pd1Gd55XGbNlaaq85yrbUl0//a/h4XVkCqGTg5qhFFaXqzOkwMY hMkaP7RWWWSmMVPUS10A47fo5DXIS6KxbWoQD14mSa6at3XKM88PtpUFZ0W4mZ+D+Q67 BS+ud2jtnyd1m9UfNxwQHWA2GCLgxn50RaxrghOceOTxcqVFpLmUNtWTmuFuu4bbcKlf 2gC92giiKbcQ5eRKDbK8dMmCEKXA2Mt1Sq4pO/AZ6jKfgJL7PfT4IBZNxmHcXwGbMR1h lLCA== 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:mime-version:content-transfer-encoding; bh=/vvEvB/Rt/gQET+Q2V8zco3STgeZML4cIwynYccYaRA=; b=rQpC49SGA+hpUiFLM0E6fAi2HfF6ANZCvX1qz0tIFn93hRN4ic8yaT0v3dYvtnu8pv u2Mz5ojdZDSW7XDpdzwFZQlgvmihPPFpXGpR/uBAB91D0onzFmCTEq/3rU94KD+heJRd hGWusAFecFjEQl72AGG+KyqQQGoupnVBGxkfqhkl/+saqLfl170m26/HddswSW/8wj97 +lHjVS2RlBuVY5oyJeGI5A6aNGtJ58jYVqmOtyImR7J3Vk+U1S6DMbCCnnbff17hMPpw q/llHc0/Mqg3mS2m9cTCzn+vSkIdAFiEmVOO1U7SoSw7r1uEvBAeddaxpDdomCrFBYOx K4aw== X-Gm-Message-State: AOAM532xSnbpuyYuJnzFo9gRMe7ZmewzWTegoklgr6cWzIufF/D/xMNB +cER2216701hxzvyL2EKiLrfjTRJMc0= X-Google-Smtp-Source: ABdhPJxNOsPQrm+1InnK5CsOF+g2EYG233LvZWnd9veNj9Vgp3jZg600ZDqBHT7nyUOxlIW4FkszqQ== X-Received: by 2002:a17:906:a201:: with SMTP id r1mr7284120ejy.432.1597505291945; Sat, 15 Aug 2020 08:28:11 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:a7fb:e200:9042:5c68:7405:68b0]) by smtp.gmail.com with ESMTPSA id o7sm9065091edq.53.2020.08.15.08.28.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Aug 2020 08:28:11 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 3/3] union-cast: teach sparse about union casts Date: Sat, 15 Aug 2020 17:28:03 +0200 Message-Id: <20200815152803.2788-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200815152803.2788-1-luc.vanoostenryck@gmail.com> References: <20200815152803.2788-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Casts to union type are a GCC extension and are similar to compound literals. However, sparse doesn't know about them and treats them like other casts to non-scalars. So, teach sparse about them, convert them to the corresponding compound literal and add a warning flag to enable/disable the associated warning: -W[no-]union-cast. Note: a difference between union casts and compound literals is that the union casts yield rvalues while compound literals are lvalues but this distinction is not yet done in this series. Signed-off-by: Luc Van Oostenryck --- Documentation/release-notes/v0.6.3.rst | 5 +++ evaluate.c | 47 +++++++++++++++++++++++++- options.c | 2 ++ options.h | 1 + sparse.1 | 6 ++++ validation/eval/union-cast-no.c | 1 - validation/eval/union-cast.c | 1 - 7 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Documentation/release-notes/v0.6.3.rst b/Documentation/release-notes/v0.6.3.rst new file mode 100644 index 000000000000..1aae742e42de --- /dev/null +++ b/Documentation/release-notes/v0.6.3.rst @@ -0,0 +1,5 @@ +v0.6.3 (2020-xx-xy) +=================== + +* Changes in warnings: + "warning: cast to union type" [disable with -Wno-union-cast] diff --git a/evaluate.c b/evaluate.c index 0563be939f23..847137250d05 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2948,6 +2948,26 @@ static int cast_flags(struct expression *expr, struct expression *old) return flags; } +/// +// check if a type matches one of the members of a union type +// @utype: the union type +// @type: to type to check +// @return: to identifier of the matching type in the union. +static struct symbol *find_member_type(struct symbol *utype, struct symbol *type) +{ + struct symbol *t, *member; + + if (utype->type != SYM_UNION) + return NULL; + + FOR_EACH_PTR(utype->symbol_list, member) { + classify_type(member, &t); + if (type == t) + return member; + } END_FOR_EACH_PTR(member); + return NULL; +} + static struct symbol *evaluate_compound_literal(struct expression *expr, struct expression *source) { struct expression *addr = alloc_expression(expr->pos, EXPR_SYMBOL); @@ -2973,6 +2993,7 @@ static struct symbol *evaluate_cast(struct expression *expr) struct expression *source = expr->cast_expression; struct symbol *ctype; struct symbol *ttype, *stype; + struct symbol *member; int tclass, sclass; struct ident *tas = NULL, *sas = NULL; @@ -3022,8 +3043,32 @@ static struct symbol *evaluate_cast(struct expression *expr) if (expr->type == EXPR_FORCE_CAST) goto out; - if (tclass & (TYPE_COMPOUND | TYPE_FN)) + if (tclass & (TYPE_COMPOUND | TYPE_FN)) { + /* + * Special case: cast to union type (GCC extension) + * The effect is similar to a compound literal except + * that the result is a rvalue. + */ + if ((member = find_member_type(ttype, stype))) { + struct expression *item, *init; + + if (Wunion_cast) + warning(expr->pos, "cast to union type"); + + item = alloc_expression(source->pos, EXPR_IDENTIFIER); + item->expr_ident = member->ident; + item->ident_expression = source; + + init = alloc_expression(source->pos, EXPR_INITIALIZER); + add_expression(&init->expr_list, item); + + // FIXME: this should be a rvalue + evaluate_compound_literal(expr, init); + return ctype; + } + warning(expr->pos, "cast to non-scalar"); + } if (sclass & TYPE_COMPOUND) warning(expr->pos, "cast from non-scalar"); diff --git a/options.c b/options.c index f7e81b84d749..b25f715042f8 100644 --- a/options.c +++ b/options.c @@ -129,6 +129,7 @@ int Wtransparent_union = 0; int Wtypesign = 0; int Wundef = 0; int Wuninitialized = 1; +int Wunion_cast = 0; int Wuniversal_initializer = 0; int Wunknown_attribute = 0; int Wvla = 1; @@ -866,6 +867,7 @@ static const struct flag warnings[] = { { "typesign", &Wtypesign }, { "undef", &Wundef }, { "uninitialized", &Wuninitialized }, + { "union-cast", &Wunion_cast }, { "universal-initializer", &Wuniversal_initializer }, { "unknown-attribute", &Wunknown_attribute }, { "vla", &Wvla }, diff --git a/options.h b/options.h index 070c0dd87183..40945e287a4a 100644 --- a/options.h +++ b/options.h @@ -128,6 +128,7 @@ extern int Wtransparent_union; extern int Wtypesign; extern int Wundef; extern int Wuninitialized; +extern int Wunion_cast; extern int Wuniversal_initializer; extern int Wunknown_attribute; extern int Wvla; diff --git a/sparse.1 b/sparse.1 index 60203d5a6463..fecd6dd90d42 100644 --- a/sparse.1 +++ b/sparse.1 @@ -451,6 +451,12 @@ The concerned warnings are, for example, those triggered by Sparse does not issue these warnings by default, processing '{\ 0\ }' the same as '{\ }'. . +.TP +.B -Wunion-cast +Warn on casts to union types. + +Sparse does not issues these warnings by default. +. .SH MISC OPTIONS .TP .B \-\-arch=\fIARCH\fR diff --git a/validation/eval/union-cast-no.c b/validation/eval/union-cast-no.c index d06b348d209e..6ba38db8e3f3 100644 --- a/validation/eval/union-cast-no.c +++ b/validation/eval/union-cast-no.c @@ -16,7 +16,6 @@ static union u bar(long l) /* * check-name: union-cast-no * check-command: sparse -Wno-union-cast $file - * check-known-to-fail * * check-error-start eval/union-cast-no.c:13:17: warning: cast to non-scalar diff --git a/validation/eval/union-cast.c b/validation/eval/union-cast.c index 1d8167531081..5bee9e0dc705 100644 --- a/validation/eval/union-cast.c +++ b/validation/eval/union-cast.c @@ -16,7 +16,6 @@ static union u bar(long a) /* * check-name: union-cast * check-command: sparse -Wunion-cast $file - * check-known-to-fail * * check-error-start eval/union-cast.c:8:17: warning: cast to union type