|
| 1 | +//===--- UseSpanCheck.cpp - 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 | +#include "UseSpanCheck.h" |
| 10 | +#include "clang/AST/ASTContext.h" |
| 11 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | +#include "clang/Lex/Lexer.h" |
| 13 | +#include "../utils/IncludeInserter.h" |
| 14 | +#include <string> |
| 15 | + |
| 16 | +using namespace clang::ast_matchers; |
| 17 | + |
| 18 | +namespace clang::tidy::modernize { |
| 19 | + |
| 20 | +namespace { |
| 21 | +AST_MATCHER(QualType, isRefToVectorOrArray) { |
| 22 | + if (!Node->isReferenceType()) |
| 23 | + return false; |
| 24 | + |
| 25 | + QualType PointeeType = Node->getPointeeType(); |
| 26 | + |
| 27 | + const Type *UnqualifiedType = PointeeType.getTypePtr()->getUnqualifiedDesugaredType(); |
| 28 | + if (!UnqualifiedType || !UnqualifiedType->isRecordType()) |
| 29 | + return false; |
| 30 | + |
| 31 | + const CXXRecordDecl *Record = UnqualifiedType->getAsCXXRecordDecl(); |
| 32 | + if (!Record) |
| 33 | + return false; |
| 34 | + |
| 35 | + const std::string Name = Record->getQualifiedNameAsString(); |
| 36 | + return Name == "std::vector" || Name == "std::array"; |
| 37 | +} |
| 38 | +} // namespace |
| 39 | + |
| 40 | +UseSpanCheck::UseSpanCheck(StringRef Name, ClangTidyContext *Context) |
| 41 | + : ClangTidyCheck(Name, Context), |
| 42 | + Inserter(utils::IncludeSorter::IS_LLVM, areDiagsSelfContained()) {} |
| 43 | + |
| 44 | +void UseSpanCheck::registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, |
| 45 | + Preprocessor *ModuleExpanderPP) { |
| 46 | + Inserter.registerPreprocessor(PP); |
| 47 | +} |
| 48 | + |
| 49 | +void UseSpanCheck::registerMatchers(MatchFinder *Finder) { |
| 50 | + Finder->addMatcher( |
| 51 | + functionDecl( |
| 52 | + forEachDescendant( |
| 53 | + parmVarDecl(hasType(qualType(isRefToVectorOrArray()))) |
| 54 | + .bind("param"))), |
| 55 | + this); |
| 56 | +} |
| 57 | + |
| 58 | +void UseSpanCheck::check(const MatchFinder::MatchResult &Result) { |
| 59 | + const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param"); |
| 60 | + if (!Param) |
| 61 | + return; |
| 62 | + |
| 63 | + QualType ParamType = Param->getType(); |
| 64 | + if (!ParamType->isReferenceType()) |
| 65 | + return; |
| 66 | + |
| 67 | + // Get the pointee type (the vector/array type) |
| 68 | + QualType PointeeType = ParamType->getPointeeType(); |
| 69 | + bool IsConst = PointeeType.isConstQualified(); |
| 70 | + |
| 71 | + const Type *UnqualifiedType = PointeeType.getTypePtr()->getUnqualifiedDesugaredType(); |
| 72 | + if (!UnqualifiedType || !UnqualifiedType->isRecordType()) |
| 73 | + return; |
| 74 | + |
| 75 | + const CXXRecordDecl *Record = UnqualifiedType->getAsCXXRecordDecl(); |
| 76 | + if (!Record) |
| 77 | + return; |
| 78 | + |
| 79 | + const std::string RecordName = Record->getQualifiedNameAsString(); |
| 80 | + if (RecordName != "std::vector" && RecordName != "std::array") |
| 81 | + return; |
| 82 | + |
| 83 | + // Check if it's a template specialization |
| 84 | + if (!isa<ClassTemplateSpecializationDecl>(Record)) |
| 85 | + return; |
| 86 | + |
| 87 | + // Get the template arguments |
| 88 | + const auto *TemplateSpecRecord = cast<ClassTemplateSpecializationDecl>(Record); |
| 89 | + const TemplateArgumentList &Args = TemplateSpecRecord->getTemplateArgs(); |
| 90 | + if (Args.size() < 1) |
| 91 | + return; |
| 92 | + |
| 93 | + // Get the element type from the first template argument |
| 94 | + const TemplateArgument &Arg = Args[0]; |
| 95 | + if (Arg.getKind() != TemplateArgument::Type) |
| 96 | + return; |
| 97 | + |
| 98 | + QualType ElementType = Arg.getAsType(); |
| 99 | + |
| 100 | + // Get the source range for the parameter type |
| 101 | + TypeSourceInfo *TSI = Param->getTypeSourceInfo(); |
| 102 | + TypeLoc TL = TSI->getTypeLoc(); |
| 103 | + |
| 104 | + // Get the source range for the entire type, including qualifiers |
| 105 | + SourceRange TypeRange = TL.getSourceRange(); |
| 106 | + |
| 107 | + // Create the diagnostic |
| 108 | + auto Diag = diag(Param->getBeginLoc(), |
| 109 | + "parameter %0 is reference to %1; consider using std::span instead") |
| 110 | + << Param |
| 111 | + << (RecordName == "std::vector" ? "std::vector" : "std::array"); |
| 112 | + |
| 113 | + // Create the fix-it hint |
| 114 | + std::string SpanType; |
| 115 | + std::string ElementTypeWithConst = IsConst ? "const " + ElementType.getAsString() : ElementType.getAsString(); |
| 116 | + |
| 117 | + // For std::array, we should preserve the size in the span |
| 118 | + if (RecordName == "std::array" && Args.size() >= 2) { |
| 119 | + const TemplateArgument &SizeArg = Args[1]; |
| 120 | + if (SizeArg.getKind() == TemplateArgument::Integral) { |
| 121 | + llvm::APSInt Size = SizeArg.getAsIntegral(); |
| 122 | + // Convert APSInt to string |
| 123 | + SmallString<16> SizeStr; |
| 124 | + Size.toString(SizeStr, 10); |
| 125 | + SpanType = "std::span<" + ElementTypeWithConst + ", " + SizeStr.str().str() + ">"; |
| 126 | + } else { |
| 127 | + SpanType = "std::span<" + ElementTypeWithConst + ">"; |
| 128 | + } |
| 129 | + } else { |
| 130 | + SpanType = "std::span<" + ElementTypeWithConst + ">"; |
| 131 | + } |
| 132 | + |
| 133 | + // Create the replacement for the entire type including qualifiers |
| 134 | + Diag << FixItHint::CreateReplacement(TypeRange, SpanType); |
| 135 | + |
| 136 | + // Add the include for <span> |
| 137 | + Diag << Inserter.createIncludeInsertion( |
| 138 | + Result.SourceManager->getFileID(Param->getBeginLoc()), |
| 139 | + "<span>"); |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace clang::tidy::modernize |
0 commit comments