CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
153 lines
5.4 KiB
C++
153 lines
5.4 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#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::tidy::modernize {
|
|
|
|
static constexpr char AutoPtrTokenId[] = "AutoPrTokenId";
|
|
static constexpr char AutoPtrOwnershipTransferId[] =
|
|
"AutoPtrOwnershipTransferId";
|
|
|
|
namespace {
|
|
|
|
/// 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; }
|
|
|
|
} // namespace
|
|
|
|
ReplaceAutoPtrCheck::ReplaceAutoPtrCheck(StringRef Name,
|
|
ClangTidyContext *Context)
|
|
: ClangTidyCheck(Name, Context),
|
|
Inserter(Options.getLocalOrGlobal("IncludeStyle",
|
|
utils::IncludeSorter::IS_LLVM),
|
|
areDiagsSelfContained()) {}
|
|
|
|
void ReplaceAutoPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
Options.store(Opts, "IncludeStyle", Inserter.getStyle());
|
|
}
|
|
|
|
void ReplaceAutoPtrCheck::registerMatchers(MatchFinder *Finder) {
|
|
auto AutoPtrDecl = recordDecl(hasName("auto_ptr"), isInStdNamespace());
|
|
auto AutoPtrType = hasCanonicalType(recordType(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))).bind(AutoPtrTokenId),
|
|
this);
|
|
|
|
// using std::auto_ptr;
|
|
// ^~~~~~~~~~~~~~~~~~~
|
|
Finder->addMatcher(usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(namedDecl(
|
|
hasName("auto_ptr"), isInStdNamespace()))))
|
|
.bind(AutoPtrTokenId),
|
|
this);
|
|
|
|
// Find ownership transfers via copy construction and assignment.
|
|
// AutoPtrOwnershipTransferId is bound to 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(
|
|
traverse(TK_AsIs,
|
|
cxxConstructExpr(hasType(AutoPtrType), argumentCountIs(1),
|
|
hasArgument(0, MovableArgumentMatcher))),
|
|
this);
|
|
}
|
|
|
|
void ReplaceAutoPtrCheck::registerPPCallbacks(const SourceManager &SM,
|
|
Preprocessor *PP,
|
|
Preprocessor *ModuleExpanderPP) {
|
|
Inserter.registerPreprocessor(PP);
|
|
}
|
|
|
|
void ReplaceAutoPtrCheck::check(const MatchFinder::MatchResult &Result) {
|
|
const SourceManager &SM = *Result.SourceManager;
|
|
if (const auto *E =
|
|
Result.Nodes.getNodeAs<Expr>(AutoPtrOwnershipTransferId)) {
|
|
const 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(), ")")
|
|
<< Inserter.createMainFileIncludeInsertion("<utility>");
|
|
|
|
return;
|
|
}
|
|
|
|
SourceLocation AutoPtrLoc;
|
|
if (const auto *PTL = Result.Nodes.getNodeAs<TypeLoc>(AutoPtrTokenId)) {
|
|
auto TL = *PTL;
|
|
if (auto QTL = TL.getAs<QualifiedTypeLoc>())
|
|
TL = QTL.getUnqualifiedLoc();
|
|
// std::auto_ptr<int> i;
|
|
// ^
|
|
if (auto Loc = TL.getAs<TemplateSpecializationTypeLoc>())
|
|
AutoPtrLoc = Loc.getTemplateNameLoc();
|
|
} else if (const auto *D =
|
|
Result.Nodes.getNodeAs<UsingDecl>(AutoPtrTokenId)) {
|
|
// using std::auto_ptr;
|
|
// ^
|
|
AutoPtrLoc = D->getNameInfo().getBeginLoc();
|
|
} else {
|
|
llvm_unreachable("Bad Callback. No node provided.");
|
|
}
|
|
|
|
if (AutoPtrLoc.isMacroID())
|
|
AutoPtrLoc = SM.getSpellingLoc(AutoPtrLoc);
|
|
|
|
// Ensure that only the 'auto_ptr' token is replaced and not the template
|
|
// aliases.
|
|
if (StringRef(SM.getCharacterData(AutoPtrLoc), strlen("auto_ptr")) !=
|
|
"auto_ptr")
|
|
return;
|
|
|
|
const SourceLocation EndLoc =
|
|
AutoPtrLoc.getLocWithOffset(strlen("auto_ptr") - 1);
|
|
diag(AutoPtrLoc, "auto_ptr is deprecated, use unique_ptr instead")
|
|
<< FixItHint::CreateReplacement(SourceRange(AutoPtrLoc, EndLoc),
|
|
"unique_ptr");
|
|
}
|
|
|
|
} // namespace clang::tidy::modernize
|