From f268377c4dd7339c89cf2013a295ed82af5a949d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Wed, 3 Apr 2019 01:01:33 -0300 Subject: [PATCH 1/3] [go] Fix grammar around escape characters - Fixes #1751 --- .../sourceforge/pmd/lang/go/antlr4/Golang.g4 | 4 +-- .../pmd/cpd/EdgeCasesTokenizerTest.java | 25 +++++++++++++++++++ .../net/sourceforge/pmd/cpd/issue1751.go | 6 +++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java create mode 100644 pmd-go/src/test/resources/net/sourceforge/pmd/cpd/issue1751.go diff --git a/pmd-go/src/main/antlr4/net/sourceforge/pmd/lang/go/antlr4/Golang.g4 b/pmd-go/src/main/antlr4/net/sourceforge/pmd/lang/go/antlr4/Golang.g4 index ec54cbac43..673c0b6682 100644 --- a/pmd-go/src/main/antlr4/net/sourceforge/pmd/lang/go/antlr4/Golang.g4 +++ b/pmd-go/src/main/antlr4/net/sourceforge/pmd/lang/go/antlr4/Golang.g4 @@ -917,10 +917,10 @@ RUNE_LIT //unicode_value = unicode_char | little_u_value | big_u_value | escaped_char . fragment UNICODE_VALUE - : UNICODE_CHAR - | LITTLE_U_VALUE + : LITTLE_U_VALUE | BIG_U_VALUE | ESCAPED_CHAR + | UNICODE_CHAR ; //byte_value = octal_byte_value | hex_byte_value . diff --git a/pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java b/pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java new file mode 100644 index 0000000000..e3ca7d0e58 --- /dev/null +++ b/pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java @@ -0,0 +1,25 @@ + +package net.sourceforge.pmd.cpd; + +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; + +public class EdgeCasesTokenizerTest { + + private String getSampleCode(final String filename) throws IOException { + return IOUtils.toString(GoTokenizer.class.getResourceAsStream(filename)); + } + + @Test + public void testEscapedBackSlash() throws IOException { + // See https://github.com/pmd/pmd/issues/1751 + final String filename = "issue1751.go"; + final GoTokenizer tokenizer = new GoTokenizer(); + final SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getSampleCode(filename), filename)); + + final Tokens tokens = new Tokens(); + tokenizer.tokenize(sourceCode, tokens); // it should simply not fail + } +} diff --git a/pmd-go/src/test/resources/net/sourceforge/pmd/cpd/issue1751.go b/pmd-go/src/test/resources/net/sourceforge/pmd/cpd/issue1751.go new file mode 100644 index 0000000000..1e0042e68a --- /dev/null +++ b/pmd-go/src/test/resources/net/sourceforge/pmd/cpd/issue1751.go @@ -0,0 +1,6 @@ +func test(in *Value, param *Value) (*Value, *Error) { + output := strings.Replace(in.String(), "\\", "\\\\", -1) + output = strings.Replace(output, "\"", "\\\"", -1) + output = strings.Replace(output, "'", "\\'", -1) + return AsValue(output), nil +} From 3aea3cfe2979491b35a562d8c50896065cbb5570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Wed, 3 Apr 2019 01:02:54 -0300 Subject: [PATCH 2/3] Update changelog, refs #1751 --- docs/pages/release_notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 8b53655c31..5e8fe25beb 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -16,6 +16,8 @@ This is a {{ site.pmd.release_type }} release. ### Fixed Issues +* go + * [#1751](https://github.com/pmd/pmd/issues/1751): \[go] Parsing errors encountered with escaped backslash * java * [#1729](https://github.com/pmd/pmd/issues/1729): \[java] JavaRuleViolation loses information in `className` field when class has package-private access level * java-bestpractices From 2e8056dc2877d6499f2d614234f59ef3e62fc8b1 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 5 Apr 2019 14:10:57 +0200 Subject: [PATCH 3/3] Fix checkstyle --- .../net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java b/pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java index e3ca7d0e58..1d56e989f0 100644 --- a/pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java +++ b/pmd-go/src/test/java/net/sourceforge/pmd/cpd/EdgeCasesTokenizerTest.java @@ -1,7 +1,11 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ package net.sourceforge.pmd.cpd; import java.io.IOException; +import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; import org.junit.Test; @@ -9,9 +13,9 @@ import org.junit.Test; public class EdgeCasesTokenizerTest { private String getSampleCode(final String filename) throws IOException { - return IOUtils.toString(GoTokenizer.class.getResourceAsStream(filename)); + return IOUtils.toString(GoTokenizer.class.getResourceAsStream(filename), StandardCharsets.UTF_8); } - + @Test public void testEscapedBackSlash() throws IOException { // See https://github.com/pmd/pmd/issues/1751