Skip to content

Commit edc768b

Browse files
authored
Merge pull request #11707 from smowton/smowton/fix/java-empty-multiline-comment
Java: handle printing an empty comment (/**/); add relevant tests
2 parents 52cafdf + 2ca56e0 commit edc768b

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: fix
3+
---
4+
* We now correctly handle empty block comments, like `/**/`. Previously these could be mistaken for Javadoc comments and led to attribution of Javadoc tags to the wrong declaration.

java/ql/lib/semmle/code/java/Javadoc.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ class Javadoc extends JavadocParent, @javadoc {
3333
string getAuthor() { result = this.getATag("@author").getChild(0).toString() }
3434

3535
override string toString() {
36-
result = this.toStringPrefix() + this.getChild(0) + this.toStringPostfix()
36+
exists(string childStr |
37+
if exists(this.getChild(0)) then childStr = this.getChild(0).toString() else childStr = ""
38+
|
39+
result = this.toStringPrefix() + childStr + this.toStringPostfix()
40+
)
3741
}
3842

3943
private string toStringPrefix() {
@@ -48,7 +52,7 @@ class Javadoc extends JavadocParent, @javadoc {
4852
if isEolComment(this)
4953
then result = ""
5054
else (
51-
if strictcount(this.getAChild()) = 1 then result = " */" else result = " ... */"
55+
if strictcount(this.getAChild()) > 1 then result = " ... */" else result = " */"
5256
)
5357
}
5458

java/ql/test/library-tests/comments/PrintAst.expected

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ Test.java:
1414
# 21| 3: [Method] test
1515
# 21| 3: [TypeAccess] void
1616
# 21| 5: [BlockStmt] { ... }
17+
# 23| 4: [Method] method1
18+
# 23| 3: [TypeAccess] void
19+
# 23| 5: [BlockStmt] { ... }
20+
# 24| 5: [Method] method2
21+
# 24| 3: [TypeAccess] void
22+
# 24| 5: [BlockStmt] { ... }
23+
# 28| 6: [Method] method3
24+
#-----| 0: (Javadoc)
25+
# 25| 1: [Javadoc] /** JavaDoc for method3 */
26+
# 26| 0: [JavadocText] JavaDoc for method3
27+
# 28| 3: [TypeAccess] void
28+
# 28| 5: [BlockStmt] { ... }
1729
TestWindows.java:
1830
# 0| [CompilationUnit] TestWindows
1931
# 5| 1: [Class] TestWindows

java/ql/test/library-tests/comments/Test.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ void m() {
1919
// an end-of-line comment with trailing whitespace
2020
//an end-of-line comment without a leading space
2121
void test() {} // an end-of-line comment with preceding code
22+
23+
void method1() { /**/ } // A block comment containing the /** JavaDoc prefix }
24+
void method2() { }
25+
/**
26+
* JavaDoc for method3
27+
*/
28+
void method3() { }
2229
}

java/ql/test/library-tests/comments/toString.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
| Test.java:19:2:19:59 | // an end-of-line comment with trailing whitespace |
99
| Test.java:20:2:20:49 | //an end-of-line comment without a leading space |
1010
| Test.java:21:17:21:61 | // an end-of-line comment with preceding code |
11+
| Test.java:23:26:23:29 | /* */ |
12+
| Test.java:23:33:23:86 | // A block comment containing the /** JavaDoc prefix } |
13+
| Test.java:25:9:27:11 | /** JavaDoc for method3 */ |
1114
| TestWindows.java:1:1:4:3 | /** A JavaDoc comment ... */ |
1215
| TestWindows.java:6:2:6:45 | /** A JavaDoc comment with a single line. */ |
1316
| TestWindows.java:8:3:8:27 | // a single-line comment |

0 commit comments

Comments
 (0)