From 4fe253c1db6ed9c42f7aa51d8673ae890b7548f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sun, 15 Nov 2020 18:50:59 +0100 Subject: [PATCH 1/2] Remove old stuff --- .../table/internal/SemanticChecksLogger.java | 94 ------------------- 1 file changed, 94 deletions(-) delete mode 100644 pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SemanticChecksLogger.java diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SemanticChecksLogger.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SemanticChecksLogger.java deleted file mode 100644 index 4202caab0e..0000000000 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/SemanticChecksLogger.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.lang.java.symbols.table.internal; - -import java.text.MessageFormat; - -import net.sourceforge.pmd.lang.java.ast.JavaNode; -import net.sourceforge.pmd.lang.java.ast.internal.LanguageLevelChecker; - -/** - * TODO pull that up to PMD core, use for {@link LanguageLevelChecker} - * as well - */ -public interface SemanticChecksLogger { - - // TODO how strict do we need to be here? - // many rules don't absolutely need correctness to work - // maybe we need to identify separate "levels" of the tree - // eg level 0: lexable (CPD) - // level 1: parsable (many syntax-only rules, eg UnnecessaryParentheses) - // level 2: type-resolved (more complicated rules) - - /** - * Warning, classpath is misconfigured (or not configured). - */ - String CANNOT_RESOLVE_SYMBOL = "Cannot resolve symbol {0}"; - String INACCESSIBLE_SYMBOL = "Symbol {0} is inaccessible"; - - - /** - * We had resolved a prefix, and a suffix is not resolved. This may - * mean that the classpath is out-of-date, or the code is incorrect. - * Eg {@code System.oute}: {@code System} is resolved, {@code oute} - * is not a member of that type. - * - *

This differs from {@link #CANNOT_RESOLVE_SYMBOL} in that for - * the latter, it's more probable that the classpath is incorrect. - * It is emitted eg when we see an import, so a fully qualified - * name, and yet we can't resolve it. - * TODO whether it's incorrect code or incorrect classpath is only - * a guess, probably we need an option to differentiate the two - * (eg in an IDE plugin, it may well be incorrect code, but in a - * CLI run, it's more likely to be incorrect classpath). - */ - String CANNOT_RESOLVE_MEMBER = "Cannot resolve ''{0}'' in {1}, treating it as {2}"; // javac gives a simple "cannot resolve symbol {0}" - - /** - * TODO Should be an error. - */ - String MALFORMED_GENERIC_TYPE = "Maformed generic type: expected {0} type arguments, got {1}"; - - // this is an error - String EXPECTED_ANNOTATION_TYPE = "Expected an annotation type"; - - /** - * An ambiguous name is completely ambiguous. We don't have info - * about it at all, classpath is incomplete or code is incorrect. - * Eg {@code package.that.doesnt.exist.Type} - */ - String CANNOT_RESOLVE_AMBIGUOUS_NAME = "Cannot resolve ambiguous name {0}, treating it as {1}"; - - String AMBIGUOUS_NAME_REFERENCE = "Reference ''{0}'' is ambiguous, both {1} and {2} match"; - - /* - TODO Checks that are essential for typeres/symtable - - no self-reference in initializer - - else infinite loop - - class or type param doesn't extend/implement itself - - else infinite loop - - types are well-formed, ie if they are parameterized, then the number of type arguments is the number of formal type params - - note that this will be tricky when dealing with unresolved types: - for now we give them zero type params, but we must infer that from - looking through the file. - - else failure when doing type substitution - - method reference is well formed, ie a constructor reference - has a type as LHS (currently the parser throws, it would need - to be more lenient) - */ - - - /** - * Log a warning at the given location. - * - * @param location Node owning the warning - * @param message Message, possibly formatted (see {@link MessageFormat}) - * @param args Arguments for the template - */ - void warning(JavaNode location, String message, Object... args); - - - void error(JavaNode location, String message, Object... args); -} From b57221d7acda942d8c580c800f690caf320468a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sun, 15 Nov 2020 18:53:26 +0100 Subject: [PATCH 2/2] Cleanups --- .../pmd/lang/ast/SemanticErrorReporter.java | 3 ++- .../pmd/lang/java/ast/TypeDisambiguationTest.kt | 15 ++++++++------- .../pmd/lang/java/ast/VarDisambiguationTest.kt | 9 +++++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/SemanticErrorReporter.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/SemanticErrorReporter.java index 608b3736e3..0eb88fdb20 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/SemanticErrorReporter.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/SemanticErrorReporter.java @@ -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}) diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TypeDisambiguationTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TypeDisambiguationTest.kt index 05af1baf19..304b9fbbf2 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TypeDisambiguationTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TypeDisambiguationTest.kt @@ -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 { } } @@ -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") } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/VarDisambiguationTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/VarDisambiguationTest.kt index 7081ce162c..d890ddd97d 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/VarDisambiguationTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/VarDisambiguationTest.kt @@ -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 { 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 { 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 { 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 { 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 {