10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Lex/Lexer.h"
14#include "clang/Lex/Preprocessor.h"
22static const char AutoPtrTokenId[] =
"AutoPrTokenId";
23static const char AutoPtrOwnershipTransferId[] =
"AutoPtrOwnershipTransferId";
41 Inserter(Options.getLocalOrGlobal(
"IncludeStyle",
42 utils::IncludeSorter::IS_LLVM),
43 areDiagsSelfContained()) {}
50 auto AutoPtrDecl = recordDecl(hasName(
"auto_ptr"), isInStdNamespace());
51 auto AutoPtrType = qualType(hasDeclaration(AutoPtrDecl));
61 Finder->addMatcher(typeLoc(loc(qualType(AutoPtrType,
64 unless(elaboratedType()))))
65 .bind(AutoPtrTokenId),
70 Finder->addMatcher(usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(namedDecl(
71 hasName(
"auto_ptr"), isInStdNamespace()))))
72 .bind(AutoPtrTokenId),
81 auto MovableArgumentMatcher =
82 expr(isLValue(), hasType(AutoPtrType)).bind(AutoPtrOwnershipTransferId);
85 cxxOperatorCallExpr(hasOverloadedOperatorName(
"="),
86 callee(cxxMethodDecl(ofClass(AutoPtrDecl))),
87 hasArgument(1, MovableArgumentMatcher)),
91 cxxConstructExpr(hasType(AutoPtrType), argumentCountIs(1),
92 hasArgument(0, MovableArgumentMatcher))),
98 Preprocessor *ModuleExpanderPP) {
103 SourceManager &SM = *Result.SourceManager;
105 Result.Nodes.getNodeAs<Expr>(AutoPtrOwnershipTransferId)) {
106 CharSourceRange
Range = Lexer::makeFileCharRange(
107 CharSourceRange::getTokenRange(
E->getSourceRange()), SM, LangOptions());
109 if (
Range.isInvalid())
112 auto Diag =
diag(
Range.getBegin(),
"use std::move to transfer ownership")
113 << FixItHint::CreateInsertion(
Range.getBegin(),
"std::move(")
114 << FixItHint::CreateInsertion(
Range.getEnd(),
")")
120 SourceLocation AutoPtrLoc;
121 if (
const auto *TL = Result.Nodes.getNodeAs<TypeLoc>(AutoPtrTokenId)) {
124 if (
auto Loc = TL->getAs<TemplateSpecializationTypeLoc>())
125 AutoPtrLoc =
Loc.getTemplateNameLoc();
126 }
else if (
const auto *D =
127 Result.Nodes.getNodeAs<UsingDecl>(AutoPtrTokenId)) {
130 AutoPtrLoc = D->getNameInfo().getBeginLoc();
132 llvm_unreachable(
"Bad Callback. No node provided.");
135 if (AutoPtrLoc.isMacroID())
136 AutoPtrLoc = SM.getSpellingLoc(AutoPtrLoc);
140 if (StringRef(SM.getCharacterData(AutoPtrLoc), strlen(
"auto_ptr")) !=
144 SourceLocation EndLoc =
145 AutoPtrLoc.getLocWithOffset(strlen(
"auto_ptr") - 1);
146 diag(AutoPtrLoc,
"auto_ptr is deprecated, use unique_ptr instead")
147 << FixItHint::CreateReplacement(SourceRange(AutoPtrLoc, EndLoc),
llvm::SmallString< 256U > Name
CharSourceRange Range
SourceRange for the file name.
::clang::DynTypedNode Node
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
ReplaceAutoPtrCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
IncludeSorter::IncludeStyle getStyle() const
std::optional< FixItHint > createMainFileIncludeInsertion(llvm::StringRef Header)
Creates a Header inclusion directive fixit in the main file.
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringMap< ClangTidyValue > OptionMap