diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CpdAnalysis.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CpdAnalysis.java index d9b8f452b3..34497c47f4 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CpdAnalysis.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CpdAnalysis.java @@ -24,6 +24,7 @@ import net.sourceforge.pmd.lang.Language; import net.sourceforge.pmd.lang.LanguagePropertyBundle; import net.sourceforge.pmd.lang.ast.FileAnalysisException; import net.sourceforge.pmd.lang.ast.LexException; +import net.sourceforge.pmd.lang.ast.impl.javacc.MalformedSourceException; import net.sourceforge.pmd.lang.document.FileCollector; import net.sourceforge.pmd.lang.document.FileId; import net.sourceforge.pmd.lang.document.InternalApiBridge; @@ -171,7 +172,7 @@ public final class CpdAnalysis implements AutoCloseable { int newTokens = doTokenize(textDocument, tokenizers.get(textFile.getLanguageVersion().getLanguage()), tokens); numberOfTokensPerFile.put(textDocument.getFileId(), newTokens); listener.addedFile(1); - } catch (LexException | IOException e) { + } catch (LexException | IOException | MalformedSourceException e) { if (e instanceof FileAnalysisException) { // NOPMD ((FileAnalysisException) e).setFileId(textFile.getFileId()); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/BaseMappedDocument.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/BaseMappedDocument.java index 55daae33ce..be92385faa 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/BaseMappedDocument.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/BaseMappedDocument.java @@ -68,6 +68,9 @@ abstract class BaseMappedDocument implements TextDocument { * @return Input region */ protected @NonNull TextRegion inputRegion(TextRegion outputRegion) { + if (outputRegion.isEmpty()) { + return TextRegion.caretAt(inputOffset(outputRegion.getStartOffset(), false)); + } return TextRegion.fromBothOffsets(inputOffset(outputRegion.getStartOffset(), true), inputOffset(outputRegion.getEndOffset(), false)); }