forked from phoedos/pmd
Merge remote-tracking branch 'upstream/pmd/7.0.x' into text-utils-simple
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
<groupId>net.sourceforge.pmd</groupId>
|
||||
<artifactId>pmd</artifactId>
|
||||
<version>7.0.0-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
|
@@ -22,6 +22,7 @@ public class CsTokenizer extends AntlrTokenizer {
|
||||
|
||||
private boolean ignoreUsings = false;
|
||||
private boolean ignoreLiteralSequences = false;
|
||||
private boolean ignoreAttributes = false;
|
||||
|
||||
/**
|
||||
* Sets the possible options for the C# tokenizer.
|
||||
@@ -29,19 +30,16 @@ public class CsTokenizer extends AntlrTokenizer {
|
||||
* @param properties the properties
|
||||
* @see #IGNORE_USINGS
|
||||
* @see #OPTION_IGNORE_LITERAL_SEQUENCES
|
||||
* @see #IGNORE_ANNOTATIONS
|
||||
*/
|
||||
public void setProperties(Properties properties) {
|
||||
ignoreUsings = Boolean.parseBoolean(properties.getProperty(IGNORE_USINGS, Boolean.FALSE.toString()));
|
||||
ignoreLiteralSequences = Boolean.parseBoolean(properties.getProperty(OPTION_IGNORE_LITERAL_SEQUENCES,
|
||||
Boolean.FALSE.toString()));
|
||||
ignoreUsings = getBooleanProperty(properties, IGNORE_USINGS);
|
||||
ignoreLiteralSequences = getBooleanProperty(properties, OPTION_IGNORE_LITERAL_SEQUENCES);
|
||||
ignoreAttributes = getBooleanProperty(properties, IGNORE_ANNOTATIONS);
|
||||
}
|
||||
|
||||
public void setIgnoreUsings(boolean ignoreUsings) {
|
||||
this.ignoreUsings = ignoreUsings;
|
||||
}
|
||||
|
||||
public void setIgnoreLiteralSequences(boolean ignoreLiteralSequences) {
|
||||
this.ignoreLiteralSequences = ignoreLiteralSequences;
|
||||
private boolean getBooleanProperty(final Properties properties, final String property) {
|
||||
return Boolean.parseBoolean(properties.getProperty(property, Boolean.FALSE.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,7 +49,7 @@ public class CsTokenizer extends AntlrTokenizer {
|
||||
|
||||
@Override
|
||||
protected AntlrTokenFilter getTokenFilter(final AntlrTokenManager tokenManager) {
|
||||
return new CsTokenFilter(tokenManager, ignoreUsings, ignoreLiteralSequences);
|
||||
return new CsTokenFilter(tokenManager, ignoreUsings, ignoreLiteralSequences, ignoreAttributes);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,15 +68,18 @@ public class CsTokenizer extends AntlrTokenizer {
|
||||
|
||||
private final boolean ignoreUsings;
|
||||
private final boolean ignoreLiteralSequences;
|
||||
private final boolean ignoreAttributes;
|
||||
private boolean discardingUsings = false;
|
||||
private boolean discardingNL = false;
|
||||
private boolean isDiscardingAttribute = false;
|
||||
private AntlrToken discardingLiteralsUntil = null;
|
||||
private boolean discardCurrent = false;
|
||||
|
||||
CsTokenFilter(final AntlrTokenManager tokenManager, boolean ignoreUsings, boolean ignoreLiteralSequences) {
|
||||
CsTokenFilter(final AntlrTokenManager tokenManager, boolean ignoreUsings, boolean ignoreLiteralSequences, boolean ignoreAttributes) {
|
||||
super(tokenManager);
|
||||
this.ignoreUsings = ignoreUsings;
|
||||
this.ignoreLiteralSequences = ignoreLiteralSequences;
|
||||
this.ignoreAttributes = ignoreAttributes;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -91,6 +92,7 @@ public class CsTokenizer extends AntlrTokenizer {
|
||||
discardCurrent = false;
|
||||
skipUsingDirectives(currentToken, remainingTokens);
|
||||
skipLiteralSequences(currentToken, remainingTokens);
|
||||
skipAttributes(currentToken);
|
||||
}
|
||||
|
||||
private void skipUsingDirectives(final AntlrToken currentToken, final Iterable<AntlrToken> remainingTokens) {
|
||||
@@ -167,6 +169,25 @@ public class CsTokenizer extends AntlrTokenizer {
|
||||
discardingNL = currentToken.getKind() == CSharpLexer.NL;
|
||||
}
|
||||
|
||||
private void skipAttributes(final AntlrToken currentToken) {
|
||||
if (ignoreAttributes) {
|
||||
switch (currentToken.getKind()) {
|
||||
case CSharpLexer.OPEN_BRACKET:
|
||||
// Start of an attribute.
|
||||
isDiscardingAttribute = true;
|
||||
break;
|
||||
case CSharpLexer.CLOSE_BRACKET:
|
||||
// End of an attribute.
|
||||
isDiscardingAttribute = false;
|
||||
discardCurrent = true;
|
||||
break;
|
||||
default:
|
||||
// Skip any other token.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void skipLiteralSequences(final AntlrToken currentToken, final Iterable<AntlrToken> remainingTokens) {
|
||||
if (ignoreLiteralSequences) {
|
||||
final int type = currentToken.getKind();
|
||||
@@ -222,7 +243,7 @@ public class CsTokenizer extends AntlrTokenizer {
|
||||
|
||||
@Override
|
||||
protected boolean isLanguageSpecificDiscarding() {
|
||||
return discardingUsings || discardingNL || isDiscardingLiterals() || discardCurrent;
|
||||
return discardingUsings || discardingNL || isDiscardingAttribute || isDiscardingLiterals() || discardCurrent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -105,18 +105,33 @@ public class CsTokenizerTest extends CpdTextComparisonTest {
|
||||
doTest("csharp7And8Additions");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAttributesAreNotIgnored() {
|
||||
doTest("attributes");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAttributesAreIgnored() {
|
||||
doTest("attributes", "_ignored", skipAttributes());
|
||||
}
|
||||
|
||||
private Properties ignoreUsings() {
|
||||
return properties(true, false);
|
||||
return properties(true, false, false);
|
||||
}
|
||||
|
||||
private Properties skipLiteralSequences() {
|
||||
return properties(false, true);
|
||||
return properties(false, true, false);
|
||||
}
|
||||
|
||||
private Properties properties(boolean ignoreUsings, boolean ignoreLiteralSequences) {
|
||||
private Properties skipAttributes() {
|
||||
return properties(false, false, true);
|
||||
}
|
||||
|
||||
private Properties properties(boolean ignoreUsings, boolean ignoreLiteralSequences, boolean ignoreAttributes) {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty(Tokenizer.IGNORE_USINGS, Boolean.toString(ignoreUsings));
|
||||
properties.setProperty(Tokenizer.OPTION_IGNORE_LITERAL_SEQUENCES, Boolean.toString(ignoreLiteralSequences));
|
||||
properties.setProperty(Tokenizer.IGNORE_ANNOTATIONS, Boolean.toString(ignoreAttributes));
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
|
42
pmd-cs/src/test/resources/net/sourceforge/pmd/lang/cs/cpd/testdata/attributes.cs
vendored
Normal file
42
pmd-cs/src/test/resources/net/sourceforge/pmd/lang/cs/cpd/testdata/attributes.cs
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
[Serializable]
|
||||
public class SampleClass
|
||||
{
|
||||
// Objects of this type can be serialized.
|
||||
}
|
||||
|
||||
[System.Runtime.InteropServices.DllImport("user32.dll")]
|
||||
extern static void SampleMethod();
|
||||
|
||||
void MethodA([In][Out] ref double x) { }
|
||||
void MethodB([Out][In] ref double x) { }
|
||||
void MethodC([In, Out] ref double x) { }
|
||||
|
||||
[Conditional("DEBUG"), Conditional("TEST1")]
|
||||
void TraceMethod()
|
||||
{
|
||||
// ...
|
||||
}
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
[DllImport("user32.dll", SetLastError=false, ExactSpelling=false)]
|
||||
[DllImport("user32.dll", ExactSpelling=false, SetLastError=false)]
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
[assembly: AssemblyTitleAttribute("Production assembly 4")]
|
||||
[module: CLSCompliant(true)]
|
||||
|
||||
// default: applies to method
|
||||
[ValidatedContract]
|
||||
int Method1() { return 0; }
|
||||
|
||||
// applies to method
|
||||
[method: ValidatedContract]
|
||||
int Method2() { return 0; }
|
||||
|
||||
// applies to parameter
|
||||
int Method3([ValidatedContract] string contract) { return 0; }
|
||||
|
||||
// applies to return value
|
||||
[return: ValidatedContract]
|
||||
int Method4() { return 0; }
|
229
pmd-cs/src/test/resources/net/sourceforge/pmd/lang/cs/cpd/testdata/attributes.txt
vendored
Normal file
229
pmd-cs/src/test/resources/net/sourceforge/pmd/lang/cs/cpd/testdata/attributes.txt
vendored
Normal file
@@ -0,0 +1,229 @@
|
||||
[Image] or [Truncated image[ Bcol Ecol
|
||||
L1
|
||||
[\[] 1 2
|
||||
[Serializable] 2 14
|
||||
[\]] 14 15
|
||||
L2
|
||||
[public] 1 7
|
||||
[class] 8 13
|
||||
[SampleClass] 14 25
|
||||
L3
|
||||
[{] 1 2
|
||||
L5
|
||||
[}] 1 2
|
||||
L7
|
||||
[\[] 1 2
|
||||
[System] 2 8
|
||||
[.] 8 9
|
||||
[Runtime] 9 16
|
||||
[.] 16 17
|
||||
[InteropServices] 17 32
|
||||
[.] 32 33
|
||||
[DllImport] 33 42
|
||||
[(] 42 43
|
||||
["user32.dll"] 43 55
|
||||
[)] 55 56
|
||||
[\]] 56 57
|
||||
L8
|
||||
[extern] 1 7
|
||||
[static] 8 14
|
||||
[void] 15 19
|
||||
[SampleMethod] 20 32
|
||||
[(] 32 33
|
||||
[)] 33 34
|
||||
[;] 34 35
|
||||
L10
|
||||
[void] 1 5
|
||||
[MethodA] 6 13
|
||||
[(] 13 14
|
||||
[\[] 14 15
|
||||
[In] 15 17
|
||||
[\]] 17 18
|
||||
[\[] 18 19
|
||||
[Out] 19 22
|
||||
[\]] 22 23
|
||||
[ref] 24 27
|
||||
[double] 28 34
|
||||
[x] 35 36
|
||||
[)] 36 37
|
||||
[{] 38 39
|
||||
[}] 40 41
|
||||
L11
|
||||
[void] 1 5
|
||||
[MethodB] 6 13
|
||||
[(] 13 14
|
||||
[\[] 14 15
|
||||
[Out] 15 18
|
||||
[\]] 18 19
|
||||
[\[] 19 20
|
||||
[In] 20 22
|
||||
[\]] 22 23
|
||||
[ref] 24 27
|
||||
[double] 28 34
|
||||
[x] 35 36
|
||||
[)] 36 37
|
||||
[{] 38 39
|
||||
[}] 40 41
|
||||
L12
|
||||
[void] 1 5
|
||||
[MethodC] 6 13
|
||||
[(] 13 14
|
||||
[\[] 14 15
|
||||
[In] 15 17
|
||||
[,] 17 18
|
||||
[Out] 19 22
|
||||
[\]] 22 23
|
||||
[ref] 24 27
|
||||
[double] 28 34
|
||||
[x] 35 36
|
||||
[)] 36 37
|
||||
[{] 38 39
|
||||
[}] 40 41
|
||||
L14
|
||||
[\[] 1 2
|
||||
[Conditional] 2 13
|
||||
[(] 13 14
|
||||
["DEBUG"] 14 21
|
||||
[)] 21 22
|
||||
[,] 22 23
|
||||
[Conditional] 24 35
|
||||
[(] 35 36
|
||||
["TEST1"] 36 43
|
||||
[)] 43 44
|
||||
[\]] 44 45
|
||||
L15
|
||||
[void] 1 5
|
||||
[TraceMethod] 6 17
|
||||
[(] 17 18
|
||||
[)] 18 19
|
||||
L16
|
||||
[{] 1 2
|
||||
L18
|
||||
[}] 1 2
|
||||
L20
|
||||
[\[] 1 2
|
||||
[DllImport] 2 11
|
||||
[(] 11 12
|
||||
["user32.dll"] 12 24
|
||||
[)] 24 25
|
||||
[\]] 25 26
|
||||
L21
|
||||
[\[] 1 2
|
||||
[DllImport] 2 11
|
||||
[(] 11 12
|
||||
["user32.dll"] 12 24
|
||||
[,] 24 25
|
||||
[SetLastError] 26 38
|
||||
[=] 38 39
|
||||
[false] 39 44
|
||||
[,] 44 45
|
||||
[ExactSpelling] 46 59
|
||||
[=] 59 60
|
||||
[false] 60 65
|
||||
[)] 65 66
|
||||
[\]] 66 67
|
||||
L22
|
||||
[\[] 1 2
|
||||
[DllImport] 2 11
|
||||
[(] 11 12
|
||||
["user32.dll"] 12 24
|
||||
[,] 24 25
|
||||
[ExactSpelling] 26 39
|
||||
[=] 39 40
|
||||
[false] 40 45
|
||||
[,] 45 46
|
||||
[SetLastError] 47 59
|
||||
[=] 59 60
|
||||
[false] 60 65
|
||||
[)] 65 66
|
||||
[\]] 66 67
|
||||
L24
|
||||
[using] 1 6
|
||||
[System] 7 13
|
||||
[;] 13 14
|
||||
L25
|
||||
[using] 1 6
|
||||
[System] 7 13
|
||||
[.] 13 14
|
||||
[Reflection] 14 24
|
||||
[;] 24 25
|
||||
L26
|
||||
[\[] 1 2
|
||||
[assembly] 2 10
|
||||
[:] 10 11
|
||||
[AssemblyTitleAttribute] 12 34
|
||||
[(] 34 35
|
||||
["Production assembly 4"] 35 58
|
||||
[)] 58 59
|
||||
[\]] 59 60
|
||||
L27
|
||||
[\[] 1 2
|
||||
[module] 2 8
|
||||
[:] 8 9
|
||||
[CLSCompliant] 10 22
|
||||
[(] 22 23
|
||||
[true] 23 27
|
||||
[)] 27 28
|
||||
[\]] 28 29
|
||||
L30
|
||||
[\[] 1 2
|
||||
[ValidatedContract] 2 19
|
||||
[\]] 19 20
|
||||
L31
|
||||
[int] 1 4
|
||||
[Method1] 5 12
|
||||
[(] 12 13
|
||||
[)] 13 14
|
||||
[{] 15 16
|
||||
[return] 17 23
|
||||
[0] 24 25
|
||||
[;] 25 26
|
||||
[}] 27 28
|
||||
L34
|
||||
[\[] 1 2
|
||||
[method] 2 8
|
||||
[:] 8 9
|
||||
[ValidatedContract] 10 27
|
||||
[\]] 27 28
|
||||
L35
|
||||
[int] 1 4
|
||||
[Method2] 5 12
|
||||
[(] 12 13
|
||||
[)] 13 14
|
||||
[{] 15 16
|
||||
[return] 17 23
|
||||
[0] 24 25
|
||||
[;] 25 26
|
||||
[}] 27 28
|
||||
L38
|
||||
[int] 1 4
|
||||
[Method3] 5 12
|
||||
[(] 12 13
|
||||
[\[] 13 14
|
||||
[ValidatedContract] 14 31
|
||||
[\]] 31 32
|
||||
[string] 33 39
|
||||
[contract] 40 48
|
||||
[)] 48 49
|
||||
[{] 50 51
|
||||
[return] 52 58
|
||||
[0] 59 60
|
||||
[;] 60 61
|
||||
[}] 62 63
|
||||
L41
|
||||
[\[] 1 2
|
||||
[return] 2 8
|
||||
[:] 8 9
|
||||
[ValidatedContract] 10 27
|
||||
[\]] 27 28
|
||||
L42
|
||||
[int] 1 4
|
||||
[Method4] 5 12
|
||||
[(] 12 13
|
||||
[)] 13 14
|
||||
[{] 15 16
|
||||
[return] 17 23
|
||||
[0] 24 25
|
||||
[;] 25 26
|
||||
[}] 27 28
|
||||
EOF
|
109
pmd-cs/src/test/resources/net/sourceforge/pmd/lang/cs/cpd/testdata/attributes_ignored.txt
vendored
Normal file
109
pmd-cs/src/test/resources/net/sourceforge/pmd/lang/cs/cpd/testdata/attributes_ignored.txt
vendored
Normal file
@@ -0,0 +1,109 @@
|
||||
[Image] or [Truncated image[ Bcol Ecol
|
||||
L2
|
||||
[public] 1 7
|
||||
[class] 8 13
|
||||
[SampleClass] 14 25
|
||||
L3
|
||||
[{] 1 2
|
||||
L5
|
||||
[}] 1 2
|
||||
L8
|
||||
[extern] 1 7
|
||||
[static] 8 14
|
||||
[void] 15 19
|
||||
[SampleMethod] 20 32
|
||||
[(] 32 33
|
||||
[)] 33 34
|
||||
[;] 34 35
|
||||
L10
|
||||
[void] 1 5
|
||||
[MethodA] 6 13
|
||||
[(] 13 14
|
||||
[ref] 24 27
|
||||
[double] 28 34
|
||||
[x] 35 36
|
||||
[)] 36 37
|
||||
[{] 38 39
|
||||
[}] 40 41
|
||||
L11
|
||||
[void] 1 5
|
||||
[MethodB] 6 13
|
||||
[(] 13 14
|
||||
[ref] 24 27
|
||||
[double] 28 34
|
||||
[x] 35 36
|
||||
[)] 36 37
|
||||
[{] 38 39
|
||||
[}] 40 41
|
||||
L12
|
||||
[void] 1 5
|
||||
[MethodC] 6 13
|
||||
[(] 13 14
|
||||
[ref] 24 27
|
||||
[double] 28 34
|
||||
[x] 35 36
|
||||
[)] 36 37
|
||||
[{] 38 39
|
||||
[}] 40 41
|
||||
L15
|
||||
[void] 1 5
|
||||
[TraceMethod] 6 17
|
||||
[(] 17 18
|
||||
[)] 18 19
|
||||
L16
|
||||
[{] 1 2
|
||||
L18
|
||||
[}] 1 2
|
||||
L24
|
||||
[using] 1 6
|
||||
[System] 7 13
|
||||
[;] 13 14
|
||||
L25
|
||||
[using] 1 6
|
||||
[System] 7 13
|
||||
[.] 13 14
|
||||
[Reflection] 14 24
|
||||
[;] 24 25
|
||||
L31
|
||||
[int] 1 4
|
||||
[Method1] 5 12
|
||||
[(] 12 13
|
||||
[)] 13 14
|
||||
[{] 15 16
|
||||
[return] 17 23
|
||||
[0] 24 25
|
||||
[;] 25 26
|
||||
[}] 27 28
|
||||
L35
|
||||
[int] 1 4
|
||||
[Method2] 5 12
|
||||
[(] 12 13
|
||||
[)] 13 14
|
||||
[{] 15 16
|
||||
[return] 17 23
|
||||
[0] 24 25
|
||||
[;] 25 26
|
||||
[}] 27 28
|
||||
L38
|
||||
[int] 1 4
|
||||
[Method3] 5 12
|
||||
[(] 12 13
|
||||
[string] 33 39
|
||||
[contract] 40 48
|
||||
[)] 48 49
|
||||
[{] 50 51
|
||||
[return] 52 58
|
||||
[0] 59 60
|
||||
[;] 60 61
|
||||
[}] 62 63
|
||||
L42
|
||||
[int] 1 4
|
||||
[Method4] 5 12
|
||||
[(] 12 13
|
||||
[)] 13 14
|
||||
[{] 15 16
|
||||
[return] 17 23
|
||||
[0] 24 25
|
||||
[;] 25 26
|
||||
[}] 27 28
|
||||
EOF
|
Reference in New Issue
Block a user