[PATCH] D90042: [clang-tidy] performance-unnecessary-copy-initialization: Check for const reference arguments that are replaced template parameter type.
Felix Berger via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 23 06:48:16 PDT 2020
flx created this revision.
flx added reviewers: aaron.ballman, gribozavr2.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.
flx requested review of this revision.
This fixes false positive cases where a non-const reference is passed to a
std::function but interpreted as a const reference.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D90042
Files:
clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-copy-initialization.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-copy-initialization.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-copy-initialization.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-copy-initialization.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s performance-unnecessary-copy-initialization %t
+// RUN: %check_clang_tidy -std=c++17 %s performance-unnecessary-copy-initialization %t
struct ExpensiveToCopyType {
ExpensiveToCopyType();
@@ -411,12 +411,12 @@
template <class>
class function;
-template <class R, class... Args>
-class function<R(Args...)> {
+template <class R, class... ArgTypes>
+class function<R(ArgTypes...)> {
public:
function();
- function(const function &other);
- R operator()(Args &&...args) const;
+ function(const function &Other);
+ R operator()(ArgTypes &&...Args) const;
};
} // namespace __1
@@ -460,3 +460,19 @@
}
} // namespace fake
+
+void positiveInvokedOnStdFunction(
+ std::function<void(const ExpensiveToCopyType &)> Update,
+ const ExpensiveToCopyType Orig) {
+ auto Copy = Orig.reference();
+ // CHECK-MESSAGES: [[@LINE-1]]:8: warning: the variable 'Copy' is copy-constructed from a const reference
+ // CHECK-FIXES: const auto& Copy = Orig.reference();
+ Update(Copy);
+}
+
+void negativeInvokedOnStdFunction(
+ std::function<void(ExpensiveToCopyType &)> Update,
+ const ExpensiveToCopyType Orig) {
+ auto Copy = Orig.reference();
+ Update(Copy);
+}
Index: clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
===================================================================
--- clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
+++ clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
@@ -57,11 +57,15 @@
Stmt, Context);
SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
extractNodesByIdTo(Matches, "declRef", DeclRefs);
- auto ConstReferenceOrValue =
- qualType(anyOf(referenceType(pointee(qualType(isConstQualified()))),
- unless(anyOf(referenceType(), pointerType()))));
+ auto ConstReferenceOrValue = qualType(
+ anyOf(referenceType(pointee(qualType(isConstQualified()))),
+ unless(anyOf(referenceType(),
+ pointerType() /*, substTemplateTypeParmType()*/))));
+ auto ConstReferenceOrValueOrReplaced = qualType(anyOf(
+ ConstReferenceOrValue,
+ substTemplateTypeParmType(hasReplacementType(ConstReferenceOrValue))));
auto UsedAsConstRefOrValueArg = forEachArgumentWithParam(
- DeclRefToVar, parmVarDecl(hasType(ConstReferenceOrValue)));
+ DeclRefToVar, parmVarDecl(hasType(ConstReferenceOrValueOrReplaced)));
Matches = match(findAll(callExpr(UsedAsConstRefOrValueArg)), Stmt, Context);
extractNodesByIdTo(Matches, "declRef", DeclRefs);
Matches =
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90042.300272.patch
Type: text/x-patch
Size: 2911 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201023/8f0e5120/attachment.bin>
More information about the cfe-commits
mailing list