-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8341907: javac -Xlint should ignore /// on first line of source file #21923
Conversation
👋 Welcome back prappo! A progress list of the required criteria for merging this PR into |
@pavelrappo This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 2 new commits pushed to the
Please see this link for an up-to-date comparison between the source branch of this pull request and the ➡️ To integrate this PR with the above commit message to the |
@pavelrappo The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
// otherwise interfere with the JTReg test comment. For similar reasons, | ||
// the files with test classes do __NOT__ have a copyright header. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, the normal guidance for source files that cannot follow standard stylistic guidance (such as files with tabs etc) is to dynamically generate the files, which is medium easy these days using text blocks. That being said, that advice does not work well in this case because the jtreg
infrastructure for @compile
tags does not work for generated files.
In this case, I would recommend working the text string nodynamiccopyright
into the head of the two JBang files. There are two or three possibilities.
- Just put the word
nodynamiccopyright
at the end of the first line. Syntactically, it would be a command name, but such a command would never be executed after theexit
statement. - Put a comment
#nodynamiccopyright
at the end of the first line. - Put a separate Java comment on the second line, using either an end-of-line comment or traditional comment. It might then be reasonably detected and taken into account by any automated scripts that ensure copyright headers are present unless that keyword is present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Number 3 seems to be the cleanest. I'll try it; thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, but since we're not testing the files here with JBang, all that matters is the recognition of the overall comment, regardless of its content. But #3 is OK too. The main thing is to have the keyword present so that any checkers scripts have explicit evidence that no comment is intended in these files.
if (lint.isEnabled(Lint.LintCategory.DANGLING_DOC_COMMENTS) && | ||
(c.getStyle() != Comment.CommentStyle.JAVADOC_LINE || | ||
c.getPos().getStartPosition() != 0)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consideration, you could further refine the test by making sure it is a single line comment (perhaps optionally terminated by a newline). In other words, maybe the warning should be given if the leading comment is a multi-line comment -- which would unlikely be a JBang-style comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I admit, I thought about it initially, but couldn't quickly come up with a simple and robust way to figure out if a comment tree spans a single line. I'm not yet comfortable working with compiler coordinate system (incl. source positions). If you propose such a way, I'd happily incorporate it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you propose such a way, I'd happily incorporate it.
Does the content of the comment (c.getText()
) contain a newline character that is not at the end of the content? Maybe something like
c.getText().matches("(?s)\\R.")
although there may be more effective ways to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regex sounds simple and robust. However, I thought we could concoct a more idiomatic compiler solution, using trees, start and end diagnostic positions, and also line numbers.
So, if a comment tree start and end position are on the same line that is also the first line of the file, we don't output the warning. Can we do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Computing line numbers is a relatively expensive operation, because the internal coordinates are character-offset based. That being said, there is a bunch of caching going on under the covers, and, the check can be guarded by the additional (proposed) check that the warning is only suppressed if the comment begins at offset 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, the method you want is DiagnosticSource.getLineNumber(int pos)
, which means you need a DiagnosticSource
object. JavacParser
(that is, this class) has a Log log
member, and [Abstract]Log
provides currentSource()
, so to get the line number for a position it should be possible to use something like log.currentSource().getLineNumber(pos)
, assuming that the current source is set up correctly at the time you need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, if a comment tree start and end position are on the same line that is also the first line of the file
The end position will always be after the start position, so you only need check if the line number of the end position of the comment is line 1. (Line numbers are 1-based.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jon, please have a look at the updated version and approve if it is to your liking; thanks.
My recollection is that this just a convenience to get the output of the compilation into the log without any |
/integrate |
Going to push as commit 3eece6e.
Your commit was automatically rebased without conflicts. |
@pavelrappo Pushed as commit 3eece6e. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Please review this PR to exempt the trick that JBang uses from the "dangling comment" lint introduced in JDK 23. For more information see this PR's JBS issue and its comment section.
The fix makes sure that the warning is not issued if it relates to a leading
///
comment. For simplicity and similarity with the shebang construct, the comment should start from the first character of the file, and not a more permissive first non-whitespace character of the file.Since I'm not an expert in
java.compiler
, I'm unsure if my code intercepts warning in the most appropriate layer. Please double-check that.Skimming through the tests which I used for inspiration,
test/langtools/tools/javac/danglingDocComments
, I was unsure why they compile itself first without any/ref=
construct. The test I introduced here does not do this; am I missing something?Progress
Warning
8341907: javac -Xlint should ignore /// on first line of source file
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/21923/head:pull/21923
$ git checkout pull/21923
Update a local copy of the PR:
$ git checkout pull/21923
$ git pull https://git.openjdk.org/jdk.git pull/21923/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 21923
View PR using the GUI difftool:
$ git pr show -t 21923
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/21923.diff
Using Webrev
Link to Webrev Comment