From 6d828833c0cf6d8131b8baf32416610dc90d986d Mon Sep 17 00:00:00 2001 From: Maikel Steneker Date: Fri, 10 May 2019 14:32:36 +0200 Subject: [PATCH 1/4] Added unit test for C++14 digit separators. --- .../net/sourceforge/pmd/cpd/CPPTokenizerTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java b/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java index 4bffb5208d..f6a8dc11fd 100644 --- a/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java +++ b/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java @@ -159,6 +159,18 @@ public class CPPTokenizerTest { tokenizer.tokenize(code, new Tokens()); } + @Test + public void testDigitSeparators() { + final String code = "auto integer_literal = 1'000'000;" + PMD.EOL + + "auto floating_point_literal = 0.000'015'3;" + PMD.EOL + + "auto hex_literal = 0x0F00'abcd'6f3d;" + PMD.EOL + + "auto silly_example = 1'0'0'000'00;"; + Tokens tokens = parse(code); + System.out.println(tokens.getTokens()); + assertTrue(TokenEntry.getEOF() != tokens.getTokens().get(0)); + assertEquals(21, tokens.size()); + } + private Tokens parse(String snippet) { try { return parse(snippet, false, new Tokens()); From af41fc4230f7b5540b84033ad683fe59c2b65b96 Mon Sep 17 00:00:00 2001 From: Maikel Steneker Date: Fri, 10 May 2019 14:33:22 +0200 Subject: [PATCH 2/4] Added support for C++14 digit separators. --- pmd-cpp/etc/grammar/cpp.jj | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pmd-cpp/etc/grammar/cpp.jj b/pmd-cpp/etc/grammar/cpp.jj index a39effe982..ed44b6fcd8 100644 --- a/pmd-cpp/etc/grammar/cpp.jj +++ b/pmd-cpp/etc/grammar/cpp.jj @@ -284,27 +284,31 @@ TOKEN : TOKEN [IGNORE_CASE] : { - < OCTALINT : "0" (["0"-"7"])* > + < OCTALINT : "0" (["'", "0"-"7"])* > | < OCTALLONG : "l" > | < UNSIGNED_OCTALINT : "u" > | < UNSIGNED_OCTALLONG : ("ul" | "lu") > -| < DECIMALINT : ["1"-"9"] (["0"-"9"])* > +| < #DECIMALDIGIT : ["'", "0"-"9"] > + +| < DECIMALINT : ["1"-"9"] ()* > | < DECIMALLONG : ["u","l"] > | < UNSIGNED_DECIMALINT : "u" > | < UNSIGNED_DECIMALLONG : ("ul" | "lu") > -| < HEXADECIMALINT : "0x" (["0"-"9","a"-"f"])+ > +| < HEXADECIMALINT : "0x" ( | ["a"-"f"])+ > | < HEXADECIMALLONG : (["u","l"])? > | < UNSIGNED_HEXADECIMALINT : "u" > | < UNSIGNED_HEXADECIMALLONG : ("ul" | "lu") > -| < FLOATONE : ((["0"-"9"])+ "." (["0"-"9"])* | (["0"-"9"])* "." (["0"-"9"])+) - ("e" (["-","+"])? (["0"-"9"])+)? (["f","l"])? > +| < FLOATONE : (["0"-"9"]()* "." + | "." ()+ + | ["0"-"9"]()* "." ()+) + ("e" (["-","+"])? ()+)? (["f","l"])? > -| < FLOATTWO : (["0"-"9"])+ "e" (["-","+"])? (["0"-"9"])+ (["f","l"])? > +| < FLOATTWO : ["0"-"9"]()* "e" (["-","+"])? ()+ (["f","l"])? > } TOKEN : From 44ca6d307e55081b3e14f4b7022e2d8f4c2ecd74 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sun, 12 May 2019 19:25:08 +0200 Subject: [PATCH 3/4] Improve unit test --- .../src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java b/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java index f6a8dc11fd..1796852fda 100644 --- a/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java +++ b/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java @@ -166,8 +166,8 @@ public class CPPTokenizerTest { + "auto hex_literal = 0x0F00'abcd'6f3d;" + PMD.EOL + "auto silly_example = 1'0'0'000'00;"; Tokens tokens = parse(code); - System.out.println(tokens.getTokens()); assertTrue(TokenEntry.getEOF() != tokens.getTokens().get(0)); + assertEquals("1'000'000", tokens.getTokens().get(3).toString()); assertEquals(21, tokens.size()); } From 615ebb6fe3af37a062ec925569384d0327f078fa Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sun, 12 May 2019 19:25:23 +0200 Subject: [PATCH 4/4] Update release notes, refs #1819 --- docs/pages/release_notes.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 0c717925c2..d5226fd6a6 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -29,6 +29,14 @@ Being based on a proper Antlr grammar, CPD can: This PMD release ships a new version of the pmd-designer. For the changes, see [PMD Designer Changelog](https://github.com/pmd/pmd-designer/blob/6.14.0/CHANGELOG.md). +#### Enhanced C++ support + +CPD now supports digit separators in C++ (language module "cpp"). This is a C++14 feature. + +Example: `auto integer_literal = 1'000'000;` + +The single quotes can be used to add some structure to large numbers. + ### Modified Rules * The Java rule {% rule "java/errorprone/AssignmentToNonFinalStatic" %} (`java-errorprone`) will now report on each @@ -73,6 +81,7 @@ No changes. * [#1781](https://github.com/pmd/pmd/pull/1781): \[java] Location change in AssignmentToNonFinalStatic - [Maikel Steneker](https://github.com/maikelsteneker) * [#1789](https://github.com/pmd/pmd/pull/1789): \[cpd] \[core] Use current classloader instead of Thread's classloader - [Andreas Schmid](https://github.com/aaschmid) * [#1791](https://github.com/pmd/pmd/pull/1791): \[dart] \[cpd] Dart escaped string - [Maikel Steneker](https://github.com/maikelsteneker) +* [#1819](https://github.com/pmd/pmd/pull/1819): \[cpp] \[cpd] Add support for digit separators - [Maikel Steneker](https://github.com/maikelsteneker) {% endtocmaker %}