11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Frontend/CompilerInstance.h"
14 #include "clang/Lex/Lexer.h"
15 #include "clang/Lex/Preprocessor.h"
17 using namespace clang;
18 using namespace clang::ast_matchers;
36 AST_MATCHER(Expr, isLValue) {
return Node.getValueKind() == VK_LValue; }
59 const DeclContext *D = Node.getDeclContext();
61 while (D->isInlineNamespace())
64 if (!D->isNamespace() || !D->getParent()->isTranslationUnit())
67 const IdentifierInfo *Info = cast<NamespaceDecl>(D)->getIdentifier();
69 return (Info && Info->isStr(
"std"));
73 static DeclarationMatcher AutoPtrDecl =
74 recordDecl(hasName(
"auto_ptr"), isFromStdNamespace());
77 static TypeMatcher AutoPtrType = qualType(hasDeclaration(AutoPtrDecl));
83 static StatementMatcher MovableArgumentMatcher =
84 expr(allOf(isLValue(), hasType(AutoPtrType)))
104 return typeLoc(loc(qualType(AutoPtrType, unless(elaboratedType()))))
116 return usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(
117 allOf(hasName(
"auto_ptr"), isFromStdNamespace()))))
134 cxxOperatorCallExpr(allOf(hasOverloadedOperatorName(
"="),
135 callee(cxxMethodDecl(ofClass(AutoPtrDecl))),
136 hasArgument(1, MovableArgumentMatcher))),
137 cxxConstructExpr(allOf(hasType(AutoPtrType), argumentCountIs(1),
138 hasArgument(0, MovableArgumentMatcher))));
154 const SourceManager &
SM) {
155 auto TL = AutoPtrTypeLoc->getAs<TemplateSpecializationTypeLoc>();
157 return SourceLocation();
159 return TL.getTemplateNameLoc();
174 const SourceManager &
SM) {
175 return UsingAutoPtrDecl->getNameInfo().getBeginLoc();
180 const SourceManager &
SM,
181 const LangOptions &LO) {
182 SmallVector<char, 8> Buffer;
183 bool Invalid =
false;
184 StringRef Res = Lexer::getSpelling(TokenStart, Buffer, SM, LO, &Invalid);
186 return (!Invalid && Res ==
"auto_ptr");
189 ReplaceAutoPtrCheck::ReplaceAutoPtrCheck(StringRef
Name,
192 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
193 Options.get(
"IncludeStyle",
"llvm"))) {}
216 Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
217 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
222 SourceManager &
SM = *Result.SourceManager;
225 CharSourceRange
Range = Lexer::makeFileCharRange(
226 CharSourceRange::getTokenRange(E->getSourceRange()), SM, LangOptions());
228 if (Range.isInvalid())
231 auto Diag =
diag(Range.getBegin(),
"use std::move to transfer ownership")
232 << FixItHint::CreateInsertion(Range.getBegin(),
"std::move(")
233 << FixItHint::CreateInsertion(Range.getEnd(),
")");
236 Inserter->CreateIncludeInsertion(SM.getMainFileID(),
"utility",
238 if (Insertion.hasValue())
239 Diag << Insertion.getValue();
244 SourceLocation IdentifierLoc;
245 if (
const auto *TL = Result.Nodes.getNodeAs<TypeLoc>(
AutoPtrTokenId)) {
247 }
else if (
const auto *D =
251 llvm_unreachable(
"Bad Callback. No node provided.");
254 if (IdentifierLoc.isMacroID())
255 IdentifierLoc = SM.getSpellingLoc(IdentifierLoc);
262 SourceLocation EndLoc =
263 IdentifierLoc.getLocWithOffset(strlen(
"auto_ptr") - 1);
264 diag(IdentifierLoc,
"auto_ptr is deprecated, use unique_ptr instead")
265 << FixItHint::CreateReplacement(SourceRange(IdentifierLoc, EndLoc),
LangOptions getLangOpts() const
Returns the language options from the context.
static SourceLocation locateFromUsingDecl(const UsingDecl *UsingAutoPtrDecl, const SourceManager &SM)
Locates the auto_ptr token in using declarations.
std::unique_ptr< ast_matchers::MatchFinder > Finder
static const char AutoPtrOwnershipTransferId[]
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
Base class for all clang-tidy checks.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static const char AutoPtrTokenId[]
StatementMatcher makeTransferOwnershipExprMatcher()
Creates a matcher that finds the std::auto_ptr copy-ctor and assign-operator expressions.
TypeLocMatcher makeAutoPtrTypeLocMatcher()
Matches declarations whose declaration context is the C++ standard library namespace std...
static bool checkTokenIsAutoPtr(SourceLocation TokenStart, const SourceManager &SM, const LangOptions &LO)
Verifies that the token at TokenStart is 'auto_ptr'.
AST_MATCHER(Expr, isLValue)
Matches expressions that are lvalues.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
std::map< std::string, std::string > OptionMap
DeclarationMatcher makeAutoPtrUsingDeclMatcher()
Creates a matcher that finds the using declarations referring to std::auto_ptr.
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
CharSourceRange Range
SourceRange for the file name.
Produces fixes to insert specified includes to source files, if not yet present.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
static SourceLocation locateFromTypeLoc(const TypeLoc *AutoPtrTypeLoc, const SourceManager &SM)
Locates the auto_ptr token when it is referred by a TypeLoc.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.