2015-08-25 21:03:43 +08:00
|
|
|
//===--- ReplaceAutoPtrCheck.cpp - clang-tidy------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-08-25 21:03:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ReplaceAutoPtrCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace modernize {
|
|
|
|
|
2018-02-19 03:02:35 +08:00
|
|
|
namespace {
|
2015-10-22 17:48:23 +08:00
|
|
|
static const char AutoPtrTokenId[] = "AutoPrTokenId";
|
|
|
|
static const char AutoPtrOwnershipTransferId[] = "AutoPtrOwnershipTransferId";
|
2015-08-25 21:03:43 +08:00
|
|
|
|
|
|
|
/// \brief Matches expressions that are lvalues.
|
|
|
|
///
|
|
|
|
/// In the following example, a[0] matches expr(isLValue()):
|
|
|
|
/// \code
|
|
|
|
/// std::string a[2];
|
|
|
|
/// std::string b;
|
|
|
|
/// b = a[0];
|
|
|
|
/// b = "this string won't match";
|
|
|
|
/// \endcode
|
|
|
|
AST_MATCHER(Expr, isLValue) { return Node.getValueKind() == VK_LValue; }
|
|
|
|
|
|
|
|
/// Matches declarations whose declaration context is the C++ standard library
|
|
|
|
/// namespace std.
|
|
|
|
///
|
|
|
|
/// Note that inline namespaces are silently ignored during the lookup since
|
|
|
|
/// both libstdc++ and libc++ are known to use them for versioning purposes.
|
|
|
|
///
|
|
|
|
/// Given:
|
|
|
|
/// \code
|
|
|
|
/// namespace ns {
|
|
|
|
/// struct my_type {};
|
|
|
|
/// using namespace std;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// using std::vector;
|
|
|
|
/// using ns:my_type;
|
|
|
|
/// using ns::list;
|
|
|
|
/// \code
|
|
|
|
///
|
|
|
|
/// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(isFromStdNamespace())))
|
|
|
|
/// matches "using std::vector" and "using ns::list".
|
|
|
|
AST_MATCHER(Decl, isFromStdNamespace) {
|
|
|
|
const DeclContext *D = Node.getDeclContext();
|
|
|
|
|
|
|
|
while (D->isInlineNamespace())
|
|
|
|
D = D->getParent();
|
|
|
|
|
|
|
|
if (!D->isNamespace() || !D->getParent()->isTranslationUnit())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const IdentifierInfo *Info = cast<NamespaceDecl>(D)->getIdentifier();
|
|
|
|
|
|
|
|
return (Info && Info->isStr("std"));
|
|
|
|
}
|
|
|
|
|
2018-02-19 03:02:35 +08:00
|
|
|
} // namespace
|
|
|
|
|
2015-08-25 21:03:43 +08:00
|
|
|
ReplaceAutoPtrCheck::ReplaceAutoPtrCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
2016-05-03 10:54:05 +08:00
|
|
|
IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
|
2017-07-20 20:02:03 +08:00
|
|
|
Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
|
2015-08-25 21:03:43 +08:00
|
|
|
|
|
|
|
void ReplaceAutoPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
2016-05-03 10:54:05 +08:00
|
|
|
Options.store(Opts, "IncludeStyle",
|
|
|
|
utils::IncludeSorter::toString(IncludeStyle));
|
2015-08-25 21:03:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReplaceAutoPtrCheck::registerMatchers(MatchFinder *Finder) {
|
2015-08-29 01:58:10 +08:00
|
|
|
// Only register the matchers for C++; the functionality currently does not
|
|
|
|
// provide any benefit to other languages, despite being benign.
|
2017-05-17 20:57:06 +08:00
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto AutoPtrDecl = recordDecl(hasName("auto_ptr"), isFromStdNamespace());
|
|
|
|
auto AutoPtrType = qualType(hasDeclaration(AutoPtrDecl));
|
|
|
|
|
|
|
|
// std::auto_ptr<int> a;
|
|
|
|
// ^~~~~~~~~~~~~
|
|
|
|
//
|
|
|
|
// typedef std::auto_ptr<int> int_ptr_t;
|
|
|
|
// ^~~~~~~~~~~~~
|
|
|
|
//
|
|
|
|
// std::auto_ptr<int> fn(std::auto_ptr<int>);
|
|
|
|
// ^~~~~~~~~~~~~ ^~~~~~~~~~~~~
|
|
|
|
Finder->addMatcher(typeLoc(loc(qualType(AutoPtrType,
|
|
|
|
// Skip elaboratedType() as the named
|
|
|
|
// type will match soon thereafter.
|
|
|
|
unless(elaboratedType()))))
|
|
|
|
.bind(AutoPtrTokenId),
|
|
|
|
this);
|
|
|
|
|
|
|
|
// using std::auto_ptr;
|
|
|
|
// ^~~~~~~~~~~~~~~~~~~
|
2018-11-25 10:41:01 +08:00
|
|
|
Finder->addMatcher(usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(namedDecl(
|
2017-05-17 20:57:06 +08:00
|
|
|
hasName("auto_ptr"), isFromStdNamespace()))))
|
|
|
|
.bind(AutoPtrTokenId),
|
|
|
|
this);
|
|
|
|
|
|
|
|
// Find ownership transfers via copy construction and assignment.
|
|
|
|
// AutoPtrOwnershipTransferId is bound to the the part that has to be wrapped
|
|
|
|
// into std::move().
|
|
|
|
// std::auto_ptr<int> i, j;
|
|
|
|
// i = j;
|
|
|
|
// ~~~~^
|
|
|
|
auto MovableArgumentMatcher =
|
|
|
|
expr(isLValue(), hasType(AutoPtrType)).bind(AutoPtrOwnershipTransferId);
|
|
|
|
|
|
|
|
Finder->addMatcher(
|
|
|
|
cxxOperatorCallExpr(hasOverloadedOperatorName("="),
|
|
|
|
callee(cxxMethodDecl(ofClass(AutoPtrDecl))),
|
|
|
|
hasArgument(1, MovableArgumentMatcher)),
|
|
|
|
this);
|
|
|
|
Finder->addMatcher(cxxConstructExpr(hasType(AutoPtrType), argumentCountIs(1),
|
|
|
|
hasArgument(0, MovableArgumentMatcher)),
|
|
|
|
this);
|
2015-08-25 21:03:43 +08:00
|
|
|
}
|
|
|
|
|
2019-03-23 02:58:12 +08:00
|
|
|
void ReplaceAutoPtrCheck::registerPPCallbacks(const SourceManager &SM,
|
|
|
|
Preprocessor *PP,
|
|
|
|
Preprocessor *ModuleExpanderPP) {
|
2015-08-29 01:58:10 +08:00
|
|
|
// Only register the preprocessor callbacks for C++; the functionality
|
|
|
|
// currently does not provide any benefit to other languages, despite being
|
|
|
|
// benign.
|
2017-05-17 20:57:06 +08:00
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
2019-03-23 02:58:12 +08:00
|
|
|
Inserter = llvm::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
|
|
|
|
IncludeStyle);
|
|
|
|
PP->addPPCallbacks(Inserter->CreatePPCallbacks());
|
2015-08-25 21:03:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReplaceAutoPtrCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
SourceManager &SM = *Result.SourceManager;
|
|
|
|
if (const auto *E =
|
|
|
|
Result.Nodes.getNodeAs<Expr>(AutoPtrOwnershipTransferId)) {
|
|
|
|
CharSourceRange Range = Lexer::makeFileCharRange(
|
|
|
|
CharSourceRange::getTokenRange(E->getSourceRange()), SM, LangOptions());
|
|
|
|
|
|
|
|
if (Range.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto Diag = diag(Range.getBegin(), "use std::move to transfer ownership")
|
|
|
|
<< FixItHint::CreateInsertion(Range.getBegin(), "std::move(")
|
|
|
|
<< FixItHint::CreateInsertion(Range.getEnd(), ")");
|
|
|
|
|
2017-05-17 20:57:06 +08:00
|
|
|
if (auto Fix =
|
|
|
|
Inserter->CreateIncludeInsertion(SM.getMainFileID(), "utility",
|
|
|
|
/*IsAngled=*/true))
|
|
|
|
Diag << *Fix;
|
2015-08-25 21:03:43 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-17 20:57:06 +08:00
|
|
|
SourceLocation AutoPtrLoc;
|
2015-08-25 21:03:43 +08:00
|
|
|
if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>(AutoPtrTokenId)) {
|
2017-05-17 20:57:06 +08:00
|
|
|
// std::auto_ptr<int> i;
|
|
|
|
// ^
|
|
|
|
if (auto Loc = TL->getAs<TemplateSpecializationTypeLoc>())
|
|
|
|
AutoPtrLoc = Loc.getTemplateNameLoc();
|
2015-08-25 21:03:43 +08:00
|
|
|
} else if (const auto *D =
|
|
|
|
Result.Nodes.getNodeAs<UsingDecl>(AutoPtrTokenId)) {
|
2017-05-17 20:57:06 +08:00
|
|
|
// using std::auto_ptr;
|
|
|
|
// ^
|
|
|
|
AutoPtrLoc = D->getNameInfo().getBeginLoc();
|
2015-08-25 21:03:43 +08:00
|
|
|
} else {
|
|
|
|
llvm_unreachable("Bad Callback. No node provided.");
|
|
|
|
}
|
|
|
|
|
2017-05-17 20:57:06 +08:00
|
|
|
if (AutoPtrLoc.isMacroID())
|
|
|
|
AutoPtrLoc = SM.getSpellingLoc(AutoPtrLoc);
|
2015-08-25 21:03:43 +08:00
|
|
|
|
|
|
|
// Ensure that only the 'auto_ptr' token is replaced and not the template
|
|
|
|
// aliases.
|
2017-05-17 20:57:06 +08:00
|
|
|
if (StringRef(SM.getCharacterData(AutoPtrLoc), strlen("auto_ptr")) !=
|
|
|
|
"auto_ptr")
|
2015-08-25 21:03:43 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
SourceLocation EndLoc =
|
2017-05-17 20:57:06 +08:00
|
|
|
AutoPtrLoc.getLocWithOffset(strlen("auto_ptr") - 1);
|
|
|
|
diag(AutoPtrLoc, "auto_ptr is deprecated, use unique_ptr instead")
|
|
|
|
<< FixItHint::CreateReplacement(SourceRange(AutoPtrLoc, EndLoc),
|
2015-08-25 21:03:43 +08:00
|
|
|
"unique_ptr");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace modernize
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|