-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[clang-tidy] Add performance-explicit-move-constructor
check
#122599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2f1627e
2f0b722
56acf54
d3a09b5
9280db3
8284612
22c9180
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//===--- ExplicitMoveConstructorCheck.cpp - clang-tidy --------------------===// | ||
// | ||
// 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 "ExplicitMoveConstructorCheck.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
#include "clang/Lex/Lexer.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::performance { | ||
|
||
static SourceRange findExplicitToken(const CXXConstructorDecl *Ctor, | ||
const SourceManager &Source, | ||
const LangOptions &LangOpts) { | ||
SourceLocation CurrentLoc = Ctor->getBeginLoc(); | ||
const SourceLocation EndLoc = Ctor->getEndLoc(); | ||
Token Tok; | ||
|
||
do { | ||
const bool failed = Lexer::getRawToken(CurrentLoc, Tok, Source, LangOpts); | ||
|
||
if (failed) | ||
return {}; | ||
|
||
if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "explicit") | ||
return {Tok.getLocation(), Tok.getEndLoc()}; | ||
|
||
CurrentLoc = Tok.getEndLoc(); | ||
} while (Tok.isNot(tok::eof) && CurrentLoc < EndLoc); | ||
|
||
return {}; | ||
} | ||
|
||
void ExplicitMoveConstructorCheck::registerMatchers(MatchFinder *Finder) { | ||
Finder->addMatcher( | ||
traverse( | ||
TK_IgnoreUnlessSpelledInSource, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move the traversal kind selection into getCheckTraversalKind instead |
||
cxxRecordDecl( | ||
has(cxxConstructorDecl(isMoveConstructor(), isExplicit(), | ||
unless(isDeleted())) | ||
.bind("move-ctor")), | ||
has(cxxConstructorDecl(isCopyConstructor(), unless(isDeleted())) | ||
.bind("copy-ctor")), | ||
unless(isExpansionInSystemHeader()))), | ||
this); | ||
} | ||
|
||
void ExplicitMoveConstructorCheck::check( | ||
const MatchFinder::MatchResult &Result) { | ||
const auto *MoveCtor = | ||
Result.Nodes.getNodeAs<CXXConstructorDecl>("move-ctor"); | ||
const auto *CopyCtor = | ||
Result.Nodes.getNodeAs<CXXConstructorDecl>("copy-ctor"); | ||
|
||
if (!MoveCtor || !CopyCtor) | ||
return; | ||
|
||
auto Diag = | ||
diag(MoveCtor->getLocation(), | ||
"copy constructor may be called instead of move constructor"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would write |
||
const SourceRange ExplicitTokenRange = | ||
findExplicitToken(MoveCtor, *Result.SourceManager, getLangOpts()); | ||
|
||
if (ExplicitTokenRange.isInvalid()) | ||
return; | ||
|
||
Diag << FixItHint::CreateRemoval( | ||
CharSourceRange::getCharRange(ExplicitTokenRange)); | ||
} | ||
|
||
} // namespace clang::tidy::performance |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===--- ExplicitMoveConstructorCheck.h - clang-tidy ------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_EXPLICITMOVECONSTRUCTORCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_EXPLICITMOVECONSTRUCTORCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::performance { | ||
|
||
/// Find classes that define an explicit move constructor and a (non-deleted) | ||
/// copy constructor. | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/performance/explicit-move-constructor.html | ||
class ExplicitMoveConstructorCheck : public ClangTidyCheck { | ||
public: | ||
ExplicitMoveConstructorCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus11; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::performance | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_EXPLICITMOVECONSTRUCTORCHECK_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
.. title:: clang-tidy - performance-explicit-move-constructor | ||
|
||
performance-explicit-move-constructor | ||
===================================== | ||
|
||
Warns when a class defines an explicit move constructor, which may cause | ||
the copy constructor to get called instead. | ||
|
||
Example: | ||
|
||
.. code-block:: c++ | ||
|
||
class Expensive { | ||
public: | ||
// ... | ||
Expensive(const Expensive&) { /* ... */ } | ||
explicit Expensive(Expensive&&) { /* ... */ } | ||
}; | ||
|
||
void process(Expensive); | ||
|
||
int main() { | ||
Expensive exp{}; | ||
process(std::move(exp)); | ||
|
||
return 0; | ||
} | ||
|
||
Here, the call to ``process`` is actually going to copy ``exp`` instead of | ||
moving it, potentially incurring a performance penalty if copying is expensive. | ||
No warning will be emitted if the copy constructor is deleted, as any call to | ||
it would make the program fail to compile. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: missing newline at end of file |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// RUN: %check_clang_tidy %s performance-explicit-move-constructor %t | ||
|
||
struct Empty {}; | ||
|
||
class NotReported1 {}; | ||
|
||
class NotReported2 { | ||
public: | ||
NotReported2(NotReported2&&) = default; | ||
NotReported2(const NotReported2&) = default; | ||
}; | ||
|
||
class NotReported3 { | ||
public: | ||
explicit NotReported3(NotReported3&&) = default; | ||
}; | ||
|
||
class NotReported4 { | ||
public: | ||
explicit NotReported4(NotReported4&&) = default; | ||
NotReported4(const NotReported4&) = delete; | ||
}; | ||
|
||
class NotReported5 { | ||
public: | ||
explicit NotReported5(NotReported5&&) = delete; | ||
NotReported5(const NotReported5&) = default; | ||
}; | ||
|
||
class Reported1 { | ||
public: | ||
explicit Reported1(Reported1&&) = default; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: copy constructor may be called instead of move constructor [performance-explicit-move-constructor] | ||
// CHECK-FIXES: {{^ }}Reported1(Reported1&&) = default;{{$}} | ||
Reported1(const Reported1&) = default; | ||
}; | ||
|
||
template <typename> | ||
class Reported2 { | ||
public: | ||
explicit Reported2(Reported2&&) = default; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: copy constructor may be called instead of move constructor [performance-explicit-move-constructor] | ||
// CHECK-FIXES: {{^ }}Reported2(Reported2&&) = default;{{$}} | ||
Reported2(const Reported2&) = default; | ||
}; | ||
|
||
template <typename T> | ||
class Reported3 : public T { | ||
public: | ||
explicit Reported3(Reported3&&) = default; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: copy constructor may be called instead of move constructor [performance-explicit-move-constructor] | ||
// CHECK-FIXES: {{^ }}Reported3(Reported3&&) = default;{{$}} | ||
Reported3(const Reported3&) = default; | ||
}; | ||
|
||
template <typename T> | ||
class Reported4 { | ||
public: | ||
explicit Reported4(Reported4&&) = default; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: copy constructor may be called instead of move constructor [performance-explicit-move-constructor] | ||
// CHECK-FIXES: {{^ }}Reported4(Reported4&&) = default;{{$}} | ||
Reported4(const Reported4&) = default; | ||
|
||
T member; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test with inherited constructors and with template classes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have some existing utils function, you should be able to use one of these instead https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clang-tidy/utils/LexerUtils.h