Skip to content

Commit c815e43

Browse files
committed
further improvements as suggested
1 parent ec6a689 commit c815e43

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ add_clang_library(clangTidyReadabilityModule STATIC
5757
UniqueptrDeleteReleaseCheck.cpp
5858
UppercaseLiteralSuffixCheck.cpp
5959
UseAnyOfAllOfCheck.cpp
60+
UseCppStyleCommentsCheck.cpp
6061
UseStdMinMaxCheck.cpp
6162

6263
LINK_LIBS

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include "UniqueptrDeleteReleaseCheck.h"
6161
#include "UppercaseLiteralSuffixCheck.h"
6262
#include "UseAnyOfAllOfCheck.h"
63+
#include "UseCppStyleCommentsCheck.h"
6364
#include "UseStdMinMaxCheck.h"
6465

6566
namespace clang::tidy {
@@ -146,6 +147,8 @@ class ReadabilityModule : public ClangTidyModule {
146147
"readability-static-definition-in-anonymous-namespace");
147148
CheckFactories.registerCheck<StringCompareCheck>(
148149
"readability-string-compare");
150+
CheckFactories.registerCheck<UseCppStyleCommentsCheck>(
151+
"readability-use-cpp-style-comments");
149152
CheckFactories.registerCheck<readability::NamedParameterCheck>(
150153
"readability-named-parameter");
151154
CheckFactories.registerCheck<NonConstParameterCheck>(
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
#include <sstream.h>
15+
16+
using namespace clang::ast_matchers;
17+
18+
namespace clang::tidy::readability {
19+
class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler {
20+
public:
21+
CStyleCommentHandler(UseCppStyleCommentsCheck &Check)
22+
: Check(Check),
23+
CStyleCommentMatch(
24+
"^[ \t]*/\\*+[ \t\r\n]*(.*[ \t\r\n]*)*[ \t\r\n]*\\*+/[ \t\r\n]*$") {
25+
}
26+
27+
std::string convertToCppStyleComment(const SourceManager &SM,
28+
const SourceRange &Range) {
29+
const StringRef CommentText = Lexer::getSourceText(
30+
CharSourceRange::getTokenRange(Range), SM, LangOptions());
31+
32+
std::string InnerText = CommentText.str();
33+
InnerText.erase(0, 2);
34+
InnerText.erase(InnerText.size() - 2, 2);
35+
36+
std::string Result;
37+
std::istringstream Stream(InnerText);
38+
std::string Line;
39+
40+
if (std::getline(Stream, Line)) {
41+
const size_t StartPos = Line.find_first_not_of(" \t");
42+
if (StartPos != std::string::npos) {
43+
Line = Line.substr(StartPos);
44+
} else {
45+
Line.clear();
46+
}
47+
Result += "// " + Line;
48+
}
49+
50+
while (std::getline(Stream, Line)) {
51+
const size_t StartPos = Line.find_first_not_of(" \t");
52+
if (StartPos != std::string::npos) {
53+
Line = Line.substr(StartPos);
54+
} else {
55+
Line.clear();
56+
}
57+
Result += "\n// " + Line;
58+
}
59+
return Result;
60+
}
61+
62+
bool CheckForInlineComments(Preprocessor &PP, SourceRange Range) {
63+
const SourceManager &SM = PP.getSourceManager();
64+
const SourceLocation CommentStart = Range.getBegin();
65+
const SourceLocation CommentEnd = Range.getEnd();
66+
67+
unsigned StartLine = SM.getSpellingLineNumber(CommentStart);
68+
unsigned EndLine = SM.getSpellingLineNumber(CommentEnd);
69+
70+
if (StartLine == EndLine) {
71+
SourceLocation LineBegin =
72+
SM.translateLineCol(SM.getFileID(CommentStart), StartLine, 1);
73+
SourceLocation LineEnd =
74+
SM.translateLineCol(SM.getFileID(CommentEnd), EndLine,
75+
std::numeric_limits<unsigned>::max());
76+
StringRef LineContent = Lexer::getSourceText(
77+
CharSourceRange::getCharRange(LineBegin, LineEnd), SM,
78+
PP.getLangOpts());
79+
size_t CommentStartOffset = SM.getSpellingColumnNumber(CommentStart) - 1;
80+
StringRef AfterComment =
81+
LineContent.drop_front(CommentStartOffset + Text.size());
82+
83+
if (!AfterComment.trim().empty()) {
84+
return true;
85+
}
86+
}
87+
return false;
88+
}
89+
90+
bool HandleComment(Preprocessor &PP, SourceRange Range) override {
91+
const SourceManager &SM = PP.getSourceManager();
92+
93+
if (Range.getBegin().isMacroID() || SM.isInSystemHeader(Range.getBegin())) {
94+
return false;
95+
}
96+
97+
const StringRef Text = Lexer::getSourceText(
98+
CharSourceRange::getCharRange(Range), SM, PP.getLangOpts());
99+
100+
SmallVector<StringRef> Matches;
101+
if (!CStyleCommentMatch.match(Text, &Matches)) {
102+
return false;
103+
}
104+
105+
if (CheckForInlineComments(PP, Range)) {
106+
return false;
107+
}
108+
109+
Check.diag(
110+
Range.getBegin(),
111+
"use C++ style comments '//' instead of C style comments '/*...*/'")
112+
<< clang::FixItHint::CreateReplacement(
113+
Range, convertToCppStyleComment(SM, Range));
114+
115+
return false;
116+
}
117+
118+
private:
119+
UseCppStyleCommentsCheck &Check;
120+
llvm::Regex CStyleCommentMatch;
121+
};
122+
123+
UseCppStyleCommentsCheck::UseCppStyleCommentsCheck(StringRef Name,
124+
ClangTidyContext *Context)
125+
: ClangTidyCheck(Name, Context),
126+
Handler(std::make_unique<CStyleCommentHandler>(*this)) {}
127+
128+
void UseCppStyleCommentsCheck::registerPPCallbacks(
129+
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
130+
PP->addCommentHandler(Handler.get());
131+
}
132+
133+
void UseCppStyleCommentsCheck::check(const MatchFinder::MatchResult &Result) {}
134+
135+
UseCppStyleCommentsCheck::~UseCppStyleCommentsCheck() = default;
136+
} // namespace clang::tidy::readability
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- UseCppStyleCommentsCheck.h - clang-tidy-----------------*- C++ -*-===//
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_READABILITY_USECPPSTYLECOMMENTSCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_USECPPSTYLECOMMENTSCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::readability {
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/readability/use-cpp-style-comments.html
19+
class UseCppStyleCommentsCheck : public ClangTidyCheck {
20+
public:
21+
UseCppStyleCommentsCheck(StringRef Name, ClangTidyContext *Context);
22+
23+
~UseCppStyleCommentsCheck() override;
24+
25+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
26+
return LangOpts.CPlusPlus;
27+
}
28+
29+
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
30+
Preprocessor *ModuleExpanderPP) override;
31+
32+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
33+
34+
private:
35+
class CStyleCommentHandler;
36+
std::unique_ptr<CStyleCommentHandler> Handler;
37+
};
38+
} // namespace clang::tidy::readability
39+
40+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_USECPPSTYLECOMMENTSCHECK_H

0 commit comments

Comments
 (0)