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
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Matches expressions that are lvalues.
|
2015-08-25 21:03:43 +08:00
|
|
|
///
|
|
|
|
/// 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; }
|
|
|
|
|
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),
|
2020-07-27 19:48:53 +08:00
|
|
|
Inserter(Options.getLocalOrGlobal("IncludeStyle",
|
|
|
|
utils::IncludeSorter::IS_LLVM)) {}
|
2015-08-25 21:03:43 +08:00
|
|
|
|
|
|
|
void ReplaceAutoPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
2020-07-27 19:48:53 +08:00
|
|
|
Options.store(Opts, "IncludeStyle", Inserter.getStyle());
|
2015-08-25 21:03:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReplaceAutoPtrCheck::registerMatchers(MatchFinder *Finder) {
|
2020-10-18 23:02:11 +08:00
|
|
|
auto AutoPtrDecl = recordDecl(hasName("auto_ptr"), isInStdNamespace());
|
2017-05-17 20:57:06 +08:00
|
|
|
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(
|
2020-10-18 23:02:11 +08:00
|
|
|
hasName("auto_ptr"), isInStdNamespace()))))
|
2017-05-17 20:57:06 +08:00
|
|
|
.bind(AutoPtrTokenId),
|
|
|
|
this);
|
|
|
|
|
|
|
|
// Find ownership transfers via copy construction and assignment.
|
2020-01-03 02:41:43 +08:00
|
|
|
// AutoPtrOwnershipTransferId is bound to the part that has to be wrapped
|
2017-05-17 20:57:06 +08:00
|
|
|
// 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);
|
2019-11-12 23:15:56 +08:00
|
|
|
Finder->addMatcher(
|
2020-12-11 07:52:35 +08:00
|
|
|
traverse(TK_AsIs,
|
2019-11-12 23:15:56 +08:00
|
|
|
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) {
|
2020-07-27 19:48:53 +08:00
|
|
|
Inserter.registerPreprocessor(PP);
|
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;
|
|
|
|
|
2020-07-27 19:48:53 +08:00
|
|
|
auto Diag = diag(Range.getBegin(), "use std::move to transfer ownership")
|
|
|
|
<< FixItHint::CreateInsertion(Range.getBegin(), "std::move(")
|
|
|
|
<< FixItHint::CreateInsertion(Range.getEnd(), ")")
|
2020-09-28 20:58:27 +08:00
|
|
|
<< Inserter.createMainFileIncludeInsertion("<utility>");
|
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
|