|
| 1 | +// SPDX-FileCopyrightText: 2025 Mike Crowe |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +#include "ModernizeNlohmannJsonExplicitConversionsCheck.h" |
| 5 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 6 | +#include "clang/Lex/Lexer.h" |
| 7 | + |
| 8 | +using namespace clang::ast_matchers; |
| 9 | + |
| 10 | +namespace { |
| 11 | + void replaceAll(std::string &Str, const std::string &From, const std::string &To) { |
| 12 | + assert(!From.empty()); |
| 13 | + |
| 14 | + std::string::size_type StartPos = 0; |
| 15 | + while ((StartPos = Str.find(From, StartPos)) != std::string::npos) { |
| 16 | + Str.replace(StartPos, From.length(), To); |
| 17 | + StartPos += To.length(); |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +namespace clang::tidy::modernize { |
| 23 | + |
| 24 | +void NlohmannJsonExplicitConversionsCheck::registerMatchers( |
| 25 | + MatchFinder *Finder) { |
| 26 | + auto Matcher = |
| 27 | + cxxMemberCallExpr( |
| 28 | + on(expr().bind("arg")), |
| 29 | + callee(cxxConversionDecl(ofClass(hasName("nlohmann::basic_json"))) |
| 30 | + .bind("conversionDecl"))) |
| 31 | + .bind("conversionCall"); |
| 32 | + Finder->addMatcher(Matcher, this); |
| 33 | +} |
| 34 | + |
| 35 | +void NlohmannJsonExplicitConversionsCheck::check( |
| 36 | + const MatchFinder::MatchResult &Result) { |
| 37 | + const auto *Decl = |
| 38 | + Result.Nodes.getNodeAs<CXXConversionDecl>("conversionDecl"); |
| 39 | + const auto *Call = |
| 40 | + Result.Nodes.getNodeAs<CXXMemberCallExpr>("conversionCall"); |
| 41 | + const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg"); |
| 42 | + |
| 43 | + const QualType DestinationType = Decl->getConversionType(); |
| 44 | + std::string DestinationTypeStr = |
| 45 | + DestinationType.getAsString(Result.Context->getPrintingPolicy()); |
| 46 | + replaceAll(DestinationTypeStr, "std::basic_string<char>", "std::string"); |
| 47 | + |
| 48 | + const SourceRange ExprRange = Call->getSourceRange(); |
| 49 | + if (!ExprRange.isValid()) |
| 50 | + return; |
| 51 | + |
| 52 | + bool Deref = false; |
| 53 | + if (const auto *Op = llvm::dyn_cast<UnaryOperator>(Arg); |
| 54 | + Op && Op->getOpcode() == UO_Deref) |
| 55 | + Deref = true; |
| 56 | + else if (const auto *Op = llvm::dyn_cast<CXXOperatorCallExpr>(Arg); |
| 57 | + Op && Op->getOperator() == OO_Star) |
| 58 | + Deref = true; |
| 59 | + |
| 60 | + llvm::StringRef SourceText = clang::Lexer::getSourceText( |
| 61 | + clang::CharSourceRange::getTokenRange(ExprRange), *Result.SourceManager, |
| 62 | + Result.Context->getLangOpts()); |
| 63 | + |
| 64 | + if (Deref) |
| 65 | + SourceText.consume_front("*"); |
| 66 | + |
| 67 | + const std::string ReplacementText = |
| 68 | + (llvm::Twine(SourceText) + (Deref ? "->" : ".") + "get<" + |
| 69 | + DestinationTypeStr + ">()") |
| 70 | + .str(); |
| 71 | + diag(Call->getExprLoc(), |
| 72 | + "implicit nlohmann::json conversion to %0 should be explicit") |
| 73 | + << DestinationTypeStr |
| 74 | + << FixItHint::CreateReplacement(CharSourceRange::getTokenRange(ExprRange), |
| 75 | + ReplacementText); |
| 76 | +} |
| 77 | + |
| 78 | +} // namespace clang::tidy::modernize |
0 commit comments