[PATCH] D48854: Use ExprMutationAnalyzer in performance-for-range-copy

Shuai Wang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 10 15:56:11 PDT 2018


This revision was automatically updated to reflect the committed changes.
shuaiwang marked an inline comment as done.
Closed by commit rL336737: Use ExprMutationAnalyzer in performance-for-range-copy (authored by shuaiwang, committed by ).
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

https://reviews.llvm.org/D48854

Files:
  clang-tools-extra/trunk/clang-tidy/performance/ForRangeCopyCheck.cpp
  clang-tools-extra/trunk/test/clang-tidy/performance-for-range-copy.cpp


Index: clang-tools-extra/trunk/clang-tidy/performance/ForRangeCopyCheck.cpp
===================================================================
--- clang-tools-extra/trunk/clang-tidy/performance/ForRangeCopyCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/performance/ForRangeCopyCheck.cpp
@@ -8,7 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "ForRangeCopyCheck.h"
-#include "../utils/DeclRefExprUtils.h"
+#include "../utils/ExprMutationAnalyzer.h"
 #include "../utils/FixItHintUtils.h"
 #include "../utils/TypeTraits.h"
 
@@ -79,8 +79,8 @@
       utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context);
   if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive)
     return false;
-  if (!utils::decl_ref_expr::isOnlyUsedAsConst(LoopVar, *ForRange.getBody(),
-                                               Context))
+  if (utils::ExprMutationAnalyzer(ForRange.getBody(), &Context)
+          .isMutated(&LoopVar))
     return false;
   diag(LoopVar.getLocation(),
        "loop variable is copied but only used as const reference; consider "
Index: clang-tools-extra/trunk/test/clang-tidy/performance-for-range-copy.cpp
===================================================================
--- clang-tools-extra/trunk/test/clang-tidy/performance-for-range-copy.cpp
+++ clang-tools-extra/trunk/test/clang-tidy/performance-for-range-copy.cpp
@@ -117,6 +117,11 @@
   ~Mutable() {}
 };
 
+struct Point {
+  ~Point() {}
+  int x, y;
+};
+
 Mutable& operator<<(Mutable &Out, bool B) {
   Out.setBool(B);
   return Out;
@@ -214,6 +219,15 @@
   }
 }
 
+void positiveOnlyAccessedFieldAsConst() {
+  for (auto UsedAsConst : View<Iterator<Point>>()) {
+    // CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]
+    // CHECK-FIXES: for (const auto& UsedAsConst : View<Iterator<Point>>()) {
+    use(UsedAsConst.x);
+    use(UsedAsConst.y);
+  }
+}
+
 void positiveOnlyUsedInCopyConstructor() {
   for (auto A : View<Iterator<Mutable>>()) {
     // CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48854.154900.patch
Type: text/x-patch
Size: 2299 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180710/0339390c/attachment.bin>


More information about the llvm-commits mailing list