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,
193 Options.get(
"IncludeStyle",
"llvm"))) {}
215 Compiler.getLangOpts(), IncludeStyle));
216 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
221 SourceManager &
SM = *Result.SourceManager;
224 CharSourceRange
Range = Lexer::makeFileCharRange(
225 CharSourceRange::getTokenRange(E->getSourceRange()), SM, LangOptions());
227 if (Range.isInvalid())
230 auto Diag =
diag(Range.getBegin(),
"use std::move to transfer ownership")
231 << FixItHint::CreateInsertion(Range.getBegin(),
"std::move(")
232 << FixItHint::CreateInsertion(Range.getEnd(),
")");
235 Inserter->CreateIncludeInsertion(SM.getMainFileID(),
"utility",
237 if (Insertion.hasValue())
238 Diag << Insertion.getValue();
243 SourceLocation IdentifierLoc;
244 if (
const auto *TL = Result.Nodes.getNodeAs<TypeLoc>(
AutoPtrTokenId)) {
246 }
else if (
const auto *D =
250 llvm_unreachable(
"Bad Callback. No node provided.");
253 if (IdentifierLoc.isMacroID())
254 IdentifierLoc = SM.getSpellingLoc(IdentifierLoc);
261 SourceLocation EndLoc =
262 IdentifierLoc.getLocWithOffset(strlen(
"auto_ptr") - 1);
263 diag(IdentifierLoc,
"auto_ptr is deprecated, use unique_ptr instead")
264 << 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)
Base class for all clang-tidy checks.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers 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.
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.