Skip to content

Commit ba3c7bb

Browse files
committed
improvements as suggested
1 parent 7150e04 commit ba3c7bb

File tree

8 files changed

+28
-29
lines changed

8 files changed

+28
-29
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ add_clang_library(clangTidyModernizeModule STATIC
3131
UseAutoCheck.cpp
3232
UseBoolLiteralsCheck.cpp
3333
UseConstraintsCheck.cpp
34-
UseCppStyleCommentsCheck.cpp
3534
UseDefaultMemberInitCheck.cpp
3635
UseDesignatedInitializersCheck.cpp
3736
UseEmplaceCheck.cpp

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "UseAutoCheck.h"
3333
#include "UseBoolLiteralsCheck.h"
3434
#include "UseConstraintsCheck.h"
35-
#include "UseCppStyleCommentsCheck.h"
3635
#include "UseDefaultMemberInitCheck.h"
3736
#include "UseDesignatedInitializersCheck.h"
3837
#include "UseEmplaceCheck.h"
@@ -108,8 +107,6 @@ class ModernizeModule : public ClangTidyModule {
108107
"modernize-use-bool-literals");
109108
CheckFactories.registerCheck<UseConstraintsCheck>(
110109
"modernize-use-constraints");
111-
CheckFactories.registerCheck<UseCppStyleCommentsCheck>(
112-
"modernize-use-cpp-style-comments");
113110
CheckFactories.registerCheck<UseDefaultMemberInitCheck>(
114111
"modernize-use-default-member-init");
115112
CheckFactories.registerCheck<UseEmplaceCheck>("modernize-use-emplace");

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//===--- UseCppStyleCommentsCheck.cpp - clang-tidy-------------------------===//
2-
32
//
43
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54
// See https://llvm.org/LICENSE.txt for license information.
@@ -11,7 +10,7 @@
1110
#include "clang/ASTMatchers/ASTMatchFinder.h"
1211
#include "clang/Lex/Lexer.h"
1312
#include "clang/Lex/Preprocessor.h"
14-
#include <sstream.h>
13+
#include <sstream>
1514

1615
using namespace clang::ast_matchers;
1716

@@ -67,17 +66,20 @@ class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler {
6766
unsigned StartLine = SM.getSpellingLineNumber(CommentStart);
6867
unsigned EndLine = SM.getSpellingLineNumber(CommentEnd);
6968

69+
const StringRef Text = Lexer::getSourceText(
70+
CharSourceRange::getCharRange(Range), SM, PP.getLangOpts());
71+
7072
if (StartLine == EndLine) {
71-
SourceLocation LineBegin =
73+
const SourceLocation LineBegin =
7274
SM.translateLineCol(SM.getFileID(CommentStart), StartLine, 1);
73-
SourceLocation LineEnd =
75+
const SourceLocation LineEnd =
7476
SM.translateLineCol(SM.getFileID(CommentEnd), EndLine,
7577
std::numeric_limits<unsigned>::max());
76-
StringRef LineContent = Lexer::getSourceText(
78+
const StringRef LineContent = Lexer::getSourceText(
7779
CharSourceRange::getCharRange(LineBegin, LineEnd), SM,
7880
PP.getLangOpts());
79-
size_t CommentStartOffset = SM.getSpellingColumnNumber(CommentStart) - 1;
80-
StringRef AfterComment =
81+
const size_t CommentStartOffset = SM.getSpellingColumnNumber(CommentStart) - 1;
82+
const StringRef AfterComment =
8183
LineContent.drop_front(CommentStartOffset + Text.size());
8284

8385
if (!AfterComment.trim().empty()) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_USECPPSTYLECOMMENTSCHECK_H
1111

1212
#include "../ClangTidyCheck.h"
13+
#include <memory>
1314

1415
namespace clang::tidy::readability {
1516
/// Detects C Style comments and suggests to use C++ style comments instead.

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,17 @@ New checks
174174
Replace comparisons between signed and unsigned integers with their safe
175175
C++20 ``std::cmp_*`` alternative, if available.
176176

177-
- New :doc:`modernize-use-cpp-style-comments
178-
<clang-tidy/checks/modernize/use-cpp-style-comments>` check.
179-
180-
Detects C Style comments and suggests to use C++ style comments instead.
181-
182177
- New :doc:`portability-template-virtual-member-function
183178
<clang-tidy/checks/portability/template-virtual-member-function>` check.
184179

185180
Finds cases when an uninstantiated virtual member function in a template class
186181
causes cross-compiler incompatibility.
187182

183+
- New :doc:`readability-use-cpp-style-comments
184+
<clang-tidy/checks/readability/use-cpp-style-comments>` check.
185+
186+
Replace C-style comments with C++-style comments.
187+
188188
New check aliases
189189
^^^^^^^^^^^^^^^^^
190190

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ Clang-Tidy Checks
403403
:doc:`readability-uniqueptr-delete-release <readability/uniqueptr-delete-release>`, "Yes"
404404
:doc:`readability-uppercase-literal-suffix <readability/uppercase-literal-suffix>`, "Yes"
405405
:doc:`readability-use-anyofallof <readability/use-anyofallof>`,
406+
:doc:`readability-use-cpp-style-comments <readability/use-cpp-style-comments>`, "Yes"
406407
:doc:`readability-use-std-min-max <readability/use-std-min-max>`, "Yes"
407408
:doc:`zircon-temporary-objects <zircon/temporary-objects>`,
408409

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. title:: clang-tidy - use-cpp-style-comments
22

3-
modernize-use-cpp-style-comments
4-
================================
3+
readability-use-cpp-style-comments
4+
==================================
55

66
Replace C-style comments with C++-style comments.
77
C-style comments (``/* ... */``) are inherited from C, while C++ introduces
@@ -37,4 +37,3 @@ Output:
3737
Example:
3838
.. code-block:: c++
3939
int a = /* this is a comment */ 5;
40-

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// RUN: %check_clang_tidy -std=c++11 %s modernize-use-cpp-style-comments %t -- --
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 */
5-
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
5+
// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: use C++ style comments '//' instead of C style comments '/*...*/' [readability-use-cpp-style-comments]
66
// CHECK-FIXES: static const int CONSTANT = 42; // Important constant value
77

88
// Inline comment that should NOT be transformed
@@ -12,7 +12,7 @@ int a = /* inline comment */ 5;
1212
/* This is a multiline comment
1313
that spans several lines
1414
and should be converted to C++ style */
15-
// CHECK-MESSAGES: :[[@LINE-3]]:1: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
15+
// CHECK-MESSAGES: :[[@LINE-3]]:1: warning: use C++ style comments '//' instead of C style comments '/*...*/' [readability-use-cpp-style-comments]
1616
// CHECK-FIXES: // This is a multiline comment
1717
// CHECK-FIXES: // that spans several lines
1818
// CHECK-FIXES: // and should be converted to C++ style
@@ -29,40 +29,40 @@ void processData(int data /* input data */,
2929
* Block comment with asterisks
3030
* Should be converted to C++ style
3131
*******************************/
32-
// CHECK-MESSAGES: :[[@LINE-4]]:1: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
32+
// CHECK-MESSAGES: :[[@LINE-4]]:1: warning: use C++ style comments '//' instead of C style comments '/*...*/' [readability-use-cpp-style-comments]
3333
// CHECK-FIXES: // ******************************
3434
// CHECK-FIXES: // * Block comment with asterisks
3535
// CHECK-FIXES: // * Should be converted to C++ style
3636
// CHECK-FIXES: // ******************************
3737
int calculateSomething() { return 1;}
3838
// Comment at end of complex line
3939
int complexCalculation = calculateSomething(); /* Result of complex calculation */
40-
// CHECK-MESSAGES: :[[@LINE-1]]:48: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
40+
// CHECK-MESSAGES: :[[@LINE-1]]:48: warning: use C++ style comments '//' instead of C style comments '/*...*/' [readability-use-cpp-style-comments]
4141
// CHECK-FIXES: int complexCalculation = calculateSomething(); // Result of complex calculation
4242

4343
// Nested comments and edge cases
4444
void edgeCaseFunction() {
4545
int x = 10 /* First value */ + 20 /* Second value */; // Inline comments should not transform
4646

4747
/* Comment with special characters !@#$%^&*()_+ */
48-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
48+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use C++ style comments '//' instead of C style comments '/*...*/' [readability-use-cpp-style-comments]
4949
// CHECK-FIXES: // Comment with special characters !@#$%^&*()_+
5050
}
5151

5252
// Multiline comment with various indentations
5353
/* This comment is indented
5454
and should preserve indentation when converted */
55-
// CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
55+
// CHECK-MESSAGES: :[[@LINE-2]]:5: warning: use C++ style comments '//' instead of C style comments '/*...*/' [readability-use-cpp-style-comments]
5656
// CHECK-FIXES: // This comment is indented
5757
// CHECK-FIXES: // and should preserve indentation when converted
5858

5959
// Complex function with mixed comment types
6060
void complexFunction() {
6161
/* Full line comment at start of block */
62-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
62+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use C++ style comments '//' instead of C style comments '/*...*/' [readability-use-cpp-style-comments]
6363
// CHECK-FIXES: // Full line comment at start of block
6464

6565
int x = 10; /* Inline comment not to be transformed */
66-
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use C++ style comments '//' instead of C style comments '/*...*/' [modernize-use-cpp-style-comments]
66+
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use C++ style comments '//' instead of C style comments '/*...*/' [readability-use-cpp-style-comments]
6767
// CHECK-FIXES: int x = 10; // Inline comment not to be transformed
68-
}
68+
}

0 commit comments

Comments
 (0)