Skip to content

Commit 3eece6e

Browse files
author
Pavel Rappo
committed
8341907: javac -Xlint should ignore /// on first line of source file
Reviewed-by: jjg
1 parent 5b9932f commit 3eece6e

File tree

8 files changed

+115
-2
lines changed

8 files changed

+115
-2
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public enum LintCategory {
181181
CLASSFILE("classfile"),
182182

183183
/**
184-
* Warn about"dangling" documentation comments,
184+
* Warn about "dangling" documentation comments,
185185
* not attached to any declaration.
186186
*/
187187
DANGLING_DOC_COMMENTS("dangling-doc-comments"),

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -667,14 +667,23 @@ void reportDanglingDocComment(Comment c) {
667667
var pos = c.getPos();
668668
if (pos != null) {
669669
deferredLintHandler.report(lint -> {
670-
if (lint.isEnabled(Lint.LintCategory.DANGLING_DOC_COMMENTS)) {
670+
if (lint.isEnabled(Lint.LintCategory.DANGLING_DOC_COMMENTS) &&
671+
!shebang(c, pos)) {
671672
log.warning(Lint.LintCategory.DANGLING_DOC_COMMENTS,
672673
pos, Warnings.DanglingDocComment);
673674
}
674675
});
675676
}
676677
}
677678

679+
/** Returns true for a comment that acts similarly to shebang in UNIX */
680+
private boolean shebang(Comment c, JCDiagnostic.DiagnosticPosition pos) {
681+
var src = log.currentSource();
682+
return c.getStyle() == Comment.CommentStyle.JAVADOC_LINE &&
683+
c.getPos().getStartPosition() == 0 &&
684+
src.getLineNumber(pos.getEndPosition(src.getEndPosTable())) == 1;
685+
}
686+
678687
/**
679688
* Ignores any recent documentation comments found by the scanner,
680689
* such as those that cannot be associated with a nearby declaration.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
// /nodynamiccopyright/
3+
4+
/** A class comment */
5+
public class JBangException1 {
6+
7+
/**
8+
* A method comment
9+
*
10+
* @param args a parameter comment
11+
*/
12+
public static void main(String[] args) {
13+
if (args.length == 0) {
14+
System.out.println("Hello World!");
15+
} else {
16+
System.out.println("Hello " + args[0]);
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
JBangException2.java:1:1: compiler.warn.dangling.doc.comment
2+
1 warning
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/** /usr/bin/env jbang "$0" "$@" ; exit $? */
2+
// /nodynamiccopyright/
3+
4+
/** A class comment */
5+
public class JBangException2 {
6+
7+
/**
8+
* A method comment
9+
*
10+
* @param args a parameter comment
11+
*/
12+
public static void main(String[] args) {
13+
if (args.length == 0) {
14+
System.out.println("Hello World!");
15+
} else {
16+
System.out.println("Hello " + args[0]);
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
JBangException3.java:1:1: compiler.warn.dangling.doc.comment
2+
1 warning
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// A
2+
/// multiline
3+
/// dangling
4+
/// comment
5+
// /nodynamiccopyright/
6+
7+
/** A class comment */
8+
public class JBangException3 {
9+
10+
/**
11+
* A method comment
12+
*
13+
* @param args a parameter comment
14+
*/
15+
public static void main(String[] args) {
16+
if (args.length == 0) {
17+
System.out.println("Hello World!");
18+
} else {
19+
System.out.println("Hello " + args[0]);
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
*
27+
* @compile/ref=empty.out -XDrawDiagnostics JBangException1.java
28+
* @compile/ref=empty.out -XDrawDiagnostics -Xlint:dangling-doc-comments JBangException1.java
29+
*
30+
* @compile/ref=empty.out -XDrawDiagnostics JBangException2.java
31+
* @compile/ref=JBangException2.enabled.out -XDrawDiagnostics -Xlint:dangling-doc-comments JBangException2.java
32+
*
33+
* @compile/ref=empty.out -XDrawDiagnostics JBangException3.java
34+
* @compile/ref=JBangException3.enabled.out -XDrawDiagnostics -Xlint:dangling-doc-comments JBangException3.java
35+
*/
36+
37+
// The classes being tested reside in files separate from this one because
38+
// the classes need to provide the initial dangling comment, which would
39+
// otherwise interfere with the JTReg test comment. For similar reasons,
40+
// the files with test classes do __NOT__ have a copyright header.

0 commit comments

Comments
 (0)