ReplaceAutoPtrCheck.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "ReplaceAutoPtrCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. #include "clang/Frontend/CompilerInstance.h"
  12. #include "clang/Lex/Lexer.h"
  13. #include "clang/Lex/Preprocessor.h"
  14. using namespace clang;
  15. using namespace clang::ast_matchers;
  16. namespace clang::tidy::modernize {
  17. namespace {
  18. static const char AutoPtrTokenId[] = "AutoPrTokenId";
  19. static const char AutoPtrOwnershipTransferId[] = "AutoPtrOwnershipTransferId";
  20. /// Matches expressions that are lvalues.
  21. ///
  22. /// In the following example, a[0] matches expr(isLValue()):
  23. /// \code
  24. /// std::string a[2];
  25. /// std::string b;
  26. /// b = a[0];
  27. /// b = "this string won't match";
  28. /// \endcode
  29. AST_MATCHER(Expr, isLValue) { return Node.getValueKind() == VK_LValue; }
  30. } // namespace
  31. ReplaceAutoPtrCheck::ReplaceAutoPtrCheck(StringRef Name,
  32. ClangTidyContext *Context)
  33. : ClangTidyCheck(Name, Context),
  34. Inserter(Options.getLocalOrGlobal("IncludeStyle",
  35. utils::IncludeSorter::IS_LLVM),
  36. areDiagsSelfContained()) {}
  37. void ReplaceAutoPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
  38. Options.store(Opts, "IncludeStyle", Inserter.getStyle());
  39. }
  40. void ReplaceAutoPtrCheck::registerMatchers(MatchFinder *Finder) {
  41. auto AutoPtrDecl = recordDecl(hasName("auto_ptr"), isInStdNamespace());
  42. auto AutoPtrType = qualType(hasDeclaration(AutoPtrDecl));
  43. // std::auto_ptr<int> a;
  44. // ^~~~~~~~~~~~~
  45. //
  46. // typedef std::auto_ptr<int> int_ptr_t;
  47. // ^~~~~~~~~~~~~
  48. //
  49. // std::auto_ptr<int> fn(std::auto_ptr<int>);
  50. // ^~~~~~~~~~~~~ ^~~~~~~~~~~~~
  51. Finder->addMatcher(typeLoc(loc(qualType(AutoPtrType,
  52. // Skip elaboratedType() as the named
  53. // type will match soon thereafter.
  54. unless(elaboratedType()))))
  55. .bind(AutoPtrTokenId),
  56. this);
  57. // using std::auto_ptr;
  58. // ^~~~~~~~~~~~~~~~~~~
  59. Finder->addMatcher(usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(namedDecl(
  60. hasName("auto_ptr"), isInStdNamespace()))))
  61. .bind(AutoPtrTokenId),
  62. this);
  63. // Find ownership transfers via copy construction and assignment.
  64. // AutoPtrOwnershipTransferId is bound to the part that has to be wrapped
  65. // into std::move().
  66. // std::auto_ptr<int> i, j;
  67. // i = j;
  68. // ~~~~^
  69. auto MovableArgumentMatcher =
  70. expr(isLValue(), hasType(AutoPtrType)).bind(AutoPtrOwnershipTransferId);
  71. Finder->addMatcher(
  72. cxxOperatorCallExpr(hasOverloadedOperatorName("="),
  73. callee(cxxMethodDecl(ofClass(AutoPtrDecl))),
  74. hasArgument(1, MovableArgumentMatcher)),
  75. this);
  76. Finder->addMatcher(
  77. traverse(TK_AsIs,
  78. cxxConstructExpr(hasType(AutoPtrType), argumentCountIs(1),
  79. hasArgument(0, MovableArgumentMatcher))),
  80. this);
  81. }
  82. void ReplaceAutoPtrCheck::registerPPCallbacks(const SourceManager &SM,
  83. Preprocessor *PP,
  84. Preprocessor *ModuleExpanderPP) {
  85. Inserter.registerPreprocessor(PP);
  86. }
  87. void ReplaceAutoPtrCheck::check(const MatchFinder::MatchResult &Result) {
  88. SourceManager &SM = *Result.SourceManager;
  89. if (const auto *E =
  90. Result.Nodes.getNodeAs<Expr>(AutoPtrOwnershipTransferId)) {
  91. CharSourceRange Range = Lexer::makeFileCharRange(
  92. CharSourceRange::getTokenRange(E->getSourceRange()), SM, LangOptions());
  93. if (Range.isInvalid())
  94. return;
  95. auto Diag = diag(Range.getBegin(), "use std::move to transfer ownership")
  96. << FixItHint::CreateInsertion(Range.getBegin(), "std::move(")
  97. << FixItHint::CreateInsertion(Range.getEnd(), ")")
  98. << Inserter.createMainFileIncludeInsertion("<utility>");
  99. return;
  100. }
  101. SourceLocation AutoPtrLoc;
  102. if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>(AutoPtrTokenId)) {
  103. // std::auto_ptr<int> i;
  104. // ^
  105. if (auto Loc = TL->getAs<TemplateSpecializationTypeLoc>())
  106. AutoPtrLoc = Loc.getTemplateNameLoc();
  107. } else if (const auto *D =
  108. Result.Nodes.getNodeAs<UsingDecl>(AutoPtrTokenId)) {
  109. // using std::auto_ptr;
  110. // ^
  111. AutoPtrLoc = D->getNameInfo().getBeginLoc();
  112. } else {
  113. llvm_unreachable("Bad Callback. No node provided.");
  114. }
  115. if (AutoPtrLoc.isMacroID())
  116. AutoPtrLoc = SM.getSpellingLoc(AutoPtrLoc);
  117. // Ensure that only the 'auto_ptr' token is replaced and not the template
  118. // aliases.
  119. if (StringRef(SM.getCharacterData(AutoPtrLoc), strlen("auto_ptr")) !=
  120. "auto_ptr")
  121. return;
  122. SourceLocation EndLoc =
  123. AutoPtrLoc.getLocWithOffset(strlen("auto_ptr") - 1);
  124. diag(AutoPtrLoc, "auto_ptr is deprecated, use unique_ptr instead")
  125. << FixItHint::CreateReplacement(SourceRange(AutoPtrLoc, EndLoc),
  126. "unique_ptr");
  127. }
  128. } // namespace clang::tidy::modernize