Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Anton Kot
2019-07-16 17:21:36 +02:00
20 changed files with 594 additions and 12 deletions

View File

@ -12,7 +12,7 @@ unnecessary object creation, and so forth. It supports Java, JavaScript, Salesfo
XML, XSL.
Additionally it includes **CPD**, the copy-paste-detector. CPD finds duplicated code in
C/C++, C#, Dart, Fortran, Go, Groovy, Java, JavaScript, JSP, Kotlin, Matlab,
C/C++, C#, Dart, Fortran, Go, Groovy, Java, JavaScript, JSP, Kotlin, Lua, Matlab,
Objective-C, Perl, PHP, PLSQL, Python, Ruby, Salesforce.com Apex, Scala, Swift and Visualforce.
## Source and Documentation

View File

@ -45,7 +45,7 @@ author: Tom Copeland <tom@infoether.org>
PMD/CPD analyses of Azureus and Columba.
* April 2006 - John Ferguson Smart's article "PMD Squashes Code Bugs" on
[DevX](http://www.devx.com/Java/Article/31286) discusses PMD and the Eclipse plugin. Lots of screenshots!
[DevX](https://web.archive.org/web/20140214143838/http://www.devx.com/Java/Article/31286) discusses PMD and the Eclipse plugin. Lots of screenshots!
* November 2005 - Mike Clark's article "Staying Out of Code Debt" on
[StickyMinds](http://www.stickyminds.com/sitewide.asp?Function=edetail&amp;ObjectType=ART&amp;ObjectId=9860&amp;tth=DYN&amp;tt=siteemail&amp;iDyn=2)
@ -63,7 +63,7 @@ author: Tom Copeland <tom@infoether.org>
[Doctor Dobb's Journal](http://www.drdobbs.com/benefits-of-the-build/184415286) mentions PMD as a way
to automate code reviews
* February 2005 - [Java Is Well-Suited for Open-Source Projects](http://www.eweek.com/c/a/Application-Development/Java-Is-WellSuited-for-OpenSource-Projects/) -
* February 2005 - [Java Is Well-Suited for Open-Source Projects](https://webcache.googleusercontent.com/search?q=cache:aEL-9Ncx2RgJ:https://www.eweek.com/development/java-is-well-suited-for-open-source-projects) -
Peter Coffee's eWeek article on open source, Java, and PMD
* January 2005 - [Zap bugs with PMD](http://www.ibm.com/developerworks/java/library/j-pmd/) - Elliotte Rusty

View File

@ -11,7 +11,7 @@ folder: pmd/rules
{% include callout.html content="Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors." %}
* [MistypedCDATASection](pmd_rules_xml_errorprone.html#mistypedcdatasection): An XML CDATA section begins with a &lt;!CDATA[ marker, which has only one [, and ends with a ]]&gt; mar...
* [MistypedCDATASection](pmd_rules_xml_errorprone.html#mistypedcdatasection): An XML CDATA section begins with a &lt;![CDATA[ marker, which has only one [, and ends with a ]]&gt; ma...
## Additional rulesets

View File

@ -15,7 +15,7 @@ language: XML
**Priority:** Medium (3)
An XML CDATA section begins with a &lt;!CDATA[ marker, which has only one [, and ends with a ]]&gt; marker, which has only two ].
An XML CDATA section begins with a &lt;![CDATA[ marker, which has only one [, and ends with a ]]&gt; marker, which has two ].
**This rule is defined by the following XPath expression:**
``` xpath
@ -25,7 +25,18 @@ An XML CDATA section begins with a &lt;!CDATA[ marker, which has only one [, and
**Example(s):**
``` xml
An extra [ looks like &lt;!CDATA[[]]&gt;, and an extra ] looks like &lt;!CDATA[]]]&gt;.
<root>
<child>
<![CDATA[[ character data ]]> - this cdata section is valid, but it contains an
additional square bracket at the beginning.
It should probably be just <![CDATA[ character data ]]>.
</child>
<child>
<![CDATA[ character data ]]]> - this cdata section is valid, but it contains an
additional square bracket in the end.
It should probably be just <![CDATA[ character data ]]>.
</child>
</root>
```
**Use this rule by referencing it:**

View File

@ -218,6 +218,7 @@ This behavior has been introduced to ease CPD integration into scripts or hooks,
* Java
* Jsp
* Kotlin
* Lua
* Matlab
* Objective-C
* Perl
@ -366,7 +367,7 @@ Here's a screenshot of CPD after running on the JDK 8 java.lang package:
## Suppression
Arbitrary blocks of code can be ignored through comments on **Java**, **C/C++**, **Dart**, **Go**, **Javascript**,
**Kotlin**, **Matlab**, **Objective-C**, **PL/SQL**, **Python** and **Swift** by including the keywords `CPD-OFF` and `CPD-ON`.
**Kotlin**, **Lua**, **Matlab**, **Objective-C**, **PL/SQL**, **Python** and **Swift** by including the keywords `CPD-OFF` and `CPD-ON`.
```java
public Object someParameterizedFactoryMethod(int x) throws Exception {

View File

@ -14,17 +14,30 @@ This is a {{ site.pmd.release_type }} release.
### New and noteworthy
#### Lua support
Thanks to the contribution from [Maikel Steneker](https://github.com/maikelsteneker), and built on top of the ongoing efforts to fully support Antlr-based languages,
PMD now has CPD support for [Lua](https://www.lua.org/).
Being based on a proper Antlr grammar, CPD can:
* ignore comments
* honor [comment-based suppressions](pmd_userdocs_cpd.html#suppression)
### Fixed Issues
* doc
* [#1896](https://github.com/pmd/pmd/issues/1896): \[doc] Error in changelog 6.16.0 due to not properly closed rule tag
* [#1906](https://github.com/pmd/pmd/issues/1906): \[doc] Broken link for adding own CPD languages
* [#1909](https://github.com/pmd/pmd/issues/1909): \[doc] Sample usage example refers to deprecated ruleset "basic.xml" instead of "quickstart.xml"
* xml
* [#1666](https://github.com/pmd/pmd/issues/1666): \[xml] wrong cdata rule description and examples
### API Changes
### External Contributions
* [#1869](https://github.com/pmd/pmd/pull/1869): \[xml] fix #1666 wrong cdata rule description and examples - [Artem](https://github.com/KroArtem)
* [#1892](https://github.com/pmd/pmd/pull/1892): \[lua] \[cpd] Added CPD support for Lua - [Maikel Steneker](https://github.com/maikelsteneker)
* [#1908](https://github.com/pmd/pmd/pull/1908): \[doc] Update ruleset filename from deprecated basic.xml to quickstart.xml - [crunsk](https://github.com/crunsk)
{% endtocmaker %}

View File

@ -127,6 +127,11 @@
<artifactId>pmd-groovy</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-lua</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>

View File

@ -112,7 +112,7 @@ public class BinaryDistributionIT {
result = CpdExecutor.runCpd(tempDir, "-h");
result.assertExecutionResult(1, "Supported languages: [apex, cpp, cs, dart, ecmascript, fortran, go, groovy, java, jsp, kotlin, matlab, objectivec, perl, php, plsql, python, ruby, scala, swift, vf]");
result.assertExecutionResult(1, "Supported languages: [apex, cpp, cs, dart, ecmascript, fortran, go, groovy, java, jsp, kotlin, lua, matlab, objectivec, perl, php, plsql, python, ruby, scala, swift, vf]");
result = CpdExecutor.runCpd(tempDir, "--minimum-tokens", "10", "--format", "text", "--files", srcDir);
result.assertExecutionResult(4, "Found a 10 line (55 tokens) duplication in the following files:");

57
pmd-lua/pom.xml Normal file
View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>pmd-lua</artifactId>
<name>PMD Lua</name>
<parent>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd</artifactId>
<version>6.17.0-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>${*}</delimiter>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,18 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
/**
* Language implementation for Lua
*/
public class LuaLanguage extends AbstractLanguage {
/**
* Creates a new Lua Language instance.
*/
public LuaLanguage() {
super("Lua", "lua", new LuaTokenizer(), ".lua");
}
}

View File

@ -0,0 +1,28 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import org.antlr.v4.runtime.CharStream;
import net.sourceforge.pmd.cpd.token.AntlrTokenFilter;
import net.sourceforge.pmd.lang.antlr.AntlrTokenManager;
import net.sourceforge.pmd.lang.lua.antlr4.LuaLexer;
/**
* The Lua Tokenizer
*/
public class LuaTokenizer extends AntlrTokenizer {
@Override
protected AntlrTokenManager getLexerForSource(SourceCode sourceCode) {
CharStream charStream = AntlrTokenizer.getCharStreamFromSourceCode(sourceCode);
return new AntlrTokenManager(new LuaLexer(charStream), sourceCode.getFileName());
}
@Override
protected AntlrTokenFilter getTokenFilter(final AntlrTokenManager tokenManager) {
return new AntlrTokenFilter(tokenManager);
}
}

View File

@ -0,0 +1 @@
net.sourceforge.pmd.cpd.LuaLanguage

View File

@ -0,0 +1,56 @@
/**
* 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 java.util.Arrays;
import java.util.Collection;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
@RunWith(Parameterized.class)
public class LuaTokenizerTest extends AbstractTokenizerTest {
private final String filename;
private final int nExpectedTokens;
public LuaTokenizerTest(String filename, int nExpectedTokens) {
this.filename = filename;
this.nExpectedTokens = nExpectedTokens;
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[] { "factorial.lua", 44 },
new Object[] { "helloworld.lua", 5 }
);
}
@Before
@Override
public void buildTokenizer() throws IOException {
this.tokenizer = new LuaTokenizer();
this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), this.filename));
}
@Override
public String getSampleCode() throws IOException {
return IOUtils.toString(LuaTokenizer.class.getResourceAsStream(this.filename), StandardCharsets.UTF_8);
}
@Test
public void tokenizeTest() throws IOException {
this.expectedTokenCount = nExpectedTokens;
super.tokenizeTest();
}
}

View File

@ -0,0 +1,13 @@
-- defines a factorial function
function fact (n)
if n == 0 then
return 1
else
return n * fact(n-1)
end
end
print("enter a number:")
a = io.read("*number") -- read a number
print(fact(a))

View File

@ -0,0 +1,2 @@
print("Hello World")

View File

@ -12,11 +12,11 @@ Rules to detect constructs that are either broken, extremely confusing or prone
<rule name="MistypedCDATASection"
language="xml"
since="5.0"
message="Potentialy mistyped CDATA section with extra [ at beginning or ] at the end."
message="Potentially mistyped CDATA section with extra [ at beginning or ] at the end."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_xml_errorprone.html#mistypedcdatasection">
<description>
An XML CDATA section begins with a &lt;!CDATA[ marker, which has only one [, and ends with a ]]&gt; marker, which has only two ].
An XML CDATA section begins with a &lt;![CDATA[ marker, which has only one [, and ends with a ]]&gt; marker, which has two ].
</description>
<priority>3</priority>
<properties>
@ -30,7 +30,18 @@ An XML CDATA section begins with a &lt;!CDATA[ marker, which has only one [, and
</properties>
<example>
<![CDATA[
An extra [ looks like &lt;!CDATA[[]]&gt;, and an extra ] looks like &lt;!CDATA[]]]&gt;.
<root>
<child>
<![CDATA[[ character data ]]><![CDATA[]]]]><![CDATA[> - this cdata section is valid, but it contains an
additional square bracket at the beginning.
It should probably be just <![CDATA[ character data ]]><![CDATA[]]]]><![CDATA[>.
</child>
<child>
<![CDATA[ character data ]]]><![CDATA[]]]]><![CDATA[> - this cdata section is valid, but it contains an
additional square bracket in the end.
It should probably be just <![CDATA[ character data ]]><![CDATA[]]]]><![CDATA[>.
</child>
</root>
]]>
</example>
</rule>

View File

@ -15,6 +15,6 @@ public class PMDTaskTest extends AbstractAntTestHelper {
@Test
public void testXML() {
executeTarget("testXML");
assertOutputContaining("Potentialy mistyped CDATA section with extra [ at beginning or ] at the end.");
assertOutputContaining("Potentially mistyped CDATA section with extra [ at beginning or ] at the end.");
}
}

View File

@ -70,4 +70,34 @@
]]></code>
<source-type>xml</source-type>
</test-code>
<test-code>
<description>Example code</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>3,8</expected-linenumbers>
<code><![CDATA[
<root>
<child>
<![CDATA[ character data ]]]><![CDATA[]]]]><![CDATA[> - this cdata section is valid, but it contains an
additional square bracket in the end.
It should probably be just <![CDATA[ character data ]]><![CDATA[]]]]><![CDATA[>.
</child>
<child>
<![CDATA[[ character data ]]><![CDATA[]]]]><![CDATA[> - this cdata section is valid, but it contains an
additional square bracket at the beginning.
It should probably be just <![CDATA[ character data ]]><![CDATA[]]]]><![CDATA[>.
</child>
</root>
]]></code>
</test-code>
<test-code>
<description>Square bracket at end separate by space is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
<root>
<child>
<![CDATA[ character data[] ]]><![CDATA[]]]]><![CDATA[>
</child>
</root>
]]></code>
</test-code>
</test-data>

View File

@ -1138,6 +1138,7 @@ Additionally it includes CPD, the copy-paste-detector. CPD finds duplicated code
<module>pmd-fortran</module>
<module>pmd-go</module>
<module>pmd-groovy</module>
<module>pmd-lua</module>
<module>pmd-java</module>
<module>pmd-javascript</module>
<module>pmd-jsp</module>