|
| 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 |
0 commit comments