Skip to content

Commit 581cc0e

Browse files
chaitanyav4m4n-x-B4w4ne
authored andcommitted
This commit is to resolve the issue llvm#24841
1 parent 61b294a commit 581cc0e

File tree

9 files changed

+337
-490
lines changed

9 files changed

+337
-490
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 STATIC
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"
@@ -107,6 +108,8 @@ class ModernizeModule : public ClangTidyModule {
107108
"modernize-use-bool-literals");
108109
CheckFactories.registerCheck<UseConstraintsCheck>(
109110
"modernize-use-constraints");
111+
CheckFactories.registerCheck<UseCppStyleCommentsCheck>(
112+
"modernize-use-cpp-style-comments");
110113
CheckFactories.registerCheck<UseDefaultMemberInitCheck>(
111114
"modernize-use-default-member-init");
112115
CheckFactories.registerCheck<UseEmplaceCheck>("modernize-use-emplace");
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 <iostream>
15+
using namespace clang::ast_matchers;
16+
17+
namespace clang::tidy::modernize {
18+
class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler {
19+
public:
20+
CStyleCommentHandler(UseCppStyleCommentsCheck &Check)
21+
: Check(Check),
22+
CStyleCommentMatch(
23+
"^[ \t]*/\\*+[ \t\r\n]*(.*[ \t\r\n]*)*[ \t\r\n]*\\*+/[ \t\r\n]*$"){}
24+
25+
std::string convertToCppStyleComment(const SourceManager &SM, const SourceRange &Range) {
26+
StringRef CommentText = Lexer::getSourceText(
27+
CharSourceRange::getTokenRange(Range), SM, LangOptions());
28+
29+
std::string InnerText = CommentText.str();
30+
InnerText.erase(0, 2);
31+
InnerText.erase(InnerText.size() - 2, 2);
32+
33+
std::string Result;
34+
std::istringstream Stream(InnerText);
35+
std::string Line;
36+
37+
if (std::getline(Stream, Line)) {
38+
size_t startPos = Line.find_first_not_of(" \t");
39+
if (startPos != std::string::npos) {
40+
Line = Line.substr(startPos);
41+
} else {
42+
Line.clear();
43+
}
44+
Result += "// " + Line;
45+
}
46+
47+
while (std::getline(Stream, Line)) {
48+
size_t startPos = Line.find_first_not_of(" \t");
49+
if (startPos != std::string::npos) {
50+
Line = Line.substr(startPos);
51+
} else {
52+
Line.clear();
53+
}
54+
Result += "\n// " + Line;
55+
}
56+
return Result;
57+
}
58+
59+
bool HandleComment(Preprocessor &PP, SourceRange Range) override {
60+
const SourceManager &SM = PP.getSourceManager();
61+
62+
if (Range.getBegin().isMacroID() ||
63+
SM.isInSystemHeader(Range.getBegin())) {
64+
return false;
65+
}
66+
67+
const StringRef Text =
68+
Lexer::getSourceText(CharSourceRange::getCharRange(Range),
69+
SM, PP.getLangOpts());
70+
71+
SmallVector<StringRef> Matches;
72+
if (!CStyleCommentMatch.match(Text, &Matches)) {
73+
return false;
74+
}
75+
76+
SourceLocation CommentStart = Range.getBegin();
77+
SourceLocation CommentEnd = Range.getEnd();
78+
79+
unsigned StartLine = SM.getSpellingLineNumber(CommentStart);
80+
unsigned EndLine = SM.getSpellingLineNumber(CommentEnd);
81+
82+
83+
if (StartLine == EndLine) {
84+
SourceLocation LineBegin = SM.translateLineCol(
85+
SM.getFileID(CommentStart), StartLine, 1);
86+
SourceLocation LineEnd = SM.translateLineCol(SM.getFileID(CommentEnd), EndLine, std::numeric_limits<unsigned>::max());
87+
StringRef LineContent =
88+
Lexer::getSourceText(CharSourceRange::getCharRange(LineBegin, LineEnd),
89+
SM, PP.getLangOpts());
90+
size_t CommentStartOffset = SM.getSpellingColumnNumber(CommentStart) - 1;
91+
StringRef AfterComment = LineContent.drop_front(CommentStartOffset + Text.size());
92+
93+
if (!AfterComment.trim().empty()) {
94+
return false;
95+
}
96+
}
97+
98+
Check.diag(
99+
Range.getBegin(),
100+
"use C++ style comments '//' instead of C style comments '/*...*/'")
101+
<< clang::FixItHint::CreateReplacement(
102+
Range, convertToCppStyleComment(SM, Range));
103+
104+
return false;
105+
}
106+
107+
108+
private:
109+
UseCppStyleCommentsCheck &Check;
110+
llvm::Regex CStyleCommentMatch;
111+
};
112+
113+
UseCppStyleCommentsCheck::UseCppStyleCommentsCheck(StringRef Name,
114+
ClangTidyContext *Context)
115+
: ClangTidyCheck(Name, Context),
116+
Handler(std::make_unique<CStyleCommentHandler>(*this)) {}
117+
118+
void UseCppStyleCommentsCheck::registerPPCallbacks(
119+
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
120+
PP->addCommentHandler(Handler.get());
121+
}
122+
123+
void UseCppStyleCommentsCheck::check(const MatchFinder::MatchResult &Result) {}
124+
125+
UseCppStyleCommentsCheck::~UseCppStyleCommentsCheck() = default;
126+
} // namespace clang::tidy::modernize
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#include <sstream>
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+
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::modernize
39+
40+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_CPP_STYLE_COMMENTS_CHECK_H

0 commit comments

Comments
 (0)