Merge branch 'core-parser-tasks' into analysis-listener

This commit is contained in:
Clément Fournier
2020-11-15 18:53:32 +01:00
3 changed files with 15 additions and 12 deletions

View File

@ -29,7 +29,8 @@ public interface SemanticErrorReporter {
/**
* Report an error at the given location. Errors abort subsequent analysis.
* The produced error can be thrown by the caller.
* The produced error can be thrown by the caller if it cannot be recovered
* from.
*
* @param location Location where the error should be reported
* @param message Message (rendered using a {@link MessageFormat})

View File

@ -10,6 +10,7 @@ import net.sourceforge.pmd.lang.ast.test.*
import net.sourceforge.pmd.lang.ast.test.shouldBe
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol
import net.sourceforge.pmd.lang.java.symbols.table.internal.JavaSemanticErrors
import net.sourceforge.pmd.lang.java.symbols.table.internal.JavaSemanticErrors.*
import net.sourceforge.pmd.lang.java.types.JClassType
import kotlin.test.assertEquals
import kotlin.test.assertNull
@ -86,7 +87,7 @@ class TypeDisambiguationTest : ParserTestSpec({
doTest("Unresolved inner type should produce a warning") {
val (node, args) = logger.warnings[JavaSemanticErrors.CANNOT_RESOLVE_MEMBER]!![0]
val (node, args) = logger.warnings[CANNOT_RESOLVE_MEMBER]!![0]
args.map { it.toString() } shouldBe listOf("Bar", "com.Foo", "an unresolved type")
node.shouldBeA<ASTClassOrInterfaceType> { }
}
@ -128,12 +129,12 @@ class TypeDisambiguationTest : ParserTestSpec({
doTest("Ambiguous inner type should produce an error (ref in Foo)") {
val (_, args) = logger.errors[JavaSemanticErrors.AMBIGUOUS_NAME_REFERENCE]?.first { it.first == refInFoo }!!
val (_, args) = logger.errors[AMBIGUOUS_NAME_REFERENCE]?.first { it.first == refInFoo }!!
args.map { it.toString() } shouldBe listOf("Mem", "p.Scratch.A.Mem", "p.Scratch.B.Mem")
}
doTest("Ambiguous inner type should produce an error (ref in Scratch)") {
val (_, args) = logger.errors[JavaSemanticErrors.AMBIGUOUS_NAME_REFERENCE]?.first { it.first == refInScratch }!!
val (_, args) = logger.errors[AMBIGUOUS_NAME_REFERENCE]?.first { it.first == refInScratch }!!
args.map { it.toString() } shouldBe listOf("Mem", "p.Scratch.A.Mem", "p.Scratch.B.Mem")
}
@ -177,14 +178,14 @@ class TypeDisambiguationTest : ParserTestSpec({
acu.descendants(ASTFieldDeclaration::class.java).map { it.typeNode as ASTClassOrInterfaceType }.toList()
fun assertErrored(t: ASTClassOrInterfaceType, expected: Int, actual: Int) {
val errs = logger.errors[JavaSemanticErrors.MALFORMED_GENERIC_TYPE]?.filter { it.first == t }
val errs = logger.errors[MALFORMED_GENERIC_TYPE]?.filter { it.first == t }
?: emptyList()
assertEquals(errs.size, 1, "`${t.text}` should have produced a single error")
errs.single().second.toList() shouldBe listOf(expected, actual)
}
fun assertNoError(t: ASTClassOrInterfaceType) {
val err = logger.errors[JavaSemanticErrors.MALFORMED_GENERIC_TYPE]?.firstOrNull { it.first == t }
val err = logger.errors[MALFORMED_GENERIC_TYPE]?.firstOrNull { it.first == t }
assertNull(err, "`${t.text}` should not have produced an error")
}
@ -259,14 +260,14 @@ class TypeDisambiguationTest : ParserTestSpec({
acu.descendants(ASTAnnotation::class.java).map { it.typeNode }.toList()
fun assertErrored(t: ASTClassOrInterfaceType) {
val errs = logger.errors[JavaSemanticErrors.EXPECTED_ANNOTATION_TYPE]?.filter { it.first == t }
val errs = logger.errors[EXPECTED_ANNOTATION_TYPE]?.filter { it.first == t }
?: emptyList()
assertEquals(errs.size, 1, "`${t.text}` should have produced a single error")
errs.single().second.toList() shouldBe emptyList()
}
fun assertNoError(t: ASTClassOrInterfaceType) {
val err = logger.errors[JavaSemanticErrors.MALFORMED_GENERIC_TYPE]?.firstOrNull { it.first == t }
val err = logger.errors[MALFORMED_GENERIC_TYPE]?.firstOrNull { it.first == t }
assertNull(err, "`${t.text}` should not have produced an error")
}

View File

@ -12,6 +12,7 @@ import net.sourceforge.pmd.lang.java.JavaParsingHelper
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol
import net.sourceforge.pmd.lang.java.symbols.JTypeParameterSymbol
import net.sourceforge.pmd.lang.java.symbols.table.internal.JavaSemanticErrors
import net.sourceforge.pmd.lang.java.symbols.table.internal.JavaSemanticErrors.*
class VarDisambiguationTest : ParserTestSpec({
@ -153,7 +154,7 @@ class Foo<T> {
doTest("Unresolved field") {
logger.getWarning(JavaSemanticErrors.CANNOT_RESOLVE_MEMBER, 0) { node, args ->
logger.getWarning(CANNOT_RESOLVE_MEMBER, 0) { node, args ->
args shouldBe listOf("noField", "com.foo.bar.Foo.Inner", "a field access")
node.shouldMatchN {
@ -170,7 +171,7 @@ class Foo<T> {
doTest("Unresolved field chain") {
logger.getWarning(JavaSemanticErrors.CANNOT_RESOLVE_MEMBER, 1) { node, args ->
logger.getWarning(CANNOT_RESOLVE_MEMBER, 1) { node, args ->
args shouldBe listOf("noField", "com.foo.bar.Foo.Inner", "a field access")
node.shouldMatchN {
@ -189,7 +190,7 @@ class Foo<T> {
doTest("Unresolved type var member") {
logger.getWarning(JavaSemanticErrors.CANNOT_RESOLVE_MEMBER, 2) { node, args ->
logger.getWarning(CANNOT_RESOLVE_MEMBER, 2) { node, args ->
args shouldBe listOf("fofo", "type variable T", "a field access")
node.shouldMatchN {
@ -206,7 +207,7 @@ class Foo<T> {
doTest("Unresolved type var member (in type ctx)") {
logger.getWarning(JavaSemanticErrors.CANNOT_RESOLVE_MEMBER, 3) { node, args ->
logger.getWarning(CANNOT_RESOLVE_MEMBER, 3) { node, args ->
args shouldBe listOf("Fofo", "type variable T", "an unresolved type")
node.shouldMatchN {