Skip to content

Commit 0bee2b5

Browse files
committed
[clang-tidy]
modernize-use-cpp-style-comments check finds C style comments and suggests to use C++ style comments Fixes #24841
1 parent 56ffbd9 commit 0bee2b5

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

clang-tools-extra/clang-tidy/modernize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ add_clang_library(clangTidyModernizeModule
3131
UseAutoCheck.cpp
3232
UseBoolLiteralsCheck.cpp
3333
UseConstraintsCheck.cpp
34+
UseCppStyleCommentsCheck.cpp
3435
UseDefaultMemberInitCheck.cpp
3536
UseDesignatedInitializersCheck.cpp
3637
UseEmplaceCheck.cpp

clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "UseAutoCheck.h"
3333
#include "UseBoolLiteralsCheck.h"
3434
#include "UseConstraintsCheck.h"
35+
#include "UseCppStyleCommentsCheck.h"
3536
#include "UseDefaultMemberInitCheck.h"
3637
#include "UseDesignatedInitializersCheck.h"
3738
#include "UseEmplaceCheck.h"
@@ -104,6 +105,8 @@ class ModernizeModule : public ClangTidyModule {
104105
"modernize-use-bool-literals");
105106
CheckFactories.registerCheck<UseConstraintsCheck>(
106107
"modernize-use-constraints");
108+
CheckFactories.registerCheck<UseCppStyleCommentsCheck>(
109+
"modernize-use-cpp-style-comments");
107110
CheckFactories.registerCheck<UseDefaultMemberInitCheck>(
108111
"modernize-use-default-member-init");
109112
CheckFactories.registerCheck<UseEmplaceCheck>("modernize-use-emplace");
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===--- UseCppStyleCommentsCheck.cpp - clang-tidy-------------------------===//
2+
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "UseCppStyleCommentsCheck.h"
11+
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
#include "clang/Lex/Lexer.h"
13+
#include "clang/Lex/Preprocessor.h"
14+
15+
using namespace clang::ast_matchers;
16+
17+
namespace clang::tidy::modernize {
18+
19+
class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler {
20+
public:
21+
CStyleCommentHandler(UseCppStyleCommentsCheck &Check)
22+
: Check(Check),
23+
CStyleCommentMatch(
24+
"^[ \t]*/\\*+[ \t\n]*(.*[ \t\n]*)*[ \t\n]*\\*+/[ \t\n]*$") {}
25+
26+
bool HandleComment(Preprocessor &PP, SourceRange Range) override {
27+
28+
if (Range.getBegin().isMacroID() ||
29+
PP.getSourceManager().isInSystemHeader(Range.getBegin()))
30+
return false;
31+
32+
const StringRef Text =
33+
Lexer::getSourceText(CharSourceRange::getCharRange(Range),
34+
PP.getSourceManager(), PP.getLangOpts());
35+
36+
SmallVector<StringRef> Matches;
37+
if (!CStyleCommentMatch.match(Text, &Matches)) {
38+
return false;
39+
}
40+
41+
Check.diag(
42+
Range.getBegin(),
43+
"use C++ style comments '//' instead of C style comments '/*...*/'")
44+
<< FixItHint::CreateRemoval(CharSourceRange::getCharRange(Range));
45+
46+
return false;
47+
}
48+
49+
private:
50+
UseCppStyleCommentsCheck &Check;
51+
llvm::Regex CStyleCommentMatch;
52+
};
53+
54+
UseCppStyleCommentsCheck::UseCppStyleCommentsCheck(StringRef Name,
55+
ClangTidyContext *Context)
56+
: ClangTidyCheck(Name, Context),
57+
Handler(std::make_unique<CStyleCommentHandler>(*this)) {}
58+
59+
void UseCppStyleCommentsCheck::registerPPCallbacks(
60+
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
61+
PP->addCommentHandler(Handler.get());
62+
}
63+
64+
void UseCppStyleCommentsCheck::check(const MatchFinder::MatchResult &Result) {}
65+
66+
UseCppStyleCommentsCheck::~UseCppStyleCommentsCheck() = default;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===--- UseCppStyleCommentsCheck.h - clang-tidy---------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_CPP_STYLE_COMMENTS_CHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_CPP_STYLE_COMMENTS_CHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::modernize {
15+
/// detects C-style comments and suggests to use C++ style comments instead
16+
///
17+
/// For the user-facing documentation see:
18+
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-cpp-style-comments.html
19+
class UseCppStyleCommentsCheck : public ClangTidyCheck {
20+
public:
21+
UseCppStyleCommentsCheck(StringRef Name, ClangTidyContext *Context);
22+
~UseCppStyleCommentsCheck() override;
23+
24+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
25+
return LangOpts.CPlusPlus;
26+
}
27+
28+
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
29+
Preprocessor *ModuleExpanderPP) override;
30+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
31+
32+
private:
33+
class CStyleCommentHandler;
34+
std::unique_ptr<CStyleCommentHandler> Handler;
35+
};
36+
} // namespace clang::tidy::modernize
37+
38+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_CPP_STYLE_COMMENTS_CHECK_H

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ New checks
172172
Detects variables and functions that can be marked as static or moved into
173173
an anonymous namespace to enforce internal linkage.
174174

175+
- New :doc:`modernize-use-cpp-style-comments
176+
<clang-tidy/checks/modernize/use-cpp-style-comments>` check.
177+
178+
Find C Style comments and suggests to use C++ style comments instead
179+
175180
- New :doc:`modernize-min-max-use-initializer-list
176181
<clang-tidy/checks/modernize/min-max-use-initializer-list>` check.
177182

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. title:: clang-tidy - modernize-use-cpp-style-comments
2+
3+
modernize-use-cpp-style-comments
4+
================================
5+
6+
Finds C-style comments and suggests to use C++ style comments `//`.
7+
8+
9+
.. code-block:: c++
10+
11+
memcpy(a, b, sizeof(int) * 5); /* use std::copy_n instead of memcpy */
12+
// warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %check_clang_tidy -std=c++11 %s modernize-use-cpp-style-comments %t
2+
3+
static auto PI = 3.14159265; /* value of pi upto 8 decimal places */
4+
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
5+
6+
/******************************************************
7+
* Fancy frame comment goes here
8+
*******************************************************/
9+
// CHECK-MESSAGES: :[[@LINE-3]]:1: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
10+
11+
/** \brief Brief description.
12+
* Brief description continued.
13+
*
14+
* Detailed description starts here. (this is a doxygen comment)
15+
*/
16+
// CHECK-MESSAGES: :[[@LINE-5]]:1: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]

0 commit comments

Comments
 (0)