From patchwork Mon Jul 24 20:04:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 793007 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-458801-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="vRzpjDyT"; dkim-atps=neutral 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 3xGWj00Gqhz9s4q for ; Tue, 25 Jul 2017 05:32:03 +1000 (AEST) 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:cc:subject:date:message-id:in-reply-to:references; q=dns; s= default; b=ZhS2E+tI5SKW/i/mBDHV3lZWF6Kugabjm5S5c1IhlJ5cJ0sVzSfeM LXfTB6pVnPbyZfcx65YzPRTEd6328Fih+pCvURVNJtyZbNZq0JEBYvVAAypwNseM MHElfnHrbYo0NmgIBYkK6jUrDZCdnW03DUTeKyQSChpEMRWdN9Oq58= 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:cc:subject:date:message-id:in-reply-to:references; s= default; bh=YQAOJTVgyBj99NkDYiCLz3CrDsE=; b=vRzpjDyTJOdYHNC9fjt2 SZsR7kuK2axOOnfSuMpXaJrg1wJW+4rgW0cpDsoM5shsjkrWmb8l4KwuZvgA4hSv 2qDXp1my1JpgkM6qd7l3u2W1oU8riDWZw1W7SBN449wAmWEb2brcFxq76DvOyzoh ZEkb9tnUezVRVbeBc/t7y3Y= Received: (qmail 80606 invoked by alias); 24 Jul 2017 19:31:08 -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 71995 invoked by uid 89); 24 Jul 2017 19:31:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=amongst, aims X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 24 Jul 2017 19:30:58 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 71DE0883CE for ; Mon, 24 Jul 2017 19:30:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 71DE0883CE Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dmalcolm@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 71DE0883CE Received: from c64.redhat.com (ovpn-112-25.phx2.redhat.com [10.3.112.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id ED4456953A; Mon, 24 Jul 2017 19:30:49 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 01/17] Add param-type-mismatch.c/C testcases as a baseline Date: Mon, 24 Jul 2017 16:04:58 -0400 Message-Id: <1500926714-56988-2-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1500926714-56988-1-git-send-email-dmalcolm@redhat.com> References: <1500926714-56988-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes Amongst other things, this patch kit aims to improve our handling of mismatched types within function calls. For example, given: extern int callee_1 (int one, const char *two, float three); int test_1 (int first, int second, float third) { return callee_1 (first, second, third); } the C++ FE currently reports: error: invalid conversion from 'int' to 'const char*' [-fpermissive] return callee_1 (first, second, third); ^ note: initializing argument 2 of 'int callee_1(int, const char*, float)' extern int callee_1 (int one, const char *two, float three); ^~~~~~~~ when it ought to underline the pertinent parts of the code: error: invalid conversion from 'int' to 'const char*' [-fpermissive] return callee_1 (first, second, third); ^~~~~~ note: initializing argument 2 of 'int callee_1(int, const char*, float)' extern int callee_1 (int one, const char *two, float three); ^~~~~~~~~~~~~~~ The C FE currently does better, underlining the pertinent argument at the callsite (due to the "vec arg_loc" passed around when the call is created); but, like the C++ frontend, it doesn't underline the pertinent parameter at the decl of the callee. This patch adds a pair of test cases to exercise various cases of this in the C and C++ frontends, to establish a baseline for how we currently handle these; the "TODO" comments in the test cases note the aspects where we could do better (some of which are fixed by this kit). Patches 6 and 7 of the kit fix the parameter highlighting, for C and C++ respectively (making use of -fblt). gcc/testsuite/ChangeLog: * g++.dg/diagnostic/param-type-mismatch.C: New test acse. * gcc.dg/param-type-mismatch.c: New test case. --- .../g++.dg/diagnostic/param-type-mismatch.C | 162 +++++++++++++++++++++ gcc/testsuite/gcc.dg/param-type-mismatch.c | 63 ++++++++ 2 files changed, 225 insertions(+) create mode 100644 gcc/testsuite/g++.dg/diagnostic/param-type-mismatch.C create mode 100644 gcc/testsuite/gcc.dg/param-type-mismatch.c diff --git a/gcc/testsuite/g++.dg/diagnostic/param-type-mismatch.C b/gcc/testsuite/g++.dg/diagnostic/param-type-mismatch.C new file mode 100644 index 0000000..864ead1 --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/param-type-mismatch.C @@ -0,0 +1,162 @@ +// { dg-options "-fdiagnostics-show-caret" } + +/* A collection of calls where argument 2 is of the wrong type. + + TODO: we should put the caret and underline for the diagnostic + at the second argument, rather than the close paren. + + TODO: we should highlight the second parameter of the callee, rather + than its name. */ + +/* decl, with argname. */ + +extern int callee_1 (int one, const char *two, float three); // { dg-line callee_1 } + +int test_1 (int first, int second, float third) +{ + return callee_1 (first, second, third); // { dg-error "invalid conversion from 'int' to 'const char\\*'" } + /* { dg-begin-multiline-output "" } + return callee_1 (first, second, third); + ^ + { dg-end-multiline-output "" } */ + // { dg-message "initializing argument 2 of 'int callee_1\\(int, const char\\*, float\\)'" "" { target *-*-* } callee_1 } + /* { dg-begin-multiline-output "" } + extern int callee_1 (int one, const char *two, float three); + ^~~~~~~~ + { dg-end-multiline-output "" } */ +} + +/* decl, without argname. */ + +extern int callee_2 (int, const char *, float); // { dg-line callee_2 } + +int test_2 (int first, int second, float third) +{ + return callee_2 (first, second, third); // { dg-error "invalid conversion from 'int' to 'const char\\*'" } + /* { dg-begin-multiline-output "" } + return callee_2 (first, second, third); + ^ + { dg-end-multiline-output "" } */ + // { dg-message "initializing argument 2 of 'int callee_2\\(int, const char\\*, float\\)'" "" { target *-*-* } callee_2 } + /* { dg-begin-multiline-output "" } + extern int callee_2 (int, const char *, float); + ^~~~~~~~ + { dg-end-multiline-output "" } */ +} + +/* defn, with argname. */ + +static int callee_3 (int one, const char *two, float three) // { dg-line callee_3 } +{ + return callee_2 (one, two, three); +} + +int test_3 (int first, int second, float third) +{ + return callee_3 (first, second, third); // { dg-error "invalid conversion from 'int' to 'const char\\*'" } + /* { dg-begin-multiline-output "" } + return callee_3 (first, second, third); + ^ + { dg-end-multiline-output "" } */ + // { dg-message "initializing argument 2 of 'int callee_3\\(int, const char\\*, float\\)'" "" { target *-*-* } callee_3 } + /* { dg-begin-multiline-output "" } + static int callee_3 (int one, const char *two, float three) + ^~~~~~~~ + { dg-end-multiline-output "" } */ +} + +/* static member, with argname. */ + +struct s4 { static int member_1 (int one, const char *two, float three); }; + +int test_4 (int first, int second, float third) +{ + return s4::member_1 (first, second, third); // { dg-error "invalid conversion from 'int' to 'const char\\*'" } + /* { dg-begin-multiline-output "" } + return s4::member_1 (first, second, third); + ^ + { dg-end-multiline-output "" } */ + /* { dg-begin-multiline-output "" } + struct s4 { static int member_1 (int one, const char *two, float three); }; + ^~~~~~~~ + { dg-end-multiline-output "" } */ +} + +/* non-static member, with argname. */ + +struct s5 { int member_1 (int one, const char *two, float three); }; + +int test_5 (int first, int second, float third) +{ + s5 inst; + return inst.member_1 (first, second, third); // { dg-error "invalid conversion from 'int' to 'const char\\*'" } + /* { dg-begin-multiline-output "" } + return inst.member_1 (first, second, third); + ^ + { dg-end-multiline-output "" } */ + /* { dg-begin-multiline-output "" } + struct s5 { int member_1 (int one, const char *two, float three); }; + ^~~~~~~~ + { dg-end-multiline-output "" } */ +} + +/* Template function. */ + +template +int test_6 (int one, T two, float three); + +int test_6 (int first, int second, float third) +{ + return test_6 (first, second, third); // { dg-error "no matching function" } + /* { dg-begin-multiline-output "" } + return test_6 (first, second, third); + ^ + { dg-end-multiline-output "" } */ + /* { dg-begin-multiline-output "" } + return test_6 (first, second, third); + ^ + { dg-end-multiline-output "" } */ + /* { dg-begin-multiline-output "" } + int test_6 (int one, T two, float three); + ^~~~~~ + { dg-end-multiline-output "" } */ +} + +/* Template class, static function. */ + +template +struct s7 { static int member_1 (int one, T two, float three); }; + +int test_7 (int first, int second, float third) +{ + return s7 ::member_1 (first, second, third); // { dg-error "invalid conversion from 'int' to 'const char\\*'" } + /* { dg-begin-multiline-output "" } + return s7 ::member_1 (first, second, third); + ^ + { dg-end-multiline-output "" } */ + /* { dg-begin-multiline-output "" } + struct s7 { static int member_1 (int one, T two, float three); }; + ^~~~~~~~ + { dg-end-multiline-output "" } */ +} + +/* Template class, non-static function. */ + +template +struct s8 { int member_1 (int one, T two, float three); }; + +int test_8 (int first, int second, float third) +{ + s8 inst; + return inst.member_1 (first, second, third); // { dg-error "invalid conversion from 'int' to 'const char\\*'" } + /* { dg-begin-multiline-output "" } + return inst.member_1 (first, second, third); + ^ + { dg-end-multiline-output "" } */ + /* { dg-begin-multiline-output "" } + struct s8 { int member_1 (int one, T two, float three); }; + ^~~~~~~~ + { dg-end-multiline-output "" } */ +} + +// TODO: template callsite diff --git a/gcc/testsuite/gcc.dg/param-type-mismatch.c b/gcc/testsuite/gcc.dg/param-type-mismatch.c new file mode 100644 index 0000000..70ea0bc --- /dev/null +++ b/gcc/testsuite/gcc.dg/param-type-mismatch.c @@ -0,0 +1,63 @@ +/* { dg-options "-fdiagnostics-show-caret" } */ + +/* A collection of calls where argument 2 is of the wrong type. + + TODO: we should highlight the second parameter of the callee, rather + than its name. */ + +/* decl, with argname. */ + +extern int callee_1 (int one, const char *two, float three); /* { dg-line callee_1 } */ + +int test_1 (int first, int second, float third) +{ + return callee_1 (first, second, third); /* { dg-warning "passing argument 2 of 'callee_1' makes pointer from integer without a cast" } */ + /* { dg-begin-multiline-output "" } + return callee_1 (first, second, third); + ^~~~~~ + { dg-end-multiline-output "" } */ + /* { dg-message "expected 'const char \\*' but argument is of type 'int'" "" { target *-*-* } callee_1 } */ + /* { dg-begin-multiline-output "" } + extern int callee_1 (int one, const char *two, float three); + ^~~~~~~~ + { dg-end-multiline-output "" } */ +} + +/* decl, without argname. */ + +extern int callee_2 (int, const char *, float); /* { dg-line callee_2 } */ + +int test_2 (int first, int second, float third) +{ + return callee_2 (first, second, third); /* { dg-warning "passing argument 2 of 'callee_2' makes pointer from integer without a cast" } */ + /* { dg-begin-multiline-output "" } + return callee_2 (first, second, third); + ^~~~~~ + { dg-end-multiline-output "" } */ + /* { dg-message "expected 'const char \\*' but argument is of type 'int'" "" { target *-*-* } callee_2 } */ + /* { dg-begin-multiline-output "" } + extern int callee_2 (int, const char *, float); + ^~~~~~~~ + { dg-end-multiline-output "" } */ +} + +/* defn, with argname. */ + +static int callee_3 (int one, const char *two, float three) /* { dg-line callee_3 } */ +{ + return callee_2 (one, two, three); +} + +int test_3 (int first, int second, float third) +{ + return callee_3 (first, second, third); // { dg-warning "passing argument 2 of 'callee_3' makes pointer from integer without a cast" } + /* { dg-begin-multiline-output "" } + return callee_3 (first, second, third); + ^~~~~~ + { dg-end-multiline-output "" } */ + /* { dg-message "expected 'const char \\*' but argument is of type 'int'" "" { target *-*-* } callee_3 } */ + /* { dg-begin-multiline-output "" } + static int callee_3 (int one, const char *two, float three) + ^~~~~~~~ + { dg-end-multiline-output "" } */ +}