clang-tools  14.0.0git
ReplaceAutoPtrCheck.cpp
Go to the documentation of this file.
1 //===--- ReplaceAutoPtrCheck.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 "ReplaceAutoPtrCheck.h"
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"
15 
16 using namespace clang;
17 using namespace clang::ast_matchers;
18 
19 namespace clang {
20 namespace tidy {
21 namespace modernize {
22 
23 namespace {
24 static const char AutoPtrTokenId[] = "AutoPrTokenId";
25 static const char AutoPtrOwnershipTransferId[] = "AutoPtrOwnershipTransferId";
26 
27 /// Matches expressions that are lvalues.
28 ///
29 /// In the following example, a[0] matches expr(isLValue()):
30 /// \code
31 /// std::string a[2];
32 /// std::string b;
33 /// b = a[0];
34 /// b = "this string won't match";
35 /// \endcode
36 AST_MATCHER(Expr, isLValue) { return Node.getValueKind() == VK_LValue; }
37 
38 } // namespace
39 
40 ReplaceAutoPtrCheck::ReplaceAutoPtrCheck(StringRef Name,
41  ClangTidyContext *Context)
42  : ClangTidyCheck(Name, Context),
43  Inserter(Options.getLocalOrGlobal("IncludeStyle",
44  utils::IncludeSorter::IS_LLVM)) {}
45 
47  Options.store(Opts, "IncludeStyle", Inserter.getStyle());
48 }
49 
50 void ReplaceAutoPtrCheck::registerMatchers(MatchFinder *Finder) {
51  auto AutoPtrDecl = recordDecl(hasName("auto_ptr"), isInStdNamespace());
52  auto AutoPtrType = qualType(hasDeclaration(AutoPtrDecl));
53 
54  // std::auto_ptr<int> a;
55  // ^~~~~~~~~~~~~
56  //
57  // typedef std::auto_ptr<int> int_ptr_t;
58  // ^~~~~~~~~~~~~
59  //
60  // std::auto_ptr<int> fn(std::auto_ptr<int>);
61  // ^~~~~~~~~~~~~ ^~~~~~~~~~~~~
62  Finder->addMatcher(typeLoc(loc(qualType(AutoPtrType,
63  // Skip elaboratedType() as the named
64  // type will match soon thereafter.
65  unless(elaboratedType()))))
66  .bind(AutoPtrTokenId),
67  this);
68 
69  // using std::auto_ptr;
70  // ^~~~~~~~~~~~~~~~~~~
71  Finder->addMatcher(usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(namedDecl(
72  hasName("auto_ptr"), isInStdNamespace()))))
73  .bind(AutoPtrTokenId),
74  this);
75 
76  // Find ownership transfers via copy construction and assignment.
77  // AutoPtrOwnershipTransferId is bound to the part that has to be wrapped
78  // into std::move().
79  // std::auto_ptr<int> i, j;
80  // i = j;
81  // ~~~~^
82  auto MovableArgumentMatcher =
83  expr(isLValue(), hasType(AutoPtrType)).bind(AutoPtrOwnershipTransferId);
84 
85  Finder->addMatcher(
86  cxxOperatorCallExpr(hasOverloadedOperatorName("="),
87  callee(cxxMethodDecl(ofClass(AutoPtrDecl))),
88  hasArgument(1, MovableArgumentMatcher)),
89  this);
90  Finder->addMatcher(
91  traverse(TK_AsIs,
92  cxxConstructExpr(hasType(AutoPtrType), argumentCountIs(1),
93  hasArgument(0, MovableArgumentMatcher))),
94  this);
95 }
96 
97 void ReplaceAutoPtrCheck::registerPPCallbacks(const SourceManager &SM,
98  Preprocessor *PP,
99  Preprocessor *ModuleExpanderPP) {
100  Inserter.registerPreprocessor(PP);
101 }
102 
103 void ReplaceAutoPtrCheck::check(const MatchFinder::MatchResult &Result) {
104  SourceManager &SM = *Result.SourceManager;
105  if (const auto *E =
106  Result.Nodes.getNodeAs<Expr>(AutoPtrOwnershipTransferId)) {
107  CharSourceRange Range = Lexer::makeFileCharRange(
108  CharSourceRange::getTokenRange(E->getSourceRange()), SM, LangOptions());
109 
110  if (Range.isInvalid())
111  return;
112 
113  auto Diag = diag(Range.getBegin(), "use std::move to transfer ownership")
114  << FixItHint::CreateInsertion(Range.getBegin(), "std::move(")
115  << FixItHint::CreateInsertion(Range.getEnd(), ")")
116  << Inserter.createMainFileIncludeInsertion("<utility>");
117 
118  return;
119  }
120 
121  SourceLocation AutoPtrLoc;
122  if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>(AutoPtrTokenId)) {
123  // std::auto_ptr<int> i;
124  // ^
125  if (auto Loc = TL->getAs<TemplateSpecializationTypeLoc>())
126  AutoPtrLoc = Loc.getTemplateNameLoc();
127  } else if (const auto *D =
128  Result.Nodes.getNodeAs<UsingDecl>(AutoPtrTokenId)) {
129  // using std::auto_ptr;
130  // ^
131  AutoPtrLoc = D->getNameInfo().getBeginLoc();
132  } else {
133  llvm_unreachable("Bad Callback. No node provided.");
134  }
135 
136  if (AutoPtrLoc.isMacroID())
137  AutoPtrLoc = SM.getSpellingLoc(AutoPtrLoc);
138 
139  // Ensure that only the 'auto_ptr' token is replaced and not the template
140  // aliases.
141  if (StringRef(SM.getCharacterData(AutoPtrLoc), strlen("auto_ptr")) !=
142  "auto_ptr")
143  return;
144 
145  SourceLocation EndLoc =
146  AutoPtrLoc.getLocWithOffset(strlen("auto_ptr") - 1);
147  diag(AutoPtrLoc, "auto_ptr is deprecated, use unique_ptr instead")
148  << FixItHint::CreateReplacement(SourceRange(AutoPtrLoc, EndLoc),
149  "unique_ptr");
150 }
151 
152 } // namespace modernize
153 } // namespace tidy
154 } // namespace clang
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::tidy::utils::IncludeInserter::registerPreprocessor
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
Definition: IncludeInserter.cpp:42
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::modernize::ReplaceAutoPtrCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: ReplaceAutoPtrCheck.cpp:97
clang::ast_matchers
Definition: AbseilMatcher.h:14
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:71
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::utils::IncludeInserter::createMainFileIncludeInsertion
llvm::Optional< FixItHint > createMainFileIncludeInsertion(llvm::StringRef Header)
Creates a Header inclusion directive fixit in the main file.
Definition: IncludeInserter.cpp:83
clang::tidy::modernize::ReplaceAutoPtrCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: ReplaceAutoPtrCheck.cpp:103
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
clang::tidy::modernize::ReplaceAutoPtrCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: ReplaceAutoPtrCheck.cpp:50
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
ReplaceAutoPtrCheck.h
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::tidy::utils::IncludeInserter::getStyle
IncludeSorter::IncludeStyle getStyle() const
Definition: IncludeInserter.h:84
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
SM
const SourceManager & SM
Definition: IncludeCleaner.cpp:140
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:120
clang::tidy::modernize::ReplaceAutoPtrCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: ReplaceAutoPtrCheck.cpp:46