Skip to content

Commit b5f255f

Browse files
committed
Improved Logic and added new test file
1 parent a02263f commit b5f255f

File tree

4 files changed

+65
-59
lines changed

4 files changed

+65
-59
lines changed

clang-tools-extra/clang-tidy/readability/UseCppStyleCommentsCheck.cpp

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,36 +70,23 @@ class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler {
7070
Trimmed.drop_front(2).starts_with("*"));
7171
}
7272

73-
bool CheckForInlineComments(Preprocessor &PP, SourceRange Range) {
73+
bool CheckForTextAfterComment(Preprocessor &PP, SourceRange Range) {
7474
const SourceManager &SM = PP.getSourceManager();
75-
const SourceLocation CommentStart = Range.getBegin();
7675
const SourceLocation CommentEnd = Range.getEnd();
7776

78-
unsigned StartLine = SM.getSpellingLineNumber(CommentStart);
7977
unsigned EndLine = SM.getSpellingLineNumber(CommentEnd);
80-
81-
const StringRef Text = Lexer::getSourceText(
82-
CharSourceRange::getCharRange(Range), SM, PP.getLangOpts());
83-
84-
if (StartLine == EndLine) {
85-
const SourceLocation LineBegin =
86-
SM.translateLineCol(SM.getFileID(CommentStart), StartLine, 1);
87-
const SourceLocation LineEnd =
88-
SM.translateLineCol(SM.getFileID(CommentEnd), EndLine,
89-
std::numeric_limits<unsigned>::max());
90-
const StringRef LineContent = Lexer::getSourceText(
91-
CharSourceRange::getCharRange(LineBegin, LineEnd), SM,
92-
PP.getLangOpts());
93-
const size_t CommentStartOffset =
94-
SM.getSpellingColumnNumber(CommentStart) - 1;
95-
const StringRef AfterComment =
96-
LineContent.drop_front(CommentStartOffset + Text.size());
97-
98-
if (!AfterComment.trim().empty()) {
99-
return true;
100-
}
101-
}
102-
return false;
78+
unsigned EndCol = SM.getSpellingColumnNumber(CommentEnd);
79+
80+
const SourceLocation LineBegin =
81+
SM.translateLineCol(SM.getFileID(CommentEnd),EndLine, EndCol);
82+
const SourceLocation LineEnd =
83+
SM.translateLineCol(SM.getFileID(CommentEnd), EndLine,
84+
std::numeric_limits<unsigned>::max());
85+
const StringRef AfterComment = Lexer::getSourceText(
86+
CharSourceRange::getCharRange(LineBegin, LineEnd), SM,
87+
PP.getLangOpts());
88+
89+
return !AfterComment.trim().empty();
10390
}
10491

10592
bool HandleComment(Preprocessor &PP, SourceRange Range) override {
@@ -121,7 +108,7 @@ class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler {
121108
return false;
122109
}
123110

124-
if (CheckForInlineComments(PP, Range)) {
111+
if (CheckForTextAfterComment(PP, Range)) {
125112
return false;
126113
}
127114

clang-tools-extra/docs/clang-tidy/checks/readability/use-cpp-style-comments.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ Output:
4141
.. code-block:: c++
4242

4343
int a = /* this is a comment */ 5;
44+
45+
Options
46+
-------
47+
48+
.. option:: ExcludeDoxygenStyleComments
49+
50+
A boolean option that determines whether Doxygen-style comments should be excluded.
51+
By default, this option is set to ``false``.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %check_clang_tidy -std=c++11 %s readability-use-cpp-style-comments %t -config="{CheckOptions: [{key: readability-use-cpp-style-comments.ExcludeDoxygenStyleComments, value: true}]}"
2+
3+
// Tests for Doxygen comments with ExcludeDoxygenStyleComments enabled
4+
/**
5+
* This is a Doxygen comment for a function.
6+
* It should NOT be transformed.
7+
*/
8+
void doxygenFunction1();
9+
10+
/*!
11+
* This is another Doxygen-style comment.
12+
* It should also NOT be transformed.
13+
*/
14+
void doxygenFunction2();
15+
16+
/**
17+
* Multiline Doxygen comment describing parameters.
18+
*
19+
* @param x The first parameter.
20+
* @param y The second parameter.
21+
* @return A result value.
22+
*/
23+
int doxygenFunctionWithParams(int x, int y);
24+
25+
/*******************************
26+
* Non-Doxygen block comments without markers
27+
*******************************/
28+
void DoxygenBlock();
29+
30+
/*!
31+
* This is a single-line Doxygen comment.
32+
* Should NOT be transformed.
33+
*/
34+
void singleLineDoxygen();

clang-tools-extra/test/clang-tidy/checkers/readability/use-cpp-style-comments.cpp

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy -std=c++11 %s readability-use-cpp-style-comments %t -config="{CheckOptions: [{key: readability-use-cpp-style-comments.ExcludeDoxygenStyleComments, value: true}]}"
1+
// RUN: %check_clang_tidy -std=c++11 %s readability-use-cpp-style-comments %t
22

33
// Single-line full C-style comment
44
static const int CONSTANT = 42; /* Important constant value */
@@ -57,35 +57,12 @@ void complexFunction() {
5757
// CHECK-FIXES: int x = 10; // Inline comment not to be transformed
5858
}
5959

60-
// Tests for Doxygen comments with ExcludeDoxygenStyleComments enabled
61-
/**
62-
* This is a Doxygen comment for a function.
63-
* It should NOT be transformed.
64-
*/
65-
void doxygenFunction1();
60+
/* aaa
61+
bbbb
62+
ccc */ int z = 1;
63+
// There is a text after the comment ends so it should be ignored.
6664

67-
/*!
68-
* This is another Doxygen-style comment.
69-
* It should also NOT be transformed.
70-
*/
71-
void doxygenFunction2();
72-
73-
/**
74-
* Multiline Doxygen comment describing parameters.
75-
*
76-
* @param x The first parameter.
77-
* @param y The second parameter.
78-
* @return A result value.
79-
*/
80-
int doxygenFunctionWithParams(int x, int y);
81-
82-
/*******************************
83-
* Non-Doxygen block comments without markers
84-
*******************************/
85-
void DoxygenBlock();
86-
87-
/*!
88-
* This is a single-line Doxygen comment.
89-
* Should NOT be transformed.
90-
*/
91-
void singleLineDoxygen();
65+
int y = 10;/* aaa
66+
bbbb
67+
ccc */ int z1 = 1;
68+
// There is a text after the comment ends so it should be ignored.

0 commit comments

Comments
 (0)