From patchwork Tue Aug 10 14:43:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tassilo Horn X-Patchwork-Id: 12429049 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA5CCC4338F for ; Tue, 10 Aug 2021 14:44:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CFCF160F02 for ; Tue, 10 Aug 2021 14:44:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240152AbhHJOob (ORCPT ); Tue, 10 Aug 2021 10:44:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237554AbhHJOoa (ORCPT ); Tue, 10 Aug 2021 10:44:30 -0400 Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8892C0613D3 for ; Tue, 10 Aug 2021 07:44:07 -0700 (PDT) Received: from fencepost.gnu.org ([2001:470:142:3::e]:51830) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mDSz7-0001T4-TK for git@vger.kernel.org; Tue, 10 Aug 2021 10:44:05 -0400 Received: from auth2-smtp.messagingengine.com ([66.111.4.228]:33117) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mDSz7-00053b-NP; Tue, 10 Aug 2021 10:44:05 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailauth.nyi.internal (Postfix) with ESMTP id 4E61827C0054; Tue, 10 Aug 2021 10:44:05 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 10 Aug 2021 10:44:05 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrjeelgdekudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhephffvufffkffoggfgsedtkeertdertd dtnecuhfhrohhmpefvrghsshhilhhoucfjohhrnhcuoehtshguhhesghhnuhdrohhrgheq necuggftrfgrthhtvghrnhepfedvleeugfdvgfejueetgfekffelffduveegffegudefte ekhfffieevkeetjeeinecuffhomhgrihhnpehuthhilhdrmhgrphenucevlhhushhtvghr ufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehthhhorhhnodhmvghsmhhtph gruhhthhhpvghrshhonhgrlhhithihqdekieejfeekjeekgedqieefhedvleekqdhtshgu hheppehgnhhurdhorhhgsehfrghsthhmrghilhdrfhhm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 10 Aug 2021 10:44:04 -0400 (EDT) From: Tassilo Horn To: git@vger.kernel.org Cc: Tassilo Horn Subject: [PATCH v3] userdiff: improve java regex for array, qualified, and generic return types Date: Tue, 10 Aug 2021 16:43:58 +0200 Message-Id: <20210810144358.235603-1-tsdh@gnu.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Currently, the git diff hunk headers show the wrong method signature if the method has a qualified return type, an array return type, or a generic return type because the regex doesn't allow dots (.), [], or < and > in the return type. Also, type parameter declarations couldn't be matched. Add several t4018 tests asserting the right hunk headers for increasingly complex method signatures: public String[] secondMethod(String[] RIGHT) public List secondMethod(String[] RIGHT) public List secondMethod(T[] RIGHT) public Map secondMethod(String[] RIGHT) public java.util.Map> secondMethod(String[] RIGHT) Signed-off-by: Tassilo Horn --- t/t4018/java-return-array | 10 ++++++++++ t/t4018/java-return-generic | 10 ++++++++++ t/t4018/java-return-generic2 | 10 ++++++++++ t/t4018/java-return-generic3 | 10 ++++++++++ t/t4018/java-return-generic4 | 10 ++++++++++ userdiff.c | 10 +++++++++- 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 t/t4018/java-return-array create mode 100644 t/t4018/java-return-generic create mode 100644 t/t4018/java-return-generic2 create mode 100644 t/t4018/java-return-generic3 create mode 100644 t/t4018/java-return-generic4 diff --git a/t/t4018/java-return-array b/t/t4018/java-return-array new file mode 100644 index 0000000000..fc8882a5a9 --- /dev/null +++ b/t/t4018/java-return-array @@ -0,0 +1,10 @@ +class MyExample { + public void firstMethod() { + // Whatever... + } + + public String[] secondMethod(String[] RIGHT) { + // Whatever... + return new; // ChangeMe + } +} diff --git a/t/t4018/java-return-generic b/t/t4018/java-return-generic new file mode 100644 index 0000000000..7ea7bf8f07 --- /dev/null +++ b/t/t4018/java-return-generic @@ -0,0 +1,10 @@ +class MyExample { + public void firstMethod() { + // Whatever... + } + + public List secondMethod(String[] RIGHT) { + // Whatever... + return Arrays.asList("ChangeMe"); + } +} diff --git a/t/t4018/java-return-generic2 b/t/t4018/java-return-generic2 new file mode 100644 index 0000000000..d2d2ab7f37 --- /dev/null +++ b/t/t4018/java-return-generic2 @@ -0,0 +1,10 @@ +class MyExample { + public void firstMethod() { + // Whatever... + } + + public List secondMethod(T[] RIGHT) { + // Whatever... + return (List) Arrays.asList("ChangeMe"); + } +} diff --git a/t/t4018/java-return-generic3 b/t/t4018/java-return-generic3 new file mode 100644 index 0000000000..3db1a1ddba --- /dev/null +++ b/t/t4018/java-return-generic3 @@ -0,0 +1,10 @@ +class MyExample { + public void firstMethod() { + // Whatever... + } + + public Map secondMethod(String[] RIGHT) { + // Whatever... + return new java.util.HashMap<>(); // ChangeMe + } +} diff --git a/t/t4018/java-return-generic4 b/t/t4018/java-return-generic4 new file mode 100644 index 0000000000..59f2a50dd2 --- /dev/null +++ b/t/t4018/java-return-generic4 @@ -0,0 +1,10 @@ +class MyExample { + public void firstMethod() { + // Whatever... + } + + public java.util.Map> secondMethod(String[] RIGHT) { + // Whatever... + return new java.util.HashMap<>(); // ChangeMe + } +} diff --git a/userdiff.c b/userdiff.c index 3c3bbe38b0..ded42945c3 100644 --- a/userdiff.c +++ b/userdiff.c @@ -142,7 +142,15 @@ PATTERNS("html", "[^<>= \t]+"), PATTERNS("java", "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n" - "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$", + /* Method signatures contain: */ + /* modifiers: public static */ + "^[ \t]*(([A-Za-z_][A-Za-z]*[ \t]+)*" + /* optionally type parameters: */ + "(<[A-Za-z0-9_, \t]+>[ \t]+)?" + /* a return type: java.util.Map */ + "([A-Za-z_]([A-Za-z_0-9<>,.]|\\[[ \t]*\\])*[ \t]+)+" + /* the method name followed by the parameter list: myMethod(...) */ + "[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$", /* -- */ "[a-zA-Z_][a-zA-Z0-9_]*" "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"