Merge branch 'master' into deprecate-jjtree-methods

This commit is contained in:
Clément Fournier
2020-01-06 18:50:43 +01:00
10 changed files with 327 additions and 133 deletions

View File

@ -127,6 +127,8 @@ You can identify them with the `@InternalApi` annotation. You'll also get a depr
* [#2169](https://github.com/pmd/pmd/pull/2169): \[modelica] Follow-up fixes for Modelica language module - [Anatoly Trosinenko](https://github.com/atrosinenko)
* [#2193](https://github.com/pmd/pmd/pull/2193): \[core] Fix odd logic in test runner - [Egor Bredikhin](https://github.com/Egor18)
* [#2194](https://github.com/pmd/pmd/pull/2194): \[java] Fix odd logic in AvoidUsingHardCodedIPRule - [Egor Bredikhin](https://github.com/Egor18)
* [#2195](https://github.com/pmd/pmd/pull/2195): \[modelica] Normalize invalid node ranges - [Anatoly Trosinenko](https://github.com/atrosinenko)
* [#2199](https://github.com/pmd/pmd/pull/2199): \[modelica] Fix Javadoc tags - [Anatoly Trosinenko](https://github.com/atrosinenko)
{% endtocmaker %}

View File

@ -101,7 +101,7 @@ public final class CppParser {
int i;
if (t.kind != SCOPE)
{
s.append(t.image);
s.append(t.getImage());
t = getToken(2);
i = 3;
}
@ -110,8 +110,8 @@ public final class CppParser {
while (t.kind == SCOPE)
{
s.append(t.image);
s.append((t = getToken(i++)).image);
s.append(t.getImage());
s.append((t = getToken(i++)).getImage());
t = getToken(i++);
}
@ -606,12 +606,12 @@ String scope_override() :
("::") { name += "::"; }
(
LOOKAHEAD(2) t = <ID> ("<" template_argument_list() ">")? "::"
{ name += t.image + "::"; }
{ name += t.getImage() + "::"; }
)*
|
(
LOOKAHEAD(2) t = <ID> ("<" template_argument_list() ">")? "::"
{ name += t.image + "::"; }
{ name += t.getImage() + "::"; }
)+
)
{ return name; }
@ -627,7 +627,7 @@ String qualified_id() :
[ LOOKAHEAD(scope_override_lookahead()) name = scope_override() ]
(
t = <ID> [ "<" template_argument_list() ">" ]
{ return name + t.image; }
{ return name + t.getImage(); }
|
"operator" optor() { return "operator"; }
)
@ -718,7 +718,7 @@ void class_specifier() :
|
LOOKAHEAD(2) t = <ID>
{
sc = (ClassScope)sym.OpenScope(t.image, true);
sc = (ClassScope)sym.OpenScope(t.getImage(), true);
}
(base_clause(sc))?
"{"
@ -729,7 +729,7 @@ void class_specifier() :
}
|
t=<ID> (LOOKAHEAD(2) "<" template_argument_list() ">")?
{ sym.PutTypeName(t.image); }
{ sym.PutTypeName(t.getImage()); }
)
}
@ -745,7 +745,7 @@ void base_specifier(ClassScope scope) :
("virtual" (access_specifier())? | access_specifier() ("virtual")?)?
(LOOKAHEAD(scope_override_lookahead()) scope_override())?
t = <ID> ("<" template_argument_list() ">")?
{ scope.AddSuper(sym.GetScope(t.image));
{ scope.AddSuper(sym.GetScope(t.getImage()));
}
}
@ -837,7 +837,7 @@ void enum_specifier() :
"{" enumerator_list() "}"
|
t=<ID> (LOOKAHEAD(2) "{" enumerator_list() "}")?
{ sym.PutTypeName(t.image); }
{ sym.PutTypeName(t.getImage()); }
)
}
@ -894,7 +894,7 @@ String direct_declarator() :
{
LOOKAHEAD(2)
"~" t = <ID> (LOOKAHEAD(2) declarator_suffixes())?
{ return "~" + t.image; }
{ return "~" + t.getImage(); }
|
"(" name = declarator() ")" (LOOKAHEAD(2) declarator_suffixes())?
{ return name; }
@ -1149,7 +1149,7 @@ void template_parameter() :
{ Token t; }
{
LOOKAHEAD(3)
"class" t=<ID> { sym.PutTypeName(t.image); }
"class" t=<ID> { sym.PutTypeName(t.getImage()); }
|
parameter_declaration()
}

View File

@ -22,6 +22,13 @@
</configuration>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
@ -70,10 +77,20 @@
<artifactId>pmd-core</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-lang-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -34,14 +34,14 @@ abstract class AbstractModelicaImportClause extends AbstractModelicaNode impleme
abstract boolean isQualified();
/**
* A template method to be used by {@link resolveSimpleName}. Usually used to fetch the lexically referenced
* A template method to be used by {@link #resolveSimpleName}. Usually used to fetch the lexically referenced
* class in the corresponding import statement.
*/
protected abstract ResolutionResult<ModelicaDeclaration> getCacheableImportSources(ResolutionState state, ModelicaScope scope);
/**
* A template method to be used by {@link resolveSimpleName}. Usually used to try to fetch declarations for
* <code>simpleName</code> from particular <i>source</i> returned by {@link getCacheableImportSources}.
* A template method to be used by {@link #resolveSimpleName}. Usually used to try to fetch declarations for
* <code>simpleName</code> from particular <i>source</i> returned by {@link #getCacheableImportSources}.
*/
protected abstract void fetchImportedClassesFromSource(ResolutionContext result, ModelicaDeclaration source, String simpleName) throws Watchdog.CountdownException;

View File

@ -4,6 +4,7 @@
package net.sourceforge.pmd.lang.modelica.ast;
import net.sourceforge.pmd.lang.ast.GenericToken;
import net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode;
import net.sourceforge.pmd.lang.modelica.resolver.ModelicaScope;
@ -17,6 +18,13 @@ import net.sourceforge.pmd.lang.modelica.resolver.ModelicaScope;
* @see ModelicaNode for public API.
*/
abstract class AbstractModelicaNode extends AbstractJjtreeNode<ModelicaNode> implements ModelicaNode {
/**
* Kind for implicit tokens. Negative because JavaCC only picks
* positive numbers for token kinds.
*/
private static final int IMPLICIT_TOKEN = -1;
private ModelicaParser parser;
private ModelicaScope ownScope;
@ -38,23 +46,67 @@ abstract class AbstractModelicaNode extends AbstractJjtreeNode<ModelicaNode> imp
}
@Override
public void jjtOpen() {
if (beginLine == -1 && parser.token.next != null) {
beginLine = parser.token.next.beginLine;
beginColumn = parser.token.next.beginColumn;
public int getBeginLine() {
return jjtGetFirstToken().getBeginLine();
}
@Override
public int getBeginColumn() {
return jjtGetFirstToken().getBeginColumn();
}
@Override
public int getEndLine() {
return jjtGetLastToken().getEndLine();
}
@Override
public int getEndColumn() {
return jjtGetLastToken().getEndColumn();
}
@Override
public void jjtClose() {
if (beginLine == -1 && (children == null || children.length == 0)) {
beginColumn = parser.token.beginColumn;
// in jjtClose, jjtSetLastToken has not been called yet, so we use parser.token.next
if (parser.token.next == jjtGetFirstToken()) {
// Reversed, this node consumed no token.
// Forge a token with the correct coordinates, and zero length
Token next = parser.token.next;
Token implicit = new Token(IMPLICIT_TOKEN, "");
implicit.beginColumn = next.beginColumn;
implicit.endColumn = next.beginColumn - 1; // because of inclusive columns..
implicit.beginLine = next.beginLine;
implicit.endLine = next.beginLine;
// insert it right before the next token
// as a special token
implicit.next = next;
if (next.specialToken != null) {
next.specialToken.next = implicit;
implicit.specialToken = next.specialToken;
}
if (beginLine == -1) {
beginLine = parser.token.beginLine;
next.specialToken = implicit;
// set it as both first and last
// beware, JJTree calls jjtSetLastToken after this routine..
// hence the override below
jjtSetFirstToken(implicit);
jjtSetLastToken(implicit);
}
}
@Override
public void jjtSetLastToken(GenericToken token) {
// don't let jjtree override tokens we've chosen
if (lastToken == null) {
super.jjtSetLastToken(token);
}
endLine = parser.token.endLine;
endColumn = parser.token.endColumn;
}
@Override

View File

@ -69,7 +69,7 @@ public final class CompositeName {
/**
* Tries to match the <code>prefix</code> argument with the first elements of this name
*
* @returns the remaining elements on success or null on failure
* @return the remaining elements on success or null on failure
*/
public CompositeName matchPrefix(String[] prefix) {
return matchPrefix(prefix, 0);

View File

@ -5,7 +5,7 @@
package net.sourceforge.pmd.lang.modelica.resolver;
/**
* A watchdog counter initialized with some value. Throws an exception after the specified {@link decrement} calls.
* A watchdog counter initialized with some value. Throws an exception after the specified {@link #decrement} calls.
*
* Is used to break possible resolution loops.
*/

View File

@ -0,0 +1,123 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.modelica.ast
import io.kotlintest.should
import io.kotlintest.shouldBe
import io.kotlintest.specs.FunSpec
import net.sourceforge.pmd.lang.LanguageRegistry
import net.sourceforge.pmd.lang.ast.Node
import net.sourceforge.pmd.lang.ast.test.matchNode
import net.sourceforge.pmd.lang.ast.test.shouldBe
import java.io.StringReader
class ModelicaCoordsTest : FunSpec({
test("Test line/column numbers for implicit nodes") {
"""
package TestPackage
package EmptyPackage
end EmptyPackage;
end TestPackage;
""".trim().parseModelica() should matchNode<ASTStoredDefinition> {
it.assertBounds(1, 1, 4, 16)
child<ASTClassDefinition> {
it.assertBounds(1, 1, 4, 15)
child<ASTClassPrefixes> {
it.assertBounds(1, 1, 1, 7)
child<ASTPackageClause> {
it.assertBounds(1, 1, 1, 7)
}
}
child<ASTClassSpecifier> {
it.assertBounds(1, 9, 4, 15)
child<ASTSimpleLongClassSpecifier> {
it.assertBounds(1, 9, 4, 15)
child<ASTSimpleName> {
it.assertBounds(1, 9, 1, 19)
}
child<ASTComposition> {
it.assertBounds(2, 3, 3, 19)
child<ASTElementList> {
it.assertBounds(2, 3, 3, 19)
child<ASTRegularElement> {
it.assertBounds(2, 3, 3, 18)
child<ASTClassDefinition> {
it.assertBounds(2, 3, 3, 18)
it.isPartial shouldBe false
child<ASTClassPrefixes> {
it.assertBounds(2, 3, 2, 9)
child<ASTPackageClause> {
it.assertBounds(2, 3, 2, 9)
}
}
child<ASTClassSpecifier> {
it.assertBounds(2, 11, 3, 18)
child<ASTSimpleLongClassSpecifier> {
it.assertBounds(2, 11, 3, 18)
it.simpleClassName shouldBe "EmptyPackage"
child<ASTSimpleName> {
it.assertBounds(2, 11, 2, 22)
}
child<ASTComposition> {
it.assertBounds(3, 3, 3, 2)
child<ASTElementList> {
/*
This ElementList is empty and has no explicit token.
*/
it.assertBounds(3, 3, 3, 2)
}
}
child<ASTSimpleName> {
it.assertBounds(3, 7, 3, 18)
}
}
}
}
}
}
}
child<ASTSimpleName> {
it.assertBounds(4, 5, 4, 15)
}
}
}
}
}
}
})
fun String.parseModelica(): ASTStoredDefinition {
val ver = LanguageRegistry.getLanguage("Modelica").defaultVersion.languageVersionHandler
val parser = ver.getParser(ver.defaultParserOptions)
return parser.parse(":dummy:", StringReader(this)) as ASTStoredDefinition
}
fun Node.assertBounds(bline: Int, bcol: Int, eline: Int, ecol: Int) {
this::getBeginLine shouldBe bline
this::getBeginColumn shouldBe bcol
this::getEndLine shouldBe eline
this::getEndColumn shouldBe ecol
}

File diff suppressed because it is too large Load Diff

View File

@ -998,9 +998,9 @@ TOKEN:
/*
* Remove the double period if it is there
*/
if (matchedToken.image.endsWith("..")) {
if (matchedToken.getImage().endsWith("..")) {
input_stream.backup(2);
matchedToken.image = matchedToken.image.substring(0,matchedToken.image.length()-2);
matchedToken.image = matchedToken.getImage().substring(0,matchedToken.getImage().length()-2);
}
/*
@ -1197,7 +1197,7 @@ void EscapedDirective() : {}
/*
* churn and burn..
*/
t.image = escapedDirective( t.image );
t.image = escapedDirective( t.getImage() );
}
}
@ -1238,7 +1238,7 @@ void Escape() : {}
/*
* if that failed, lets lookahead to see if we matched a PD or a VM
*/
String nTag = t.next.image.substring(1);
String nTag = t.next.getImage().substring(1);
if (strictEscape
|| isDirective(nTag)
|| macroNames.containsKey(nTag)
@ -1380,11 +1380,11 @@ VmNode Directive() :
String directiveName;
if (t.kind == VmParserConstants.BRACKETED_WORD)
{
directiveName = t.image.substring(2, t.image.length() - 1);
directiveName = t.getImage().substring(2, t.getImage().length() - 1);
}
else
{
directiveName = t.image.substring(1);
directiveName = t.getImage().substring(1);
}
d = getDirective(directiveName);
@ -1459,13 +1459,13 @@ VmNode Directive() :
else if (isVM)
{
throw new MacroParseException("Invalid arg #"
+ argPos + " in VM " + t.image, currentTemplateName, t);
+ argPos + " in VM " + t.getImage(), currentTemplateName, t);
}
/* if #foreach and it's the 2nd arg, ok */
else if (d != null && (!directiveName.equals("foreach") || argPos != 1))
{
throw new MacroParseException("Invalid arg #"
+ argPos + " in directive " + t.image, currentTemplateName, t);
+ argPos + " in directive " + t.getImage(), currentTemplateName, t);
}
else
{
@ -1533,7 +1533,7 @@ VmNode Directive() :
{
// Add the macro name so that we can peform escape processing
// on defined macros
String macroName = ((AbstractVmNode)jjtThis.jjtGetChild(0)).getFirstToken().image;
String macroName = ((AbstractVmNode)jjtThis.jjtGetChild(0)).getFirstToken().getImage();
macroNames.put(macroName, macroName);
}
/*