Skip to content

Commit e9c899f

Browse files
committed
Added Options for Doxygen Comments
1 parent 2deb9d2 commit e9c899f

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ using namespace clang::ast_matchers;
1717
namespace clang::tidy::readability {
1818
class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler {
1919
public:
20-
CStyleCommentHandler(UseCppStyleCommentsCheck &Check)
20+
CStyleCommentHandler(UseCppStyleCommentsCheck &Check, bool ExcludeDoxygen)
2121
: Check(Check),
2222
CStyleCommentMatch(
23-
"^[ \t]*/\\*+[ \t\r\n]*(.*[ \t\r\n]*)*[ \t\r\n]*\\*+/[ \t\r\n]*$") {
24-
}
23+
"^[ \t]*/\\*+[ \t\r\n]*(.*[ \t\r\n]*)*[ \t\r\n]*\\*+/[ \t\r\n]*$"),
24+
ExcludeDoxygen(ExcludeDoxygen) {}
25+
26+
void setExcludeDoxygen(bool Exclude) { ExcludeDoxygen = Exclude; }
27+
28+
bool isExcludeDoxygen() const { return ExcludeDoxygen; }
2529

2630
std::string convertToCppStyleComment(const SourceManager &SM,
2731
const SourceRange &Range) {
@@ -58,6 +62,14 @@ class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler {
5862
return Result;
5963
}
6064

65+
bool isDoxygenStyleComment(const StringRef &Text) {
66+
StringRef Trimmed = Text.ltrim();
67+
return Trimmed.starts_with("/**") || Trimmed.starts_with("/*!") ||
68+
Trimmed.starts_with("///") || Trimmed.starts_with("//!") ||
69+
(Trimmed.starts_with("/*") &&
70+
Trimmed.drop_front(2).starts_with("*"));
71+
}
72+
6173
bool CheckForInlineComments(Preprocessor &PP, SourceRange Range) {
6274
const SourceManager &SM = PP.getSourceManager();
6375
const SourceLocation CommentStart = Range.getBegin();
@@ -100,6 +112,10 @@ class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler {
100112
const StringRef Text = Lexer::getSourceText(
101113
CharSourceRange::getCharRange(Range), SM, PP.getLangOpts());
102114

115+
if (ExcludeDoxygen && isDoxygenStyleComment(Text)) {
116+
return false;
117+
}
118+
103119
SmallVector<StringRef> Matches;
104120
if (!CStyleCommentMatch.match(Text, &Matches)) {
105121
return false;
@@ -120,13 +136,15 @@ class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler {
120136

121137
private:
122138
UseCppStyleCommentsCheck &Check;
139+
bool ExcludeDoxygen;
123140
llvm::Regex CStyleCommentMatch;
124141
};
125142

126143
UseCppStyleCommentsCheck::UseCppStyleCommentsCheck(StringRef Name,
127144
ClangTidyContext *Context)
128145
: ClangTidyCheck(Name, Context),
129-
Handler(std::make_unique<CStyleCommentHandler>(*this)) {}
146+
Handler(std::make_unique<CStyleCommentHandler>(
147+
*this, Options.get("ExcludeDoxygenStyleComments", false))) {}
130148

131149
void UseCppStyleCommentsCheck::registerPPCallbacks(
132150
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
@@ -135,5 +153,10 @@ void UseCppStyleCommentsCheck::registerPPCallbacks(
135153

136154
void UseCppStyleCommentsCheck::check(const MatchFinder::MatchResult &Result) {}
137155

156+
void UseCppStyleCommentsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
157+
Options.store(Opts, "ExcludeDoxygenStyleComments",
158+
Handler->isExcludeDoxygen());
159+
}
160+
138161
UseCppStyleCommentsCheck::~UseCppStyleCommentsCheck() = default;
139-
} // namespace clang::tidy::readability
162+
} // namespace clang::tidy::readability

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
namespace clang::tidy::readability {
1616
/// Detects C Style comments and suggests to use C++ style comments instead.
1717
///
18+
/// Optionally excludes Doxygen-style comments.
19+
///
1820
/// For the user-facing documentation see:
1921
/// http://clang.llvm.org/extra/clang-tidy/checks/readability/use-cpp-style-comments.html
2022
class UseCppStyleCommentsCheck : public ClangTidyCheck {
@@ -32,9 +34,13 @@ class UseCppStyleCommentsCheck : public ClangTidyCheck {
3234

3335
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
3436

37+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
38+
3539
private:
3640
class CStyleCommentHandler;
3741
std::unique_ptr<CStyleCommentHandler> Handler;
42+
const bool ExcludeDoxygenStyleComments =
43+
Options.get("ExcludeDoxygenStyleComments", false);
3844
};
3945
} // namespace clang::tidy::readability
4046

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

Lines changed: 34 additions & 11 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 -- --
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}]}"
22

33
// Single-line full C-style comment
44
static const int CONSTANT = 42; /* Important constant value */
@@ -24,16 +24,6 @@ void processData(int data /* input data */,
2424
fnWithSomeBools(/*ControlsA=*/ true, /*ControlsB=*/ false);
2525
}
2626

27-
// Multiline comment with asterisk styling
28-
/*******************************
29-
* Block comment with asterisks
30-
* Should be converted to C++ style
31-
*******************************/
32-
// CHECK-MESSAGES: :[[@LINE-4]]:1: warning: use C++ style comments '//' instead of C style comments '/*...*/' [readability-use-cpp-style-comments]
33-
// CHECK-FIXES: // ******************************
34-
// CHECK-FIXES: // * Block comment with asterisks
35-
// CHECK-FIXES: // * Should be converted to C++ style
36-
// CHECK-FIXES: // ******************************
3727
int calculateSomething() { return 1;}
3828
// Comment at end of complex line
3929
int complexCalculation = calculateSomething(); /* Result of complex calculation */
@@ -66,3 +56,36 @@ void complexFunction() {
6656
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use C++ style comments '//' instead of C style comments '/*...*/' [readability-use-cpp-style-comments]
6757
// CHECK-FIXES: int x = 10; // Inline comment not to be transformed
6858
}
59+
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();
66+
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();

0 commit comments

Comments
 (0)