From 0d79c65d739b62e6dc8abbe0fce742f4630cd67d Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Thu, 17 Dec 2015 18:09:43 +0300 Subject: [PATCH] KT-8275 Unterminated multi-line comment should be compilation error #KT-8275 fixed --- .../kotlin/parsing/KotlinParsing.java | 15 +++++++++++++ .../psi/BlockCommentAtBeginningOfFile1.txt | 4 +++- .../psi/BlockCommentAtBeginningOfFile2.txt | 4 +++- .../psi/BlockCommentAtBeginningOfFile3.txt | 4 +++- .../psi/BlockCommentAtBeginningOfFile4.txt | 4 +++- .../psi/BlockCommentUnmatchedClosing_ERR.kt | 3 +++ .../psi/BlockCommentUnmatchedClosing_ERR.txt | 22 +++++++++++++++++++ .../psi/kdoc/DocCommentAtBeginningOfFile1.txt | 2 ++ .../psi/kdoc/DocCommentAtBeginningOfFile2.txt | 2 ++ .../psi/kdoc/DocCommentAtBeginningOfFile3.txt | 2 ++ .../psi/kdoc/DocCommentAtBeginningOfFile4.txt | 2 ++ compiler/testData/psi/kdoc/Incomplete.txt | 2 ++ .../kotlin/parsing/ParsingTestGenerated.java | 6 +++++ 13 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/psi/BlockCommentUnmatchedClosing_ERR.kt create mode 100644 compiler/testData/psi/BlockCommentUnmatchedClosing_ERR.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java b/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java index b6a7e64624e..06864867646 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.parsing; import com.intellij.lang.PsiBuilder; +import com.intellij.lang.WhitespacesBinders; import com.intellij.openapi.diagnostic.Logger; import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.TokenSet; @@ -112,9 +113,23 @@ public class KotlinParsing extends AbstractKotlinParsing { parseTopLevelDeclaration(); } + checkUnclosedBlockComment(); fileMarker.done(KT_FILE); } + private void checkUnclosedBlockComment() { + if (TokenSet.create(BLOCK_COMMENT, DOC_COMMENT).contains(myBuilder.rawLookup(-1))) { + int startOffset = myBuilder.rawTokenTypeStart(-1); + int endOffset = myBuilder.rawTokenTypeStart(0); + CharSequence tokenChars = myBuilder.getOriginalText().subSequence(startOffset, endOffset); + if (!(tokenChars.length() > 2 && tokenChars.subSequence(tokenChars.length() - 2, tokenChars.length()).toString().equals("*/"))) { + PsiBuilder.Marker marker = myBuilder.mark(); + marker.error("Unclosed comment"); + marker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_RIGHT_BINDER, null); + } + } + } + void parseTypeCodeFragment() { PsiBuilder.Marker marker = mark(); parseTypeRef(); diff --git a/compiler/testData/psi/BlockCommentAtBeginningOfFile1.txt b/compiler/testData/psi/BlockCommentAtBeginningOfFile1.txt index 8272f460860..921f7144f27 100644 --- a/compiler/testData/psi/BlockCommentAtBeginningOfFile1.txt +++ b/compiler/testData/psi/BlockCommentAtBeginningOfFile1.txt @@ -3,4 +3,6 @@ JetFile: BlockCommentAtBeginningOfFile1.kt IMPORT_LIST - PsiComment(BLOCK_COMMENT)('/*') \ No newline at end of file + PsiComment(BLOCK_COMMENT)('/*') + PsiErrorElement:Unclosed comment + \ No newline at end of file diff --git a/compiler/testData/psi/BlockCommentAtBeginningOfFile2.txt b/compiler/testData/psi/BlockCommentAtBeginningOfFile2.txt index 8827e015c37..c1f9531922b 100644 --- a/compiler/testData/psi/BlockCommentAtBeginningOfFile2.txt +++ b/compiler/testData/psi/BlockCommentAtBeginningOfFile2.txt @@ -3,4 +3,6 @@ JetFile: BlockCommentAtBeginningOfFile2.kt IMPORT_LIST - PsiComment(BLOCK_COMMENT)('/*\n/*') \ No newline at end of file + PsiComment(BLOCK_COMMENT)('/*\n/*') + PsiErrorElement:Unclosed comment + \ No newline at end of file diff --git a/compiler/testData/psi/BlockCommentAtBeginningOfFile3.txt b/compiler/testData/psi/BlockCommentAtBeginningOfFile3.txt index 610b6f178d4..6f4e10519e9 100644 --- a/compiler/testData/psi/BlockCommentAtBeginningOfFile3.txt +++ b/compiler/testData/psi/BlockCommentAtBeginningOfFile3.txt @@ -3,4 +3,6 @@ JetFile: BlockCommentAtBeginningOfFile3.kt IMPORT_LIST - PsiComment(BLOCK_COMMENT)('/*\n\nfooo') \ No newline at end of file + PsiComment(BLOCK_COMMENT)('/*\n\nfooo') + PsiErrorElement:Unclosed comment + \ No newline at end of file diff --git a/compiler/testData/psi/BlockCommentAtBeginningOfFile4.txt b/compiler/testData/psi/BlockCommentAtBeginningOfFile4.txt index e16f5e1ab8b..e6b9cf8a907 100644 --- a/compiler/testData/psi/BlockCommentAtBeginningOfFile4.txt +++ b/compiler/testData/psi/BlockCommentAtBeginningOfFile4.txt @@ -3,4 +3,6 @@ JetFile: BlockCommentAtBeginningOfFile4.kt IMPORT_LIST - PsiComment(BLOCK_COMMENT)('/*\n\n/*foo*/\n\nasdfas') \ No newline at end of file + PsiComment(BLOCK_COMMENT)('/*\n\n/*foo*/\n\nasdfas') + PsiErrorElement:Unclosed comment + \ No newline at end of file diff --git a/compiler/testData/psi/BlockCommentUnmatchedClosing_ERR.kt b/compiler/testData/psi/BlockCommentUnmatchedClosing_ERR.kt new file mode 100644 index 00000000000..742b9903d7f --- /dev/null +++ b/compiler/testData/psi/BlockCommentUnmatchedClosing_ERR.kt @@ -0,0 +1,3 @@ +fun f() { + */ +} \ No newline at end of file diff --git a/compiler/testData/psi/BlockCommentUnmatchedClosing_ERR.txt b/compiler/testData/psi/BlockCommentUnmatchedClosing_ERR.txt new file mode 100644 index 00000000000..f7c8eb23607 --- /dev/null +++ b/compiler/testData/psi/BlockCommentUnmatchedClosing_ERR.txt @@ -0,0 +1,22 @@ +JetFile: BlockCommentUnmatchedClosing_ERR.kt + PACKAGE_DIRECTIVE + + IMPORT_LIST + + FUN + PsiElement(fun)('fun') + PsiWhiteSpace(' ') + PsiElement(IDENTIFIER)('f') + VALUE_PARAMETER_LIST + PsiElement(LPAR)('(') + PsiElement(RPAR)(')') + PsiWhiteSpace(' ') + BLOCK + PsiElement(LBRACE)('{') + PsiWhiteSpace('\n ') + PsiErrorElement:Expecting an element + PsiElement(MUL)('*') + PsiErrorElement:Unexpected tokens (use ';' to separate expressions on the same line) + PsiElement(DIV)('/') + PsiWhiteSpace('\n') + PsiElement(RBRACE)('}') \ No newline at end of file diff --git a/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile1.txt b/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile1.txt index c4a62c14d8e..a65814b78f7 100644 --- a/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile1.txt +++ b/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile1.txt @@ -7,3 +7,5 @@ JetFile: DocCommentAtBeginningOfFile1.kt PsiElement(KDOC_START)('/**') KDOC_SECTION + PsiErrorElement:Unclosed comment + \ No newline at end of file diff --git a/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile2.txt b/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile2.txt index beba9bf27c5..d415c34701d 100644 --- a/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile2.txt +++ b/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile2.txt @@ -8,3 +8,5 @@ JetFile: DocCommentAtBeginningOfFile2.kt PsiWhiteSpace('\n') KDOC_SECTION PsiElement(KDOC_TEXT)('/**') + PsiErrorElement:Unclosed comment + \ No newline at end of file diff --git a/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile3.txt b/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile3.txt index ce8cc94e466..3450cbf34a9 100644 --- a/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile3.txt +++ b/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile3.txt @@ -8,3 +8,5 @@ JetFile: DocCommentAtBeginningOfFile3.kt PsiWhiteSpace('\n\n') KDOC_SECTION PsiElement(KDOC_TEXT)('fooo') + PsiErrorElement:Unclosed comment + \ No newline at end of file diff --git a/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile4.txt b/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile4.txt index b216b8b6649..190a1e01810 100644 --- a/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile4.txt +++ b/compiler/testData/psi/kdoc/DocCommentAtBeginningOfFile4.txt @@ -10,3 +10,5 @@ JetFile: DocCommentAtBeginningOfFile4.kt PsiElement(KDOC_TEXT)('/**foo*/') PsiWhiteSpace('\n\n') PsiElement(KDOC_TEXT)('asdfas') + PsiErrorElement:Unclosed comment + \ No newline at end of file diff --git a/compiler/testData/psi/kdoc/Incomplete.txt b/compiler/testData/psi/kdoc/Incomplete.txt index 87b3be828b6..2d53e7d96b9 100644 --- a/compiler/testData/psi/kdoc/Incomplete.txt +++ b/compiler/testData/psi/kdoc/Incomplete.txt @@ -8,3 +8,5 @@ JetFile: Incomplete.kt PsiWhiteSpace('\n ') KDOC_SECTION PsiElement(KDOC_TEXT)('contents') + PsiErrorElement:Unclosed comment + \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java index 905f53ff5cf..064cd27ac66 100644 --- a/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java @@ -97,6 +97,12 @@ public class ParsingTestGenerated extends AbstractParsingTest { doParsingTest(fileName); } + @TestMetadata("BlockCommentUnmatchedClosing_ERR.kt") + public void testBlockCommentUnmatchedClosing_ERR() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/psi/BlockCommentUnmatchedClosing_ERR.kt"); + doParsingTest(fileName); + } + @TestMetadata("ByClauses.kt") public void testByClauses() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/psi/ByClauses.kt");