llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Replace /* ... */ single-line comments with // ... comments

Open LegalizeAdulthood opened this issue 11 years ago • 12 comments

Bugzilla Link 24467
Version unspecified
OS Windows NT

Extended Description

Detect when C-style /.../ block comments are used to create a single-line comment. Replace those block-style comments with // style comments.

Possible false positives:

/* ....\ */

comments could result in a // comment like this:

// ...\

which would escape the newline and be a problem.

LegalizeAdulthood avatar Aug 15 '15 02:08 LegalizeAdulthood

Proposed check name: modernize-use-single-line-comments

LegalizeAdulthood avatar Feb 04 '16 01:02 LegalizeAdulthood

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Assign the issue to you.
  2. Fix the issue locally.
  3. Run the test suite locally. 3.1) Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  4. Create a git commit
  5. Run git clang-format HEAD~1 to format your changes.
  6. Submit the patch to Phabricator. 6.1) Detailed instructions can be found here

For more instructions on how to submit a patch to LLVM, see our documentation.

If you have any further questions about this issue, don't hesitate to ask via a comment on this Github issue.

@llvm/issue-subscribers-good-first-issue

llvmbot avatar Aug 25 '23 20:08 llvmbot

@PiotrZSL can you provide some analysis and points of entry into codebase?

Endilll avatar Aug 30 '23 13:08 Endilll

Create new check, call it modernize-use-double-slash-comments, modernize-use-cpp-comment-style or something else.

  1. Register new comment handler in registerPPCallbacks with: PP->addCommentHandler
  2. Implement clang::CommentHandler interface
  3. Read current comment by using StringRef CommentText = Lexer::getSourceText(CharSourceRange::getCharRange(CommentRange), PP.getSourceManager(), PP.getLangOpts());
  4. Verify if its block comment for example matching against regexp: "^[ \t]*/\\*+[ \t\n]*(.*[ \t\n]*)*[ \t\n]*\\*+/[ \t\n]*$" (or implement this in other way)
  5. Add support to exclude some comments that are for example Doxygen comment (regexp) ?
  6. Add option to detect / remove frames (those ***** or #######)
  7. Generate fixes that would replace current comment with new one.

PiotrZSL avatar Aug 30 '23 14:08 PiotrZSL

I would like to take up the issue if nobody is working as this would be a good start for me to begin llvm. So, please correct me if I am wrong. We have to implement a check that converts /* styled comment that is used for single line to // style. While doing that we have to take care of the like Doxygen comment (as @PiotrZSL mentioned) in clang-tidy.

I am new to this project please help me out.

akshaykumars614 avatar Sep 28 '23 08:09 akshaykumars614

@chaitanyav , Are you still working on it ? I think I can work on it ! @PiotrZSL @EugeneZelenko .

4m4n-x-B4w4ne avatar Jan 05 '25 07:01 4m4n-x-B4w4ne

I think that an option called LineThreshold can be very useful for tuning behavior of this check. In some codebases users may not want to replace big chunks of comments from C-style to C++ style since text can lose readability. For example

/*
10 lines of text
10 lines of text
10 lines of text
*/
void foo()

is arguably easier to read than

// 10 lines of text
// 10 lines of text
// 10 lines of text
void foo()

With LineThreshold set to 5 such big chunks will be excluded from checking.

vbvictor avatar Jan 24 '25 20:01 vbvictor

@vbvictor I wouldn't expect this to cause much of a change, since the check requires us to change single-line C-style comments. However, if that is not the case, I think another case to look into would be the use of doxygen style comments, which, irrespective of the LineThreshold, should be excluded from checking. Also, @4m4n-x-B4w4ne if nobody is currently working on this issue, I would like to take it up Thanks!

RoshanYSingh23 avatar Feb 08 '26 14:02 RoshanYSingh23

This is very much a stylistic type of check and I have no objections to the proposed options. For doxygen style comments one can ues the /// form of doxygen comment.

LegalizeAdulthood avatar Feb 08 '26 18:02 LegalizeAdulthood

Hi @LegalizeAdulthood @PiotrZSL @EugeneZelenko,

Can you please assign this to me if @4m4n-x-B4w4ne is not working on it anymore?

Thanks

ashu3103 avatar Mar 28 '26 12:03 ashu3103

@vbvictor wrote:

I think that an option called LineThreshold can be very useful for tuning behavior of this check.

I will just point out that the original request was for single-line comments, not multi-line comments.

LegalizeAdulthood avatar Mar 28 '26 23:03 LegalizeAdulthood

@vbvictor wrote:

I think that an option called LineThreshold can be very useful for tuning behavior of this check.

I will just point out that the original request was for single-line comments, not multi-line comments.

Yeah, so if I'm not wrong only single-line C-like block comments have to be replaced with '//' handling the false positives mentioned above and excluding any comments for doxygen.

ashu3103 avatar Mar 29 '26 04:03 ashu3103